wormajs 0.4.0 → 1.0.0-beta.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.
Files changed (50) hide show
  1. package/dist/bin/actions.js +162 -5
  2. package/dist/bin/cli.js +7 -1
  3. package/dist/bin/renderer.js +2 -8
  4. package/dist/checkUpdates.js +98 -0
  5. package/dist/config.js +7 -0
  6. package/dist/constant.js +1 -2
  7. package/dist/core/WorkerPool.js +14 -0
  8. package/dist/core/loader/callingCodeLoader/helper.js +2 -3
  9. package/dist/core/loader/callingCodeLoader/index.js +1 -1
  10. package/dist/core/parser/openApiParser/helper.js +32 -19
  11. package/dist/core/parser/templateParser/index.js +25 -12
  12. package/dist/core/workerPool/index.js +2 -1
  13. package/dist/functions/changeReport.js +230 -0
  14. package/dist/functions/diffApis.js +82 -0
  15. package/dist/functions/diffDocument.js +542 -0
  16. package/dist/functions/sourceSnapshot.js +107 -0
  17. package/dist/functions/wormaJson.js +306 -66
  18. package/dist/generate.js +24 -2
  19. package/dist/helper/config/ConfigHelper.js +1 -2
  20. package/dist/helper/config/ConfigManager.js +7 -0
  21. package/dist/helper/config/GeneratorHelper.js +74 -21
  22. package/dist/helper/config/zType.js +24 -1
  23. package/dist/helper/template/index.js +60 -4
  24. package/dist/index.js +17 -1
  25. package/dist/plugins/index.js +2 -2
  26. package/dist/plugins/presets/payloadModifier/dsl.js +147 -0
  27. package/dist/plugins/presets/payloadModifier/index.js +122 -135
  28. package/dist/plugins/presets/payloadModifier/patch.js +171 -0
  29. package/dist/plugins/presets/payloadModifier/scope.js +109 -0
  30. package/dist/plugins/presets/platform/index.js +1 -3
  31. package/dist/plugins/presets/postman.js +105 -0
  32. package/dist/template/presets/alova/common/services/{tag}.d.cts.handlebars +1 -1
  33. package/dist/template/presets/alova/module/services/{tag}.d.ts.handlebars +1 -1
  34. package/dist/template/presets/alova/partials/dts-fn-declare.handlebars +1 -1
  35. package/dist/template/presets/alova/partials/dts-types.handlebars +12 -0
  36. package/dist/template/presets/alova/typescript/services/{tag}.ts.handlebars +11 -6
  37. package/dist/template/presets/axios/partials/dts-types.handlebars +9 -5
  38. package/dist/template/presets/axios/typescript/services/{tag}.ts.handlebars +9 -5
  39. package/dist/template/presets/fetch/partials/dts-types.handlebars +8 -5
  40. package/dist/template/presets/fetch/typescript/services/{tag}.ts.handlebars +9 -5
  41. package/dist/template/presets/ky/partials/dts-types.handlebars +8 -5
  42. package/dist/template/presets/ky/typescript/services/{tag}.ts.handlebars +9 -5
  43. package/dist/utils/format.js +62 -15
  44. package/dist/utils/template.js +1 -1
  45. package/package.json +3 -2
  46. package/typings/index.d.ts +266 -13
  47. package/typings/plugins.d.ts +166 -81
  48. package/dist/plugins/presets/payloadModifier/hepler.js +0 -347
  49. package/dist/plugins/presets/platform/fastapi.js +0 -22
  50. package/dist/template/presets/alova/partials/dts-extra-config.handlebars +0 -8
