@nmtjs/type 0.3.11 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/compiler.js +43 -20
- package/dist/compiler.js.map +1 -1
- package/package.json +3 -3
- package/src/compiler.ts +59 -20
package/dist/compiler.js
CHANGED
|
@@ -1,39 +1,35 @@
|
|
|
1
1
|
import { TypeCompiler } from '@sinclair/typebox/compiler';
|
|
2
2
|
import { Value } from '@sinclair/typebox/value';
|
|
3
3
|
import { getTypeSchema } from "./types/base.js";
|
|
4
|
-
|
|
4
|
+
function _prepare(schema, value) {
|
|
5
|
+
return Value.Default(schema, Value.Clean(schema, value));
|
|
6
|
+
}
|
|
7
|
+
function _convert(schema, value) {
|
|
8
|
+
return Value.Convert(schema, value);
|
|
9
|
+
}
|
|
10
|
+
function compileType(type) {
|
|
5
11
|
const schema = getTypeSchema(type);
|
|
6
12
|
const compiled = TypeCompiler.Compile(schema);
|
|
7
|
-
const prepare = (value)=>{
|
|
8
|
-
for (const fn of [
|
|
9
|
-
Value.Clean,
|
|
10
|
-
Value.Default
|
|
11
|
-
]){
|
|
12
|
-
value = fn(schema, value);
|
|
13
|
-
}
|
|
14
|
-
return value;
|
|
15
|
-
};
|
|
16
|
-
const convert = (value)=>Value.Convert(schema, value);
|
|
17
13
|
return {
|
|
18
14
|
check: compiled.Check.bind(compiled),
|
|
19
15
|
errors: compiled.Errors.bind(compiled),
|
|
20
16
|
decode: compiled.Decode.bind(compiled),
|
|
21
17
|
encode: compiled.Encode.bind(compiled),
|
|
22
|
-
prepare,
|
|
23
|
-
convert
|
|
18
|
+
prepare: _prepare.bind(null, schema),
|
|
19
|
+
convert: _convert.bind(null, schema)
|
|
24
20
|
};
|
|
25
|
-
}
|
|
26
|
-
export
|
|
21
|
+
}
|
|
22
|
+
export function compile(schema) {
|
|
27
23
|
const compiled = compileType(schema);
|
|
28
24
|
return {
|
|
29
25
|
...compiled,
|
|
30
|
-
decode: (val)=>compiled.decode(
|
|
31
|
-
encode: (val)=>compiled.encode(
|
|
26
|
+
decode: (val)=>compiled.decode(val),
|
|
27
|
+
encode: (val)=>compiled.encode(val),
|
|
32
28
|
decodeSafe: (val)=>{
|
|
33
29
|
try {
|
|
34
30
|
return {
|
|
35
31
|
success: true,
|
|
36
|
-
value: compiled.decode(
|
|
32
|
+
value: compiled.decode(val)
|
|
37
33
|
};
|
|
38
34
|
} catch (error) {
|
|
39
35
|
return {
|
|
@@ -46,7 +42,7 @@ export const compile = (schema)=>{
|
|
|
46
42
|
try {
|
|
47
43
|
return {
|
|
48
44
|
success: true,
|
|
49
|
-
value: compiled.encode(
|
|
45
|
+
value: compiled.encode(val)
|
|
50
46
|
};
|
|
51
47
|
} catch (error) {
|
|
52
48
|
return {
|
|
@@ -56,4 +52,31 @@ export const compile = (schema)=>{
|
|
|
56
52
|
}
|
|
57
53
|
}
|
|
58
54
|
};
|
|
59
|
-
}
|
|
55
|
+
}
|
|
56
|
+
export var runtime;
|
|
57
|
+
(function(runtime) {
|
|
58
|
+
function prepare(type, value) {
|
|
59
|
+
return _prepare(getTypeSchema(type), value);
|
|
60
|
+
}
|
|
61
|
+
runtime.prepare = prepare;
|
|
62
|
+
function convert(type, value) {
|
|
63
|
+
return _convert(getTypeSchema(type), value);
|
|
64
|
+
}
|
|
65
|
+
runtime.convert = convert;
|
|
66
|
+
function errors(type, value) {
|
|
67
|
+
return Value.Errors(getTypeSchema(type), value);
|
|
68
|
+
}
|
|
69
|
+
runtime.errors = errors;
|
|
70
|
+
function check(type, value) {
|
|
71
|
+
return Value.Check(getTypeSchema(type), value);
|
|
72
|
+
}
|
|
73
|
+
runtime.check = check;
|
|
74
|
+
function decode(type, value) {
|
|
75
|
+
return Value.Decode(getTypeSchema(type), value);
|
|
76
|
+
}
|
|
77
|
+
runtime.decode = decode;
|
|
78
|
+
function encode(type, value) {
|
|
79
|
+
return Value.Encode(getTypeSchema(type), value);
|
|
80
|
+
}
|
|
81
|
+
runtime.encode = encode;
|
|
82
|
+
})(runtime || (runtime = {}));
|
package/dist/compiler.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/compiler.ts"],"sourcesContent":["import {\n TypeCompiler,\n type ValueErrorIterator,\n} from '@sinclair/typebox/compiler'\nimport { Value } from '@sinclair/typebox/value'\nimport { type BaseType, getTypeSchema } from './types/base.ts'\n\nexport type Compiled = {\n check: (val: unknown) => boolean\n errors: (val: unknown) => ValueErrorIterator\n prepare: (val: unknown) => unknown\n convert: (val: unknown) => unknown\n decode: (val: unknown) =>
|
|
1
|
+
{"version":3,"sources":["../../src/compiler.ts"],"sourcesContent":["import type { TSchema } from '@sinclair/typebox'\nimport {\n TypeCompiler,\n type ValueErrorIterator,\n} from '@sinclair/typebox/compiler'\nimport { Value } from '@sinclair/typebox/value'\nimport type { typeStatic } from './constants.ts'\nimport { type BaseType, getTypeSchema } from './types/base.ts'\n\nexport type Compiled<T extends BaseType = BaseType> = {\n check: (val: unknown) => boolean\n errors: (val: unknown) => ValueErrorIterator\n prepare: (val: unknown) => unknown\n convert: (val: unknown) => unknown\n decode: (val: unknown) => T[typeStatic]['decoded']\n encode: (val: unknown) => T[typeStatic]['encoded']\n decodeSafe: (\n val: unknown,\n ) =>\n | { success: true; value: T[typeStatic]['decoded'] }\n | { success: false; error: any }\n encodeSafe: (\n val: unknown,\n ) =>\n | { success: true; value: T[typeStatic]['encoded'] }\n | { success: false; error: any }\n}\n\nfunction _prepare(schema: TSchema, value: any) {\n return Value.Default(schema, Value.Clean(schema, value))\n}\n\nfunction _convert(schema: TSchema, value: any) {\n return Value.Convert(schema, value)\n}\n\nfunction compileType(type: BaseType) {\n const schema = getTypeSchema(type)\n const compiled = TypeCompiler.Compile(schema)\n return {\n check: compiled.Check.bind(compiled),\n errors: compiled.Errors.bind(compiled),\n decode: compiled.Decode.bind(compiled),\n encode: compiled.Encode.bind(compiled),\n prepare: _prepare.bind(null, schema),\n convert: _convert.bind(null, schema),\n }\n}\n\nexport function compile<T extends BaseType>(schema: T): Compiled<T> {\n const compiled = compileType(schema)\n\n // TODO: custom error handling/shaping\n return {\n ...compiled,\n decode: (val) => compiled.decode(val),\n encode: (val) => compiled.encode(val),\n decodeSafe: (val) => {\n try {\n return {\n success: true as const,\n value: compiled.decode(val),\n }\n } catch (error) {\n return { success: false as const, error }\n }\n },\n encodeSafe: (val) => {\n try {\n return {\n success: true as const,\n value: compiled.encode(val),\n }\n } catch (error) {\n return { success: false as const, error }\n }\n },\n }\n}\n\nexport namespace runtime {\n export function prepare(type: BaseType, value: any) {\n return _prepare(getTypeSchema(type), value)\n }\n\n export function convert(type: BaseType, value: any) {\n return _convert(getTypeSchema(type), value)\n }\n\n export function errors(type: BaseType, value: any): ValueErrorIterator {\n return Value.Errors(getTypeSchema(type), value)\n }\n\n export function check(type: BaseType, value: any): boolean {\n return Value.Check(getTypeSchema(type), value)\n }\n\n export function decode<T extends BaseType>(\n type: BaseType,\n value: any,\n ): T[typeStatic]['decoded'] {\n return Value.Decode(getTypeSchema(type), value)\n }\n\n export function encode<T extends BaseType>(\n type: T,\n value: any,\n ): T[typeStatic]['encoded'] {\n return Value.Encode(getTypeSchema(type), value)\n }\n}\n"],"names":["TypeCompiler","Value","getTypeSchema","_prepare","schema","value","Default","Clean","_convert","Convert","compileType","type","compiled","Compile","check","Check","bind","errors","Errors","decode","Decode","encode","Encode","prepare","convert","compile","val","decodeSafe","success","error","encodeSafe","runtime"],"mappings":"AACA,SACEA,YAAY,QAEP,6BAA4B;AACnC,SAASC,KAAK,QAAQ,0BAAyB;AAE/C,SAAwBC,aAAa,QAAQ,kBAAiB;AAqB9D,SAASC,SAASC,MAAe,EAAEC,KAAU;IAC3C,OAAOJ,MAAMK,OAAO,CAACF,QAAQH,MAAMM,KAAK,CAACH,QAAQC;AACnD;AAEA,SAASG,SAASJ,MAAe,EAAEC,KAAU;IAC3C,OAAOJ,MAAMQ,OAAO,CAACL,QAAQC;AAC/B;AAEA,S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SAAAA;IAIT,SAASH,MAAMH,IAAc,EAAEN,KAAU;QAC9C,OAAOJ,MAAMc,KAAK,CAACb,cAAcS,OAAON;IAC1C;YAFgBS,QAAAA;IAIT,SAASK,OACdR,IAAc,EACdN,KAAU;QAEV,OAAOJ,MAAMmB,MAAM,CAAClB,cAAcS,OAAON;IAC3C;YALgBc,SAAAA;IAOT,SAASE,OACdV,IAAO,EACPN,KAAU;QAEV,OAAOJ,MAAMqB,MAAM,CAACpB,cAAcS,OAAON;IAC3C;YALgBgB,SAAAA;AAMlB,GA9BiBU,YAAAA"}
|
package/package.json
CHANGED
|
@@ -21,12 +21,12 @@
|
|
|
21
21
|
"peerDependencies": {
|
|
22
22
|
"@sinclair/typebox": "^0.33.7",
|
|
23
23
|
"temporal-polyfill": "^0.2.5",
|
|
24
|
-
"@nmtjs/common": "0.
|
|
24
|
+
"@nmtjs/common": "0.4.1"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@sinclair/typebox": "^0.33.7",
|
|
28
28
|
"temporal-polyfill": "^0.2.5",
|
|
29
|
-
"@nmtjs/common": "0.
|
|
29
|
+
"@nmtjs/common": "0.4.1"
|
|
30
30
|
},
|
|
31
31
|
"files": [
|
|
32
32
|
"src",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"LICENSE.md",
|
|
36
36
|
"README.md"
|
|
37
37
|
],
|
|
38
|
-
"version": "0.
|
|
38
|
+
"version": "0.4.1",
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "neemata-build -p neutral --root=./src './**/*.ts'",
|
|
41
41
|
"type-check": "tsc --noEmit"
|
package/src/compiler.ts
CHANGED
|
@@ -1,58 +1,65 @@
|
|
|
1
|
+
import type { TSchema } from '@sinclair/typebox'
|
|
1
2
|
import {
|
|
2
3
|
TypeCompiler,
|
|
3
4
|
type ValueErrorIterator,
|
|
4
5
|
} from '@sinclair/typebox/compiler'
|
|
5
6
|
import { Value } from '@sinclair/typebox/value'
|
|
7
|
+
import type { typeStatic } from './constants.ts'
|
|
6
8
|
import { type BaseType, getTypeSchema } from './types/base.ts'
|
|
7
9
|
|
|
8
|
-
export type Compiled = {
|
|
10
|
+
export type Compiled<T extends BaseType = BaseType> = {
|
|
9
11
|
check: (val: unknown) => boolean
|
|
10
12
|
errors: (val: unknown) => ValueErrorIterator
|
|
11
13
|
prepare: (val: unknown) => unknown
|
|
12
14
|
convert: (val: unknown) => unknown
|
|
13
|
-
decode: (val: unknown) =>
|
|
14
|
-
encode: (val: unknown) =>
|
|
15
|
+
decode: (val: unknown) => T[typeStatic]['decoded']
|
|
16
|
+
encode: (val: unknown) => T[typeStatic]['encoded']
|
|
15
17
|
decodeSafe: (
|
|
16
18
|
val: unknown,
|
|
17
|
-
) =>
|
|
19
|
+
) =>
|
|
20
|
+
| { success: true; value: T[typeStatic]['decoded'] }
|
|
21
|
+
| { success: false; error: any }
|
|
18
22
|
encodeSafe: (
|
|
19
23
|
val: unknown,
|
|
20
|
-
) =>
|
|
24
|
+
) =>
|
|
25
|
+
| { success: true; value: T[typeStatic]['encoded'] }
|
|
26
|
+
| { success: false; error: any }
|
|
21
27
|
}
|
|
22
28
|
|
|
23
|
-
|
|
29
|
+
function _prepare(schema: TSchema, value: any) {
|
|
30
|
+
return Value.Default(schema, Value.Clean(schema, value))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function _convert(schema: TSchema, value: any) {
|
|
34
|
+
return Value.Convert(schema, value)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function compileType(type: BaseType) {
|
|
24
38
|
const schema = getTypeSchema(type)
|
|
25
39
|
const compiled = TypeCompiler.Compile(schema)
|
|
26
|
-
const prepare = (value: any) => {
|
|
27
|
-
for (const fn of [Value.Clean, Value.Default]) {
|
|
28
|
-
value = fn(schema, value)
|
|
29
|
-
}
|
|
30
|
-
return value
|
|
31
|
-
}
|
|
32
|
-
const convert = (value: any) => Value.Convert(schema, value)
|
|
33
40
|
return {
|
|
34
41
|
check: compiled.Check.bind(compiled),
|
|
35
42
|
errors: compiled.Errors.bind(compiled),
|
|
36
43
|
decode: compiled.Decode.bind(compiled),
|
|
37
44
|
encode: compiled.Encode.bind(compiled),
|
|
38
|
-
prepare,
|
|
39
|
-
convert,
|
|
45
|
+
prepare: _prepare.bind(null, schema),
|
|
46
|
+
convert: _convert.bind(null, schema),
|
|
40
47
|
}
|
|
41
48
|
}
|
|
42
49
|
|
|
43
|
-
export
|
|
50
|
+
export function compile<T extends BaseType>(schema: T): Compiled<T> {
|
|
44
51
|
const compiled = compileType(schema)
|
|
45
52
|
|
|
46
53
|
// TODO: custom error handling/shaping
|
|
47
54
|
return {
|
|
48
55
|
...compiled,
|
|
49
|
-
decode: (val) => compiled.decode(
|
|
50
|
-
encode: (val) => compiled.encode(
|
|
56
|
+
decode: (val) => compiled.decode(val),
|
|
57
|
+
encode: (val) => compiled.encode(val),
|
|
51
58
|
decodeSafe: (val) => {
|
|
52
59
|
try {
|
|
53
60
|
return {
|
|
54
61
|
success: true as const,
|
|
55
|
-
value: compiled.decode(
|
|
62
|
+
value: compiled.decode(val),
|
|
56
63
|
}
|
|
57
64
|
} catch (error) {
|
|
58
65
|
return { success: false as const, error }
|
|
@@ -62,7 +69,7 @@ export const compile = (schema: BaseType): Compiled => {
|
|
|
62
69
|
try {
|
|
63
70
|
return {
|
|
64
71
|
success: true as const,
|
|
65
|
-
value: compiled.encode(
|
|
72
|
+
value: compiled.encode(val),
|
|
66
73
|
}
|
|
67
74
|
} catch (error) {
|
|
68
75
|
return { success: false as const, error }
|
|
@@ -70,3 +77,35 @@ export const compile = (schema: BaseType): Compiled => {
|
|
|
70
77
|
},
|
|
71
78
|
}
|
|
72
79
|
}
|
|
80
|
+
|
|
81
|
+
export namespace runtime {
|
|
82
|
+
export function prepare(type: BaseType, value: any) {
|
|
83
|
+
return _prepare(getTypeSchema(type), value)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function convert(type: BaseType, value: any) {
|
|
87
|
+
return _convert(getTypeSchema(type), value)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function errors(type: BaseType, value: any): ValueErrorIterator {
|
|
91
|
+
return Value.Errors(getTypeSchema(type), value)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function check(type: BaseType, value: any): boolean {
|
|
95
|
+
return Value.Check(getTypeSchema(type), value)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function decode<T extends BaseType>(
|
|
99
|
+
type: BaseType,
|
|
100
|
+
value: any,
|
|
101
|
+
): T[typeStatic]['decoded'] {
|
|
102
|
+
return Value.Decode(getTypeSchema(type), value)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function encode<T extends BaseType>(
|
|
106
|
+
type: T,
|
|
107
|
+
value: any,
|
|
108
|
+
): T[typeStatic]['encoded'] {
|
|
109
|
+
return Value.Encode(getTypeSchema(type), value)
|
|
110
|
+
}
|
|
111
|
+
}
|