mobx-tanstack-query-api 0.24.0 → 0.26.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/builtin.cjs +27 -0
- package/builtin.cjs.map +1 -0
- package/builtin.d.ts +6 -0
- package/builtin.js +27 -0
- package/builtin.js.map +1 -0
- package/cli.cjs +1279 -0
- package/cli.cjs.map +1 -0
- package/{codegen/types/generate-query-api-params.d.cts → cli.d.ts} +55 -10
- package/cli.js +1278 -0
- package/cli.js.map +1 -0
- package/index.cjs +771 -16
- package/index.cjs.map +1 -0
- package/index.d.ts +322 -2
- package/index.js +768 -2
- package/index.js.map +1 -0
- package/package.json +21 -64
- package/builtin/index.cjs +0 -25
- package/builtin/index.d.cts +0 -4
- package/builtin/index.d.cts.map +0 -1
- package/builtin/index.d.ts +0 -4
- package/builtin/index.d.ts.map +0 -1
- package/builtin/index.js +0 -22
- package/cli/bin.cjs +0 -17
- package/cli/bin.d.cts +0 -2
- package/cli/bin.d.cts.map +0 -1
- package/cli/bin.d.ts +0 -2
- package/cli/bin.d.ts.map +0 -1
- package/cli/bin.js +0 -15
- package/cli/index.cjs +0 -18
- package/cli/index.d.cts +0 -3
- package/cli/index.d.cts.map +0 -1
- package/cli/index.d.ts +0 -3
- package/cli/index.d.ts.map +0 -1
- package/cli/index.js +0 -2
- package/cli/utils/define-config.cjs +0 -9
- package/cli/utils/define-config.d.cts +0 -4
- package/cli/utils/define-config.d.cts.map +0 -1
- package/cli/utils/define-config.d.ts +0 -4
- package/cli/utils/define-config.d.ts.map +0 -1
- package/cli/utils/define-config.js +0 -5
- package/cli/utils/exec-config-path.cjs +0 -16
- package/cli/utils/exec-config-path.d.cts +0 -2
- package/cli/utils/exec-config-path.d.cts.map +0 -1
- package/cli/utils/exec-config-path.d.ts +0 -2
- package/cli/utils/exec-config-path.d.ts.map +0 -1
- package/cli/utils/exec-config-path.js +0 -12
- package/cli/utils/resolve-config-path.cjs +0 -65
- package/cli/utils/resolve-config-path.d.cts +0 -3
- package/cli/utils/resolve-config-path.d.cts.map +0 -1
- package/cli/utils/resolve-config-path.d.ts +0 -3
- package/cli/utils/resolve-config-path.d.ts.map +0 -1
- package/cli/utils/resolve-config-path.js +0 -25
- package/codegen/index.cjs +0 -545
- package/codegen/index.d.cts +0 -4
- package/codegen/index.d.cts.map +0 -1
- package/codegen/index.d.ts +0 -4
- package/codegen/index.d.ts.map +0 -1
- package/codegen/index.js +0 -524
- package/codegen/templates/all-endpoints-per-file.tmpl.cjs +0 -94
- package/codegen/templates/all-endpoints-per-file.tmpl.d.cts +0 -14
- package/codegen/templates/all-endpoints-per-file.tmpl.d.cts.map +0 -1
- package/codegen/templates/all-endpoints-per-file.tmpl.d.ts +0 -14
- package/codegen/templates/all-endpoints-per-file.tmpl.d.ts.map +0 -1
- package/codegen/templates/all-endpoints-per-file.tmpl.js +0 -90
- package/codegen/templates/all-exports.tmpl.cjs +0 -14
- package/codegen/templates/all-exports.tmpl.d.cts +0 -9
- package/codegen/templates/all-exports.tmpl.d.cts.map +0 -1
- package/codegen/templates/all-exports.tmpl.d.ts +0 -9
- package/codegen/templates/all-exports.tmpl.d.ts.map +0 -1
- package/codegen/templates/all-exports.tmpl.js +0 -9
- package/codegen/templates/constants.cjs +0 -5
- package/codegen/templates/constants.d.cts +0 -2
- package/codegen/templates/constants.d.cts.map +0 -1
- package/codegen/templates/constants.d.ts +0 -2
- package/codegen/templates/constants.d.ts.map +0 -1
- package/codegen/templates/constants.js +0 -2
- package/codegen/templates/data-contract.tmpl.cjs +0 -49
- package/codegen/templates/data-contract.tmpl.d.cts +0 -8
- package/codegen/templates/data-contract.tmpl.d.cts.map +0 -1
- package/codegen/templates/data-contract.tmpl.d.ts +0 -8
- package/codegen/templates/data-contract.tmpl.d.ts.map +0 -1
- package/codegen/templates/data-contract.tmpl.js +0 -45
- package/codegen/templates/data-contracts-file.tmpl.cjs +0 -27
- package/codegen/templates/data-contracts-file.tmpl.d.cts +0 -6
- package/codegen/templates/data-contracts-file.tmpl.d.cts.map +0 -1
- package/codegen/templates/data-contracts-file.tmpl.d.ts +0 -6
- package/codegen/templates/data-contracts-file.tmpl.d.ts.map +0 -1
- package/codegen/templates/data-contracts-file.tmpl.js +0 -23
- package/codegen/templates/endpoint-jsdoc.tmpl.cjs +0 -111
- package/codegen/templates/endpoint-jsdoc.tmpl.d.cts +0 -8
- package/codegen/templates/endpoint-jsdoc.tmpl.d.cts.map +0 -1
- package/codegen/templates/endpoint-jsdoc.tmpl.d.ts +0 -8
- package/codegen/templates/endpoint-jsdoc.tmpl.d.ts.map +0 -1
- package/codegen/templates/endpoint-jsdoc.tmpl.js +0 -107
- package/codegen/templates/endpoint-per-file.tmpl.cjs +0 -87
- package/codegen/templates/endpoint-per-file.tmpl.d.cts +0 -14
- package/codegen/templates/endpoint-per-file.tmpl.d.cts.map +0 -1
- package/codegen/templates/endpoint-per-file.tmpl.d.ts +0 -14
- package/codegen/templates/endpoint-per-file.tmpl.d.ts.map +0 -1
- package/codegen/templates/endpoint-per-file.tmpl.js +0 -83
- package/codegen/templates/index-ts-for-endpoint-per-file.tmpl.cjs +0 -10
- package/codegen/templates/index-ts-for-endpoint-per-file.tmpl.d.cts +0 -6
- package/codegen/templates/index-ts-for-endpoint-per-file.tmpl.d.cts.map +0 -1
- package/codegen/templates/index-ts-for-endpoint-per-file.tmpl.d.ts +0 -6
- package/codegen/templates/index-ts-for-endpoint-per-file.tmpl.d.ts.map +0 -1
- package/codegen/templates/index-ts-for-endpoint-per-file.tmpl.js +0 -6
- package/codegen/templates/meta-info.tmpl.cjs +0 -48
- package/codegen/templates/meta-info.tmpl.d.cts +0 -9
- package/codegen/templates/meta-info.tmpl.d.cts.map +0 -1
- package/codegen/templates/meta-info.tmpl.d.ts +0 -9
- package/codegen/templates/meta-info.tmpl.d.ts.map +0 -1
- package/codegen/templates/meta-info.tmpl.js +0 -42
- package/codegen/templates/new-endpoint.tmpl.cjs +0 -188
- package/codegen/templates/new-endpoint.tmpl.d.cts +0 -15
- package/codegen/templates/new-endpoint.tmpl.d.cts.map +0 -1
- package/codegen/templates/new-endpoint.tmpl.d.ts +0 -15
- package/codegen/templates/new-endpoint.tmpl.d.ts.map +0 -1
- package/codegen/templates/new-endpoint.tmpl.js +0 -184
- package/codegen/types/all-import-file-params.cjs +0 -2
- package/codegen/types/all-import-file-params.d.cts +0 -5
- package/codegen/types/all-import-file-params.d.cts.map +0 -1
- package/codegen/types/all-import-file-params.d.ts +0 -5
- package/codegen/types/all-import-file-params.d.ts.map +0 -1
- package/codegen/types/all-import-file-params.js +0 -1
- package/codegen/types/base-tmpl-params.cjs +0 -2
- package/codegen/types/base-tmpl-params.d.cts +0 -16
- package/codegen/types/base-tmpl-params.d.cts.map +0 -1
- package/codegen/types/base-tmpl-params.d.ts +0 -16
- package/codegen/types/base-tmpl-params.d.ts.map +0 -1
- package/codegen/types/base-tmpl-params.js +0 -1
- package/codegen/types/codegen-data-utils.cjs +0 -2
- package/codegen/types/codegen-data-utils.d.cts +0 -8
- package/codegen/types/codegen-data-utils.d.cts.map +0 -1
- package/codegen/types/codegen-data-utils.d.ts +0 -8
- package/codegen/types/codegen-data-utils.d.ts.map +0 -1
- package/codegen/types/codegen-data-utils.js +0 -1
- package/codegen/types/endpoint-data.cjs +0 -2
- package/codegen/types/endpoint-data.d.cts +0 -3
- package/codegen/types/endpoint-data.d.cts.map +0 -1
- package/codegen/types/endpoint-data.d.ts +0 -3
- package/codegen/types/endpoint-data.d.ts.map +0 -1
- package/codegen/types/endpoint-data.js +0 -1
- package/codegen/types/generate-query-api-params.cjs +0 -2
- package/codegen/types/generate-query-api-params.d.cts.map +0 -1
- package/codegen/types/generate-query-api-params.d.ts +0 -162
- package/codegen/types/generate-query-api-params.d.ts.map +0 -1
- package/codegen/types/generate-query-api-params.js +0 -1
- package/codegen/types/import-file-params.cjs +0 -2
- package/codegen/types/import-file-params.d.cts +0 -5
- package/codegen/types/import-file-params.d.cts.map +0 -1
- package/codegen/types/import-file-params.d.ts +0 -5
- package/codegen/types/import-file-params.d.ts.map +0 -1
- package/codegen/types/import-file-params.js +0 -1
- package/codegen/types/index.cjs +0 -23
- package/codegen/types/index.d.cts +0 -8
- package/codegen/types/index.d.cts.map +0 -1
- package/codegen/types/index.d.ts +0 -8
- package/codegen/types/index.d.ts.map +0 -1
- package/codegen/types/index.js +0 -7
- package/codegen/types/meta-info.cjs +0 -2
- package/codegen/types/meta-info.d.cts +0 -6
- package/codegen/types/meta-info.d.cts.map +0 -1
- package/codegen/types/meta-info.d.ts +0 -6
- package/codegen/types/meta-info.d.ts.map +0 -1
- package/codegen/types/meta-info.js +0 -1
- package/codegen/types/type-info.cjs +0 -2
- package/codegen/types/type-info.d.cts +0 -4
- package/codegen/types/type-info.d.cts.map +0 -1
- package/codegen/types/type-info.d.ts +0 -4
- package/codegen/types/type-info.d.ts.map +0 -1
- package/codegen/types/type-info.js +0 -1
- package/codegen/utils/create-short-model-type.cjs +0 -12
- package/codegen/utils/create-short-model-type.d.cts +0 -5
- package/codegen/utils/create-short-model-type.d.cts.map +0 -1
- package/codegen/utils/create-short-model-type.d.ts +0 -5
- package/codegen/utils/create-short-model-type.d.ts.map +0 -1
- package/codegen/utils/create-short-model-type.js +0 -8
- package/codegen/utils/remove-unused-types.cjs +0 -117
- package/codegen/utils/remove-unused-types.d.cts +0 -7
- package/codegen/utils/remove-unused-types.d.cts.map +0 -1
- package/codegen/utils/remove-unused-types.d.ts +0 -7
- package/codegen/utils/remove-unused-types.d.ts.map +0 -1
- package/codegen/utils/remove-unused-types.js +0 -110
- package/codegen/utils/unpack-filter-option.cjs +0 -23
- package/codegen/utils/unpack-filter-option.d.cts +0 -7
- package/codegen/utils/unpack-filter-option.d.cts.map +0 -1
- package/codegen/utils/unpack-filter-option.d.ts +0 -7
- package/codegen/utils/unpack-filter-option.d.ts.map +0 -1
- package/codegen/utils/unpack-filter-option.js +0 -19
- package/index.d.cts +0 -3
- package/index.d.cts.map +0 -1
- package/index.d.ts.map +0 -1
- package/runtime/endpoint-infinite-query.cjs +0 -101
- package/runtime/endpoint-infinite-query.d.cts +0 -17
- package/runtime/endpoint-infinite-query.d.cts.map +0 -1
- package/runtime/endpoint-infinite-query.d.ts +0 -17
- package/runtime/endpoint-infinite-query.d.ts.map +0 -1
- package/runtime/endpoint-infinite-query.js +0 -97
- package/runtime/endpoint-infinite-query.types.cjs +0 -2
- package/runtime/endpoint-infinite-query.types.d.cts +0 -30
- package/runtime/endpoint-infinite-query.types.d.cts.map +0 -1
- package/runtime/endpoint-infinite-query.types.d.ts +0 -30
- package/runtime/endpoint-infinite-query.types.d.ts.map +0 -1
- package/runtime/endpoint-infinite-query.types.js +0 -1
- package/runtime/endpoint-mutation.cjs +0 -74
- package/runtime/endpoint-mutation.d.cts +0 -13
- package/runtime/endpoint-mutation.d.cts.map +0 -1
- package/runtime/endpoint-mutation.d.ts +0 -13
- package/runtime/endpoint-mutation.d.ts.map +0 -1
- package/runtime/endpoint-mutation.js +0 -70
- package/runtime/endpoint-mutation.types.cjs +0 -2
- package/runtime/endpoint-mutation.types.d.cts +0 -36
- package/runtime/endpoint-mutation.types.d.cts.map +0 -1
- package/runtime/endpoint-mutation.types.d.ts +0 -36
- package/runtime/endpoint-mutation.types.d.ts.map +0 -1
- package/runtime/endpoint-mutation.types.js +0 -1
- package/runtime/endpoint-query-client.cjs +0 -79
- package/runtime/endpoint-query-client.d.cts +0 -13
- package/runtime/endpoint-query-client.d.cts.map +0 -1
- package/runtime/endpoint-query-client.d.ts +0 -13
- package/runtime/endpoint-query-client.d.ts.map +0 -1
- package/runtime/endpoint-query-client.js +0 -75
- package/runtime/endpoint-query-client.types.cjs +0 -2
- package/runtime/endpoint-query-client.types.d.cts +0 -16
- package/runtime/endpoint-query-client.types.d.cts.map +0 -1
- package/runtime/endpoint-query-client.types.d.ts +0 -16
- package/runtime/endpoint-query-client.types.d.ts.map +0 -1
- package/runtime/endpoint-query-client.types.js +0 -1
- package/runtime/endpoint-query.cjs +0 -214
- package/runtime/endpoint-query.d.cts +0 -29
- package/runtime/endpoint-query.d.cts.map +0 -1
- package/runtime/endpoint-query.d.ts +0 -29
- package/runtime/endpoint-query.d.ts.map +0 -1
- package/runtime/endpoint-query.js +0 -208
- package/runtime/endpoint-query.types.cjs +0 -2
- package/runtime/endpoint-query.types.d.cts +0 -42
- package/runtime/endpoint-query.types.d.cts.map +0 -1
- package/runtime/endpoint-query.types.d.ts +0 -42
- package/runtime/endpoint-query.types.d.ts.map +0 -1
- package/runtime/endpoint-query.types.js +0 -1
- package/runtime/endpoint.cjs +0 -131
- package/runtime/endpoint.d.cts +0 -73
- package/runtime/endpoint.d.cts.map +0 -1
- package/runtime/endpoint.d.ts +0 -73
- package/runtime/endpoint.d.ts.map +0 -1
- package/runtime/endpoint.js +0 -127
- package/runtime/endpoint.types.cjs +0 -2
- package/runtime/endpoint.types.d.cts +0 -24
- package/runtime/endpoint.types.d.cts.map +0 -1
- package/runtime/endpoint.types.d.ts +0 -24
- package/runtime/endpoint.types.d.ts.map +0 -1
- package/runtime/endpoint.types.js +0 -1
- package/runtime/http-client.cjs +0 -190
- package/runtime/http-client.d.cts +0 -85
- package/runtime/http-client.d.cts.map +0 -1
- package/runtime/http-client.d.ts +0 -85
- package/runtime/http-client.d.ts.map +0 -1
- package/runtime/http-client.js +0 -184
- package/runtime/index.cjs +0 -25
- package/runtime/index.d.cts +0 -10
- package/runtime/index.d.cts.map +0 -1
- package/runtime/index.d.ts +0 -10
- package/runtime/index.d.ts.map +0 -1
- package/runtime/index.js +0 -9
package/cli.js
ADDED
|
@@ -0,0 +1,1278 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { cloneDeep } from "lodash-es";
|
|
5
|
+
import { generateApi as generateApi$1 } from "swagger-typescript-api";
|
|
6
|
+
import { splitTextByLines } from "yummies/text";
|
|
7
|
+
import { Project, SyntaxKind, Node } from "ts-morph";
|
|
8
|
+
const LINTERS_IGNORE = `/* eslint-disable */
|
|
9
|
+
/* tslint:disable */`;
|
|
10
|
+
const buildGenerics = (contract) => {
|
|
11
|
+
if (!contract.genericArgs?.length) return "";
|
|
12
|
+
return "<" + contract.genericArgs.map((arg) => {
|
|
13
|
+
const { name, default: defaultType, extends: extendsType } = arg;
|
|
14
|
+
return [
|
|
15
|
+
name,
|
|
16
|
+
extendsType && `extends ${extendsType}`,
|
|
17
|
+
defaultType && `= ${defaultType}`
|
|
18
|
+
].filter(Boolean).join(" ");
|
|
19
|
+
}).join(", ") + ">";
|
|
20
|
+
};
|
|
21
|
+
const dataContractTmpl = async ({
|
|
22
|
+
contract,
|
|
23
|
+
configuration,
|
|
24
|
+
addExportKeyword
|
|
25
|
+
}) => {
|
|
26
|
+
const { utils } = configuration;
|
|
27
|
+
const { formatDescription } = utils;
|
|
28
|
+
const dataContractTemplates = {
|
|
29
|
+
enum: (contract2) => {
|
|
30
|
+
return `enum ${contract2.name} {\r
|
|
31
|
+
${contract2.content}\r
|
|
32
|
+
}`;
|
|
33
|
+
},
|
|
34
|
+
interface: (contract2) => {
|
|
35
|
+
return `interface ${contract2.name}${buildGenerics(contract2)} {\r
|
|
36
|
+
${contract2.content}}`;
|
|
37
|
+
},
|
|
38
|
+
type: (contract2) => {
|
|
39
|
+
return `type ${contract2.name}${buildGenerics(contract2)} = ${contract2.content === contract2.name ? "any" : contract2.content}`;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
let result = "";
|
|
43
|
+
let jsdoc = "";
|
|
44
|
+
if (contract.description) {
|
|
45
|
+
jsdoc = `/**
|
|
46
|
+
* ${formatDescription(contract.description, true)}
|
|
47
|
+
*/
|
|
48
|
+
`;
|
|
49
|
+
}
|
|
50
|
+
if (jsdoc) {
|
|
51
|
+
result += jsdoc;
|
|
52
|
+
}
|
|
53
|
+
const templateFn = dataContractTemplates[contract.typeIdentifier] || dataContractTemplates.type;
|
|
54
|
+
const contractType = `${"export "}${templateFn(contract)}`;
|
|
55
|
+
return result + contractType;
|
|
56
|
+
};
|
|
57
|
+
const endpointJSDocTmpl = (params) => {
|
|
58
|
+
const { route, configuration, offset = 0 } = params;
|
|
59
|
+
const { routeName } = route;
|
|
60
|
+
const rawRoute = route.raw;
|
|
61
|
+
const routeRequest = route.request;
|
|
62
|
+
const { utils } = configuration;
|
|
63
|
+
const { _, formatDescription } = utils;
|
|
64
|
+
const jsDocLines = [];
|
|
65
|
+
if (rawRoute.summary) {
|
|
66
|
+
const summaryLines = splitTextByLines(rawRoute.summary, 60).filter(Boolean).map((line) => ({
|
|
67
|
+
content: `**${formatDescription(line, true)}**`
|
|
68
|
+
}));
|
|
69
|
+
if (summaryLines.length > 0) {
|
|
70
|
+
jsDocLines.push(...summaryLines, { content: "" });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (rawRoute.description) {
|
|
74
|
+
const descriptionLines = splitTextByLines(rawRoute.description, 60).filter(Boolean).map((line) => ({
|
|
75
|
+
content: formatDescription(line, true)
|
|
76
|
+
}));
|
|
77
|
+
if (descriptionLines.length > 0) {
|
|
78
|
+
jsDocLines.push(...descriptionLines, { content: "" });
|
|
79
|
+
} else {
|
|
80
|
+
jsDocLines.push({
|
|
81
|
+
content: "No description"
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
} else {
|
|
85
|
+
jsDocLines.push({
|
|
86
|
+
content: "No description"
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
if (rawRoute.operationId) {
|
|
90
|
+
jsDocLines.push({
|
|
91
|
+
name: "operationId",
|
|
92
|
+
content: rawRoute.operationId
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (_.size(rawRoute.tags)) {
|
|
96
|
+
jsDocLines.push({
|
|
97
|
+
name: "tags",
|
|
98
|
+
content: rawRoute.tags.join(", ")
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
jsDocLines.push({
|
|
102
|
+
name: "request",
|
|
103
|
+
content: `**${_.upperCase(routeRequest.method)}:${rawRoute.route}**`
|
|
104
|
+
});
|
|
105
|
+
if (rawRoute.deprecated) {
|
|
106
|
+
jsDocLines.push({
|
|
107
|
+
name: "deprecated"
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
if (routeName.duplicate) {
|
|
111
|
+
jsDocLines.push(
|
|
112
|
+
{
|
|
113
|
+
name: "duplicate"
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
name: "originalName",
|
|
117
|
+
content: routeName.original
|
|
118
|
+
}
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
if (routeRequest.security) {
|
|
122
|
+
jsDocLines.push({
|
|
123
|
+
name: "secure"
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
if (rawRoute.responsesTypes.length > 0) {
|
|
127
|
+
jsDocLines.push({
|
|
128
|
+
name: "responses"
|
|
129
|
+
});
|
|
130
|
+
rawRoute.responsesTypes.forEach((response) => {
|
|
131
|
+
jsDocLines.push({
|
|
132
|
+
name: `**${response.status}**`,
|
|
133
|
+
content: `${_.replace(_.replace(response.type, /\/\*/g, String.raw`\*`), /\*\//g, "*\\")} ${response.description}`
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
const jsdocContent = jsDocLines.map((it) => {
|
|
138
|
+
let line = " * ";
|
|
139
|
+
if (it.name) {
|
|
140
|
+
line += `@${it.name} `;
|
|
141
|
+
}
|
|
142
|
+
const content = (it.content ?? "").trimEnd();
|
|
143
|
+
if (content) {
|
|
144
|
+
line += content;
|
|
145
|
+
}
|
|
146
|
+
return line;
|
|
147
|
+
});
|
|
148
|
+
const result = `
|
|
149
|
+
/**
|
|
150
|
+
${jsdocContent.join("\n")}
|
|
151
|
+
*/`;
|
|
152
|
+
if (offset > 0) {
|
|
153
|
+
return result.split("\n").map((line) => line.padStart(offset)).join("\n");
|
|
154
|
+
}
|
|
155
|
+
return result;
|
|
156
|
+
};
|
|
157
|
+
const createShortModelType = (shortModelType) => {
|
|
158
|
+
return {
|
|
159
|
+
...shortModelType,
|
|
160
|
+
typeIdentifier: shortModelType.typeIdentifier || "type",
|
|
161
|
+
rawContent: shortModelType.rawContent || shortModelType.content,
|
|
162
|
+
description: shortModelType.description || ""
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
const formatGroupNameEnumKey = (groupName, { _ }) => _.upperFirst(_.camelCase(groupName));
|
|
166
|
+
const formatTagNameEnumKey = (tagName, utils) => formatGroupNameEnumKey(tagName, utils);
|
|
167
|
+
const metaInfoTmpl = async ({
|
|
168
|
+
metaInfo,
|
|
169
|
+
utils,
|
|
170
|
+
formatTSContent,
|
|
171
|
+
codegenParams,
|
|
172
|
+
...other
|
|
173
|
+
}) => {
|
|
174
|
+
const tagsMap = new Map(
|
|
175
|
+
other.configuration?.apiConfig?.tags?.map((it) => [
|
|
176
|
+
it.name,
|
|
177
|
+
it
|
|
178
|
+
])
|
|
179
|
+
);
|
|
180
|
+
return await formatTSContent(`${LINTERS_IGNORE}
|
|
181
|
+
${[
|
|
182
|
+
metaInfo?.namespace && `export const namespace = "${metaInfo?.namespace}";`,
|
|
183
|
+
metaInfo?.groupNames?.length && `
|
|
184
|
+
export const enum Group {
|
|
185
|
+
${metaInfo?.groupNames.map((groupName) => `${formatGroupNameEnumKey(groupName, utils)} = "${codegenParams.transforms?.groupEnumValue?.(groupName) ?? groupName}"`).join(",\n")}
|
|
186
|
+
}
|
|
187
|
+
`,
|
|
188
|
+
metaInfo?.tags?.length && `
|
|
189
|
+
export const enum Tag {
|
|
190
|
+
${metaInfo?.tags.map((tagName) => {
|
|
191
|
+
const tagData = tagsMap.get(tagName);
|
|
192
|
+
let description = tagData?.description;
|
|
193
|
+
if (!description) {
|
|
194
|
+
description = utils._.words(tagName).join(" ");
|
|
195
|
+
}
|
|
196
|
+
return [
|
|
197
|
+
description && `/** ${description} */`,
|
|
198
|
+
`${formatTagNameEnumKey(tagName, utils)} = "${codegenParams.transforms?.tagEnumValue?.(tagName) ?? tagName}"`
|
|
199
|
+
].filter(Boolean).join("\n");
|
|
200
|
+
}).join(",\n")}
|
|
201
|
+
}
|
|
202
|
+
`
|
|
203
|
+
].filter(Boolean).join("\n")}
|
|
204
|
+
`);
|
|
205
|
+
};
|
|
206
|
+
const requestContentKind = {
|
|
207
|
+
URL_ENCODED: '"application/x-www-form-urlencoded"',
|
|
208
|
+
FORM_DATA: '"multipart/form-data"',
|
|
209
|
+
TEXT: '"text/plain"',
|
|
210
|
+
BINARY: '"application/octet-stream"'
|
|
211
|
+
};
|
|
212
|
+
const responseContentKind = {
|
|
213
|
+
TEXT: '"text"',
|
|
214
|
+
IMAGE: '"blob"',
|
|
215
|
+
FORM_DATA: '"formData"',
|
|
216
|
+
BYTES: '"bytes"'
|
|
217
|
+
};
|
|
218
|
+
const newEndpointTmpl = ({
|
|
219
|
+
route,
|
|
220
|
+
codegenParams,
|
|
221
|
+
importFileParams,
|
|
222
|
+
utils,
|
|
223
|
+
groupName,
|
|
224
|
+
metaInfo,
|
|
225
|
+
filterTypes,
|
|
226
|
+
configuration
|
|
227
|
+
}) => {
|
|
228
|
+
const { _ } = utils;
|
|
229
|
+
const positiveResponseTypes = route.raw.responsesTypes?.filter(
|
|
230
|
+
(it) => +it.status >= 200 && +it.status < 300 && (!it.typeData || filterTypes(it.typeData))
|
|
231
|
+
);
|
|
232
|
+
const { requestBodyInfo, responseBodyInfo } = route;
|
|
233
|
+
const routeRequest = route.request;
|
|
234
|
+
const routeResponse = route.response;
|
|
235
|
+
const { parameters, path: path2, method, payload, query, requestParams, security } = routeRequest;
|
|
236
|
+
const { raw } = route;
|
|
237
|
+
const queryName = query?.name || "query";
|
|
238
|
+
const pathParams = _.values(parameters);
|
|
239
|
+
const pathParamsNames = _.map(pathParams, "name");
|
|
240
|
+
const requestConfigParam = {
|
|
241
|
+
name: "requestParams",
|
|
242
|
+
optional: true,
|
|
243
|
+
type: "RequestParams",
|
|
244
|
+
defaultValue: "{}"
|
|
245
|
+
};
|
|
246
|
+
const inputParams = [
|
|
247
|
+
...pathParams,
|
|
248
|
+
payload,
|
|
249
|
+
query,
|
|
250
|
+
requestConfigParam
|
|
251
|
+
].filter(Boolean);
|
|
252
|
+
const getArgs = ({
|
|
253
|
+
withPayload,
|
|
254
|
+
withRequestConfigParam
|
|
255
|
+
}) => {
|
|
256
|
+
return _.sortBy(
|
|
257
|
+
_.compact([
|
|
258
|
+
...requestParams ? [
|
|
259
|
+
{
|
|
260
|
+
name: pathParams.length > 0 ? `{ ${_.join(pathParamsNames, ", ")}, ...${queryName} }` : queryName,
|
|
261
|
+
optional: false,
|
|
262
|
+
type: utils.getInlineParseContent(requestParams)
|
|
263
|
+
}
|
|
264
|
+
] : pathParams,
|
|
265
|
+
payload,
|
|
266
|
+
withRequestConfigParam && requestConfigParam
|
|
267
|
+
]),
|
|
268
|
+
[(o) => o.optional]
|
|
269
|
+
);
|
|
270
|
+
};
|
|
271
|
+
const tags = (raw.tags || []).filter(Boolean);
|
|
272
|
+
const requestOutputDataTypes = positiveResponseTypes.map((it) => it.type);
|
|
273
|
+
const foundErrorModelType = routeResponse.errorType && configuration.modelTypes.find(
|
|
274
|
+
(it) => it.name === routeResponse.errorType
|
|
275
|
+
) || null;
|
|
276
|
+
const requestOutputErrorType = foundErrorModelType ? routeResponse.errorType : "any";
|
|
277
|
+
const pathParamsToInline = path2.split("/").slice(1);
|
|
278
|
+
query == null ? null : {
|
|
279
|
+
i: pathParamsToInline.length
|
|
280
|
+
};
|
|
281
|
+
const requestInfoMeta = codegenParams.getEndpointMeta?.(route, utils);
|
|
282
|
+
const requestMeta = codegenParams.getRequestMeta?.(route, utils);
|
|
283
|
+
const resultPath = (codegenParams.requestPathPrefix ?? "") + path2 + (codegenParams.requestPathSuffix ?? "");
|
|
284
|
+
const bodyContentType = requestContentKind[requestBodyInfo.contentKind] || null;
|
|
285
|
+
const responseFormat = responseContentKind[responseBodyInfo.success?.schema?.contentKind] || null;
|
|
286
|
+
const reservedDataContractNames = _.uniq([
|
|
287
|
+
...requestOutputDataTypes,
|
|
288
|
+
requestOutputErrorType || "any",
|
|
289
|
+
...getArgs({
|
|
290
|
+
withPayload: true
|
|
291
|
+
}).map((it) => it.type)
|
|
292
|
+
]);
|
|
293
|
+
const pathDeclaration = resultPath.replaceAll("$", "");
|
|
294
|
+
const getHttpRequestGenerics = () => {
|
|
295
|
+
const defaultOkResponse = positiveResponseTypes?.[0]?.type || "unknown";
|
|
296
|
+
const defaultBadResponse = requestOutputErrorType;
|
|
297
|
+
const responses = raw.responsesTypes?.filter(
|
|
298
|
+
(it) => it.status !== "default" && (!it.typeData || filterTypes(it.typeData))
|
|
299
|
+
) || [];
|
|
300
|
+
if (!responses?.length) {
|
|
301
|
+
return `HttpResponse<unknown, ${requestOutputErrorType}>`;
|
|
302
|
+
}
|
|
303
|
+
if (responses.length === 1 && responses[0].isSuccess) {
|
|
304
|
+
return `HttpResponse<${responses[0].type}, ${requestOutputErrorType}>`;
|
|
305
|
+
}
|
|
306
|
+
return `HttpMultistatusResponse<{
|
|
307
|
+
${responses.map((it) => {
|
|
308
|
+
return [
|
|
309
|
+
it.description && `/** ${it.description} */`,
|
|
310
|
+
`${it.status}: ${it.type};`
|
|
311
|
+
].filter(Boolean).join("\n");
|
|
312
|
+
}).join("\n")}
|
|
313
|
+
},
|
|
314
|
+
${defaultOkResponse},
|
|
315
|
+
${defaultBadResponse}
|
|
316
|
+
>`;
|
|
317
|
+
};
|
|
318
|
+
const requestInputTypeDc = createShortModelType({
|
|
319
|
+
typeIdentifier: "type",
|
|
320
|
+
name: _.upperFirst(_.camelCase(`${route.routeName.usage}Params`)),
|
|
321
|
+
content: `{
|
|
322
|
+
${inputParams.map(({ name, optional, type }) => {
|
|
323
|
+
return `${name}${optional ? "?" : ""}:${type}`;
|
|
324
|
+
}).filter(Boolean).join(", ")}
|
|
325
|
+
}`
|
|
326
|
+
});
|
|
327
|
+
const isAllowedInputType = filterTypes(requestInputTypeDc);
|
|
328
|
+
return {
|
|
329
|
+
reservedDataContractNames,
|
|
330
|
+
localModelTypes: isAllowedInputType ? [requestInputTypeDc] : [],
|
|
331
|
+
content: `
|
|
332
|
+
new ${importFileParams.endpoint.exportName}<
|
|
333
|
+
${getHttpRequestGenerics()},
|
|
334
|
+
${isAllowedInputType ? requestInputTypeDc.name : "any"},
|
|
335
|
+
${requestInfoMeta?.typeName ?? "any"}
|
|
336
|
+
>(
|
|
337
|
+
{
|
|
338
|
+
params: ({
|
|
339
|
+
${inputParams.map((it) => it.name)}
|
|
340
|
+
}) => ({
|
|
341
|
+
path: \`${resultPath}\`,
|
|
342
|
+
method: '${_.upperCase(method)}',
|
|
343
|
+
${requestMeta?.tmplData ? `meta: ${requestMeta.tmplData},` : ""}
|
|
344
|
+
${query == null ? "" : `query: ${query.name},`}
|
|
345
|
+
${payload?.name ? `body: ${payload.name},` : ""}
|
|
346
|
+
${security ? "secure: true," : ""}
|
|
347
|
+
${bodyContentType ? `contentType: ${bodyContentType},` : ""}
|
|
348
|
+
${responseFormat ? `format: ${responseFormat},` : ""}
|
|
349
|
+
...${requestConfigParam.name},
|
|
350
|
+
}),
|
|
351
|
+
requiredParams: [${inputParams.filter((it) => !it.optional).map((it) => `"${it.name}"`)}],
|
|
352
|
+
operationId: "${raw.operationId || _.camelCase(route.routeName.usage)}",
|
|
353
|
+
path: [${pathDeclaration.split("/").filter(Boolean).map((it) => `"${it}"`)}],
|
|
354
|
+
tags: [${tags.map((tag) => {
|
|
355
|
+
if (metaInfo) {
|
|
356
|
+
return `Tag.${formatTagNameEnumKey(tag, utils)}`;
|
|
357
|
+
}
|
|
358
|
+
return `"${tag}"`;
|
|
359
|
+
})}],
|
|
360
|
+
${groupName ? `group: ${metaInfo ? `Group.${formatGroupNameEnumKey(groupName, utils)}` : `"${groupName}"`},` : ""}
|
|
361
|
+
${metaInfo?.namespace ? `namespace,` : ""}
|
|
362
|
+
meta: ${requestInfoMeta?.tmplData ?? "{} as any"},
|
|
363
|
+
},
|
|
364
|
+
${importFileParams.queryClient.exportName},
|
|
365
|
+
${importFileParams.httpClient.exportName},
|
|
366
|
+
)
|
|
367
|
+
`.split("\n").map((it) => it.trim()).filter(Boolean).join("\n")
|
|
368
|
+
};
|
|
369
|
+
};
|
|
370
|
+
const allEndpointPerFileTmpl = async (params) => {
|
|
371
|
+
const {
|
|
372
|
+
routes,
|
|
373
|
+
configuration,
|
|
374
|
+
codegenParams,
|
|
375
|
+
formatTSContent,
|
|
376
|
+
importFileParams,
|
|
377
|
+
utils,
|
|
378
|
+
relativePathDataContracts,
|
|
379
|
+
groupName,
|
|
380
|
+
metaInfo
|
|
381
|
+
} = params;
|
|
382
|
+
const { _ } = utils;
|
|
383
|
+
const dataContractNamesInThisFile = [];
|
|
384
|
+
const newEndpointTemplates = routes.map((route) => {
|
|
385
|
+
const newEndpointTemplateData = newEndpointTmpl({
|
|
386
|
+
...params,
|
|
387
|
+
route
|
|
388
|
+
});
|
|
389
|
+
const { reservedDataContractNames } = newEndpointTemplateData;
|
|
390
|
+
const dataContactNames = new Set(
|
|
391
|
+
Object.keys(
|
|
392
|
+
configuration.config.swaggerSchema?.components?.schemas
|
|
393
|
+
).map((schemaName) => utils.formatModelName(schemaName))
|
|
394
|
+
);
|
|
395
|
+
reservedDataContractNames.forEach((reservedDataContractName) => {
|
|
396
|
+
if (!dataContactNames.has(reservedDataContractName)) {
|
|
397
|
+
dataContractNamesInThisFile.push(reservedDataContractName);
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
return { ...newEndpointTemplateData, route };
|
|
401
|
+
});
|
|
402
|
+
const extraImportLines = [];
|
|
403
|
+
const endpointTemplates = await Promise.all(
|
|
404
|
+
newEndpointTemplates.map(
|
|
405
|
+
async ({
|
|
406
|
+
content: requestInfoInstanceContent,
|
|
407
|
+
localModelTypes,
|
|
408
|
+
route
|
|
409
|
+
}) => {
|
|
410
|
+
const requestInfoMeta = codegenParams.getEndpointMeta?.(route, utils);
|
|
411
|
+
if (requestInfoMeta?.typeNameImportPath && requestInfoMeta.typeName) {
|
|
412
|
+
extraImportLines.push(
|
|
413
|
+
`import { ${requestInfoMeta.typeName} } from "${requestInfoMeta.typeNameImportPath}";`
|
|
414
|
+
);
|
|
415
|
+
}
|
|
416
|
+
return `
|
|
417
|
+
${(await Promise.all(
|
|
418
|
+
localModelTypes.map(async (modelType) => {
|
|
419
|
+
const contractType = await dataContractTmpl({
|
|
420
|
+
...params,
|
|
421
|
+
contract: modelType,
|
|
422
|
+
addExportKeyword: true
|
|
423
|
+
});
|
|
424
|
+
return contractType;
|
|
425
|
+
})
|
|
426
|
+
)).filter(Boolean).join("\n\n")}
|
|
427
|
+
|
|
428
|
+
${endpointJSDocTmpl({
|
|
429
|
+
...params,
|
|
430
|
+
route
|
|
431
|
+
})}
|
|
432
|
+
export const ${_.camelCase(route.routeName.usage)} = ${requestInfoInstanceContent}
|
|
433
|
+
`;
|
|
434
|
+
}
|
|
435
|
+
)
|
|
436
|
+
);
|
|
437
|
+
if (metaInfo) {
|
|
438
|
+
extraImportLines.push(
|
|
439
|
+
`import { ${[groupName && "Group", metaInfo?.namespace && "namespace", "Tag"].filter(Boolean).join(",")} } from "${groupName ? "../" : "./"}meta-info";`
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
return {
|
|
443
|
+
reservedDataContractNames: dataContractNamesInThisFile,
|
|
444
|
+
content: await formatTSContent(`${LINTERS_IGNORE}
|
|
445
|
+
import {
|
|
446
|
+
RequestParams,
|
|
447
|
+
HttpResponse,
|
|
448
|
+
HttpMultistatusResponse,
|
|
449
|
+
} from "${codegenParams.libImports?.["mobx-tanstack-query-api"] ?? "mobx-tanstack-query-api"}";
|
|
450
|
+
import { ${importFileParams.endpoint.exportName} } from "${importFileParams.endpoint.path}";
|
|
451
|
+
import { ${importFileParams.httpClient.exportName} } from "${importFileParams.httpClient.path}";
|
|
452
|
+
import { ${importFileParams.queryClient.exportName} } from "${importFileParams.queryClient.path}";
|
|
453
|
+
${extraImportLines.join("\n")}
|
|
454
|
+
|
|
455
|
+
${configuration.modelTypes.length > 0 ? `
|
|
456
|
+
import { ${configuration.modelTypes.map((it) => it.name).filter(
|
|
457
|
+
(it) => !dataContractNamesInThisFile.includes(it)
|
|
458
|
+
)} } from "${relativePathDataContracts}";
|
|
459
|
+
` : ""}
|
|
460
|
+
|
|
461
|
+
${(await Promise.all(
|
|
462
|
+
dataContractNamesInThisFile.map(async (dataContractName) => {
|
|
463
|
+
const modelType = configuration.modelTypes.find(
|
|
464
|
+
(modelType2) => modelType2.name === dataContractName
|
|
465
|
+
);
|
|
466
|
+
if (!modelType) {
|
|
467
|
+
return "";
|
|
468
|
+
}
|
|
469
|
+
const contractType = await dataContractTmpl({
|
|
470
|
+
...params,
|
|
471
|
+
contract: modelType,
|
|
472
|
+
addExportKeyword: true
|
|
473
|
+
});
|
|
474
|
+
return contractType;
|
|
475
|
+
})
|
|
476
|
+
)).filter(Boolean).join("\n\n")}
|
|
477
|
+
|
|
478
|
+
${endpointTemplates.filter(Boolean).join("\n\n")}
|
|
479
|
+
`)
|
|
480
|
+
};
|
|
481
|
+
};
|
|
482
|
+
const allExportsTmpl = async ({
|
|
483
|
+
collectedExportFiles,
|
|
484
|
+
metaInfo,
|
|
485
|
+
formatTSContent
|
|
486
|
+
}) => {
|
|
487
|
+
return await formatTSContent(`${LINTERS_IGNORE}
|
|
488
|
+
export * from './data-contracts';
|
|
489
|
+
${collectedExportFiles.map((fileName) => `export * from './${fileName}';`).join("\n")}
|
|
490
|
+
${metaInfo ? 'export * from "./meta-info";' : ""}
|
|
491
|
+
`);
|
|
492
|
+
};
|
|
493
|
+
const dataContractsFileTmpl = async (params) => {
|
|
494
|
+
const { config, modelTypes } = params.configuration;
|
|
495
|
+
const contractDefinitions = [];
|
|
496
|
+
if (config.internalTemplateOptions?.addUtilRequiredKeysType) {
|
|
497
|
+
contractDefinitions.push(
|
|
498
|
+
`type UtilRequiredKeys<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>`
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
for await (const contract of modelTypes) {
|
|
502
|
+
if (params.excludedDataContractNames?.includes(contract.name)) {
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
contractDefinitions.push(
|
|
506
|
+
await dataContractTmpl({
|
|
507
|
+
...params,
|
|
508
|
+
contract,
|
|
509
|
+
addExportKeyword: true
|
|
510
|
+
})
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
return await params.formatTSContent(`${LINTERS_IGNORE}
|
|
514
|
+
|
|
515
|
+
${contractDefinitions.length > 0 ? contractDefinitions.join("\n\n") : `export {}`}
|
|
516
|
+
`);
|
|
517
|
+
};
|
|
518
|
+
const endpointPerFileTmpl = async (params) => {
|
|
519
|
+
const {
|
|
520
|
+
route,
|
|
521
|
+
configuration,
|
|
522
|
+
codegenParams,
|
|
523
|
+
formatTSContent,
|
|
524
|
+
importFileParams,
|
|
525
|
+
utils,
|
|
526
|
+
relativePathDataContracts,
|
|
527
|
+
groupName,
|
|
528
|
+
metaInfo
|
|
529
|
+
} = params;
|
|
530
|
+
const { _ } = utils;
|
|
531
|
+
const {
|
|
532
|
+
content: requestInfoInstanceContent,
|
|
533
|
+
reservedDataContractNames,
|
|
534
|
+
localModelTypes
|
|
535
|
+
} = newEndpointTmpl({
|
|
536
|
+
...params,
|
|
537
|
+
route,
|
|
538
|
+
groupName,
|
|
539
|
+
metaInfo
|
|
540
|
+
});
|
|
541
|
+
const dataContactNames = new Set(
|
|
542
|
+
Object.keys(
|
|
543
|
+
configuration.config.swaggerSchema?.components?.schemas
|
|
544
|
+
).map((schemaName) => utils.formatModelName(schemaName))
|
|
545
|
+
);
|
|
546
|
+
const dataContractNamesInThisFile = [];
|
|
547
|
+
reservedDataContractNames.forEach((reservedDataContractName) => {
|
|
548
|
+
if (!dataContactNames.has(reservedDataContractName)) {
|
|
549
|
+
dataContractNamesInThisFile.push(reservedDataContractName);
|
|
550
|
+
}
|
|
551
|
+
});
|
|
552
|
+
const extraImportLines = [];
|
|
553
|
+
if (metaInfo) {
|
|
554
|
+
extraImportLines.push(
|
|
555
|
+
`import { ${[groupName && "Group", metaInfo?.namespace && "namespace", "Tag"].filter(Boolean).join(",")} } from "../${groupName ? "../" : ""}meta-info";`
|
|
556
|
+
);
|
|
557
|
+
}
|
|
558
|
+
const requestInfoMeta = codegenParams.getEndpointMeta?.(route, utils);
|
|
559
|
+
if (requestInfoMeta?.typeNameImportPath && requestInfoMeta.typeName) {
|
|
560
|
+
extraImportLines.push(
|
|
561
|
+
`import { ${requestInfoMeta.typeName} } from "${requestInfoMeta.typeNameImportPath}";`
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
return {
|
|
565
|
+
reservedDataContractNames: dataContractNamesInThisFile,
|
|
566
|
+
content: await formatTSContent(`${LINTERS_IGNORE}
|
|
567
|
+
import {
|
|
568
|
+
RequestParams,
|
|
569
|
+
HttpResponse,
|
|
570
|
+
HttpMultistatusResponse,
|
|
571
|
+
} from "${codegenParams.libImports?.["mobx-tanstack-query-api"] ?? "mobx-tanstack-query-api"}";
|
|
572
|
+
import { ${importFileParams.endpoint.exportName} } from "${importFileParams.endpoint.path}";
|
|
573
|
+
import { ${importFileParams.httpClient.exportName} } from "${importFileParams.httpClient.path}";
|
|
574
|
+
import { ${importFileParams.queryClient.exportName} } from "${importFileParams.queryClient.path}";
|
|
575
|
+
${extraImportLines.join("\n")}
|
|
576
|
+
|
|
577
|
+
${configuration.modelTypes.length > 0 ? `
|
|
578
|
+
import { ${configuration.modelTypes.map((it) => it.name).filter(
|
|
579
|
+
(it) => !dataContractNamesInThisFile.includes(it)
|
|
580
|
+
)} } from "${relativePathDataContracts}";
|
|
581
|
+
` : ""}
|
|
582
|
+
|
|
583
|
+
${(await Promise.all(
|
|
584
|
+
dataContractNamesInThisFile.map(async (dataContractName) => {
|
|
585
|
+
const modelType = configuration.modelTypes.find(
|
|
586
|
+
(modelType2) => modelType2.name === dataContractName
|
|
587
|
+
);
|
|
588
|
+
if (!modelType) {
|
|
589
|
+
return "";
|
|
590
|
+
}
|
|
591
|
+
const contractType = await dataContractTmpl({
|
|
592
|
+
...params,
|
|
593
|
+
contract: modelType,
|
|
594
|
+
addExportKeyword: true
|
|
595
|
+
});
|
|
596
|
+
return contractType;
|
|
597
|
+
})
|
|
598
|
+
)).filter(Boolean).join("\n\n")}
|
|
599
|
+
|
|
600
|
+
${(await Promise.all(
|
|
601
|
+
localModelTypes.map(async (modelType) => {
|
|
602
|
+
const contractType = await dataContractTmpl({
|
|
603
|
+
...params,
|
|
604
|
+
contract: modelType,
|
|
605
|
+
addExportKeyword: true
|
|
606
|
+
});
|
|
607
|
+
return contractType;
|
|
608
|
+
})
|
|
609
|
+
)).filter(Boolean).join("\n\n")}
|
|
610
|
+
|
|
611
|
+
${endpointJSDocTmpl({
|
|
612
|
+
...params,
|
|
613
|
+
route
|
|
614
|
+
})}
|
|
615
|
+
export const ${_.camelCase(route.routeName.usage)} = ${requestInfoInstanceContent}
|
|
616
|
+
`)
|
|
617
|
+
};
|
|
618
|
+
};
|
|
619
|
+
const indexTsForEndpointPerFileTmpl = async ({
|
|
620
|
+
generatedRequestFileNames
|
|
621
|
+
}) => {
|
|
622
|
+
return `${LINTERS_IGNORE}
|
|
623
|
+
${generatedRequestFileNames.map((fileName) => `export * from './${fileName.replace(".ts", "")}';`).join("\n")}
|
|
624
|
+
`;
|
|
625
|
+
};
|
|
626
|
+
const unpackFilterOption = (option, argsToString, defaultReturnValue = true) => {
|
|
627
|
+
if (typeof option === "function") {
|
|
628
|
+
return option;
|
|
629
|
+
}
|
|
630
|
+
if (option == null) {
|
|
631
|
+
return () => defaultReturnValue;
|
|
632
|
+
}
|
|
633
|
+
const inputs = Array.isArray(option) ? option : [option];
|
|
634
|
+
return (...args) => inputs.some((input) => {
|
|
635
|
+
const str = argsToString(...args);
|
|
636
|
+
if (typeof input === "string") {
|
|
637
|
+
return input === str;
|
|
638
|
+
}
|
|
639
|
+
return input.test(str);
|
|
640
|
+
});
|
|
641
|
+
};
|
|
642
|
+
const removeUnusedTypesItteration = async ({
|
|
643
|
+
directory,
|
|
644
|
+
keepTypes
|
|
645
|
+
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
|
|
646
|
+
}) => {
|
|
647
|
+
const project = new Project();
|
|
648
|
+
project.addSourceFilesAtPaths([
|
|
649
|
+
path.join(directory, "**/*.ts"),
|
|
650
|
+
path.join(directory, "**/*.tsx")
|
|
651
|
+
]);
|
|
652
|
+
const dataContractsSourceFile = project.getSourceFile(
|
|
653
|
+
(sourceFile) => sourceFile.getFilePath().includes(`${directory}/data-contracts.ts`)
|
|
654
|
+
);
|
|
655
|
+
if (!dataContractsSourceFile) return;
|
|
656
|
+
const exportedDeclarations = dataContractsSourceFile.getExportedDeclarations();
|
|
657
|
+
const candidateTypes = /* @__PURE__ */ new Map();
|
|
658
|
+
for (const [name, declarations] of exportedDeclarations) {
|
|
659
|
+
const validDeclarations = declarations.filter(
|
|
660
|
+
(decl) => decl.getKind() === SyntaxKind.InterfaceDeclaration || decl.getKind() === SyntaxKind.TypeAliasDeclaration || decl.getKind() === SyntaxKind.EnumDeclaration
|
|
661
|
+
);
|
|
662
|
+
if (validDeclarations.length > 0) {
|
|
663
|
+
candidateTypes.set(name, validDeclarations);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
if (candidateTypes.size === 0) return;
|
|
667
|
+
const usedTypes = /* @__PURE__ */ new Set();
|
|
668
|
+
const externalFiles = project.getSourceFiles().filter((sf) => sf !== dataContractsSourceFile);
|
|
669
|
+
for (const file of externalFiles) {
|
|
670
|
+
const identifiers = file.getDescendantsOfKind(SyntaxKind.Identifier);
|
|
671
|
+
for (const identifier of identifiers) {
|
|
672
|
+
const name = identifier.getText();
|
|
673
|
+
if (candidateTypes.has(name)) {
|
|
674
|
+
usedTypes.add(name);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
const memberAccessExpressions = file.getDescendantsOfKind(
|
|
678
|
+
SyntaxKind.PropertyAccessExpression
|
|
679
|
+
);
|
|
680
|
+
for (const expr of memberAccessExpressions) {
|
|
681
|
+
const expression = expr.getExpression();
|
|
682
|
+
if (Node.isIdentifier(expression)) {
|
|
683
|
+
const name = expression.getText();
|
|
684
|
+
if (candidateTypes.has(name)) {
|
|
685
|
+
usedTypes.add(name);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
const dependencyGraph = /* @__PURE__ */ new Map();
|
|
691
|
+
for (const [name, declarations] of candidateTypes) {
|
|
692
|
+
const dependencies = /* @__PURE__ */ new Set();
|
|
693
|
+
for (const decl of declarations) {
|
|
694
|
+
const typeReferences = decl.getDescendantsOfKind(
|
|
695
|
+
SyntaxKind.TypeReference
|
|
696
|
+
);
|
|
697
|
+
for (const ref of typeReferences) {
|
|
698
|
+
const typeName = ref.getTypeName().getText();
|
|
699
|
+
if (candidateTypes.has(typeName)) {
|
|
700
|
+
dependencies.add(typeName);
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
if (decl.getKind() === SyntaxKind.EnumDeclaration) {
|
|
704
|
+
const initializers = decl.getDescendantsOfKind(SyntaxKind.Identifier);
|
|
705
|
+
for (const init of initializers) {
|
|
706
|
+
const text = init.getText();
|
|
707
|
+
if (candidateTypes.has(text)) {
|
|
708
|
+
dependencies.add(text);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
dependencyGraph.set(name, dependencies);
|
|
714
|
+
}
|
|
715
|
+
const queue = Array.from(usedTypes);
|
|
716
|
+
const visited = new Set(usedTypes);
|
|
717
|
+
while (queue.length > 0) {
|
|
718
|
+
const current = queue.shift();
|
|
719
|
+
if (dependencyGraph.has(current)) {
|
|
720
|
+
for (const dep of dependencyGraph.get(current)) {
|
|
721
|
+
if (!visited.has(dep)) {
|
|
722
|
+
visited.add(dep);
|
|
723
|
+
usedTypes.add(dep);
|
|
724
|
+
queue.push(dep);
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
let removedCount = 0;
|
|
730
|
+
const isNeedToRemoveType = unpackFilterOption(
|
|
731
|
+
keepTypes,
|
|
732
|
+
(name) => name,
|
|
733
|
+
false
|
|
734
|
+
);
|
|
735
|
+
for (const [name, declarations] of candidateTypes) {
|
|
736
|
+
if (usedTypes.has(name)) continue;
|
|
737
|
+
for (const decl of declarations) {
|
|
738
|
+
if ("remove" in decl && isNeedToRemoveType(name)) {
|
|
739
|
+
decl.remove();
|
|
740
|
+
removedCount++;
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
if (removedCount > 0) {
|
|
745
|
+
await dataContractsSourceFile.save();
|
|
746
|
+
}
|
|
747
|
+
return removedCount;
|
|
748
|
+
};
|
|
749
|
+
const removeUnusedTypes = async (params) => {
|
|
750
|
+
while (true) {
|
|
751
|
+
const removedCount = await removeUnusedTypesItteration(params) ?? 0;
|
|
752
|
+
if (removedCount === 0) break;
|
|
753
|
+
}
|
|
754
|
+
};
|
|
755
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
756
|
+
const __dirname = path.dirname(__filename);
|
|
757
|
+
const __execdirname = process.cwd();
|
|
758
|
+
const generateApi = async (params) => {
|
|
759
|
+
if (Array.isArray(params)) {
|
|
760
|
+
for await (const param of params) {
|
|
761
|
+
await generateApi(param);
|
|
762
|
+
}
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
const tsconfigPath = params.tsconfigPath ? path.resolve(__execdirname, params.tsconfigPath) : path.resolve(__execdirname, "./tsconfig.json");
|
|
766
|
+
console.info("using tsconfig", tsconfigPath);
|
|
767
|
+
const importFileParams = {
|
|
768
|
+
queryClient: !params.queryClient || typeof params.queryClient === "string" ? {
|
|
769
|
+
exportName: "queryClient",
|
|
770
|
+
path: "mobx-tanstack-query-api/builtin"
|
|
771
|
+
} : params.queryClient,
|
|
772
|
+
endpoint: !params.endpoint || typeof params.endpoint === "string" ? {
|
|
773
|
+
exportName: "Endpoint",
|
|
774
|
+
path: "mobx-tanstack-query-api"
|
|
775
|
+
} : params.endpoint,
|
|
776
|
+
httpClient: !params.httpClient || typeof params.httpClient === "string" ? {
|
|
777
|
+
exportName: "http",
|
|
778
|
+
path: "mobx-tanstack-query-api/builtin"
|
|
779
|
+
} : params.httpClient
|
|
780
|
+
};
|
|
781
|
+
const paths = {
|
|
782
|
+
templates: path.resolve(__dirname, "templates"),
|
|
783
|
+
requestInfoClass: path.resolve(
|
|
784
|
+
__dirname,
|
|
785
|
+
"templates/request-info-class.ejs"
|
|
786
|
+
),
|
|
787
|
+
httpClient: path.resolve(__dirname, "templates/http-client.ejs"),
|
|
788
|
+
createRequestInfoInstance: path.resolve(
|
|
789
|
+
__dirname,
|
|
790
|
+
"templates/create-request-info-instance.ejs"
|
|
791
|
+
),
|
|
792
|
+
outputDir: path.resolve(process.cwd(), params.output)
|
|
793
|
+
};
|
|
794
|
+
const swaggerTypescriptApiCodegenBaseParams = {
|
|
795
|
+
httpClientType: "fetch",
|
|
796
|
+
cleanOutput: params.cleanOutput ?? true,
|
|
797
|
+
modular: true,
|
|
798
|
+
patch: true,
|
|
799
|
+
typeSuffix: "DC",
|
|
800
|
+
disableStrictSSL: false,
|
|
801
|
+
singleHttpClient: true,
|
|
802
|
+
extractRequestBody: true,
|
|
803
|
+
extractRequestParams: false,
|
|
804
|
+
extractResponseBody: true,
|
|
805
|
+
extractResponseError: true,
|
|
806
|
+
generateResponses: true,
|
|
807
|
+
generateClient: false,
|
|
808
|
+
addReadonly: true,
|
|
809
|
+
moduleNameFirstTag: true,
|
|
810
|
+
sortTypes: true,
|
|
811
|
+
templates: paths.templates.toString(),
|
|
812
|
+
primitiveTypeConstructs: (constructs) => {
|
|
813
|
+
return {
|
|
814
|
+
...constructs,
|
|
815
|
+
object: () => `Record<string, any>`,
|
|
816
|
+
float: () => `number`,
|
|
817
|
+
...params.otherCodegenParams?.primitiveTypeConstructs?.(constructs)
|
|
818
|
+
};
|
|
819
|
+
},
|
|
820
|
+
requestOptions: params.fetchSchemaRequestOptions,
|
|
821
|
+
...params.otherCodegenParams
|
|
822
|
+
};
|
|
823
|
+
let codegenProcess;
|
|
824
|
+
if (!params.input) {
|
|
825
|
+
console.warn(
|
|
826
|
+
"[mobx-tanstack-query-api/codegen]",
|
|
827
|
+
"input is not specified",
|
|
828
|
+
"\nprocess will be skipped"
|
|
829
|
+
);
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
832
|
+
const inputToCodegenInput = (input) => {
|
|
833
|
+
const inputData = {};
|
|
834
|
+
if (typeof input === "string") {
|
|
835
|
+
inputData.input = input;
|
|
836
|
+
inputData.url = input;
|
|
837
|
+
} else {
|
|
838
|
+
inputData.spec = input;
|
|
839
|
+
}
|
|
840
|
+
return inputData;
|
|
841
|
+
};
|
|
842
|
+
let mixinSwaggerSchema = null;
|
|
843
|
+
if (params.mixinInput) {
|
|
844
|
+
await generateApi$1({
|
|
845
|
+
...swaggerTypescriptApiCodegenBaseParams,
|
|
846
|
+
...inputToCodegenInput(params.mixinInput),
|
|
847
|
+
hooks: {
|
|
848
|
+
onInit: (configuration) => {
|
|
849
|
+
mixinSwaggerSchema = cloneDeep(configuration.swaggerSchema);
|
|
850
|
+
},
|
|
851
|
+
onPrepareConfig: (config) => {
|
|
852
|
+
config.routes.combined?.forEach((routeInfo) => {
|
|
853
|
+
routeInfo.routes.sort(
|
|
854
|
+
(routeA, routeB) => routeA.routeName.usage.localeCompare(routeB.routeName.usage)
|
|
855
|
+
);
|
|
856
|
+
});
|
|
857
|
+
},
|
|
858
|
+
onFormatRouteName: (routeInfo, usageRouteName) => {
|
|
859
|
+
let formattedRouteName = usageRouteName;
|
|
860
|
+
if (params.addPathSegmentToRouteName === true || typeof params.addPathSegmentToRouteName === "number") {
|
|
861
|
+
const pathSegmentForSuffix = typeof params.addPathSegmentToRouteName === "number" ? params.addPathSegmentToRouteName : 0;
|
|
862
|
+
const pathSegments = routeInfo.route.split("/").filter(Boolean);
|
|
863
|
+
const { _: _2 } = codegenProcess.getRenderTemplateData().utils;
|
|
864
|
+
formattedRouteName = _2.camelCase(
|
|
865
|
+
`${pathSegments[pathSegmentForSuffix] || ""}_${formattedRouteName}`
|
|
866
|
+
);
|
|
867
|
+
}
|
|
868
|
+
const endpointName = formattedRouteName;
|
|
869
|
+
return params?.formatEndpointName?.(endpointName, routeInfo) ?? swaggerTypescriptApiCodegenBaseParams?.hooks?.onFormatRouteName?.(
|
|
870
|
+
routeInfo,
|
|
871
|
+
endpointName
|
|
872
|
+
) ?? endpointName;
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
});
|
|
876
|
+
}
|
|
877
|
+
const generated = await generateApi$1({
|
|
878
|
+
...swaggerTypescriptApiCodegenBaseParams,
|
|
879
|
+
...inputToCodegenInput(params.input),
|
|
880
|
+
hooks: {
|
|
881
|
+
onInit: (configuration, codeGenProcessFromInit) => {
|
|
882
|
+
codegenProcess = codeGenProcessFromInit;
|
|
883
|
+
const resultSwaggerSchema = configuration.swaggerSchema;
|
|
884
|
+
resultSwaggerSchema.components = resultSwaggerSchema.components || {};
|
|
885
|
+
resultSwaggerSchema.components.schemas = resultSwaggerSchema.components.schemas || {};
|
|
886
|
+
resultSwaggerSchema.paths = {
|
|
887
|
+
...resultSwaggerSchema.paths,
|
|
888
|
+
...mixinSwaggerSchema?.paths
|
|
889
|
+
};
|
|
890
|
+
resultSwaggerSchema.components.schemas = {
|
|
891
|
+
...resultSwaggerSchema.components.schemas,
|
|
892
|
+
...mixinSwaggerSchema?.components?.schemas
|
|
893
|
+
};
|
|
894
|
+
return swaggerTypescriptApiCodegenBaseParams?.hooks?.onInit?.(
|
|
895
|
+
configuration,
|
|
896
|
+
codeGenProcessFromInit
|
|
897
|
+
);
|
|
898
|
+
},
|
|
899
|
+
onPrepareConfig: (config) => {
|
|
900
|
+
config.routes.combined?.forEach((routeInfo) => {
|
|
901
|
+
routeInfo.routes.sort(
|
|
902
|
+
(routeA, routeB) => routeA.routeName.usage.localeCompare(routeB.routeName.usage)
|
|
903
|
+
);
|
|
904
|
+
});
|
|
905
|
+
return swaggerTypescriptApiCodegenBaseParams?.hooks?.onPrepareConfig?.(
|
|
906
|
+
config
|
|
907
|
+
);
|
|
908
|
+
},
|
|
909
|
+
onFormatRouteName: (routeInfo, usageRouteName) => {
|
|
910
|
+
let formattedRouteName = usageRouteName;
|
|
911
|
+
if (params.addPathSegmentToRouteName === true || typeof params.addPathSegmentToRouteName === "number") {
|
|
912
|
+
const pathSegmentForSuffix = typeof params.addPathSegmentToRouteName === "number" ? params.addPathSegmentToRouteName : 0;
|
|
913
|
+
const pathSegments = routeInfo.route.split("/").filter(Boolean);
|
|
914
|
+
const { _: _2 } = codegenProcess.getRenderTemplateData().utils;
|
|
915
|
+
formattedRouteName = _2.camelCase(
|
|
916
|
+
`${pathSegments[pathSegmentForSuffix] || ""}_${formattedRouteName}`
|
|
917
|
+
);
|
|
918
|
+
}
|
|
919
|
+
const endpointName = formattedRouteName;
|
|
920
|
+
return params?.formatEndpointName?.(endpointName, routeInfo) ?? swaggerTypescriptApiCodegenBaseParams?.hooks?.onFormatRouteName?.(
|
|
921
|
+
routeInfo,
|
|
922
|
+
endpointName
|
|
923
|
+
) ?? endpointName;
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
});
|
|
927
|
+
const utils = codegenProcess.getRenderTemplateData().utils;
|
|
928
|
+
const { _ } = utils;
|
|
929
|
+
let namespace = null;
|
|
930
|
+
if (params.namespace) {
|
|
931
|
+
if (typeof params.namespace === "function") {
|
|
932
|
+
namespace = params.namespace(utils);
|
|
933
|
+
} else {
|
|
934
|
+
namespace = utils._.camelCase(params.namespace);
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
const codegenFs = codegenProcess.fileSystem;
|
|
938
|
+
codegenFs.cleanDir(params.output);
|
|
939
|
+
codegenFs.createDir(params.output);
|
|
940
|
+
const filterTypes = unpackFilterOption(
|
|
941
|
+
params.filterTypes,
|
|
942
|
+
(modelType) => modelType.name
|
|
943
|
+
);
|
|
944
|
+
generated.configuration.modelTypes = generated.configuration.modelTypes.filter(
|
|
945
|
+
(modelType) => filterTypes(modelType)
|
|
946
|
+
);
|
|
947
|
+
const allRoutes = Object.values(generated.configuration.routes).flat().flatMap(
|
|
948
|
+
(routeGroup) => "routes" in routeGroup ? routeGroup.routes : routeGroup
|
|
949
|
+
);
|
|
950
|
+
const filterEndpoint = unpackFilterOption(
|
|
951
|
+
params.filterEndpoints,
|
|
952
|
+
(route) => route.raw?.operationId || ""
|
|
953
|
+
);
|
|
954
|
+
const baseTmplParams = {
|
|
955
|
+
...generated,
|
|
956
|
+
codegenParams: params,
|
|
957
|
+
configuration: generated.configuration,
|
|
958
|
+
formatTSContent: generated.formatTSContent,
|
|
959
|
+
codegenProcess,
|
|
960
|
+
importFileParams,
|
|
961
|
+
utils,
|
|
962
|
+
filterTypes
|
|
963
|
+
};
|
|
964
|
+
const reservedDataContractNamesMap = /* @__PURE__ */ new Map();
|
|
965
|
+
const collectedExportFilesFromIndexFile = [];
|
|
966
|
+
const groupsMap = /* @__PURE__ */ new Map();
|
|
967
|
+
const nonEmptyGroups = /* @__PURE__ */ new Set();
|
|
968
|
+
const tagsSet = /* @__PURE__ */ new Set();
|
|
969
|
+
if (params.groupBy == null) {
|
|
970
|
+
collectedExportFilesFromIndexFile.push("endpoints");
|
|
971
|
+
if (params.outputType === "one-endpoint-per-file") {
|
|
972
|
+
codegenFs.createDir(path.resolve(params.output, "endpoints"));
|
|
973
|
+
const fileNamesWithRequestInfo = [];
|
|
974
|
+
for await (const route of allRoutes) {
|
|
975
|
+
const {
|
|
976
|
+
content: requestInfoPerFileContent,
|
|
977
|
+
reservedDataContractNames
|
|
978
|
+
} = await endpointPerFileTmpl({
|
|
979
|
+
...baseTmplParams,
|
|
980
|
+
route,
|
|
981
|
+
relativePathDataContracts: "../data-contracts",
|
|
982
|
+
groupName: null,
|
|
983
|
+
metaInfo: params.noMetaInfo ? null : {
|
|
984
|
+
groupNames: [],
|
|
985
|
+
namespace
|
|
986
|
+
}
|
|
987
|
+
});
|
|
988
|
+
if (Array.isArray(route.raw.tags)) {
|
|
989
|
+
route.raw.tags.forEach((tag) => {
|
|
990
|
+
tagsSet.add(tag);
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
reservedDataContractNames.forEach((name) => {
|
|
994
|
+
reservedDataContractNamesMap.set(
|
|
995
|
+
name,
|
|
996
|
+
(reservedDataContractNamesMap.get(name) ?? 0) + 1
|
|
997
|
+
);
|
|
998
|
+
});
|
|
999
|
+
if (!filterEndpoint(route)) {
|
|
1000
|
+
continue;
|
|
1001
|
+
}
|
|
1002
|
+
const fileName = `${_.kebabCase(route.routeName.usage)}.ts`;
|
|
1003
|
+
fileNamesWithRequestInfo.push(fileName);
|
|
1004
|
+
codegenFs.createFile({
|
|
1005
|
+
path: path.resolve(params.output, "endpoints"),
|
|
1006
|
+
fileName,
|
|
1007
|
+
withPrefix: false,
|
|
1008
|
+
content: requestInfoPerFileContent
|
|
1009
|
+
});
|
|
1010
|
+
}
|
|
1011
|
+
codegenFs.createFile({
|
|
1012
|
+
path: path.resolve(params.output, "endpoints"),
|
|
1013
|
+
fileName: "index.ts",
|
|
1014
|
+
withPrefix: false,
|
|
1015
|
+
content: await indexTsForEndpointPerFileTmpl({
|
|
1016
|
+
generatedRequestFileNames: fileNamesWithRequestInfo
|
|
1017
|
+
})
|
|
1018
|
+
});
|
|
1019
|
+
} else {
|
|
1020
|
+
const { content: requestInfoPerFileContent, reservedDataContractNames } = await allEndpointPerFileTmpl({
|
|
1021
|
+
...baseTmplParams,
|
|
1022
|
+
routes: allRoutes,
|
|
1023
|
+
relativePathDataContracts: "./data-contracts",
|
|
1024
|
+
groupName: null,
|
|
1025
|
+
metaInfo: params.noMetaInfo ? null : {
|
|
1026
|
+
namespace,
|
|
1027
|
+
groupNames: []
|
|
1028
|
+
}
|
|
1029
|
+
});
|
|
1030
|
+
reservedDataContractNames.forEach((name) => {
|
|
1031
|
+
reservedDataContractNamesMap.set(
|
|
1032
|
+
name,
|
|
1033
|
+
(reservedDataContractNamesMap.get(name) ?? 0) + 1
|
|
1034
|
+
);
|
|
1035
|
+
});
|
|
1036
|
+
const filteredRoutes = allRoutes.filter(filterEndpoint);
|
|
1037
|
+
const hasFilteredRoutes = filteredRoutes.length > 0;
|
|
1038
|
+
if (hasFilteredRoutes) {
|
|
1039
|
+
filteredRoutes.forEach((route) => {
|
|
1040
|
+
if (Array.isArray(route.raw.tags)) {
|
|
1041
|
+
route.raw.tags.forEach((tag) => {
|
|
1042
|
+
tagsSet.add(tag);
|
|
1043
|
+
});
|
|
1044
|
+
}
|
|
1045
|
+
});
|
|
1046
|
+
const fileName = "endpoints.ts";
|
|
1047
|
+
collectedExportFilesFromIndexFile.push("endpoints");
|
|
1048
|
+
codegenFs.createFile({
|
|
1049
|
+
path: params.output,
|
|
1050
|
+
fileName,
|
|
1051
|
+
withPrefix: false,
|
|
1052
|
+
content: requestInfoPerFileContent
|
|
1053
|
+
});
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
} else {
|
|
1057
|
+
allRoutes.forEach((route) => {
|
|
1058
|
+
let group;
|
|
1059
|
+
if (typeof params.groupBy === "function") {
|
|
1060
|
+
group = params.groupBy(route);
|
|
1061
|
+
} else if (params.groupBy?.includes("path-segment")) {
|
|
1062
|
+
const segmentIndex = +params.groupBy.replaceAll(/path-segment-?/g, "") || 0;
|
|
1063
|
+
group = route.request.path?.split("/")?.filter(Boolean)?.[segmentIndex] || void 0;
|
|
1064
|
+
} else if (params.groupBy?.includes("tag")) {
|
|
1065
|
+
const tagIndex = +params.groupBy.replaceAll(/tag-?/g, "") || 0;
|
|
1066
|
+
group = route.raw?.tags?.[tagIndex] ?? void 0;
|
|
1067
|
+
}
|
|
1068
|
+
if (group == null) {
|
|
1069
|
+
group = "other";
|
|
1070
|
+
}
|
|
1071
|
+
if (!groupsMap.has(group)) {
|
|
1072
|
+
groupsMap.set(group, []);
|
|
1073
|
+
}
|
|
1074
|
+
groupsMap.get(group)?.push(route);
|
|
1075
|
+
});
|
|
1076
|
+
const filterGroups = unpackFilterOption(
|
|
1077
|
+
params.filterGroups,
|
|
1078
|
+
(groupName) => groupName
|
|
1079
|
+
);
|
|
1080
|
+
for await (const [groupName, routes] of groupsMap) {
|
|
1081
|
+
if (!filterGroups(groupName)) {
|
|
1082
|
+
continue;
|
|
1083
|
+
}
|
|
1084
|
+
const fileNamesWithRequestInfo = [];
|
|
1085
|
+
const groupDirectory = path.resolve(
|
|
1086
|
+
params.output,
|
|
1087
|
+
_.kebabCase(groupName)
|
|
1088
|
+
);
|
|
1089
|
+
codegenFs.createDir(groupDirectory);
|
|
1090
|
+
let hasFilteredRoutes = false;
|
|
1091
|
+
if (params.outputType === "one-endpoint-per-file") {
|
|
1092
|
+
codegenFs.createDir(path.resolve(groupDirectory, "endpoints"));
|
|
1093
|
+
for await (const route of routes) {
|
|
1094
|
+
const {
|
|
1095
|
+
content: requestInfoPerFileContent,
|
|
1096
|
+
reservedDataContractNames
|
|
1097
|
+
} = await endpointPerFileTmpl({
|
|
1098
|
+
...baseTmplParams,
|
|
1099
|
+
route,
|
|
1100
|
+
relativePathDataContracts: "../../data-contracts",
|
|
1101
|
+
groupName,
|
|
1102
|
+
metaInfo: params.noMetaInfo ? null : {
|
|
1103
|
+
namespace,
|
|
1104
|
+
groupNames: []
|
|
1105
|
+
}
|
|
1106
|
+
});
|
|
1107
|
+
reservedDataContractNames.forEach((name) => {
|
|
1108
|
+
reservedDataContractNamesMap.set(
|
|
1109
|
+
name,
|
|
1110
|
+
(reservedDataContractNamesMap.get(name) ?? 0) + 1
|
|
1111
|
+
);
|
|
1112
|
+
});
|
|
1113
|
+
if (!filterEndpoint(route)) {
|
|
1114
|
+
continue;
|
|
1115
|
+
}
|
|
1116
|
+
hasFilteredRoutes = true;
|
|
1117
|
+
if (Array.isArray(route.raw.tags)) {
|
|
1118
|
+
route.raw.tags.forEach((tag) => {
|
|
1119
|
+
tagsSet.add(tag);
|
|
1120
|
+
});
|
|
1121
|
+
}
|
|
1122
|
+
const fileName = `${_.kebabCase(route.routeName.usage)}.ts`;
|
|
1123
|
+
fileNamesWithRequestInfo.push(fileName);
|
|
1124
|
+
codegenFs.createFile({
|
|
1125
|
+
path: path.resolve(
|
|
1126
|
+
params.output,
|
|
1127
|
+
_.kebabCase(groupName),
|
|
1128
|
+
"endpoints"
|
|
1129
|
+
),
|
|
1130
|
+
fileName,
|
|
1131
|
+
withPrefix: false,
|
|
1132
|
+
content: requestInfoPerFileContent
|
|
1133
|
+
});
|
|
1134
|
+
}
|
|
1135
|
+
} else {
|
|
1136
|
+
const {
|
|
1137
|
+
content: requestInfoPerFileContent,
|
|
1138
|
+
reservedDataContractNames
|
|
1139
|
+
} = await allEndpointPerFileTmpl({
|
|
1140
|
+
...baseTmplParams,
|
|
1141
|
+
routes,
|
|
1142
|
+
relativePathDataContracts: "../data-contracts",
|
|
1143
|
+
groupName,
|
|
1144
|
+
metaInfo: params.noMetaInfo ? null : {
|
|
1145
|
+
namespace,
|
|
1146
|
+
groupNames: []
|
|
1147
|
+
}
|
|
1148
|
+
});
|
|
1149
|
+
reservedDataContractNames.forEach((name) => {
|
|
1150
|
+
reservedDataContractNamesMap.set(
|
|
1151
|
+
name,
|
|
1152
|
+
(reservedDataContractNamesMap.get(name) ?? 0) + 1
|
|
1153
|
+
);
|
|
1154
|
+
});
|
|
1155
|
+
const filteredRoutes = routes.filter(filterEndpoint);
|
|
1156
|
+
hasFilteredRoutes = filteredRoutes.length > 0;
|
|
1157
|
+
if (hasFilteredRoutes) {
|
|
1158
|
+
filteredRoutes.forEach((route) => {
|
|
1159
|
+
if (Array.isArray(route.raw.tags)) {
|
|
1160
|
+
route.raw.tags.forEach((tag) => {
|
|
1161
|
+
tagsSet.add(tag);
|
|
1162
|
+
});
|
|
1163
|
+
}
|
|
1164
|
+
});
|
|
1165
|
+
const fileName = "endpoints.ts";
|
|
1166
|
+
fileNamesWithRequestInfo.push(fileName);
|
|
1167
|
+
codegenFs.createFile({
|
|
1168
|
+
path: groupDirectory,
|
|
1169
|
+
fileName,
|
|
1170
|
+
withPrefix: false,
|
|
1171
|
+
content: requestInfoPerFileContent
|
|
1172
|
+
});
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
if (hasFilteredRoutes) {
|
|
1176
|
+
nonEmptyGroups.add(groupName);
|
|
1177
|
+
const exportGroupName = params.formatExportGroupName ? params.formatExportGroupName(_.camelCase(groupName), utils) : _.camelCase(groupName);
|
|
1178
|
+
codegenFs.createFile({
|
|
1179
|
+
path: groupDirectory,
|
|
1180
|
+
fileName: "index.ts",
|
|
1181
|
+
withPrefix: false,
|
|
1182
|
+
content: `${LINTERS_IGNORE}
|
|
1183
|
+
export * as ${exportGroupName} from './endpoints';
|
|
1184
|
+
`
|
|
1185
|
+
});
|
|
1186
|
+
if (params.outputType === "one-endpoint-per-file") {
|
|
1187
|
+
codegenFs.createFile({
|
|
1188
|
+
path: path.resolve(groupDirectory, "endpoints"),
|
|
1189
|
+
fileName: "index.ts",
|
|
1190
|
+
withPrefix: false,
|
|
1191
|
+
content: await indexTsForEndpointPerFileTmpl({
|
|
1192
|
+
generatedRequestFileNames: fileNamesWithRequestInfo
|
|
1193
|
+
})
|
|
1194
|
+
});
|
|
1195
|
+
}
|
|
1196
|
+
collectedExportFilesFromIndexFile.push(_.kebabCase(groupName));
|
|
1197
|
+
} else {
|
|
1198
|
+
codegenFs.removeDir(
|
|
1199
|
+
path.resolve(params.output, _.kebabCase(groupName))
|
|
1200
|
+
);
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
const metaInfo = !params.noMetaInfo && (namespace ?? (nonEmptyGroups.size > 0 || tagsSet.size > 0)) ? {
|
|
1205
|
+
namespace,
|
|
1206
|
+
groupNames: [...nonEmptyGroups.values()],
|
|
1207
|
+
tags: [...tagsSet.values()]
|
|
1208
|
+
} : null;
|
|
1209
|
+
const excludedDataContractNames = Array.from(
|
|
1210
|
+
reservedDataContractNamesMap.entries()
|
|
1211
|
+
).filter(([_2, count]) => count === 1).map(([name]) => name);
|
|
1212
|
+
const dataContractsContent = await dataContractsFileTmpl({
|
|
1213
|
+
...baseTmplParams,
|
|
1214
|
+
excludedDataContractNames
|
|
1215
|
+
});
|
|
1216
|
+
codegenFs.createFile({
|
|
1217
|
+
path: paths.outputDir,
|
|
1218
|
+
fileName: "data-contracts.ts",
|
|
1219
|
+
withPrefix: false,
|
|
1220
|
+
content: dataContractsContent
|
|
1221
|
+
});
|
|
1222
|
+
if (metaInfo) {
|
|
1223
|
+
codegenFs.createFile({
|
|
1224
|
+
path: paths.outputDir,
|
|
1225
|
+
fileName: "meta-info.ts",
|
|
1226
|
+
withPrefix: false,
|
|
1227
|
+
content: await metaInfoTmpl({
|
|
1228
|
+
...baseTmplParams,
|
|
1229
|
+
metaInfo
|
|
1230
|
+
})
|
|
1231
|
+
});
|
|
1232
|
+
}
|
|
1233
|
+
if (namespace) {
|
|
1234
|
+
codegenFs.createFile({
|
|
1235
|
+
path: paths.outputDir,
|
|
1236
|
+
fileName: "__exports.ts",
|
|
1237
|
+
withPrefix: false,
|
|
1238
|
+
content: await allExportsTmpl({
|
|
1239
|
+
...baseTmplParams,
|
|
1240
|
+
collectedExportFiles: collectedExportFilesFromIndexFile,
|
|
1241
|
+
metaInfo
|
|
1242
|
+
})
|
|
1243
|
+
});
|
|
1244
|
+
codegenFs.createFile({
|
|
1245
|
+
path: paths.outputDir,
|
|
1246
|
+
fileName: "index.ts",
|
|
1247
|
+
withPrefix: false,
|
|
1248
|
+
content: `${LINTERS_IGNORE}
|
|
1249
|
+
export * as ${namespace} from './__exports';
|
|
1250
|
+
`
|
|
1251
|
+
});
|
|
1252
|
+
} else {
|
|
1253
|
+
codegenFs.createFile({
|
|
1254
|
+
path: paths.outputDir,
|
|
1255
|
+
fileName: "index.ts",
|
|
1256
|
+
withPrefix: false,
|
|
1257
|
+
content: await allExportsTmpl({
|
|
1258
|
+
...baseTmplParams,
|
|
1259
|
+
collectedExportFiles: collectedExportFilesFromIndexFile,
|
|
1260
|
+
metaInfo
|
|
1261
|
+
})
|
|
1262
|
+
});
|
|
1263
|
+
}
|
|
1264
|
+
if (params.removeUnusedTypes) {
|
|
1265
|
+
removeUnusedTypes({
|
|
1266
|
+
directory: params.output,
|
|
1267
|
+
keepTypes: params.removeUnusedTypes === true ? void 0 : params.removeUnusedTypes.keepTypes
|
|
1268
|
+
});
|
|
1269
|
+
}
|
|
1270
|
+
};
|
|
1271
|
+
const defineConfig = (...configs) => {
|
|
1272
|
+
return configs.flat().filter((config) => !!config);
|
|
1273
|
+
};
|
|
1274
|
+
export {
|
|
1275
|
+
defineConfig,
|
|
1276
|
+
generateApi
|
|
1277
|
+
};
|
|
1278
|
+
//# sourceMappingURL=cli.js.map
|