@genapi/pipeline 3.4.0 → 3.6.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/dist/{index.d.ts → index.d.mts} +14 -2
- package/dist/index.mjs +193 -221
- package/package.json +4 -4
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { ApiPipeline } from
|
|
1
|
+
import { ApiPipeline } from "@genapi/shared";
|
|
2
2
|
|
|
3
|
+
//#region src/pipeline/index.d.ts
|
|
3
4
|
/**
|
|
4
5
|
* Pipeline read(input)function
|
|
5
6
|
*/
|
|
@@ -31,14 +32,25 @@ declare namespace pipeline {
|
|
|
31
32
|
var dest: typeof dest;
|
|
32
33
|
}
|
|
33
34
|
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/compiler/index.d.ts
|
|
34
37
|
declare function compiler(configRead: ApiPipeline.ConfigRead): ApiPipeline.ConfigRead<ApiPipeline.Config>;
|
|
35
38
|
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/config/index.d.ts
|
|
36
41
|
declare function config(userConfig: ApiPipeline.Config): ApiPipeline.ConfigRead<Required<ApiPipeline.Config>>;
|
|
37
42
|
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/dest/index.d.ts
|
|
38
45
|
declare function dest(configRead: ApiPipeline.ConfigRead): void;
|
|
39
46
|
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/generate/index.d.ts
|
|
40
49
|
declare function generate(configRead: ApiPipeline.ConfigRead): ApiPipeline.ConfigRead<ApiPipeline.Config>;
|
|
41
50
|
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/original/index.d.ts
|
|
42
53
|
declare function original(configRead: ApiPipeline.ConfigRead): Promise<ApiPipeline.ConfigRead<ApiPipeline.Config>>;
|
|
43
54
|
|
|
44
|
-
|
|
55
|
+
//#endregion
|
|
56
|
+
export { PipelineDest, PipelineFlow, PipelineRead, compiler, config, pipeline as default, dest, generate, original };
|
package/dist/index.mjs
CHANGED
|
@@ -1,245 +1,221 @@
|
|
|
1
|
-
// src/pipeline/index.ts
|
|
2
1
|
import pPipe from "p-pipe";
|
|
2
|
+
import { astNodeToCode, codeToAstNode, createComment, createFunction, createImport, createInterface, createTypeAlias, createVariable } from "ts-factory-extra";
|
|
3
|
+
import { NodeFlags, factory } from "typescript";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import process from "node:process";
|
|
6
|
+
import fs from "fs-extra";
|
|
7
|
+
import { format } from "prettier";
|
|
8
|
+
import got from "got";
|
|
3
9
|
|
|
4
|
-
|
|
5
|
-
import { codeToAstNode, createComment as createComment2, createFunction, createImport, createVariable } from "ts-factory-extra";
|
|
6
|
-
import { factory as factory2, NodeFlags } from "typescript";
|
|
7
|
-
|
|
8
|
-
// src/compiler/typings.ts
|
|
9
|
-
import { createComment, createInterface, createTypeAlias } from "ts-factory-extra";
|
|
10
|
-
import { factory } from "typescript";
|
|
10
|
+
//#region src/compiler/typings.ts
|
|
11
11
|
function compilerTsTypingsDeclaration(configRead, comment = true) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return nodes;
|
|
12
|
+
configRead.graphs.comments = configRead.graphs.comments || [];
|
|
13
|
+
configRead.graphs.typings = configRead.graphs.typings || [];
|
|
14
|
+
configRead.graphs.interfaces = configRead.graphs.interfaces || [];
|
|
15
|
+
const typings = configRead.graphs.typings.map((item) => {
|
|
16
|
+
return createTypeAlias(item.export, item.name, item.value);
|
|
17
|
+
});
|
|
18
|
+
const interfaces = configRead.graphs.interfaces.map((item) => {
|
|
19
|
+
return createInterface({
|
|
20
|
+
export: item.export,
|
|
21
|
+
name: item.name,
|
|
22
|
+
properties: item.properties || []
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
const nodes = [
|
|
26
|
+
factory.createIdentifier(""),
|
|
27
|
+
...typings,
|
|
28
|
+
factory.createIdentifier(""),
|
|
29
|
+
...interfaces
|
|
30
|
+
];
|
|
31
|
+
if (comment) nodes.unshift(createComment("multi", configRead.graphs.comments));
|
|
32
|
+
return nodes;
|
|
34
33
|
}
|
|
35
34
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/compiler/request.ts
|
|
37
|
+
const varFlags = {
|
|
38
|
+
let: NodeFlags.Let,
|
|
39
|
+
const: NodeFlags.Const,
|
|
40
|
+
var: NodeFlags.None
|
|
41
41
|
};
|
|
42
42
|
function compilerTsRequestDeclaration(configRead) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
return nodes;
|
|
43
|
+
configRead.graphs.imports = configRead.graphs.imports || [];
|
|
44
|
+
configRead.graphs.comments = configRead.graphs.comments || [];
|
|
45
|
+
configRead.graphs.variables = configRead.graphs.variables || [];
|
|
46
|
+
configRead.graphs.functions = configRead.graphs.functions || [];
|
|
47
|
+
const isGenerateType = configRead.outputs.some((v) => v.type === "typings");
|
|
48
|
+
const isTypescript = configRead.outputs.some((v) => v.type === "request" && v.path.endsWith(".ts"));
|
|
49
|
+
const comments = [createComment("multi", configRead.graphs.comments)];
|
|
50
|
+
const imports = configRead.graphs.imports?.map((item) => {
|
|
51
|
+
return createImport(item.name, item.names, item.value, item.namespace);
|
|
52
|
+
});
|
|
53
|
+
const variables = configRead.graphs.variables.map((item) => {
|
|
54
|
+
return createVariable(item.export, varFlags[item.flag], item.name, item.value);
|
|
55
|
+
});
|
|
56
|
+
const functions = configRead.graphs.functions.flatMap((item) => {
|
|
57
|
+
return createFunction({
|
|
58
|
+
export: true,
|
|
59
|
+
comment: item.description,
|
|
60
|
+
name: item.name,
|
|
61
|
+
parameters: item.parameters,
|
|
62
|
+
body: item.body?.map(codeToAstNode),
|
|
63
|
+
async: item.async,
|
|
64
|
+
returnType: item.returnType,
|
|
65
|
+
generics: item.generics,
|
|
66
|
+
generator: item.generator
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
const nodes = [
|
|
70
|
+
...comments,
|
|
71
|
+
factory.createIdentifier(""),
|
|
72
|
+
...imports,
|
|
73
|
+
factory.createIdentifier(""),
|
|
74
|
+
...variables,
|
|
75
|
+
factory.createIdentifier(""),
|
|
76
|
+
...functions
|
|
77
|
+
];
|
|
78
|
+
if (!isGenerateType && isTypescript) {
|
|
79
|
+
nodes.push(factory.createIdentifier(""));
|
|
80
|
+
nodes.push(factory.createIdentifier(""));
|
|
81
|
+
nodes.push(...compilerTsTypingsDeclaration(configRead, false));
|
|
82
|
+
}
|
|
83
|
+
return nodes;
|
|
86
84
|
}
|
|
87
85
|
|
|
88
|
-
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/compiler/index.ts
|
|
89
88
|
function compiler(configRead) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
return configRead;
|
|
89
|
+
for (const output of configRead.outputs) {
|
|
90
|
+
if (output.type === "request") output.ast = compilerTsRequestDeclaration(configRead);
|
|
91
|
+
if (output.type === "typings") output.ast = compilerTsTypingsDeclaration(configRead);
|
|
92
|
+
}
|
|
93
|
+
return configRead;
|
|
97
94
|
}
|
|
98
95
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
import process from "node:process";
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region src/config/index.ts
|
|
102
98
|
function config(userConfig) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
graphs: {
|
|
156
|
-
imports: imports.filter(Boolean),
|
|
157
|
-
variables: [],
|
|
158
|
-
comments: [],
|
|
159
|
-
functions: [],
|
|
160
|
-
interfaces: [],
|
|
161
|
-
typings: typings.filter(Boolean),
|
|
162
|
-
response: userConfig.responseType
|
|
163
|
-
}
|
|
164
|
-
};
|
|
165
|
-
return configRead;
|
|
99
|
+
userConfig.import = userConfig.import || {};
|
|
100
|
+
userConfig.responseType = userConfig.responseType || {};
|
|
101
|
+
if (typeof userConfig.output === "string") userConfig.output = { main: userConfig.output };
|
|
102
|
+
userConfig.output = userConfig.output || {};
|
|
103
|
+
userConfig.output.main = userConfig.output.main || "src/api/index.ts";
|
|
104
|
+
if (typeof userConfig.baseURL === "string") userConfig.baseURL = userConfig.baseURL.endsWith("/\"") ? userConfig.baseURL = `${userConfig.baseURL.slice(0, userConfig.baseURL.length - 2)}"` : userConfig.baseURL;
|
|
105
|
+
if (userConfig.output?.type !== false) userConfig.output.type = userConfig.output.type || userConfig.output.main.replace(/\.ts|\.js/g, ".type.ts");
|
|
106
|
+
if (typeof userConfig.responseType === "string") userConfig.responseType = { infer: userConfig.responseType };
|
|
107
|
+
const userRoot = process.cwd();
|
|
108
|
+
const isTypescript = userConfig.output.main.endsWith(".ts");
|
|
109
|
+
const isGenerateType = userConfig.output?.type !== false;
|
|
110
|
+
const importTypePath = userConfig.import.type || getImportTypePath(userConfig.output.main, userConfig.output.type || "");
|
|
111
|
+
const imports = [isTypescript && isGenerateType && {
|
|
112
|
+
name: "Types",
|
|
113
|
+
value: importTypePath,
|
|
114
|
+
type: true,
|
|
115
|
+
namespace: true
|
|
116
|
+
}];
|
|
117
|
+
const outputs = [{
|
|
118
|
+
type: "request",
|
|
119
|
+
root: path.join(userRoot, path.dirname(userConfig.output.main)),
|
|
120
|
+
path: path.join(userRoot, userConfig.output.main)
|
|
121
|
+
}];
|
|
122
|
+
const typings = [!!userConfig.responseType.infer && {
|
|
123
|
+
export: true,
|
|
124
|
+
name: "Infer<T>",
|
|
125
|
+
value: userConfig.responseType.infer
|
|
126
|
+
}];
|
|
127
|
+
if (userConfig.output.type !== false) outputs.push({
|
|
128
|
+
type: "typings",
|
|
129
|
+
root: path.join(userRoot, path.dirname(userConfig.output.type)),
|
|
130
|
+
import: importTypePath,
|
|
131
|
+
path: path.join(userRoot, userConfig.output.type)
|
|
132
|
+
});
|
|
133
|
+
const inputs = {};
|
|
134
|
+
if (typeof userConfig.input === "string") inputs.uri = userConfig.input;
|
|
135
|
+
if (typeof userConfig.input === "object") Object.assign(inputs, userConfig.input);
|
|
136
|
+
const configRead = {
|
|
137
|
+
config: userConfig,
|
|
138
|
+
inputs,
|
|
139
|
+
outputs,
|
|
140
|
+
graphs: {
|
|
141
|
+
imports: imports.filter(Boolean),
|
|
142
|
+
variables: [],
|
|
143
|
+
comments: [],
|
|
144
|
+
functions: [],
|
|
145
|
+
interfaces: [],
|
|
146
|
+
typings: typings.filter(Boolean),
|
|
147
|
+
response: userConfig.responseType
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
return configRead;
|
|
166
151
|
}
|
|
167
|
-
function prefix(
|
|
168
|
-
|
|
152
|
+
function prefix(path$1) {
|
|
153
|
+
return path$1.startsWith(".") ? path$1 : `./${path$1}`;
|
|
169
154
|
}
|
|
170
155
|
function getImportTypePath(main, type) {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
156
|
+
let importTypePath = path.dirname(main);
|
|
157
|
+
importTypePath = path.relative(importTypePath, type || "");
|
|
158
|
+
importTypePath = prefix(importTypePath).replace(".ts", "");
|
|
159
|
+
return importTypePath;
|
|
175
160
|
}
|
|
176
161
|
|
|
177
|
-
|
|
178
|
-
|
|
162
|
+
//#endregion
|
|
163
|
+
//#region src/dest/index.ts
|
|
179
164
|
function dest(configRead) {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
165
|
+
configRead.outputs.map(async (output) => {
|
|
166
|
+
await fs.ensureDir(output.root);
|
|
167
|
+
await fs.writeFile(output.path, output.code || "", { flag: "w" });
|
|
168
|
+
});
|
|
184
169
|
}
|
|
185
170
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
import { astNodeToCode } from "ts-factory-extra";
|
|
171
|
+
//#endregion
|
|
172
|
+
//#region src/generate/index.ts
|
|
189
173
|
function generate(configRead) {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
196
|
-
return configRead;
|
|
174
|
+
for (const output of configRead.outputs || []) {
|
|
175
|
+
if (output.ast) output.code = astNodeToCode(output.ast);
|
|
176
|
+
if (output.code) output.code = formatTypescript(output.code);
|
|
177
|
+
}
|
|
178
|
+
return configRead;
|
|
197
179
|
}
|
|
198
180
|
function formatTypescript(code) {
|
|
199
|
-
|
|
181
|
+
return format(code, {
|
|
182
|
+
printWidth: 800,
|
|
183
|
+
parser: "typescript"
|
|
184
|
+
});
|
|
200
185
|
}
|
|
201
186
|
|
|
202
|
-
|
|
203
|
-
|
|
187
|
+
//#endregion
|
|
188
|
+
//#region src/original/index.ts
|
|
204
189
|
async function original(configRead) {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
190
|
+
if (configRead.inputs.uri) configRead.source = await got({
|
|
191
|
+
url: configRead.inputs.uri,
|
|
192
|
+
responseType: "json"
|
|
193
|
+
}).json();
|
|
194
|
+
if (configRead.inputs.http) configRead.source = await got({
|
|
195
|
+
...configRead.inputs.http,
|
|
196
|
+
responseType: "json"
|
|
197
|
+
}).json();
|
|
198
|
+
if (configRead.inputs.json) configRead.source = await readJsonSource(configRead.inputs.json);
|
|
199
|
+
if (!configRead.source) throw new Error("No source found, please check your input config.");
|
|
200
|
+
if (!configRead.source.schemes?.length) {
|
|
201
|
+
const schemes = [];
|
|
202
|
+
if (configRead.inputs.uri?.startsWith("https://")) schemes.push("https", "http");
|
|
203
|
+
if (configRead.inputs.uri?.startsWith("http://")) schemes.push("http");
|
|
204
|
+
configRead.source.schemes = schemes;
|
|
205
|
+
}
|
|
206
|
+
return configRead;
|
|
222
207
|
}
|
|
223
208
|
function readJsonSource(json) {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
return json;
|
|
228
|
-
else
|
|
229
|
-
return import(json).then((mod) => mod.default);
|
|
209
|
+
if (!json) return;
|
|
210
|
+
if (typeof json === "object") return json;
|
|
211
|
+
else return import(json).then((mod) => mod.default);
|
|
230
212
|
}
|
|
231
213
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
parser,
|
|
238
|
-
compiler2,
|
|
239
|
-
generate2,
|
|
240
|
-
dest2
|
|
241
|
-
);
|
|
242
|
-
return pipe;
|
|
214
|
+
//#endregion
|
|
215
|
+
//#region src/pipeline/index.ts
|
|
216
|
+
function pipeline(config$1, original$1, parser, compiler$1, generate$1, dest$1) {
|
|
217
|
+
const pipe = pPipe(config$1, original$1, parser, compiler$1, generate$1, dest$1);
|
|
218
|
+
return pipe;
|
|
243
219
|
}
|
|
244
220
|
pipeline.config = config;
|
|
245
221
|
pipeline.original = original;
|
|
@@ -248,13 +224,9 @@ pipeline.compiler = compiler;
|
|
|
248
224
|
pipeline.generate = generate;
|
|
249
225
|
pipeline.dest = dest;
|
|
250
226
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
dest,
|
|
258
|
-
generate,
|
|
259
|
-
original
|
|
260
|
-
};
|
|
227
|
+
//#endregion
|
|
228
|
+
//#region src/index.ts
|
|
229
|
+
var src_default = pipeline;
|
|
230
|
+
|
|
231
|
+
//#endregion
|
|
232
|
+
export { compiler, config, src_default as default, dest, generate, original };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@genapi/pipeline",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.6.0",
|
|
5
5
|
"author": "Hairyf <wwu710632@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/hairyf/genapi#readme",
|
|
@@ -26,18 +26,18 @@
|
|
|
26
26
|
"prettier": "^2.8.4",
|
|
27
27
|
"ts-factory-extra": "^0.0.5",
|
|
28
28
|
"typescript": "^5.0.0",
|
|
29
|
-
"@genapi/shared": "3.
|
|
29
|
+
"@genapi/shared": "3.6.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/fs-extra": "^11.0.1"
|
|
33
33
|
},
|
|
34
34
|
"scripts": {
|
|
35
|
-
"build": "
|
|
35
|
+
"build": "tsdown",
|
|
36
36
|
"start": "tsx src/index.ts"
|
|
37
37
|
},
|
|
38
38
|
"exports": {
|
|
39
39
|
".": "./dist/index.mjs"
|
|
40
40
|
},
|
|
41
41
|
"module": "./dist/index.mjs",
|
|
42
|
-
"types": "./dist/index.d.
|
|
42
|
+
"types": "./dist/index.d.mts"
|
|
43
43
|
}
|