@tsed/cli-generate-http-client 3.22.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/jest.config.js +1 -0
- package/lib/cjs/commands/GenerateHttpClientCmd.js +138 -0
- package/lib/cjs/commands/GenerateHttpClientCmd.js.map +1 -0
- package/lib/cjs/index.js +6 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/package.json +3 -0
- package/lib/esm/commands/GenerateHttpClientCmd.js +135 -0
- package/lib/esm/commands/GenerateHttpClientCmd.js.map +1 -0
- package/lib/esm/index.js +3 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/package.json +3 -0
- package/lib/types/commands/GenerateHttpClientCmd.d.ts +27 -0
- package/lib/types/index.d.ts +3 -0
- package/package.json +49 -0
- package/readme.md +98 -0
- package/tsconfig.compile.esm.json +21 -0
package/jest.config.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require("../../jest.config");
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GenerateHttpClientCmd = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const cli_core_1 = require("@tsed/cli-core");
|
|
6
|
+
const di_1 = require("@tsed/di");
|
|
7
|
+
const core_1 = require("@tsed/core");
|
|
8
|
+
const change_case_1 = require("change-case");
|
|
9
|
+
const path_1 = tslib_1.__importStar(require("path"));
|
|
10
|
+
const swagger_typescript_api_1 = require("swagger-typescript-api");
|
|
11
|
+
let GenerateHttpClientCmd = class GenerateHttpClientCmd {
|
|
12
|
+
injector;
|
|
13
|
+
fs;
|
|
14
|
+
serverModule;
|
|
15
|
+
$mapContext($ctx) {
|
|
16
|
+
return { ...$ctx, output: (0, path_1.resolve)((0, path_1.join)(process.cwd(), $ctx.output)) };
|
|
17
|
+
}
|
|
18
|
+
async $exec($ctx) {
|
|
19
|
+
return [
|
|
20
|
+
{
|
|
21
|
+
title: "generate client",
|
|
22
|
+
task: () => this.generate($ctx)
|
|
23
|
+
}
|
|
24
|
+
];
|
|
25
|
+
}
|
|
26
|
+
async loadPlatformModule() {
|
|
27
|
+
let platform = await (0, core_1.importPackage)("@tsed/platform-express", undefined, true);
|
|
28
|
+
if (platform) {
|
|
29
|
+
return platform.PlatformExpress;
|
|
30
|
+
}
|
|
31
|
+
platform = await (0, core_1.importPackage)("@tsed/platform-koa", undefined, true);
|
|
32
|
+
if (platform) {
|
|
33
|
+
return platform.PlatformKoa;
|
|
34
|
+
}
|
|
35
|
+
throw new Error("Unsupported platform. Please use Express.js or Koa.js platform.");
|
|
36
|
+
}
|
|
37
|
+
async generate($ctx) {
|
|
38
|
+
const Platform = await this.loadPlatformModule();
|
|
39
|
+
const { SwaggerService } = await (0, core_1.importPackage)("@tsed/swagger");
|
|
40
|
+
const platform = await Platform.bootstrap(this.serverModule, { logger: { level: "off" } });
|
|
41
|
+
const swaggerService = platform.injector.get(SwaggerService);
|
|
42
|
+
const confs = platform.injector.settings.get("swagger", []);
|
|
43
|
+
await this.fs.raw.remove($ctx.output);
|
|
44
|
+
await this.fs.ensureDir($ctx.output);
|
|
45
|
+
const promises = confs.map(async (conf) => {
|
|
46
|
+
const spec = await swaggerService.getOpenAPISpec(conf);
|
|
47
|
+
await this.generateFromSpec(spec, $ctx);
|
|
48
|
+
});
|
|
49
|
+
await Promise.all(promises);
|
|
50
|
+
}
|
|
51
|
+
async generateFromSpec(spec, $ctx) {
|
|
52
|
+
const { files } = await (0, swagger_typescript_api_1.generateApi)({
|
|
53
|
+
name: `${$ctx.name}.ts`,
|
|
54
|
+
httpClientType: $ctx.type,
|
|
55
|
+
spec: spec,
|
|
56
|
+
moduleNameIndex: 1,
|
|
57
|
+
typeSuffix: $ctx.suffix,
|
|
58
|
+
unwrapResponseData: true,
|
|
59
|
+
hooks: {
|
|
60
|
+
onCreateRouteName(routeNameInfo, raw) {
|
|
61
|
+
const [, operationId] = raw.operationId.split("_");
|
|
62
|
+
const name = raw.moduleName === "oidc" ? (0, change_case_1.camelCase)(routeNameInfo.original.replace("oidc", "")) : (0, change_case_1.camelCase)(operationId);
|
|
63
|
+
routeNameInfo.usage = name;
|
|
64
|
+
routeNameInfo.original = name;
|
|
65
|
+
raw.operationId = name;
|
|
66
|
+
return routeNameInfo;
|
|
67
|
+
},
|
|
68
|
+
onParseSchema(originalSchema, parsedSchema) {
|
|
69
|
+
const { content } = parsedSchema;
|
|
70
|
+
if ((0, core_1.isString)(content)) {
|
|
71
|
+
if (content.includes("null") && content.includes("any") && content.includes("Record")) {
|
|
72
|
+
parsedSchema.content = "any";
|
|
73
|
+
}
|
|
74
|
+
parsedSchema.content = parsedSchema.content.replace("object", "Record<string, any>");
|
|
75
|
+
}
|
|
76
|
+
return parsedSchema;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
const promises = files.map(({ content, name }) => {
|
|
81
|
+
content = content
|
|
82
|
+
.replace("class Api", `class ${$ctx.name}`)
|
|
83
|
+
.replace(".then((response) => response.data)", ".then((response) => response.data as T)")
|
|
84
|
+
.replace('requestParams.headers.common = { Accept: "*/*" };', "")
|
|
85
|
+
.replace("requestParams.headers.post = {};", "")
|
|
86
|
+
.replace("requestParams.headers.put = {};", "")
|
|
87
|
+
.replace("(this.instance.defaults.headers || {})", "((this.instance.defaults.headers || {}) as any)");
|
|
88
|
+
console.log(`${$ctx.output}/${name}`, path_1.default.resolve(`${$ctx.output}/${name}`));
|
|
89
|
+
return this.fs.writeFile(`${$ctx.output}/${name}`, content, { encoding: "utf8" });
|
|
90
|
+
});
|
|
91
|
+
return Promise.all(promises);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
tslib_1.__decorate([
|
|
95
|
+
(0, cli_core_1.Inject)(),
|
|
96
|
+
tslib_1.__metadata("design:type", di_1.InjectorService)
|
|
97
|
+
], GenerateHttpClientCmd.prototype, "injector", void 0);
|
|
98
|
+
tslib_1.__decorate([
|
|
99
|
+
(0, cli_core_1.Inject)(),
|
|
100
|
+
tslib_1.__metadata("design:type", cli_core_1.CliFs)
|
|
101
|
+
], GenerateHttpClientCmd.prototype, "fs", void 0);
|
|
102
|
+
tslib_1.__decorate([
|
|
103
|
+
(0, di_1.Constant)("server"),
|
|
104
|
+
tslib_1.__metadata("design:type", cli_core_1.Type)
|
|
105
|
+
], GenerateHttpClientCmd.prototype, "serverModule", void 0);
|
|
106
|
+
GenerateHttpClientCmd = tslib_1.__decorate([
|
|
107
|
+
(0, cli_core_1.Command)({
|
|
108
|
+
name: "generate-http-client",
|
|
109
|
+
description: "Generate the client API from swagger spec",
|
|
110
|
+
options: {
|
|
111
|
+
"-s, --suffix <suffix>": {
|
|
112
|
+
required: false,
|
|
113
|
+
type: String,
|
|
114
|
+
defaultValue: "Model",
|
|
115
|
+
description: "The suffix applied on model"
|
|
116
|
+
},
|
|
117
|
+
"-t, --type <type>": {
|
|
118
|
+
required: false,
|
|
119
|
+
type: String,
|
|
120
|
+
defaultValue: "axios",
|
|
121
|
+
description: "The client type by the Http client (axios or fetch)"
|
|
122
|
+
},
|
|
123
|
+
"-n, --name <name>": {
|
|
124
|
+
required: false,
|
|
125
|
+
type: String,
|
|
126
|
+
defaultValue: "ApiClient",
|
|
127
|
+
description: "The class name of the generated client"
|
|
128
|
+
},
|
|
129
|
+
"-o, --output <output>": {
|
|
130
|
+
required: true,
|
|
131
|
+
type: String,
|
|
132
|
+
description: "Path to generate files"
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
})
|
|
136
|
+
], GenerateHttpClientCmd);
|
|
137
|
+
exports.GenerateHttpClientCmd = GenerateHttpClientCmd;
|
|
138
|
+
//# sourceMappingURL=GenerateHttpClientCmd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GenerateHttpClientCmd.js","sourceRoot":"","sources":["../../../src/commands/GenerateHttpClientCmd.ts"],"names":[],"mappings":";;;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M;gBACZ,WAAW,EAAE,wBAAwB;aACtC;SACF;KACF,CAAC;GACW,qBAAqB,CA8GjC;AA9GY,sDAAqB","sourcesContent":["import {CliFs, Command, CommandProvider, Inject, Type} from \"@tsed/cli-core\";\nimport {Constant, InjectorService} from \"@tsed/di\";\nimport {importPackage, isString} from \"@tsed/core\";\nimport {camelCase} from \"change-case\";\nimport path, {join, resolve} from \"path\";\nimport {generateApi} from \"swagger-typescript-api\";\n\ninterface GenerateHttpClientCtx {\n output: string;\n type: \"axios\" | \"fetch\";\n name: string;\n suffix: string;\n}\n\n@Command({\n name: \"generate-http-client\",\n description: \"Generate the client API from swagger spec\",\n options: {\n \"-s, --suffix <suffix>\": {\n required: false,\n type: String,\n defaultValue: \"Model\",\n description: \"The suffix applied on model\"\n },\n \"-t, --type <type>\": {\n required: false,\n type: String,\n defaultValue: \"axios\",\n description: \"The client type by the Http client (axios or fetch)\"\n },\n \"-n, --name <name>\": {\n required: false,\n type: String,\n defaultValue: \"ApiClient\",\n description: \"The class name of the generated client\"\n },\n \"-o, --output <output>\": {\n required: true,\n type: String,\n description: \"Path to generate files\"\n }\n }\n})\nexport class GenerateHttpClientCmd implements CommandProvider {\n @Inject()\n injector: InjectorService;\n\n @Inject()\n protected fs: CliFs;\n\n @Constant(\"server\")\n protected serverModule: Type<any>;\n\n $mapContext($ctx: GenerateHttpClientCtx) {\n return {...$ctx, output: resolve(join(process.cwd(), $ctx.output))};\n }\n\n async $exec($ctx: GenerateHttpClientCtx) {\n return [\n {\n title: \"generate client\",\n task: () => this.generate($ctx)\n }\n ];\n }\n\n private async loadPlatformModule(): Promise<any> {\n let platform = await importPackage(\"@tsed/platform-express\", undefined, true);\n\n if (platform) {\n return platform.PlatformExpress;\n }\n\n platform = await importPackage(\"@tsed/platform-koa\", undefined, true);\n\n if (platform) {\n return platform.PlatformKoa;\n }\n\n throw new Error(\"Unsupported platform. Please use Express.js or Koa.js platform.\");\n }\n\n private async generate($ctx: GenerateHttpClientCtx) {\n const Platform = await this.loadPlatformModule();\n const {SwaggerService} = await importPackage(\"@tsed/swagger\");\n\n const platform: {injector: InjectorService} = await Platform.bootstrap(this.serverModule, {logger: {level: \"off\"}});\n const swaggerService = platform.injector.get<any>(SwaggerService)!;\n const confs = platform.injector.settings.get(\"swagger\", []);\n\n await this.fs.raw.remove($ctx.output);\n await this.fs.ensureDir($ctx.output);\n\n const promises = confs.map(async (conf) => {\n const spec = await swaggerService.getOpenAPISpec(conf);\n\n await this.generateFromSpec(spec, $ctx);\n });\n\n await Promise.all(promises);\n }\n\n private async generateFromSpec(spec: any, $ctx: GenerateHttpClientCtx) {\n const {files} = await generateApi({\n name: `${$ctx.name}.ts`,\n httpClientType: $ctx.type,\n spec: spec as any,\n moduleNameIndex: 1,\n typeSuffix: $ctx.suffix,\n unwrapResponseData: true,\n hooks: {\n onCreateRouteName(routeNameInfo: any, raw: any) {\n const [, operationId] = raw.operationId.split(\"_\");\n\n const name = raw.moduleName === \"oidc\" ? camelCase(routeNameInfo.original.replace(\"oidc\", \"\")) : camelCase(operationId);\n\n routeNameInfo.usage = name;\n routeNameInfo.original = name;\n raw.operationId = name;\n\n return routeNameInfo;\n },\n onParseSchema(originalSchema: any, parsedSchema: any) {\n const {content} = parsedSchema;\n\n if (isString(content)) {\n if (content.includes(\"null\") && content.includes(\"any\") && content.includes(\"Record\")) {\n parsedSchema.content = \"any\";\n }\n\n parsedSchema.content = parsedSchema.content.replace(\"object\", \"Record<string, any>\");\n }\n\n return parsedSchema;\n }\n }\n } as any);\n\n const promises = files.map(({content, name}) => {\n content = content\n .replace(\"class Api\", `class ${$ctx.name}`)\n .replace(\".then((response) => response.data)\", \".then((response) => response.data as T)\")\n .replace('requestParams.headers.common = { Accept: \"*/*\" };', \"\")\n .replace(\"requestParams.headers.post = {};\", \"\")\n .replace(\"requestParams.headers.put = {};\", \"\")\n .replace(\"(this.instance.defaults.headers || {})\", \"((this.instance.defaults.headers || {}) as any)\");\n\n console.log(`${$ctx.output}/${name}`, path.resolve(`${$ctx.output}/${name}`));\n return this.fs.writeFile(`${$ctx.output}/${name}`, content, {encoding: \"utf8\"});\n });\n\n return Promise.all(promises);\n }\n}\n"]}
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,2EAAiD;AAEjD,kBAAe,EAAE,CAAC","sourcesContent":["export * from \"./commands/GenerateHttpClientCmd\";\n\nexport default {};\n"]}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { __decorate, __metadata } from "tslib";
|
|
2
|
+
import { CliFs, Command, Inject, Type } from "@tsed/cli-core";
|
|
3
|
+
import { Constant, InjectorService } from "@tsed/di";
|
|
4
|
+
import { importPackage, isString } from "@tsed/core";
|
|
5
|
+
import { camelCase } from "change-case";
|
|
6
|
+
import path, { join, resolve } from "path";
|
|
7
|
+
import { generateApi } from "swagger-typescript-api";
|
|
8
|
+
let GenerateHttpClientCmd = class GenerateHttpClientCmd {
|
|
9
|
+
injector;
|
|
10
|
+
fs;
|
|
11
|
+
serverModule;
|
|
12
|
+
$mapContext($ctx) {
|
|
13
|
+
return { ...$ctx, output: resolve(join(process.cwd(), $ctx.output)) };
|
|
14
|
+
}
|
|
15
|
+
async $exec($ctx) {
|
|
16
|
+
return [
|
|
17
|
+
{
|
|
18
|
+
title: "generate client",
|
|
19
|
+
task: () => this.generate($ctx)
|
|
20
|
+
}
|
|
21
|
+
];
|
|
22
|
+
}
|
|
23
|
+
async loadPlatformModule() {
|
|
24
|
+
let platform = await importPackage("@tsed/platform-express", undefined, true);
|
|
25
|
+
if (platform) {
|
|
26
|
+
return platform.PlatformExpress;
|
|
27
|
+
}
|
|
28
|
+
platform = await importPackage("@tsed/platform-koa", undefined, true);
|
|
29
|
+
if (platform) {
|
|
30
|
+
return platform.PlatformKoa;
|
|
31
|
+
}
|
|
32
|
+
throw new Error("Unsupported platform. Please use Express.js or Koa.js platform.");
|
|
33
|
+
}
|
|
34
|
+
async generate($ctx) {
|
|
35
|
+
const Platform = await this.loadPlatformModule();
|
|
36
|
+
const { SwaggerService } = await importPackage("@tsed/swagger");
|
|
37
|
+
const platform = await Platform.bootstrap(this.serverModule, { logger: { level: "off" } });
|
|
38
|
+
const swaggerService = platform.injector.get(SwaggerService);
|
|
39
|
+
const confs = platform.injector.settings.get("swagger", []);
|
|
40
|
+
await this.fs.raw.remove($ctx.output);
|
|
41
|
+
await this.fs.ensureDir($ctx.output);
|
|
42
|
+
const promises = confs.map(async (conf) => {
|
|
43
|
+
const spec = await swaggerService.getOpenAPISpec(conf);
|
|
44
|
+
await this.generateFromSpec(spec, $ctx);
|
|
45
|
+
});
|
|
46
|
+
await Promise.all(promises);
|
|
47
|
+
}
|
|
48
|
+
async generateFromSpec(spec, $ctx) {
|
|
49
|
+
const { files } = await generateApi({
|
|
50
|
+
name: `${$ctx.name}.ts`,
|
|
51
|
+
httpClientType: $ctx.type,
|
|
52
|
+
spec: spec,
|
|
53
|
+
moduleNameIndex: 1,
|
|
54
|
+
typeSuffix: $ctx.suffix,
|
|
55
|
+
unwrapResponseData: true,
|
|
56
|
+
hooks: {
|
|
57
|
+
onCreateRouteName(routeNameInfo, raw) {
|
|
58
|
+
const [, operationId] = raw.operationId.split("_");
|
|
59
|
+
const name = raw.moduleName === "oidc" ? camelCase(routeNameInfo.original.replace("oidc", "")) : camelCase(operationId);
|
|
60
|
+
routeNameInfo.usage = name;
|
|
61
|
+
routeNameInfo.original = name;
|
|
62
|
+
raw.operationId = name;
|
|
63
|
+
return routeNameInfo;
|
|
64
|
+
},
|
|
65
|
+
onParseSchema(originalSchema, parsedSchema) {
|
|
66
|
+
const { content } = parsedSchema;
|
|
67
|
+
if (isString(content)) {
|
|
68
|
+
if (content.includes("null") && content.includes("any") && content.includes("Record")) {
|
|
69
|
+
parsedSchema.content = "any";
|
|
70
|
+
}
|
|
71
|
+
parsedSchema.content = parsedSchema.content.replace("object", "Record<string, any>");
|
|
72
|
+
}
|
|
73
|
+
return parsedSchema;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
const promises = files.map(({ content, name }) => {
|
|
78
|
+
content = content
|
|
79
|
+
.replace("class Api", `class ${$ctx.name}`)
|
|
80
|
+
.replace(".then((response) => response.data)", ".then((response) => response.data as T)")
|
|
81
|
+
.replace('requestParams.headers.common = { Accept: "*/*" };', "")
|
|
82
|
+
.replace("requestParams.headers.post = {};", "")
|
|
83
|
+
.replace("requestParams.headers.put = {};", "")
|
|
84
|
+
.replace("(this.instance.defaults.headers || {})", "((this.instance.defaults.headers || {}) as any)");
|
|
85
|
+
console.log(`${$ctx.output}/${name}`, path.resolve(`${$ctx.output}/${name}`));
|
|
86
|
+
return this.fs.writeFile(`${$ctx.output}/${name}`, content, { encoding: "utf8" });
|
|
87
|
+
});
|
|
88
|
+
return Promise.all(promises);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
__decorate([
|
|
92
|
+
Inject(),
|
|
93
|
+
__metadata("design:type", InjectorService)
|
|
94
|
+
], GenerateHttpClientCmd.prototype, "injector", void 0);
|
|
95
|
+
__decorate([
|
|
96
|
+
Inject(),
|
|
97
|
+
__metadata("design:type", CliFs)
|
|
98
|
+
], GenerateHttpClientCmd.prototype, "fs", void 0);
|
|
99
|
+
__decorate([
|
|
100
|
+
Constant("server"),
|
|
101
|
+
__metadata("design:type", Type)
|
|
102
|
+
], GenerateHttpClientCmd.prototype, "serverModule", void 0);
|
|
103
|
+
GenerateHttpClientCmd = __decorate([
|
|
104
|
+
Command({
|
|
105
|
+
name: "generate-http-client",
|
|
106
|
+
description: "Generate the client API from swagger spec",
|
|
107
|
+
options: {
|
|
108
|
+
"-s, --suffix <suffix>": {
|
|
109
|
+
required: false,
|
|
110
|
+
type: String,
|
|
111
|
+
defaultValue: "Model",
|
|
112
|
+
description: "The suffix applied on model"
|
|
113
|
+
},
|
|
114
|
+
"-t, --type <type>": {
|
|
115
|
+
required: false,
|
|
116
|
+
type: String,
|
|
117
|
+
defaultValue: "axios",
|
|
118
|
+
description: "The client type by the Http client (axios or fetch)"
|
|
119
|
+
},
|
|
120
|
+
"-n, --name <name>": {
|
|
121
|
+
required: false,
|
|
122
|
+
type: String,
|
|
123
|
+
defaultValue: "ApiClient",
|
|
124
|
+
description: "The class name of the generated client"
|
|
125
|
+
},
|
|
126
|
+
"-o, --output <output>": {
|
|
127
|
+
required: true,
|
|
128
|
+
type: String,
|
|
129
|
+
description: "Path to generate files"
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
})
|
|
133
|
+
], GenerateHttpClientCmd);
|
|
134
|
+
export { GenerateHttpClientCmd };
|
|
135
|
+
//# sourceMappingURL=GenerateHttpClientCmd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GenerateHttpClientCmd.js","sourceRoot":"","sources":["../../../src/commands/GenerateHttpClientCmd.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,KAAK,EAAE,OAAO,EAAmB,MAAM,EAAE,IAAI,EAAC,MAAM,gBAAgB,CAAC;AAC7E,OAAO,EAAC,QAAQ,EAAE,eAAe,EAAC,MAAM,UAAU,CAAC;AACnD,OAAO,EAAC,aAAa,EAAE,QAAQ,EAAC,MAAM,YAAY,CAAC;AACnD,OAAO,EAAC,SAAS,EAAC,MAAM,aAAa,CAAC;AACtC,OAAO,IAAI,EAAE,EAAC,IAAI,EAAE,OAAO,EAAC,MAAM,MAAM,CAAC;AACzC,OAAO,EAAC,WAAW,EAAC,MAAM,wBAAwB,CAAC;AAsCnD,IAAa,qBAAqB,GAAlC,MAAa,qBAAqB;IAEhC,QAAQ,CAAkB;IAGhB,EAAE,CAAQ;IAGV,YAAY,CAAY;IAElC,WAAW,CAAC,IAA2B;QACrC,OAAO,EAAC,GAAG,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,IAAI,CAAC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QAClF,CAAC,CAAC,CAAC;QAEH,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC/B,CAAC;CACF,CAAA;AA5GC;IADC,MAAM,EAAE;8BACC,eAAe;uDAAC;AAG1B;IADC,MAAM,EAAE;8BACK,KAAK;iDAAC;AAGpB;IADC,QAAQ,CAAC,QAAQ,CAAC;8BACK,IAAI;2DAAM;AARvB,qBAAqB;IA7BjC,OAAO,CAAC;QACP,IAAI,EAAE,sBAAsB;QAC5B,WAAW,EAAE,2CAA2C;QACxD,OAAO,EAAE;YACP,uBAAuB,EAAE;gBACvB,QAAQ,EAAE,KAAK;gBACf,IAAI,EAAE,MAAM;gBACZ,YAAY,EAAE,OAAO;gBACrB,WAAW,EAAE,6BAA6B;aAC3C;YACD,mBAAmB,EAAE;gBACnB,QAAQ,EAAE,KAAK;gBACf,IAAI,EAAE,MAAM;gBACZ,YAAY,EAAE,OAAO;gBACrB,WAAW,EAAE,qDAAqD;aACnE;YACD,mBAAmB,EAAE;gBACnB,QAAQ,EAAE,KAAK;gBACf,IAAI,EAAE,MAAM;gBACZ,YAAY,EAAE,WAAW;gBACzB,WAAW,EAAE,wCAAwC;aACtD;YACD,uBAAuB,EAAE;gBACvB,QAAQ,EAAE,IAAI;gBACd,IAAI,EAAE,MAAM;gBACZ,WAAW,EAAE,wBAAwB;aACtC;SACF;KACF,CAAC;GACW,qBAAqB,CA8GjC;SA9GY,qBAAqB","sourcesContent":["import {CliFs, Command, CommandProvider, Inject, Type} from \"@tsed/cli-core\";\nimport {Constant, InjectorService} from \"@tsed/di\";\nimport {importPackage, isString} from \"@tsed/core\";\nimport {camelCase} from \"change-case\";\nimport path, {join, resolve} from \"path\";\nimport {generateApi} from \"swagger-typescript-api\";\n\ninterface GenerateHttpClientCtx {\n output: string;\n type: \"axios\" | \"fetch\";\n name: string;\n suffix: string;\n}\n\n@Command({\n name: \"generate-http-client\",\n description: \"Generate the client API from swagger spec\",\n options: {\n \"-s, --suffix <suffix>\": {\n required: false,\n type: String,\n defaultValue: \"Model\",\n description: \"The suffix applied on model\"\n },\n \"-t, --type <type>\": {\n required: false,\n type: String,\n defaultValue: \"axios\",\n description: \"The client type by the Http client (axios or fetch)\"\n },\n \"-n, --name <name>\": {\n required: false,\n type: String,\n defaultValue: \"ApiClient\",\n description: \"The class name of the generated client\"\n },\n \"-o, --output <output>\": {\n required: true,\n type: String,\n description: \"Path to generate files\"\n }\n }\n})\nexport class GenerateHttpClientCmd implements CommandProvider {\n @Inject()\n injector: InjectorService;\n\n @Inject()\n protected fs: CliFs;\n\n @Constant(\"server\")\n protected serverModule: Type<any>;\n\n $mapContext($ctx: GenerateHttpClientCtx) {\n return {...$ctx, output: resolve(join(process.cwd(), $ctx.output))};\n }\n\n async $exec($ctx: GenerateHttpClientCtx) {\n return [\n {\n title: \"generate client\",\n task: () => this.generate($ctx)\n }\n ];\n }\n\n private async loadPlatformModule(): Promise<any> {\n let platform = await importPackage(\"@tsed/platform-express\", undefined, true);\n\n if (platform) {\n return platform.PlatformExpress;\n }\n\n platform = await importPackage(\"@tsed/platform-koa\", undefined, true);\n\n if (platform) {\n return platform.PlatformKoa;\n }\n\n throw new Error(\"Unsupported platform. Please use Express.js or Koa.js platform.\");\n }\n\n private async generate($ctx: GenerateHttpClientCtx) {\n const Platform = await this.loadPlatformModule();\n const {SwaggerService} = await importPackage(\"@tsed/swagger\");\n\n const platform: {injector: InjectorService} = await Platform.bootstrap(this.serverModule, {logger: {level: \"off\"}});\n const swaggerService = platform.injector.get<any>(SwaggerService)!;\n const confs = platform.injector.settings.get(\"swagger\", []);\n\n await this.fs.raw.remove($ctx.output);\n await this.fs.ensureDir($ctx.output);\n\n const promises = confs.map(async (conf) => {\n const spec = await swaggerService.getOpenAPISpec(conf);\n\n await this.generateFromSpec(spec, $ctx);\n });\n\n await Promise.all(promises);\n }\n\n private async generateFromSpec(spec: any, $ctx: GenerateHttpClientCtx) {\n const {files} = await generateApi({\n name: `${$ctx.name}.ts`,\n httpClientType: $ctx.type,\n spec: spec as any,\n moduleNameIndex: 1,\n typeSuffix: $ctx.suffix,\n unwrapResponseData: true,\n hooks: {\n onCreateRouteName(routeNameInfo: any, raw: any) {\n const [, operationId] = raw.operationId.split(\"_\");\n\n const name = raw.moduleName === \"oidc\" ? camelCase(routeNameInfo.original.replace(\"oidc\", \"\")) : camelCase(operationId);\n\n routeNameInfo.usage = name;\n routeNameInfo.original = name;\n raw.operationId = name;\n\n return routeNameInfo;\n },\n onParseSchema(originalSchema: any, parsedSchema: any) {\n const {content} = parsedSchema;\n\n if (isString(content)) {\n if (content.includes(\"null\") && content.includes(\"any\") && content.includes(\"Record\")) {\n parsedSchema.content = \"any\";\n }\n\n parsedSchema.content = parsedSchema.content.replace(\"object\", \"Record<string, any>\");\n }\n\n return parsedSchema;\n }\n }\n } as any);\n\n const promises = files.map(({content, name}) => {\n content = content\n .replace(\"class Api\", `class ${$ctx.name}`)\n .replace(\".then((response) => response.data)\", \".then((response) => response.data as T)\")\n .replace('requestParams.headers.common = { Accept: \"*/*\" };', \"\")\n .replace(\"requestParams.headers.post = {};\", \"\")\n .replace(\"requestParams.headers.put = {};\", \"\")\n .replace(\"(this.instance.defaults.headers || {})\", \"((this.instance.defaults.headers || {}) as any)\");\n\n console.log(`${$ctx.output}/${name}`, path.resolve(`${$ctx.output}/${name}`));\n return this.fs.writeFile(`${$ctx.output}/${name}`, content, {encoding: \"utf8\"});\n });\n\n return Promise.all(promises);\n }\n}\n"]}
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kCAAkC,CAAC;AAEjD,eAAe,EAAE,CAAC","sourcesContent":["export * from \"./commands/GenerateHttpClientCmd\";\n\nexport default {};\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { CliFs, CommandProvider, Type } from "@tsed/cli-core";
|
|
2
|
+
import { InjectorService } from "@tsed/di";
|
|
3
|
+
interface GenerateHttpClientCtx {
|
|
4
|
+
output: string;
|
|
5
|
+
type: "axios" | "fetch";
|
|
6
|
+
name: string;
|
|
7
|
+
suffix: string;
|
|
8
|
+
}
|
|
9
|
+
export declare class GenerateHttpClientCmd implements CommandProvider {
|
|
10
|
+
injector: InjectorService;
|
|
11
|
+
protected fs: CliFs;
|
|
12
|
+
protected serverModule: Type<any>;
|
|
13
|
+
$mapContext($ctx: GenerateHttpClientCtx): {
|
|
14
|
+
output: string;
|
|
15
|
+
type: "axios" | "fetch";
|
|
16
|
+
name: string;
|
|
17
|
+
suffix: string;
|
|
18
|
+
};
|
|
19
|
+
$exec($ctx: GenerateHttpClientCtx): Promise<{
|
|
20
|
+
title: string;
|
|
21
|
+
task: () => Promise<void>;
|
|
22
|
+
}[]>;
|
|
23
|
+
private loadPlatformModule;
|
|
24
|
+
private generate;
|
|
25
|
+
private generateFromSpec;
|
|
26
|
+
}
|
|
27
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tsed/cli-generate-http-client",
|
|
3
|
+
"version": "3.22.1",
|
|
4
|
+
"description": "Ts.ED CLI plugin. Export a command that generate an HTTP Client (axios or fetch) from your Ts.ED controllers and OS3 spec.",
|
|
5
|
+
"source": "./src/index.ts",
|
|
6
|
+
"main": "./lib/cjs/index.js",
|
|
7
|
+
"module": "./lib/esm/index.js",
|
|
8
|
+
"typings": "./lib/types/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
"types": "./lib/types/index.d.ts",
|
|
11
|
+
"import": "./lib/esm/index.js",
|
|
12
|
+
"require": "./lib/cjs/index.js",
|
|
13
|
+
"default": "./lib/esm/index.js"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "yarn run build:esm && yarn run build:cjs",
|
|
17
|
+
"build:cjs": "tsc --build tsconfig.compile.json",
|
|
18
|
+
"build:esm": "tsc --build tsconfig.compile.esm.json"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"change-case": "4.1.2",
|
|
22
|
+
"tslib": "2.3.1"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@tsed/cli": "3.22.1",
|
|
26
|
+
"@tsed/cli-core": "3.22.1",
|
|
27
|
+
"swagger-typescript-api": "^9.3.1"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@tsed/common": ">=6.114.14",
|
|
31
|
+
"@tsed/swagger": ">=6.114.14",
|
|
32
|
+
"swagger-typescript-api": "^9.3.1"
|
|
33
|
+
},
|
|
34
|
+
"peerDependenciesMeta": {
|
|
35
|
+
"@tsed/swagger": {
|
|
36
|
+
"optional": false
|
|
37
|
+
},
|
|
38
|
+
"swagger-typescript-api": {
|
|
39
|
+
"optional": false
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"repository": "https://github.com/tsedio/tsed-cli",
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/tsedio/tsed-cli/issues"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://github.com/tsedio/tsed-cli/tree/master/packages/cli-generate-http-client",
|
|
47
|
+
"author": "Romain Lenzotti",
|
|
48
|
+
"license": "MIT"
|
|
49
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
<p style="text-align: center" align="center">
|
|
2
|
+
<a href="https://tsed.io" target="_blank"><img src="https://tsed.io/tsed-og.png" width="200" alt="Ts.ED logo"/></a>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
<h1>Ts.ED CLI - Generate Http Client</h1>
|
|
7
|
+
|
|
8
|
+
[](https://github.com/tsedio/tsed-cli/actions?query=workflow%3A%22Build+%26+Release%22)
|
|
9
|
+
[](https://github.com/tsedio/tsed-cli/blob/master/CONTRIBUTING.md)
|
|
10
|
+
[](https://github.com/semantic-release/semantic-release)
|
|
11
|
+
[](https://github.com/prettier/prettier)
|
|
12
|
+
[](https://opencollective.com/tsed)
|
|
13
|
+
|
|
14
|
+
<br />
|
|
15
|
+
<div align="center">
|
|
16
|
+
<a href="https://tsed.io/">Website</a>
|
|
17
|
+
<span> • </span>
|
|
18
|
+
<a href="https://tsed.io/getting-started.html">Getting started</a>
|
|
19
|
+
<span> • </span>
|
|
20
|
+
<a href="https://api.tsed.io/rest/slack/tsedio/tsed">Slack</a>
|
|
21
|
+
<span> • </span>
|
|
22
|
+
<a href="https://twitter.com/TsED_io">Twitter</a>
|
|
23
|
+
</div>
|
|
24
|
+
<hr />
|
|
25
|
+
</div>
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
> Ts.ED CLI plugin. Generate your HttpClient from OpenApiSpec!
|
|
29
|
+
|
|
30
|
+
## Features
|
|
31
|
+
|
|
32
|
+
The plugin let you generate your HttpClient (axios or fetch) from the Ts.ED Controller and the OpenApiSpec.
|
|
33
|
+
Any changes from your controllers and models are reflected in your client!
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
Run:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install @tsed/cli-generate-client @tsed/cli-core @tsed/cli @tsed/swagger swagger-typescript-api
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Then create or edit the `src/bin/index.ts` in your project:
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
#!/usr/bin/env node
|
|
47
|
+
import { CliCore } from "@tsed/cli-core";
|
|
48
|
+
import {GenerateHttpClientCmd} from "@tsed/cli-generate-http-client";
|
|
49
|
+
import { config } from "../config";
|
|
50
|
+
import { Server } from "../Server";
|
|
51
|
+
|
|
52
|
+
CliCore.bootstrap({
|
|
53
|
+
...config,
|
|
54
|
+
server: Server,
|
|
55
|
+
// add your custom commands here
|
|
56
|
+
commands: [GenerateHttpClientCmd]
|
|
57
|
+
}).catch(console.error);
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Add a new script your `package.json`:
|
|
61
|
+
|
|
62
|
+
```json
|
|
63
|
+
{
|
|
64
|
+
"scripts": {
|
|
65
|
+
"build:client": "yarn tsed run generate-http-client --output ../client/src/__generated__"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Contributors
|
|
71
|
+
|
|
72
|
+
Please read [contributing guidelines here](https://tsed.io/CONTRIBUTING.html)
|
|
73
|
+
|
|
74
|
+
<a href="https://github.com/tsedio/ts-express-decorators/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
## Backers
|
|
78
|
+
|
|
79
|
+
Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]
|
|
80
|
+
|
|
81
|
+
<a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/tiers/backer.svg?width=890"></a>
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
## Sponsors
|
|
85
|
+
|
|
86
|
+
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
The MIT License (MIT)
|
|
91
|
+
|
|
92
|
+
Copyright (c) 2016 - 2018 Romain Lenzotti
|
|
93
|
+
|
|
94
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
95
|
+
|
|
96
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
97
|
+
|
|
98
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.compile.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"baseUrl": ".",
|
|
5
|
+
"module": "ES2020",
|
|
6
|
+
"rootDir": "src",
|
|
7
|
+
"outDir": "./lib/esm",
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"declarationDir": "./lib/types"
|
|
10
|
+
},
|
|
11
|
+
"exclude": [
|
|
12
|
+
"node_modules",
|
|
13
|
+
"test",
|
|
14
|
+
"lib",
|
|
15
|
+
"coverage",
|
|
16
|
+
".nyc_output",
|
|
17
|
+
"**/*.spec.ts",
|
|
18
|
+
"jest.config.js",
|
|
19
|
+
"templates"
|
|
20
|
+
]
|
|
21
|
+
}
|