arri 0.0.37 → 0.0.40

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.
@@ -2,12 +2,84 @@
2
2
 
3
3
  const child_process = require('child_process');
4
4
  const require$$0 = require('fs');
5
- const app = require('./arri.8a065f92.cjs');
5
+ const utils = require('./arri.0752aa2b.cjs');
6
6
 
7
7
  function defineClientGeneratorPlugin(plugin) {
8
8
  return plugin;
9
9
  }
10
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
+
11
83
  const dartClientGenerator = defineClientGeneratorPlugin(
12
84
  (options) => {
13
85
  return {
@@ -49,11 +121,11 @@ function createDartClient(appDef, opts = { clientName: "Client" }) {
49
121
  const { models, procedures } = appDef;
50
122
  const serviceParts = [];
51
123
  const procedureParts = [];
52
- const services = app.unflattenProcedures(procedures);
124
+ const services = utils.unflattenProcedures(procedures);
53
125
  Object.keys(services).forEach((k) => {
54
126
  const item = services[k];
55
- if (app.isServiceDef(item)) {
56
- const serviceName = app.pascalCase(`${opts.clientName}_${k}_service`);
127
+ if (utils.isServiceDef(item)) {
128
+ const serviceName = utils.pascalCase(`${opts.clientName}_${k}_service`);
57
129
  serviceParts.push({
58
130
  name: serviceName,
59
131
  key: k,
@@ -74,11 +146,11 @@ function createDartClient(appDef, opts = { clientName: "Client" }) {
74
146
  const modelParts = [];
75
147
  Object.keys(models).forEach((key) => {
76
148
  const model = models[key];
77
- const typeName = app.pascalCase(key);
149
+ const typeName = utils.pascalCase(key);
78
150
  const objModel = model;
79
151
  if (!generatedModels.includes(typeName)) {
80
152
  modelParts.push(
81
- dartModelFromJsonSchema(`${app.pascalCase(key)}`, objModel)
153
+ dartModelFromJsonSchema(`${utils.pascalCase(key)}`, objModel)
82
154
  );
83
155
  generatedModels.push(typeName);
84
156
  }
@@ -110,7 +182,7 @@ ${modelParts.join("\n")}
110
182
 
111
183
  enum ${opts.clientName}Endpoints implements Comparable<${opts.clientName}Endpoints>, ArriEndpoint {
112
184
  ${Object.keys(endpoints).map(
113
- (key) => `${app.camelCase(key.split("/").join("_"))}(
185
+ (key) => `${utils.camelCase(key.split("/").join("_"))}(
114
186
  path: "${key}",
115
187
  method: HttpMethod.${endpoints[key]},
116
188
  )`
@@ -134,13 +206,13 @@ function dartServiceFromServiceDefinition(name, def, opts) {
134
206
  const serviceName = `${name}`;
135
207
  Object.keys(def).forEach((key) => {
136
208
  const item = def[key];
137
- if (app.isProcedureDef(item)) {
209
+ if (utils.isProcedureDef(item)) {
138
210
  rpcParts.push(dartProcedureFromServiceDefinition(key, item));
139
211
  return;
140
212
  }
141
213
  const nameParts = name.split("Service");
142
214
  nameParts.pop();
143
- const subServiceName = app.pascalCase(
215
+ const subServiceName = utils.pascalCase(
144
216
  `${nameParts.join("")}_${key}_Service`
145
217
  );
146
218
  const subService = dartServiceFromServiceDefinition(
@@ -227,7 +299,7 @@ function dartProcedureFromServiceDefinition(key, def, opts) {
227
299
  }`;
228
300
  }
229
301
  function dartModelFromJsonSchema(modelName, schema, opts) {
230
- const modelDisplayName = app.pascalCase(`${modelName}`);
302
+ const modelDisplayName = utils.pascalCase(`${modelName}`);
231
303
  const fields = [];
232
304
  const subModelParts = [];
233
305
  Object.entries(schema.properties ?? {}).forEach(([key, val]) => {
@@ -243,12 +315,12 @@ function dartModelFromJsonSchema(modelName, schema, opts) {
243
315
  subModelParts.push(sub);
244
316
  }
245
317
  }
246
- fields.push({ type: dartType, name: app.camelCase(key), jsonKey: key });
247
- if (!isDartType(dartType) && app.isJsonSchemaObject(val)) {
318
+ fields.push({ type: dartType, name: utils.camelCase(key), jsonKey: key });
319
+ if (!isDartType(dartType) && isJsonSchemaObject(val)) {
248
320
  if (val.$id && generatedModels.includes(val.$id)) {
249
321
  return;
250
322
  }
251
- const subModelName = val.$id ? app.pascalCase(val.$id) : app.pascalCase(dartType.replace("?", ""));
323
+ const subModelName = val.$id ? utils.pascalCase(val.$id) : utils.pascalCase(dartType.replace("?", ""));
252
324
  subModelParts.push(
253
325
  dartModelFromJsonSchema(subModelName, val)
254
326
  );
@@ -320,21 +392,21 @@ function dartPropertyTypeFromSchema(objectName, propertyName, schema, opts) {
320
392
  const isOptional = !schema.required?.includes(propertyName);
321
393
  let finalType = "";
322
394
  const subTypes = [];
323
- if (app.isJsonSchemaEnum(prop)) {
324
- const enumName = app.pascalCase(`${objectName}_${propertyName}`);
395
+ if (isJsonSchemaEnum(prop)) {
396
+ const enumName = utils.pascalCase(`${objectName}_${propertyName}`);
325
397
  finalType = isOptional ? `${enumName}?` : enumName;
326
398
  const options = [];
327
399
  for (const opt of prop.anyOf) {
328
400
  if (opt.type === "string") {
329
401
  options.push({
330
- name: app.camelCase(opt.const.toString()),
402
+ name: utils.camelCase(opt.const.toString()),
331
403
  type: "String",
332
404
  value: opt.const
333
405
  });
334
406
  }
335
407
  if (opt.type === "number" || opt.type === "integer") {
336
408
  options.push({
337
- name: app.camelCase(
409
+ name: utils.camelCase(
338
410
  `num_${opt.const}`.split(".").join("Point")
339
411
  ),
340
412
  type: opt.type === "number" ? "double" : "int",
@@ -367,11 +439,11 @@ function dartPropertyTypeFromSchema(objectName, propertyName, schema, opts) {
367
439
  }`);
368
440
  return [finalType, subTypes];
369
441
  }
370
- if (app.isJsonSchemaNullType(prop)) {
442
+ if (isJsonSchemaNullType(prop)) {
371
443
  finalType = "null";
372
444
  return [finalType, subTypes];
373
445
  }
374
- if (app.isJsonSchemaScalarType(prop)) {
446
+ if (isJsonSchemaScalarType(prop)) {
375
447
  switch (prop.type) {
376
448
  case "string":
377
449
  finalType = isOptional ? "String?" : "String";
@@ -390,14 +462,14 @@ function dartPropertyTypeFromSchema(objectName, propertyName, schema, opts) {
390
462
  break;
391
463
  }
392
464
  }
393
- if (app.isJsonSchemaObject(prop)) {
394
- const joinedPropName = prop.$id ? app.pascalCase(prop.$id) : app.pascalCase(`${objectName}_${propertyName}`);
465
+ if (isJsonSchemaObject(prop)) {
466
+ const joinedPropName = prop.$id ? utils.pascalCase(prop.$id) : utils.pascalCase(`${objectName}_${propertyName}`);
395
467
  finalType = isOptional ? `${joinedPropName}?` : joinedPropName;
396
468
  }
397
- if (app.isJsonSchemaRecord(prop)) {
469
+ if (isJsonSchemaRecord(prop)) {
398
470
  finalType = isOptional ? `Map<dynamic, dynamic>?` : "Map<dynamic, dynamic>";
399
471
  }
400
- if (app.isJsonSchemaArray(prop)) {
472
+ if (isJsonSchemaArray(prop)) {
401
473
  const item = prop.items;
402
474
  const type = item.type;
403
475
  if (typeof type !== "string") {
@@ -425,8 +497,8 @@ function dartPropertyTypeFromSchema(objectName, propertyName, schema, opts) {
425
497
  finalType = isOptional ? "List<DateTime>?" : "List<DateTime>";
426
498
  break;
427
499
  case "object": {
428
- if (app.isJsonSchemaObject(item)) {
429
- const subTypeName = item.$id ?? app.pascalCase(`${objectName}_${propertyName}_item`);
500
+ if (isJsonSchemaObject(item)) {
501
+ const subTypeName = item.$id ?? utils.pascalCase(`${objectName}_${propertyName}_item`);
430
502
  if (!generatedModels.includes(subTypeName)) {
431
503
  const model = dartModelFromJsonSchema(
432
504
  subTypeName,
@@ -693,4 +765,10 @@ exports.dartServiceFromServiceDefinition = dartServiceFromServiceDefinition;
693
765
  exports.defineClientGeneratorPlugin = defineClientGeneratorPlugin;
694
766
  exports.isDartType = isDartType;
695
767
  exports.isDartTypeWithNullables = isDartTypeWithNullables;
768
+ exports.isJsonSchemaArray = isJsonSchemaArray;
769
+ exports.isJsonSchemaEnum = isJsonSchemaEnum;
770
+ exports.isJsonSchemaNullType = isJsonSchemaNullType;
771
+ exports.isJsonSchemaObject = isJsonSchemaObject;
772
+ exports.isJsonSchemaRecord = isJsonSchemaRecord;
773
+ exports.isJsonSchemaScalarType = isJsonSchemaScalarType;
696
774
  exports.transformers = transformers;
@@ -0,0 +1,179 @@
1
+ const NUMBER_CHAR_RE = /\d/;
2
+ const STR_SPLITTERS = ["-", "_", "/", "."];
3
+ function isUppercase(char = "") {
4
+ if (NUMBER_CHAR_RE.test(char)) {
5
+ return void 0;
6
+ }
7
+ return char.toUpperCase() === char;
8
+ }
9
+ function splitByCase(string_, separators) {
10
+ const splitters = separators ?? STR_SPLITTERS;
11
+ const parts = [];
12
+ if (!string_ || typeof string_ !== "string") {
13
+ return parts;
14
+ }
15
+ let buff = "";
16
+ let previousUpper;
17
+ let previousSplitter;
18
+ for (const char of string_) {
19
+ const isSplitter = splitters.includes(char);
20
+ if (isSplitter === true) {
21
+ parts.push(buff);
22
+ buff = "";
23
+ previousUpper = void 0;
24
+ continue;
25
+ }
26
+ const isUpper = isUppercase(char);
27
+ if (previousSplitter === false) {
28
+ if (previousUpper === false && isUpper === true) {
29
+ parts.push(buff);
30
+ buff = char;
31
+ previousUpper = isUpper;
32
+ continue;
33
+ }
34
+ if (previousUpper === true && isUpper === false && buff.length > 1) {
35
+ const lastChar = buff[buff.length - 1];
36
+ parts.push(buff.slice(0, Math.max(0, buff.length - 1)));
37
+ buff = lastChar + char;
38
+ previousUpper = isUpper;
39
+ continue;
40
+ }
41
+ }
42
+ buff += char;
43
+ previousUpper = isUpper;
44
+ previousSplitter = isSplitter;
45
+ }
46
+ parts.push(buff);
47
+ return parts;
48
+ }
49
+ function upperFirst(string_) {
50
+ return !string_ ? "" : string_[0].toUpperCase() + string_.slice(1);
51
+ }
52
+ function lowerFirst(string_) {
53
+ return !string_ ? "" : string_[0].toLowerCase() + string_.slice(1);
54
+ }
55
+ function pascalCase(string_) {
56
+ return !string_ ? "" : (Array.isArray(string_) ? string_ : splitByCase(string_)).map((p) => upperFirst(p)).join("");
57
+ }
58
+ function camelCase(string_) {
59
+ return lowerFirst(pascalCase(string_));
60
+ }
61
+ function kebabCase(string_, joiner) {
62
+ return !string_ ? "" : (Array.isArray(string_) ? string_ : splitByCase(string_)).map((p) => p.toLowerCase()).join(joiner ?? "-");
63
+ }
64
+
65
+ const HttpMethodValues = [
66
+ "get",
67
+ "post",
68
+ "put",
69
+ "patch",
70
+ "delete",
71
+ "head"
72
+ ];
73
+ const isHttpMethod = (input) => {
74
+ if (typeof input !== "string") {
75
+ return false;
76
+ }
77
+ return HttpMethodValues.includes(input);
78
+ };
79
+
80
+ function normalizeWhitespace(input) {
81
+ if (input.includes("\n\n")) {
82
+ return normalizeWhitespace(input.split("\n\n").join("\n"));
83
+ }
84
+ const lines = [];
85
+ for (const line of input.split("\n")) {
86
+ lines.push(line.trim());
87
+ }
88
+ const result = lines.join("\n").trim();
89
+ if (result.includes("\n\n")) {
90
+ return normalizeWhitespace(result.split("\n\n").join("\n"));
91
+ }
92
+ return result;
93
+ }
94
+ function isProcedureDef(input) {
95
+ if (typeof input !== "object") {
96
+ return false;
97
+ }
98
+ const expectedKeys = ["path", "method"];
99
+ const existingKeys = Object.keys(input);
100
+ for (const key of expectedKeys) {
101
+ if (!existingKeys.includes(key)) {
102
+ return false;
103
+ }
104
+ }
105
+ return isHttpMethod(input.method) && typeof input.path === "string" && (typeof input.params === "string" || typeof input.params === "undefined") && (typeof input.response === "string" || typeof input.response === "undefined");
106
+ }
107
+ function isServiceDef(input) {
108
+ if (typeof input !== "object") {
109
+ return false;
110
+ }
111
+ for (const key of Object.keys(input)) {
112
+ if (typeof input[key] !== "object") {
113
+ return false;
114
+ }
115
+ }
116
+ return true;
117
+ }
118
+ function isApplicationDef(input) {
119
+ if (typeof input !== "object") {
120
+ return false;
121
+ }
122
+ if (!("procedures" in input) || typeof input.procedures !== "object") {
123
+ return false;
124
+ }
125
+ if (!("models" in input) || typeof input.models !== "object") {
126
+ return false;
127
+ }
128
+ if (!("errors" in input) || typeof input.errors !== "object") {
129
+ return false;
130
+ }
131
+ return true;
132
+ }
133
+ function unflattenObject(data) {
134
+ if (Object(data) !== data || Array.isArray(data))
135
+ return data;
136
+ const regex = /\.?([^.[\]]+)|\[(\d+)\]/g;
137
+ const result = {};
138
+ for (const p in data) {
139
+ let cur = result;
140
+ let prop = "";
141
+ let m;
142
+ while (m = regex.exec(p)) {
143
+ cur = cur[prop] || (cur[prop] = m[2] ? [] : {});
144
+ prop = m[2] || m[1];
145
+ }
146
+ cur[prop] = data[p];
147
+ }
148
+ return result[""] || result;
149
+ }
150
+ function unflattenProcedures(procedures) {
151
+ return unflattenObject(procedures);
152
+ }
153
+ function setNestedObjectProperty(targetProp, value, object) {
154
+ const parts = targetProp.split(".");
155
+ let current = object;
156
+ for (let i = 0; i < parts.length; i++) {
157
+ const key = parts[i];
158
+ if (i === parts.length - 1) {
159
+ current[key] = value;
160
+ } else {
161
+ if (!current[key]) {
162
+ current[key] = {};
163
+ }
164
+ current = current[key];
165
+ }
166
+ }
167
+ return object;
168
+ }
169
+ const removeDisallowedChars = (input, disallowedChars) => {
170
+ let result = input;
171
+ for (const char of disallowedChars) {
172
+ if (result.includes(char)) {
173
+ result = result.split(char).join("");
174
+ }
175
+ }
176
+ return result;
177
+ };
178
+
179
+ export { isProcedureDef as a, unflattenProcedures as b, camelCase as c, isApplicationDef as d, isHttpMethod as e, isServiceDef as i, kebabCase as k, normalizeWhitespace as n, pascalCase as p, removeDisallowedChars as r, setNestedObjectProperty as s, unflattenObject as u };
package/package.json CHANGED
@@ -18,14 +18,14 @@
18
18
  "scule": "^1.0.0",
19
19
  "globby": "^13.2.2",
20
20
  "pathe": "^1.1.1",
21
- "citty": "^0.1.3",
21
+ "citty": "^0.1.4",
22
22
  "c12": "^1.4.2",
23
23
  "consola": "^3.2.3",
24
24
  "ofetch": "^1.3.3",
25
25
  "chokidar": "^3.5.3",
26
- "listhen": "^1.4.7"
26
+ "listhen": "^1.5.0"
27
27
  },
28
- "version": "0.0.37",
28
+ "version": "0.0.40",
29
29
  "repository": {
30
30
  "url": "https://github.com/modiimedia/arri"
31
31
  }