skir-client 0.0.1 → 0.0.2
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/cjs/skir-client.d.ts +38 -21
- package/dist/cjs/skir-client.d.ts.map +1 -1
- package/dist/cjs/skir-client.js +49 -35
- package/dist/cjs/skir-client.js.map +1 -1
- package/dist/esm/skir-client.d.ts +38 -21
- package/dist/esm/skir-client.d.ts.map +1 -1
- package/dist/esm/skir-client.js +49 -35
- package/dist/esm/skir-client.js.map +1 -1
- package/package.json +1 -1
- package/src/skir-client.ts +70 -39
package/src/skir-client.ts
CHANGED
|
@@ -298,15 +298,15 @@ export interface BinaryForm {
|
|
|
298
298
|
* compact representation than when using JSON objects, and this makes
|
|
299
299
|
* serialization and deserialization a bit faster. Because field names are left
|
|
300
300
|
* out of the JSON, it is a representation which allows persistence: you can
|
|
301
|
-
* safely rename a field in a
|
|
301
|
+
* safely rename a field in a '.skir' file without breaking backwards
|
|
302
302
|
* compatibility.
|
|
303
303
|
* One con of this representation is that it is harder to tell, just by looking
|
|
304
304
|
* at the JSON, what field of the struct each value in the array corresponds to.
|
|
305
305
|
*
|
|
306
306
|
* When using the readable flavor, structs are converted to JSON objects. The
|
|
307
|
-
* name of each field in the
|
|
307
|
+
* name of each field in the '.skir' file is used as-is in the JSON. This
|
|
308
308
|
* results in a representation which is much more readable by humans, but also
|
|
309
|
-
* not suited for persistence: when you rename a field in a
|
|
309
|
+
* not suited for persistence: when you rename a field in a '.skir' file, you
|
|
310
310
|
* will no lonnger be able to deserialize old JSONs.
|
|
311
311
|
*
|
|
312
312
|
* @example
|
|
@@ -514,7 +514,7 @@ export interface PrimitiveTypes {
|
|
|
514
514
|
}
|
|
515
515
|
|
|
516
516
|
/**
|
|
517
|
-
* Describes an optional type. In a
|
|
517
|
+
* Describes an optional type. In a '.skir' file, an optional type is
|
|
518
518
|
* represented with a question mark at the end of another type.
|
|
519
519
|
*/
|
|
520
520
|
export interface OptionalDescriptor<T> extends TypeDescriptorBase {
|
|
@@ -539,7 +539,7 @@ export interface ArrayDescriptor<T> extends TypeDescriptorBase {
|
|
|
539
539
|
*/
|
|
540
540
|
export interface StructDescriptor<T = unknown> extends TypeDescriptorBase {
|
|
541
541
|
readonly kind: "struct";
|
|
542
|
-
/** Name of the struct as specified in the
|
|
542
|
+
/** Name of the struct as specified in the '.skir' file. */
|
|
543
543
|
readonly name: string;
|
|
544
544
|
/**
|
|
545
545
|
* A string containing all the names in the hierarchic sequence above and
|
|
@@ -558,7 +558,7 @@ export interface StructDescriptor<T = unknown> extends TypeDescriptorBase {
|
|
|
558
558
|
* Undefined if the struct is defined at the top-level of the module.
|
|
559
559
|
*/
|
|
560
560
|
readonly parentType: StructDescriptor | EnumDescriptor | undefined;
|
|
561
|
-
/** The fields of the struct in the order they appear in the
|
|
561
|
+
/** The fields of the struct in the order they appear in the '.skir' file. */
|
|
562
562
|
readonly fields: ReadonlyArray<StructField<T>>;
|
|
563
563
|
/** The field numbers marked as removed. */
|
|
564
564
|
readonly removedNumbers: ReadonlySet<number>;
|
|
@@ -583,7 +583,7 @@ export interface StructDescriptor<T = unknown> extends TypeDescriptorBase {
|
|
|
583
583
|
|
|
584
584
|
/** Field of a Skir struct. */
|
|
585
585
|
export interface StructField<Struct = unknown, Value = unknown> {
|
|
586
|
-
/** Field name as specified in the
|
|
586
|
+
/** Field name as specified in the '.skir' file, e.g. "user_id". */
|
|
587
587
|
readonly name: string;
|
|
588
588
|
/** Name of the property in the generated class, e.g. "userId". */
|
|
589
589
|
readonly property: string;
|
|
@@ -591,6 +591,11 @@ export interface StructField<Struct = unknown, Value = unknown> {
|
|
|
591
591
|
readonly number: number;
|
|
592
592
|
/** Describes the field type. */
|
|
593
593
|
readonly type: TypeDescriptor<Value>;
|
|
594
|
+
/**
|
|
595
|
+
* Documentation for this struct field, specified as doc comments in the
|
|
596
|
+
* '.skir' file. May be empty.
|
|
597
|
+
*/
|
|
598
|
+
doc: string;
|
|
594
599
|
|
|
595
600
|
/** Extracts the value of the field from the given struct. */
|
|
596
601
|
get(struct: Struct | MutableForm<Struct>): Value;
|
|
@@ -623,7 +628,7 @@ export type StructFieldResult<Struct, Key extends string | number> =
|
|
|
623
628
|
/** Describes a Skir enum. */
|
|
624
629
|
export interface EnumDescriptor<T = unknown> extends TypeDescriptorBase {
|
|
625
630
|
readonly kind: "enum";
|
|
626
|
-
/** Name of the enum as specified in the
|
|
631
|
+
/** Name of the enum as specified in the '.skir' file. */
|
|
627
632
|
readonly name: string;
|
|
628
633
|
/**
|
|
629
634
|
* A string containing all the names in the hierarchic sequence above and
|
|
@@ -644,7 +649,7 @@ export interface EnumDescriptor<T = unknown> extends TypeDescriptorBase {
|
|
|
644
649
|
readonly parentType: StructDescriptor | EnumDescriptor | undefined;
|
|
645
650
|
/**
|
|
646
651
|
* Includes the UNKNOWN variant, followed by the other variants in the order
|
|
647
|
-
* they appear in the
|
|
652
|
+
* they appear in the '.skir' file.
|
|
648
653
|
*/
|
|
649
654
|
readonly variants: ReadonlyArray<EnumVariant<T>>;
|
|
650
655
|
/** The variant numbers marked as removed. */
|
|
@@ -671,34 +676,44 @@ export type EnumVariant<Enum = unknown> =
|
|
|
671
676
|
| EnumConstantVariant<Enum>
|
|
672
677
|
| EnumWrapperVariant<Enum, unknown>;
|
|
673
678
|
|
|
674
|
-
/**
|
|
679
|
+
/** Constant variant of a Skir enum. */
|
|
675
680
|
export interface EnumConstantVariant<Enum = unknown> {
|
|
676
681
|
/**
|
|
677
|
-
* Variant name as specified in the
|
|
682
|
+
* Variant name as specified in the '.skir' file, e.g. "MONDAY".
|
|
678
683
|
* Always in UPPER_CASE format.
|
|
679
684
|
*/
|
|
680
685
|
readonly name: string;
|
|
681
686
|
/** Variant number. */
|
|
682
687
|
readonly number: number;
|
|
683
|
-
/** The instance of the generated class which corresponds to this
|
|
688
|
+
/** The instance of the generated class which corresponds to this variant. */
|
|
684
689
|
readonly constant: Enum;
|
|
685
690
|
/** Always undefined, unlike the `type` field of `EnumWrapperVariant`. */
|
|
686
691
|
readonly type?: undefined;
|
|
692
|
+
/**
|
|
693
|
+
* Documentation for this enum variant, specified as doc comments in the
|
|
694
|
+
* '.skir' file. May be empty.
|
|
695
|
+
*/
|
|
696
|
+
readonly doc: string;
|
|
687
697
|
}
|
|
688
698
|
|
|
689
|
-
/**
|
|
699
|
+
/** Wrapper variant of a Skir enum. */
|
|
690
700
|
export interface EnumWrapperVariant<Enum = unknown, Value = unknown> {
|
|
691
701
|
/**
|
|
692
|
-
* Variant name as specified in the
|
|
702
|
+
* Variant name as specified in the '.skir' file, e.g. "v4".
|
|
693
703
|
* Always in lower_case format.
|
|
694
704
|
*/
|
|
695
705
|
readonly name: string;
|
|
696
706
|
/** Variant number. */
|
|
697
707
|
readonly number: number;
|
|
698
|
-
/** Describes the type of the value held by the
|
|
708
|
+
/** Describes the type of the value held by the variant. */
|
|
699
709
|
readonly type: TypeDescriptor<Value>;
|
|
700
710
|
/** Always undefined, unlike the `type` field of `EnumConstantVariant`. */
|
|
701
711
|
readonly constant?: undefined;
|
|
712
|
+
/**
|
|
713
|
+
* Documentation for this enum variant, specified as doc comments in the
|
|
714
|
+
* '.skir' file. May be empty.
|
|
715
|
+
*/
|
|
716
|
+
readonly doc: string;
|
|
702
717
|
|
|
703
718
|
/**
|
|
704
719
|
* Extracts the value held by the given enum instance if it matches this
|
|
@@ -739,11 +754,11 @@ export type EnumVariantResult<Enum, Key extends string | number> =
|
|
|
739
754
|
* server side.
|
|
740
755
|
*/
|
|
741
756
|
export interface Method<Request, Response> {
|
|
742
|
-
/** Name of the procedure as specified in the
|
|
757
|
+
/** Name of the procedure as specified in the '.skir' file. */
|
|
743
758
|
name: string;
|
|
744
759
|
/**
|
|
745
760
|
* A number which uniquely identifies this procedure.
|
|
746
|
-
* When it is not specified in the
|
|
761
|
+
* When it is not specified in the '.skir' file, it is obtained by hashing the
|
|
747
762
|
* procedure name.
|
|
748
763
|
*/
|
|
749
764
|
number: number;
|
|
@@ -752,8 +767,8 @@ export interface Method<Request, Response> {
|
|
|
752
767
|
/** Serializer of response objects. */
|
|
753
768
|
responseSerializer: Serializer<Response>;
|
|
754
769
|
/**
|
|
755
|
-
* Documentation for this procedure specified as doc comments in the
|
|
756
|
-
* file.
|
|
770
|
+
* Documentation for this procedure, specified as doc comments in the '.skir'
|
|
771
|
+
* file. May be empty.
|
|
757
772
|
*/
|
|
758
773
|
doc: string;
|
|
759
774
|
}
|
|
@@ -1217,7 +1232,13 @@ export function parseTypeDescriptorFromJson(json: Json): TypeDescriptor {
|
|
|
1217
1232
|
for (const f of definition.fields) {
|
|
1218
1233
|
const fieldSerializer = parse(f.type!);
|
|
1219
1234
|
fields.push(
|
|
1220
|
-
new StructFieldImpl(
|
|
1235
|
+
new StructFieldImpl(
|
|
1236
|
+
f.name,
|
|
1237
|
+
f.name,
|
|
1238
|
+
f.number,
|
|
1239
|
+
fieldSerializer,
|
|
1240
|
+
f.doc ?? "",
|
|
1241
|
+
),
|
|
1221
1242
|
);
|
|
1222
1243
|
(defaultValue as AnyRecord)[f.name] = fieldSerializer.defaultValue;
|
|
1223
1244
|
}
|
|
@@ -1237,12 +1258,14 @@ export function parseTypeDescriptorFromJson(json: Json): TypeDescriptor {
|
|
|
1237
1258
|
f.name,
|
|
1238
1259
|
f.number,
|
|
1239
1260
|
parse(f.type),
|
|
1261
|
+
f.doc ?? "",
|
|
1240
1262
|
serializer.createFn,
|
|
1241
1263
|
)
|
|
1242
1264
|
: ({
|
|
1243
1265
|
name: f.name,
|
|
1244
1266
|
number: f.number,
|
|
1245
1267
|
constant: Object.freeze({ kind: f.name }),
|
|
1268
|
+
doc: f.doc ?? "",
|
|
1246
1269
|
} as EnumConstantVariant<Json>),
|
|
1247
1270
|
);
|
|
1248
1271
|
initOps.push(() =>
|
|
@@ -1686,6 +1709,7 @@ class StructFieldImpl<Struct = unknown, Value = unknown>
|
|
|
1686
1709
|
readonly property: string,
|
|
1687
1710
|
readonly number: number,
|
|
1688
1711
|
readonly serializer: InternalSerializer<Value>,
|
|
1712
|
+
readonly doc: string,
|
|
1689
1713
|
) {}
|
|
1690
1714
|
|
|
1691
1715
|
get type(): TypeDescriptor<Value> {
|
|
@@ -2001,7 +2025,7 @@ class StructSerializerImpl<T = unknown>
|
|
|
2001
2025
|
}
|
|
2002
2026
|
|
|
2003
2027
|
readonly kind = "struct";
|
|
2004
|
-
// Fields in the order they appear in the
|
|
2028
|
+
// Fields in the order they appear in the '.skir' file.
|
|
2005
2029
|
readonly fields: Array<StructFieldImpl<T>> = [];
|
|
2006
2030
|
readonly fieldMapping: { [key: string | number]: StructFieldImpl<T> } = {};
|
|
2007
2031
|
// Fields sorted by number in descending order.
|
|
@@ -2270,6 +2294,7 @@ class StructSerializerImpl<T = unknown>
|
|
|
2270
2294
|
name: f.name,
|
|
2271
2295
|
number: f.number,
|
|
2272
2296
|
type: f.serializer.typeSignature,
|
|
2297
|
+
doc: f.doc,
|
|
2273
2298
|
})),
|
|
2274
2299
|
};
|
|
2275
2300
|
}
|
|
@@ -2298,6 +2323,7 @@ class EnumWrapperVariantImpl<Enum, Value = unknown> {
|
|
|
2298
2323
|
readonly name: string,
|
|
2299
2324
|
readonly number: number,
|
|
2300
2325
|
readonly serializer: InternalSerializer<Value>,
|
|
2326
|
+
readonly doc: string,
|
|
2301
2327
|
private createFn: (initializer: { kind: string; value: unknown }) => Enum,
|
|
2302
2328
|
) {}
|
|
2303
2329
|
|
|
@@ -2445,7 +2471,7 @@ class EnumSerializerImpl<T = unknown>
|
|
|
2445
2471
|
}
|
|
2446
2472
|
serializer.encode(value, stream);
|
|
2447
2473
|
} else {
|
|
2448
|
-
// A constant
|
|
2474
|
+
// A constant variant.
|
|
2449
2475
|
encodeUint32(number, stream);
|
|
2450
2476
|
}
|
|
2451
2477
|
}
|
|
@@ -2517,11 +2543,11 @@ class EnumSerializerImpl<T = unknown>
|
|
|
2517
2543
|
return this.variantMapping[key]!;
|
|
2518
2544
|
}
|
|
2519
2545
|
|
|
2520
|
-
registerFieldsOrVariants(
|
|
2521
|
-
for (const
|
|
2522
|
-
this.variants.push(
|
|
2523
|
-
this.variantMapping[
|
|
2524
|
-
this.variantMapping[
|
|
2546
|
+
registerFieldsOrVariants(variants: ReadonlyArray<EnumVariantImpl<T>>): void {
|
|
2547
|
+
for (const variant of variants) {
|
|
2548
|
+
this.variants.push(variant);
|
|
2549
|
+
this.variantMapping[variant.name] = variant;
|
|
2550
|
+
this.variantMapping[variant.number] = variant;
|
|
2525
2551
|
}
|
|
2526
2552
|
}
|
|
2527
2553
|
|
|
@@ -3112,6 +3138,7 @@ interface StructFieldSpec {
|
|
|
3112
3138
|
property: string;
|
|
3113
3139
|
number: number;
|
|
3114
3140
|
type: TypeSpec;
|
|
3141
|
+
doc?: string;
|
|
3115
3142
|
mutableGetter?: string;
|
|
3116
3143
|
indexable?: IndexableSpec;
|
|
3117
3144
|
}
|
|
@@ -3135,14 +3162,15 @@ interface EnumSpec<Enum = unknown> {
|
|
|
3135
3162
|
createValueFn?: (initializer: unknown) => unknown;
|
|
3136
3163
|
name: string;
|
|
3137
3164
|
parentCtor?: { new (): unknown };
|
|
3138
|
-
|
|
3165
|
+
variants: EnumVariantSpec[];
|
|
3139
3166
|
removedNumbers?: readonly number[];
|
|
3140
3167
|
}
|
|
3141
3168
|
|
|
3142
|
-
interface
|
|
3169
|
+
interface EnumVariantSpec {
|
|
3143
3170
|
name: string;
|
|
3144
3171
|
number: number;
|
|
3145
3172
|
type?: TypeSpec;
|
|
3173
|
+
doc?: string;
|
|
3146
3174
|
}
|
|
3147
3175
|
|
|
3148
3176
|
type TypeSpec =
|
|
@@ -3165,7 +3193,7 @@ type TypeSpec =
|
|
|
3165
3193
|
};
|
|
3166
3194
|
|
|
3167
3195
|
// The UNKNOWN variant is common to all enums.
|
|
3168
|
-
const
|
|
3196
|
+
const UNKNOWN_VARIANT_SPEC: EnumVariantSpec = {
|
|
3169
3197
|
name: "?",
|
|
3170
3198
|
number: 0,
|
|
3171
3199
|
};
|
|
@@ -3217,15 +3245,15 @@ export function _initModuleClasses(
|
|
|
3217
3245
|
}
|
|
3218
3246
|
case "enum": {
|
|
3219
3247
|
// Create the constants.
|
|
3220
|
-
// Prepend the UNKNOWN variant to the array of
|
|
3248
|
+
// Prepend the UNKNOWN variant to the array of variants specified from the
|
|
3221
3249
|
// generated code.
|
|
3222
|
-
record.
|
|
3223
|
-
for (const
|
|
3224
|
-
if (
|
|
3250
|
+
record.variants = [UNKNOWN_VARIANT_SPEC].concat(record.variants);
|
|
3251
|
+
for (const variant of record.variants) {
|
|
3252
|
+
if (variant.type) {
|
|
3225
3253
|
continue;
|
|
3226
3254
|
}
|
|
3227
|
-
const property = enumConstantNameToProperty(
|
|
3228
|
-
clazz[property] = new record.ctor(PRIVATE_KEY,
|
|
3255
|
+
const property = enumConstantNameToProperty(variant.name);
|
|
3256
|
+
clazz[property] = new record.ctor(PRIVATE_KEY, variant.name);
|
|
3229
3257
|
}
|
|
3230
3258
|
// Define the 'create' static factory function.
|
|
3231
3259
|
const createFn = makeCreateEnumFunction(record);
|
|
@@ -3259,6 +3287,7 @@ export function _initModuleClasses(
|
|
|
3259
3287
|
f.property,
|
|
3260
3288
|
f.number,
|
|
3261
3289
|
getSerializerForType(f.type) as InternalSerializer,
|
|
3290
|
+
f.doc ?? "",
|
|
3262
3291
|
),
|
|
3263
3292
|
);
|
|
3264
3293
|
const serializer = clazz.serializer as StructSerializerImpl;
|
|
@@ -3298,25 +3327,27 @@ export function _initModuleClasses(
|
|
|
3298
3327
|
}
|
|
3299
3328
|
case "enum": {
|
|
3300
3329
|
const serializer = clazz.serializer as EnumSerializerImpl;
|
|
3301
|
-
const
|
|
3330
|
+
const variants = record.variants.map((f) =>
|
|
3302
3331
|
f.type
|
|
3303
3332
|
? new EnumWrapperVariantImpl(
|
|
3304
3333
|
f.name,
|
|
3305
3334
|
f.number,
|
|
3306
3335
|
getSerializerForType(f.type) as InternalSerializer,
|
|
3336
|
+
f.doc ?? "",
|
|
3307
3337
|
serializer.createFn,
|
|
3308
3338
|
)
|
|
3309
3339
|
: {
|
|
3310
3340
|
name: f.name,
|
|
3311
3341
|
number: f.number,
|
|
3312
3342
|
constant: clazz[enumConstantNameToProperty(f.name)],
|
|
3343
|
+
doc: f.doc ?? "",
|
|
3313
3344
|
},
|
|
3314
3345
|
);
|
|
3315
3346
|
serializer.init(
|
|
3316
3347
|
record.name,
|
|
3317
3348
|
modulePath,
|
|
3318
3349
|
parentTypeDescriptor,
|
|
3319
|
-
|
|
3350
|
+
variants,
|
|
3320
3351
|
record.removedNumbers ?? [],
|
|
3321
3352
|
);
|
|
3322
3353
|
// Freeze the enum class.
|
|
@@ -3360,7 +3391,7 @@ function makeCreateEnumFunction(
|
|
|
3360
3391
|
}
|
|
3361
3392
|
const value = createValue(initializer);
|
|
3362
3393
|
if (value === undefined) {
|
|
3363
|
-
throw new Error(`Wrapper
|
|
3394
|
+
throw new Error(`Wrapper variant not found: ${kind}`);
|
|
3364
3395
|
}
|
|
3365
3396
|
return new ctor(privateKey, kind, value);
|
|
3366
3397
|
};
|