react-query-lightbase-codegen 0.2.0 → 0.5.0

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.
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.generateSchemasDefinition = void 0;
7
+ const case_1 = require("case");
8
+ const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
9
+ const getResReqTypes_1 = require("./getResReqTypes");
10
+ /**
11
+ * Generate the interface string
12
+ */
13
+ const generateInterface = (name, schema) => {
14
+ const scalar = (0, getResReqTypes_1.getScalar)(schema);
15
+ return `
16
+ ${(0, getResReqTypes_1.formatDescription)(schema.description)}
17
+ export type ${(0, case_1.pascal)(name)} = ${scalar}
18
+ `;
19
+ };
20
+ /**
21
+ * Extract all types from #/components/schemas
22
+ */
23
+ const generateSchemasDefinition = (schemas = {}) => {
24
+ if ((0, isEmpty_1.default)(schemas)) {
25
+ return '';
26
+ }
27
+ return ('\n // SCEHMAS' +
28
+ Object.entries(schemas)
29
+ .map(([name, schema]) => !(0, getResReqTypes_1.isReference)(schema) &&
30
+ (!schema.type || schema.type === 'object') &&
31
+ !schema.allOf &&
32
+ !schema.oneOf &&
33
+ !(0, getResReqTypes_1.isReference)(schema) &&
34
+ !schema.nullable
35
+ ? generateInterface(name, schema)
36
+ : `${(0, getResReqTypes_1.formatDescription)((0, getResReqTypes_1.isReference)(schema) ? undefined : schema.description)} type ${(0, case_1.pascal)(name)} = ${(0, getResReqTypes_1.resolveValue)(schema)};`)
37
+ .join('\n\n') +
38
+ '\n');
39
+ };
40
+ exports.generateSchemasDefinition = generateSchemasDefinition;
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getResReqTypes = exports.formatDescription = exports.resolveValue = exports.getScalar = exports.isReference = exports.getDocs = void 0;
7
+ const case_1 = require("case");
8
+ const lodash_1 = require("lodash");
9
+ const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
10
+ const getDocs = (data) => (0, exports.isReference)(data) ? '' : (0, exports.formatDescription)(data.description);
11
+ exports.getDocs = getDocs;
12
+ /**
13
+ * Discriminator helper for `ReferenceObject`
14
+ */
15
+ const isReference = (property) => {
16
+ return Boolean(property.$ref);
17
+ };
18
+ exports.isReference = isReference;
19
+ /**
20
+ * Return the typescript equivalent of open-api data type
21
+ */
22
+ const getScalar = (item) => {
23
+ const nullable = item.nullable ? ' | null' : '';
24
+ switch (item.type) {
25
+ case 'number':
26
+ case 'integer':
27
+ return 'number' + nullable;
28
+ case 'boolean':
29
+ return 'boolean' + nullable;
30
+ case 'array':
31
+ return getArray(item) + nullable;
32
+ case 'string':
33
+ return (item.enum ? `"${item.enum.join(`" | "`)}"` : 'string') + nullable;
34
+ case 'object':
35
+ default:
36
+ return getObject(item) + nullable;
37
+ }
38
+ };
39
+ exports.getScalar = getScalar;
40
+ /**
41
+ * Return the output type from the $ref
42
+ */
43
+ const getRef = ($ref) => {
44
+ if ($ref.startsWith('#/components/schemas')) {
45
+ return (0, case_1.pascal)($ref.replace('#/components/schemas/', ''));
46
+ }
47
+ else if ($ref.startsWith('#/components/responses')) {
48
+ return (0, case_1.pascal)($ref.replace('#/components/responses/', '')) + 'Response';
49
+ }
50
+ else if ($ref.startsWith('#/components/parameters')) {
51
+ return (0, case_1.pascal)($ref.replace('#/components/parameters/', '')) + 'Parameter';
52
+ }
53
+ else if ($ref.startsWith('#/components/requestBodies')) {
54
+ return (0, case_1.pascal)($ref.replace('#/components/requestBodies/', '')) + 'RequestBody';
55
+ }
56
+ else {
57
+ throw new Error('This library only resolve $ref that are include into `#/components/*` for now');
58
+ }
59
+ };
60
+ /**
61
+ * Return the output type from an array
62
+ */
63
+ const getArray = (item) => {
64
+ if (item.items) {
65
+ if (!(0, exports.isReference)(item.items) && (item.items.oneOf || item.items.allOf || item.items.enum)) {
66
+ return `(${(0, exports.resolveValue)(item.items)})[]`;
67
+ }
68
+ else {
69
+ return `${(0, exports.resolveValue)(item.items)}[]`;
70
+ }
71
+ }
72
+ else {
73
+ throw new Error('All arrays must have an `items` key define');
74
+ }
75
+ };
76
+ /**
77
+ * Return the output type from an object
78
+ */
79
+ const getObject = (item) => {
80
+ if ((0, exports.isReference)(item)) {
81
+ return getRef(item.$ref);
82
+ }
83
+ if (item.allOf) {
84
+ return item.allOf.map(exports.resolveValue).join(' & ');
85
+ }
86
+ if (item.oneOf) {
87
+ return item.oneOf.map(exports.resolveValue).join(' | ');
88
+ }
89
+ if (!item.type && !item.properties && !item.additionalProperties) {
90
+ return '{}';
91
+ }
92
+ // Free form object (https://swagger.io/docs/specification/data-models/data-types/#free-form)
93
+ if (item.type === 'object' &&
94
+ !item.properties &&
95
+ (!item.additionalProperties || item.additionalProperties === true || (0, isEmpty_1.default)(item.additionalProperties))) {
96
+ return '{[key: string]: any}';
97
+ }
98
+ const IdentifierRegexp = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
99
+ // Consolidation of item.properties & item.additionalProperties
100
+ let output = '{\n';
101
+ if (item.properties) {
102
+ output += Object.entries(item.properties)
103
+ .map(([key, prop]) => {
104
+ const doc = (0, exports.getDocs)(prop);
105
+ const isRequired = (item.required || []).includes(key);
106
+ const processedKey = IdentifierRegexp.test(key) ? key : `"${key}"`;
107
+ return `${doc}\n${processedKey}${isRequired ? '' : '?'}: ${(0, exports.resolveValue)(prop)};`;
108
+ })
109
+ .join('\n');
110
+ }
111
+ if (item.additionalProperties) {
112
+ if (item.properties) {
113
+ output += '\n';
114
+ }
115
+ output += `} & { [key: string]: ${item.additionalProperties === true ? 'any' : (0, exports.resolveValue)(item.additionalProperties)}`;
116
+ }
117
+ if (item.properties || item.additionalProperties) {
118
+ if (output === '{\n') {
119
+ return '{}';
120
+ }
121
+ return output + '\n}';
122
+ }
123
+ return item.type === 'object' ? '{[key: string]: any}' : 'any';
124
+ };
125
+ /**
126
+ * Resolve the value of a schema object to a proper type definition.
127
+ */
128
+ const resolveValue = (schema) => (0, exports.isReference)(schema) ? getRef(schema.$ref) : (0, exports.getScalar)(schema);
129
+ exports.resolveValue = resolveValue;
130
+ /**
131
+ * Format a description to code documentation.
132
+ */
133
+ const formatDescription = (description, tabSize = 0) => description
134
+ ? `/**\n${description
135
+ .split('\n')
136
+ .map((i) => `${' '.repeat(tabSize)} * ${i}`)
137
+ .join('\n')}\n${' '.repeat(tabSize)} */${' '.repeat(tabSize)}`
138
+ : '';
139
+ exports.formatDescription = formatDescription;
140
+ /**
141
+ * Extract responses / request types from open-api specs
142
+ */
143
+ const getResReqTypes = (responsesOrRequests) => (0, lodash_1.uniq)(responsesOrRequests.map(([_, res]) => {
144
+ if (!res) {
145
+ return 'void';
146
+ }
147
+ if ((0, exports.isReference)(res)) {
148
+ return getRef(res.$ref);
149
+ }
150
+ if (res.content) {
151
+ for (let contentType of Object.keys(res.content)) {
152
+ if (contentType.startsWith('application/json') ||
153
+ contentType.startsWith('application/octet-stream')) {
154
+ const schema = res.content[contentType].schema;
155
+ return (0, exports.resolveValue)(schema);
156
+ }
157
+ }
158
+ return 'void';
159
+ }
160
+ return 'void';
161
+ })).join(' | ');
162
+ exports.getResReqTypes = getResReqTypes;