@@ -1,347 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.applyModifierSchema = applyModifierSchema;
4
- // Detect a `SchemaOptional` wrapper: { required: boolean, type: Schema }.
5
- // The `required` must be a literal boolean so a plain SchemaReference whose
6
- // property happens to be named "required" (e.g. { required: 'boolean' }) is not misread.
7
- function isSchemaOptional(val) {
8
- return !!val
9
- && typeof val === 'object'
10
- && !Array.isArray(val)
11
- && typeof val.required === 'boolean'
12
- && 'type' in val;
13
- }
14
- // Collapse (possibly nested) `SchemaOptional` wrappers.
15
- // - The OUTERMOST `required` wins; inner `required` fields are ignored.
16
- // - A non-wrapped value defaults to required (required: true).
17
- function unwrapOptional(s) {
18
- if (!isSchemaOptional(s)) {
19
- return { required: true, type: s };
20
- }
21
- const required = s.required;
22
- let type = s.type;
23
- while (isSchemaOptional(type)) {
24
- type = type.type;
25
- }
26
- return { required, type };
27
- }
28
- // Remove the internal `_$ref` marker that `removeAll$ref` stamps onto dereferenced
29
- // component schemas. When a handler replaces a schema, the result must NOT inherit the
30
- // original component's `_$ref` — otherwise `mergeObject`/`removeBaseReference` downstream
31
- // treats the replacement as a reference to the original component and discards the change.
32
- function stripInternalRef(schema) {
33
- if (!schema || typeof schema !== 'object') {
34
- return schema;
35
- }
36
- if (Array.isArray(schema)) {
37
- return schema.map(stripInternalRef);
38
- }
39
- const out = {};
40
- for (const key of Object.keys(schema)) {
41
- if (key === '_$ref') {
42
- continue;
43
- }
44
- out[key] = stripInternalRef(schema[key]);
45
- }
46
- return out;
47
- }
48
- // Set of valid SchemaPrimitive values for O(1) validation lookup
49
- const VALID_PRIMITIVES = new Set([
50
- 'number',
51
- 'string',
52
- 'boolean',
53
- 'undefined',
54
- 'null',
55
- 'unknown',
56
- 'any',
57
- 'never',
58
- ]);
59
- function validatePrimitive(val) {
60
- if (!VALID_PRIMITIVES.has(val)) {
61
- throw new Error(`[payloadModifier] Invalid schema type "${val}". Must be one of: ${[...VALID_PRIMITIVES].join(', ')}`);
62
- }
63
- }
64
- // Convert Schema (custom spec) -> OpenAPI SchemaObject
65
- function toSchemaObject(base, s) {
66
- // A `SchemaOptional` wrapper only affects requiredness (handled by the caller);
67
- // here we care about the type shape, so fully unwrap nested wrappers first.
68
- if (isSchemaOptional(s)) {
69
- s = unwrapOptional(s).type;
70
- }
71
- const result = { ...base };
72
- const cleanType = (schema) => {
73
- delete schema.type;
74
- delete schema.enum;
75
- delete schema.oneOf;
76
- delete schema.anyOf;
77
- delete schema.allOf;
78
- delete schema.items;
79
- delete schema.properties;
80
- delete schema.required;
81
- return schema;
82
- };
83
- // Native array type (elements are Schema)
84
- if (Array.isArray(s)) {
85
- const arr = s;
86
- cleanType(result);
87
- result.type = 'array';
88
- // Pass the original `items` down as the base of each element so documentation
89
- // fields (e.g. `description`) survive the round-trip.
90
- const baseItems = base.items;
91
- const baseItemsList = Array.isArray(baseItems) ? baseItems : (baseItems ? [baseItems] : []);
92
- const items = arr.map((item, idx) => toSchemaObject((baseItemsList[idx] || {}), item));
93
- result.items = (items.length === 1 ? items[0] : items);
94
- return result;
95
- }
96
- // Primitive types — validate against SchemaPrimitive set during conversion
97
- if (typeof s === 'string') {
98
- validatePrimitive(s);
99
- // Drop the structural fields inherited from the base so they don't leak into the
100
- // new type; documentation fields such as `description` are kept.
101
- cleanType(result);
102
- result.type = s;
103
- return result;
104
- }
105
- // Handle union keywords: overwrite arrays but preserve unrelated fields
106
- if (s.oneOf) {
107
- const spec = s;
108
- const baseOneOf = base.oneOf || [];
109
- cleanType(result);
110
- result.oneOf = spec.oneOf.map((item, idx) => toSchemaObject(baseOneOf[idx] || {}, item));
111
- return result;
112
- }
113
- if (s.anyOf) {
114
- const spec = s;
115
- const baseAnyOf = base.anyOf || [];
116
- cleanType(result);
117
- result.anyOf = spec.anyOf.map((item, idx) => toSchemaObject(baseAnyOf[idx] || {}, item));
118
- return result;
119
- }
120
- if (s.allOf) {
121
- const spec = s;
122
- const baseAllOf = base.allOf || [];
123
- cleanType(result);
124
- result.allOf = spec.allOf.map((item, idx) => toSchemaObject(baseAllOf[idx] || {}, item));
125
- return result;
126
- }
127
- // Enum: write the enum values back, converting the TS primitive type from the
128
- // handler into its OpenAPI counterpart (`number` -> `integer`/`number`, `string`, ...).
129
- if (s.enum) {
130
- const spec = s;
131
- // Drop the structural fields inherited from the base; documentation fields are kept.
132
- cleanType(result);
133
- result.enum = spec.enum;
134
- if (spec.type) {
135
- if (typeof spec.type === 'string') {
136
- validatePrimitive(spec.type);
137
- }
138
- // `number` only becomes `integer` when every enum value is an integer.
139
- result.type = enumTypeToSchemaType(spec.type, spec.enum);
140
- }
141
- else {
142
- // No type from the handler: keep an OpenAPI 3.1 type array (e.g. `['string', 'null']`)
143
- // as-is, otherwise infer the type from the enum values.
144
- const fallback = Array.isArray(base.type) ? base.type : inferEnumType(spec.enum);
145
- if (fallback) {
146
- result.type = fallback;
147
- }
148
- // otherwise the enum stays untyped (mixed or empty values)
149
- }
150
- return result;
151
- }
152
- // Object (reference-like map): replace properties and required with handler's spec
153
- // (the SchemaReference returned by the handler fully replaces this field, only keeping
154
- // scalar fields like description from base)
155
- const ref = s;
156
- if (ref && typeof ref === 'object') {
157
- // Drop the structural fields inherited from the base; documentation fields are kept.
158
- cleanType(result);
159
- result.type = 'object';
160
- const properties = {};
161
- const requiredSet = new Set();
162
- // The base of each property is taken from the ORIGINAL `base.properties` (not from the
163
- // object being built) so documentation fields like `description` survive the round-trip.
164
- const baseProperties = (base.properties || {});
165
- for (const key in ref) {
166
- const val = ref[key];
167
- if (!val) {
168
- continue;
169
- }
170
- // SchemaOptional wrapper: optionality expressed via { required, type };
171
- // bare value defaults to required. Nested wrappers are collapsed — outermost
172
- // `required` wins, inner ones are ignored.
173
- let isOptional;
174
- let effectiveVal;
175
- if (isSchemaOptional(val)) {
176
- const { required, type } = unwrapOptional(val);
177
- isOptional = !required;
178
- effectiveVal = type;
179
- }
180
- else {
181
- isOptional = false;
182
- effectiveVal = val;
183
- }
184
- const baseProp = baseProperties[key];
185
- properties[key] = toSchemaObject(baseProp || {}, effectiveVal);
186
- if (isOptional) {
187
- requiredSet.delete(key);
188
- }
189
- else {
190
- requiredSet.add(key);
191
- }
192
- }
193
- result.properties = properties;
194
- result.required = Array.from(requiredSet);
195
- return result;
196
- }
197
- return result;
198
- }
199
- function schemaTypeToPrimitiveType(t) {
200
- if (t === null) {
201
- return 'null';
202
- }
203
- if (t === undefined) {
204
- return 'undefined';
205
- }
206
- if (!t) {
207
- return 'unknown';
208
- }
209
- if (t === 'integer') {
210
- return 'number';
211
- }
212
- return t;
213
- }
214
- // Convert a SchemaPrimitive (the TS type used by the handler) back into the OpenAPI type
215
- // of an enum. A numeric enum is written as `integer` (the OpenAPI counterpart of the TS
216
- // `number`) only when every value is an integer, otherwise it stays `number` so the type
217
- // matches the values. `string`/`boolean` map 1:1, TS-only types are written through as-is.
218
- function enumTypeToSchemaType(t, enumValues) {
219
- if (t === 'number') {
220
- return enumValues.every(v => typeof v === 'number' && Number.isInteger(v))
221
- ? 'integer'
222
- : 'number';
223
- }
224
- return t;
225
- }
226
- // Infer the OpenAPI type of an enum from its values, used when no type is declared so the
227
- // enum does not stay untyped. Returns `undefined` for mixed or empty values so that no
228
- // (possibly wrong) type is written.
229
- function inferEnumType(enumValues) {
230
- if (!enumValues.length) {
231
- return undefined;
232
- }
233
- if (enumValues.every(v => typeof v === 'string')) {
234
- return 'string';
235
- }
236
- if (enumValues.every(v => typeof v === 'boolean')) {
237
- return 'boolean';
238
- }
239
- if (enumValues.every(v => typeof v === 'number')) {
240
- return enumValues.every(v => Number.isInteger(v)) ? 'integer' : 'number';
241
- }
242
- // mixed value types -> leave the enum untyped
243
- return undefined;
244
- }
245
- // Convert existing OpenAPI SchemaObject -> Schema (best-effort, for handler input)
246
- function toSchemaSpec(obj) {
247
- if (!obj || typeof obj !== 'object') {
248
- return 'unknown';
249
- }
250
- // Union keywords
251
- if (Array.isArray(obj.oneOf)) {
252
- const arr = obj.oneOf;
253
- return { oneOf: arr.map(item => toSchemaSpec(item)) };
254
- }
255
- if (Array.isArray(obj.anyOf)) {
256
- const arr = obj.anyOf;
257
- return { anyOf: arr.map(item => toSchemaSpec(item)) };
258
- }
259
- if (Array.isArray(obj.allOf)) {
260
- const arr = obj.allOf;
261
- return { allOf: arr.map(item => toSchemaSpec(item)) };
262
- }
263
- // Enum: the OpenAPI type is normalized to its TS primitive counterpart
264
- // (e.g. `integer` -> `number`), consistent with how plain primitives are converted.
265
- if (Array.isArray(obj.enum) && obj.enum.length > 0) {
266
- const type = typeof obj.type === 'string'
267
- ? schemaTypeToPrimitiveType(obj.type)
268
- : undefined;
269
- return { enum: obj.enum, type };
270
- }
271
- // Array -> native array
272
- if (obj.type === 'array' || obj.items) {
273
- const items = obj.items;
274
- if (Array.isArray(items)) {
275
- return items.map((it) => toSchemaSpec(it));
276
- }
277
- if (items) {
278
- return [toSchemaSpec(items)];
279
- }
280
- return ['unknown'];
281
- }
282
- // Object
283
- if (obj.type === 'object' || obj.properties) {
284
- const properties = obj.properties || {};
285
- const requiredSet = new Set(Array.isArray(obj.required) ? obj.required : []);
286
- const result = {};
287
- for (const key of Object.keys(properties)) {
288
- const spec = toSchemaSpec(properties[key]);
289
- // Required fields are written bare; optional fields wrapped with SchemaOptional
290
- result[key] = requiredSet.has(key) ? spec : { required: false, type: spec };
291
- }
292
- return result;
293
- }
294
- // type union as array -> oneOf
295
- if (Array.isArray(obj.type)) {
296
- const typeArr = obj.type;
297
- const mapped = typeArr.map(schemaTypeToPrimitiveType);
298
- return { oneOf: mapped };
299
- }
300
- return schemaTypeToPrimitiveType(obj.type);
301
- }
302
- // Replace whole schema based on handler result (used for params/pathParams)
303
- function applyModifierSchema(schema, config, { required, key }) {
304
- if (!schema || typeof schema !== 'object') {
305
- return { required, schema: schema };
306
- }
307
- const cloned = { ...schema };
308
- const currentSpec = toSchemaSpec(cloned);
309
- // When the field is itself optional and is a primitive, wrap it as { required, type } before passing to handler
310
- const handlerInput = (required === false && typeof currentSpec === 'string')
311
- ? { required: false, type: currentSpec }
312
- : currentSpec;
313
- const ret = config.handler(handlerInput, key);
314
- if (!ret) {
315
- return {
316
- required,
317
- schema: null,
318
- };
319
- }
320
- // A returned SchemaOptional means changing requiredness (driven by the `type` field).
321
- // Nested wrappers are collapsed: the outermost `required` wins, inner ones are ignored;
322
- // `type` may be any Schema expression (primitive, object, array, union, ...).
323
- if (isSchemaOptional(ret)) {
324
- const { required: nextRequired, type } = unwrapOptional(ret);
325
- let r = stripInternalRef(toSchemaObject(cloned, type));
326
- // When handler explicitly sets required=false, propagate to object-level required array
327
- // so all properties become nullable as semantically expected
328
- if (!nextRequired && r && typeof r === 'object' && !Array.isArray(r)) {
329
- const robj = r;
330
- if (robj.type === 'object' && Array.isArray(robj.required) && robj.required.length > 0) {
331
- r = { ...r, required: [] };
332
- }
333
- }
334
- return {
335
- required: nextRequired,
336
- schema: r,
337
- };
338
- }
339
- // Non-SchemaOptional return: handler explicitly provides a type value,
340
- // so default to required=true (the handler had the chance to wrap with
341
- // { required: false, type: ... } if it wanted to keep it optional).
342
- const r = stripInternalRef(toSchemaObject(cloned, ret));
343
- return {
344
- required: true,
345
- schema: r,
346
- };
347
- }
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fastapi = void 0;
4
- const constant_1 = require("../../../constant");
5
- const shared_1 = require("./shared");
6
- /**
7
- * FastAPI platform plugin.
8
- *
9
- * Pass the base URL of your FastAPI app; the plugin will try `/openapi.json`
10
- * first, then fall back to the bare base URL.
11
- *
12
- * @param input - base URL string, or an array of base URLs
13
- *
14
- * @example
15
- * ```ts
16
- * plugins: [fastapi('http://fastapi-example.dokkuapp.com'), alovaGlobals()]
17
- * ```
18
- */
19
- exports.fastapi = (0, shared_1.defineUrlPlatformPlugin)(constant_1.PluginName.FASTAPI, base => [
20
- `${base}/openapi.json`,
21
- base,
22
- ]);
@@ -1,8 +0,0 @@
1
- {{#or pathParameters queryParameters requestBody}}
2
- type {{{name}}}ExtraConfig = {
3
- {{#if pathParameters}}pathParams: {{addNamespace pathParameters @root.componentNames}};
4
- {{/if}}{{#if queryParameters}}params: {{addNamespace queryParameters @root.componentNames}};
5
- {{/if}}{{#if requestBody}}data: {{addNamespace requestBody @root.componentNames}};
6
- {{/if}}
7
- };
8
- {{/or}}