react-query-lightbase-codegen 1.4.0 → 1.4.1

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/utils.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ReferenceObject, RequestBodyObject, ResponseObject, SchemaObject } from 'openapi3-ts';
1
+ import type { ReferenceObject, RequestBodyObject, ResponseObject, SchemaObject } from "openapi3-ts";
2
2
  export declare const getDocs: (data: ReferenceObject | {
3
3
  description?: string;
4
4
  }) => string;
@@ -21,4 +21,7 @@ export declare const formatDescription: (description?: string) => string;
21
21
  /**
22
22
  * Extract responses / request types from open-api specs
23
23
  */
24
- export declare const getResReqTypes: (responsesOrRequests: Array<[string, ResponseObject | ReferenceObject | RequestBodyObject]>) => string;
24
+ export declare const getResReqTypes: (responsesOrRequests: Array<[
25
+ string,
26
+ ResponseObject | ReferenceObject | RequestBodyObject
27
+ ]>) => string;
package/dist/utils.js CHANGED
@@ -1,8 +1,9 @@
1
- import lodash from 'lodash';
1
+ import lodash from "lodash";
2
2
  const { uniq, isEmpty } = lodash;
3
- import pasCase from 'case';
3
+ import pasCase from "case";
4
+ import chalk from "chalk";
4
5
  const { pascal } = pasCase;
5
- export const getDocs = (data) => isReference(data) ? '' : formatDescription(data.description);
6
+ export const getDocs = (data) => isReference(data) ? "" : formatDescription(data.description);
6
7
  /**
7
8
  * Discriminator helper for `ReferenceObject`
8
9
  */
