@appweaver/client 1.3.0 → 1.4.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.
- package/cjs/clients/modules/resource-client.d.ts +12 -11
- package/cjs/clients/modules/resource-client.js +6 -6
- package/cjs/commands/generate-command.js +3 -1
- package/cjs/constants.d.ts +9 -0
- package/cjs/constants.js +28 -1
- package/cjs/generators/generate-types.d.ts +3 -1
- package/cjs/generators/generate-types.js +99 -9
- package/cjs/index.d.ts +1 -0
- package/cjs/index.js +1 -0
- package/cjs/types/generator.d.ts +6 -0
- package/cjs/types/generator.js +2 -0
- package/cjs/types/index.d.ts +2 -0
- package/cjs/types/index.js +2 -0
- package/cjs/types/resource.d.ts +2 -0
- package/cjs/types/resource.js +2 -0
- package/cjs/utils/enum-util.d.ts +17 -17
- package/cjs/utils/enum-util.js +50 -170
- package/cjs/utils/hoist-util.d.ts +29 -0
- package/cjs/utils/hoist-util.js +338 -0
- package/cjs/utils/index.d.ts +1 -0
- package/cjs/utils/index.js +1 -0
- package/esm/clients/modules/resource-client.d.ts +12 -11
- package/esm/clients/modules/resource-client.js +6 -6
- package/esm/commands/generate-command.js +3 -1
- package/esm/constants.d.ts +9 -0
- package/esm/constants.js +27 -0
- package/esm/generators/generate-types.d.ts +3 -1
- package/esm/generators/generate-types.js +100 -10
- package/esm/index.d.ts +1 -0
- package/esm/index.js +1 -0
- package/esm/types/generator.d.ts +6 -0
- package/esm/types/generator.js +1 -0
- package/esm/types/index.d.ts +2 -0
- package/esm/types/index.js +2 -0
- package/esm/types/resource.d.ts +2 -0
- package/esm/types/resource.js +1 -0
- package/esm/utils/enum-util.d.ts +17 -17
- package/esm/utils/enum-util.js +49 -169
- package/esm/utils/hoist-util.d.ts +29 -0
- package/esm/utils/hoist-util.js +335 -0
- package/esm/utils/index.d.ts +1 -0
- package/esm/utils/index.js +1 -0
- package/package.json +3 -2
package/esm/utils/enum-util.js
CHANGED
|
@@ -1,176 +1,56 @@
|
|
|
1
|
-
import { SHARED_ENUM_NAMES } from '../constants.js';
|
|
2
1
|
/**
|
|
3
|
-
*
|
|
4
|
-
* of their
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* The
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
|
|
22
|
-
export function hoistSharedEnums(schema) {
|
|
23
|
-
const definitions = schema.components?.schemas;
|
|
24
|
-
if (!definitions) {
|
|
25
|
-
return [];
|
|
26
|
-
}
|
|
27
|
-
// Enums declared by identical schema content, keyed by that content
|
|
28
|
-
const groups = new Map();
|
|
29
|
-
for (const [key, definition] of Object.entries(definitions)) {
|
|
30
|
-
if (definition && typeof definition === 'object') {
|
|
31
|
-
const owner = definition['title'];
|
|
32
|
-
collectEnums(definition, typeof owner === 'string' ? owner : key, '', groups);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
const takenNames = new Set(Object.keys(definitions));
|
|
36
|
-
for (const definition of Object.values(definitions)) {
|
|
37
|
-
const title = definition?.['title'];
|
|
38
|
-
if (typeof title === 'string') {
|
|
39
|
-
takenNames.add(title);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
const hoistedNames = [];
|
|
43
|
-
for (const [content, occurrences] of groups) {
|
|
44
|
-
if (occurrences.length < 2) {
|
|
45
|
-
continue;
|
|
46
|
-
}
|
|
47
|
-
const name = resolveEnumName(JSON.parse(content), occurrences);
|
|
48
|
-
if (!name || takenNames.has(name)) {
|
|
49
|
-
continue;
|
|
50
|
-
}
|
|
51
|
-
takenNames.add(name);
|
|
52
|
-
hoistedNames.push(name);
|
|
53
|
-
definitions[name] = { title: name, ...JSON.parse(content) };
|
|
54
|
-
for (const { container, key } of occurrences) {
|
|
55
|
-
container[key] = {
|
|
56
|
-
$ref: `#/components/schemas/${name}`
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return hoistedNames;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Collects every inline enum declared below the given schema node into the groups map,
|
|
64
|
-
* keyed by the stable serialization of the enum schema, so identical enums group together.
|
|
65
|
-
*
|
|
66
|
-
* @param {SchemaContainer} container The schema node to collect the enums of.
|
|
67
|
-
* @param {string} owner The name of the definition the node belongs to.
|
|
68
|
-
* @param {string} property The name of the property the node is declared under, if any.
|
|
69
|
-
* @param {Map<string, EnumOccurrence[]>} groups The collected occurrences, grouped by content.
|
|
70
|
-
*/
|
|
71
|
-
function collectEnums(container, owner, property, groups) {
|
|
72
|
-
const entries = Array.isArray(container)
|
|
73
|
-
? container.map((value, index) => [index, value])
|
|
74
|
-
: Object.entries(container);
|
|
75
|
-
for (const [key, value] of entries) {
|
|
76
|
-
if (!value || typeof value !== 'object') {
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
// The keys of a `properties` object name the schemas below them, everything
|
|
80
|
-
// else keeps the property name of the node it was reached through
|
|
81
|
-
if (key === 'properties' && !Array.isArray(value)) {
|
|
82
|
-
const properties = value;
|
|
83
|
-
for (const name of Object.keys(properties)) {
|
|
84
|
-
visitSchema(properties, name, owner, name, groups);
|
|
85
|
-
}
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
visitSchema(container, key, owner, property, groups);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Records the schema held by the container under the given key as an enum occurrence, or
|
|
93
|
-
* descends into it when it declares no enum of its own.
|
|
94
|
-
*
|
|
95
|
-
* @param {SchemaContainer} container The container holding the schema node.
|
|
96
|
-
* @param {string | number} key The key the schema node is held under.
|
|
97
|
-
* @param {string} owner The name of the definition the node belongs to.
|
|
98
|
-
* @param {string} property The name of the property the node is declared under, if any.
|
|
99
|
-
* @param {Map<string, EnumOccurrence[]>} groups The collected occurrences, grouped by content.
|
|
100
|
-
*/
|
|
101
|
-
function visitSchema(container, key, owner, property, groups) {
|
|
102
|
-
const node = container[key];
|
|
103
|
-
if (Array.isArray(node['enum'])) {
|
|
104
|
-
const content = stableStringify(node);
|
|
105
|
-
const occurrences = groups.get(content) ?? [];
|
|
106
|
-
occurrences.push({ container, key, owner, property });
|
|
107
|
-
groups.set(content, occurrences);
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
collectEnums(node, owner, property, groups);
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Resolves the name of the definition an enum is hoisted into, preferring the name given to
|
|
114
|
-
* its values by {@link SHARED_ENUM_NAMES} and falling back to the common part of the names of
|
|
115
|
-
* the definitions declaring it, followed by the property name they all declare it under.
|
|
116
|
-
*
|
|
117
|
-
* @param {Record<string, unknown>} node The enum schema to resolve the name of.
|
|
118
|
-
* @param {EnumOccurrence[]} occurrences The places the enum is declared in.
|
|
119
|
-
* @return {string | undefined} The resolved name, or undefined when the enum has no name to
|
|
120
|
-
* be hoisted under.
|
|
121
|
-
*/
|
|
122
|
-
function resolveEnumName(node, occurrences) {
|
|
123
|
-
const values = node['enum'];
|
|
124
|
-
const knownName = SHARED_ENUM_NAMES[values.join('|')];
|
|
125
|
-
if (knownName) {
|
|
126
|
-
return knownName;
|
|
127
|
-
}
|
|
128
|
-
const { property } = occurrences[0];
|
|
129
|
-
if (!property || occurrences.some((o) => o.property !== property)) {
|
|
130
|
-
return undefined;
|
|
131
|
-
}
|
|
132
|
-
const prefix = commonNamePrefix(occurrences.map((o) => o.owner));
|
|
133
|
-
if (!prefix) {
|
|
134
|
-
return undefined;
|
|
135
|
-
}
|
|
136
|
-
return prefix + property.charAt(0).toUpperCase() + property.slice(1);
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Resolves the longest prefix the given names share, cut at a word boundary so the result
|
|
140
|
-
* stays a readable name (i.e. `HealthCheckResponse` and `HealthCheckResult` give
|
|
141
|
-
* `HealthCheck` rather than `HealthCheckRes`).
|
|
142
|
-
*
|
|
143
|
-
* @param {string[]} names The names to find the common prefix of.
|
|
144
|
-
* @return {string} The common prefix, empty when the names start with different words.
|
|
2
|
+
* Rewrites the TypeScript enums of the generated types into a constant object holding the
|
|
3
|
+
* members and a type alias of their values, so both forms are accepted wherever the enum is
|
|
4
|
+
* used (i.e. `SortDirection.asc` and the plain `'asc'` literal alike).
|
|
5
|
+
*
|
|
6
|
+
* A TypeScript `enum` declares a nominal type, which rejects the very literals it is built
|
|
7
|
+
* from, forcing users of the generated client to import the enum for a value the API
|
|
8
|
+
* documents as a string. The rewritten declaration keeps the member access working while
|
|
9
|
+
* typing the property as the union of its values:
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* export const SortDirection = { asc: 'asc', desc: 'desc' } as const;
|
|
13
|
+
* export type SortDirection = (typeof SortDirection)[keyof typeof SortDirection];
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @param {string} content The generated TypeScript types to rewrite the enums of.
|
|
17
|
+
* @param {boolean} [declaration=false] Whether the types are emitted into a declaration
|
|
18
|
+
* (`.d.ts`) file, which holds no runtime values and so declares the constant instead of
|
|
19
|
+
* initializing it.
|
|
20
|
+
* @return {string} The types with every enum declaration rewritten.
|
|
145
21
|
*/
|
|
146
|
-
function
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
156
|
-
|
|
22
|
+
export function rewriteEnumsAsObjects(content, declaration = false) {
|
|
23
|
+
return content.replace(/export enum (\w+) \{([^}]*)}/g, (match, name, body) => {
|
|
24
|
+
const members = parseEnumMembers(body);
|
|
25
|
+
if (members.length === 0) {
|
|
26
|
+
return match;
|
|
27
|
+
}
|
|
28
|
+
const values = members
|
|
29
|
+
.map(([member, value]) => declaration
|
|
30
|
+
? ` readonly ${member}: ${value};`
|
|
31
|
+
: ` ${member}: ${value},`)
|
|
32
|
+
.join('\n');
|
|
33
|
+
const constant = declaration
|
|
34
|
+
? `export declare const ${name}: {\n${values}\n};`
|
|
35
|
+
: `export const ${name} = {\n${values}\n} as const;`;
|
|
36
|
+
return `${constant}\nexport type ${name} = (typeof ${name})[keyof typeof ${name}];`;
|
|
37
|
+
});
|
|
157
38
|
}
|
|
158
39
|
/**
|
|
159
|
-
*
|
|
160
|
-
*
|
|
40
|
+
* Parses the members of an enum declaration body into their name and value pairs, keeping
|
|
41
|
+
* the value verbatim so a quoted member name or a numeric value survives the rewrite.
|
|
161
42
|
*
|
|
162
|
-
* @param {
|
|
163
|
-
* @return {string} The
|
|
43
|
+
* @param {string} body The body of the enum declaration, without the enclosing braces.
|
|
44
|
+
* @return {[string, string][]} The name and value of every member, in declaration order.
|
|
164
45
|
*/
|
|
165
|
-
function
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
return JSON.stringify(value) ?? 'null';
|
|
46
|
+
function parseEnumMembers(body) {
|
|
47
|
+
const members = [];
|
|
48
|
+
// A member name, quoted or not, followed by its value, which is a quoted string
|
|
49
|
+
// (holding a comma of its own or not) or a number
|
|
50
|
+
const pattern = /("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|[\w$]+)\s*=\s*("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|-?[\d.]+)/g;
|
|
51
|
+
let member;
|
|
52
|
+
while ((member = pattern.exec(body)) !== null) {
|
|
53
|
+
members.push([member[1], member[2]]);
|
|
54
|
+
}
|
|
55
|
+
return members;
|
|
176
56
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { OpenAPI3 } from 'openapi-typescript';
|
|
2
|
+
/**
|
|
3
|
+
* Hoists the schemas an OpenAPI document repeats inline across its definitions into shared
|
|
4
|
+
* definitions of their own, replacing every occurrence with a reference to the hoisted
|
|
5
|
+
* definition.
|
|
6
|
+
*
|
|
7
|
+
* An Appweaver schema declares the same shapes over and over, once per property that accepts
|
|
8
|
+
* them, and each of them would otherwise be spelled out again in the generated types (i.e. the
|
|
9
|
+
* `asc | desc` enum of every sortable field, or the accepted value of every filterable field of
|
|
10
|
+
* every resource). Two kinds of schema are hoisted:
|
|
11
|
+
*
|
|
12
|
+
* - The well known shapes of {@link SHARED_SCHEMA_SHAPES}, matched in the order they are
|
|
13
|
+
* declared so an outer shape is hoisted before the shapes nested inside it.
|
|
14
|
+
* - The enums the definitions repeat, named after {@link SHARED_ENUM_NAMES} when their values
|
|
15
|
+
* are well known, and otherwise after the part the declaring definitions have in common
|
|
16
|
+
* followed by the property name (i.e. `PostCreate` and `PostSingle` declaring `status` give
|
|
17
|
+
* `PostStatus`). Enums whose name cannot be resolved are left inline.
|
|
18
|
+
*
|
|
19
|
+
* Only the structure of a schema decides whether it is hoisted, and every occurrence keeps the
|
|
20
|
+
* documentation it carries, so no property loses its description or example along the way.
|
|
21
|
+
*
|
|
22
|
+
* A schema declared only once, or whose name is already taken, is left inline as well.
|
|
23
|
+
*
|
|
24
|
+
* The given schema is mutated in place.
|
|
25
|
+
*
|
|
26
|
+
* @param {OpenAPI3} schema The OpenAPI v3 schema to hoist the shared definitions of.
|
|
27
|
+
* @return {string[]} The names of the hoisted definitions, in the order they were created.
|
|
28
|
+
*/
|
|
29
|
+
export declare function hoistSharedTypes(schema: OpenAPI3): string[];
|
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { SHARED_ENUM_NAMES, SHARED_SCHEMA_SHAPES } from '../constants.js';
|
|
2
|
+
/** Keys documenting a schema node rather than describing its structure. They are kept on the
|
|
3
|
+
* property the node is hoisted out of, so no property loses its description along the way. */
|
|
4
|
+
const ANNOTATION_KEYS = ['description', 'example', 'examples', 'deprecated'];
|
|
5
|
+
/**
|
|
6
|
+
* Hoists the schemas an OpenAPI document repeats inline across its definitions into shared
|
|
7
|
+
* definitions of their own, replacing every occurrence with a reference to the hoisted
|
|
8
|
+
* definition.
|
|
9
|
+
*
|
|
10
|
+
* An Appweaver schema declares the same shapes over and over, once per property that accepts
|
|
11
|
+
* them, and each of them would otherwise be spelled out again in the generated types (i.e. the
|
|
12
|
+
* `asc | desc` enum of every sortable field, or the accepted value of every filterable field of
|
|
13
|
+
* every resource). Two kinds of schema are hoisted:
|
|
14
|
+
*
|
|
15
|
+
* - The well known shapes of {@link SHARED_SCHEMA_SHAPES}, matched in the order they are
|
|
16
|
+
* declared so an outer shape is hoisted before the shapes nested inside it.
|
|
17
|
+
* - The enums the definitions repeat, named after {@link SHARED_ENUM_NAMES} when their values
|
|
18
|
+
* are well known, and otherwise after the part the declaring definitions have in common
|
|
19
|
+
* followed by the property name (i.e. `PostCreate` and `PostSingle` declaring `status` give
|
|
20
|
+
* `PostStatus`). Enums whose name cannot be resolved are left inline.
|
|
21
|
+
*
|
|
22
|
+
* Only the structure of a schema decides whether it is hoisted, and every occurrence keeps the
|
|
23
|
+
* documentation it carries, so no property loses its description or example along the way.
|
|
24
|
+
*
|
|
25
|
+
* A schema declared only once, or whose name is already taken, is left inline as well.
|
|
26
|
+
*
|
|
27
|
+
* The given schema is mutated in place.
|
|
28
|
+
*
|
|
29
|
+
* @param {OpenAPI3} schema The OpenAPI v3 schema to hoist the shared definitions of.
|
|
30
|
+
* @return {string[]} The names of the hoisted definitions, in the order they were created.
|
|
31
|
+
*/
|
|
32
|
+
export function hoistSharedTypes(schema) {
|
|
33
|
+
const definitions = schema.components?.schemas;
|
|
34
|
+
if (!definitions) {
|
|
35
|
+
return [];
|
|
36
|
+
}
|
|
37
|
+
const takenNames = new Set(Object.keys(definitions));
|
|
38
|
+
for (const definition of Object.values(definitions)) {
|
|
39
|
+
const title = definition?.['title'];
|
|
40
|
+
if (typeof title === 'string') {
|
|
41
|
+
takenNames.add(title);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const hoistedNames = [];
|
|
45
|
+
for (const { name, schema: shape } of SHARED_SCHEMA_SHAPES) {
|
|
46
|
+
if (hoistShape(definitions, name, shape, takenNames)) {
|
|
47
|
+
hoistedNames.push(name);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
hoistedNames.push(...hoistEnums(definitions, takenNames));
|
|
51
|
+
return hoistedNames;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Hoists every occurrence of a well known shape into a definition of the given name. The
|
|
55
|
+
* definition is built from the first occurrence, so it holds the references the shape declares
|
|
56
|
+
* by title as the references of the document.
|
|
57
|
+
*
|
|
58
|
+
* @param {Record<string, unknown>} definitions The definitions of the schema.
|
|
59
|
+
* @param {string} name The name of the definition the shape is hoisted into.
|
|
60
|
+
* @param {unknown} shape The shape to hoist, holding its references as definition titles.
|
|
61
|
+
* @param {Set<string>} takenNames The definition names already in use.
|
|
62
|
+
* @return {boolean} Whether the shape was hoisted.
|
|
63
|
+
*/
|
|
64
|
+
function hoistShape(definitions, name, shape, takenNames) {
|
|
65
|
+
const titles = definitionTitles(definitions);
|
|
66
|
+
const signature = schemaSignature(shape, titles);
|
|
67
|
+
const groups = collectOccurrences(definitions, (node) => schemaSignature(structure(node), titles) === signature ? name : undefined);
|
|
68
|
+
const occurrences = groups.get(name);
|
|
69
|
+
if (!occurrences || occurrences.length < 2 || takenNames.has(name)) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
takenNames.add(name);
|
|
73
|
+
const { container, key } = occurrences[0];
|
|
74
|
+
definitions[name] = {
|
|
75
|
+
title: name,
|
|
76
|
+
...structure(container[key])
|
|
77
|
+
};
|
|
78
|
+
for (const occurrence of occurrences) {
|
|
79
|
+
replaceWithReference(occurrence, name);
|
|
80
|
+
}
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Hoists the enums the definitions repeat into shared definitions named after their values.
|
|
85
|
+
*
|
|
86
|
+
* @param {Record<string, unknown>} definitions The definitions of the schema.
|
|
87
|
+
* @param {Set<string>} takenNames The definition names already in use.
|
|
88
|
+
* @return {string[]} The names of the hoisted definitions, in the order they were created.
|
|
89
|
+
*/
|
|
90
|
+
function hoistEnums(definitions, takenNames) {
|
|
91
|
+
// Enums declared by identical schema content, keyed by that content. The
|
|
92
|
+
// documentation of a node is left out of the key, since every occurrence keeps
|
|
93
|
+
// the documentation of its own when it is replaced by a reference
|
|
94
|
+
const groups = collectOccurrences(definitions, (node) => Array.isArray(node['enum']) ? stableStringify(structure(node)) : undefined);
|
|
95
|
+
const hoistedNames = [];
|
|
96
|
+
for (const [content, occurrences] of groups) {
|
|
97
|
+
if (occurrences.length < 2) {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const name = resolveEnumName(JSON.parse(content), occurrences);
|
|
101
|
+
if (!name || takenNames.has(name)) {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
takenNames.add(name);
|
|
105
|
+
hoistedNames.push(name);
|
|
106
|
+
definitions[name] = { title: name, ...JSON.parse(content) };
|
|
107
|
+
for (const occurrence of occurrences) {
|
|
108
|
+
replaceWithReference(occurrence, name);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return hoistedNames;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Replaces the schema node of an occurrence with a reference to the given definition, keeping
|
|
115
|
+
* the documentation the node carries.
|
|
116
|
+
*
|
|
117
|
+
* @param {Occurrence} occurrence The occurrence to replace.
|
|
118
|
+
* @param {string} name The name of the definition to reference.
|
|
119
|
+
*/
|
|
120
|
+
function replaceWithReference({ container, key }, name) {
|
|
121
|
+
const node = container[key];
|
|
122
|
+
container[key] = {
|
|
123
|
+
...annotations(node),
|
|
124
|
+
$ref: `#/components/schemas/${name}`
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Collects every inline schema the matcher accepts into a map of occurrences, grouped by the
|
|
129
|
+
* key the matcher returns for them. A node the matcher accepts is not descended into, so an
|
|
130
|
+
* outer match wins over the nodes nested inside it.
|
|
131
|
+
*
|
|
132
|
+
* @param {Record<string, unknown>} definitions The definitions of the schema.
|
|
133
|
+
* @param {Matcher} match The matcher deciding which nodes are collected.
|
|
134
|
+
* @return {Map<string, Occurrence[]>} The collected occurrences, grouped by matcher key.
|
|
135
|
+
*/
|
|
136
|
+
function collectOccurrences(definitions, match) {
|
|
137
|
+
const groups = new Map();
|
|
138
|
+
for (const [key, definition] of Object.entries(definitions)) {
|
|
139
|
+
if (definition && typeof definition === 'object') {
|
|
140
|
+
const owner = definition['title'];
|
|
141
|
+
collect(definition, typeof owner === 'string' ? owner : key, '', match, groups);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return groups;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Collects every inline schema declared below the given schema node into the groups map.
|
|
148
|
+
*
|
|
149
|
+
* @param {SchemaContainer} container The schema node to collect the schemas of.
|
|
150
|
+
* @param {string} owner The name of the definition the node belongs to.
|
|
151
|
+
* @param {string} property The name of the property the node is declared under, if any.
|
|
152
|
+
* @param {Matcher} match The matcher deciding which nodes are collected.
|
|
153
|
+
* @param {Map<string, Occurrence[]>} groups The collected occurrences, grouped by matcher key.
|
|
154
|
+
*/
|
|
155
|
+
function collect(container, owner, property, match, groups) {
|
|
156
|
+
const entries = Array.isArray(container)
|
|
157
|
+
? container.map((value, index) => [index, value])
|
|
158
|
+
: Object.entries(container);
|
|
159
|
+
for (const [key, value] of entries) {
|
|
160
|
+
if (!value || typeof value !== 'object') {
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
// The keys of a `properties` object name the schemas below them, everything
|
|
164
|
+
// else keeps the property name of the node it was reached through
|
|
165
|
+
if (key === 'properties' && !Array.isArray(value)) {
|
|
166
|
+
const properties = value;
|
|
167
|
+
for (const name of Object.keys(properties)) {
|
|
168
|
+
visit(properties, name, owner, name, match, groups);
|
|
169
|
+
}
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
visit(container, key, owner, property, match, groups);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Records the schema held by the container under the given key as an occurrence, or descends
|
|
177
|
+
* into it when the matcher does not accept it.
|
|
178
|
+
*
|
|
179
|
+
* @param {SchemaContainer} container The container holding the schema node.
|
|
180
|
+
* @param {string | number} key The key the schema node is held under.
|
|
181
|
+
* @param {string} owner The name of the definition the node belongs to.
|
|
182
|
+
* @param {string} property The name of the property the node is declared under, if any.
|
|
183
|
+
* @param {Matcher} match The matcher deciding which nodes are collected.
|
|
184
|
+
* @param {Map<string, Occurrence[]>} groups The collected occurrences, grouped by matcher key.
|
|
185
|
+
*/
|
|
186
|
+
function visit(container, key, owner, property, match, groups) {
|
|
187
|
+
const node = container[key];
|
|
188
|
+
const group = match(node);
|
|
189
|
+
if (group !== undefined) {
|
|
190
|
+
const occurrences = groups.get(group) ?? [];
|
|
191
|
+
occurrences.push({ container, key, owner, property });
|
|
192
|
+
groups.set(group, occurrences);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
collect(node, owner, property, match, groups);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Resolves the name of the definition an enum is hoisted into, preferring the name given to
|
|
199
|
+
* its values by {@link SHARED_ENUM_NAMES} and falling back to the common part of the names of
|
|
200
|
+
* the definitions declaring it, followed by the property name they all declare it under.
|
|
201
|
+
*
|
|
202
|
+
* @param {SchemaNode} node The enum schema to resolve the name of.
|
|
203
|
+
* @param {Occurrence[]} occurrences The places the enum is declared in.
|
|
204
|
+
* @return {string | undefined} The resolved name, or undefined when the enum has no name to
|
|
205
|
+
* be hoisted under.
|
|
206
|
+
*/
|
|
207
|
+
function resolveEnumName(node, occurrences) {
|
|
208
|
+
const values = node['enum'];
|
|
209
|
+
const knownName = SHARED_ENUM_NAMES[values.join('|')];
|
|
210
|
+
if (knownName) {
|
|
211
|
+
return knownName;
|
|
212
|
+
}
|
|
213
|
+
const { property } = occurrences[0];
|
|
214
|
+
if (!property || occurrences.some((o) => o.property !== property)) {
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
const prefix = commonNamePrefix(occurrences.map((o) => o.owner));
|
|
218
|
+
if (!prefix) {
|
|
219
|
+
return undefined;
|
|
220
|
+
}
|
|
221
|
+
return prefix + property.charAt(0).toUpperCase() + property.slice(1);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Resolves the longest prefix the given names share, cut at a word boundary so the result
|
|
225
|
+
* stays a readable name (i.e. `HealthCheckResponse` and `HealthCheckResult` give
|
|
226
|
+
* `HealthCheck` rather than `HealthCheckRes`).
|
|
227
|
+
*
|
|
228
|
+
* @param {string[]} names The names to find the common prefix of.
|
|
229
|
+
* @return {string} The common prefix, empty when the names start with different words.
|
|
230
|
+
*/
|
|
231
|
+
function commonNamePrefix(names) {
|
|
232
|
+
const wordsPerName = names.map((name) => name.match(/[A-Z]+(?![a-z])|[A-Z]?[a-z0-9]+|[A-Z]/g) ?? []);
|
|
233
|
+
const words = [];
|
|
234
|
+
for (let i = 0; i < wordsPerName[0].length; i++) {
|
|
235
|
+
const word = wordsPerName[0][i];
|
|
236
|
+
if (wordsPerName.some((other) => other[i] !== word)) {
|
|
237
|
+
break;
|
|
238
|
+
}
|
|
239
|
+
words.push(word);
|
|
240
|
+
}
|
|
241
|
+
return words.join('');
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Maps every definition key to the title it is generated under, so a reference can be compared
|
|
245
|
+
* by the name it resolves to rather than by the generated key holding it.
|
|
246
|
+
*
|
|
247
|
+
* @param {Record<string, unknown>} definitions The definitions of the schema.
|
|
248
|
+
* @return {Map<string, string>} The title of every definition, keyed by its definition key.
|
|
249
|
+
*/
|
|
250
|
+
function definitionTitles(definitions) {
|
|
251
|
+
const titles = new Map();
|
|
252
|
+
for (const [key, definition] of Object.entries(definitions)) {
|
|
253
|
+
const title = definition?.['title'];
|
|
254
|
+
titles.set(key, typeof title === 'string' ? title : key);
|
|
255
|
+
}
|
|
256
|
+
return titles;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Returns the documentation keys of a schema node, which the node keeps when it is replaced by
|
|
260
|
+
* a reference to a hoisted definition.
|
|
261
|
+
*
|
|
262
|
+
* @param {SchemaNode} node The schema node to take the documentation of.
|
|
263
|
+
* @return {SchemaNode} The documentation keys of the node.
|
|
264
|
+
*/
|
|
265
|
+
function annotations(node) {
|
|
266
|
+
return Object.fromEntries(Object.entries(node).filter(([key]) => ANNOTATION_KEYS.includes(key)));
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Returns the structural keys of a schema node, so two nodes describing the same shape under a
|
|
270
|
+
* different description compare alike.
|
|
271
|
+
*
|
|
272
|
+
* @param {SchemaNode} node The schema node to take the structure of.
|
|
273
|
+
* @return {SchemaNode} The structural keys of the node.
|
|
274
|
+
*/
|
|
275
|
+
function structure(node) {
|
|
276
|
+
return Object.fromEntries(Object.entries(node).filter(([key]) => !ANNOTATION_KEYS.includes(key)));
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Serializes a schema with its references resolved to the title of the definition they point
|
|
280
|
+
* to, so a shape declared by {@link SHARED_SCHEMA_SHAPES} compares equal to the same shape of a
|
|
281
|
+
* document, whose references name the generated definition keys instead.
|
|
282
|
+
*
|
|
283
|
+
* @param {unknown} value The schema to serialize.
|
|
284
|
+
* @param {Map<string, string>} titles The title of every definition, keyed by definition key.
|
|
285
|
+
* @return {string} The stable serialization of the schema.
|
|
286
|
+
*/
|
|
287
|
+
function schemaSignature(value, titles) {
|
|
288
|
+
if (Array.isArray(value)) {
|
|
289
|
+
return `[${value.map((item) => schemaSignature(item, titles)).join(',')}]`;
|
|
290
|
+
}
|
|
291
|
+
if (value && typeof value === 'object') {
|
|
292
|
+
const entries = Object.keys(value)
|
|
293
|
+
.sort()
|
|
294
|
+
.map((key) => {
|
|
295
|
+
const nested = value[key];
|
|
296
|
+
const resolved = key === '$ref' && typeof nested === 'string'
|
|
297
|
+
? JSON.stringify(referenceTitle(nested, titles))
|
|
298
|
+
: schemaSignature(nested, titles);
|
|
299
|
+
return `${JSON.stringify(key)}:${resolved}`;
|
|
300
|
+
});
|
|
301
|
+
return `{${entries.join(',')}}`;
|
|
302
|
+
}
|
|
303
|
+
return JSON.stringify(value) ?? 'null';
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Resolves a reference to the title of the definition it points to.
|
|
307
|
+
*
|
|
308
|
+
* @param {string} reference The reference to resolve (i.e. `#/components/schemas/def-1`).
|
|
309
|
+
* @param {Map<string, string>} titles The title of every definition, keyed by definition key.
|
|
310
|
+
* @return {string} The title of the referenced definition, or the reference when it points
|
|
311
|
+
* outside of the definitions of the document.
|
|
312
|
+
*/
|
|
313
|
+
function referenceTitle(reference, titles) {
|
|
314
|
+
const key = reference.replace('#/components/schemas/', '');
|
|
315
|
+
return titles.get(key) ?? reference;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Serializes a value with its object keys sorted, so two schemas holding the same content
|
|
319
|
+
* in a different key order serialize alike.
|
|
320
|
+
*
|
|
321
|
+
* @param {unknown} value The value to serialize.
|
|
322
|
+
* @return {string} The stable JSON serialization of the value.
|
|
323
|
+
*/
|
|
324
|
+
function stableStringify(value) {
|
|
325
|
+
if (Array.isArray(value)) {
|
|
326
|
+
return `[${value.map(stableStringify).join(',')}]`;
|
|
327
|
+
}
|
|
328
|
+
if (value && typeof value === 'object') {
|
|
329
|
+
const entries = Object.keys(value)
|
|
330
|
+
.sort()
|
|
331
|
+
.map((key) => `${JSON.stringify(key)}:${stableStringify(value[key])}`);
|
|
332
|
+
return `{${entries.join(',')}}`;
|
|
333
|
+
}
|
|
334
|
+
return JSON.stringify(value) ?? 'null';
|
|
335
|
+
}
|
package/esm/utils/index.d.ts
CHANGED
package/esm/utils/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@appweaver/client",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Appweaver - the backend framework for AI-first development (@client)",
|
|
5
5
|
"author": "Luka Matosevic",
|
|
6
6
|
"license": "MIT",
|
|
@@ -52,7 +52,8 @@
|
|
|
52
52
|
"node": ">= 22"
|
|
53
53
|
},
|
|
54
54
|
"scripts": {
|
|
55
|
-
"
|
|
55
|
+
"generate-client": "node ./dist/cjs/weaver-client.js generate ./generated/openapi.json --outputPath ./generated/client.ts",
|
|
56
|
+
"generate-client-schema": "node ./dist/cjs/weaver-client.js generate ./generated/openapi.json --typesPath ./generated/schema.d.ts --clientPath ./generated/client.ts"
|
|
56
57
|
},
|
|
57
58
|
"dependencies": {
|
|
58
59
|
"commander": "15.0.0",
|