@atscript/typescript 0.1.20 → 0.1.22
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/cli.cjs +17 -2
- package/dist/index.cjs +17 -2
- package/dist/index.d.ts +6 -0
- package/dist/index.mjs +17 -2
- package/dist/utils.cjs +8 -1
- package/dist/utils.d.ts +29 -7
- package/dist/utils.mjs +7 -1
- package/package.json +3 -4
- package/skills/atscript-typescript/SKILL.md +1 -1
- package/skills/atscript-typescript/codegen.md +7 -1
- package/skills/atscript-typescript/core.md +12 -1
- package/skills/atscript-typescript/utilities.md +57 -1
package/dist/cli.cjs
CHANGED
|
@@ -334,6 +334,8 @@ var TypeRenderer = class TypeRenderer extends BaseRenderer {
|
|
|
334
334
|
this.writeln(`static validator: (opts?: Partial<TValidatorOptions>) => Validator<typeof ${asClass}>`);
|
|
335
335
|
if (resolveJsonSchemaMode(this.opts) === false) this.writeln("/** @deprecated JSON Schema support is disabled. Calling this method will throw a runtime error. To enable, set `jsonSchema: 'lazy'` or `jsonSchema: 'bundle'` in tsPlugin options, or add `@emit.jsonSchema` annotation to individual interfaces. */");
|
|
336
336
|
this.writeln("static toJsonSchema: () => any");
|
|
337
|
+
if (!this.opts?.exampleData) this.writeln("/** @deprecated Example Data support is disabled. To enable, set `exampleData: true` in tsPlugin options. */");
|
|
338
|
+
this.writeln("static toExampleData?: () => any");
|
|
337
339
|
}
|
|
338
340
|
this.pop();
|
|
339
341
|
}
|
|
@@ -397,6 +399,8 @@ else if ((0, __atscript_core.isPrimitive)(realDef)) typeDef = `TAtscriptTypeFina
|
|
|
397
399
|
this.writeln(`const validator: (opts?: Partial<TValidatorOptions>) => Validator<typeof ${name}>`);
|
|
398
400
|
if (resolveJsonSchemaMode(this.opts) === false) this.writeln("/** @deprecated JSON Schema support is disabled. Calling this method will throw a runtime error. To enable, set `jsonSchema: 'lazy'` or `jsonSchema: 'bundle'` in tsPlugin options, or add `@emit.jsonSchema` annotation to individual interfaces. */");
|
|
399
401
|
this.writeln("const toJsonSchema: () => any");
|
|
402
|
+
if (!this.opts?.exampleData) this.writeln("/** @deprecated Example Data support is disabled. To enable, set `exampleData: true` in tsPlugin options. */");
|
|
403
|
+
this.writeln("const toExampleData: (() => any) | undefined");
|
|
400
404
|
this.popln();
|
|
401
405
|
}
|
|
402
406
|
phantomPropType(def) {
|
|
@@ -1081,7 +1085,10 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1081
1085
|
this.writeln("/* eslint-disable */");
|
|
1082
1086
|
this.writeln("/* oxlint-disable */");
|
|
1083
1087
|
const imports = ["defineAnnotatedType as $", "annotate as $a"];
|
|
1084
|
-
|
|
1088
|
+
const jsonSchemaMode = resolveJsonSchemaMode(this.opts);
|
|
1089
|
+
if (jsonSchemaMode === "lazy") imports.push("buildJsonSchema as $$");
|
|
1090
|
+
if (this.opts?.exampleData) imports.push("createDataFromAnnotatedType as $e");
|
|
1091
|
+
if (jsonSchemaMode === false) imports.push("throwFeatureDisabled as $d");
|
|
1085
1092
|
this.writeln(`import { ${imports.join(", ")} } from "@atscript/typescript/utils"`);
|
|
1086
1093
|
}
|
|
1087
1094
|
buildAdHocMap(annotateNodes) {
|
|
@@ -1127,6 +1134,7 @@ else {
|
|
|
1127
1134
|
this.writeln("static type = {}");
|
|
1128
1135
|
this.writeln("static metadata = new Map()");
|
|
1129
1136
|
this.renderJsonSchemaMethod(node);
|
|
1137
|
+
this.renderExampleDataMethod(node);
|
|
1130
1138
|
}
|
|
1131
1139
|
renderInterface(node) {
|
|
1132
1140
|
this.writeln();
|
|
@@ -1182,7 +1190,14 @@ else {
|
|
|
1182
1190
|
this.writeln("}");
|
|
1183
1191
|
} else {
|
|
1184
1192
|
this.writeln("static toJsonSchema() {");
|
|
1185
|
-
this.indent().writeln("
|
|
1193
|
+
this.indent().writeln("$d(\"JSON Schema\", \"jsonSchema\", \"emit.jsonSchema\")").unindent();
|
|
1194
|
+
this.writeln("}");
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
renderExampleDataMethod(_node) {
|
|
1198
|
+
if (this.opts?.exampleData) {
|
|
1199
|
+
this.writeln("static toExampleData() {");
|
|
1200
|
+
this.indent().writeln("return $e(this, { mode: \"example\" })").unindent();
|
|
1186
1201
|
this.writeln("}");
|
|
1187
1202
|
}
|
|
1188
1203
|
}
|
package/dist/index.cjs
CHANGED
|
@@ -331,6 +331,8 @@ var TypeRenderer = class TypeRenderer extends BaseRenderer {
|
|
|
331
331
|
this.writeln(`static validator: (opts?: Partial<TValidatorOptions>) => Validator<typeof ${asClass}>`);
|
|
332
332
|
if (resolveJsonSchemaMode(this.opts) === false) this.writeln("/** @deprecated JSON Schema support is disabled. Calling this method will throw a runtime error. To enable, set `jsonSchema: 'lazy'` or `jsonSchema: 'bundle'` in tsPlugin options, or add `@emit.jsonSchema` annotation to individual interfaces. */");
|
|
333
333
|
this.writeln("static toJsonSchema: () => any");
|
|
334
|
+
if (!this.opts?.exampleData) this.writeln("/** @deprecated Example Data support is disabled. To enable, set `exampleData: true` in tsPlugin options. */");
|
|
335
|
+
this.writeln("static toExampleData?: () => any");
|
|
334
336
|
}
|
|
335
337
|
this.pop();
|
|
336
338
|
}
|
|
@@ -394,6 +396,8 @@ else if ((0, __atscript_core.isPrimitive)(realDef)) typeDef = `TAtscriptTypeFina
|
|
|
394
396
|
this.writeln(`const validator: (opts?: Partial<TValidatorOptions>) => Validator<typeof ${name}>`);
|
|
395
397
|
if (resolveJsonSchemaMode(this.opts) === false) this.writeln("/** @deprecated JSON Schema support is disabled. Calling this method will throw a runtime error. To enable, set `jsonSchema: 'lazy'` or `jsonSchema: 'bundle'` in tsPlugin options, or add `@emit.jsonSchema` annotation to individual interfaces. */");
|
|
396
398
|
this.writeln("const toJsonSchema: () => any");
|
|
399
|
+
if (!this.opts?.exampleData) this.writeln("/** @deprecated Example Data support is disabled. To enable, set `exampleData: true` in tsPlugin options. */");
|
|
400
|
+
this.writeln("const toExampleData: (() => any) | undefined");
|
|
397
401
|
this.popln();
|
|
398
402
|
}
|
|
399
403
|
phantomPropType(def) {
|
|
@@ -1078,7 +1082,10 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1078
1082
|
this.writeln("/* eslint-disable */");
|
|
1079
1083
|
this.writeln("/* oxlint-disable */");
|
|
1080
1084
|
const imports = ["defineAnnotatedType as $", "annotate as $a"];
|
|
1081
|
-
|
|
1085
|
+
const jsonSchemaMode = resolveJsonSchemaMode(this.opts);
|
|
1086
|
+
if (jsonSchemaMode === "lazy") imports.push("buildJsonSchema as $$");
|
|
1087
|
+
if (this.opts?.exampleData) imports.push("createDataFromAnnotatedType as $e");
|
|
1088
|
+
if (jsonSchemaMode === false) imports.push("throwFeatureDisabled as $d");
|
|
1082
1089
|
this.writeln(`import { ${imports.join(", ")} } from "@atscript/typescript/utils"`);
|
|
1083
1090
|
}
|
|
1084
1091
|
buildAdHocMap(annotateNodes) {
|
|
@@ -1124,6 +1131,7 @@ else {
|
|
|
1124
1131
|
this.writeln("static type = {}");
|
|
1125
1132
|
this.writeln("static metadata = new Map()");
|
|
1126
1133
|
this.renderJsonSchemaMethod(node);
|
|
1134
|
+
this.renderExampleDataMethod(node);
|
|
1127
1135
|
}
|
|
1128
1136
|
renderInterface(node) {
|
|
1129
1137
|
this.writeln();
|
|
@@ -1179,7 +1187,14 @@ else {
|
|
|
1179
1187
|
this.writeln("}");
|
|
1180
1188
|
} else {
|
|
1181
1189
|
this.writeln("static toJsonSchema() {");
|
|
1182
|
-
this.indent().writeln("
|
|
1190
|
+
this.indent().writeln("$d(\"JSON Schema\", \"jsonSchema\", \"emit.jsonSchema\")").unindent();
|
|
1191
|
+
this.writeln("}");
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
renderExampleDataMethod(_node) {
|
|
1195
|
+
if (this.opts?.exampleData) {
|
|
1196
|
+
this.writeln("static toExampleData() {");
|
|
1197
|
+
this.indent().writeln("return $e(this, { mode: \"example\" })").unindent();
|
|
1183
1198
|
this.writeln("}");
|
|
1184
1199
|
}
|
|
1185
1200
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,12 @@ interface TTsPluginOptions {
|
|
|
11
11
|
* to force build-time embedding regardless of this setting.
|
|
12
12
|
*/
|
|
13
13
|
jsonSchema?: false | 'lazy' | 'bundle';
|
|
14
|
+
/**
|
|
15
|
+
* Example data support:
|
|
16
|
+
* - `false` — No support. `toExampleData` is not rendered. *(default)*
|
|
17
|
+
* - `true` — Import `createDataFromAnnotatedType`, create example data on each call.
|
|
18
|
+
*/
|
|
19
|
+
exampleData?: boolean;
|
|
14
20
|
}
|
|
15
21
|
declare const tsPlugin: (opts?: TTsPluginOptions) => TAtscriptPlugin;
|
|
16
22
|
|
package/dist/index.mjs
CHANGED
|
@@ -307,6 +307,8 @@ var TypeRenderer = class TypeRenderer extends BaseRenderer {
|
|
|
307
307
|
this.writeln(`static validator: (opts?: Partial<TValidatorOptions>) => Validator<typeof ${asClass}>`);
|
|
308
308
|
if (resolveJsonSchemaMode(this.opts) === false) this.writeln("/** @deprecated JSON Schema support is disabled. Calling this method will throw a runtime error. To enable, set `jsonSchema: 'lazy'` or `jsonSchema: 'bundle'` in tsPlugin options, or add `@emit.jsonSchema` annotation to individual interfaces. */");
|
|
309
309
|
this.writeln("static toJsonSchema: () => any");
|
|
310
|
+
if (!this.opts?.exampleData) this.writeln("/** @deprecated Example Data support is disabled. To enable, set `exampleData: true` in tsPlugin options. */");
|
|
311
|
+
this.writeln("static toExampleData?: () => any");
|
|
310
312
|
}
|
|
311
313
|
this.pop();
|
|
312
314
|
}
|
|
@@ -370,6 +372,8 @@ else if (isPrimitive(realDef)) typeDef = `TAtscriptTypeFinal<${name}>`;
|
|
|
370
372
|
this.writeln(`const validator: (opts?: Partial<TValidatorOptions>) => Validator<typeof ${name}>`);
|
|
371
373
|
if (resolveJsonSchemaMode(this.opts) === false) this.writeln("/** @deprecated JSON Schema support is disabled. Calling this method will throw a runtime error. To enable, set `jsonSchema: 'lazy'` or `jsonSchema: 'bundle'` in tsPlugin options, or add `@emit.jsonSchema` annotation to individual interfaces. */");
|
|
372
374
|
this.writeln("const toJsonSchema: () => any");
|
|
375
|
+
if (!this.opts?.exampleData) this.writeln("/** @deprecated Example Data support is disabled. To enable, set `exampleData: true` in tsPlugin options. */");
|
|
376
|
+
this.writeln("const toExampleData: (() => any) | undefined");
|
|
373
377
|
this.popln();
|
|
374
378
|
}
|
|
375
379
|
phantomPropType(def) {
|
|
@@ -1054,7 +1058,10 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1054
1058
|
this.writeln("/* eslint-disable */");
|
|
1055
1059
|
this.writeln("/* oxlint-disable */");
|
|
1056
1060
|
const imports = ["defineAnnotatedType as $", "annotate as $a"];
|
|
1057
|
-
|
|
1061
|
+
const jsonSchemaMode = resolveJsonSchemaMode(this.opts);
|
|
1062
|
+
if (jsonSchemaMode === "lazy") imports.push("buildJsonSchema as $$");
|
|
1063
|
+
if (this.opts?.exampleData) imports.push("createDataFromAnnotatedType as $e");
|
|
1064
|
+
if (jsonSchemaMode === false) imports.push("throwFeatureDisabled as $d");
|
|
1058
1065
|
this.writeln(`import { ${imports.join(", ")} } from "@atscript/typescript/utils"`);
|
|
1059
1066
|
}
|
|
1060
1067
|
buildAdHocMap(annotateNodes) {
|
|
@@ -1100,6 +1107,7 @@ else {
|
|
|
1100
1107
|
this.writeln("static type = {}");
|
|
1101
1108
|
this.writeln("static metadata = new Map()");
|
|
1102
1109
|
this.renderJsonSchemaMethod(node);
|
|
1110
|
+
this.renderExampleDataMethod(node);
|
|
1103
1111
|
}
|
|
1104
1112
|
renderInterface(node) {
|
|
1105
1113
|
this.writeln();
|
|
@@ -1155,7 +1163,14 @@ else {
|
|
|
1155
1163
|
this.writeln("}");
|
|
1156
1164
|
} else {
|
|
1157
1165
|
this.writeln("static toJsonSchema() {");
|
|
1158
|
-
this.indent().writeln("
|
|
1166
|
+
this.indent().writeln("$d(\"JSON Schema\", \"jsonSchema\", \"emit.jsonSchema\")").unindent();
|
|
1167
|
+
this.writeln("}");
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
renderExampleDataMethod(_node) {
|
|
1171
|
+
if (this.opts?.exampleData) {
|
|
1172
|
+
this.writeln("static toExampleData() {");
|
|
1173
|
+
this.indent().writeln("return $e(this, { mode: \"example\" })").unindent();
|
|
1159
1174
|
this.writeln("}");
|
|
1160
1175
|
}
|
|
1161
1176
|
}
|
package/dist/utils.cjs
CHANGED
|
@@ -799,6 +799,12 @@ function build(def, path, mode) {
|
|
|
799
799
|
});
|
|
800
800
|
}
|
|
801
801
|
|
|
802
|
+
//#endregion
|
|
803
|
+
//#region packages/typescript/src/throw-disabled.ts
|
|
804
|
+
function throwFeatureDisabled(feature, option, annotation) {
|
|
805
|
+
throw new Error(`${feature} support is disabled. To enable, set \`${option}: 'lazy'\` or \`${option}: 'bundle'\` in tsPlugin options, or add @${annotation} annotation to individual interfaces.`);
|
|
806
|
+
}
|
|
807
|
+
|
|
802
808
|
//#endregion
|
|
803
809
|
//#region packages/typescript/src/flatten.ts
|
|
804
810
|
function flattenAnnotatedType(type, options) {
|
|
@@ -1058,4 +1064,5 @@ exports.fromJsonSchema = fromJsonSchema
|
|
|
1058
1064
|
exports.isAnnotatedType = isAnnotatedType
|
|
1059
1065
|
exports.isAnnotatedTypeOfPrimitive = isAnnotatedTypeOfPrimitive
|
|
1060
1066
|
exports.isPhantomType = isPhantomType
|
|
1061
|
-
exports.serializeAnnotatedType = serializeAnnotatedType
|
|
1067
|
+
exports.serializeAnnotatedType = serializeAnnotatedType
|
|
1068
|
+
exports.throwFeatureDisabled = throwFeatureDisabled
|
package/dist/utils.d.ts
CHANGED
|
@@ -48,11 +48,7 @@ interface TValidatorPluginContext {
|
|
|
48
48
|
* @typeParam T - The annotated type definition.
|
|
49
49
|
* @typeParam DataType - The TypeScript type that `validate` narrows to (auto-inferred).
|
|
50
50
|
*/
|
|
51
|
-
declare class Validator<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedType, DataType = T
|
|
52
|
-
type: {
|
|
53
|
-
__dataType?: infer D;
|
|
54
|
-
};
|
|
55
|
-
} ? unknown extends D ? T extends new (...args: any[]) => infer I ? I : unknown : D : unknown> {
|
|
51
|
+
declare class Validator<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedType, DataType = TAtscriptDataType<T>> {
|
|
56
52
|
protected readonly def: T;
|
|
57
53
|
protected opts: TValidatorOptions;
|
|
58
54
|
constructor(def: T, opts?: Partial<TValidatorOptions>);
|
|
@@ -148,6 +144,26 @@ interface TAtscriptTypeFinal<DataType = unknown> {
|
|
|
148
144
|
type InferDataType<T> = T extends {
|
|
149
145
|
__dataType?: infer D;
|
|
150
146
|
} ? D : unknown;
|
|
147
|
+
/**
|
|
148
|
+
* Extract the DataType from a {@link TAtscriptAnnotatedType}.
|
|
149
|
+
*
|
|
150
|
+
* Resolves the phantom `__dataType` carried by the type definition.
|
|
151
|
+
* When `__dataType` is `unknown` (unset), falls back to the constructor
|
|
152
|
+
* instance type if `T` is also a class (i.e. a generated interface).
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* import type { TAtscriptDataType } from '@atscript/typescript/utils'
|
|
157
|
+
* import MyInterface from './my-interface.as'
|
|
158
|
+
*
|
|
159
|
+
* type Data = TAtscriptDataType<typeof MyInterface>
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
type TAtscriptDataType<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedType> = T extends {
|
|
163
|
+
type: {
|
|
164
|
+
__dataType?: infer D;
|
|
165
|
+
};
|
|
166
|
+
} ? unknown extends D ? T extends new (...args: any[]) => infer I ? I : unknown : D : unknown;
|
|
151
167
|
/** Union of all possible type definition shapes. */
|
|
152
168
|
type TAtscriptTypeDef<DataType = unknown> = TAtscriptTypeComplex<DataType> | TAtscriptTypeFinal<DataType> | TAtscriptTypeArray<DataType> | TAtscriptTypeObject<string, DataType>;
|
|
153
169
|
/**
|
|
@@ -342,6 +358,12 @@ interface TCreateDataOptions {
|
|
|
342
358
|
*/
|
|
343
359
|
declare function createDataFromAnnotatedType(type: TAtscriptAnnotatedType, opts?: TCreateDataOptions): unknown;
|
|
344
360
|
|
|
361
|
+
/**
|
|
362
|
+
* Throws a runtime error indicating that a feature is disabled.
|
|
363
|
+
* Used by generated JS files to avoid duplicating the error message string.
|
|
364
|
+
*/
|
|
365
|
+
declare function throwFeatureDisabled(feature: string, option: string, annotation: string): never;
|
|
366
|
+
|
|
345
367
|
/**
|
|
346
368
|
* Options for controlling the flattening process.
|
|
347
369
|
*/
|
|
@@ -489,5 +511,5 @@ declare function serializeAnnotatedType(type: TAtscriptAnnotatedType, options?:
|
|
|
489
511
|
*/
|
|
490
512
|
declare function deserializeAnnotatedType(data: TSerializedAnnotatedType): TAtscriptAnnotatedType;
|
|
491
513
|
|
|
492
|
-
export { SERIALIZE_VERSION, Validator, ValidatorError, annotate, buildJsonSchema, createDataFromAnnotatedType, defineAnnotatedType, deserializeAnnotatedType, flattenAnnotatedType, forAnnotatedType, fromJsonSchema, isAnnotatedType, isAnnotatedTypeOfPrimitive, isPhantomType, serializeAnnotatedType };
|
|
493
|
-
export type { InferDataType, TAnnotatedTypeHandle, TAtscriptAnnotatedType, TAtscriptAnnotatedTypeConstructor, TAtscriptTypeArray, TAtscriptTypeComplex, TAtscriptTypeDef, TAtscriptTypeFinal, TAtscriptTypeObject, TCreateDataOptions, TFlattenOptions, TMetadataMap, TProcessAnnotationContext, TSerializeOptions, TSerializedAnnotatedType, TSerializedAnnotatedTypeInner, TSerializedTypeArray, TSerializedTypeComplex, TSerializedTypeDef, TSerializedTypeFinal, TSerializedTypeObject, TValidatorOptions, TValidatorPlugin, TValidatorPluginContext, TValueResolver };
|
|
514
|
+
export { SERIALIZE_VERSION, Validator, ValidatorError, annotate, buildJsonSchema, createDataFromAnnotatedType, defineAnnotatedType, deserializeAnnotatedType, flattenAnnotatedType, forAnnotatedType, fromJsonSchema, isAnnotatedType, isAnnotatedTypeOfPrimitive, isPhantomType, serializeAnnotatedType, throwFeatureDisabled };
|
|
515
|
+
export type { InferDataType, TAnnotatedTypeHandle, TAtscriptAnnotatedType, TAtscriptAnnotatedTypeConstructor, TAtscriptDataType, TAtscriptTypeArray, TAtscriptTypeComplex, TAtscriptTypeDef, TAtscriptTypeFinal, TAtscriptTypeObject, TCreateDataOptions, TFlattenOptions, TMetadataMap, TProcessAnnotationContext, TSerializeOptions, TSerializedAnnotatedType, TSerializedAnnotatedTypeInner, TSerializedTypeArray, TSerializedTypeComplex, TSerializedTypeDef, TSerializedTypeFinal, TSerializedTypeObject, TValidatorOptions, TValidatorPlugin, TValidatorPluginContext, TValueResolver };
|
package/dist/utils.mjs
CHANGED
|
@@ -798,6 +798,12 @@ function build(def, path, mode) {
|
|
|
798
798
|
});
|
|
799
799
|
}
|
|
800
800
|
|
|
801
|
+
//#endregion
|
|
802
|
+
//#region packages/typescript/src/throw-disabled.ts
|
|
803
|
+
function throwFeatureDisabled(feature, option, annotation) {
|
|
804
|
+
throw new Error(`${feature} support is disabled. To enable, set \`${option}: 'lazy'\` or \`${option}: 'bundle'\` in tsPlugin options, or add @${annotation} annotation to individual interfaces.`);
|
|
805
|
+
}
|
|
806
|
+
|
|
801
807
|
//#endregion
|
|
802
808
|
//#region packages/typescript/src/flatten.ts
|
|
803
809
|
function flattenAnnotatedType(type, options) {
|
|
@@ -1043,4 +1049,4 @@ function deserializeTypeDef(t) {
|
|
|
1043
1049
|
}
|
|
1044
1050
|
|
|
1045
1051
|
//#endregion
|
|
1046
|
-
export { SERIALIZE_VERSION, Validator, ValidatorError, annotate, buildJsonSchema, createDataFromAnnotatedType, defineAnnotatedType, deserializeAnnotatedType, flattenAnnotatedType, forAnnotatedType, fromJsonSchema, isAnnotatedType, isAnnotatedTypeOfPrimitive, isPhantomType, serializeAnnotatedType };
|
|
1052
|
+
export { SERIALIZE_VERSION, Validator, ValidatorError, annotate, buildJsonSchema, createDataFromAnnotatedType, defineAnnotatedType, deserializeAnnotatedType, flattenAnnotatedType, forAnnotatedType, fromJsonSchema, isAnnotatedType, isAnnotatedTypeOfPrimitive, isPhantomType, serializeAnnotatedType, throwFeatureDisabled };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atscript/typescript",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.22",
|
|
4
4
|
"description": "Atscript: typescript-gen support.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"annotations",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"vitest": "3.2.4"
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
|
-
"@atscript/core": "^0.1.
|
|
67
|
+
"@atscript/core": "^0.1.22"
|
|
68
68
|
},
|
|
69
69
|
"build": [
|
|
70
70
|
{},
|
|
@@ -85,7 +85,6 @@
|
|
|
85
85
|
"scripts": {
|
|
86
86
|
"pub": "pnpm publish --access public",
|
|
87
87
|
"test": "vitest",
|
|
88
|
-
"setup-skills": "node ./scripts/setup-skills.js"
|
|
89
|
-
"postinstall": "node ./scripts/setup-skills.js --postinstall"
|
|
88
|
+
"setup-skills": "node ./scripts/setup-skills.js"
|
|
90
89
|
}
|
|
91
90
|
}
|
|
@@ -34,7 +34,7 @@ import {
|
|
|
34
34
|
buildJsonSchema, fromJsonSchema,
|
|
35
35
|
serializeAnnotatedType, deserializeAnnotatedType,
|
|
36
36
|
flattenAnnotatedType, createDataFromAnnotatedType,
|
|
37
|
-
forAnnotatedType,
|
|
37
|
+
forAnnotatedType, throwFeatureDisabled,
|
|
38
38
|
} from '@atscript/typescript/utils'
|
|
39
39
|
|
|
40
40
|
// CLI
|
|
@@ -44,6 +44,8 @@ export declare class User {
|
|
|
44
44
|
static metadata: TMetadataMap<AtscriptMetadata>
|
|
45
45
|
static validator: (opts?: Partial<TValidatorOptions>) => Validator<typeof User>
|
|
46
46
|
static toJsonSchema: () => any
|
|
47
|
+
/** When exampleData is disabled (default), marked @deprecated + optional */
|
|
48
|
+
static toExampleData?: () => any
|
|
47
49
|
}
|
|
48
50
|
|
|
49
51
|
export type Status = "active" | "inactive"
|
|
@@ -53,13 +55,15 @@ declare namespace Status {
|
|
|
53
55
|
const metadata: TMetadataMap<AtscriptMetadata>
|
|
54
56
|
const validator: (opts?: Partial<TValidatorOptions>) => Validator<typeof Status>
|
|
55
57
|
const toJsonSchema: () => any
|
|
58
|
+
const toExampleData: (() => any) | undefined
|
|
56
59
|
}
|
|
57
60
|
```
|
|
58
61
|
|
|
59
62
|
Key points:
|
|
60
63
|
- **Interfaces** become `declare class` — so they work both as types and runtime values
|
|
61
64
|
- **Types** become a `type` alias + a companion `namespace` with runtime statics
|
|
62
|
-
- Each has `type`, `metadata`, `validator()`, and `
|
|
65
|
+
- Each has `type`, `metadata`, `validator()`, `toJsonSchema()`, and `toExampleData()` statics
|
|
66
|
+
- `toExampleData` is always optional in `.d.ts`. When `exampleData: true`, it's rendered without deprecation; when disabled, it's marked `@deprecated`
|
|
63
67
|
|
|
64
68
|
## `.js` Output
|
|
65
69
|
|
|
@@ -68,6 +72,8 @@ The JS module creates actual classes with runtime type definitions and metadata:
|
|
|
68
72
|
- Uses `defineAnnotatedType` (aliased as `$`) to build the type tree
|
|
69
73
|
- Populates metadata maps with all annotation values
|
|
70
74
|
- Wires up `validator()` and `toJsonSchema()` methods
|
|
75
|
+
- When `exampleData: true`, adds `toExampleData()` that calls `createDataFromAnnotatedType(this, { mode: 'example' })` (aliased as `$e`)
|
|
76
|
+
- When `jsonSchema: false` (default), `toJsonSchema()` calls `throwFeatureDisabled()` (aliased as `$d`) instead of inlining the error message
|
|
71
77
|
|
|
72
78
|
You don't normally read or modify generated JS — the build tool handles it.
|
|
73
79
|
|
|
@@ -71,10 +71,21 @@ tsPlugin({
|
|
|
71
71
|
jsonSchema: false // default — toJsonSchema() throws at runtime
|
|
72
72
|
// jsonSchema: 'lazy' — import buildJsonSchema, compute on demand, cache
|
|
73
73
|
// jsonSchema: 'bundle' — pre-compute at build time, embed in output
|
|
74
|
+
|
|
75
|
+
exampleData: false // default — toExampleData() not rendered
|
|
76
|
+
// exampleData: true — render toExampleData() using createDataFromAnnotatedType
|
|
74
77
|
})
|
|
75
78
|
```
|
|
76
79
|
|
|
77
|
-
|
|
80
|
+
### `jsonSchema`
|
|
81
|
+
|
|
82
|
+
Individual interfaces can override the JSON Schema setting with `@emit.jsonSchema` annotation to force build-time embedding regardless of plugin setting.
|
|
83
|
+
|
|
84
|
+
### `exampleData`
|
|
85
|
+
|
|
86
|
+
Controls whether `toExampleData()` is generated on output classes:
|
|
87
|
+
- `false` *(default)* — method is not rendered in `.js`; `.d.ts` marks it as optional + `@deprecated`
|
|
88
|
+
- `true` — each class gets `static toExampleData()` that calls `createDataFromAnnotatedType(this, { mode: 'example' })`, creating a new example data object on each call (no caching)
|
|
78
89
|
|
|
79
90
|
## Build Tool Integration
|
|
80
91
|
|
|
@@ -24,9 +24,15 @@ import {
|
|
|
24
24
|
flattenAnnotatedType,
|
|
25
25
|
// Data creation
|
|
26
26
|
createDataFromAnnotatedType,
|
|
27
|
+
// Feature gating (used by generated code)
|
|
28
|
+
throwFeatureDisabled,
|
|
27
29
|
} from '@atscript/typescript/utils'
|
|
28
30
|
```
|
|
29
31
|
|
|
32
|
+
### `throwFeatureDisabled(feature, option, annotation)`
|
|
33
|
+
|
|
34
|
+
Throws a runtime error indicating a feature is disabled. Used by generated `.js` files to avoid duplicating the error message string across all classes. Called as `$d("JSON Schema", "jsonSchema", "emit.jsonSchema")` in generated code when `jsonSchema: false`.
|
|
35
|
+
|
|
30
36
|
## `forAnnotatedType(def, handlers)` — Type-Safe Dispatch
|
|
31
37
|
|
|
32
38
|
Dispatches over `TAtscriptAnnotatedType` by its `type.kind`, providing type-narrowed handlers:
|
|
@@ -294,6 +300,55 @@ import { isPhantomType } from '@atscript/typescript/utils'
|
|
|
294
300
|
isPhantomType(someProperty) // true if designType === 'phantom'
|
|
295
301
|
```
|
|
296
302
|
|
|
303
|
+
## `TAtscriptDataType<T>` — Extract DataType from Annotated Type
|
|
304
|
+
|
|
305
|
+
Utility type that extracts the underlying data shape from a `TAtscriptAnnotatedType`. This is the primary way to obtain a TypeScript data type from an Atscript-generated class, especially useful in generic contexts.
|
|
306
|
+
|
|
307
|
+
```ts
|
|
308
|
+
import type { TAtscriptDataType } from '@atscript/typescript/utils'
|
|
309
|
+
import { Product } from './product.as'
|
|
310
|
+
|
|
311
|
+
type ProductData = TAtscriptDataType<typeof Product>
|
|
312
|
+
// ProductData = { name: string; price: number; tags: string[] }
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
### How It Resolves
|
|
316
|
+
|
|
317
|
+
1. Extracts the phantom `__dataType` from the type definition
|
|
318
|
+
2. If `__dataType` is `unknown` (unset), falls back to the constructor's instance type (`T extends new (...) => infer I`)
|
|
319
|
+
3. Otherwise returns `unknown`
|
|
320
|
+
|
|
321
|
+
### Use in Generics
|
|
322
|
+
|
|
323
|
+
`TAtscriptDataType` is designed for generic code that needs to derive data types from annotated type parameters:
|
|
324
|
+
|
|
325
|
+
```ts
|
|
326
|
+
import type { TAtscriptAnnotatedType, TAtscriptDataType } from '@atscript/typescript/utils'
|
|
327
|
+
|
|
328
|
+
// Generic repository that infers its entity type
|
|
329
|
+
class Repository<T extends TAtscriptAnnotatedType> {
|
|
330
|
+
findOne(id: string): Promise<TAtscriptDataType<T>> { /* ... */ }
|
|
331
|
+
insertOne(data: TAtscriptDataType<T>): Promise<void> { /* ... */ }
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Usage — DataType is automatically inferred
|
|
335
|
+
const repo = new Repository<typeof Product>()
|
|
336
|
+
const product = await repo.findOne('123') // typed as Product
|
|
337
|
+
|
|
338
|
+
// Generic function
|
|
339
|
+
function validate<T extends TAtscriptAnnotatedType>(
|
|
340
|
+
schema: T,
|
|
341
|
+
data: unknown
|
|
342
|
+
): data is TAtscriptDataType<T> {
|
|
343
|
+
return schema.validator().validate(data, true)
|
|
344
|
+
}
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
### Difference from `InferDataType`
|
|
348
|
+
|
|
349
|
+
- `TAtscriptDataType<T>` — operates on `TAtscriptAnnotatedType` (the full annotated wrapper). Use this for generated classes and generic code.
|
|
350
|
+
- `InferDataType<T>` — operates on raw type definitions (`TAtscriptTypeDef`, `TAtscriptTypeObject`, etc.). Lower-level, extracts `__dataType` from the type def's phantom generic directly.
|
|
351
|
+
|
|
297
352
|
## Type Exports
|
|
298
353
|
|
|
299
354
|
Key types you may need to import:
|
|
@@ -309,7 +364,8 @@ import type {
|
|
|
309
364
|
TAtscriptTypeComplex, // union/intersection/tuple type def
|
|
310
365
|
TMetadataMap, // typed metadata map
|
|
311
366
|
TAnnotatedTypeHandle, // fluent builder handle
|
|
312
|
-
InferDataType, // extract DataType from a type def
|
|
367
|
+
InferDataType, // extract DataType from a type def's phantom generic
|
|
368
|
+
TAtscriptDataType, // extract DataType from TAtscriptAnnotatedType
|
|
313
369
|
TValidatorOptions, // validator config
|
|
314
370
|
TValidatorPlugin, // plugin function type
|
|
315
371
|
TValidatorPluginContext, // plugin context
|