@@ -13,18 +14,19 @@ export const isReference = (property) => {
13
14
  * Return the typescript equivalent of open-api data type
14
15
  */
15
16
  export const getScalar = (item) => {
16
- const nullable = item.nullable ? ' | null' : '';
17
+ const nullable = item.nullable ? " | null" : "";
17
18
  switch (item.type) {
18
- case 'number':
19
- case 'integer':
20
- return 'number' + nullable;
21
- case 'boolean':
22
- return 'boolean' + nullable;
23
- case 'array':
19
+ case "number":
20
+ case "integer":
21
+ return "number" + nullable;
22
+ case "boolean":
23
+ return "boolean" + nullable;
24
+ case "array":
24
25
  return getArray(item) + nullable;
25
- case 'string':
26
- return (item.enum ? `"${item.enum.join(`" | "`)}"` : 'string') + nullable;
27
- case 'object':
26
+ case "string":
27
+ return (item.enum ? `"${item.enum.join(`" | "`)}"` : "string") + nullable;
28
+ case "object":
29
+ return getObject(item) + nullable;
28
30
  default:
29
31
  return getObject(item) + nullable;
30
32
  }
@@ -33,20 +35,20 @@ export const getScalar = (item) => {
33
35
  * Return the output type from the $ref
34
36
  */
35
37
  const getRef = ($ref) => {
36
- if ($ref.startsWith('#/components/schemas')) {
37
- return pascal($ref.replace('#/components/schemas/', ''));
38
+ if ($ref.startsWith("#/components/schemas")) {
39
+ return pascal($ref.replace("#/components/schemas/", ""));
38
40
  }
39
- else if ($ref.startsWith('#/components/responses')) {
40
- return pascal($ref.replace('#/components/responses/', '')) + 'Response';
41
+ else if ($ref.startsWith("#/components/responses")) {
42
+ return pascal($ref.replace("#/components/responses/", "")) + "Response";
41
43
  }
42
- else if ($ref.startsWith('#/components/parameters')) {
43
- return pascal($ref.replace('#/components/parameters/', '')) + 'Parameter';
44
+ else if ($ref.startsWith("#/components/parameters")) {
45
+ return pascal($ref.replace("#/components/parameters/", "")) + "Parameter";
44
46
  }
45
- else if ($ref.startsWith('#/components/requestBodies')) {
46
- return pascal($ref.replace('#/components/requestBodies/', '')) + 'RequestBody';
47
+ else if ($ref.startsWith("#/components/requestBodies")) {
48
+ return (pascal($ref.replace("#/components/requestBodies/", "")) + "RequestBody");
47
49
  }
48
50
  else {
49
- throw new Error('This library only resolve $ref that are include into `#/components/*` for now');
51
+ throw new Error("This library only resolve $ref that are include into `#/components/*` for now");
50
52
  }
51
53
  };
52
54
  /**
@@ -54,16 +56,15 @@ const getRef = ($ref) => {
54
56
  */
55
57
  const getArray = (item) => {
56
58
  if (item.items) {
57
- if (!isReference(item.items) && (item.items.oneOf || item.items.allOf || item.items.enum)) {
59
+ if (!isReference(item.items) &&
60
+ (item.items.oneOf || item.items.allOf || item.items.enum)) {
58
61
  return `(${resolveValue(item.items)})[]`;
59
62
  }
60
63
  else {
61
64
  return `${resolveValue(item.items)}[]`;
62
65
  }
63
66
  }
64
- else {
65
- throw new Error('All arrays must have an `items` key define');
66
- }
67
+ console.log(chalk.red("Skipped: All arrays must have an `items` key define"));
67
68
  };
68
69
  /**
69
70
  * Return the output type from an object
@@ -73,46 +74,50 @@ const getObject = (item) => {
73
74
  return getRef(item.$ref);
74
75
  }
75
76
  if (item.allOf) {
76
- return item.allOf.map(resolveValue).join(' & ');
77
+ return item.allOf.map(resolveValue).join(" & ");
77
78
  }
78
79
  if (item.oneOf) {
79
- return item.oneOf.map(resolveValue).join(' | ');
80
+ return item.oneOf.map(resolveValue).join(" | ");
80
81
  }
81
82
  if (!item.type && !item.properties && !item.additionalProperties) {
82
- return '{}';
83
+ return "{}";
83
84
  }
84
85
  // Free form object (https://swagger.io/docs/specification/data-models/data-types/#free-form)
85
- if (item.type === 'object' &&
86
+ if (item.type === "object" &&
86
87
  !item.properties &&
87
- (!item.additionalProperties || item.additionalProperties === true || isEmpty(item.additionalProperties))) {
88
- return '{[key: string]: any}';
88
+ (!item.additionalProperties ||
89
+ item.additionalProperties === true ||
90
+ isEmpty(item.additionalProperties))) {
91
+ return "{[key: string]: any}";
89
92
  }
90
93
  const IdentifierRegexp = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
91
94
  // Consolidation of item.properties & item.additionalProperties
92
- let output = '{';
95
+ let output = "{";
93
96
  if (item.properties) {
94
97
  output += Object.entries(item.properties)
95
98
  .map(([key, prop]) => {
96
99
  const doc = getDocs(prop);
97
100
  const isRequired = (item.required || []).includes(key);
98
101
  const processedKey = IdentifierRegexp.test(key) ? key : `"${key}"`;
99
- return `${doc}\n${processedKey}${isRequired ? '' : '?'}: ${resolveValue(prop)};`;
102
+ return `${doc}\n${processedKey}${isRequired ? "" : "?"}: ${resolveValue(prop)};`;
100
103
  })
101
- .join('');
104
+ .join("");
102
105
  }
103
106
  if (item.additionalProperties) {
104
107
  if (item.properties) {
105
- output += '\n';
108
+ output += "\n";
106
109
  }
107
- output += `} & { [key: string]: ${item.additionalProperties === true ? 'any' : resolveValue(item.additionalProperties)}`;
110
+ output += `} & { [key: string]: ${item.additionalProperties === true
111
+ ? "any"
112
+ : resolveValue(item.additionalProperties)}`;
108
113
  }
109
114
  if (item.properties || item.additionalProperties) {
110
- if (output === '{\n') {
111
- return '{}';
115
+ if (output === "{\n") {
116
+ return "{}";
112
117
  }
113
- return output + '\n}';
118
+ return output + "\n}";
114
119
  }
115
- return item.type === 'object' ? '{[key: string]: any}' : 'any';
120
+ return item.type === "object" ? "{[key: string]: any}" : "any";
116
121
  };
117
122
  /**
118
123
  * Resolve the value of a schema object to a proper type definition.
@@ -123,12 +128,12 @@ export const resolveValue = (schema) => isReference(schema) ? getRef(schema.$ref
123
128
  */
124
129
  export const formatDescription = (description) => {
125
130
  if (!description) {
126
- return '';
131
+ return "";
127
132
  }
128
133
  return `\n/**\n${description
129
- .split('\n')
134
+ .split("\n")
130
135
  .map((i) => `* ${i}`)
131
- .join('\n')}\n */`;
136
+ .join("\n")}\n */`;
132
137
  };
133
138
  /**
134
139
  * Extract responses / request types from open-api specs
@@ -142,9 +147,9 @@ export const getResReqTypes = (responsesOrRequests) => {
142
147
  return getRef(res.$ref);
143
148
  }
144
149
  if (res.content) {
145
- for (let contentType of Object.keys(res.content)) {
146
- if (contentType.startsWith('application/json') ||
147
- contentType.startsWith('application/octet-stream')) {
150
+ for (const contentType of Object.keys(res.content)) {
151
+ if (contentType.startsWith("application/json") ||
152
+ contentType.startsWith("application/octet-stream")) {
148
153
  const schema = res.content[contentType].schema;
149
154
  return resolveValue(schema);
150
155
  }
@@ -152,6 +157,6 @@ export const getResReqTypes = (responsesOrRequests) => {
152
157
  return;
153
158
  }
154
159
  return;
155
- })).join(' | ');
160
+ })).join(" | ");
156
161
  };
157
162
  //# sourceMappingURL=utils.js.map
package/dist/utils.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","s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AC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;gBACpD,IACC,WAAW,CAAC,UAAU,CAAC,kBAAkB,CAAC;oBAC1C,WAAW,CAAC,UAAU,CAAC,0BAA0B,CAAC,EACjD,CAAC;oBACF,MAAM,MAAM,GAAG,GAAG,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,MAAO,CAAC;oBAEhD,OAAO,YAAY,CAAC,MAAM,CAAC,CAAC;gBAC7B,CAAC;YACF,CAAC;YACD,OAAO;QACR,CAAC;QAED,OAAO;IACR,CAAC,CAAC,CACF,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACf,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "react-query-lightbase-codegen",
3
3
  "description": "Fully typed react query code generation tool based on openApi specifications",
4
- "version": "1.4.0",
4
+ "version": "1.4.1",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "exports": "./dist/index.js",
package/src/utils.ts CHANGED
@@ -1,190 +1,212 @@
1
- import lodash from 'lodash';
1
+ import lodash from "lodash";
2
2
  const { uniq, isEmpty } = lodash;
3
- import pasCase from 'case';
3
+ import pasCase from "case";
4
+ import chalk from "chalk";
4
5
 
5
6
  const { pascal } = pasCase;
6
7
 
7
- import { ReferenceObject, RequestBodyObject, ResponseObject, SchemaObject } from 'openapi3-ts';
8
+ import type {
9
+ ReferenceObject,
10
+ RequestBodyObject,
11
+ ResponseObject,
12
+ SchemaObject,
13
+ } from "openapi3-ts";
8
14
 
9
15
  export const getDocs = (data: ReferenceObject | { description?: string }) =>
10
- isReference(data) ? '' : formatDescription(data.description);
16
+ isReference(data) ? "" : formatDescription(data.description);
11
17
  /**
12
18
  * Discriminator helper for `ReferenceObject`
13
19
  */
14
20
  export const isReference = (property: any): property is ReferenceObject => {
15
- return Boolean(property.$ref);
21
+ return Boolean(property.$ref);
16
22
  };
17
23
 
18
24
  /**
19
25
  * Return the typescript equivalent of open-api data type
20
26
  */
21
27
  export const getScalar = (item: SchemaObject) => {
22
- const nullable = item.nullable ? ' | null' : '';
28
+ const nullable = item.nullable ? " | null" : "";
23
29
 
24
- switch (item.type) {
25
- case 'number':
26
- case 'integer':
27
- return 'number' + nullable;
30
+ switch (item.type) {
31
+ case "number":
32
+ case "integer":
33
+ return "number" + nullable;
28
34
 
29
- case 'boolean':
30
- return 'boolean' + nullable;
35
+ case "boolean":
36
+ return "boolean" + nullable;
31
37
 
32
- case 'array':
33
- return getArray(item) + nullable;
38
+ case "array":
39
+ return getArray(item) + nullable;
34
40
 
35
- case 'string':
36
- return (item.enum ? `"${item.enum.join(`" | "`)}"` : 'string') + nullable;
41
+ case "string":
42
+ return (item.enum ? `"${item.enum.join(`" | "`)}"` : "string") + nullable;
37
43
 
38
- case 'object':
39
- default:
40
- return getObject(item) + nullable;
41
- }
44
+ case "object":
45
+ return getObject(item) + nullable;
46
+
47
+ default:
48
+ return getObject(item) + nullable;
49
+ }
42
50
  };
43
51
 
44
52
  /**
45
53
  * Return the output type from the $ref
46
54
  */
47
- const getRef = ($ref: ReferenceObject['$ref']) => {
48
- if ($ref.startsWith('#/components/schemas')) {
49
- return pascal($ref.replace('#/components/schemas/', ''));
50
- } else if ($ref.startsWith('#/components/responses')) {
51
- return pascal($ref.replace('#/components/responses/', '')) + 'Response';
52
- } else if ($ref.startsWith('#/components/parameters')) {
53
- return pascal($ref.replace('#/components/parameters/', '')) + 'Parameter';
54
- } else if ($ref.startsWith('#/components/requestBodies')) {
55
- return pascal($ref.replace('#/components/requestBodies/', '')) + 'RequestBody';
56
- } else {
57
- throw new Error('This library only resolve $ref that are include into `#/components/*` for now');
58
- }
55
+ const getRef = ($ref: ReferenceObject["$ref"]) => {
56
+ if ($ref.startsWith("#/components/schemas")) {
57
+ return pascal($ref.replace("#/components/schemas/", ""));
58
+ } else if ($ref.startsWith("#/components/responses")) {
59
+ return pascal($ref.replace("#/components/responses/", "")) + "Response";
60
+ } else if ($ref.startsWith("#/components/parameters")) {
61
+ return pascal($ref.replace("#/components/parameters/", "")) + "Parameter";
62
+ } else if ($ref.startsWith("#/components/requestBodies")) {
63
+ return (
64
+ pascal($ref.replace("#/components/requestBodies/", "")) + "RequestBody"
65
+ );
66
+ } else {
67
+ throw new Error(
68
+ "This library only resolve $ref that are include into `#/components/*` for now",
69
+ );
70
+ }
59
71
  };
60
72
 
61
73
  /**
62
74
  * Return the output type from an array
63
75
  */
64
- const getArray = (item: SchemaObject): string => {
65
- if (item.items) {
66
- if (!isReference(item.items) && (item.items.oneOf || item.items.allOf || item.items.enum)) {
67
- return `(${resolveValue(item.items)})[]`;
68
- } else {
69
- return `${resolveValue(item.items)}[]`;
70
- }
71
- } else {
72
- throw new Error('All arrays must have an `items` key define');
73
- }
76
+ const getArray = (item: SchemaObject): string | undefined => {
77
+ if (item.items) {
78
+ if (
79
+ !isReference(item.items) &&
80
+ (item.items.oneOf || item.items.allOf || item.items.enum)
81
+ ) {
82
+ return `(${resolveValue(item.items)})[]`;
83
+ } else {
84
+ return `${resolveValue(item.items)}[]`;
85
+ }
86
+ }
87
+ console.log(chalk.red("Skipped: All arrays must have an `items` key define"));
74
88
  };
75
89
 
76
90
  /**
77
91
  * Return the output type from an object
78
92
  */
79
93
  const getObject = (item: SchemaObject): string => {
80
- if (isReference(item)) {
81
- return getRef(item.$ref);
82
- }
83
-
84
- if (item.allOf) {
85
- return item.allOf.map(resolveValue).join(' & ');
86
- }
87
-
88
- if (item.oneOf) {
89
- return item.oneOf.map(resolveValue).join(' | ');
90
- }
91
-
92
- if (!item.type && !item.properties && !item.additionalProperties) {
93
- return '{}';
94
- }
95
-
96
- // Free form object (https://swagger.io/docs/specification/data-models/data-types/#free-form)
97
- if (
98
- item.type === 'object' &&
99
- !item.properties &&
100
- (!item.additionalProperties || item.additionalProperties === true || isEmpty(item.additionalProperties))
101
- ) {
102
- return '{[key: string]: any}';
103
- }
104
-
105
- const IdentifierRegexp = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
106
- // Consolidation of item.properties & item.additionalProperties
107
- let output = '{';
108
- if (item.properties) {
109
- output += Object.entries(item.properties)
110
- .map(([key, prop]: [string, ReferenceObject | SchemaObject]) => {
111
- const doc = getDocs(prop);
112
- const isRequired = (item.required || []).includes(key);
113
- const processedKey = IdentifierRegexp.test(key) ? key : `"${key}"`;
114
- return `${doc}\n${processedKey}${isRequired ? '' : '?'}: ${resolveValue(prop)};`;
115
- })
116
- .join('');
117
- }
118
-
119
- if (item.additionalProperties) {
120
- if (item.properties) {
121
- output += '\n';
122
- }
123
- output += `} & { [key: string]: ${
124
- item.additionalProperties === true ? 'any' : resolveValue(item.additionalProperties)
125
- }`;
126
- }
127
-
128
- if (item.properties || item.additionalProperties) {
129
- if (output === '{\n') {
130
- return '{}';
131
- }
132
- return output + '\n}';
133
- }
134
-
135
- return item.type === 'object' ? '{[key: string]: any}' : 'any';
94
+ if (isReference(item)) {
95
+ return getRef(item.$ref);
96
+ }
97
+
98
+ if (item.allOf) {
99
+ return item.allOf.map(resolveValue).join(" & ");
100
+ }
101
+
102
+ if (item.oneOf) {
103
+ return item.oneOf.map(resolveValue).join(" | ");
104
+ }
105
+
106
+ if (!item.type && !item.properties && !item.additionalProperties) {
107
+ return "{}";
108
+ }
109
+
110
+ // Free form object (https://swagger.io/docs/specification/data-models/data-types/#free-form)
111
+ if (
112
+ item.type === "object" &&
113
+ !item.properties &&
114
+ (!item.additionalProperties ||
115
+ item.additionalProperties === true ||
116
+ isEmpty(item.additionalProperties))
117
+ ) {
118
+ return "{[key: string]: any}";
119
+ }
120
+
121
+ const IdentifierRegexp = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
122
+ // Consolidation of item.properties & item.additionalProperties
123
+ let output = "{";
124
+ if (item.properties) {
125
+ output += Object.entries(item.properties)
126
+ .map(([key, prop]: [string, ReferenceObject | SchemaObject]) => {
127
+ const doc = getDocs(prop);
128
+ const isRequired = (item.required || []).includes(key);
129
+ const processedKey = IdentifierRegexp.test(key) ? key : `"${key}"`;
130
+ return `${doc}\n${processedKey}${isRequired ? "" : "?"}: ${resolveValue(
131
+ prop,
132
+ )};`;
133
+ })
134
+ .join("");
135
+ }
136
+
137
+ if (item.additionalProperties) {
138
+ if (item.properties) {
139
+ output += "\n";
140
+ }
141
+ output += `} & { [key: string]: ${
142
+ item.additionalProperties === true
143
+ ? "any"
144
+ : resolveValue(item.additionalProperties)
145
+ }`;
146
+ }
147
+
148
+ if (item.properties || item.additionalProperties) {
149
+ if (output === "{\n") {
150
+ return "{}";
151
+ }
152
+ return output + "\n}";
153
+ }
154
+
155
+ return item.type === "object" ? "{[key: string]: any}" : "any";
136
156
  };
137
157
 
138
158
  /**
139
159
  * Resolve the value of a schema object to a proper type definition.
140
160
  */
141
161
  export const resolveValue = (schema: SchemaObject) =>
142
- isReference(schema) ? getRef(schema.$ref) : getScalar(schema);
162
+ isReference(schema) ? getRef(schema.$ref) : getScalar(schema);
143
163
 
144
164
  /**
145
165
  * Format a description to code documentation.
146
166
  */
147
167
  export const formatDescription = (description?: string) => {
148
- if (!description) {
149
- return '';
150
- }
151
- return `\n/**\n${description
152
- .split('\n')
153
- .map((i) => `* ${i}`)
154
- .join('\n')}\n */`;
168
+ if (!description) {
169
+ return "";
170
+ }
171
+ return `\n/**\n${description
172
+ .split("\n")
173
+ .map((i) => `* ${i}`)
174
+ .join("\n")}\n */`;
155
175
  };
156
176
 
157
177
  /**
158
178
  * Extract responses / request types from open-api specs
159
179
  */
160
180
  export const getResReqTypes = (
161
- responsesOrRequests: Array<[string, ResponseObject | ReferenceObject | RequestBodyObject]>
181
+ responsesOrRequests: Array<
182
+ [string, ResponseObject | ReferenceObject | RequestBodyObject]
183
+ >,
162
184
  ) => {
163
- return uniq(
164
- responsesOrRequests.map(([_, res]) => {
165
- if (!res) {
166
- return;
167
- }
168
-
169
- if (isReference(res)) {
170
- return getRef(res.$ref);
171
- }
172
-
173
- if (res.content) {
174
- for (let contentType of Object.keys(res.content)) {
175
- if (
176
- contentType.startsWith('application/json') ||
177
- contentType.startsWith('application/octet-stream')
178
- ) {
179
- const schema = res.content[contentType].schema!;
180
-
181
- return resolveValue(schema);
182
- }
183
- }
184
- return;
185
- }
186
-
187
- return;
188
- })
189
- ).join(' | ');
185
+ return uniq(
186
+ responsesOrRequests.map(([_, res]) => {
187
+ if (!res) {
188
+ return;
189
+ }
190
+
191
+ if (isReference(res)) {
192
+ return getRef(res.$ref);
193
+ }
194
+
195
+ if (res.content) {
196
+ for (const contentType of Object.keys(res.content)) {
197
+ if (
198
+ contentType.startsWith("application/json") ||
199
+ contentType.startsWith("application/octet-stream")
200
+ ) {
201
+ const schema = res.content[contentType].schema!;
202
+
203
+ return resolveValue(schema);
204
+ }
205
+ }
206
+ return;
207
+ }
208
+
209
+ return;
210
+ }),
211
+ ).join(" | ");
190
212
  };