This repository was archived by the owner on Mar 1, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
feat(server): migrate fastify adapter #339
Merged
Merged
Changes from 1 commit
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
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
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,12 @@ | ||
| import type { SchemaDef } from "@zenstackhq/orm/schema"; | ||
| import type { ApiHandler } from "../types"; | ||
|
|
||
| /** | ||
| * Options common to all adapters | ||
| */ | ||
| export interface CommonAdapterOptions<Schema extends SchemaDef> { | ||
| /** | ||
| * The API handler to process requests | ||
| */ | ||
| apiHandler: ApiHandler<Schema>; | ||
| } |
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,2 @@ | ||
| export { ZenStackFastifyPlugin, type PluginOptions } from './plugin'; | ||
|
|
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,57 @@ | ||
| import type { ClientContract } from '@zenstackhq/orm'; | ||
| import type { SchemaDef } from '@zenstackhq/orm/schema'; | ||
| import type { FastifyPluginCallback, FastifyReply, FastifyRequest } from 'fastify'; | ||
| import fp from 'fastify-plugin'; | ||
| import type { CommonAdapterOptions } from '../common'; | ||
|
|
||
| /** | ||
| * Fastify plugin options | ||
| */ | ||
| export interface PluginOptions<Schema extends SchemaDef> extends CommonAdapterOptions<Schema> { | ||
|
|
||
| /** | ||
| * Url prefix, e.g.: /api | ||
| */ | ||
| prefix: string; | ||
|
|
||
| /** | ||
| * Callback for getting a PrismaClient for the given request | ||
|
ymc9 marked this conversation as resolved.
Outdated
|
||
| */ | ||
| getClient: (request: FastifyRequest, reply: FastifyReply) => ClientContract<Schema> | Promise<ClientContract<Schema>>; | ||
| } | ||
|
ymc9 marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Fastify plugin for handling CRUD requests. | ||
| */ | ||
| const pluginHandler: FastifyPluginCallback<PluginOptions<SchemaDef>> = (fastify, options, done) => { | ||
| const prefix = options.prefix ?? ''; | ||
|
|
||
| fastify.all(`${prefix}/*`, async (request, reply) => { | ||
| const client = await options.getClient(request, reply); | ||
| if (!client) { | ||
| reply.status(500).send({ message: 'unable to get prisma from request context' }); | ||
|
ymc9 marked this conversation as resolved.
Outdated
|
||
| return reply; | ||
| } | ||
|
ymc9 marked this conversation as resolved.
|
||
|
|
||
| try { | ||
| const response = await options.apiHandler.handleRequest({ | ||
| method: request.method, | ||
| path: (request.params as any)['*'], | ||
| query: request.query as Record<string, string | string[]>, | ||
| requestBody: request.body, | ||
| client, | ||
| }); | ||
| reply.status(response.status).send(response.body); | ||
| } catch (err) { | ||
| reply.status(500).send({ message: `An unhandled error occurred: ${err}` }); | ||
| } | ||
|
|
||
| return reply; | ||
| }); | ||
|
|
||
| done(); | ||
| }; | ||
|
|
||
| const plugin = fp(pluginHandler); | ||
|
|
||
| export { plugin as ZenStackFastifyPlugin }; | ||
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,198 @@ | ||
| import { createTestClient } from '@zenstackhq/testtools'; | ||
| import fastify from 'fastify'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { ZenStackFastifyPlugin } from '../../src/adapter/fastify'; | ||
| import { RestApiHandler, RPCApiHandler } from '../../src/api'; | ||
| import { makeUrl, schema } from '../utils'; | ||
|
|
||
| describe('Fastify adapter tests - rpc handler', () => { | ||
| it('run plugin regular json', async () => { | ||
| const client = await createTestClient(schema); | ||
|
|
||
| const app = fastify(); | ||
| app.register(ZenStackFastifyPlugin, { | ||
| prefix: '/api', | ||
| getClient: () => client, | ||
| apiHandler: new RPCApiHandler({ schema: client.schema }) | ||
| }); | ||
|
|
||
| let r = await app.inject({ | ||
| method: 'GET', | ||
| url: makeUrl('/api/post/findMany', { where: { id: { equals: '1' } } }), | ||
| }); | ||
| expect(r.statusCode).toBe(200); | ||
| expect(r.json().data).toHaveLength(0); | ||
|
|
||
| r = await app.inject({ | ||
| method: 'POST', | ||
| url: '/api/user/create', | ||
| payload: { | ||
| include: { posts: true }, | ||
| data: { | ||
| id: 'user1', | ||
| email: 'user1@abc.com', | ||
| posts: { | ||
| create: [ | ||
| { title: 'post1', published: true, viewCount: 1 }, | ||
| { title: 'post2', published: false, viewCount: 2 }, | ||
| ], | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| expect(r.statusCode).toBe(201); | ||
| const data = r.json().data; | ||
| expect(data).toEqual( | ||
| expect.objectContaining({ | ||
| email: 'user1@abc.com', | ||
| posts: expect.arrayContaining([ | ||
| expect.objectContaining({ title: 'post1' }), | ||
| expect.objectContaining({ title: 'post2' }), | ||
| ]), | ||
| }) | ||
| ); | ||
|
|
||
| r = await app.inject({ | ||
| method: 'GET', | ||
| url: makeUrl('/api/post/findMany'), | ||
| }); | ||
| expect(r.statusCode).toBe(200); | ||
| expect(r.json().data).toHaveLength(2); | ||
|
|
||
| r = await app.inject({ | ||
| method: 'GET', | ||
| url: makeUrl('/api/post/findMany', { where: { viewCount: { gt: 1 } } }), | ||
| }); | ||
| expect(r.statusCode).toBe(200); | ||
| expect(r.json().data).toHaveLength(1); | ||
|
|
||
| r = await app.inject({ | ||
| method: 'PUT', | ||
| url: '/api/user/update', | ||
| payload: { where: { id: 'user1' }, data: { email: 'user1@def.com' } }, | ||
| }); | ||
| expect(r.statusCode).toBe(200); | ||
| expect(r.json().data.email).toBe('user1@def.com'); | ||
|
|
||
| r = await app.inject({ | ||
| method: 'GET', | ||
| url: makeUrl('/api/post/count', { where: { viewCount: { gt: 1 } } }), | ||
| }); | ||
| expect(r.statusCode).toBe(200); | ||
| expect(r.json().data).toBe(1); | ||
|
|
||
| r = await app.inject({ | ||
| method: 'GET', | ||
| url: makeUrl('/api/post/aggregate', { _sum: { viewCount: true } }), | ||
| }); | ||
| expect(r.statusCode).toBe(200); | ||
| expect(r.json().data._sum.viewCount).toBe(3); | ||
|
|
||
| r = await app.inject({ | ||
| method: 'GET', | ||
| url: makeUrl('/api/post/groupBy', { by: ['published'], _sum: { viewCount: true } }), | ||
| }); | ||
| expect(r.statusCode).toBe(200); | ||
| expect(r.json().data).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ published: true, _sum: { viewCount: 1 } }), | ||
| expect.objectContaining({ published: false, _sum: { viewCount: 2 } }), | ||
| ]) | ||
| ); | ||
|
|
||
| r = await app.inject({ | ||
| method: 'DELETE', | ||
| url: makeUrl('/api/user/deleteMany', { where: { id: 'user1' } }), | ||
| }); | ||
| expect(r.statusCode).toBe(200); | ||
| expect(r.json().data.count).toBe(1); | ||
| }); | ||
|
|
||
| it('invalid path or args', async () => { | ||
| const client = await createTestClient(schema); | ||
|
|
||
| const app = fastify(); | ||
| app.register(ZenStackFastifyPlugin, { | ||
| prefix: '/api', | ||
| getClient: () => client, | ||
| apiHandler: new RPCApiHandler({ schema: client.schema }), | ||
| }); | ||
|
|
||
| let r = await app.inject({ | ||
| method: 'GET', | ||
| url: '/api/post/', | ||
| }); | ||
| expect(r.statusCode).toBe(400); | ||
|
|
||
| r = await app.inject({ | ||
| method: 'GET', | ||
| url: '/api/post/findMany/abc', | ||
| }); | ||
| expect(r.statusCode).toBe(400); | ||
|
|
||
| r = await app.inject({ | ||
| method: 'GET', | ||
| url: '/api/post/findMany?q=abc', | ||
| }); | ||
| expect(r.statusCode).toBe(400); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Fastify adapter tests - rest handler', () => { | ||
| it('run plugin regular json', async () => { | ||
| const client = await createTestClient(schema); | ||
|
|
||
| const app = fastify(); | ||
| app.register(ZenStackFastifyPlugin, { | ||
| prefix: '/api', | ||
| getClient: () => client, | ||
| apiHandler: new RestApiHandler({ schema: client.schema, endpoint: 'http://localhost/api' }), | ||
| }); | ||
|
|
||
| let r = await app.inject({ | ||
| method: 'GET', | ||
| url: '/api/post/1', | ||
| }); | ||
| expect(r.statusCode).toBe(404); | ||
|
|
||
| r = await app.inject({ | ||
| method: 'POST', | ||
| url: '/api/user', | ||
| payload: { | ||
| data: { | ||
| type: 'User', | ||
| attributes: { | ||
| id: 'user1', | ||
| email: 'user1@abc.com', | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| expect(r.statusCode).toBe(201); | ||
| expect(r.json()).toMatchObject({ | ||
| jsonapi: { version: '1.1' }, | ||
| data: { type: 'User', id: 'user1', attributes: { email: 'user1@abc.com' } }, | ||
| }); | ||
|
|
||
| r = await app.inject({ method: 'GET', url: '/api/user?filter[id]=user1' }); | ||
| expect(r.json().data).toHaveLength(1); | ||
|
|
||
| r = await app.inject({ method: 'GET', url: '/api/user?filter[id]=user2' }); | ||
| expect(r.json().data).toHaveLength(0); | ||
|
|
||
| r = await app.inject({ method: 'GET', url: '/api/user?filter[id]=user1&filter[email]=xyz' }); | ||
| expect(r.json().data).toHaveLength(0); | ||
|
|
||
| r = await app.inject({ | ||
| method: 'PUT', | ||
| url: '/api/user/user1', | ||
| payload: { data: { type: 'User', attributes: { email: 'user1@def.com' } } }, | ||
| }); | ||
| expect(r.statusCode).toBe(200); | ||
| expect(r.json().data.attributes.email).toBe('user1@def.com'); | ||
|
|
||
| r = await app.inject({ method: 'DELETE', url: '/api/user/user1' }); | ||
| expect(r.statusCode).toBe(200); | ||
| expect(await client.user.findMany()).toHaveLength(0); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.