Skip to content

Commit b91042c

Browse files
authored
nodenext compatibility, correct typings (#22)
* nodenext compatibility * add missing function declaration * fix wrong typing * fix typings * fix optional readmode typing * Add missing EOF newline * restoreFunction needs to return Serializer
1 parent ebe7c7d commit b91042c

File tree

6 files changed

+187
-71
lines changed

6 files changed

+187
-71
lines changed

index.d.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Build and manage the fast-json-stringify instances for the fastify framework",
44
"version": "4.1.0",
55
"main": "index.js",
6-
"types": "index.d.ts",
6+
"types": "types/index.d.ts",
77
"scripts": {
88
"lint": "standard",
99
"lint:fix": "standard --fix",
@@ -33,8 +33,5 @@
3333
},
3434
"dependencies": {
3535
"fast-json-stringify": "^5.0.0"
36-
},
37-
"tsd": {
38-
"directory": "test/types"
3936
}
4037
}

test/types/index.test-d.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

test/types/standalone.test-d.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

types/index.d.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Options } from 'fast-json-stringify'
2+
3+
type FastJsonStringifyFactory = () => SerializerSelector.SerializerFactory
4+
5+
declare namespace SerializerSelector {
6+
export type SerializerFactory = (
7+
externalSchemas?: unknown,
8+
options?: Options
9+
) => SerializerCompiler;
10+
11+
export type SerializerCompiler = (
12+
externalSchemas?: unknown,
13+
options?: Options
14+
) => Serializer;
15+
16+
export type Serializer = (doc: any) => string
17+
18+
export type RouteDefinition = {
19+
method: string;
20+
url: string;
21+
httpStatus: string;
22+
schema?: unknown;
23+
}
24+
25+
export type StandaloneOptions = StandaloneOptionsReadModeOn | StandaloneOptionsReadModeOff
26+
27+
export type StandaloneOptionsReadModeOn = {
28+
readMode: true;
29+
restoreFunction?(opts: RouteDefinition): Serializer;
30+
}
31+
32+
export type StandaloneOptionsReadModeOff = {
33+
readMode?: false | undefined;
34+
storeFunction?(opts: RouteDefinition, schemaSerializationCode: string): void;
35+
}
36+
37+
export type { Options }
38+
export const SerializerSelector: FastJsonStringifyFactory;
39+
export function StandaloneSerializer(options: StandaloneOptions): SerializerFactory;
40+
41+
export { SerializerSelector as default }
42+
}
43+
44+
declare function SerializerSelector(...params: Parameters<FastJsonStringifyFactory>): ReturnType<FastJsonStringifyFactory>
45+
export = SerializerSelector

types/index.test-d.ts

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { expectAssignable, expectError, expectType } from "tsd";
2+
import SerializerSelector, {
3+
RouteDefinition,
4+
Serializer,
5+
SerializerCompiler,
6+
SerializerFactory,
7+
SerializerSelector as SerializerSelectorNamed,
8+
StandaloneSerializer,
9+
} from "..";
10+
11+
/**
12+
* SerializerSelector
13+
*/
14+
15+
{
16+
const compiler = SerializerSelector();
17+
expectType<SerializerFactory>(compiler);
18+
}
19+
20+
{
21+
const compiler = SerializerSelectorNamed();
22+
expectType<SerializerFactory>(compiler);
23+
}
24+
25+
{
26+
{
27+
const sampleSchema = {
28+
$id: 'example1',
29+
type: 'object',
30+
properties: {
31+
name: { type: 'string' }
32+
}
33+
}
34+
35+
const externalSchemas1 = {}
36+
37+
const factory = SerializerSelector()
38+
expectType<SerializerFactory>(factory);
39+
const compiler = factory(externalSchemas1, {})
40+
expectType<SerializerCompiler>(compiler);
41+
const serializeFunc = compiler({ schema: sampleSchema })
42+
expectType<Serializer>(serializeFunc);
43+
44+
expectType<string>(serializeFunc({ name: 'hello' }))
45+
}
46+
}
47+
48+
/**
49+
* StandaloneSerializer
50+
*/
51+
52+
const reader = StandaloneSerializer({
53+
readMode: true,
54+
restoreFunction: (route: RouteDefinition) => {
55+
expectAssignable<RouteDefinition>(route)
56+
return {} as Serializer
57+
},
58+
});
59+
expectType<SerializerFactory>(reader);
60+
61+
const writer = StandaloneSerializer({
62+
readMode: false,
63+
storeFunction: (route: RouteDefinition, code: string) => {
64+
expectAssignable<RouteDefinition>(route)
65+
expectAssignable<string>(code)
66+
},
67+
});
68+
expectType<SerializerFactory>(writer);
69+
70+
{
71+
const base = {
72+
$id: 'urn:schema:base',
73+
definitions: {
74+
hello: { type: 'string' }
75+
},
76+
type: 'object',
77+
properties: {
78+
hello: { $ref: '#/definitions/hello' }
79+
}
80+
}
81+
82+
const refSchema = {
83+
$id: 'urn:schema:ref',
84+
type: 'object',
85+
properties: {
86+
hello: { $ref: 'urn:schema:base#/definitions/hello' }
87+
}
88+
}
89+
90+
const endpointSchema = {
91+
schema: {
92+
$id: 'urn:schema:endpoint',
93+
$ref: 'urn:schema:ref'
94+
}
95+
}
96+
97+
const schemaMap = {
98+
[base.$id]: base,
99+
[refSchema.$id]: refSchema
100+
}
101+
102+
expectError(StandaloneSerializer({
103+
readMode: true,
104+
storeFunction () { }
105+
}))
106+
expectError(StandaloneSerializer({
107+
readMode: false,
108+
restoreFunction () {}
109+
}))
110+
expectError(StandaloneSerializer({
111+
restoreFunction () {}
112+
}))
113+
114+
expectType<SerializerFactory>(StandaloneSerializer({
115+
storeFunction (routeOpts, schemaSerializerCode) {
116+
expectType<RouteDefinition>(routeOpts)
117+
expectType<string>(schemaSerializerCode)
118+
}
119+
}))
120+
121+
expectType<SerializerFactory>(StandaloneSerializer({
122+
readMode: true,
123+
restoreFunction (routeOpts) {
124+
expectType<RouteDefinition>(routeOpts)
125+
return {} as Serializer
126+
}
127+
}))
128+
129+
const factory = StandaloneSerializer({
130+
readMode: false,
131+
storeFunction (routeOpts, schemaSerializerCode) {
132+
expectType<RouteDefinition>(routeOpts)
133+
expectType<string>(schemaSerializerCode)
134+
}
135+
})
136+
expectType<SerializerFactory>(factory)
137+
138+
const compiler = factory(schemaMap)
139+
expectType<SerializerCompiler>(compiler)
140+
expectType<Serializer>(compiler(endpointSchema))
141+
}

0 commit comments

Comments
 (0)