arri 0.0.44 → 0.1.0-beta.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.
@@ -1,786 +0,0 @@
1
- 'use strict';
2
-
3
- const child_process = require('child_process');
4
- const require$$0 = require('fs');
5
- const utils = require('./arri.0752aa2b.cjs');
6
-
7
- function defineClientGeneratorPlugin(plugin) {
8
- return plugin;
9
- }
10
-
11
- const JsonSchemaScalarTypeValues = [
12
- "integer",
13
- "number",
14
- "bigint",
15
- "string",
16
- "boolean",
17
- "Date"
18
- ];
19
- const JsonSchemaNullTypeValues = ["null", "undefined"];
20
- function isJsonSchemaScalarType(input) {
21
- if (typeof input !== "object") {
22
- return false;
23
- }
24
- if (!("type" in input)) {
25
- return false;
26
- }
27
- return JsonSchemaScalarTypeValues.includes(input.type);
28
- }
29
- function isJsonSchemaNullType(input) {
30
- if (typeof input !== "object") {
31
- return false;
32
- }
33
- if (!("type" in input)) {
34
- return false;
35
- }
36
- return JsonSchemaNullTypeValues.includes(input.type);
37
- }
38
- const isJsonSchemaObject = (input) => {
39
- if (typeof input !== "object") {
40
- return false;
41
- }
42
- return "type" in input && input.type === "object" && "properties" in input && typeof input.properties === "object";
43
- };
44
- const isJsonSchemaRecord = (input) => {
45
- if (typeof input !== "object") {
46
- return false;
47
- }
48
- return "type" in input && input.type === "object" && "patternProperties" in input && typeof input.patternProperties === "object";
49
- };
50
- function isJsonSchemaArray(input) {
51
- if (typeof input !== "object") {
52
- return false;
53
- }
54
- if ("type" in input && input.type === "array") {
55
- return true;
56
- }
57
- return false;
58
- }
59
- function isJsonSchemaEnum(input) {
60
- if (typeof input !== "object") {
61
- return false;
62
- }
63
- if (!("anyOf" in input) || !Array.isArray(input.anyOf)) {
64
- return false;
65
- }
66
- let prevType;
67
- for (const item of input.anyOf) {
68
- if (!isJsonSchemaScalarType(item)) {
69
- return false;
70
- }
71
- if (item.type !== "string" && item.type !== "integer" && item.type !== "number") {
72
- return false;
73
- }
74
- if (!prevType) {
75
- prevType = item.type;
76
- } else if (prevType !== item.type) {
77
- return false;
78
- }
79
- }
80
- return true;
81
- }
82
-
83
- const dartClientGenerator = defineClientGeneratorPlugin(
84
- (options) => {
85
- return {
86
- generator: async (def) => {
87
- if (!options.clientName) {
88
- throw new Error(
89
- 'Missing "clientName" cannot generate dart client'
90
- );
91
- }
92
- if (!options.outputFile) {
93
- throw new Error(
94
- 'Missing "outputFile" cannot generate dart client'
95
- );
96
- }
97
- const numProcedures = Object.keys(def.procedures).length;
98
- if (numProcedures <= 0) {
99
- console.warn(
100
- "No procedures found in definition file. Dart client will not be generated"
101
- );
102
- return;
103
- }
104
- const result = createDartClient(def, {
105
- clientName: options.clientName
106
- });
107
- require$$0.writeFileSync(options.outputFile, result);
108
- try {
109
- child_process.execSync(`dart format ${options.outputFile}`);
110
- } catch (err) {
111
- console.error("Error formatting dart client", err);
112
- }
113
- },
114
- options
115
- };
116
- }
117
- );
118
- let generatedModels = [];
119
- function createDartClient(appDef, opts = { clientName: "Client" }) {
120
- generatedModels = [];
121
- const { models, procedures } = appDef;
122
- const serviceParts = [];
123
- const procedureParts = [];
124
- const services = utils.unflattenProcedures(procedures);
125
- Object.keys(services).forEach((k) => {
126
- const item = services[k];
127
- if (utils.isServiceDef(item)) {
128
- const serviceName = utils.pascalCase(`${opts.clientName}_${k}_service`);
129
- serviceParts.push({
130
- name: serviceName,
131
- key: k,
132
- content: dartServiceFromServiceDefinition(
133
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
134
- serviceName,
135
- item)
136
- });
137
- return;
138
- }
139
- procedureParts.push(dartProcedureFromServiceDefinition(k, item));
140
- });
141
- const endpoints = {};
142
- Object.keys(procedures).forEach((key) => {
143
- const rpc = procedures[key];
144
- endpoints[rpc.path] = rpc.method;
145
- });
146
- const modelParts = [];
147
- Object.keys(models).forEach((key) => {
148
- const model = models[key];
149
- const typeName = utils.pascalCase(key);
150
- const objModel = model;
151
- if (!generatedModels.includes(typeName)) {
152
- modelParts.push(
153
- dartModelFromJsonSchema(`${utils.pascalCase(key)}`, objModel)
154
- );
155
- generatedModels.push(typeName);
156
- }
157
- });
158
- return `// This code was autogenerated by Arri. Do not modify directly.
159
- // For additional documentation visit http://github.com/modiimedia/arri
160
- import "dart:convert";
161
- import "package:arri_client/arri_client.dart";
162
-
163
- class ${opts.clientName} {
164
- final String _baseUrl;
165
- final Map<String, String> _headers;
166
- const ${opts.clientName}({
167
- String baseUrl = "",
168
- Map<String, String> headers = const {},
169
- }): _headers = headers,
170
- _baseUrl = baseUrl;
171
- ${procedureParts.join("\n")}
172
- ${serviceParts.map(
173
- (service) => `${service.name} get ${service.key} {
174
- return ${service.name}(
175
- baseUrl: _baseUrl, headers: _headers,
176
- );
177
- }`
178
- ).join("\n ")}
179
- }
180
- ${serviceParts.map((item) => item.content).join("\n")}
181
- ${modelParts.join("\n")}
182
-
183
- enum ${opts.clientName}Endpoints implements Comparable<${opts.clientName}Endpoints>, ArriEndpoint {
184
- ${Object.keys(endpoints).map(
185
- (key) => `${utils.camelCase(key.split("/").join("_"))}(
186
- path: "${key}",
187
- method: HttpMethod.${endpoints[key]},
188
- )`
189
- ).join(",\n ")};
190
-
191
- const ${opts.clientName}Endpoints({
192
- required this.path, required this.method,
193
- });
194
- @override
195
- final String path;
196
- @override
197
- final HttpMethod method;
198
-
199
- @override
200
- compareTo(${opts.clientName}Endpoints other) => name.compareTo(other.name);
201
- }`;
202
- }
203
- function dartServiceFromServiceDefinition(name, def, opts) {
204
- const rpcParts = [];
205
- const subServiceParts = [];
206
- const serviceName = `${name}`;
207
- Object.keys(def).forEach((key) => {
208
- const item = def[key];
209
- if (utils.isProcedureDef(item)) {
210
- rpcParts.push(dartProcedureFromServiceDefinition(key, item));
211
- return;
212
- }
213
- const nameParts = name.split("Service");
214
- nameParts.pop();
215
- const subServiceName = utils.pascalCase(
216
- `${nameParts.join("")}_${key}_Service`
217
- );
218
- const subService = dartServiceFromServiceDefinition(
219
- subServiceName,
220
- item);
221
- subServiceParts.push({
222
- name: subServiceName,
223
- key,
224
- content: subService
225
- });
226
- });
227
- return `class ${serviceName} {
228
- final String _baseUrl;
229
- final Map<String, String> _headers;
230
- const ${serviceName}({
231
- String baseUrl = "",
232
- Map<String, String> headers = const {},
233
- }): _baseUrl = baseUrl,
234
- _headers = headers;
235
- ${subServiceParts.map(
236
- (sub) => `${sub.name} get ${sub.key} {
237
- return ${sub.name}(
238
- baseUrl: _baseUrl,
239
- headers: _headers,
240
- );
241
- }`
242
- ).join("\n")}
243
- ${rpcParts.join("\n ")}
244
- }
245
- ${subServiceParts.map((sub) => sub.content).join("\n")}
246
- `;
247
- }
248
- function dartProcedureFromServiceDefinition(key, def, opts) {
249
- let returnType = `Future<String>`;
250
- let returnTypeName = "String";
251
- if (def.response) {
252
- returnType = `Future<${def.response}>`;
253
- returnTypeName = `${def.response}`;
254
- } else {
255
- returnType = "Future<void>";
256
- }
257
- let paramsInput = "";
258
- if (def.params) {
259
- paramsInput = `${def.params} params`;
260
- }
261
- let responseParser = "(body) => body;";
262
- switch (returnType) {
263
- case "Future<String>":
264
- break;
265
- case "Future<int>":
266
- responseParser = `(body) => Int.parse(body)`;
267
- break;
268
- case "Future<double>":
269
- responseParser = `(body) => Double.parse(body)`;
270
- break;
271
- case "Future<void>":
272
- responseParser = `(body) {}`;
273
- break;
274
- case "Future<bool>":
275
- responseParser = `(body) {
276
- switch(body) {
277
- case "true":
278
- case "1":
279
- return true;
280
- case "false":
281
- case "0":
282
- default:
283
- return false;
284
- }
285
- }`;
286
- break;
287
- default:
288
- responseParser = `(body) => ${returnTypeName}.fromJson(json.decode(body))`;
289
- break;
290
- }
291
- return `${returnType} ${key}(${paramsInput}) {
292
- return parsedArriRequest(
293
- "$_baseUrl${def.path}",
294
- method: HttpMethod.${def.method},
295
- headers: _headers,
296
- params: ${paramsInput.length ? `params.toJson()` : "null"},
297
- parser: ${responseParser},
298
- );
299
- }`;
300
- }
301
- function dartModelFromJsonSchema(modelName, schema, opts) {
302
- const modelDisplayName = utils.pascalCase(`${modelName}`);
303
- const fields = [];
304
- const subModelParts = [];
305
- Object.entries(schema.properties ?? {}).forEach(([key, val]) => {
306
- if (typeof val !== "object") {
307
- return;
308
- }
309
- const [dartType, subTypes] = dartPropertyTypeFromSchema(
310
- modelName,
311
- key,
312
- schema);
313
- if (subTypes?.length) {
314
- for (const sub of subTypes) {
315
- subModelParts.push(sub);
316
- }
317
- }
318
- fields.push({ type: dartType, name: utils.camelCase(key), jsonKey: key });
319
- if (!isDartType(dartType) && isJsonSchemaObject(val)) {
320
- if (val.$id && generatedModels.includes(val.$id)) {
321
- return;
322
- }
323
- const subModelName = val.$id ? utils.pascalCase(val.$id) : utils.pascalCase(dartType.replace("?", ""));
324
- subModelParts.push(
325
- dartModelFromJsonSchema(subModelName, val)
326
- );
327
- generatedModels.push(subModelName);
328
- }
329
- });
330
- return `class ${modelDisplayName} {
331
- ${fields.map((field) => `final ${field.type} ${field.name};`).join("\n ")}
332
- const ${modelDisplayName}({
333
- ${fields.map(
334
- (field) => field.type.includes("?") ? `this.${field.name},` : `required this.${field.name},`
335
- ).join("\n ")}
336
- });
337
- factory ${modelDisplayName}.fromJson(Map<String, dynamic> json) {
338
- return ${modelDisplayName}(
339
- ${fields.map(
340
- (field) => `${dartParsedJsonField(
341
- field.name,
342
- field.jsonKey,
343
- field.type
344
- )},`
345
- ).join("\n ")}
346
- );
347
- }
348
- Map<String, dynamic> toJson() {
349
- return {
350
- ${fields.map((field) => {
351
- const isNullable = field.type.endsWith("?");
352
- const typeName = field.type.replace("?", "");
353
- if (typeName === "dynamic") {
354
- return `"${field.jsonKey}": ${field.name}`;
355
- }
356
- if (typeName === "List<dynamic>" || typeName === "List<dynamic>?") {
357
- return `"${field.jsonKey}": ${field.name}`;
358
- }
359
- if (isDartTypeWithNullables(field.type)) {
360
- const transformer = transformers[typeName];
361
- return transformer.toJsonBody(
362
- field.name,
363
- field.jsonKey,
364
- isNullable
365
- );
366
- }
367
- if (field.type.includes("List<")) {
368
- return classListTransformer(typeName).toJsonBody(
369
- field.name,
370
- field.jsonKey,
371
- isNullable
372
- );
373
- }
374
- if (field.type.includes("Map<")) {
375
- return `"${field.jsonKey}": ${field.name}`;
376
- }
377
- return classTransformer(typeName, isNullable).toJsonBody(
378
- field.name,
379
- field.jsonKey,
380
- isNullable
381
- );
382
- }).join(",\n ")},
383
- };
384
- }
385
- ${modelDisplayName} copyWith({
386
- ${fields.map(
387
- (field) => `${field.type.replace("?", "")}${field.type !== "dynamic" ? "?" : ""} ${field.name},`
388
- ).join("\n ")}
389
- }) {
390
- return ${modelDisplayName}(
391
- ${fields.map((field) => `${field.name}: ${field.name} ?? this.${field.name},`).join("\n ")}
392
- );
393
- }
394
- }
395
-
396
- ${subModelParts.join("\n")}`;
397
- }
398
- function dartPropertyTypeFromSchema(objectName, propertyName, schema, opts) {
399
- const prop = schema.properties?.[propertyName];
400
- const isOptional = !schema.required?.includes(propertyName);
401
- let finalType = "dynamic";
402
- const subTypes = [];
403
- if (isJsonSchemaEnum(prop)) {
404
- const enumName = utils.pascalCase(`${objectName}_${propertyName}`);
405
- finalType = isOptional ? `${enumName}?` : enumName;
406
- const options = [];
407
- for (const opt of prop.anyOf) {
408
- if (opt.type === "string") {
409
- options.push({
410
- name: utils.camelCase(opt.const.toString()),
411
- type: "String",
412
- value: opt.const
413
- });
414
- }
415
- if (opt.type === "number" || opt.type === "integer") {
416
- options.push({
417
- name: utils.camelCase(
418
- `num_${opt.const}`.split(".").join("Point")
419
- ),
420
- type: opt.type === "number" ? "double" : "int",
421
- value: opt.const
422
- });
423
- }
424
- }
425
- subTypes.push(`enum ${enumName} implements Comparable<${enumName}> {
426
- ${options.map(
427
- (opt) => `${opt.name}(${opt.type === "String" ? `"${opt.value}"` : opt.value})`
428
- ).join(",\n ")};
429
- const ${enumName}(this.value);
430
- final dynamic value;
431
-
432
- @override
433
- compareTo(${enumName} other) => name.compareTo(other.name);
434
-
435
- factory ${enumName}.fromJson(dynamic input) {
436
- for(final val in values) {
437
- if(val.value == input) {
438
- return val;
439
- }
440
- }
441
- return ${options[0].name};
442
- }
443
-
444
- dynamic toJson() {
445
- return value;
446
- }
447
- }`);
448
- return [finalType, subTypes];
449
- }
450
- if (isJsonSchemaNullType(prop)) {
451
- finalType = "null";
452
- return [finalType, subTypes];
453
- }
454
- if (isJsonSchemaScalarType(prop)) {
455
- switch (prop.type) {
456
- case "string":
457
- finalType = isOptional ? "String?" : "String";
458
- break;
459
- case "integer":
460
- finalType = isOptional ? "int?" : "int";
461
- break;
462
- case "number":
463
- finalType = isOptional ? "double?" : "double";
464
- break;
465
- case "Date":
466
- finalType = isOptional ? "DateTime?" : "DateTime";
467
- break;
468
- case "boolean":
469
- finalType = isOptional ? "bool?" : "bool";
470
- break;
471
- }
472
- }
473
- if (isJsonSchemaObject(prop)) {
474
- const joinedPropName = prop.$id ? utils.pascalCase(prop.$id) : utils.pascalCase(`${objectName}_${propertyName}`);
475
- finalType = isOptional ? `${joinedPropName}?` : joinedPropName;
476
- }
477
- if (isJsonSchemaRecord(prop)) {
478
- finalType = isOptional ? `Map<dynamic, dynamic>?` : "Map<dynamic, dynamic>";
479
- }
480
- if (isJsonSchemaArray(prop)) {
481
- const item = prop.items;
482
- const type = item.type;
483
- switch (type) {
484
- case "string":
485
- finalType = isOptional ? "List<String>?" : "List<String>";
486
- break;
487
- case "integer":
488
- finalType = isOptional ? "List<int>?" : "List<int>";
489
- break;
490
- case "number":
491
- finalType = isOptional ? "List<double>?" : "List<double>";
492
- break;
493
- case "null":
494
- finalType = isOptional ? "List<null>?" : "List<null>";
495
- break;
496
- case "boolean":
497
- finalType = isOptional ? "List<bool>?" : "List<bool>";
498
- break;
499
- case "Date":
500
- finalType = isOptional ? "List<DateTime>?" : "List<DateTime>";
501
- break;
502
- case "object": {
503
- if (isJsonSchemaObject(item)) {
504
- const subTypeName = item.$id ?? utils.pascalCase(`${objectName}_${propertyName}_item`);
505
- if (!generatedModels.includes(subTypeName)) {
506
- const model = dartModelFromJsonSchema(
507
- subTypeName,
508
- item);
509
- subTypes.push(model);
510
- generatedModels.push(subTypeName);
511
- }
512
- finalType = isOptional ? `List<${subTypeName}>?` : `List<${subTypeName}>`;
513
- } else {
514
- finalType = isOptional ? `Map<dynamic, dynamic>?` : `Map<dynamic, dynamic>`;
515
- }
516
- break;
517
- }
518
- default:
519
- finalType = isOptional ? "List<dynamic>?" : "List<dynamic>";
520
- break;
521
- }
522
- }
523
- return [finalType, subTypes.length ? subTypes : void 0];
524
- }
525
- function dartParsedJsonField(fieldName, jsonKey, dartType) {
526
- if (dartType === "dynamic") {
527
- return `${fieldName}: json["${jsonKey}"]`;
528
- }
529
- if (dartType === "List<dynamic>" || dartType === "List<dynamic>?") {
530
- return `${fieldName}: json["${jsonKey}"]`;
531
- }
532
- if (isDartTypeWithNullables(dartType)) {
533
- switch (dartType) {
534
- case "String":
535
- return transformers.String.fromJsonBody(
536
- fieldName,
537
- jsonKey,
538
- '""'
539
- );
540
- case "String?":
541
- return transformers.String.fromJsonBody(
542
- fieldName,
543
- jsonKey,
544
- "null"
545
- );
546
- case "bool":
547
- return transformers.bool.fromJsonBody(
548
- fieldName,
549
- jsonKey,
550
- "false"
551
- );
552
- case "bool?":
553
- return transformers.bool.fromJsonBody(
554
- fieldName,
555
- jsonKey,
556
- "null"
557
- );
558
- case "double":
559
- return transformers.double.fromJsonBody(
560
- fieldName,
561
- jsonKey,
562
- "0.0"
563
- );
564
- case "double?":
565
- return transformers.double.fromJsonBody(
566
- fieldName,
567
- jsonKey,
568
- "null"
569
- );
570
- case "int":
571
- return transformers.int.fromJsonBody(fieldName, jsonKey, "0");
572
- case "int?":
573
- return transformers.int.fromJsonBody(
574
- fieldName,
575
- jsonKey,
576
- "null"
577
- );
578
- case "null":
579
- return transformers.null.fromJsonBody(fieldName, jsonKey, "");
580
- case "DateTime":
581
- return transformers.DateTime.fromJsonBody(
582
- fieldName,
583
- jsonKey,
584
- "DateTime(0)"
585
- );
586
- case "DateTime?":
587
- return transformers.DateTime.fromJsonBody(
588
- fieldName,
589
- jsonKey,
590
- "null"
591
- );
592
- case "List<String>":
593
- case "List<bool>":
594
- case "List<double>":
595
- case "List<int>":
596
- case "List<null>":
597
- return defaultListFromJsonBody(
598
- fieldName,
599
- jsonKey,
600
- dartType.replace("List<", "").replace(">", ""),
601
- "[]"
602
- );
603
- default:
604
- return `${fieldName}: json["${jsonKey}"] is ${dartType.replace(
605
- "?",
606
- ""
607
- )} ? json["${jsonKey}"] : null`;
608
- }
609
- }
610
- if (dartType.includes("List<")) {
611
- const innerType = dartType.replace("List<", "").replace(">", "").replace("?", "");
612
- return classListTransformer(innerType).fromJsonBody(
613
- fieldName,
614
- jsonKey,
615
- dartType.endsWith("?") ? "null" : "[]"
616
- );
617
- }
618
- if (dartType.includes("Map<")) {
619
- return `${fieldName}: json["${jsonKey}"] is Map ? json["${jsonKey}"] : {}`;
620
- }
621
- const classType = dartType.split("?").join("").trim();
622
- return classTransformer(classType, dartType.endsWith("?")).fromJsonBody(
623
- fieldName,
624
- jsonKey,
625
- classType
626
- );
627
- }
628
- const dartTypeMap = {
629
- integer: "int",
630
- bigint: "int",
631
- number: "double",
632
- string: "String",
633
- boolean: "bool",
634
- null: "null",
635
- Date: "DateTime",
636
- object: void 0,
637
- list: void 0,
638
- // eslint-disable-next-line object-shorthand
639
- undefined: void 0,
640
- array: void 0,
641
- Uint8Array: "Uint8List"
642
- };
643
- const transformers = {
644
- null: {
645
- fromJsonBody: (_) => `null`,
646
- toJsonBody: defaultToJsonBody
647
- },
648
- int: {
649
- fromJsonBody: (key, jsonKey, fallback) => fallback === "null" ? `${key}: nullableIntFromDynamic(json["${jsonKey}"])` : `${key}: intFromDynamic(json["${jsonKey}"], ${fallback})`,
650
- toJsonBody: defaultToJsonBody
651
- },
652
- double: {
653
- fromJsonBody: (key, jsonKey, fallback) => fallback === "null" ? `${key}: nullableDoubleFromDynamic(json["${jsonKey}"])` : `${key}: doubleFromDynamic(json["${jsonKey}"], ${fallback})`,
654
- toJsonBody: defaultToJsonBody
655
- },
656
- String: {
657
- fromJsonBody: (key, jsonKey, fallback) => defaultFromJsonBody(key, jsonKey, "String", fallback),
658
- toJsonBody: defaultToJsonBody
659
- },
660
- bool: {
661
- fromJsonBody: (key, jsonKey, fallback) => defaultFromJsonBody(key, jsonKey, "bool", fallback),
662
- toJsonBody: defaultToJsonBody
663
- },
664
- DateTime: {
665
- fromJsonBody: (key, jsonKey, fallback) => fallback === "null" ? `${key}: nullableDateTimeFromDynamic(json["${jsonKey}"])` : `${key}: dateTimeFromDynamic(json["${jsonKey}"], ${fallback})`,
666
- toJsonBody: (key, jsonKey, isNullable) => `"${jsonKey}": ${key}${isNullable ? "?" : ""}.toIso8601String()`
667
- },
668
- "List<null>": {
669
- fromJsonBody: (key, jsonKey, fallback) => defaultListFromJsonBody(key, jsonKey, "null", fallback),
670
- toJsonBody: defaultToJsonBody
671
- },
672
- "List<int>": {
673
- fromJsonBody: (key, jsonKey, fallback) => defaultListFromJsonBody(key, jsonKey, "int", fallback),
674
- toJsonBody: defaultToJsonBody
675
- },
676
- "List<double>": {
677
- fromJsonBody: (key, jsonKey, fallback) => defaultListFromJsonBody(key, jsonKey, "double", fallback),
678
- toJsonBody: defaultToJsonBody
679
- },
680
- "List<String>": {
681
- fromJsonBody: (key, jsonKey, fallback) => defaultListFromJsonBody(key, jsonKey, "String", fallback),
682
- toJsonBody: defaultToJsonBody
683
- },
684
- "List<bool>": {
685
- fromJsonBody: (key, jsonKey, fallback) => defaultListFromJsonBody(key, jsonKey, "bool", fallback),
686
- toJsonBody: defaultToJsonBody
687
- },
688
- "List<DateTime>": {
689
- fromJsonBody: (key, jsonKey, fallback) => `${key}: json["${jsonKey}"] is List<String> ?
690
- (json["${jsonKey}"] as List<String>)
691
- .map((item) => DateTime.parse(item)).toList() : ${fallback}`,
692
- toJsonBody: (key, jsonKey, isNullable) => `"${jsonKey}": ${key}${isNullable ? "?" : ""}.map((val) => val.toIso8601String()).toList()`
693
- },
694
- Uint8List: {
695
- fromJsonBody: (key, jsonKey, fallback) => `${key}: json["${jsonKey}"] is List ? Uint8List.fromList(json["${jsonKey}"]) : ${fallback}`,
696
- toJsonBody: (key, jsonKey, isNullable) => `"${jsonKey}": ${key}${isNullable ? "?" : ""}.toList()`
697
- },
698
- "List<Uint8List>": {
699
- fromJsonBody: (key, jsonKey, fallback) => `${key}: json["${jsonKey}"] is List && (json["${jsonKey}"] as List).isNotEmpty && (json["${jsonKey}"] as List).first is List ? json["${jsonKey}"] : ${fallback}`,
700
- toJsonBody: (key, jsonKey, isNullable) => `"${jsonKey}": ${key}${isNullable ? "?" : ""}.map((val) => val.toList()).toList()`
701
- }
702
- };
703
- function defaultFromJsonBody(key, jsonKey, typename, fallback) {
704
- if (fallback === "null") {
705
- return `${key}: nullableTypeFromDynamic<${typename}>(json["${jsonKey}"])`;
706
- }
707
- return `${key}: typeFromDynamic<${typename}>(json["${jsonKey}"], ${fallback})`;
708
- }
709
- function defaultListFromJsonBody(key, jsonKey, typename, fallback) {
710
- return `${key}: json["${jsonKey}"] is List<${typename}> ? json["${jsonKey}"] : ${fallback}`;
711
- }
712
- function defaultToJsonBody(key, jsonKey) {
713
- return `"${jsonKey}": ${key}`;
714
- }
715
- const classTransformer = (typename, nullable) => {
716
- return {
717
- fromJsonBody: (key, jsonKey, fallback) => {
718
- if (nullable) {
719
- return `${key}: json["${jsonKey}"] is Map<String, dynamic> ?
720
- ${typename}.fromJson(json["${jsonKey}"]) : null`;
721
- }
722
- return `${key}: ${typename}.fromJson(json["${jsonKey}"])`;
723
- },
724
- toJsonBody: (key, jsonKey, isNullable) => {
725
- return `"${jsonKey}": ${key}${isNullable ? "?" : ""}.toJson()`;
726
- }
727
- };
728
- };
729
- const classListTransformer = (typename) => {
730
- return {
731
- fromJsonBody: (key, jsonKey, fallback) => {
732
- return `${key}: json["${jsonKey}"] is List ?
733
- (json["${jsonKey}"] as List<dynamic>)
734
- .map((val) => ${typename}.fromJson(val)).toList() : ${fallback}`;
735
- },
736
- toJsonBody: (key, jsonKey, isNullable) => `"${jsonKey}": ${key}${isNullable ? "?" : ""}.map((val) => val.toJson()).toList()`
737
- };
738
- };
739
- function isDartType(input) {
740
- if (typeof input !== "string") {
741
- return false;
742
- }
743
- for (const val of Object.values(dartTypeMap)) {
744
- if (input === val) {
745
- return true;
746
- }
747
- if (input.replace("List<", "").replace(">", "") === val) {
748
- return true;
749
- }
750
- }
751
- return false;
752
- }
753
- function isDartTypeWithNullables(input) {
754
- if (typeof input !== "string") {
755
- return false;
756
- }
757
- for (const val of Object.values(dartTypeMap)) {
758
- if (input.replace("?", "") === val) {
759
- return true;
760
- }
761
- if (input.replace("?", "").replace("List<", "").replace(">", "") === val) {
762
- return true;
763
- }
764
- }
765
- return false;
766
- }
767
-
768
- exports.classListTransformer = classListTransformer;
769
- exports.classTransformer = classTransformer;
770
- exports.createDartClient = createDartClient;
771
- exports.dartClientGenerator = dartClientGenerator;
772
- exports.dartModelFromJsonSchema = dartModelFromJsonSchema;
773
- exports.dartParsedJsonField = dartParsedJsonField;
774
- exports.dartProcedureFromServiceDefinition = dartProcedureFromServiceDefinition;
775
- exports.dartPropertyTypeFromSchema = dartPropertyTypeFromSchema;
776
- exports.dartServiceFromServiceDefinition = dartServiceFromServiceDefinition;
777
- exports.defineClientGeneratorPlugin = defineClientGeneratorPlugin;
778
- exports.isDartType = isDartType;
779
- exports.isDartTypeWithNullables = isDartTypeWithNullables;
780
- exports.isJsonSchemaArray = isJsonSchemaArray;
781
- exports.isJsonSchemaEnum = isJsonSchemaEnum;
782
- exports.isJsonSchemaNullType = isJsonSchemaNullType;
783
- exports.isJsonSchemaObject = isJsonSchemaObject;
784
- exports.isJsonSchemaRecord = isJsonSchemaRecord;
785
- exports.isJsonSchemaScalarType = isJsonSchemaScalarType;
786
- exports.transformers = transformers;