-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.mts
92 lines (89 loc) · 2.96 KB
/
index.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import {
FastifyPluginAsync,
FastifyPluginCallback,
FastifyPluginOptions,
FastifySchemaCompiler,
FastifyTypeProvider,
RawServerBase,
RawServerDefault
} from 'fastify'
import { TypeCompiler } from '@sinclair/typebox/compiler'
import { Static, TSchema } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'
export * from '@sinclair/typebox'
/**
* Enables TypeBox schema validation
*
* @example
* ```typescript
* import Fastify from 'fastify'
*
* const server = Fastify().setValidatorCompiler(TypeBoxValidatorCompiler)
* ```
*/
export const TypeBoxValidatorCompiler: FastifySchemaCompiler<TSchema> = ({ schema, httpPart }) => {
const typeCheck = TypeCompiler.Compile(schema)
return (value): any => {
// Note: Only support value conversion for querystring, params and header schematics
const converted = httpPart === 'body' ? value : Value.Convert(schema, value)
if (typeCheck.Check(converted)) {
return { value: converted }
}
const errors = [...typeCheck.Errors(converted)]
return {
// Note: Here we return a FastifySchemaValidationError[] result. As of writing, Fastify
// does not currently export this type. Future revisions should uncomment the assertion
// below and return the full set of properties. The specified properties 'message' and
// 'instancePath' do however result in a near equivalent error messages to Ajv.
error: errors.map((error) => ({
message: `${error.message}`,
instancePath: error.path
})) // as FastifySchemaValidationError[]
}
}
}
/**
* Enables automatic type inference on a Fastify instance.
*
* @example
* ```typescript
* import Fastify from 'fastify'
*
* const server = Fastify().withTypeProvider<TypeBoxTypeProvider>()
* ```
*/
export interface TypeBoxTypeProvider extends FastifyTypeProvider {
validator: this['schema'] extends TSchema ? Static<this['schema']> : unknown
serializer: this['schema'] extends TSchema ? Static<this['schema']> : unknown
}
/**
* FastifyPluginCallback with Typebox automatic type inference
*
* @example
* ```typescript
* import { FastifyPluginCallbackTypebox } fromg "@fastify/type-provider-typebox"
*
* const plugin: FastifyPluginCallbackTypebox = (fastify, options, done) => {
* done()
* }
* ```
*/
export type FastifyPluginCallbackTypebox<
Options extends FastifyPluginOptions = Record<never, never>,
Server extends RawServerBase = RawServerDefault,
> = FastifyPluginCallback<Options, Server, TypeBoxTypeProvider>
/**
* FastifyPluginAsync with Typebox automatic type inference
*
* @example
* ```typescript
* import { FastifyPluginAsyncTypebox } from "@fastify/type-provider-typebox"
*
* const plugin: FastifyPluginAsyncTypebox = async (fastify, options) => {
* }
* ```
*/
export type FastifyPluginAsyncTypebox<
Options extends FastifyPluginOptions = Record<never, never>,
Server extends RawServerBase = RawServerDefault
> = FastifyPluginAsync<Options, Server, TypeBoxTypeProvider>