Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions index.d.ts

This file was deleted.

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -33,8 +33,5 @@
},
"dependencies": {
"fast-json-stringify": "^5.0.0"
},
"tsd": {
"directory": "test/types"
}
}
16 changes: 0 additions & 16 deletions test/types/index.test-d.ts

This file was deleted.

21 changes: 0 additions & 21 deletions test/types/standalone.test-d.ts

This file was deleted.

45 changes: 45 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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): void;
}

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<FastJsonStringifyFactory>): ReturnType<FastJsonStringifyFactory>
export = SerializerSelector
139 changes: 139 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { expectAssignable, expectError, expectType } from "tsd";
import SerializerSelector, {
RouteDefinition,
Serializer,
SerializerCompiler,
SerializerFactory,
SerializerSelector as SerializerSelectorNamed,
StandaloneSerializer,
} from "..";

/**
* SerializerSelector
*/

{
const compiler = SerializerSelector();
expectType<SerializerFactory>(compiler);
}

{
const compiler = SerializerSelectorNamed();
expectType<SerializerFactory>(compiler);
}

{
{
const sampleSchema = {
$id: 'example1',
type: 'object',
properties: {
name: { type: 'string' }
}
}

const externalSchemas1 = {}

const factory = SerializerSelector()
expectType<SerializerFactory>(factory);
const compiler = factory(externalSchemas1, {})
expectType<SerializerCompiler>(compiler);
const serializeFunc = compiler({ schema: sampleSchema })
expectType<Serializer>(serializeFunc);

expectType<string>(serializeFunc({ name: 'hello' }))
}
}

/**
* StandaloneSerializer
*/

const reader = StandaloneSerializer({
readMode: true,
restoreFunction: (route: RouteDefinition) => {
expectAssignable<RouteDefinition>(route)
},
});
expectType<SerializerFactory>(reader);

const writer = StandaloneSerializer({
readMode: false,
storeFunction: (route: RouteDefinition, code: string) => {
expectAssignable<RouteDefinition>(route)
expectAssignable<string>(code)
},
});
expectType<SerializerFactory>(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<SerializerFactory>(StandaloneSerializer({
storeFunction (routeOpts, schemaSerializerCode) {
expectType<RouteDefinition>(routeOpts)
expectType<string>(schemaSerializerCode)
}
}))

expectType<SerializerFactory>(StandaloneSerializer({
readMode: true,
restoreFunction (routeOpts) {
expectType<RouteDefinition>(routeOpts)
}
}))

const factory = StandaloneSerializer({
readMode: false,
storeFunction (routeOpts, schemaSerializerCode) {
expectType<RouteDefinition>(routeOpts)
expectType<string>(schemaSerializerCode)
}
})
expectType<SerializerFactory>(factory)

const compiler = factory(schemaMap)
expectType<SerializerCompiler>(compiler)
expectType<Serializer>(compiler(endpointSchema))
}