-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
405 lines (363 loc) · 10.5 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import { Either, isRight, either } from 'fp-ts/lib/Either';
import * as t from 'io-ts';
import { PathReporter } from 'io-ts/lib/PathReporter';
import { runtime } from 'ts-transformer-interface';
import { ICaster } from 'ts-transformer-decoder-cast';
/** @since 1.0.0 */
export { runtime } from 'ts-transformer-interface';
/** @since 1.0.0 */
export function schema<T extends object>(): runtime.Schema {
throw new Error(
`tsc did not expand this macro! Ensure you setup tsconfig plugins section correctly!`,
);
}
/** @since 1.9.0 */
export function enumSchema(name: string, e: any): CasterBuilder {
const inValues = (x: any) => Object.values(e).some(v => v === x);
return {
typeName: name,
caster: new t.Type(
name,
(x: unknown): x is typeof e[keyof typeof e] => inValues(x),
(x, context) => (inValues(x) ? t.success(x) : t.failure(x, context)),
t.identity,
),
};
}
/** @since 1.0.0 */
export interface Caster<T = any> extends t.Type<T> {}
/** @since 1.0.0 */
export interface Casters {
[type: string]: Caster;
}
const TypeC: t.Type<runtime.Type> = t.recursion('TypeC', () =>
t.union([
t.null,
t.string,
ReferenceTypeC,
ArrayTypeC,
ParameterizedTypeC,
GenericTypeC,
LiteralTypeC,
UnionTypeC,
]),
);
const ReferenceTypeC: t.Type<runtime.ReferenceType> = t.type({
referenceName: t.string,
});
const ArrayTypeC: t.Type<runtime.ArrayType> = t.type({
arrayElementType: TypeC,
});
const GenericTypeC: t.Type<runtime.GenericType> = t.type({
genericParameterName: t.string,
genericParameterType: TypeC,
});
const ParameterizedTypeC: t.Type<runtime.ParameterizedType> = t.type({
selfType: t.string,
typeArgumentType: TypeC,
});
const UnionTypeC: t.Type<runtime.UnionType> = t.type({
union: t.array(TypeC),
});
const PropertyC: t.Type<runtime.Property> = t.type({
name: t.string,
type: TypeC,
optional: t.boolean,
});
const LiteralTypeC: t.Type<runtime.LiteralType> = t.type({
props: t.array(PropertyC),
});
/**
* Use this to attach additional values to the interface
* other than the original source.
*
* You should mark all fields inside attrs as optional since
* they are not defined from the data source.
*
* The Decoder will think it as "any" and assign an empty
* object to it.
* @deprecated use Model<T> instead
* @since 1.5.0
* @example
* interface User {
* name: string;
* attrs: {
* marker?: google.maps.Marker
* }
* }
*/
export const ATTRS_KEYWORD = 'attrs';
/** @since 1.9.0 */
export interface ClassBuilder {
schema: runtime.Schema;
constructor: new (...args: any[]) => any;
className: string;
}
/** @since 1.9.0 */
interface CasterBuilder {
typeName: string;
caster: Caster;
}
/** @since 1.9.0 */
export type Builder = ClassBuilder | CasterBuilder;
/**
* Extends type T with mixin U.
* @deprecated use Model<T> instead
* @since 1.6.0
* @example
* interface IUser {
* firstName: string;
* lastName: string;
* }
*
* const User = extend<IUser>()(user => ({
* get fullName(): string {
* return `${user.firstName} ${user.lastName}`;
* }
* }));
*
* type User = InstanceType<typeof User>;
*/
export function extend<T extends object>() {
return <U>(mixin: (self: T) => U) => {
const impl = class {
constructor(data: T) {
Object.assign(this, data, mixin(data));
}
};
return (impl as any) as new (data: T) => T & U;
};
}
/**
* Allow constructing from a plain object.
* @since 1.10.0
* @example
* interface IUser { name: string; }
* interface User extends IUser {}
* class User extends Model<IUser> {
* get title() {
* return `Dr. ${this.name}`;
* }
* }
*/
export class Model<T> {
constructor(data: T) {
Object.assign(this, data);
}
}
function isClassBuilder(o: any): o is ClassBuilder {
return ['schema', 'className', 'constructor'].every(k => k in o);
}
function isCasterBuilder(o: any): o is CasterBuilder {
return ['typeName', 'caster'].every(k => k in o);
}
/** @since 1.7.0 */
export interface GenericType {
type: string;
arg: string | GenericType;
}
/** @since 1.0.0 */
export class Decoder implements ICaster {
/** @since 1.0.0 */
readonly casters: Casters = {};
/** @since 1.7.0*/
readonly factories: { [name: string]: (caster: Caster) => Caster } = {
Array: t.array,
};
/** @since 1.6.0 */
readonly constructors: { [clazz: string]: Builder } = {};
private todos: { [name: string]: boolean } = {};
private resolves: { [name: string]: boolean } = {};
private withAttributes: { [name: string]: boolean } = {};
/** @since 1.6.0 */
constructor(schemas: Array<runtime.Schema | Builder> = [], casters: Casters = {}) {
Object.assign(this.casters, casters);
schemas.forEach(s => {
if (isClassBuilder(s)) {
this.todos[s.schema.name] = true;
this.todos[s.className] = true;
} else if (isCasterBuilder(s)) {
this.todos[s.typeName] = true;
} else {
this.todos[s.name] = true;
}
});
schemas.forEach(s => this.register(s));
}
/** @since 1.7.0 */
decode<T>(
typeArg: string | GenericType,
data: unknown,
onError?: (errors: string[]) => void,
): T | undefined {
if (typeof typeArg === 'string') {
return this.processResult(this.getCaster<T>(typeArg), typeArg, data, onError);
} else {
return this.processResult(this.buildGenericCaster(typeArg), typeArg.type, data, onError);
}
}
private buildGenericCaster({ type, arg }: GenericType): Caster {
const factory = this.factories[type];
if (!factory) {
throw new Error(`generic type '${type}' not registered`);
}
if (typeof arg === 'string') {
return factory(this.getCaster(arg));
} else {
return factory(this.buildGenericCaster(arg));
}
}
/**
* A shortcut to decode<T[]>({ type: 'Array', arg: T }, data, onError)
* @since 1.1.0
*/
decodeArray<T>(
typeName: string,
data: unknown,
onError?: (errors: string[]) => void,
): T[] | undefined {
return this.decode<T[]>({ type: 'Array', arg: typeName }, data, onError);
}
private processResult<T>(
caster: Caster<T>,
typeName: string,
data: unknown,
onError?: (errors: string[]) => void,
): T | undefined {
const result = caster.decode(data);
if (isRight(result)) {
const decoded = result.right;
if (typeName in this.withAttributes) {
(decoded as any).attrs = {};
}
return decoded;
} else if (onError) {
onError(this.errors(result));
}
}
/** @since 1.6.0 */
register(spec: runtime.Schema | Builder) {
if (isClassBuilder(spec)) {
this.registerBuilder(spec);
} else if (isCasterBuilder(spec)) {
this.casters[spec.typeName] = spec.caster;
} else {
this.registerSchema(spec);
}
}
private registerSchema(spec: runtime.Schema) {
const name = spec.name;
if (name in this.resolves) {
throw new Error(`type '${name}' already registered`);
}
this.resolves[name] = false;
this.casters[name] = this.buildCaster(spec.props, name);
this.resolves[name] = true;
}
private buildCaster(props: runtime.Property[], name?: string): Caster {
if (name && props.some(p => p.name === ATTRS_KEYWORD)) {
this.withAttributes[name] = true;
}
const required = props.filter(p => !p.optional);
const optional = props.filter(p => p.optional);
if (required.length > 0 && optional.length > 0) {
return t.intersection(
[
t.type(this.buildCasters(required, name), '[required]'),
t.partial(this.buildCasters(optional, name), '[optional]'),
],
name,
);
} else if (required.length > 0) {
return t.type(this.buildCasters(required, name), name);
} else if (optional.length > 0) {
return t.partial(this.buildCasters(optional, name), name);
} else {
throw new Error(`type '${name || '<LITERAL>'}' is an empty interface which is not supported`);
}
}
private registerBuilder(spec: ClassBuilder) {
this.registerSchema(spec.schema);
const name = spec.className;
if (name in this.resolves) {
throw new Error(`type '${name}' already registered`);
}
this.resolves[name] = false;
this.casters[name] = this.buildConstructor(spec);
this.resolves[name] = true;
}
private buildConstructor(spec: ClassBuilder) {
const schemaCaster = this.getCaster(spec.schema.name);
const constructor = spec.constructor;
return schemaCaster.pipe(
new t.Type(
spec.className,
(input: unknown): input is InstanceType<typeof constructor> => input instanceof constructor,
(input, context) => t.success(new constructor(input)),
t.identity,
),
);
}
private checkRegistry(typeName: string) {
if (!(typeName in this.casters)) {
if (this.resolves[typeName] === false) {
throw new Error(`recursive definition not supported`);
} else if (this.todos[typeName]) {
throw new Error(
`depends on ${typeName} but it's not registered yet. (try to move '${typeName}' before this type)`,
);
} else {
throw new Error(`decoder for '${typeName}' not registered`);
}
}
}
private getCaster<T>(typeName: string): Caster<T> {
this.checkRegistry(typeName);
return this.casters[typeName];
}
private buildCasters(props: runtime.Property[], name?: string): Casters {
const casters: Casters = {};
props.forEach(p => {
if (p.name === ATTRS_KEYWORD) {
return t.undefined;
}
let caster;
try {
caster = this.buildTypeCaster(p.type);
} catch (e) {
throw new Error(`${name || '<LITERAL>'}.${p.name}: ${e.message}`);
}
casters[p.name] = caster;
});
return casters;
}
private buildTypeCaster(type: runtime.Type): Caster {
if (ArrayTypeC.is(type)) {
return t.array(this.buildTypeCaster(type.arrayElementType));
}
if (ReferenceTypeC.is(type)) {
return this.getCaster(type.referenceName);
}
if (LiteralTypeC.is(type)) {
return t.type(this.buildCasters(type.props));
}
if (UnionTypeC.is(type)) {
return type.union.map(t => this.buildTypeCaster(t)).reduce((t1, t2) => t.union([t1, t2]));
}
switch (type) {
case 'string':
return t.string;
case 'number':
return t.number;
case 'boolean':
return t.boolean;
case 'null':
return t.null;
default:
throw new Error(`illegal decoder type ${JSON.stringify(type)}`);
}
}
private errors(result: Either<t.Errors, any>): string[] {
return PathReporter.report(result);
}
}