-
Notifications
You must be signed in to change notification settings - Fork 32
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
Modify body request in service. #593
Comments
hey @mdanialr ! so i looked into this and it's currently not possible to do it in a service. the request body is unmodifiable. i checked to see if our internal API ( it is possible to use a base resource class for this though. not sure if it will meet your needs, but the implementation would be something like: import * as Drash from "https://deno.land/x/drash@v2.3.0/mod.ts";
class BaseResourceThatModifiesRequests extends Drash.Resource {
protected formatBody(req: Drash.Request): any {
const body = req.bodyAll() as any;
if (!body) {
return;
}
if (body.hello) {
return {
Hello: "World",
}
}
return body;
}
}
class MyResource extends BaseResourceThatModifiesRequests {
public paths = ["/"];
public POST(req: Drash.Request, res: Drash.Response): void {
const body = this.formatBody(req);
// Body gets changed from { hello: "world" } to { Hello: "World" }
console.log(body);
}
}
// Create your server and plug in dexter to the middleware config
const server = new Drash.Server({
resources: [
MyResource,
],
hostname: "0.0.0.0",
port: 1447,
protocol: "http",
});
server.run();
console.log(`Server running at ${server.address}`); |
hi @crookse, thanks for the quick response, indeed this is a nice workaround although it won't as flexible as Service.
to
thus will only modify certain fields without changing anything other than that. One more thing, i wonder if this would be added to the roadmap in the future?Thank you. |
we could separate validation, sanitizing input things, etc. from resources if this could be implemented in Service |
@mdanialr, i agree it's not as flexible as a service. there's currently an open issue (#586) that will introduce validation and should be released before march (we have it slotted for quarter 1 this year). the issue says "OpenAPI / Swagger", but it will include validation code in a separate service that can be used without having to import the OpenAPI service. |
@crookse that's great. Thanks for the quick response. |
Summary
What is your question?
Since i didn't find it in docs, so i will ask it here.
How to modify body requests in service say like in
runBeforeResource()
?Example scenario:
I want to transform certain body requests to the expected format or something similar.
Thanks.
The text was updated successfully, but these errors were encountered: