-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
7,743 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
'@hey-api/openapi-ts': minor | ||
--- | ||
|
||
feat: add fastify-openapi-glue plugin |
This file contains 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,10 @@ | ||
import { defineConfig } from '@hey-api/openapi-ts'; | ||
|
||
export default defineConfig({ | ||
client: '@hey-api/client-fetch', | ||
experimentalParser: true, | ||
input: | ||
'https://gist.githubusercontent.com/seriousme/55bd4c8ba2e598e416bb5543dcd362dc/raw/cf0b86ba37bb54bf1a6bf047c0ecf2a0ce4c62e0/petstore-v3.1.json', | ||
output: './src/gen', | ||
plugins: ['fastify'], | ||
}); |
This file contains 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 @@ | ||
{ | ||
"name": "@example/openapi-ts-fastify", | ||
"private": true, | ||
"version": "0.0.1", | ||
"type": "module", | ||
"scripts": { | ||
"openapi-ts": "openapi-ts", | ||
"typecheck": "tsc --noEmit", | ||
"test": "vitest" | ||
}, | ||
"dependencies": { | ||
"@hey-api/openapi-ts": "workspace:*", | ||
"fastify": "5.0.0", | ||
"fastify-openapi-glue": "4.7.1" | ||
}, | ||
"devDependencies": { | ||
"@hey-api/client-fetch": "workspace:*", | ||
"vitest": "2.1.5" | ||
} | ||
} |
This file contains 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,46 @@ | ||
// This file is auto-generated by @hey-api/openapi-ts | ||
|
||
import type { RouteHandler } from 'fastify'; | ||
|
||
export type Pet = { | ||
id: number; | ||
name: string; | ||
tag?: string; | ||
}; | ||
|
||
export type Pets = Array<Pet>; | ||
|
||
export type Error = { | ||
code: number; | ||
message: string; | ||
}; | ||
|
||
export type RouteHandlers = { | ||
createPets: RouteHandler<{ | ||
Reply: { | ||
201: unknown; | ||
}; | ||
}>; | ||
listPets: RouteHandler<{ | ||
Querystring?: { | ||
/** | ||
* How many items to return at one time (max 100) | ||
*/ | ||
limit?: number; | ||
}; | ||
Reply: { | ||
200: Pets; | ||
}; | ||
}>; | ||
showPetById: RouteHandler<{ | ||
Params: { | ||
/** | ||
* The id of the pet to retrieve | ||
*/ | ||
petId: string; | ||
}; | ||
Reply: { | ||
200: Pet; | ||
}; | ||
}>; | ||
}; |
This file contains 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 @@ | ||
// This file is auto-generated by @hey-api/openapi-ts | ||
export * from './services.gen'; | ||
export * from './types.gen'; |
This file contains 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,60 @@ | ||
// This file is auto-generated by @hey-api/openapi-ts | ||
|
||
import { | ||
createClient, | ||
createConfig, | ||
type Options, | ||
} from '@hey-api/client-fetch'; | ||
|
||
import type { | ||
CreatePetsError, | ||
ListPetsData, | ||
ListPetsError, | ||
ListPetsResponse, | ||
ShowPetByIdData, | ||
ShowPetByIdError, | ||
ShowPetByIdResponse, | ||
} from './types.gen'; | ||
|
||
export const client = createClient(createConfig()); | ||
|
||
/** | ||
* List all pets | ||
*/ | ||
export const listPets = <ThrowOnError extends boolean = false>( | ||
options?: Options<ListPetsData, ThrowOnError>, | ||
) => | ||
(options?.client ?? client).get< | ||
ListPetsResponse, | ||
ListPetsError, | ||
ThrowOnError | ||
>({ | ||
...options, | ||
url: '/pets', | ||
}); | ||
|
||
/** | ||
* Create a pet | ||
*/ | ||
export const createPets = <ThrowOnError extends boolean = false>( | ||
options?: Options<unknown, ThrowOnError>, | ||
) => | ||
(options?.client ?? client).post<unknown, CreatePetsError, ThrowOnError>({ | ||
...options, | ||
url: '/pets', | ||
}); | ||
|
||
/** | ||
* Info for a specific pet | ||
*/ | ||
export const showPetById = <ThrowOnError extends boolean = false>( | ||
options: Options<ShowPetByIdData, ThrowOnError>, | ||
) => | ||
(options?.client ?? client).get< | ||
ShowPetByIdResponse, | ||
ShowPetByIdError, | ||
ThrowOnError | ||
>({ | ||
...options, | ||
url: '/pets/{petId}', | ||
}); |
This file contains 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,46 @@ | ||
// This file is auto-generated by @hey-api/openapi-ts | ||
|
||
export type Pet = { | ||
id: number; | ||
name: string; | ||
tag?: string; | ||
}; | ||
|
||
export type Pets = Array<Pet>; | ||
|
||
export type Error = { | ||
code: number; | ||
message: string; | ||
}; | ||
|
||
export type ListPetsData = { | ||
body?: never; | ||
path?: never; | ||
query?: { | ||
/** | ||
* How many items to return at one time (max 100) | ||
*/ | ||
limit?: number; | ||
}; | ||
}; | ||
|
||
export type ListPetsError = Error; | ||
|
||
export type ListPetsResponse = Pets; | ||
|
||
export type CreatePetsError = Error; | ||
|
||
export type ShowPetByIdData = { | ||
body?: never; | ||
path: { | ||
/** | ||
* The id of the pet to retrieve | ||
*/ | ||
petId: string; | ||
}; | ||
query?: never; | ||
}; | ||
|
||
export type ShowPetByIdError = Error; | ||
|
||
export type ShowPetByIdResponse = Pet; |
This file contains 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,14 @@ | ||
import type { Pet, RouteHandlers } from './gen/fastify.gen'; | ||
|
||
export const serviceHandlers: Pick<RouteHandlers, 'showPetById'> = { | ||
showPetById(request, reply) { | ||
const { | ||
params: { petId }, | ||
} = request; | ||
const pet: Pet = { | ||
id: Number(petId), | ||
name: 'petname', | ||
}; | ||
reply.code(200).send(pet); | ||
}, | ||
}; |
This file contains 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,10 @@ | ||
import { buildServer } from './server'; | ||
|
||
const fastify = buildServer(); | ||
|
||
fastify.listen({ port: 3000 }, function (err) { | ||
if (err) { | ||
fastify.log.error(err); | ||
process.exit(1); | ||
} | ||
}); |
This file contains 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,23 @@ | ||
import Fastify from 'fastify'; | ||
import glue from 'fastify-openapi-glue'; | ||
|
||
import { serviceHandlers } from './handlers'; | ||
|
||
export const buildServer = () => { | ||
const fastify = Fastify(); | ||
|
||
const specification = fetch( | ||
'https://gist.githubusercontent.com/seriousme/55bd4c8ba2e598e416bb5543dcd362dc/raw/cf0b86ba37bb54bf1a6bf047c0ecf2a0ce4c62e0/petstore-v3.1.json', | ||
) | ||
.then((reply) => reply.json()) | ||
.then((data) => data); | ||
console.log(specification); | ||
|
||
fastify.register(glue, { | ||
prefix: 'v3', | ||
serviceHandlers, | ||
specification, | ||
}); | ||
|
||
return fastify; | ||
}; |
This file contains 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,31 @@ | ||
import { type FastifyInstance } from 'fastify'; | ||
import { client, showPetById } from 'src/gen'; | ||
import { buildServer } from 'src/server'; | ||
import { afterAll, beforeAll, describe, expect, test } from 'vitest'; | ||
|
||
describe('/pet/findByTags', () => { | ||
let server: FastifyInstance; | ||
|
||
beforeAll(async () => { | ||
server = buildServer(); | ||
await server.listen(); | ||
|
||
// @ts-ignore | ||
const baseUrl = `http://localhost:${server.server.address().port}/v3`; | ||
client.setConfig({ baseUrl }); | ||
}); | ||
|
||
afterAll(async () => { | ||
await Promise.all([server.close()]); | ||
}); | ||
|
||
test('showPetById', async () => { | ||
const result = await showPetById({ | ||
client, | ||
path: { | ||
petId: '123', | ||
}, | ||
}); | ||
expect(result.response.status).toBe(200); | ||
}); | ||
}); |
This file contains 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,13 @@ | ||
{ | ||
"include": ["src/**/*", "test/**/*"], | ||
"compilerOptions": { | ||
"lib": ["es2023"], | ||
"target": "es2022", | ||
"module": "ESNext", | ||
"moduleResolution": "Bundler", | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"baseUrl": "." | ||
} | ||
} |
This file contains 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 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 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 { DefineConfig, PluginConfig } from '../types'; | ||
import { handler } from './plugin'; | ||
import type { Config } from './types'; | ||
|
||
export const defaultConfig: PluginConfig<Config> = { | ||
_dependencies: ['@hey-api/services'], | ||
_handler: handler, | ||
_handlerLegacy: () => {}, | ||
name: 'fastify', | ||
operationId: true, | ||
output: 'fastify', | ||
}; | ||
|
||
/** | ||
* Type helper for the Fastify plugin, returns {@link PluginConfig} object | ||
*/ | ||
export const defineConfig: DefineConfig<Config> = (config) => ({ | ||
...defaultConfig, | ||
...config, | ||
}); | ||
This file contains 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 { defaultConfig, defineConfig } from './config'; | ||
export type { Config } from './types'; |
Oops, something went wrong.