From b8eba15f4c34d1047998e15d844e68ccedc1c977 Mon Sep 17 00:00:00 2001 From: Greg McKelvey Date: Wed, 2 Mar 2022 13:58:14 -0800 Subject: [PATCH 1/4] add generic to GatsbyFunctionRequest --- packages/gatsby/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts index dc9b48971ac09..2fdd94a2b8788 100644 --- a/packages/gatsby/index.d.ts +++ b/packages/gatsby/index.d.ts @@ -1648,7 +1648,7 @@ export interface GatsbyFunctionResponse extends ServerResponse { /** * Gatsby function route request */ -export interface GatsbyFunctionRequest extends IncomingMessage { +export interface GatsbyFunctionRequest extends IncomingMessage { /** * Object of values from URL query parameters (after the ? in the URL) */ @@ -1658,7 +1658,7 @@ export interface GatsbyFunctionRequest extends IncomingMessage { * Object of values from route parameters */ params: Record - body: any + body: Partial /** * Object of `cookies` from header */ From e01a85771c376334c819640d992d278c0c2d280a Mon Sep 17 00:00:00 2001 From: Greg McKelvey Date: Fri, 11 Mar 2022 10:32:29 -0800 Subject: [PATCH 2/4] change GatsbyFunctionRequest type body to follow express pattern --- packages/gatsby/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts index 2fdd94a2b8788..a4ad27b79b5cb 100644 --- a/packages/gatsby/index.d.ts +++ b/packages/gatsby/index.d.ts @@ -1648,7 +1648,7 @@ export interface GatsbyFunctionResponse extends ServerResponse { /** * Gatsby function route request */ -export interface GatsbyFunctionRequest extends IncomingMessage { +export interface GatsbyFunctionRequest extends IncomingMessage { /** * Object of values from URL query parameters (after the ? in the URL) */ @@ -1658,7 +1658,7 @@ export interface GatsbyFunctionRequest extends IncomingMessage { * Object of values from route parameters */ params: Record - body: Partial + body: ReqBody /** * Object of `cookies` from header */ From deb6ae60caf9d2a37a2ed353e97061cdef4d9573 Mon Sep 17 00:00:00 2001 From: Greg McKelvey Date: Fri, 11 Mar 2022 10:36:50 -0800 Subject: [PATCH 3/4] update Gatsby function typescript example docs --- docs/docs/reference/functions/getting-started.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/docs/reference/functions/getting-started.md b/docs/docs/reference/functions/getting-started.md index 9051f6222126b..61c9d8711c397 100644 --- a/docs/docs/reference/functions/getting-started.md +++ b/docs/docs/reference/functions/getting-started.md @@ -48,11 +48,15 @@ Functions can be written in JavaScript or Typescript. ```ts:title=src/api/typescript.ts import { GatsbyFunctionRequest, GatsbyFunctionResponse } from "gatsby" +interface RequestBody { + message: string +} + export default function handler( - req: GatsbyFunctionRequest, + req: GatsbyFunctionRequest, res: GatsbyFunctionResponse ) { - res.send(`I am TYPESCRIPT`) + res.send({ title: `I am TYPESCRIPT`, message: req.body.message }) } ``` From a5145c4dc8483a3752b863b86c97af6bbfe2d353 Mon Sep 17 00:00:00 2001 From: Lennart Date: Mon, 14 Mar 2022 11:35:08 +0100 Subject: [PATCH 4/4] Update getting-started.md --- docs/docs/reference/functions/getting-started.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/reference/functions/getting-started.md b/docs/docs/reference/functions/getting-started.md index 61c9d8711c397..11af1364185b3 100644 --- a/docs/docs/reference/functions/getting-started.md +++ b/docs/docs/reference/functions/getting-started.md @@ -48,7 +48,7 @@ Functions can be written in JavaScript or Typescript. ```ts:title=src/api/typescript.ts import { GatsbyFunctionRequest, GatsbyFunctionResponse } from "gatsby" -interface RequestBody { +interface ContactBody { message: string }