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

Implicit Request interface extension causes error 'property rawBody does not exist on type' #198

Open
dobromyslov opened this issue Aug 14, 2020 · 2 comments

Comments

@dobromyslov
Copy link

dobromyslov commented Aug 14, 2020

Cloud functions implicitly extend express Request interface with rawBody (see https://github.com/GoogleCloudPlatform/functions-framework-nodejs/blob/master/src/invoker.ts#L44):

// We optionally annotate the express Request with a rawBody field.
// Express leaves the Express namespace open to allow merging of new fields.
declare global {
  // eslint-disable-next-line @typescript-eslint/no-namespace
  namespace Express {
    export interface Request {
      rawBody?: Buffer;
    }
  }
}

I get error Property 'rawBody' does not exist on type 'Request<ParamsDictionary>' when implement handler which uses Request.rawBody attribute like below:

import {Response, Request} from 'express';

export class HandlerClass{
  public async http(request: Request, response: Response): Promise<void> {
    const body = request.rawBody;
    ...
  }
}

As a workaround I have to create and use my own type instead of native Express Request:

import * as express from 'express';

export interface Request extends express.Request {
  rawBody: Buffer;
}

If you have a look at the firebase-functions repository then you will see they use explicit extension of Request interface and then use it in code (https://github.com/firebase/firebase-functions/blob/master/src/providers/https.ts#L33)

I think functions-framework-nodejs should change implicit interface extension to explicit and provide stand-alone Request with rawBody attribute to avoid this workaround mess in depended applications code.

@dobromyslov dobromyslov changed the title Implicit Request interface extension causes error property rawBody does not exists on type Implicit Request interface extension causes error 'property rawBody does not exists on type' Aug 14, 2020
@dobromyslov dobromyslov changed the title Implicit Request interface extension causes error 'property rawBody does not exists on type' Implicit Request interface extension causes error 'property rawBody does not exist on type' Aug 14, 2020
@grant
Copy link
Contributor

grant commented Aug 16, 2020

Hey @dobromyslov, yeah that does look like an error with the TS interfaces.

I believe this workaround was just to fix the TS compiler, I don't think we really expected anybody to use our Request interface.

Want to send a PR, just changing the interfaces?

@dobromyslov
Copy link
Author

Yes, I will prepare PR and send it in a couple of days.

I implemented custom handling of rawBody in TypeScript and found this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants