Skip to content

Commit bd02072

Browse files
author
mkefd
committed
mappersmith
1 parent a52271e commit bd02072

File tree

10 files changed

+76
-10
lines changed

10 files changed

+76
-10
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
"glob": "10.2.1",
109109
"jest": "29.5.0",
110110
"jest-cli": "29.5.0",
111+
"mappersmith": "^2.38.1",
111112
"node-fetch": "2.6.9",
112113
"prettier": "2.8.7",
113114
"puppeteer": "19.10.1",
@@ -121,7 +122,7 @@
121122
"typescript": "4.9.5",
122123
"zone.js": "0.13.0"
123124
},
124-
"overrides" : {
125+
"overrides": {
125126
"node-fetch": "2.6.9",
126127
"rollup": "3.20.2",
127128
"typescript": "4.9.5"

src/HttpClient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export enum HttpClient {
44
NODE = 'node',
55
AXIOS = 'axios',
66
ANGULAR = 'angular',
7+
MAPPERSMITH = 'mappersmith',
78
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import forge, { Client } from 'mappersmith';
2+
3+
export type {{{clientName}}}Resource = {
4+
{{#each services}}
5+
{{name}}: {
6+
{{#each operations}}
7+
{{name}}: {
8+
method: string;
9+
path: string;
10+
};
11+
{{/each}}
12+
};
13+
{{/each}}
14+
};
15+
16+
export const {{{camelCase clientName}}}MappersmithClientFactory = (host: string): Client<{{{clientName}}}Resource> =>
17+
forge({
18+
clientId: '{{clientName}}',
19+
host,
20+
resources: {
21+
{{#each services}}
22+
{{name}}: {
23+
{{#each operations}}
24+
{{name}}: { method: '{{method}}', path: '{{path}}' },
25+
{{/each}}
26+
},
27+
{{/each}}
28+
},
29+
});

src/templates/core/request.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
{{~#equals @root.httpClient 'axios'}}{{>axios/request}}{{/equals~}}
44
{{~#equals @root.httpClient 'angular'}}{{>angular/request}}{{/equals~}}
55
{{~#equals @root.httpClient 'node'}}{{>node/request}}{{/equals~}}
6+
{{~#equals @root.httpClient 'mappersmith'}}{{>mappersmith/request}}{{/equals~}}

src/templates/exportService.hbs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import type { {{{this}}} } from '../models/{{{this}}}';
1717
{{/each}}
1818

1919
{{/if}}
20+
{{#equals @root.httpClient 'mappersmith'}}
21+
import { Client } from 'mappersmith'
22+
import type { {{{clientName}}}Resource } from '../core/request';
23+
{{else}}
2024
{{#notEquals @root.httpClient 'angular'}}
2125
import type { CancelablePromise } from '../core/CancelablePromise';
2226
{{/notEquals}}
@@ -30,13 +34,21 @@ import type { BaseHttpRequest } from '../core/BaseHttpRequest';
3034
import { OpenAPI } from '../core/OpenAPI';
3135
import { request as __request } from '../core/request';
3236
{{/if}}
37+
{{/equals}}
3338

3439
{{#equals @root.httpClient 'angular'}}
3540
@Injectable({
3641
providedIn: 'root',
3742
})
3843
{{/equals}}
3944
export class {{{name}}}{{{@root.postfix}}} {
45+
{{#equals @root.httpClient 'mappersmith'}}
46+
private mappersmithClient: Client<{{{clientName}}}Resource>
47+
48+
constructor(mappersmithClient: Client<{{{clientName}}}Resource>) {
49+
this.mappersmithClient = mappersmithClient
50+
}
51+
{{else}}
4052
{{#if @root.exportClient}}
4153

4254
constructor(public readonly httpRequest: BaseHttpRequest) {}
@@ -46,6 +58,7 @@ export class {{{name}}}{{{@root.postfix}}} {
4658
constructor(public readonly http: HttpClient) {}
4759
{{/equals}}
4860
{{/if}}
61+
{{/equals}}
4962

5063
{{#each operations}}
5164
/**
@@ -70,6 +83,16 @@ export class {{{name}}}{{{@root.postfix}}} {
7083
{{/each}}
7184
* @throws ApiError
7285
*/
86+
{{#equals @root.httpClient 'mappersmith'}}
87+
public async {{{name}}}({{>parameters}}): Promise<{{>result}}> {
88+
return (await this.mappersmithClient.{{{../name}}}.{{{name}}}(
89+
{{#if parametersPath}}
90+
{ {{#each parametersPath}}"{{prop}}": {{name}},{{/each}} }
91+
{{/if}}
92+
{{#if parametersBody}}{{#if parametersPath}},{{/if}}{{parametersBody.name}}{{/if}}
93+
)).data()
94+
}
95+
{{else}}
7396
{{#if @root.exportClient}}
7497
{{#equals @root.httpClient 'angular'}}
7598
public {{{name}}}({{>parameters}}): Observable<{{>result}}> {
@@ -148,5 +171,6 @@ export class {{{name}}}{{{@root.postfix}}} {
148171
});
149172
}
150173

174+
{{/equals}}
151175
{{/each}}
152176
}

src/utils/registerHandlebarTemplates.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import functionIsStringWithValue from '../templates/core/functions/isStringWithV
3838
import functionIsSuccess from '../templates/core/functions/isSuccess.hbs';
3939
import functionResolve from '../templates/core/functions/resolve.hbs';
4040
import templateCoreHttpRequest from '../templates/core/HttpRequest.hbs';
41+
import mappersmithRequest from '../templates/core/mappersmith/request.hbs';
4142
import nodeGetHeaders from '../templates/core/node/getHeaders.hbs';
4243
import nodeGetRequestBody from '../templates/core/node/getRequestBody.hbs';
4344
import nodeGetResponseBody from '../templates/core/node/getResponseBody.hbs';
@@ -220,5 +221,8 @@ export const registerHandlebarTemplates = (root: {
220221
Handlebars.registerPartial('angular/sendRequest', Handlebars.template(angularSendRequest));
221222
Handlebars.registerPartial('angular/request', Handlebars.template(angularRequest));
222223

224+
// Specific files for the mappersmith client implementation
225+
Handlebars.registerPartial('mappersmith/request', Handlebars.template(mappersmithRequest));
226+
223227
return templates;
224228
};

src/utils/writeClient.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { resolve } from 'path';
22

33
import type { Client } from '../client/interfaces/Client';
4-
import type { HttpClient } from '../HttpClient';
4+
import { HttpClient } from '../HttpClient';
55
import type { Indent } from '../Indent';
66
import { mkdir, rmdir } from './fileSystem';
77
import { isDefined } from './isDefined';
@@ -94,7 +94,7 @@ export const writeClient = async (
9494
await writeClientModels(client.models, templates, outputPathModels, httpClient, useUnionTypes, indent);
9595
}
9696

97-
if (isDefined(clientName)) {
97+
if (isDefined(clientName) && httpClient !== HttpClient.MAPPERSMITH) {
9898
await mkdir(outputPath);
9999
await writeClientClass(client, templates, outputPath, httpClient, clientName, indent, postfixServices);
100100
}
@@ -112,7 +112,8 @@ export const writeClient = async (
112112
exportSchemas,
113113
postfixServices,
114114
postfixModels,
115-
clientName
115+
clientName,
116+
httpClient
116117
);
117118
}
118119
};

src/utils/writeClientCore.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { resolve } from 'path';
22

33
import type { Client } from '../client/interfaces/Client';
4-
import type { HttpClient } from '../HttpClient';
4+
import { HttpClient } from '../HttpClient';
55
import type { Indent } from '../Indent';
66
import { copyFile, exists, writeFile } from './fileSystem';
77
import { formatIndentation as i } from './formatIndentation';
@@ -35,6 +35,7 @@ export const writeClientCore = async (
3535
httpRequest,
3636
server: client.server,
3737
version: client.version,
38+
services: client.services,
3839
};
3940

4041
await writeFile(resolve(outputPath, 'OpenAPI.ts'), i(templates.core.settings(context), indent));
@@ -44,7 +45,7 @@ export const writeClientCore = async (
4445
await writeFile(resolve(outputPath, 'CancelablePromise.ts'), i(templates.core.cancelablePromise(context), indent));
4546
await writeFile(resolve(outputPath, 'request.ts'), i(templates.core.request(context), indent));
4647

47-
if (isDefined(clientName)) {
48+
if (isDefined(clientName) && httpClient !== HttpClient.MAPPERSMITH) {
4849
await writeFile(resolve(outputPath, 'BaseHttpRequest.ts'), i(templates.core.baseHttpRequest(context), indent));
4950
await writeFile(resolve(outputPath, `${httpRequest}.ts`), i(templates.core.httpRequest(context), indent));
5051
}

src/utils/writeClientIndex.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { isDefined } from './isDefined';
66
import { Templates } from './registerHandlebarTemplates';
77
import { sortModelsByName } from './sortModelsByName';
88
import { sortServicesByName } from './sortServicesByName';
9+
import {HttpClient} from '../HttpClient'
910

1011
/**
1112
* Generate the OpenAPI client index file using the Handlebar template and write it to disk.
@@ -22,6 +23,7 @@ import { sortServicesByName } from './sortServicesByName';
2223
* @param postfixServices Service name postfix
2324
* @param postfixModels Model name postfix
2425
* @param clientName Custom client class name
26+
* @param httpClient The selected httpClient (fetch, xhr, node, axios or mappersmith)
2527
*/
2628
export const writeClientIndex = async (
2729
client: Client,
@@ -34,7 +36,8 @@ export const writeClientIndex = async (
3436
exportSchemas: boolean,
3537
postfixServices: string,
3638
postfixModels: string,
37-
clientName?: string
39+
clientName?: string,
40+
httpClient?: string
3841
): Promise<void> => {
3942
const templateResult = templates.index({
4043
exportCore,
@@ -49,7 +52,7 @@ export const writeClientIndex = async (
4952
version: client.version,
5053
models: sortModelsByName(client.models),
5154
services: sortServicesByName(client.services),
52-
exportClient: isDefined(clientName),
55+
exportClient: isDefined(clientName) && httpClient !== HttpClient.MAPPERSMITH,
5356
});
5457

5558
await writeFile(resolve(outputPath, 'index.ts'), templateResult);

src/utils/writeClientServices.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { resolve } from 'path';
22

33
import type { Service } from '../client/interfaces/Service';
4-
import type { HttpClient } from '../HttpClient';
4+
import { HttpClient } from '../HttpClient';
55
import type { Indent } from '../Indent';
66
import { writeFile } from './fileSystem';
77
import { formatCode as f } from './formatCode';
@@ -40,7 +40,8 @@ export const writeClientServices = async (
4040
useUnionTypes,
4141
useOptions,
4242
postfix,
43-
exportClient: isDefined(clientName),
43+
exportClient: isDefined(clientName) && httpClient !== HttpClient.MAPPERSMITH,
44+
clientName,
4445
});
4546
await writeFile(file, i(f(templateResult), indent));
4647
}

0 commit comments

Comments
 (0)