From fd5cd02e9b2f4bde78d177da29ec23a310439267 Mon Sep 17 00:00:00 2001 From: Brandon Bayer Date: Fri, 17 Apr 2020 15:45:46 +0700 Subject: [PATCH] implement ssrQuery --- .../store/app/products/pages/products/ssr.tsx | 33 +++++++++++++++++++ packages/core/src/index.ts | 1 + packages/core/src/ssrQuery.ts | 19 +++++++++++ 3 files changed, 53 insertions(+) create mode 100644 examples/store/app/products/pages/products/ssr.tsx create mode 100644 packages/core/src/ssrQuery.ts diff --git a/examples/store/app/products/pages/products/ssr.tsx b/examples/store/app/products/pages/products/ssr.tsx new file mode 100644 index 0000000000..df918e2d60 --- /dev/null +++ b/examples/store/app/products/pages/products/ssr.tsx @@ -0,0 +1,33 @@ +import {Link, BlitzPage, GetServerSideProps, ssrQuery} from '@blitzjs/core' +import getProducts from 'app/products/queries/getProducts' +import {Product} from 'db' + +type PageProps = { + products: Product[] +} + +export const getServerSideProps: GetServerSideProps = async ({req, res}) => { + const products = await ssrQuery(getProducts, undefined, {req, res}) + + return { + props: {products}, + } +} + +const Page: BlitzPage = function ({products}) { + return ( +
+

Products

+
+ {products.map((product) => ( +

+ + {product.name} + +

+ ))} +
+
+ ) +} +export default Page diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 6d43c6131e..86fbf4040e 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -11,4 +11,5 @@ export {default as Link} from 'next/link' export {default as Router, useRouter, withRouter} from 'next/router' export * from './useQuery' +export * from './ssrQuery' export * from './rpc' diff --git a/packages/core/src/ssrQuery.ts b/packages/core/src/ssrQuery.ts new file mode 100644 index 0000000000..61f7f3c76b --- /dev/null +++ b/packages/core/src/ssrQuery.ts @@ -0,0 +1,19 @@ +import {IncomingMessage, OutgoingMessage} from 'http' + +type QueryFn = (...args: any) => Promise + +type SsrQueryContext = { + req: IncomingMessage + res: OutgoingMessage +} + +export async function ssrQuery( + queryFn: T, + params: any, + // @ts-ignore unused + context: SsrQueryContext, +): Promise> { + const data = await queryFn(params) + + return data +}