diff --git a/types/index.d.ts b/types/index.d.ts index 0749dfa..dc93d32 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,4 +1,4 @@ -import _ajv, { AnySchema, Options as AjvOptions, ValidateFunction } from 'ajv' +import _ajv, { AnySchema, Options as AjvOptions, ValidateFunction, Plugin } from 'ajv' import AjvJTD, { JTDOptions } from 'ajv/dist/jtd' import type { Options, ErrorObject } from 'ajv' import { AnyValidateFunction } from 'ajv/dist/core' @@ -9,10 +9,15 @@ type AjvSerializerGenerator = typeof AjvCompiler type AjvJTDCompile = AjvJTD['compileSerializer'] type AjvCompile = (schema: AnySchema, _meta?: boolean) => AnyValidateFunction -declare function buildCompilerFromPool (externalSchemas: { [key: string]: AnySchema | AnySchema[] }, options?: { mode: 'JTD'; customOptions?: JTDOptions; onCreate?: (ajvInstance: Ajv) => void }): AjvCompile -declare function buildCompilerFromPool (externalSchemas: { [key: string]: AnySchema | AnySchema[] }, options?: { mode?: never; customOptions?: AjvOptions; onCreate?: (ajvInstance: Ajv) => void }): AjvCompile +type SharedCompilerOptions = { + onCreate?: (ajvInstance: Ajv) => void; + plugins?: Plugin[]; +} + +type BuildAjvJtdCompilerFromPool = (externalSchemas: { [key: string]: AnySchema | AnySchema[] }, options?: SharedCompilerOptions & { mode: 'JTD'; customOptions?: JTDOptions }) => AjvCompile +type BuildAjvCompilerFromPool = (externalSchemas: { [key: string]: AnySchema | AnySchema[] }, options?: SharedCompilerOptions & { mode?: never; customOptions?: AjvOptions }) => AjvCompile -declare function buildSerializerFromPool (externalSchemas: any, serializerOpts?: { mode?: never; } & JTDOptions): AjvJTDCompile +type BuildJtdSerializerFromPool = (externalSchemas: any, serializerOpts?: { mode?: never; } & JTDOptions) => AjvJTDCompile declare function AjvCompiler (opts: { jtdSerializer: true }): AjvCompiler.BuildSerializerFromPool declare function AjvCompiler (opts?: { jtdSerializer?: false }): AjvCompiler.BuildCompilerFromPool @@ -23,9 +28,9 @@ declare namespace AjvCompiler { export type { Options, ErrorObject } export { Ajv } - export type BuildSerializerFromPool = typeof buildSerializerFromPool + export type BuildSerializerFromPool = BuildJtdSerializerFromPool - export type BuildCompilerFromPool = typeof buildCompilerFromPool + export type BuildCompilerFromPool = BuildAjvCompilerFromPool & BuildAjvJtdCompilerFromPool export const AjvReference: Symbol diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 391e7b7..617d10e 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -224,3 +224,22 @@ expectType(AjvReference) expectAssignable(compiler) expectType>(compiler(endpointSchema)) } + +// Plugins +{ + const factory = AjvCompiler() + const compiler = factory({}, { + plugins: [ + (ajv) => { + expectType(ajv) + return ajv + }, + (ajv, options) => { + expectType(ajv) + expectType(options) + return ajv + } + ] + }) + expectAssignable(compiler) +}