-
-
Notifications
You must be signed in to change notification settings - Fork 18
feat(server): migrate next.js server adapter #335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
5 changes: 4 additions & 1 deletion
5
packages/server/src/express/middleware.ts → .../server/src/adapter/express/middleware.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import type { SchemaDef } from '@zenstackhq/orm/schema'; | ||
| import { NextRequest, NextResponse } from 'next/server'; | ||
| import type { AppRouteRequestHandlerOptions } from '.'; | ||
|
|
||
| type Context = { params: Promise<{ path: string[] }> }; | ||
|
|
||
| /** | ||
| * Creates a Next.js "app router" API route request handler that handles ZenStack CRUD requests. | ||
| * | ||
| * @param options Options for initialization | ||
| * @returns An API route request handler | ||
| */ | ||
| export default function factory<Schema extends SchemaDef>( | ||
| options: AppRouteRequestHandlerOptions<Schema>, | ||
| ): (req: NextRequest, context: Context) => Promise<NextResponse> { | ||
| return async (req: NextRequest, context: Context) => { | ||
| const client = await options.getClient(req); | ||
| if (!client) { | ||
| return NextResponse.json({ message: 'unable to get ZenStackClient from request context' }, { status: 500 }); | ||
| } | ||
|
|
||
| let params: Awaited<Context['params']>; | ||
| const url = new URL(req.url); | ||
| const query = Object.fromEntries(url.searchParams); | ||
|
|
||
| try { | ||
| params = await context.params; | ||
| } catch { | ||
| return NextResponse.json({ message: 'Failed to resolve request parameters' }, { status: 500 }); | ||
| } | ||
|
|
||
| if (!params.path) { | ||
| return NextResponse.json( | ||
| { message: 'missing path parameter' }, | ||
| { | ||
| status: 400, | ||
| }, | ||
| ); | ||
| } | ||
| const path = params.path.join('/'); | ||
|
|
||
| let requestBody: unknown; | ||
| if (req.body) { | ||
| try { | ||
| requestBody = await req.json(); | ||
| } catch { | ||
| // noop | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| const r = await options.apiHandler.handleRequest({ | ||
| method: req.method!, | ||
| path, | ||
| query, | ||
| requestBody, | ||
| client, | ||
| }); | ||
| return NextResponse.json(r.body, { status: r.status }); | ||
| } catch (err) { | ||
| return NextResponse.json({ message: `An unhandled error occurred: ${err}` }, { status: 500 }); | ||
| } | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import type { ClientContract } from '@zenstackhq/orm'; | ||
| import type { SchemaDef } from '@zenstackhq/orm/schema'; | ||
| import type { NextApiRequest, NextApiResponse } from 'next'; | ||
| import type { NextRequest } from 'next/server'; | ||
| import type { ApiHandler } from '../../types'; | ||
| import { default as AppRouteHandler } from './app-route-handler'; | ||
| import { default as PagesRouteHandler } from './pages-route-handler'; | ||
|
|
||
| interface CommonAdapterOptions<Schema extends SchemaDef> { | ||
| /** | ||
| * The API handler to process requests | ||
| */ | ||
| apiHandler: ApiHandler<Schema>; | ||
| } | ||
|
|
||
| /** | ||
| * Options for initializing a Next.js API endpoint request handler. | ||
| */ | ||
| export interface PageRouteRequestHandlerOptions<Schema extends SchemaDef> extends CommonAdapterOptions<Schema> { | ||
| /** | ||
| * Callback for getting a ZenStackClient for the given request | ||
| */ | ||
| getClient: (req: NextApiRequest, res: NextApiResponse) => ClientContract<Schema> | Promise<ClientContract<Schema>>; | ||
|
|
||
| /** | ||
| * Use app dir or not | ||
| */ | ||
| useAppDir?: false | undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Options for initializing a Next.js 13 app dir API route handler. | ||
| */ | ||
| export interface AppRouteRequestHandlerOptions<Schema extends SchemaDef> extends CommonAdapterOptions<Schema> { | ||
| /** | ||
| * Callback for getting a ZenStackClient for the given request. | ||
| */ | ||
| getClient: (req: NextRequest) => ClientContract<Schema> | Promise<ClientContract<Schema>>; | ||
|
|
||
| /** | ||
| * Use app dir or not | ||
| */ | ||
| useAppDir: true; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a Next.js API route handler. | ||
| */ | ||
| export function NextRequestHandler<Schema extends SchemaDef>( | ||
| options: PageRouteRequestHandlerOptions<Schema>, | ||
| ): ReturnType<typeof PagesRouteHandler>; | ||
| export function NextRequestHandler<Schema extends SchemaDef>( | ||
| options: AppRouteRequestHandlerOptions<Schema>, | ||
| ): ReturnType<typeof AppRouteHandler>; | ||
| export function NextRequestHandler<Schema extends SchemaDef>( | ||
| options: PageRouteRequestHandlerOptions<Schema> | AppRouteRequestHandlerOptions<Schema>, | ||
| ) { | ||
| if (options.useAppDir === true) { | ||
| return AppRouteHandler(options); | ||
| } else { | ||
| return PagesRouteHandler(options); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import type { SchemaDef } from '@zenstackhq/orm/schema'; | ||
| import type { NextApiRequest, NextApiResponse } from 'next'; | ||
| import type { PageRouteRequestHandlerOptions } from '.'; | ||
|
|
||
| /** | ||
| * Creates a Next.js API endpoint "pages" router request handler that handles ZenStack CRUD requests. | ||
| * | ||
| * @param options Options for initialization | ||
| * @returns An API endpoint request handler | ||
| */ | ||
| export default function factory<Schema extends SchemaDef>( | ||
| options: PageRouteRequestHandlerOptions<Schema>, | ||
| ): (req: NextApiRequest, res: NextApiResponse) => Promise<void> { | ||
| return async (req: NextApiRequest, res: NextApiResponse) => { | ||
| const client = await options.getClient(req, res); | ||
| if (!client) { | ||
| res.status(500).json({ message: 'unable to get ZenStackClient from request context' }); | ||
| return; | ||
| } | ||
|
|
||
| if (!req.query['path']) { | ||
| res.status(400).json({ message: 'missing path parameter' }); | ||
| return; | ||
| } | ||
| const path = (req.query['path'] as string[]).join('/'); | ||
|
|
||
| try { | ||
| const r = await options.apiHandler.handleRequest({ | ||
| method: req.method!, | ||
| path, | ||
| query: req.query as Record<string, string | string[]>, | ||
| requestBody: req.body, | ||
| client, | ||
| }); | ||
| res.status(r.status).send(r.body); | ||
| } catch (err) { | ||
| res.status(500).send({ message: `An unhandled error occurred: ${err}` }); | ||
| } | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.