forked from fastify/fastify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fastify Type Providers (fastify#3398)
- Loading branch information
1 parent
f98a2d6
commit 2965a38
Showing
19 changed files
with
713 additions
and
292 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,79 @@ | ||
<h1 align="center">Fastify</h1> | ||
|
||
## Type Providers | ||
|
||
Type Providers are a TypeScript only feature that enables Fastify to statically infer type information directly from inline JSON Schema. They are an alternative to specifying generic arguments on routes; and can greatly reduce the need to keep associated types for each schema defined in your project. | ||
|
||
### Providers | ||
|
||
Type Providers are offered as additional packages you will need to install into your project. Each provider uses a different inference library under the hood; allowing you to select the library most appropriate for your needs. Type Provider packages follow a `fastify-type-provider-{provider-name}` naming convention. | ||
|
||
The following inference packages are supported: | ||
|
||
- `json-schema-to-ts` - [github](https://github.com/ThomasAribart/json-schema-to-ts) | ||
- `typebox` - [github](https://github.com/sinclairzx81/typebox) | ||
|
||
### Json Schema to Ts | ||
|
||
The following sets up a `json-schema-to-ts` Type Provider | ||
|
||
```bash | ||
$ npm install fastify-type-provider-json-schema-to-ts --save | ||
``` | ||
|
||
```typescript | ||
import { JsonSchemaToTsTypeProvider } from 'fastify-type-provider-json-schema-to-ts' | ||
|
||
import fastify from 'fastify' | ||
|
||
const server = fastify().withTypeProvider<JsonSchemaToTsTypeProvider>() | ||
|
||
server.get('/route', { | ||
schema: { | ||
querystring: { | ||
type: 'object', | ||
properties: { | ||
foo: { type: 'number' }, | ||
bar: { type: 'string' }, | ||
}, | ||
required: ['foo', 'bar'] | ||
} | ||
} as const // don't forget to use const ! | ||
|
||
}, (request, reply) => { | ||
|
||
// type Query = { foo: number, bar: string } | ||
|
||
const { foo, bar } = request.query // type safe! | ||
}) | ||
``` | ||
|
||
### TypeBox | ||
|
||
The following sets up a TypeBox Type Provider | ||
|
||
```bash | ||
$ npm install fastify-type-provider-typebox --save | ||
``` | ||
|
||
```typescript | ||
import { TypeBoxTypeProvider, Type } from 'fastify-type-provider-typebox' | ||
|
||
import fastify from 'fastify' | ||
|
||
const server = fastify().withTypeProvider<TypeBoxTypeProvider>() | ||
|
||
server.get('/route', { | ||
schema: { | ||
querystring: Type.Object({ | ||
foo: Type.Number(), | ||
bar: Type.String() | ||
}) | ||
} | ||
}, (request, reply) => { | ||
|
||
// type Query = { foo: number, bar: string } | ||
|
||
const { foo, bar } = request.query // type safe! | ||
}) | ||
``` |
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,20 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const Fastify = require('..') | ||
|
||
test('Should export withTypeProvider function', t => { | ||
t.plan(1) | ||
try { | ||
Fastify().withTypeProvider() | ||
t.pass() | ||
} catch (e) { | ||
t.fail() | ||
} | ||
}) | ||
|
||
test('Should return same instance', t => { | ||
t.plan(1) | ||
const fastify = Fastify() | ||
t.equal(fastify, fastify.withTypeProvider()) | ||
}) |
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
Oops, something went wrong.