From aff59e6b5f81010fc9751428e7e0d03784569079 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Mon, 5 Dec 2022 11:00:03 +0100 Subject: [PATCH 1/7] nodenext compatibility --- index.d.ts | 30 ------------------- package.json | 5 +--- test/types/index.test-d.ts | 16 ---------- types/index.d.ts | 29 ++++++++++++++++++ .../index.test-d.ts | 25 ++++++++++++++-- 5 files changed, 53 insertions(+), 52 deletions(-) delete mode 100644 index.d.ts delete mode 100644 test/types/index.test-d.ts create mode 100644 types/index.d.ts rename test/types/standalone.test-d.ts => types/index.test-d.ts (55%) 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/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..dd1e40b --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,29 @@ +import { Options } from 'fast-json-stringify' + +declare namespace SerializerSelector { + export type SerializerCompiler = ( + externalSchemas: unknown, + options: Options + ) => (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, + } + + export type { Options } + export function SerializerSelector(): SerializerCompiler; + export function StandaloneSerializer(options: StandaloneOptions): SerializerCompiler; + + export { SerializerSelector as default } +} + +export = SerializerSelector diff --git a/test/types/standalone.test-d.ts b/types/index.test-d.ts similarity index 55% rename from test/types/standalone.test-d.ts rename to types/index.test-d.ts index 789b40e..d6ccf74 100644 --- a/test/types/standalone.test-d.ts +++ b/types/index.test-d.ts @@ -1,7 +1,28 @@ import { expectAssignable, expectType } from "tsd"; +import SerializerSelector, { + RouteDefinition, + SerializerCompiler, + SerializerSelector as SerializerSelectorNamed, + StandaloneSerializer, +} from ".."; -import { StandaloneSerializer, RouteDefinition } from "../../"; -import { SerializerCompiler } from "../.."; +/** + * SerializerSelector + */ + +{ + const compiler = SerializerSelector(); + expectType(compiler); +} + +{ + const compiler = SerializerSelectorNamed(); + expectType(compiler); +} + +/** + * StandaloneSerializer + */ const reader = StandaloneSerializer({ readMode: true, From 0d34c83a7743b474ef17d9e3ae49693a37e029da Mon Sep 17 00:00:00 2001 From: uzlopak Date: Mon, 5 Dec 2022 11:01:57 +0100 Subject: [PATCH 2/7] add missing function declaration --- types/index.d.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index dd1e40b..9581d54 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,5 +1,7 @@ import { Options } from 'fast-json-stringify' +type FastJsonStringify = SerializerSelector.SerializerCompiler + declare namespace SerializerSelector { export type SerializerCompiler = ( externalSchemas: unknown, @@ -20,10 +22,11 @@ declare namespace SerializerSelector { } export type { Options } - export function SerializerSelector(): SerializerCompiler; + export function SerializerSelector(): FastJsonStringify; export function StandaloneSerializer(options: StandaloneOptions): SerializerCompiler; export { SerializerSelector as default } } +declare function SerializerSelector(): FastJsonStringify; export = SerializerSelector From 5ef30f248740839a2ccebc048e132687f08745d5 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Mon, 5 Dec 2022 11:02:28 +0100 Subject: [PATCH 3/7] fix wrong typing --- types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index 9581d54..d09d94c 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -22,7 +22,7 @@ declare namespace SerializerSelector { } export type { Options } - export function SerializerSelector(): FastJsonStringify; + export const SerializerSelector: FastJsonStringify; export function StandaloneSerializer(options: StandaloneOptions): SerializerCompiler; export { SerializerSelector as default } From 142392e8239891d089a98836237d4da5ec016082 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Mon, 5 Dec 2022 12:28:17 +0100 Subject: [PATCH 4/7] fix typings --- types/index.d.ts | 43 ++++++++++++------- types/index.test-d.ts | 97 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 120 insertions(+), 20 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index d09d94c..1b631b9 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,32 +1,45 @@ import { Options } from 'fast-json-stringify' -type FastJsonStringify = SerializerSelector.SerializerCompiler +type FastJsonStringifyFactory = () => SerializerSelector.SerializerFactory declare namespace SerializerSelector { + export type SerializerFactory = ( + externalSchemas?: unknown, + options?: Options + ) => SerializerCompiler; + export type SerializerCompiler = ( - externalSchemas: unknown, - options: Options - ) => (doc: any) => string; + externalSchemas?: unknown, + options?: Options + ) => Serializer; + + export type Serializer = (doc: any) => string export type RouteDefinition = { - method: string, - url: string, - httpStatus: string, - schema?: unknown, + method: string; + url: string; + httpStatus: string; + schema?: unknown; + } + + export type StandaloneOptions = StandaloneOptionsReadModeOn | StandaloneOptionsReadModeOff + + export type StandaloneOptionsReadModeOn = { + readMode: true; + restoreFunction?(opts: RouteDefinition): void; } - export interface StandaloneOptions { - readMode: Boolean, - storeFunction?(opts: RouteDefinition, schemaSerializationCode: string): void, - restoreFunction?(opts: RouteDefinition): void, + export type StandaloneOptionsReadModeOff = { + readMode: false | undefined; + storeFunction?(opts: RouteDefinition, schemaSerializationCode: string): void; } export type { Options } - export const SerializerSelector: FastJsonStringify; - export function StandaloneSerializer(options: StandaloneOptions): SerializerCompiler; + export const SerializerSelector: FastJsonStringifyFactory; + export function StandaloneSerializer(options: StandaloneOptions): SerializerFactory; export { SerializerSelector as default } } -declare function SerializerSelector(): FastJsonStringify; +declare function SerializerSelector(...params: Parameters): ReturnType export = SerializerSelector diff --git a/types/index.test-d.ts b/types/index.test-d.ts index d6ccf74..db20795 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -1,7 +1,9 @@ -import { expectAssignable, expectType } from "tsd"; +import { expectAssignable, expectError, expectType } from "tsd"; import SerializerSelector, { RouteDefinition, + Serializer, SerializerCompiler, + SerializerFactory, SerializerSelector as SerializerSelectorNamed, StandaloneSerializer, } from ".."; @@ -12,12 +14,35 @@ import SerializerSelector, { { const compiler = SerializerSelector(); - expectType(compiler); + expectType(compiler); } { const compiler = SerializerSelectorNamed(); - expectType(compiler); + 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' })) + } } /** @@ -30,7 +55,7 @@ const reader = StandaloneSerializer({ expectAssignable(route) }, }); -expectType(reader); +expectType(reader); const writer = StandaloneSerializer({ readMode: false, @@ -39,4 +64,66 @@ const writer = StandaloneSerializer({ expectAssignable(code) }, }); -expectType(writer); \ No newline at end of file +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 () {} + })) + + expectType(StandaloneSerializer({ + readMode: true, + restoreFunction (routeOpts) { + expectType(routeOpts) + } + })) + + const factory = StandaloneSerializer({ + readMode: false, + storeFunction (routeOpts, schemaSerializerCode) { + expectType(routeOpts) + expectType(schemaSerializerCode) + } + }) + expectType(factory) + + const compiler = factory(schemaMap) + expectType(compiler) + expectType(compiler(endpointSchema)) +} \ No newline at end of file From a737dc7de3993a812c72fd301f8ac7d5eefadb75 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Mon, 5 Dec 2022 12:33:06 +0100 Subject: [PATCH 5/7] fix optional readmode typing --- types/index.d.ts | 2 +- types/index.test-d.ts | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index 1b631b9..9867e0a 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -30,7 +30,7 @@ declare namespace SerializerSelector { } export type StandaloneOptionsReadModeOff = { - readMode: false | undefined; + readMode?: false | undefined; storeFunction?(opts: RouteDefinition, schemaSerializationCode: string): void; } diff --git a/types/index.test-d.ts b/types/index.test-d.ts index db20795..8204f1e 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -106,6 +106,16 @@ expectType(writer); readMode: false, restoreFunction () {} })) + expectError(StandaloneSerializer({ + restoreFunction () {} + })) + + expectType(StandaloneSerializer({ + storeFunction (routeOpts, schemaSerializerCode) { + expectType(routeOpts) + expectType(schemaSerializerCode) + } + })) expectType(StandaloneSerializer({ readMode: true, From b10d8639f06c2bcd602718e7ec47e70b2aac89b8 Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Mon, 5 Dec 2022 12:36:13 +0100 Subject: [PATCH 6/7] Add missing EOF newline --- types/index.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 8204f1e..17fd54a 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -136,4 +136,4 @@ expectType(writer); const compiler = factory(schemaMap) expectType(compiler) expectType(compiler(endpointSchema)) -} \ No newline at end of file +} From bc5e0a3402aaac1701bd4c9ab848c720c7190898 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Mon, 5 Dec 2022 16:47:33 +0100 Subject: [PATCH 7/7] restoreFunction needs to return Serializer --- types/index.d.ts | 2 +- types/index.test-d.ts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index 9867e0a..26bbf1f 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -26,7 +26,7 @@ declare namespace SerializerSelector { export type StandaloneOptionsReadModeOn = { readMode: true; - restoreFunction?(opts: RouteDefinition): void; + restoreFunction?(opts: RouteDefinition): Serializer; } export type StandaloneOptionsReadModeOff = { diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 17fd54a..d1dbd85 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -53,6 +53,7 @@ const reader = StandaloneSerializer({ readMode: true, restoreFunction: (route: RouteDefinition) => { expectAssignable(route) + return {} as Serializer }, }); expectType(reader); @@ -121,6 +122,7 @@ expectType(writer); readMode: true, restoreFunction (routeOpts) { expectType(routeOpts) + return {} as Serializer } }))