Skip to content

Commit

Permalink
Add Documentation and TS Types missed for FastifyInstance#setSchemaCo…
Browse files Browse the repository at this point in the history
…ntroller (#3480)
  • Loading branch information
Grubba27 committed Feb 3, 2022
1 parent 7cdc903 commit 537901e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
24 changes: 24 additions & 0 deletions test/types/instance.test-d.ts
Expand Up @@ -10,6 +10,7 @@ import { expectAssignable, expectError, expectType } from 'tsd'
import { FastifyRequest } from '../../types/request'
import { FastifyReply } from '../../types/reply'
import { HookHandlerDoneFunction } from '../../types/hooks'
import { FastifySchemaControllerOptions } from '../../types/schema'

const server = fastify()

Expand Down Expand Up @@ -112,6 +113,29 @@ server.setNotFoundHandler({ preHandler: notFoundpreHandlerHandler, preValidation
function invalidErrorHandler (error: number) {}
expectError(server.setErrorHandler(invalidErrorHandler))

server.setSchemaController({
bucket: (parentSchemas: unknown) => {
return {
addSchema (schema: unknown) {
expectType<unknown>(schema)
expectType<FastifyInstance>(server.addSchema({ type: 'null' }))
return server.addSchema({ type: 'null' })
},
getSchema (schemaId: string) {
expectType<string>(schemaId)
return server.getSchema('SchemaId')
},
getSchemas () {
expectType<Record<string, unknown>>(server.getSchemas())
return server.getSchemas()
}
}
}
})

function invalidSchemaController (schemaControllerOptions: FastifySchemaControllerOptions) {}
expectError(server.setSchemaController(invalidSchemaController))

server.setReplySerializer(function (payload, statusCode) {
expectType<unknown>(payload)
expectType<number>(statusCode)
Expand Down
13 changes: 12 additions & 1 deletion types/instance.d.ts
@@ -1,6 +1,12 @@
import { Chain as LightMyRequestChain, InjectOptions, Response as LightMyRequestResponse, CallbackFunc as LightMyRequestCallback } from 'light-my-request'
import { RouteOptions, RouteShorthandMethod, RouteGenericInterface, DefaultRoute } from './route'
import { FastifySchema, FastifySchemaCompiler, FastifySchemaValidationError, FastifySerializerCompiler } from './schema'
import {
FastifySchema,
FastifySchemaCompiler,
FastifySchemaValidationError,
FastifySerializerCompiler,
FastifySchemaControllerOptions
} from './schema'
import { RawServerBase, RawRequestDefaultExpression, RawServerDefault, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
import { FastifyLoggerInstance } from './logger'
import { FastifyRegister } from './register'
Expand Down Expand Up @@ -380,6 +386,11 @@ export interface FastifyInstance<
*/
setSerializerCompiler<T = FastifySchema>(schemaCompiler: FastifySerializerCompiler<T>): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;

/**
* Set the schema controller for all routes.
*/
setSchemaController(schemaControllerOpts: FastifySchemaControllerOptions): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;

/**
* Set the reply serializer for all routes.
*/
Expand Down
14 changes: 14 additions & 0 deletions types/schema.d.ts
@@ -1,3 +1,5 @@
import { ValidatorCompiler } from '@fastify/ajv-compiler'
import { FastifyInstance, FastifyServerOptions } from '../fastify'
/**
* Schemas in Fastify follow the JSON-Schema standard. For this reason
* we have opted to not ship strict schema based types. Instead we provide
Expand Down Expand Up @@ -36,3 +38,15 @@ export interface FastifyValidationResult {
export type FastifySchemaCompiler<T> = (routeSchema: FastifyRouteSchemaDef<T>) => FastifyValidationResult

export type FastifySerializerCompiler<T> = (routeSchema: FastifyRouteSchemaDef<T>) => (data: any) => string

export interface FastifySchemaControllerOptions{
bucket?: (parentSchemas?: unknown) => {
addSchema(schema: unknown): FastifyInstance;
getSchema(schemaId: string): unknown;
getSchemas(): Record<string, unknown>;
};
compilersFactory?: {
buildValidator?: ValidatorCompiler;
buildSerializer?: (externalSchemas: unknown, serializerOptsServerOption: FastifyServerOptions['serializerOpts']) => FastifySerializerCompiler<unknown>;
};
}

0 comments on commit 537901e

Please sign in to comment.