-
-
Notifications
You must be signed in to change notification settings - Fork 141
feat(server): native Fastify adapter #1127
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
11 commits
Select commit
Hold shift + click to select a range
6fe7b5b
init
dinwwwh 93310cf
wip
dinwwwh 97ffa11
standard-server
dinwwwh 5f5c0fe
use standard server fastify in nestjs integration
dinwwwh b9b17d5
fix
dinwwwh c5ffae3
wip
dinwwwh b0825a9
fix build
dinwwwh d315070
improve test coverage
dinwwwh 20bd292
improve
dinwwwh 3c539a7
improve
dinwwwh 6168414
Merge branch 'main' into feat/server/native-fastify-adapter
dinwwwh 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
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
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
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
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,3 @@ | ||
| it('exports OpenAPIHandler', async () => { | ||
| expect(Object.keys(await import('./index'))).toContain('OpenAPIHandler') | ||
| }) |
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 @@ | ||
| export * from './openapi-handler' |
21 changes: 21 additions & 0 deletions
21
packages/openapi/src/adapters/fastify/openapi-handler.test.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { os } from '@orpc/server' | ||
| import Fastify from 'fastify' | ||
| import request from 'supertest' | ||
| import { OpenAPIHandler } from './openapi-handler' | ||
|
|
||
| describe('openAPIHandler', () => { | ||
| it('works', async () => { | ||
| const handler = new OpenAPIHandler(os.route({ method: 'GET', path: '/ping' }).handler(({ input }) => ({ output: input }))) | ||
|
|
||
| const fastify = Fastify() | ||
|
|
||
| fastify.all('/*', async (req, reply) => { | ||
| await handler.handle(req, reply, { prefix: '/prefix' }) | ||
| }) | ||
| await fastify.ready() | ||
| const res = await request(fastify.server).get('/prefix/ping?input=hello') | ||
|
|
||
| expect(res.text).toContain('hello') | ||
| expect(res.status).toBe(200) | ||
| }) | ||
| }) |
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,20 @@ | ||
| import type { Context, Router } from '@orpc/server' | ||
| import type { FastifyHandlerOptions } from '@orpc/server/fastify' | ||
| import type { StandardOpenAPIHandlerOptions } from '../standard' | ||
| import { FastifyHandler } from '@orpc/server/fastify' | ||
| import { StandardOpenAPIHandler } from '../standard' | ||
|
|
||
| export interface OpenAPIHandlerOptions<T extends Context> extends FastifyHandlerOptions<T>, StandardOpenAPIHandlerOptions<T> { | ||
| } | ||
|
|
||
| /** | ||
| * OpenAPI Handler for Fastify Server | ||
| * | ||
| * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-handler OpenAPI Handler Docs} | ||
| * @see {@link https://orpc.unnoq.com/docs/adapters/http HTTP Adapter Docs} | ||
| */ | ||
| export class OpenAPIHandler<T extends Context> extends FastifyHandler<T> { | ||
| constructor(router: Router<any, T>, options: NoInfer<OpenAPIHandlerOptions<T>> = {}) { | ||
| super(new StandardOpenAPIHandler(router, options), options) | ||
| } | ||
dinwwwh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
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,17 @@ | ||
| import type { FastifyReply, FastifyRequest } from '@orpc/standard-server-fastify' | ||
| import type { FastifyHandler } from './handler' | ||
|
|
||
| describe('FastifyHandler', () => { | ||
| it('optional context when all context is optional', () => { | ||
| const handler = {} as FastifyHandler<{ auth?: boolean }> | ||
|
|
||
| handler.handle({} as FastifyRequest, {} as FastifyReply) | ||
| handler.handle({} as FastifyRequest, {} as FastifyReply, { context: { auth: true } }) | ||
|
|
||
| const handler2 = {} as FastifyHandler<{ auth: boolean }> | ||
|
|
||
| handler2.handle({} as FastifyRequest, {} as FastifyReply, { context: { auth: true } }) | ||
| // @ts-expect-error -- context is required | ||
| handler2.handle({} as FastifyRequest, {} as FastifyReply) | ||
| }) | ||
| }) |
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.