Skip to content

Commit ea61f78

Browse files
authored
extending express.request to include rawBody (#420)
1 parent 1183c67 commit ea61f78

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

spec/providers/https.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ interface RunHandlerResult {
7171
* and response are properly converted to their http equivalents.
7272
*/
7373
interface CallTest {
74-
// An http request, mocking a subset of express.Request.
74+
// An http request, mocking a subset of https.Request.
7575
httpRequest: any;
7676

7777
// The expected format of the request passed to the handler.
@@ -90,7 +90,7 @@ interface CallTest {
9090
*/
9191
function runHandler(
9292
handler: express.Handler,
93-
request: express.Request
93+
request: https.Request
9494
): Promise<RunHandlerResult> {
9595
return new Promise((resolve, reject) => {
9696
// MockResponse mocks an express.Response.
@@ -145,7 +145,7 @@ async function runTest(test: CallTest): Promise<any> {
145145
expect(response.status).to.equal(test.expectedHttpResponse.status);
146146
}
147147

148-
// MockRequest mocks an express.Request.
148+
// MockRequest mocks an https.Request.
149149
class MockRequest {
150150
public method: 'POST' | 'GET' | 'OPTIONS' = 'POST';
151151

src/function-builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export class FunctionBuilder {
152152
* same signature as an Express app.
153153
*/
154154
onRequest: (
155-
handler: (req: express.Request, resp: express.Response) => void
155+
handler: (req: https.Request, resp: express.Response) => void
156156
) => https._onRequestWithOpts(handler, this.options),
157157
/**
158158
* Declares a callable method for clients to call using a Firebase SDK.

src/providers/https.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,20 @@ import { apps } from '../apps';
2828
import { HttpsFunction, optsToTrigger, Runnable } from '../cloud-functions';
2929
import { DeploymentOptions } from '../function-builder';
3030

31+
/**
32+
*
33+
*
34+
*/
35+
export interface Request extends express.Request {
36+
rawBody: Buffer;
37+
}
3138
/**
3239
* Handle HTTP requests.
3340
* @param handler A function that takes a request and response object,
3441
* same signature as an Express app.
3542
*/
3643
export function onRequest(
37-
handler: (req: express.Request, resp: express.Response) => void
44+
handler: (req: Request, resp: express.Response) => void
3845
) {
3946
return _onRequestWithOpts(handler, {});
4047
}
@@ -51,11 +58,11 @@ export function onCall(
5158

5259
/** @internal */
5360
export function _onRequestWithOpts(
54-
handler: (req: express.Request, resp: express.Response) => void,
61+
handler: (req: Request, resp: express.Response) => void,
5562
opts: DeploymentOptions
5663
): HttpsFunction {
5764
// lets us add __trigger without altering handler:
58-
let cloudFunction: any = (req: express.Request, res: express.Response) => {
65+
let cloudFunction: any = (req: Request, res: express.Response) => {
5966
handler(req, res);
6067
};
6168
cloudFunction.__trigger = _.assign(optsToTrigger(opts), { httpsTrigger: {} });
@@ -267,11 +274,11 @@ export interface CallableContext {
267274
/**
268275
* The raw request handled by the callable.
269276
*/
270-
rawRequest: express.Request;
277+
rawRequest: Request;
271278
}
272279

273280
// The allowed interface for an http request for a callable function.
274-
interface HttpRequest extends express.Request {
281+
interface HttpRequest extends Request {
275282
body: {
276283
data: any;
277284
};
@@ -284,7 +291,7 @@ interface HttpResponseBody {
284291
}
285292

286293
// Returns true if req is a properly formatted callable request.
287-
function isValidRequest(req: express.Request): req is HttpRequest {
294+
function isValidRequest(req: Request): req is HttpRequest {
288295
// The body must not be empty.
289296
if (!req.body) {
290297
console.warn('Request is missing body.');
@@ -416,7 +423,7 @@ export function _onCallWithOpts(
416423
handler: (data: any, context: CallableContext) => any | Promise<any>,
417424
opts: DeploymentOptions
418425
): HttpsFunction & Runnable<any> {
419-
const func = async (req: express.Request, res: express.Response) => {
426+
const func = async (req: Request, res: express.Response) => {
420427
try {
421428
if (!isValidRequest(req)) {
422429
console.error('Invalid request', req);
@@ -477,7 +484,7 @@ export function _onCallWithOpts(
477484
};
478485

479486
// Wrap the function with a cors handler.
480-
const corsFunc: any = (req: express.Request, res: express.Response) => {
487+
const corsFunc: any = (req: Request, res: express.Response) => {
481488
return corsHandler(req, res, () => func(req, res));
482489
};
483490

0 commit comments

Comments
 (0)