-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to get the validateFunc returned data in pre hook of internal API made by Rest-Hapi. #19
Comments
Hi @parveen-oodles, this is actually a feature that is being worked on and hopefully will be done soon. For now, you can omit the generated create endpoint and replace it with your own in order to access the request object. The resulting code would look something like this: 'use strict';
const RestHapi = require('rest-hapi');
module.exports = function (server, mongoose, logger) {
server.route({
method: 'POST',
path: '/pathName',
config: {
handler: function(request, reply) {
/* modify request.payload here and access auth info through request.auth.credentials */
const Model = mongoose.model('modelName');
return RestHapi.create(Model, request.payload, logger)
.then(function (result) {
return reply(result);
})
.catch(function (error) {
return reply(error);
});
},
tags: ['api'],
plugins: {
'hapi-swagger': {}
}
}
});
}; |
Thanks JKHeadley for your quick response. I really appreciate that. |
Hi Parveen,
Did you resolve your issue? I didn't see you comment in github.
It looks like you are setting the allowCreate property correctly, however
the standalone endpoint should be in a separate file in an "api" directory,
not in your model directory/file. Please review the docs for an example:
https://github.com/JKHeadley/rest-hapi/blob/master/README.md#standalone-endpoints
…On Mon, Jun 19, 2017 at 1:04 AM Parveen ***@***.***> wrote:
@JKHeadley <https://github.com/jkheadley> , I set the property of
allowCreate to false for one of my model for which i don't want to create
API implicitly. After that I have created a standalone endpoint as
suggested by you for the create(POST) call for that particular model but
after that it will not creating the API endpoint , also it will not
generating the DOC for the same. As in doc mentioned :-
NOTE: If your models directory is not in your projects root directory, you
will need to specify the path (relative to your projects root directory) by
assigning the path to the config.modelPath property and you will need to
set the config.absoluteModelPath property to true.
so i have also set the apiPath and also set the absoluteApiPath to true
because my model directory is not in the root directory. But still its not
worked . My code is below:-
module.exports = function(mongoose) {
var Schema = new mongoose.Schema({
name: {
type: Types.String,
}
}, {
collection: modelName
});
Schema.statics = {
collectionName: modelName,
routeOptions: {
allowCreate: false,
function(server, mongoose, logger) {
var createBuildingHandler = function(request, reply) {
const Model = mongoose.model('building');
return RestHapi.create(Model, request.payload, logger)
.then(function(result) {
return reply(result);
})
.catch(function(error) {
return reply(error);
});
},
server.route({
method: 'POST',
path: '/building',
config: {
apiPath: config.get('/restHapiConfig/apiPath'),
absoluteApiPath: true,
handler: createBuildingHandler,
description: 'Create new Building',
tags: ['api', 'building'],
plugins: {
'hapi-swagger': {
responseMessages: [{
code: 200,
message: 'Success'
}, {
code: 400,
message: 'Bad Request'
}, {
code: 404,
message: 'Not Found'
}, {
code: 500,
message: 'Internal Server Error'
}]
}
}
}
});
}
}
}
return Schema;
}
And the config.js is as follows:-
restHapiConfig: {
absoluteApiPath: true,
apiPath: __dirname + '/api/api',
......
...... // some other config
}
Please let me know where i am doing the wrong thing. Is is there anything
i am missing that must be included.
Thanks
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#19 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AMC_f2XXZdIXFgaHYBRo4uMlZN0ojV9iks5sFit3gaJpZM4N6zou>
.
|
Thanks JKHeadley, yes i have resolved my issue, that was due to the standalone endpoint wasn't in the separate file. I have another query regarding Rest-Hapi would appreciate if you please tell me that via association there are some more API created for that particular model, but as Rest-Hapi create API for them is that also expose some internal methods to do the same work via server side i mean like there are method like restHapi.create(modelName, data, log), that will do the same work that the create API endpoint do. So like this is rest-hapi also expose some methods at server side for association API made internally by that.
(This API is generated by rest Hapi Association), so how can i do the same in server side is there any method expose at server side like this :-
Thanks |
Hi, I am just wondering to know that,
How can we add some extra info in PAYLOAD object in auth (validateFunc) function so that we can use that in pre (create) hook in internal API (generated by Rest-Hapi). Because as i want some user info for some validation in my pre hook, like:-
but as i only able to get the payload there so how can i get the additional info as well.
You can get the detail question i posted over stackoverflow:-
https://stackoverflow.com/questions/44546596/how-to-get-result-of-validatefunc-in-pre-of-auto-created-api-rest-hapi
Please help me regarding this ASAP. Thanks
The text was updated successfully, but these errors were encountered: