skir-typescript-gen 1.0.5 → 1.0.6
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/LICENSE +21 -0
- package/README.md +69 -9
- package/package.json +14 -10
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Tyler Fibonacci
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -16,6 +16,12 @@ In your `skir.yml` file, add the following snippet under `generators`:
|
|
|
16
16
|
config: {}
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
+
The generated TypeScript code has a runtime dependency on the `skir_client` library. Install it with:
|
|
20
|
+
|
|
21
|
+
```shell
|
|
22
|
+
npm i skir-client
|
|
23
|
+
```
|
|
24
|
+
|
|
19
25
|
For more information, see this TypeScript project [example](https://github.com/gepheum/skir-typescript-example).
|
|
20
26
|
|
|
21
27
|
## TypeScript generated code guide
|
|
@@ -211,14 +217,30 @@ Every frozen struct class and enum class has a static readonly `serializer` prop
|
|
|
211
217
|
const serializer = User.serializer;
|
|
212
218
|
|
|
213
219
|
// Serialize 'john' to dense JSON.
|
|
214
|
-
|
|
215
|
-
|
|
220
|
+
const johnDenseJson = serializer.toJson(john);
|
|
221
|
+
|
|
222
|
+
// With dense JSON, structs are encoded as JSON arrays.
|
|
223
|
+
assert(Array.isArray(johnDenseJson));
|
|
224
|
+
|
|
225
|
+
// toJsonCode() returns a string containing the JSON code.
|
|
226
|
+
// Equivalent to calling JSON.stringify() on toJson()'s result.
|
|
227
|
+
const johnDenseJsonCode = serializer.toJsonCode(john);
|
|
228
|
+
assert(johnDenseJsonCode.startsWith("["));
|
|
216
229
|
|
|
217
230
|
// Serialize 'john' to readable JSON.
|
|
218
231
|
console.log(serializer.toJsonCode(john, "readable"));
|
|
219
232
|
// {
|
|
220
233
|
// "user_id": 42,
|
|
221
|
-
// "name": "John Doe"
|
|
234
|
+
// "name": "John Doe",
|
|
235
|
+
// "quote": "Coffee is just a socially acceptable form of rage.",
|
|
236
|
+
// "pets": [
|
|
237
|
+
// {
|
|
238
|
+
// "name": "Dumbo",
|
|
239
|
+
// "height_in_meters": 1,
|
|
240
|
+
// "picture": "🐘"
|
|
241
|
+
// }
|
|
242
|
+
// ],
|
|
243
|
+
// "subscription_status": "FREE"
|
|
222
244
|
// }
|
|
223
245
|
|
|
224
246
|
// The dense JSON flavor is the flavor you should pick if you intend to
|
|
@@ -241,7 +263,7 @@ const johnBytes = serializer.toBytes(john);
|
|
|
241
263
|
```typescript
|
|
242
264
|
// Use fromJson(), fromJsonCode() and fromBytes() to deserialize.
|
|
243
265
|
|
|
244
|
-
const reserializedJohn = serializer.fromJsonCode(
|
|
266
|
+
const reserializedJohn = serializer.fromJsonCode(johnDenseJsonCode);
|
|
245
267
|
assert(reserializedJohn.name === "John Doe");
|
|
246
268
|
|
|
247
269
|
const reserializedJane = serializer.fromJsonCode(
|
|
@@ -249,10 +271,48 @@ const reserializedJane = serializer.fromJsonCode(
|
|
|
249
271
|
);
|
|
250
272
|
assert(reserializedJane.name === "Jane Doe");
|
|
251
273
|
|
|
252
|
-
|
|
253
|
-
|
|
274
|
+
assert(serializer.fromJson(johnDenseJson).name === "John Doe");
|
|
275
|
+
assert(serializer.fromBytes(johnBytes.toBuffer()).name === "John Doe");
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### Primitive serializers
|
|
279
|
+
|
|
280
|
+
```typescript
|
|
281
|
+
assert(primitiveSerializer("bool").toJson(true) === 1);
|
|
282
|
+
assert(primitiveSerializer("int32").toJson(3) === 3);
|
|
283
|
+
assert(
|
|
284
|
+
primitiveSerializer("int64").toJson(BigInt("9223372036854775807")) ===
|
|
285
|
+
"9223372036854775807",
|
|
286
|
+
);
|
|
287
|
+
assert(
|
|
288
|
+
primitiveSerializer("hash64").toJson(BigInt("18446744073709551615")) ===
|
|
289
|
+
"18446744073709551615",
|
|
290
|
+
);
|
|
291
|
+
assert(
|
|
292
|
+
primitiveSerializer("timestamp").toJson(
|
|
293
|
+
Timestamp.fromUnixMillis(1743682787000),
|
|
294
|
+
) === 1743682787000,
|
|
295
|
+
);
|
|
296
|
+
assert(primitiveSerializer("float32").toJson(3.14) === 3.14);
|
|
297
|
+
assert(primitiveSerializer("float64").toJson(3.14) === 3.14);
|
|
298
|
+
assert(primitiveSerializer("string").toJson("Foo") === "Foo");
|
|
299
|
+
assert(
|
|
300
|
+
primitiveSerializer("bytes").toJson(
|
|
301
|
+
ByteString.sliceOf(new Uint8Array([1, 2, 3]).buffer),
|
|
302
|
+
) === "AQID",
|
|
303
|
+
);
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
### Composite serializers
|
|
307
|
+
|
|
308
|
+
```typescript
|
|
309
|
+
assert(
|
|
310
|
+
optionalSerializer(primitiveSerializer("string")).toJson("foo") === "foo",
|
|
254
311
|
);
|
|
255
|
-
assert(
|
|
312
|
+
assert(optionalSerializer(primitiveSerializer("string")).toJson(null) === null);
|
|
313
|
+
|
|
314
|
+
console.log(arraySerializer(primitiveSerializer("bool")).toJson([true, false]));
|
|
315
|
+
// [1, 0]
|
|
256
316
|
```
|
|
257
317
|
|
|
258
318
|
### Frozen arrays and copies
|
|
@@ -309,9 +369,9 @@ console.log(TARZAN);
|
|
|
309
369
|
// name: 'Tarzan',
|
|
310
370
|
// quote: 'AAAAaAaAaAyAAAAaAaAaAyAAAAaAaAaA',
|
|
311
371
|
// pets: [ User_Pet { name: 'Cheeta', heightInMeters: 1.67, picture: '🐒' } ],
|
|
312
|
-
// subscriptionStatus:
|
|
372
|
+
// subscriptionStatus: SubscriptionStatus {
|
|
313
373
|
// kind: 'trial',
|
|
314
|
-
// value:
|
|
374
|
+
// value: SubscriptionStatus_Trial { startTime: [Timestamp] }
|
|
315
375
|
// }
|
|
316
376
|
// }
|
|
317
377
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "skir-typescript-gen",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"repository": {
|
|
@@ -32,25 +32,29 @@
|
|
|
32
32
|
"lint:fix": "eslint src/**/*.ts --fix"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"skir-internal": "^0.1.
|
|
35
|
+
"skir-internal": "^0.1.1",
|
|
36
36
|
"zod": "^4.2.1"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@eslint/js": "^
|
|
40
|
-
"@types/mocha": "^10.0.
|
|
39
|
+
"@eslint/js": "^10.0.1",
|
|
40
|
+
"@types/mocha": "^10.0.10",
|
|
41
41
|
"@types/node": "^20.6.0",
|
|
42
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
43
|
-
"@typescript-eslint/parser": "^8.
|
|
42
|
+
"@typescript-eslint/eslint-plugin": "^8.56.0",
|
|
43
|
+
"@typescript-eslint/parser": "^8.56.0",
|
|
44
44
|
"buckwheat": "^1.1.2",
|
|
45
|
-
"eslint": "^
|
|
45
|
+
"eslint": "^10.0.1",
|
|
46
46
|
"mocha": "^11.7.5",
|
|
47
47
|
"prettier": "^3.2.4",
|
|
48
48
|
"prettier-plugin-organize-imports": "^4.2.0",
|
|
49
|
-
"skir": "^1.0.
|
|
50
|
-
"skir-client": "^1.0.
|
|
49
|
+
"skir": "^1.0.25",
|
|
50
|
+
"skir-client": "^1.0.11",
|
|
51
51
|
"ts-node": "^10.9.2",
|
|
52
52
|
"tsx": "^4.21.0",
|
|
53
53
|
"typescript": "^5.2.2",
|
|
54
|
-
"typescript-eslint": "^8.
|
|
54
|
+
"typescript-eslint": "^8.56.0"
|
|
55
|
+
},
|
|
56
|
+
"overrides": {
|
|
57
|
+
"diff": "^8.0.3",
|
|
58
|
+
"minimatch": "^10.2.2"
|
|
55
59
|
}
|
|
56
60
|
}
|