From 799acf4f90e576d808640793b8c4b2f883d662f9 Mon Sep 17 00:00:00 2001 From: Josh Date: Wed, 5 Jan 2022 16:56:49 -0500 Subject: [PATCH 1/4] Add types for getServerData --- packages/gatsby/index.d.ts | 29 ++++++++++++++++++++ packages/gatsby/src/utils/get-server-data.ts | 18 ++---------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts index ff3279321e4bf..50346bf4088cc 100644 --- a/packages/gatsby/index.d.ts +++ b/packages/gatsby/index.d.ts @@ -146,6 +146,35 @@ export type PageProps< serverData: ServerDataType } +/** + * Props object passed into the [getServerData](https://www.gatsbyjs.com/docs/reference/rendering-options/server-side-rendering/) function. + */ +export type GetServerDataProps = { + headers: Map + method: string + url: string + query?: Record + params?: Record + pageContext: Record +}; + +/** + * The return type (promise payload) from the [getServerData](https://www.gatsbyjs.com/docs/reference/rendering-options/server-side-rendering/) function. + */ +export type GetServerDataReturn> = { + headers?: Map + props?: ServerDataType + status?: number +}; + +/** + * A shorthand type for combining the props and return type for the [getServerData](https://www.gatsbyjs.com/docs/reference/rendering-options/server-side-rendering/) function. + */ +export type GetServerData = (props: GetServerDataProps) => Promise>; + +/** + * Constructor arguments for the PageRenderer. + */ export interface PageRendererProps { location: WindowLocation } diff --git a/packages/gatsby/src/utils/get-server-data.ts b/packages/gatsby/src/utils/get-server-data.ts index 67c1d96d40d74..b186cc23a93b2 100644 --- a/packages/gatsby/src/utils/get-server-data.ts +++ b/packages/gatsby/src/utils/get-server-data.ts @@ -2,22 +2,10 @@ import type { Request } from "express" import type { IGatsbyPage } from "../redux/types" import { match } from "@gatsbyjs/reach-router/lib/utils" - -export interface IServerData { - headers?: Record - props?: Record - status?: number -} +import { GetServerData, GetServerDataReturn } from "../.." interface IModuleWithServerData { - getServerData?: (args: { - headers: Map - method: string - url: string - query?: Record - params?: Record - pageContext: Record - }) => Promise + getServerData?: GetServerData> } export async function getServerData( @@ -27,7 +15,7 @@ export async function getServerData( page: IGatsbyPage, pagePath: string, mod: IModuleWithServerData | undefined -): Promise { +): Promise>> { if (!mod?.getServerData) { return {} } From 50c3339febf82a8e726abae35d3b2beb7558f12d Mon Sep 17 00:00:00 2001 From: Josh Date: Wed, 5 Jan 2022 16:56:49 -0500 Subject: [PATCH 2/4] Add types for getServerData --- packages/gatsby/index.d.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts index ff3279321e4bf..50346bf4088cc 100644 --- a/packages/gatsby/index.d.ts +++ b/packages/gatsby/index.d.ts @@ -146,6 +146,35 @@ export type PageProps< serverData: ServerDataType } +/** + * Props object passed into the [getServerData](https://www.gatsbyjs.com/docs/reference/rendering-options/server-side-rendering/) function. + */ +export type GetServerDataProps = { + headers: Map + method: string + url: string + query?: Record + params?: Record + pageContext: Record +}; + +/** + * The return type (promise payload) from the [getServerData](https://www.gatsbyjs.com/docs/reference/rendering-options/server-side-rendering/) function. + */ +export type GetServerDataReturn> = { + headers?: Map + props?: ServerDataType + status?: number +}; + +/** + * A shorthand type for combining the props and return type for the [getServerData](https://www.gatsbyjs.com/docs/reference/rendering-options/server-side-rendering/) function. + */ +export type GetServerData = (props: GetServerDataProps) => Promise>; + +/** + * Constructor arguments for the PageRenderer. + */ export interface PageRendererProps { location: WindowLocation } From 631a6d24114afd3c3d34441daad4469baee794a4 Mon Sep 17 00:00:00 2001 From: Josh Date: Thu, 6 Jan 2022 08:26:16 -0500 Subject: [PATCH 3/4] Revert get-server-data change --- packages/gatsby/src/utils/get-server-data.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/gatsby/src/utils/get-server-data.ts b/packages/gatsby/src/utils/get-server-data.ts index b186cc23a93b2..67c1d96d40d74 100644 --- a/packages/gatsby/src/utils/get-server-data.ts +++ b/packages/gatsby/src/utils/get-server-data.ts @@ -2,10 +2,22 @@ import type { Request } from "express" import type { IGatsbyPage } from "../redux/types" import { match } from "@gatsbyjs/reach-router/lib/utils" -import { GetServerData, GetServerDataReturn } from "../.." + +export interface IServerData { + headers?: Record + props?: Record + status?: number +} interface IModuleWithServerData { - getServerData?: GetServerData> + getServerData?: (args: { + headers: Map + method: string + url: string + query?: Record + params?: Record + pageContext: Record + }) => Promise } export async function getServerData( @@ -15,7 +27,7 @@ export async function getServerData( page: IGatsbyPage, pagePath: string, mod: IModuleWithServerData | undefined -): Promise>> { +): Promise { if (!mod?.getServerData) { return {} } From ea9cf582d608bae65e32abe0c79b33251e68552b Mon Sep 17 00:00:00 2001 From: Josh Date: Thu, 6 Jan 2022 09:23:12 -0500 Subject: [PATCH 4/4] Make return type a promise --- packages/gatsby/index.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts index 50346bf4088cc..0bb743ce9df9c 100644 --- a/packages/gatsby/index.d.ts +++ b/packages/gatsby/index.d.ts @@ -161,16 +161,16 @@ export type GetServerDataProps = { /** * The return type (promise payload) from the [getServerData](https://www.gatsbyjs.com/docs/reference/rendering-options/server-side-rendering/) function. */ -export type GetServerDataReturn> = { +export type GetServerDataReturn> = Promise<{ headers?: Map props?: ServerDataType status?: number -}; +}>; /** * A shorthand type for combining the props and return type for the [getServerData](https://www.gatsbyjs.com/docs/reference/rendering-options/server-side-rendering/) function. */ -export type GetServerData = (props: GetServerDataProps) => Promise>; +export type GetServerData = (props: GetServerDataProps) => GetServerDataReturn; /** * Constructor arguments for the PageRenderer.