-
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1286 from hey-api/feat/fastify-plugin
feat: add fastify plugin
- Loading branch information
Showing
115 changed files
with
14,693 additions
and
1,502 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': patch | ||
--- | ||
|
||
fix: export a map of error and response types by status code |
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` 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,5 @@ | ||
--- | ||
'@hey-api/openapi-ts': patch | ||
--- | ||
|
||
fix: deprecate types.tree option |
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
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,75 @@ | ||
--- | ||
title: Fastify | ||
description: Fastify plugin for Hey API. Compatible with all our features. | ||
--- | ||
|
||
# Fastify | ||
|
||
::: warning | ||
Fastify plugin is currently in beta. The interface might change before it becomes stable. We encourage you to leave feedback on [GitHub](https://github.com/hey-api/openapi-ts/issues). | ||
::: | ||
|
||
[Fastify](https://fastify.dev/) is a fast and low overhead web framework for Node.js. | ||
|
||
<button class="buttonLink" @click="(event) => embedProject('hey-api-client-fetch-plugin-fastify-example')(event)"> | ||
Live demo | ||
</button> | ||
|
||
## Features | ||
|
||
- seamless integration with `@hey-api/openapi-ts` ecosystem | ||
- type-safe route handlers | ||
- minimal learning curve thanks to extending the underlying technology | ||
|
||
## Installation | ||
|
||
::: warning | ||
Fastify plugin works only with the [experimental parser](/openapi-ts/configuration#parser) which is currently an opt-in feature. | ||
::: | ||
|
||
Ensure you have already [configured](/openapi-ts/get-started) `@hey-api/openapi-ts`. Update your configuration to use the Fastify plugin. | ||
|
||
```js | ||
export default { | ||
client: '@hey-api/client-fetch', | ||
experimentalParser: true, // [!code ++] | ||
input: 'path/to/openapi.json', | ||
output: 'src/client', | ||
plugins: [ | ||
// ...other plugins | ||
'fastify', // [!code ++] | ||
], | ||
}; | ||
``` | ||
|
||
You can now generate Fastify artifacts. 🎉 | ||
|
||
## Output | ||
|
||
The Fastify plugin will generate the following artifacts, depending on the input specification. | ||
|
||
## Route Handlers | ||
|
||
Route handlers are generated from all endpoints. The generated interface follows the naming convention of services. | ||
|
||
```ts | ||
const fastify = Fastify(); | ||
const serviceHandlers: RouteHandlers = { | ||
createPets(request, reply) { | ||
reply.code(201).send(); | ||
}, | ||
listPets(request, reply) { | ||
reply.code(200).send([]); | ||
}, | ||
showPetById(request, reply) { | ||
reply.code(200).send({ | ||
id: Number(request.params.petId), | ||
name: 'Kitty', | ||
}); | ||
}, | ||
}; | ||
fastify.register(glue, { serviceHandlers }); | ||
``` | ||
|
||
<!--@include: ../examples.md--> | ||
<!--@include: ../sponsorship.md--> |
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
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,14 @@ | ||
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: { | ||
format: 'prettier', | ||
lint: 'eslint', | ||
path: './src/client', | ||
}, | ||
plugins: ['fastify', '@hey-api/services'], | ||
}); |
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 @@ | ||
{ | ||
"name": "@example/openapi-ts-fastify", | ||
"private": true, | ||
"version": "0.0.1", | ||
"type": "module", | ||
"scripts": { | ||
"openapi-ts": "openapi-ts", | ||
"test": "vitest", | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"dependencies": { | ||
"@hey-api/client-fetch": "workspace:*", | ||
"fastify": "5.0.0", | ||
"fastify-openapi-glue": "4.7.1" | ||
}, | ||
"devDependencies": { | ||
"@hey-api/openapi-ts": "workspace:*", | ||
"eslint": "9.6.0", | ||
"prettier": "3.3.2", | ||
"typescript": "5.5.3", | ||
"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,25 @@ | ||
// This file is auto-generated by @hey-api/openapi-ts | ||
|
||
import type { RouteHandler } from 'fastify'; | ||
|
||
import type { | ||
CreatePetsResponses, | ||
ListPetsData, | ||
ListPetsResponses, | ||
ShowPetByIdData, | ||
ShowPetByIdResponses, | ||
} from './types.gen'; | ||
|
||
export type RouteHandlers = { | ||
createPets: RouteHandler<{ | ||
Reply: CreatePetsResponses; | ||
}>; | ||
listPets: RouteHandler<{ | ||
Querystring?: ListPetsData['query']; | ||
Reply: ListPetsResponses; | ||
}>; | ||
showPetById: RouteHandler<{ | ||
Params: ShowPetByIdData['path']; | ||
Reply: ShowPetByIdResponses; | ||
}>; | ||
}; |
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,89 @@ | ||
// 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 ListPetsErrors = { | ||
/** | ||
* unexpected error | ||
*/ | ||
default: Error; | ||
}; | ||
|
||
export type ListPetsError = ListPetsErrors[keyof ListPetsErrors]; | ||
|
||
export type ListPetsResponses = { | ||
/** | ||
* A paged array of pets | ||
*/ | ||
200: Pets; | ||
}; | ||
|
||
export type ListPetsResponse = ListPetsResponses[keyof ListPetsResponses]; | ||
|
||
export type CreatePetsErrors = { | ||
/** | ||
* unexpected error | ||
*/ | ||
default: Error; | ||
}; | ||
|
||
export type CreatePetsError = CreatePetsErrors[keyof CreatePetsErrors]; | ||
|
||
export type CreatePetsResponses = { | ||
/** | ||
* Null response | ||
*/ | ||
201: unknown; | ||
}; | ||
|
||
export type ShowPetByIdData = { | ||
body?: never; | ||
path: { | ||
/** | ||
* The id of the pet to retrieve | ||
*/ | ||
petId: string; | ||
}; | ||
query?: never; | ||
}; | ||
|
||
export type ShowPetByIdErrors = { | ||
/** | ||
* unexpected error | ||
*/ | ||
default: Error; | ||
}; | ||
|
||
export type ShowPetByIdError = ShowPetByIdErrors[keyof ShowPetByIdErrors]; | ||
|
||
export type ShowPetByIdResponses = { | ||
/** | ||
* Expected response to a valid request | ||
*/ | ||
200: Pet; | ||
}; | ||
|
||
export type ShowPetByIdResponse = | ||
ShowPetByIdResponses[keyof ShowPetByIdResponses]; |
Oops, something went wrong.