@goast/typescript 0.2.6 → 0.2.8
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/assets/client/angular/angular-service.utils.ts +26 -0
- package/cjs/lib/ast/references/angular.js +2 -1
- package/cjs/lib/generators/models/model-generator.js +6 -1
- package/cjs/lib/generators/services/angular-services/angular-service-generator.js +55 -24
- package/cjs/lib/generators/services/angular-services/angular-services-generator.js +2 -1
- package/cjs/lib/generators/services/angular-services/models.js +3 -4
- package/cjs/lib/generators/services/angular-services/refs.js +2 -0
- package/esm/lib/ast/references/angular.js +1 -0
- package/esm/lib/generators/models/model-generator.js +6 -1
- package/esm/lib/generators/services/angular-services/angular-service-generator.js +55 -24
- package/esm/lib/generators/services/angular-services/angular-services-generator.js +2 -1
- package/esm/lib/generators/services/angular-services/models.js +3 -4
- package/esm/lib/generators/services/angular-services/refs.js +2 -0
- package/package.json +1 -1
- package/types/lib/ast/references/angular.d.ts +6 -0
- package/types/lib/generators/services/angular-services/angular-service-generator.d.ts +5 -1
- package/types/lib/generators/services/angular-services/models.d.ts +18 -9
- package/types/lib/generators/services/angular-services/refs.d.ts +12 -5
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { HttpErrorResponse, HttpEvent, HttpResponse } from '@angular/common/http';
|
|
2
|
+
import { Observable, filter, firstValueFrom } from 'rxjs';
|
|
3
|
+
|
|
4
|
+
export async function waitForResponse<T extends HttpResponse<any> | HttpErrorResponse>(
|
|
5
|
+
httpEvents: Observable<HttpEvent<any>>,
|
|
6
|
+
options: { errorResponseTypes: Record<number, 'text' | 'json'> },
|
|
7
|
+
): Promise<T> {
|
|
8
|
+
try {
|
|
9
|
+
const response = await firstValueFrom(httpEvents.pipe(filter((event) => event instanceof HttpResponse)));
|
|
10
|
+
return response as T;
|
|
11
|
+
} catch (error) {
|
|
12
|
+
if (error instanceof HttpErrorResponse) {
|
|
13
|
+
if (options.errorResponseTypes[error.status] === 'json' && typeof error.error === 'string') {
|
|
14
|
+
return new HttpErrorResponse({
|
|
15
|
+
error: JSON.parse(error.error),
|
|
16
|
+
headers: error.headers,
|
|
17
|
+
status: error.status,
|
|
18
|
+
statusText: error.statusText,
|
|
19
|
+
url: error.url ?? undefined,
|
|
20
|
+
}) as T;
|
|
21
|
+
}
|
|
22
|
+
return error as T;
|
|
23
|
+
}
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.httpResponse = exports.httpContext = exports.httpClient = exports.provider = exports.injectable = exports.inject = void 0;
|
|
3
|
+
exports.httpErrorResponse = exports.httpResponse = exports.httpContext = exports.httpClient = exports.provider = exports.injectable = exports.inject = void 0;
|
|
4
4
|
const reference_1 = require("../nodes/reference");
|
|
5
5
|
// @angular/core
|
|
6
6
|
exports.inject = reference_1.tsReference.factory('inject', '@angular/core');
|
|
@@ -10,3 +10,4 @@ exports.provider = reference_1.tsReference.factory('Provider', '@angular/core');
|
|
|
10
10
|
exports.httpClient = reference_1.tsReference.factory('HttpClient', '@angular/common/http');
|
|
11
11
|
exports.httpContext = reference_1.tsReference.factory('HttpContext', '@angular/common/http');
|
|
12
12
|
exports.httpResponse = reference_1.tsReference.genericFactory('HttpResponse', '@angular/common/http');
|
|
13
|
+
exports.httpErrorResponse = reference_1.tsReference.factory('HttpErrorResponse', '@angular/common/http');
|
|
@@ -162,7 +162,12 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
|
|
|
162
162
|
members: Object.entries(discriminator.mapping).map(([key, value]) => ast_1.ts.property(key, {
|
|
163
163
|
type: ast_1.ts.intersectionType([
|
|
164
164
|
ast_1.ts.objectType({
|
|
165
|
-
members: [
|
|
165
|
+
members: [
|
|
166
|
+
ast_1.ts.property(discriminator.propertyName, {
|
|
167
|
+
type: ast_1.ts.string(key),
|
|
168
|
+
readonly: ctx.config.immutableTypes,
|
|
169
|
+
}),
|
|
170
|
+
],
|
|
166
171
|
}),
|
|
167
172
|
this.getType(ctx, value),
|
|
168
173
|
]),
|
|
@@ -50,15 +50,19 @@ class DefaultTypeScriptAngularServiceGenerator extends file_generator_1.TypeScri
|
|
|
50
50
|
}
|
|
51
51
|
result.values.push(ast_1.ts.typeAlias(this.getResponseModelName(ctx, endpoint), ast_1.ts.intersectionType([
|
|
52
52
|
ast_1.ts.unionType([
|
|
53
|
-
...Object.entries(statusCodes).map(([key, value]) =>
|
|
54
|
-
|
|
55
|
-
ast_1.ts.
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
53
|
+
...Object.entries(statusCodes).map(([key, value]) => {
|
|
54
|
+
const isSuccess = key.startsWith('2');
|
|
55
|
+
return ast_1.ts.intersectionType([
|
|
56
|
+
isSuccess ? ast_1.ts.refs.angular.httpResponse([value.type]) : ast_1.ts.refs.angular.httpErrorResponse(),
|
|
57
|
+
ast_1.ts.objectType({
|
|
58
|
+
members: [
|
|
59
|
+
isSuccess ? null : ast_1.ts.property('error', { type: ast_1.ts.unionType([value.type, ast_1.ts.refs.null_()]) }),
|
|
60
|
+
ast_1.ts.property('status', { type: key }),
|
|
61
|
+
ast_1.ts.property('ok', { type: isSuccess ? 'true' : 'false' }),
|
|
62
|
+
],
|
|
63
|
+
}),
|
|
64
|
+
]);
|
|
65
|
+
}),
|
|
62
66
|
!ctx.config.strictResponseTypes
|
|
63
67
|
? ast_1.ts.intersectionType([
|
|
64
68
|
ast_1.ts.refs.angular.httpResponse([this.getAnyType(ctx)]),
|
|
@@ -86,13 +90,30 @@ class DefaultTypeScriptAngularServiceGenerator extends file_generator_1.TypeScri
|
|
|
86
90
|
}
|
|
87
91
|
getEndpointStatusCodes(ctx, endpoint) {
|
|
88
92
|
return Object.fromEntries([
|
|
89
|
-
...Object.entries(ctx.config.defaultStatusCodeResponseTypes).map(([key, value]) =>
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
+
...Object.entries(ctx.config.defaultStatusCodeResponseTypes).map(([key, value]) => {
|
|
94
|
+
var _a;
|
|
95
|
+
return [
|
|
96
|
+
key,
|
|
97
|
+
{
|
|
98
|
+
parser: (_a = value === null || value === void 0 ? void 0 : value.parser) !== null && _a !== void 0 ? _a : 'text',
|
|
99
|
+
type: typeof (value === null || value === void 0 ? void 0 : value.type) === 'function'
|
|
100
|
+
? this.getSchemaType(ctx, value.type(ctx.data.schemas))
|
|
101
|
+
: value !== null && value !== void 0 ? value : ast_1.ts.refs.never(),
|
|
102
|
+
},
|
|
103
|
+
];
|
|
104
|
+
}),
|
|
93
105
|
...endpoint.responses
|
|
94
106
|
.filter((x) => x.statusCode)
|
|
95
|
-
.map((x) => {
|
|
107
|
+
.map((x) => {
|
|
108
|
+
var _a, _b;
|
|
109
|
+
return [
|
|
110
|
+
x.statusCode,
|
|
111
|
+
{
|
|
112
|
+
parser: this.contentTypeToResponseType((_a = x.contentOptions[0]) === null || _a === void 0 ? void 0 : _a.type),
|
|
113
|
+
type: this.getSchemaType(ctx, (_b = x.contentOptions[0]) === null || _b === void 0 ? void 0 : _b.schema, ast_1.ts.refs.never()),
|
|
114
|
+
},
|
|
115
|
+
];
|
|
116
|
+
}),
|
|
96
117
|
]);
|
|
97
118
|
}
|
|
98
119
|
// #endregion
|
|
@@ -150,11 +171,10 @@ class DefaultTypeScriptAngularServiceGenerator extends file_generator_1.TypeScri
|
|
|
150
171
|
var _a, _b, _c, _d;
|
|
151
172
|
const hasParams = this.hasEndpointParams(ctx, endpoint);
|
|
152
173
|
const paramsOptional = !endpoint.parameters.some((p) => p.required) && !((_a = endpoint.requestBody) === null || _a === void 0 ? void 0 : _a.required);
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
]);
|
|
174
|
+
const responseModelType = ast_1.ts.reference(this.getResponseModelName(ctx, endpoint), this.getResponseModelFilePath(ctx));
|
|
175
|
+
const returnType = ast_1.ts.refs.promise([responseModelType]);
|
|
156
176
|
const accept = (_d = (_c = (_b = this.getEndpointSuccessResponse(ctx, endpoint)) === null || _b === void 0 ? void 0 : _b.contentOptions[0]) === null || _c === void 0 ? void 0 : _c.type) !== null && _d !== void 0 ? _d : '*/*';
|
|
157
|
-
const responseType =
|
|
177
|
+
const responseType = this.contentTypeToResponseType(accept);
|
|
158
178
|
return ast_1.ts.method(this.getEndpointMethodName(ctx, endpoint), {
|
|
159
179
|
accessModifier: 'public',
|
|
160
180
|
parameters: [
|
|
@@ -191,16 +211,24 @@ class DefaultTypeScriptAngularServiceGenerator extends file_generator_1.TypeScri
|
|
|
191
211
|
}
|
|
192
212
|
: null,
|
|
193
213
|
'',
|
|
194
|
-
(0, core_1.builderTemplate) `return ${
|
|
214
|
+
(0, core_1.builderTemplate) `return ${ctx.refs.waitForResponse([responseModelType])}(${core_1.builderTemplate.indent `
|
|
195
215
|
this.http.request(rb.build({${core_1.builderTemplate.indent `
|
|
196
216
|
responseType: ${ast_1.ts.string(responseType)},
|
|
197
217
|
accept: ${ast_1.ts.string(accept)},
|
|
198
218
|
context,`}
|
|
199
|
-
}))
|
|
200
|
-
|
|
201
|
-
${ast_1.ts.
|
|
202
|
-
|
|
203
|
-
|
|
219
|
+
})),
|
|
220
|
+
{${core_1.builderTemplate.indent `
|
|
221
|
+
errorResponseTypes: ${ast_1.ts.object({
|
|
222
|
+
members: [
|
|
223
|
+
...Object.entries(this.getEndpointStatusCodes(ctx, endpoint))
|
|
224
|
+
.filter(([key]) => !key.startsWith('2'))
|
|
225
|
+
.map(([key, value]) => {
|
|
226
|
+
return ast_1.ts.property(key, { value: ast_1.ts.string(value.parser) });
|
|
227
|
+
}),
|
|
228
|
+
],
|
|
229
|
+
})}`}
|
|
230
|
+
}`}
|
|
231
|
+
)`,
|
|
204
232
|
], '\n'),
|
|
205
233
|
});
|
|
206
234
|
}
|
|
@@ -238,5 +266,8 @@ class DefaultTypeScriptAngularServiceGenerator extends file_generator_1.TypeScri
|
|
|
238
266
|
return fallback !== null && fallback !== void 0 ? fallback : this.getAnyType(ctx);
|
|
239
267
|
return (b) => b.appendModelUsage(output);
|
|
240
268
|
}
|
|
269
|
+
contentTypeToResponseType(contentType) {
|
|
270
|
+
return (contentType === null || contentType === void 0 ? void 0 : contentType.includes('json')) ? 'json' : 'text';
|
|
271
|
+
}
|
|
241
272
|
}
|
|
242
273
|
exports.DefaultTypeScriptAngularServiceGenerator = DefaultTypeScriptAngularServiceGenerator;
|
|
@@ -46,7 +46,8 @@ class TypeScriptAngularServicesGenerator extends core_1.OpenApiServicesGeneratio
|
|
|
46
46
|
const sourceDir = (0, path_1.resolve)((0, path_1.dirname)(require.resolve('@goast/typescript')), '../assets/client/angular');
|
|
47
47
|
const targetDir = (0, path_1.resolve)(ctx.config.outputDir, ctx.config.utilsDirPath);
|
|
48
48
|
(0, fs_extra_1.ensureDirSync)(targetDir);
|
|
49
|
-
this.copyFile('
|
|
49
|
+
this.copyFile('Request Builder', sourceDir, targetDir, 'request-builder.ts');
|
|
50
|
+
this.copyFile('Angular Service Utils', sourceDir, targetDir, 'angular-service.utils.ts');
|
|
50
51
|
file_builder_1.TypeScriptFileBuilder.generate({
|
|
51
52
|
logName: 'api configuration',
|
|
52
53
|
filePath: (0, path_1.resolve)(targetDir, 'api-configuration.ts'),
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.defaultTypeScriptAngularServicesGeneratorConfig = void 0;
|
|
4
|
-
const ast_1 = require("../../../ast");
|
|
5
4
|
const config_1 = require("../../../config");
|
|
6
5
|
exports.defaultTypeScriptAngularServicesGeneratorConfig = Object.assign(Object.assign({}, config_1.defaultTypeScriptGeneratorConfig), { fileNameCasing: { casing: 'kebab', suffix: '.service' }, responseModelFileNameCasing: { casing: 'kebab', suffix: '-responses.model' }, provideKind: 'provide-fn', strictResponseTypes: true, defaultStatusCodeResponseTypes: {
|
|
7
|
-
401:
|
|
8
|
-
403:
|
|
9
|
-
500:
|
|
6
|
+
401: null,
|
|
7
|
+
403: null,
|
|
8
|
+
500: null,
|
|
10
9
|
}, possibleStatusCodes: [
|
|
11
10
|
100, 101, 102, 103, 200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, 302, 303, 304, 305, 306, 307, 308,
|
|
12
11
|
400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 421, 422, 423, 424, 425,
|
|
@@ -9,6 +9,8 @@ function getReferenceFactories(options) {
|
|
|
9
9
|
var _a, _b;
|
|
10
10
|
const utilsDirPath = (0, path_1.resolve)(options.outputDir, options.utilsDirPath);
|
|
11
11
|
return {
|
|
12
|
+
// angular-service.utils.ts
|
|
13
|
+
waitForResponse: ast_1.ts.reference.genericFactory('waitForResponse', (0, path_1.join)(utilsDirPath, 'angular-service.utils.ts')),
|
|
12
14
|
// request-builder.ts
|
|
13
15
|
requestBuilder: ast_1.ts.reference.factory('RequestBuilder', (0, path_1.join)(utilsDirPath, 'request-builder.ts')),
|
|
14
16
|
// api-configuration.ts - generated in angular-services-generator.ts
|
|
@@ -7,3 +7,4 @@ export const provider = tsReference.factory('Provider', '@angular/core');
|
|
|
7
7
|
export const httpClient = tsReference.factory('HttpClient', '@angular/common/http');
|
|
8
8
|
export const httpContext = tsReference.factory('HttpContext', '@angular/common/http');
|
|
9
9
|
export const httpResponse = tsReference.genericFactory('HttpResponse', '@angular/common/http');
|
|
10
|
+
export const httpErrorResponse = tsReference.factory('HttpErrorResponse', '@angular/common/http');
|
|
@@ -159,7 +159,12 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator {
|
|
|
159
159
|
members: Object.entries(discriminator.mapping).map(([key, value]) => ts.property(key, {
|
|
160
160
|
type: ts.intersectionType([
|
|
161
161
|
ts.objectType({
|
|
162
|
-
members: [
|
|
162
|
+
members: [
|
|
163
|
+
ts.property(discriminator.propertyName, {
|
|
164
|
+
type: ts.string(key),
|
|
165
|
+
readonly: ctx.config.immutableTypes,
|
|
166
|
+
}),
|
|
167
|
+
],
|
|
163
168
|
}),
|
|
164
169
|
this.getType(ctx, value),
|
|
165
170
|
]),
|
|
@@ -47,15 +47,19 @@ export class DefaultTypeScriptAngularServiceGenerator extends TypeScriptFileGene
|
|
|
47
47
|
}
|
|
48
48
|
result.values.push(ts.typeAlias(this.getResponseModelName(ctx, endpoint), ts.intersectionType([
|
|
49
49
|
ts.unionType([
|
|
50
|
-
...Object.entries(statusCodes).map(([key, value]) =>
|
|
51
|
-
|
|
52
|
-
ts.
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
50
|
+
...Object.entries(statusCodes).map(([key, value]) => {
|
|
51
|
+
const isSuccess = key.startsWith('2');
|
|
52
|
+
return ts.intersectionType([
|
|
53
|
+
isSuccess ? ts.refs.angular.httpResponse([value.type]) : ts.refs.angular.httpErrorResponse(),
|
|
54
|
+
ts.objectType({
|
|
55
|
+
members: [
|
|
56
|
+
isSuccess ? null : ts.property('error', { type: ts.unionType([value.type, ts.refs.null_()]) }),
|
|
57
|
+
ts.property('status', { type: key }),
|
|
58
|
+
ts.property('ok', { type: isSuccess ? 'true' : 'false' }),
|
|
59
|
+
],
|
|
60
|
+
}),
|
|
61
|
+
]);
|
|
62
|
+
}),
|
|
59
63
|
!ctx.config.strictResponseTypes
|
|
60
64
|
? ts.intersectionType([
|
|
61
65
|
ts.refs.angular.httpResponse([this.getAnyType(ctx)]),
|
|
@@ -83,13 +87,30 @@ export class DefaultTypeScriptAngularServiceGenerator extends TypeScriptFileGene
|
|
|
83
87
|
}
|
|
84
88
|
getEndpointStatusCodes(ctx, endpoint) {
|
|
85
89
|
return Object.fromEntries([
|
|
86
|
-
...Object.entries(ctx.config.defaultStatusCodeResponseTypes).map(([key, value]) =>
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
+
...Object.entries(ctx.config.defaultStatusCodeResponseTypes).map(([key, value]) => {
|
|
91
|
+
var _a;
|
|
92
|
+
return [
|
|
93
|
+
key,
|
|
94
|
+
{
|
|
95
|
+
parser: (_a = value === null || value === void 0 ? void 0 : value.parser) !== null && _a !== void 0 ? _a : 'text',
|
|
96
|
+
type: typeof (value === null || value === void 0 ? void 0 : value.type) === 'function'
|
|
97
|
+
? this.getSchemaType(ctx, value.type(ctx.data.schemas))
|
|
98
|
+
: value !== null && value !== void 0 ? value : ts.refs.never(),
|
|
99
|
+
},
|
|
100
|
+
];
|
|
101
|
+
}),
|
|
90
102
|
...endpoint.responses
|
|
91
103
|
.filter((x) => x.statusCode)
|
|
92
|
-
.map((x) => {
|
|
104
|
+
.map((x) => {
|
|
105
|
+
var _a, _b;
|
|
106
|
+
return [
|
|
107
|
+
x.statusCode,
|
|
108
|
+
{
|
|
109
|
+
parser: this.contentTypeToResponseType((_a = x.contentOptions[0]) === null || _a === void 0 ? void 0 : _a.type),
|
|
110
|
+
type: this.getSchemaType(ctx, (_b = x.contentOptions[0]) === null || _b === void 0 ? void 0 : _b.schema, ts.refs.never()),
|
|
111
|
+
},
|
|
112
|
+
];
|
|
113
|
+
}),
|
|
93
114
|
]);
|
|
94
115
|
}
|
|
95
116
|
// #endregion
|
|
@@ -147,11 +168,10 @@ export class DefaultTypeScriptAngularServiceGenerator extends TypeScriptFileGene
|
|
|
147
168
|
var _a, _b, _c, _d;
|
|
148
169
|
const hasParams = this.hasEndpointParams(ctx, endpoint);
|
|
149
170
|
const paramsOptional = !endpoint.parameters.some((p) => p.required) && !((_a = endpoint.requestBody) === null || _a === void 0 ? void 0 : _a.required);
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
]);
|
|
171
|
+
const responseModelType = ts.reference(this.getResponseModelName(ctx, endpoint), this.getResponseModelFilePath(ctx));
|
|
172
|
+
const returnType = ts.refs.promise([responseModelType]);
|
|
153
173
|
const accept = (_d = (_c = (_b = this.getEndpointSuccessResponse(ctx, endpoint)) === null || _b === void 0 ? void 0 : _b.contentOptions[0]) === null || _c === void 0 ? void 0 : _c.type) !== null && _d !== void 0 ? _d : '*/*';
|
|
154
|
-
const responseType =
|
|
174
|
+
const responseType = this.contentTypeToResponseType(accept);
|
|
155
175
|
return ts.method(this.getEndpointMethodName(ctx, endpoint), {
|
|
156
176
|
accessModifier: 'public',
|
|
157
177
|
parameters: [
|
|
@@ -188,16 +208,24 @@ export class DefaultTypeScriptAngularServiceGenerator extends TypeScriptFileGene
|
|
|
188
208
|
}
|
|
189
209
|
: null,
|
|
190
210
|
'',
|
|
191
|
-
s `return ${
|
|
211
|
+
s `return ${ctx.refs.waitForResponse([responseModelType])}(${s.indent `
|
|
192
212
|
this.http.request(rb.build({${s.indent `
|
|
193
213
|
responseType: ${ts.string(responseType)},
|
|
194
214
|
accept: ${ts.string(accept)},
|
|
195
215
|
context,`}
|
|
196
|
-
}))
|
|
197
|
-
|
|
198
|
-
${ts.
|
|
199
|
-
|
|
200
|
-
|
|
216
|
+
})),
|
|
217
|
+
{${s.indent `
|
|
218
|
+
errorResponseTypes: ${ts.object({
|
|
219
|
+
members: [
|
|
220
|
+
...Object.entries(this.getEndpointStatusCodes(ctx, endpoint))
|
|
221
|
+
.filter(([key]) => !key.startsWith('2'))
|
|
222
|
+
.map(([key, value]) => {
|
|
223
|
+
return ts.property(key, { value: ts.string(value.parser) });
|
|
224
|
+
}),
|
|
225
|
+
],
|
|
226
|
+
})}`}
|
|
227
|
+
}`}
|
|
228
|
+
)`,
|
|
201
229
|
], '\n'),
|
|
202
230
|
});
|
|
203
231
|
}
|
|
@@ -235,4 +263,7 @@ export class DefaultTypeScriptAngularServiceGenerator extends TypeScriptFileGene
|
|
|
235
263
|
return fallback !== null && fallback !== void 0 ? fallback : this.getAnyType(ctx);
|
|
236
264
|
return (b) => b.appendModelUsage(output);
|
|
237
265
|
}
|
|
266
|
+
contentTypeToResponseType(contentType) {
|
|
267
|
+
return (contentType === null || contentType === void 0 ? void 0 : contentType.includes('json')) ? 'json' : 'text';
|
|
268
|
+
}
|
|
238
269
|
}
|
|
@@ -43,7 +43,8 @@ export class TypeScriptAngularServicesGenerator extends OpenApiServicesGeneratio
|
|
|
43
43
|
const sourceDir = resolve(dirname(require.resolve('@goast/typescript')), '../assets/client/angular');
|
|
44
44
|
const targetDir = resolve(ctx.config.outputDir, ctx.config.utilsDirPath);
|
|
45
45
|
ensureDirSync(targetDir);
|
|
46
|
-
this.copyFile('
|
|
46
|
+
this.copyFile('Request Builder', sourceDir, targetDir, 'request-builder.ts');
|
|
47
|
+
this.copyFile('Angular Service Utils', sourceDir, targetDir, 'angular-service.utils.ts');
|
|
47
48
|
TypeScriptFileBuilder.generate({
|
|
48
49
|
logName: 'api configuration',
|
|
49
50
|
filePath: resolve(targetDir, 'api-configuration.ts'),
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { ts } from '../../../ast';
|
|
2
1
|
import { defaultTypeScriptGeneratorConfig } from '../../../config';
|
|
3
2
|
export const defaultTypeScriptAngularServicesGeneratorConfig = Object.assign(Object.assign({}, defaultTypeScriptGeneratorConfig), { fileNameCasing: { casing: 'kebab', suffix: '.service' }, responseModelFileNameCasing: { casing: 'kebab', suffix: '-responses.model' }, provideKind: 'provide-fn', strictResponseTypes: true, defaultStatusCodeResponseTypes: {
|
|
4
|
-
401:
|
|
5
|
-
403:
|
|
6
|
-
500:
|
|
3
|
+
401: null,
|
|
4
|
+
403: null,
|
|
5
|
+
500: null,
|
|
7
6
|
}, possibleStatusCodes: [
|
|
8
7
|
100, 101, 102, 103, 200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, 302, 303, 304, 305, 306, 307, 308,
|
|
9
8
|
400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 421, 422, 423, 424, 425,
|
|
@@ -6,6 +6,8 @@ export function getReferenceFactories(options) {
|
|
|
6
6
|
var _a, _b;
|
|
7
7
|
const utilsDirPath = resolve(options.outputDir, options.utilsDirPath);
|
|
8
8
|
return {
|
|
9
|
+
// angular-service.utils.ts
|
|
10
|
+
waitForResponse: ts.reference.genericFactory('waitForResponse', join(utilsDirPath, 'angular-service.utils.ts')),
|
|
9
11
|
// request-builder.ts
|
|
10
12
|
requestBuilder: ts.reference.factory('RequestBuilder', join(utilsDirPath, 'request-builder.ts')),
|
|
11
13
|
// api-configuration.ts - generated in angular-services-generator.ts
|
package/package.json
CHANGED
|
@@ -35,3 +35,9 @@ export declare const httpResponse: (<TBuilder extends import("@goast/core").Sour
|
|
|
35
35
|
} & {
|
|
36
36
|
moduleNameOrfilePath: string;
|
|
37
37
|
};
|
|
38
|
+
export declare const httpErrorResponse: (<TBuilder extends import("@goast/core").SourceBuilder<never>>() => import("../nodes/reference").TsReference<TBuilder, never>) & {
|
|
39
|
+
refName: string;
|
|
40
|
+
moduleNameOrfilePath: import("@goast/core").Nullable<string>;
|
|
41
|
+
} & {
|
|
42
|
+
moduleNameOrfilePath: string;
|
|
43
|
+
};
|
|
@@ -14,7 +14,10 @@ export declare class DefaultTypeScriptAngularServiceGenerator extends TypeScript
|
|
|
14
14
|
generate(ctx: Context): Output;
|
|
15
15
|
protected generateResponseModels(ctx: Context): Record<string, TypeScriptExportOutput>;
|
|
16
16
|
protected getResponseModelFileContent(ctx: Context, endpoint: ApiEndpoint): AppendValueGroup<Builder>;
|
|
17
|
-
protected getEndpointStatusCodes(ctx: Context, endpoint: ApiEndpoint): Record<number,
|
|
17
|
+
protected getEndpointStatusCodes(ctx: Context, endpoint: ApiEndpoint): Record<number, {
|
|
18
|
+
parser: 'text' | 'json';
|
|
19
|
+
type: ts.Type<Builder>;
|
|
20
|
+
}>;
|
|
18
21
|
protected getServiceFileContent(ctx: Context): AppendValueGroup<Builder>;
|
|
19
22
|
protected getEndpointParamsType(ctx: Context, endpoint: ApiEndpoint): ts.TypeAlias<Builder>;
|
|
20
23
|
protected getClass(ctx: Context): ts.Class<Builder>;
|
|
@@ -29,5 +32,6 @@ export declare class DefaultTypeScriptAngularServiceGenerator extends TypeScript
|
|
|
29
32
|
protected getResponseModelFilePath(ctx: Context): string;
|
|
30
33
|
protected hasEndpointParams(ctx: Context, endpoint: ApiEndpoint): boolean;
|
|
31
34
|
protected getSchemaType(ctx: Context, schema: ApiSchema | undefined, fallback?: ts.Type<Builder>): ts.Type<Builder>;
|
|
35
|
+
private contentTypeToResponseType;
|
|
32
36
|
}
|
|
33
37
|
export {};
|
|
@@ -29,20 +29,29 @@ export type TypeScriptAngularServicesGeneratorConfig = TypeScriptGeneratorConfig
|
|
|
29
29
|
* The default response types for status codes that are not defined in the OpenAPI specification.
|
|
30
30
|
* @example
|
|
31
31
|
* {
|
|
32
|
-
* 400:
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
32
|
+
* 400: {
|
|
33
|
+
* parser: 'json',
|
|
34
|
+
* type: schemas =>
|
|
35
|
+
* schemas.find(s => s.name === 'BadRequestResponse')
|
|
36
|
+
* ?? throwExp('BadRequestResponse not found'),
|
|
37
|
+
* },
|
|
38
|
+
* 404: null,
|
|
39
|
+
* 500: {
|
|
40
|
+
* parser: 'json',
|
|
41
|
+
* type: ts.refs.any(),
|
|
42
|
+
* },
|
|
37
43
|
* }
|
|
38
44
|
* @default
|
|
39
45
|
* {
|
|
40
|
-
* 401:
|
|
41
|
-
* 403:
|
|
42
|
-
* 500:
|
|
46
|
+
* 401: null,
|
|
47
|
+
* 403: null,
|
|
48
|
+
* 500: null,
|
|
43
49
|
* }
|
|
44
50
|
*/
|
|
45
|
-
defaultStatusCodeResponseTypes: Record<number,
|
|
51
|
+
defaultStatusCodeResponseTypes: Record<number, {
|
|
52
|
+
parser: 'text' | 'json';
|
|
53
|
+
type: Exclude<ts.Type<TypeScriptFileBuilder>, Function> | ((schemas: readonly ApiSchema[]) => ApiSchema);
|
|
54
|
+
} | null>;
|
|
46
55
|
/**
|
|
47
56
|
* The possible status codes that any API endpoint can return.
|
|
48
57
|
* This is only used when `strictResponseTypes` is set to `false`;
|
|
@@ -1,31 +1,38 @@
|
|
|
1
1
|
import { ts } from '../../../ast';
|
|
2
2
|
import type { TypeScriptAngularServicesGeneratorConfig } from './models';
|
|
3
3
|
export declare function getReferenceFactories(options: TypeScriptAngularServicesGeneratorConfig): {
|
|
4
|
-
|
|
4
|
+
waitForResponse: (<TBuilder extends import("@goast/core").SourceBuilder<never>>(generics: [ts.Type<TBuilder>]) => ts.Reference<TBuilder, never>) & {
|
|
5
5
|
refName: string;
|
|
6
6
|
moduleNameOrfilePath: import("@goast/core").Nullable<string>;
|
|
7
|
+
infer: <TBuilder_1 extends import("@goast/core").SourceBuilder<never>>() => ts.Reference<TBuilder_1, never>;
|
|
7
8
|
} & {
|
|
8
9
|
moduleNameOrfilePath: string;
|
|
9
10
|
};
|
|
10
|
-
|
|
11
|
+
requestBuilder: (<TBuilder_2 extends import("@goast/core").SourceBuilder<never>>() => ts.Reference<TBuilder_2, never>) & {
|
|
11
12
|
refName: string;
|
|
12
13
|
moduleNameOrfilePath: import("@goast/core").Nullable<string>;
|
|
13
14
|
} & {
|
|
14
15
|
moduleNameOrfilePath: string;
|
|
15
16
|
};
|
|
16
|
-
|
|
17
|
+
apiConfiguration: (<TBuilder_2 extends import("@goast/core").SourceBuilder<never>>() => ts.Reference<TBuilder_2, never>) & {
|
|
17
18
|
refName: string;
|
|
18
19
|
moduleNameOrfilePath: import("@goast/core").Nullable<string>;
|
|
19
20
|
} & {
|
|
20
21
|
moduleNameOrfilePath: string;
|
|
21
22
|
};
|
|
22
|
-
|
|
23
|
+
apiBaseService: (<TBuilder_2 extends import("@goast/core").SourceBuilder<never>>() => ts.Reference<TBuilder_2, never>) & {
|
|
23
24
|
refName: string;
|
|
24
25
|
moduleNameOrfilePath: import("@goast/core").Nullable<string>;
|
|
25
26
|
} & {
|
|
26
27
|
moduleNameOrfilePath: string;
|
|
27
28
|
};
|
|
28
|
-
|
|
29
|
+
httpStatusCode: (<TBuilder_2 extends import("@goast/core").SourceBuilder<never>>() => ts.Reference<TBuilder_2, never>) & {
|
|
30
|
+
refName: string;
|
|
31
|
+
moduleNameOrfilePath: import("@goast/core").Nullable<string>;
|
|
32
|
+
} & {
|
|
33
|
+
moduleNameOrfilePath: string;
|
|
34
|
+
};
|
|
35
|
+
provide: (<TBuilder_2 extends import("@goast/core").SourceBuilder<never>>() => ts.Reference<TBuilder_2, never>) & {
|
|
29
36
|
refName: string;
|
|
30
37
|
moduleNameOrfilePath: import("@goast/core").Nullable<string>;
|
|
31
38
|
} & {
|