From 576e13dc07efb51c39e59f258036145d6610644a Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Sun, 16 Jun 2024 22:18:11 +0530 Subject: [PATCH 01/21] docs --- app/pages/docs/auth-server.mdx | 23 +++++++++++++++- app/pages/docs/rpc-setup.mdx | 50 +++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/app/pages/docs/auth-server.mdx b/app/pages/docs/auth-server.mdx index d0729e8b..3c44d1ab 100644 --- a/app/pages/docs/auth-server.mdx +++ b/app/pages/docs/auth-server.mdx @@ -54,7 +54,28 @@ function PageWithGssp(props: Props) { export default PageWithGssp ``` -## API Routes {#api-routes} +## App Router API Routes {#app-api-routes} + +You can get the session context inside API routes by wrapping it with the +`withBlitzAuth` function exported from `src/blitz-server`: + +```ts +//app/api/logout/route.ts +import {withBlitzAuth} from "app/blitz-server" + +export const POST = withBlitzAuth(async (_request, _params, ctx) => { + const session = ctx.session + await session.$revoke() + return new Response( + JSON.stringify({ + userId: session.userId, + }), + {status: 200}, + ) +}) +``` + +## Pages Router API Routes {#pages-api-routes} You can get the session context inside API routes by wrapping it with the `api` function exported from `src/blitz-server`: diff --git a/app/pages/docs/rpc-setup.mdx b/app/pages/docs/rpc-setup.mdx index 758263f1..d6ab08a6 100644 --- a/app/pages/docs/rpc-setup.mdx +++ b/app/pages/docs/rpc-setup.mdx @@ -47,7 +47,55 @@ export { withBlitz } You can read more about `react-query`'s `QueryClient` options [here](https://react-query.tanstack.com/reference/QueryClient). -3. Add Blitz RPC API Route: +3. Add the following to your `blitz-server.ts` file: + +```ts +const { + invoke, +} = setupBlitzServer({ + plugins: [ + // Other plugins + RpcServerPlugin({ + logging: { + //logging options + }, + onInvokeError(error) { + // Add your custom error handling here + }, + }), + ], +}) + +export { + invoke, +} +``` + +4. Add Blitz RPC API Route: + +4.1 App router (recommended): + +Create an `app/api/rpc` directory in your `src` directory with +`[[...blitz]]/route.ts` file, and add the following lines: + +```ts +// app/api/rpc/[[...blitz]]/route.ts + +import {rpcAppHandler} from "@blitzjs/rpc" +import {withBlitzAuth} from "app/blitz-server" + +// With Blitz Auth +export const {GET, POST, HEAD} = withBlitzAuth(rpcAppHandler( + {}, +)) + +// Standalone Usage +// export const {GET, POST, HEAD} = rpcAppHandler( +// {}, +// ) +``` + +4.2 Pages Router: Create an `pages/api/rpc` directory in your `src` directory with `[[...blitz]].ts` file, and add the following lines: From 9dcf512e38b38240295649a7af250cae154b59e8 Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Tue, 18 Jun 2024 16:34:59 +0530 Subject: [PATCH 02/21] Update app/pages/docs/rpc-setup.mdx Co-authored-by: Brandon Bayer --- app/pages/docs/rpc-setup.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/pages/docs/rpc-setup.mdx b/app/pages/docs/rpc-setup.mdx index d6ab08a6..186ee202 100644 --- a/app/pages/docs/rpc-setup.mdx +++ b/app/pages/docs/rpc-setup.mdx @@ -75,8 +75,8 @@ export { 4.1 App router (recommended): -Create an `app/api/rpc` directory in your `src` directory with -`[[...blitz]]/route.ts` file, and add the following lines: +Create an `app/api/rpc/[[...blitz]]` directory in your `src` directory with +a `route.ts` file, and add the following lines: ```ts // app/api/rpc/[[...blitz]]/route.ts From b6064b23217f1075bbd9e69f05f11f78e2482406 Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Tue, 18 Jun 2024 16:47:21 +0530 Subject: [PATCH 03/21] fix formatting --- app/pages/docs/rpc-setup.mdx | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/app/pages/docs/rpc-setup.mdx b/app/pages/docs/rpc-setup.mdx index 186ee202..fbb673ce 100644 --- a/app/pages/docs/rpc-setup.mdx +++ b/app/pages/docs/rpc-setup.mdx @@ -9,7 +9,9 @@ sidebar_label: Setup npm i @blitzjs/rpc # yarn add @blitzjs/rpc # pnpm add @blitzjs/rpc ``` -2. Add the following to your `blitz-client.ts` file: +### Client setup {#client-setup} + +Add the following to your `blitz-client.ts` file: ```typescript import { setupClient } from "@blitzjs/next" @@ -47,12 +49,12 @@ export { withBlitz } You can read more about `react-query`'s `QueryClient` options [here](https://react-query.tanstack.com/reference/QueryClient). -3. Add the following to your `blitz-server.ts` file: +### Server setup {#server-setup} + +Add the following to your `blitz-server.ts` file: ```ts -const { - invoke, -} = setupBlitzServer({ +const { invoke } = setupBlitzServer({ plugins: [ // Other plugins RpcServerPlugin({ @@ -60,34 +62,30 @@ const { //logging options }, onInvokeError(error) { - // Add your custom error handling here + // Add your custom error handling here }, }), ], }) -export { - invoke, -} +export { invoke } ``` -4. Add Blitz RPC API Route: +### API setup {#api-setup} -4.1 App router (recommended): +#### App router (recommended): {#app-router} -Create an `app/api/rpc/[[...blitz]]` directory in your `src` directory with -a `route.ts` file, and add the following lines: +Create an `app/api/rpc/[[...blitz]]` directory in your `src` directory +with a `route.ts` file, and add the following lines: ```ts // app/api/rpc/[[...blitz]]/route.ts -import {rpcAppHandler} from "@blitzjs/rpc" -import {withBlitzAuth} from "app/blitz-server" +import { rpcAppHandler } from "@blitzjs/rpc" +import { withBlitzAuth } from "app/blitz-server" // With Blitz Auth -export const {GET, POST, HEAD} = withBlitzAuth(rpcAppHandler( - {}, -)) +export const { GET, POST, HEAD } = withBlitzAuth(rpcAppHandler({})) // Standalone Usage // export const {GET, POST, HEAD} = rpcAppHandler( @@ -95,7 +93,7 @@ export const {GET, POST, HEAD} = withBlitzAuth(rpcAppHandler( // ) ``` -4.2 Pages Router: +#### Pages router: {#pages-router} Create an `pages/api/rpc` directory in your `src` directory with `[[...blitz]].ts` file, and add the following lines: From a730a333ffea8c6217a50b4f14bef3197b2836af Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Tue, 18 Jun 2024 16:50:23 +0530 Subject: [PATCH 04/21] rpcApphanlder config --- app/pages/docs/rpc-config.mdx | 37 ++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/app/pages/docs/rpc-config.mdx b/app/pages/docs/rpc-config.mdx index 13ba84bf..e73e7228 100644 --- a/app/pages/docs/rpc-config.mdx +++ b/app/pages/docs/rpc-config.mdx @@ -75,7 +75,9 @@ module.exports = withBlitz({ ## Logging Setup {#blitz-rpc-logging} -In your `[[...blitz]].ts` api file you can see the following settings in the `rpcHandler` +In your `[[...blitz]].ts` api file you can see the following settings in +the `rpcHandler` + ```ts logging?: { /** @@ -99,17 +101,35 @@ logging?: { disablelevel?: "debug" | "info" } ``` + - Blitz RPC defaults to: - - `verbose` to be true if it not configured. - - The `input` and `resolver completion time` will be logged with the `info` level. - - The `result` and `next.js serialization time` will be logged with the `debug` level. + Blitz RPC defaults to: - `verbose` to be true if it not configured. - + The `input` and `resolver completion time` will be logged with the + `info` level. - The `result` and `next.js serialization time` will be + logged with the `debug` level. - Example: ```ts +// app router (recommended) +// app/api/rpc/[[...blitz]]/route.ts +export default api( + rpcAppHandler({ + onError: console.log, + formatError: (error) => { + error.message = `FormatError handler: ${error.message}` + return error + }, + logging: { + verbose: true, + blockList: ["getCurrentUser", ...], //just write the resolver name [which is the resolver file name] + }, + }) +) + +// pages router +// src/pages/api/rpc/[[...blitz]].ts export default api( rpcHandler({ onError: console.log, @@ -125,6 +145,5 @@ export default api( ) ``` -This is will enable verbose Blitz RPC logging for all resolvers except the resolvers `getCurrentUser` and others mentioned in the `blockList` - - +This is will enable verbose Blitz RPC logging for all resolvers except the +resolvers `getCurrentUser` and others mentioned in the `blockList` From 2a99425948ceacc436d2aa4e9fd0c9b6b4eaf813 Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Tue, 18 Jun 2024 17:01:57 +0530 Subject: [PATCH 05/21] mention withBlitzAuth api --- app/pages/docs/auth-server.mdx | 44 ++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/app/pages/docs/auth-server.mdx b/app/pages/docs/auth-server.mdx index 3c44d1ab..6d362eb9 100644 --- a/app/pages/docs/auth-server.mdx +++ b/app/pages/docs/auth-server.mdx @@ -61,7 +61,7 @@ You can get the session context inside API routes by wrapping it with the ```ts //app/api/logout/route.ts -import {withBlitzAuth} from "app/blitz-server" +import { withBlitzAuth } from "app/blitz-server" export const POST = withBlitzAuth(async (_request, _params, ctx) => { const session = ctx.session @@ -70,11 +70,51 @@ export const POST = withBlitzAuth(async (_request, _params, ctx) => { JSON.stringify({ userId: session.userId, }), - {status: 200}, + { status: 200 } ) }) ``` +#### `withBlitzAuth` API {#with-blitz-auth-api} + +The function supports both single handler as an input as well as an object +of handlers and has the following signature: + +```ts +type Handler = ( + request: Request, + params: any, + ctx: Ctx +) => Promise | Response +function withBlitzAuth(fn: Handler): Response | Promise +function withBlitzAuth(fn: T): T +``` + +##### Example Usage with multiple handlers + +```ts +import { withBlitzAuth } from "app/blitz-server" + +export const { GET, POST } = withBlitzAuth({ + GET: async (_request, _params, { session }) => { + return new Response( + JSON.stringify({ + userId: session.userId, + }), + { status: 200 } + ) + }, + POST: async (_request, _params, { session }) => { + return new Response( + JSON.stringify({ + userId: session.userId, + }), + { status: 200 } + ) + }, +}) +``` + ## Pages Router API Routes {#pages-api-routes} You can get the session context inside API routes by wrapping it with the From 7d3ecdff01ce56f2e33191918be0644a8229fd5c Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Tue, 18 Jun 2024 17:03:55 +0530 Subject: [PATCH 06/21] use unknown than any --- app/pages/docs/auth-server.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/pages/docs/auth-server.mdx b/app/pages/docs/auth-server.mdx index 6d362eb9..1715dc85 100644 --- a/app/pages/docs/auth-server.mdx +++ b/app/pages/docs/auth-server.mdx @@ -83,7 +83,7 @@ of handlers and has the following signature: ```ts type Handler = ( request: Request, - params: any, + params: unknown, ctx: Ctx ) => Promise | Response function withBlitzAuth(fn: Handler): Response | Promise From 4c412ea1d2915e5b4e84027b6bee8a75f6d18e12 Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Wed, 3 Jul 2024 21:01:29 +0530 Subject: [PATCH 07/21] push changes --- app/pages/docs/auth-server.mdx | 8 +------- app/pages/docs/rpc-config.mdx | 11 ++++++++--- app/pages/docs/rpc-setup.mdx | 22 +++++++++++++++------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/app/pages/docs/auth-server.mdx b/app/pages/docs/auth-server.mdx index 1715dc85..614c6fe0 100644 --- a/app/pages/docs/auth-server.mdx +++ b/app/pages/docs/auth-server.mdx @@ -81,13 +81,7 @@ The function supports both single handler as an input as well as an object of handlers and has the following signature: ```ts -type Handler = ( - request: Request, - params: unknown, - ctx: Ctx -) => Promise | Response -function withBlitzAuth(fn: Handler): Response | Promise -function withBlitzAuth(fn: T): T +function withBlitzAuth(handlers: { [method: string]: Handler }) ``` ##### Example Usage with multiple handlers diff --git a/app/pages/docs/rpc-config.mdx b/app/pages/docs/rpc-config.mdx index e73e7228..06318f57 100644 --- a/app/pages/docs/rpc-config.mdx +++ b/app/pages/docs/rpc-config.mdx @@ -109,10 +109,11 @@ logging?: { logged with the `debug` level. -Example: +#### Example: + +##### In App Router (recommended) ```ts -// app router (recommended) // app/api/rpc/[[...blitz]]/route.ts export default api( rpcAppHandler({ @@ -123,11 +124,15 @@ export default api( }, logging: { verbose: true, - blockList: ["getCurrentUser", ...], //just write the resolver name [which is the resolver file name] + blockList: ["getCurrentUser", ...] }, }) ) +``` +##### In Pages Router + +```ts // pages router // src/pages/api/rpc/[[...blitz]].ts export default api( diff --git a/app/pages/docs/rpc-setup.mdx b/app/pages/docs/rpc-setup.mdx index fbb673ce..8e80b5c8 100644 --- a/app/pages/docs/rpc-setup.mdx +++ b/app/pages/docs/rpc-setup.mdx @@ -78,21 +78,27 @@ export { invoke } Create an `app/api/rpc/[[...blitz]]` directory in your `src` directory with a `route.ts` file, and add the following lines: +##### Standalone Usage + ```ts // app/api/rpc/[[...blitz]]/route.ts +import { rpcAppHandler } from "@blitzjs/rpc" + +export const { GET, POST, HEAD } = rpcAppHandler() +``` + +##### Blitz Auth Integration +```ts +// app/api/rpc/[[...blitz]]/route.ts import { rpcAppHandler } from "@blitzjs/rpc" import { withBlitzAuth } from "app/blitz-server" -// With Blitz Auth -export const { GET, POST, HEAD } = withBlitzAuth(rpcAppHandler({})) - -// Standalone Usage -// export const {GET, POST, HEAD} = rpcAppHandler( -// {}, -// ) +export const { GET, POST, HEAD } = withBlitzAuth(rpcAppHandler()) ``` +View [RPC Configurations](/rpc-config) to view the available options. + #### Pages router: {#pages-router} Create an `pages/api/rpc` directory in your `src` directory with @@ -107,6 +113,8 @@ import { api } from "src/blitz-server" export default api(rpcHandler({})) ``` +View [RPC Configurations](/rpc-config) to view the available options. + --- Follow the [Query Resolvers](/docs/query-resolvers) and From 24587cfc130263f65b5f25bb904bf30bdf03832e Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Wed, 3 Jul 2024 21:15:53 +0530 Subject: [PATCH 08/21] add api of withBlitzAuth --- app/pages/docs/auth-server.mdx | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/app/pages/docs/auth-server.mdx b/app/pages/docs/auth-server.mdx index 614c6fe0..d51c7869 100644 --- a/app/pages/docs/auth-server.mdx +++ b/app/pages/docs/auth-server.mdx @@ -84,9 +84,51 @@ of handlers and has the following signature: function withBlitzAuth(handlers: { [method: string]: Handler }) ``` +##### Signature + +##### Arguments + +- `handlers: { [method: string]: Handler })` - An object of handlers where + the key is the HTTP method and the value is the handler function. + +```ts +type Handler = ( + request: Request, + params: Record, + ctx: { session: SessionContext } +) => Promise +``` + +##### Returns + +- `{ [method: string]: Handler }` - The wrapper function returns an object + of handlers where the key is the HTTP method and the value is the + handler function wrapped with the session management of `@blitzjs/auth`. + + ##### Example Usage with single handler + +```ts +//app/api/logout/route.ts +import { withBlitzAuth } from "app/blitz-server" + +export const { POST } = withBlitzAuth({ + POST: async (_request, _params, { session }) => { + // logout the user + await session.$revoke() + return new Response( + JSON.stringify({ + userId: session.userId, + }), + { status: 200 } + ) + }, +}) +``` + ##### Example Usage with multiple handlers ```ts +//app/api/multiple/route.ts import { withBlitzAuth } from "app/blitz-server" export const { GET, POST } = withBlitzAuth({ From 906fc4b2b0b9dbe81995128c17d75f880ed3292f Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Wed, 3 Jul 2024 21:20:23 +0530 Subject: [PATCH 09/21] eloaborate a bit --- app/pages/docs/rpc-config.mdx | 54 +++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/app/pages/docs/rpc-config.mdx b/app/pages/docs/rpc-config.mdx index 06318f57..afd0df51 100644 --- a/app/pages/docs/rpc-config.mdx +++ b/app/pages/docs/rpc-config.mdx @@ -109,10 +109,35 @@ logging?: { logged with the `debug` level. -#### Example: - ##### In App Router (recommended) +#### `rpcAppHandler`: + +This function acts as the handler for all the queries and mutations in the +next.js app directory. + +#### Arguments: + +- `options` (optional): An object with the following properties: + - `onInvokeError` (optional): A function that will be called when an + error occurs during the invocation of a resolver. + - `formatError` (optional): A function that will be called to format the + error before sending it to the client. + - `logging` (optional): An object with the following properties: + - `allowList` (optional): An array of resolvers that should be logged. + - `blockList` (optional): An array of resolvers that should not be + logged. + - `verbose` (optional): A boolean that determines whether verbose + logging should be enabled. + - `disablelevel` (optional): A string that determines which level of + logging should be disabled. + +#### Returns: + +An object with the handlers that handle the GET, POST, and HEAD requests. + +#### Example: + ```ts // app/api/rpc/[[...blitz]]/route.ts export default api( @@ -132,6 +157,31 @@ export default api( ##### In Pages Router +#### `rpcHandler`: + +This function acts as the handler for all the queries and mutations in the +next.js pages directory. + +#### Arguments: + +- `options` (optional): An object with the following properties: + - `onInvokeError` (optional): A function that will be called when an + error occurs during the invocation of a resolver. + - `formatError` (optional): A function that will be called to format the + error before sending it to the client. + - `logging` (optional): An object with the following properties: + - `allowList` (optional): An array of resolvers that should be logged. + - `blockList` (optional): An array of resolvers that should not be + logged. + - `verbose` (optional): A boolean that determines whether verbose + logging should be enabled. + - `disablelevel` (optional): A string that determines which level of + logging should be disabled. + +#### Returns: + +A function that returns the handler for the RPC API. + ```ts // pages router // src/pages/api/rpc/[[...blitz]].ts From 4318f594ff1e196c0c3faed7184f075114c8ecf7 Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Wed, 3 Jul 2024 21:26:41 +0530 Subject: [PATCH 10/21] add information on changes to getSession --- app/pages/docs/auth-server.mdx | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/app/pages/docs/auth-server.mdx b/app/pages/docs/auth-server.mdx index d51c7869..feae1f12 100644 --- a/app/pages/docs/auth-server.mdx +++ b/app/pages/docs/auth-server.mdx @@ -313,3 +313,55 @@ export const updateUserRole = async ( await setPublicDataForUser(userId, { role }) } ``` + + + The following functions / patterns are meant for internal usage or for + advanced use cases. They are not needed for general use. + + +## `getSession` {#get-session} + +This function is used internally by Blitz to get the session context from +the request either from an `IncomingMessage` and `ServerResponse` pair or +from a `Request` object. + +### Arguments + +- `req: IncomingMessage | Request` - The request object from the server. +- `res: ServerResponse | never` - The response object from the server. +- `isRsc: boolean` - A boolean that determines if the request is for a + resource. + +### Returns + +- `SessionContext` - The session context object. + +### `SessionContext.setSession` {#session-context-set-session} + +//setSession(response: Response | ServerResponse) + +This function is used along with [getSession](#get-session) to set the +session context on the response object after the session has been created +or updated. + +### Arguments + +- `response: Response | ServerResponse` - The response object from the + server. + +### Returns + +- `void` + +### Example Usage + +```ts +function handler(request: Request) { + const session = await getSession(request) + // user defined logic that can mutate the session + const response = await handler(request, params, { session }) + session.setSession(response) + // the response will now container the necessary session context + return response +} +``` From c2e7b58d64f32852066a1881f0d204537202c7ae Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Wed, 3 Jul 2024 21:27:45 +0530 Subject: [PATCH 11/21] cleanup --- app/pages/docs/auth-server.mdx | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/pages/docs/auth-server.mdx b/app/pages/docs/auth-server.mdx index feae1f12..a99863a7 100644 --- a/app/pages/docs/auth-server.mdx +++ b/app/pages/docs/auth-server.mdx @@ -338,8 +338,6 @@ from a `Request` object. ### `SessionContext.setSession` {#session-context-set-session} -//setSession(response: Response | ServerResponse) - This function is used along with [getSession](#get-session) to set the session context on the response object after the session has been created or updated. From 07de9e571a964de051ff57b52d0f76f235993aab Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Wed, 3 Jul 2024 21:28:44 +0530 Subject: [PATCH 12/21] cleanup --- app/pages/docs/auth-server.mdx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/pages/docs/auth-server.mdx b/app/pages/docs/auth-server.mdx index a99863a7..7de0e280 100644 --- a/app/pages/docs/auth-server.mdx +++ b/app/pages/docs/auth-server.mdx @@ -325,33 +325,33 @@ This function is used internally by Blitz to get the session context from the request either from an `IncomingMessage` and `ServerResponse` pair or from a `Request` object. -### Arguments +#### Arguments - `req: IncomingMessage | Request` - The request object from the server. - `res: ServerResponse | never` - The response object from the server. - `isRsc: boolean` - A boolean that determines if the request is for a resource. -### Returns +#### Returns - `SessionContext` - The session context object. -### `SessionContext.setSession` {#session-context-set-session} +## `SessionContext.setSession` {#session-context-set-session} This function is used along with [getSession](#get-session) to set the session context on the response object after the session has been created or updated. -### Arguments +#### Arguments - `response: Response | ServerResponse` - The response object from the server. -### Returns +#### Returns - `void` -### Example Usage +#### Example Usage ```ts function handler(request: Request) { From dc8086b1f9cc91fb892e57bbf297e3ad51c64e05 Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Wed, 3 Jul 2024 21:29:16 +0530 Subject: [PATCH 13/21] lol --- app/pages/docs/auth-server.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/pages/docs/auth-server.mdx b/app/pages/docs/auth-server.mdx index 7de0e280..90aa3fc7 100644 --- a/app/pages/docs/auth-server.mdx +++ b/app/pages/docs/auth-server.mdx @@ -314,7 +314,7 @@ export const updateUserRole = async ( } ``` - + The following functions / patterns are meant for internal usage or for advanced use cases. They are not needed for general use. From c9012a4bf936d6c490d4e9cb398b3bee9d84ac47 Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Wed, 3 Jul 2024 21:29:39 +0530 Subject: [PATCH 14/21] fix --- app/pages/docs/auth-server.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/pages/docs/auth-server.mdx b/app/pages/docs/auth-server.mdx index 90aa3fc7..ec2ed36e 100644 --- a/app/pages/docs/auth-server.mdx +++ b/app/pages/docs/auth-server.mdx @@ -314,7 +314,7 @@ export const updateUserRole = async ( } ``` - + The following functions / patterns are meant for internal usage or for advanced use cases. They are not needed for general use. From 0a002ce2231561c54d23112958cff1e988acd5c6 Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Wed, 3 Jul 2024 21:30:17 +0530 Subject: [PATCH 15/21] fix --- app/pages/docs/auth-server.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/pages/docs/auth-server.mdx b/app/pages/docs/auth-server.mdx index ec2ed36e..5e2e0e3d 100644 --- a/app/pages/docs/auth-server.mdx +++ b/app/pages/docs/auth-server.mdx @@ -354,7 +354,7 @@ or updated. #### Example Usage ```ts -function handler(request: Request) { +function handler(request: Request, params: Record) { const session = await getSession(request) // user defined logic that can mutate the session const response = await handler(request, params, { session }) From 900fbe5a009e2d761334347e45c0af060d7f88b3 Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Wed, 3 Jul 2024 21:31:42 +0530 Subject: [PATCH 16/21] add another example --- app/pages/docs/auth-server.mdx | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app/pages/docs/auth-server.mdx b/app/pages/docs/auth-server.mdx index 5e2e0e3d..a3a41e42 100644 --- a/app/pages/docs/auth-server.mdx +++ b/app/pages/docs/auth-server.mdx @@ -353,6 +353,8 @@ or updated. #### Example Usage +##### With `Request` + ```ts function handler(request: Request, params: Record) { const session = await getSession(request) @@ -363,3 +365,16 @@ function handler(request: Request, params: Record) { return response } ``` + +##### With `IncomingMessage` and `ServerResponse` + +```ts +function handler(req: IncomingMessage, res: ServerResponse) { + const session = await getSession(req, res) + // user defined logic that can mutate the session + const response = await handler(req, res, { session }) + session.setSession(response) + // the response will now container the necessary session context + return response +} +``` From a633397444b7f368eddec58cd3296a261010eacf Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Wed, 3 Jul 2024 21:32:49 +0530 Subject: [PATCH 17/21] async --- app/pages/docs/auth-server.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/pages/docs/auth-server.mdx b/app/pages/docs/auth-server.mdx index a3a41e42..79220e33 100644 --- a/app/pages/docs/auth-server.mdx +++ b/app/pages/docs/auth-server.mdx @@ -356,7 +356,7 @@ or updated. ##### With `Request` ```ts -function handler(request: Request, params: Record) { +async function handler(request: Request, params: Record) { const session = await getSession(request) // user defined logic that can mutate the session const response = await handler(request, params, { session }) @@ -369,7 +369,7 @@ function handler(request: Request, params: Record) { ##### With `IncomingMessage` and `ServerResponse` ```ts -function handler(req: IncomingMessage, res: ServerResponse) { +async function handler(req: IncomingMessage, res: ServerResponse) { const session = await getSession(req, res) // user defined logic that can mutate the session const response = await handler(req, res, { session }) From d6019ba32913d6e3123ed3c33bb917aac778ddd0 Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Wed, 3 Jul 2024 21:34:50 +0530 Subject: [PATCH 18/21] fixes --- app/pages/docs/auth-server.mdx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/pages/docs/auth-server.mdx b/app/pages/docs/auth-server.mdx index 79220e33..1ebb24a2 100644 --- a/app/pages/docs/auth-server.mdx +++ b/app/pages/docs/auth-server.mdx @@ -358,10 +358,8 @@ or updated. ```ts async function handler(request: Request, params: Record) { const session = await getSession(request) - // user defined logic that can mutate the session const response = await handler(request, params, { session }) session.setSession(response) - // the response will now container the necessary session context return response } ``` @@ -371,10 +369,12 @@ async function handler(request: Request, params: Record) { ```ts async function handler(req: IncomingMessage, res: ServerResponse) { const session = await getSession(req, res) - // user defined logic that can mutate the session - const response = await handler(req, res, { session }) - session.setSession(response) - // the response will now container the necessary session context - return response + await handler(req, res, { session }) + session.setSession(res) } ``` + +- `handler` is the function that processes the request and can mutate the + session state +- The `response` | `res` will contain the session state after the handler + has been processed From e17aa4e96591c2ee641d9de7a0cb33ca23287830 Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Wed, 3 Jul 2024 21:36:06 +0530 Subject: [PATCH 19/21] english --- app/pages/docs/auth-server.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/pages/docs/auth-server.mdx b/app/pages/docs/auth-server.mdx index 1ebb24a2..90564c3c 100644 --- a/app/pages/docs/auth-server.mdx +++ b/app/pages/docs/auth-server.mdx @@ -315,8 +315,8 @@ export const updateUserRole = async ( ``` - The following functions / patterns are meant for internal usage or for - advanced use cases. They are not needed for general use. + The following methods are meant for internal usage or for advanced use + cases. They are not needed for general use. ## `getSession` {#get-session} @@ -374,7 +374,7 @@ async function handler(req: IncomingMessage, res: ServerResponse) { } ``` -- `handler` is the function that processes the request and can mutate the +- `handler` is a function that processes the request and can mutate the session state - The `response` | `res` will contain the session state after the handler has been processed From adb216dcc5fbdf59df02323d9b9669498a2af256 Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Wed, 14 Aug 2024 19:41:41 +0530 Subject: [PATCH 20/21] Update app/pages/docs/auth-server.mdx Co-authored-by: Brandon Bayer --- app/pages/docs/auth-server.mdx | 1 - 1 file changed, 1 deletion(-) diff --git a/app/pages/docs/auth-server.mdx b/app/pages/docs/auth-server.mdx index 90564c3c..305e3b45 100644 --- a/app/pages/docs/auth-server.mdx +++ b/app/pages/docs/auth-server.mdx @@ -84,7 +84,6 @@ of handlers and has the following signature: function withBlitzAuth(handlers: { [method: string]: Handler }) ``` -##### Signature ##### Arguments From ee5f0b0de2ea6ea409bbd16ddc2500678388c5d2 Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Wed, 14 Aug 2024 19:43:56 +0530 Subject: [PATCH 21/21] Update app/pages/docs/rpc-config.mdx --- app/pages/docs/rpc-config.mdx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/pages/docs/rpc-config.mdx b/app/pages/docs/rpc-config.mdx index afd0df51..10ddf492 100644 --- a/app/pages/docs/rpc-config.mdx +++ b/app/pages/docs/rpc-config.mdx @@ -103,9 +103,11 @@ logging?: { ``` - Blitz RPC defaults to: - `verbose` to be true if it not configured. - - The `input` and `resolver completion time` will be logged with the - `info` level. - The `result` and `next.js serialization time` will be + Blitz RPC defaults to: + - `verbose` to be true if it not configured. + - The `input` and `resolver completion time` will be logged with the + `info` level. + - The `result` and `next.js serialization time` will be logged with the `debug` level.