generated from toomuchdesign/npm-package-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add fastifyTypeProviderPlugin plugin
- Loading branch information
1 parent
61e4cfe
commit c7b4a54
Showing
9 changed files
with
153 additions
and
22 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 @@ | ||
--- | ||
'openapi-ts-json-schema': minor | ||
--- | ||
|
||
Add `fastifyTypeProviderPlugin` 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
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 @@ | ||
# Plugins | ||
|
||
## Fastify type provider plugin | ||
|
||
This plugin generate the necessary connective tissue to optimally integrate `openapi-ts-json-schema` output with Fastify's [`json-schema-to-ts` type provider](https://github.com/fastify/fastify-type-provider-json-schema-to-ts) preserving JSON schemas `$ref`s. | ||
|
||
The plugin generates a `fastifyTypeProvider.ts` file under `outputPath` exposing: | ||
|
||
- `referenceSchemas`: an array containing all the `$ref` schemas found with relevant `$id` property ready to be registered with [`fastify.addSchema`](https://fastify.dev/docs/latest/Reference/Server/#addschema) | ||
- `References` TS type specifically built to enable `json-schema-to-ts` to resolve `$ref` schema types | ||
|
||
Generate TypeScript JSON schemas: | ||
|
||
```ts | ||
import { | ||
openapiToTsJsonSchema, | ||
fastifyTypeProviderPlugin, | ||
} from 'openapi-ts-json-schema'; | ||
|
||
const { outputPath, metaData } = await openapiToTsJsonSchema({ | ||
openApiSchema: path.resolve(fixtures, 'path/to/open-api-spec.yaml'), | ||
outputPath: 'path/to/generated/schemas', | ||
definitionPathsToGenerateFrom: ['components.schemas', 'paths'], | ||
refHandling: 'keep', | ||
}); | ||
|
||
await fastifyTypeProviderPlugin({ outputPath, metaData }); | ||
``` | ||
|
||
Setup `Fastify` and `json-schema-to-ts` type provider: | ||
|
||
```ts | ||
import fastify from 'fastify'; | ||
import { JsonSchemaToTsProvider } from '@fastify/type-provider-json-schema-to-ts'; | ||
import { | ||
References, | ||
referenceSchemas, | ||
} from 'path/to/generated/schemas/fastifyTypeProvider'; | ||
|
||
// Enable @fastify/type-provider-json-schema-to-ts to resolve all found `$ref` schema types | ||
const server = | ||
fastify().withTypeProvider< | ||
JsonSchemaToTsProvider<JsonSchemaToTsProvider<{ references: References }>> | ||
>(); | ||
|
||
/** | ||
* Register `$ref` schemas individually so that they `$ref`s get resolved runtime. | ||
* This also enables @fastify.swagger to re-expose the schemas as shared components. | ||
*/ | ||
referenceSchemas.forEach((schema) => { | ||
fastify.addSchema(schema); | ||
}); | ||
|
||
// Reference the shared schema like the following | ||
fastify.get( | ||
'/profile', | ||
{ | ||
schema: { | ||
body: { | ||
type: 'object', | ||
properties: { | ||
user: { | ||
$ref: '#/components/schemas/User', | ||
}, | ||
}, | ||
required: ['user'], | ||
}, | ||
} as const, | ||
}, | ||
(req) => { | ||
// givenName and familyName will be correctly typed as strings! | ||
const { givenName, familyName } = req.body.user; | ||
}, | ||
); | ||
``` |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { openapiToTsJsonSchema } from './openapiToTsJsonSchema'; | ||
export { default as fastifyTypeProviderPlugin } from './plugins/fastifyTypeProviderPlugin'; |
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