diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 0d7ec24..0000000 --- a/index.d.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { Options as FJSOptions } from 'fast-json-stringify' - -export type { Options } from 'fast-json-stringify' - -export type SerializerCompiler = ( - externalSchemas: unknown, - options: FJSOptions -) => (doc: any) => string; - -export type RouteDefinition = { - method: string, - url: string, - httpStatus: string, - schema?: unknown, -} - -export interface StandaloneOptions { - readMode: Boolean, - storeFunction?(opts: RouteDefinition, schemaSerializationCode: string): void, - restoreFunction?(opts: RouteDefinition): void, -} - -declare function SerializerSelector(): SerializerCompiler; -declare function StandaloneSerializer(options: StandaloneOptions): SerializerCompiler; - -export default SerializerSelector; -export { - SerializerSelector, - StandaloneSerializer, -}; diff --git a/package.json b/package.json index d6a4120..461e571 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "description": "Build and manage the fast-json-stringify instances for the fastify framework", "version": "4.1.0", "main": "index.js", - "types": "index.d.ts", + "types": "types/index.d.ts", "scripts": { "lint": "standard", "lint:fix": "standard --fix", @@ -33,8 +33,5 @@ }, "dependencies": { "fast-json-stringify": "^5.0.0" - }, - "tsd": { - "directory": "test/types" } } diff --git a/test/types/index.test-d.ts b/test/types/index.test-d.ts deleted file mode 100644 index 9980fc9..0000000 --- a/test/types/index.test-d.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { expectType } from "tsd"; -import SerializerSelector, { - SerializerCompiler, - SerializerSelector as SerializerSelectorNamed, - StandaloneSerializer, -} from "../.."; - -{ - const compiler = SerializerSelector(); - expectType(compiler); -} - -{ - const compiler = SerializerSelectorNamed(); - expectType(compiler); -} \ No newline at end of file diff --git a/test/types/standalone.test-d.ts b/test/types/standalone.test-d.ts deleted file mode 100644 index 789b40e..0000000 --- a/test/types/standalone.test-d.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { expectAssignable, expectType } from "tsd"; - -import { StandaloneSerializer, RouteDefinition } from "../../"; -import { SerializerCompiler } from "../.."; - -const reader = StandaloneSerializer({ - readMode: true, - restoreFunction: (route: RouteDefinition) => { - expectAssignable(route) - }, -}); -expectType(reader); - -const writer = StandaloneSerializer({ - readMode: false, - storeFunction: (route: RouteDefinition, code: string) => { - expectAssignable(route) - expectAssignable(code) - }, -}); -expectType(writer); \ No newline at end of file diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..26bbf1f --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,45 @@ +import { Options } from 'fast-json-stringify' + +type FastJsonStringifyFactory = () => SerializerSelector.SerializerFactory + +declare namespace SerializerSelector { + export type SerializerFactory = ( + externalSchemas?: unknown, + options?: Options + ) => SerializerCompiler; + + export type SerializerCompiler = ( + externalSchemas?: unknown, + options?: Options + ) => Serializer; + + export type Serializer = (doc: any) => string + + export type RouteDefinition = { + method: string; + url: string; + httpStatus: string; + schema?: unknown; + } + + export type StandaloneOptions = StandaloneOptionsReadModeOn | StandaloneOptionsReadModeOff + + export type StandaloneOptionsReadModeOn = { + readMode: true; + restoreFunction?(opts: RouteDefinition): Serializer; + } + + export type StandaloneOptionsReadModeOff = { + readMode?: false | undefined; + storeFunction?(opts: RouteDefinition, schemaSerializationCode: string): void; + } + + export type { Options } + export const SerializerSelector: FastJsonStringifyFactory; + export function StandaloneSerializer(options: StandaloneOptions): SerializerFactory; + + export { SerializerSelector as default } +} + +declare function SerializerSelector(...params: Parameters): ReturnType +export = SerializerSelector diff --git a/types/index.test-d.ts b/types/index.test-d.ts new file mode 100644 index 0000000..d1dbd85 --- /dev/null +++ b/types/index.test-d.ts @@ -0,0 +1,141 @@ +import { expectAssignable, expectError, expectType } from "tsd"; +import SerializerSelector, { + RouteDefinition, + Serializer, + SerializerCompiler, + SerializerFactory, + SerializerSelector as SerializerSelectorNamed, + StandaloneSerializer, +} from ".."; + +/** + * SerializerSelector + */ + +{ + const compiler = SerializerSelector(); + expectType(compiler); +} + +{ + const compiler = SerializerSelectorNamed(); + expectType(compiler); +} + +{ + { + const sampleSchema = { + $id: 'example1', + type: 'object', + properties: { + name: { type: 'string' } + } + } + + const externalSchemas1 = {} + + const factory = SerializerSelector() + expectType(factory); + const compiler = factory(externalSchemas1, {}) + expectType(compiler); + const serializeFunc = compiler({ schema: sampleSchema }) + expectType(serializeFunc); + + expectType(serializeFunc({ name: 'hello' })) + } +} + +/** + * StandaloneSerializer + */ + +const reader = StandaloneSerializer({ + readMode: true, + restoreFunction: (route: RouteDefinition) => { + expectAssignable(route) + return {} as Serializer + }, +}); +expectType(reader); + +const writer = StandaloneSerializer({ + readMode: false, + storeFunction: (route: RouteDefinition, code: string) => { + expectAssignable(route) + expectAssignable(code) + }, +}); +expectType(writer); + +{ + const base = { + $id: 'urn:schema:base', + definitions: { + hello: { type: 'string' } + }, + type: 'object', + properties: { + hello: { $ref: '#/definitions/hello' } + } + } + + const refSchema = { + $id: 'urn:schema:ref', + type: 'object', + properties: { + hello: { $ref: 'urn:schema:base#/definitions/hello' } + } + } + + const endpointSchema = { + schema: { + $id: 'urn:schema:endpoint', + $ref: 'urn:schema:ref' + } + } + + const schemaMap = { + [base.$id]: base, + [refSchema.$id]: refSchema + } + + expectError(StandaloneSerializer({ + readMode: true, + storeFunction () { } + })) + expectError(StandaloneSerializer({ + readMode: false, + restoreFunction () {} + })) + expectError(StandaloneSerializer({ + restoreFunction () {} + })) + + expectType(StandaloneSerializer({ + storeFunction (routeOpts, schemaSerializerCode) { + expectType(routeOpts) + expectType(schemaSerializerCode) + } + })) + + expectType(StandaloneSerializer({ + readMode: true, + restoreFunction (routeOpts) { + expectType(routeOpts) + return {} as Serializer + } + })) + + const factory = StandaloneSerializer({ + readMode: false, + storeFunction (routeOpts, schemaSerializerCode) { + expectType(routeOpts) + expectType(schemaSerializerCode) + } + }) + expectType(factory) + + const compiler = factory(schemaMap) + expectType(compiler) + expectType(compiler(endpointSchema)) +}