Skip to content
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

Closed
mdanialr opened this issue Jan 2, 2022 · 5 comments
Closed

Modify body request in service. #593

mdanialr opened this issue Jan 2, 2022 · 5 comments

Comments

@mdanialr
Copy link

mdanialr commented Jan 2, 2022

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.

@crookse
Copy link
Member

crookse commented Jan 2, 2022

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 (Drash.Request.create()) would be able to do it, but ultimately if you modify the request in the service, the modified request doesn't make it to the resource and the resource uses the original request that came in.

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}`);

@mdanialr
Copy link
Author

mdanialr commented Jan 2, 2022

hi @crookse, thanks for the quick response, indeed this is a nice workaround although it won't as flexible as Service.
I have already tried that snippet but, i noticed that it will return a new object and override the original request. What i expected is to modify the value so i need to changes

if (body.hello) {
    return {
        Hello: "World",
    }
}

to

if (body.hello) {
    body.hello = "new value";
}

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.

@mdanialr
Copy link
Author

mdanialr commented Jan 2, 2022

we could separate validation, sanitizing input things, etc. from resources if this could be implemented in Service

@crookse
Copy link
Member

crookse commented Jan 2, 2022

@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.

@mdanialr
Copy link
Author

mdanialr commented Jan 2, 2022

@crookse that's great. Thanks for the quick response.

@mdanialr mdanialr closed this as completed Jan 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants