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