GoogleCloudPlatform / GoogleCloudPlatform/functions-framework-nodejs

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

Offen
#198 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
enhancement P3
Vorherrschende Sprache
TypeScript
Sterne
1.4k
Forks
181
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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

```ts
// 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'` when implement handler which uses Request.rawBody attribute like below:

```ts
import {Response, Request} from 'express';

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

As a workaround I have to create and use my own type instead of native Express Request:
```ts
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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginnen Sie mit src/invoker.ts und vergleichen Sie dessen implizite Erweiterung von Express Request mit der expliziten Request-Erweiterung, die in src/providers/https.ts von firebase-functions verwendet wird. Bestimmen Sie, wie das Framework rawBody für abhängige TypeScript-Anwendungen bereitstellen sollte, und überprüfen Sie anschließend, dass ein Handler, der den bereitgestellten Request-Typ verwendet, ohne lokale Behelfslösung auf request.rawBody zugreifen kann.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
express, node.js, typescript
Bereich
api, backend
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.