@accelbyte/codegen 1.0.0-alpha.6 → 1.0.0-alpha.8

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.
@@ -53,86 +53,6 @@ class CliParser {
53
53
  };
54
54
  }
55
55
 
56
- const Schema = z.object({
57
- $ref: z.string().nullish(),
58
- type: z.union([
59
- z.literal("array"),
60
- z.literal("object"),
61
- z.literal("file"),
62
- z.literal("string"),
63
- z.literal("boolean")
64
- ]).nullish(),
65
- items: z.object({
66
- $ref: z.string().nullish(),
67
- type: z.string().nullish()
68
- }).nullish(),
69
- properties: z.array(z.string()).nullish(),
70
- description: z.string().nullish(),
71
- additionalProperties: z.object({
72
- type: z.string().nullish()
73
- }).nullish()
74
- });
75
- const Definition = z.object({
76
- required: z.array(z.string()).nullish(),
77
- properties: z.record(z.object({
78
- type: z.string()
79
- })).nullish()
80
- });
81
- const Definitions = z.record(Definition);
82
- const EndpointParametersType = z.enum(["apiKey", "boolean", "int", "integer", "number", "string", "array", "file"]);
83
- const EndpointParametersIn = z.enum(["body", "formData", "header", "path", "query"]);
84
- const EndpointParameters = z.object({
85
- type: EndpointParametersType.nullish(),
86
- description: z.string().nullish(),
87
- name: z.string(),
88
- in: EndpointParametersIn,
89
- required: z.boolean().nullish(),
90
- schema: Schema.nullish(),
91
- default: z.union([z.boolean(), z.string(), z.number()]).nullish(),
92
- enum: z.array(z.union([z.boolean(), z.string(), z.number()])).nullish(),
93
- items: z.object({
94
- type: z.string()
95
- }).nullish()
96
- });
97
- const Endpoint = z.object({
98
- description: z.string().nullish(),
99
- consumes: z.array(z.string()).nullish(),
100
- produces: z.array(z.string()).nullish(),
101
- tags: z.array(z.string()).nullish(),
102
- summary: z.string().nullish(),
103
- operationId: z.string(),
104
- deprecated: z.boolean().nullish(),
105
- responses: z.record(z.object({
106
- description: z.string().nullish(),
107
- schema: Schema.nullish()
108
- })),
109
- parameters: z.array(EndpointParameters).nullish()
110
- });
111
- const Operation = z.object({
112
- get: Endpoint.nullish(),
113
- post: Endpoint.nullish(),
114
- patch: Endpoint.nullish(),
115
- delete: Endpoint.nullish(),
116
- put: Endpoint.nullish()
117
- });
118
- const Paths = z.record(Operation);
119
- z.object({
120
- paths: Paths,
121
- definitions: Definitions,
122
- basePath: z.string(),
123
- info: z.object({
124
- description: z.string(),
125
- title: z.string(),
126
- contact: z.object({
127
- name: z.string(),
128
- url: z.string(),
129
- email: z.string()
130
- }),
131
- version: z.string()
132
- }),
133
- schemes: z.array(z.string()).nullish()
134
- });
135
-
136
56
  const getImportableVarMap = () => ({
137
57
  "@accelbyte/sdk": ["CodeGenUtil", "SdkCache", "IResponse", "IResponseWithSync", "Validate"],
138
58
  axios: ["AxiosRequestConfig", "AxiosResponse"],
@@ -184,10 +104,14 @@ class ParserUtils {
184
104
  return `${attrName}: ${defaultValue}`;
185
105
  };
186
106
  static parseType = (pathParam) => {
187
- if (pathParam.type === "int" || pathParam.type === "integer")
107
+ if (pathParam.type === "int" || pathParam.type === "integer" || pathParam?.schema?.type === "integer")
188
108
  return "number";
189
109
  if (pathParam.type === "array")
190
110
  return `${pathParam.items.type ?? "any"}[]`;
111
+ if (pathParam?.schema?.type === "array")
112
+ return `${pathParam.schema.items.type ?? "any"}[]`;
113
+ if (pathParam?.schema?.type)
114
+ return pathParam.schema.type;
191
115
  return pathParam.type;
192
116
  };
193
117
  static parseQueryParamsType = (queryParams) => {
@@ -240,12 +164,21 @@ class ParserUtils {
240
164
  if (definition.type && ParserUtils.parseType(definition) === "number") {
241
165
  return `${attrName}${required}: number`;
242
166
  }
167
+ if (definition?.schema?.type && ParserUtils.parseType(definition) === "number") {
168
+ return `${attrName}${required}: number`;
169
+ }
243
170
  if (definition.type && definition.type === "array") {
244
171
  return `${attrName}${required}: ${definition.items.type ?? "any"}[]`;
245
172
  }
173
+ if (definition?.schema?.type && definition.schema.type === "array") {
174
+ return `${attrName}${required}: ${definition.schema.items.type ?? "any"}[]`;
175
+ }
246
176
  if (definition.type && definition.type) {
247
177
  return `${attrName}${required}: ${definition.type} | null`;
248
178
  }
179
+ if (definition?.schema?.type && definition.schema.type) {
180
+ return `${attrName}${required}: ${definition.schema.type} | null`;
181
+ }
249
182
  return `${attrName}${required}: any`;
250
183
  };
251
184
  static parseBodyParamsType = (bodyParams) => {
@@ -277,11 +210,14 @@ class ParserUtils {
277
210
  keys.forEach((key) => {
278
211
  if (String(key).startsWith("2")) {
279
212
  const sch = methodEntity[key].schema;
213
+ const schV3 = methodEntity[key].content && methodEntity[key].content["application/json"].schema;
280
214
  if (sch?.$ref) {
281
215
  responseClass = ParserUtils.parseRefType(sch.$ref);
282
216
  } else if (sch?.type === "array" && sch.items?.$ref) {
283
217
  responseClass = ParserUtils.parseRefType(sch.items.$ref);
284
218
  responseClass = `${responseClass}Array`;
219
+ } else if (schV3?.$ref) {
220
+ responseClass = ParserUtils.parseRefType(schV3.$ref);
285
221
  } else ;
286
222
  }
287
223
  });
@@ -414,7 +350,104 @@ ${content}`;
414
350
  };
415
351
  }
416
352
 
417
- const templateJsdocMethod = ({ classMethod, httpMethod, path, pathParams, bodyParams, queryParams }) => {
353
+ const Schema = z.object({
354
+ $ref: z.string().nullish(),
355
+ type: z.union([z.literal("array"), z.literal("object"), z.literal("file"), z.literal("string"), z.literal("boolean"), z.literal("integer")]).nullish(),
356
+ items: z.object({
357
+ $ref: z.string().nullish(),
358
+ type: z.string().nullish()
359
+ }).nullish(),
360
+ properties: z.union([z.array(z.string()).nullish(), z.record(z.object({ type: z.string() })).nullish()]),
361
+ description: z.string().nullish(),
362
+ additionalProperties: z.object({
363
+ type: z.string().nullish()
364
+ }).nullish()
365
+ });
366
+ const Definition = z.object({
367
+ required: z.array(z.string()).nullish(),
368
+ properties: z.record(z.object({
369
+ type: z.string()
370
+ })).nullish()
371
+ });
372
+ const Definitions = z.record(Definition);
373
+ const EndpointParametersType = z.enum(["apiKey", "boolean", "int", "integer", "number", "string", "array", "file"]);
374
+ const EndpointParametersIn = z.enum(["body", "formData", "header", "path", "query"]);
375
+ const EndpointParameters = z.object({
376
+ type: EndpointParametersType.nullish(),
377
+ description: z.string().nullish(),
378
+ name: z.string(),
379
+ in: EndpointParametersIn,
380
+ required: z.boolean().nullish(),
381
+ schema: Schema.nullish(),
382
+ default: z.union([z.boolean(), z.string(), z.number()]).nullish(),
383
+ enum: z.array(z.union([z.boolean(), z.string(), z.number()])).nullish(),
384
+ items: z.object({
385
+ type: z.string()
386
+ }).nullish()
387
+ });
388
+ const Endpoint = z.object({
389
+ description: z.string().nullish(),
390
+ consumes: z.array(z.string()).nullish(),
391
+ produces: z.array(z.string()).nullish(),
392
+ tags: z.array(z.string()).nullish(),
393
+ summary: z.string().nullish(),
394
+ operationId: z.string(),
395
+ deprecated: z.boolean().nullish(),
396
+ responses: z.record(z.object({
397
+ description: z.string().nullish(),
398
+ schema: Schema.nullish(),
399
+ content: z.object({
400
+ "application/json": z.object({
401
+ schema: Schema.nullish()
402
+ })
403
+ }).nullish()
404
+ })),
405
+ parameters: z.array(EndpointParameters).nullish(),
406
+ requestBody: z.object({
407
+ required: z.boolean(),
408
+ content: z.object({
409
+ "application/json": z.object({
410
+ schema: Schema.nullish()
411
+ })
412
+ }).nullish()
413
+ }).nullish()
414
+ });
415
+ const Operation = z.object({
416
+ get: Endpoint.nullish(),
417
+ post: Endpoint.nullish(),
418
+ patch: Endpoint.nullish(),
419
+ delete: Endpoint.nullish(),
420
+ put: Endpoint.nullish()
421
+ });
422
+ const Paths = z.record(Operation);
423
+ z.object({
424
+ paths: Paths,
425
+ definitions: Definitions,
426
+ basePath: z.string(),
427
+ info: z.object({
428
+ description: z.string(),
429
+ title: z.string(),
430
+ contact: z.object({
431
+ name: z.string(),
432
+ url: z.string(),
433
+ email: z.string()
434
+ }),
435
+ version: z.string()
436
+ }),
437
+ schemes: z.array(z.string()).nullish(),
438
+ components: z.object({
439
+ schemas: Definitions
440
+ }).nullish()
441
+ });
442
+
443
+ const templateJsdocMethod = ({
444
+ classMethod,
445
+ httpMethod,
446
+ path,
447
+ pathParams,
448
+ bodyParams,
449
+ queryParams
450
+ }) => {
418
451
  let jsdoc = "";
419
452
  let methodSignature = "";
420
453
  let newPath = path;
@@ -594,13 +627,17 @@ class TemplateZod {
594
627
  if (definition.additionalProperties) {
595
628
  return this.parseToZodAttribute("", definition, []);
596
629
  }
597
- if (!definition.properties) {
630
+ let properties;
631
+ if (definition.properties) {
632
+ properties = Object.entries(definition.properties);
633
+ } else if (definition.items?.properties) {
634
+ properties = Object.entries(definition.items.properties);
635
+ } else {
598
636
  return {
599
637
  schemaString: "z.any()",
600
638
  typeString: "any"
601
639
  };
602
640
  }
603
- const properties = Object.entries(definition.properties);
604
641
  const schemaFields = [];
605
642
  const typeFields = [];
606
643
  for (const property of properties) {
@@ -609,6 +646,12 @@ class TemplateZod {
609
646
  schemaFields.push(result.schemaString);
610
647
  typeFields.push(result.typeString);
611
648
  }
649
+ if (definition?.type === "array") {
650
+ return {
651
+ schemaString: `z.array(z.object({${schemaFields.join(",")}}))`,
652
+ typeString: typeFields.join(";")
653
+ };
654
+ }
612
655
  return {
613
656
  schemaString: `z.object({${schemaFields.join(",")}})`,
614
657
  typeString: typeFields.join(";")
@@ -650,7 +693,8 @@ class TemplateZod {
650
693
  };
651
694
  }
652
695
  if (type === "array") {
653
- const ref2 = definition.items?.$ref;
696
+ const items = definition.items;
697
+ const ref2 = items?.$ref;
654
698
  let model2;
655
699
  if (ref2) {
656
700
  const refType = ParserUtils.parseRefType(ref2);
@@ -659,9 +703,13 @@ class TemplateZod {
659
703
  schemaString: refType,
660
704
  typeString: refType
661
705
  };
662
- } else {
663
- const items = definition.items;
706
+ } else if (items) {
664
707
  model2 = this.parseEnumItems(items);
708
+ } else {
709
+ return {
710
+ schemaString: `${schemaAttribute} z.array(z.any())${schemaRequired}`,
711
+ typeString: `${typeAttribute} any[]${typeNullishability}`
712
+ };
665
713
  }
666
714
  return {
667
715
  schemaString: `${schemaAttribute} z.array(${model2.schemaString})${schemaRequired}`,
@@ -811,13 +859,22 @@ class CodeGenerator {
811
859
  const isFormUrlEncoded = ParserUtils.isFormUrlEncoded(httpMethod, endpoint.consumes);
812
860
  const queryParams = ParserUtils.filterQueryParameters(endpoint.parameters);
813
861
  const pathParams = ParserUtils.filterPathParams(endpoint.parameters);
814
- const bodyParams = ParserUtils.filterBodyParams(endpoint.parameters);
862
+ let bodyParams = ParserUtils.filterBodyParams(endpoint.parameters);
815
863
  const classMethod = ParserUtils.generateClassMethod({
816
864
  path: path2,
817
865
  endpoint,
818
866
  httpMethod,
819
867
  className
820
868
  });
869
+ if (endpoint.requestBody) {
870
+ bodyParams = [
871
+ {
872
+ name: "body",
873
+ in: "body",
874
+ schema: endpoint.requestBody.content["application/json"].schema
875
+ }
876
+ ];
877
+ }
821
878
  const pathWithBase = `${api.basePath ?? ""}${path2}`;
822
879
  const [generatedMethodString, importStatements] = templateMethod({
823
880
  classMethod,
@@ -868,8 +925,9 @@ class CodeGenerator {
868
925
  indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path__default.join(DIST_DIR, `${classGenName}`), targetSrcFolder));
869
926
  }
870
927
  const duplicates = /* @__PURE__ */ new Map();
871
- for (const ref in api.definitions) {
872
- const definition = api.definitions[ref];
928
+ const definitions = api?.components?.schemas || api.definitions;
929
+ for (const ref in definitions) {
930
+ const definition = definitions[ref];
873
931
  let fileName = ParserUtils.parseRefType(ref);
874
932
  const fileExist = fs__default.existsSync(path__default.join(DIST_DEFINITION_DIR, `${fileName}.ts`));
875
933
  if (fileExist) {
@@ -880,8 +938,8 @@ class CodeGenerator {
880
938
  ParserUtils.writeDefinitionFile(DIST_DEFINITION_DIR, fileName, buffer);
881
939
  indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path__default.join(DIST_DEFINITION_DIR, fileName), targetSrcFolder));
882
940
  }
883
- for (const ref in api.definitions) {
884
- const definition = api.definitions[ref];
941
+ for (const ref in definitions) {
942
+ const definition = definitions[ref];
885
943
  const fileName = ParserUtils.parseRefType(ref);
886
944
  const { buffer, duplicateFound } = new TemplateZod().render(fileName, definition, duplicates);
887
945
  if (duplicateFound) {