@goast/typescript 0.0.4 → 0.0.5
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/fetch/types.ts +6 -1
- package/cjs/lib/generators/services/fetch-clients/fetch-client-generator.js +15 -9
- package/esm/lib/generators/services/fetch-clients/fetch-client-generator.js +15 -9
- package/package.json +1 -1
- package/types/lib/generators/services/fetch-clients/fetch-client-generator.d.ts +2 -1
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
type _VoidResponse = Omit<Response, 'json'> & {
|
|
2
|
+
isVoidResponse: true;
|
|
3
|
+
};
|
|
4
|
+
type _TypedResponse<T> = Response & {
|
|
2
5
|
json(): Promise<T>;
|
|
6
|
+
isVoidResponse: false;
|
|
3
7
|
};
|
|
8
|
+
export type TypedResponse<T> = void extends T ? _VoidResponse : _TypedResponse<T>;
|
|
4
9
|
|
|
5
10
|
export type FetchClientOptions = {
|
|
6
11
|
baseUrl?: string;
|
|
@@ -168,16 +168,15 @@ class DefaultTypeScriptFetchClientGenerator extends file_generator_1.TypeScriptF
|
|
|
168
168
|
.appendLineIf(params.length > 0 && !!bodySchemaId);
|
|
169
169
|
}
|
|
170
170
|
generateServiceMethodReturnValue(ctx, builder, endpoint) {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
builder.append('Promise').parenthesize('<>', (builder) => this.generateTypedResponse(ctx, builder, schema));
|
|
171
|
+
builder
|
|
172
|
+
.append('Promise')
|
|
173
|
+
.parenthesize('<>', (builder) => this.generateTypedResponse(ctx, builder, this.getResponseSchema(ctx, endpoint)));
|
|
175
174
|
}
|
|
176
175
|
generateTypedResponse(ctx, builder, schema) {
|
|
177
176
|
builder
|
|
178
177
|
.addImport('TypedResponse', this.getUtilPath(ctx, 'types.ts'))
|
|
179
178
|
.append('TypedResponse')
|
|
180
|
-
.parenthesize('<>', (builder) => builder.append(this.getTypeName(ctx, builder, schema === null || schema === void 0 ? void 0 : schema.id)));
|
|
179
|
+
.parenthesize('<>', (builder) => builder.append(this.getTypeName(ctx, builder, schema === null || schema === void 0 ? void 0 : schema.id, 'void')));
|
|
181
180
|
}
|
|
182
181
|
generateServiceMethodContent(ctx, builder, endpoint) {
|
|
183
182
|
builder
|
|
@@ -191,7 +190,7 @@ class DefaultTypeScriptFetchClientGenerator extends file_generator_1.TypeScriptF
|
|
|
191
190
|
.parenthesize('()', `${this.toStringLiteral(ctx, parameter.name)}, params.${this.toPropertyName(ctx, parameter.name)}`)
|
|
192
191
|
.appendLine())
|
|
193
192
|
.appendLine('.build();'))
|
|
194
|
-
.append('
|
|
193
|
+
.append('const response = (this.options.fetch ?? fetch)')
|
|
195
194
|
.parenthesize('()', (builder) => builder.append('url, ').parenthesize('{}', (builder) => {
|
|
196
195
|
var _a, _b;
|
|
197
196
|
return builder
|
|
@@ -199,11 +198,13 @@ class DefaultTypeScriptFetchClientGenerator extends file_generator_1.TypeScriptF
|
|
|
199
198
|
.appendLine('headers: this.options.headers,')
|
|
200
199
|
.appendLineIf(!!((_b = (_a = endpoint.requestBody) === null || _a === void 0 ? void 0 : _a.content[0]) === null || _b === void 0 ? void 0 : _b.schema), 'body: JSON.stringify(body),');
|
|
201
200
|
}, { indent: true, multiline: true }), { indent: false })
|
|
202
|
-
.appendLine('
|
|
201
|
+
.appendLine(';')
|
|
202
|
+
.appendLine("Object.defineProperty(response, 'isVoidResponse', { value: ", this.getResponseSchema(ctx, endpoint) ? 'false' : 'true', ' });')
|
|
203
|
+
.appendLine('return response as unknown as ', (builder) => this.generateServiceMethodReturnValue(ctx, builder, endpoint), ';');
|
|
203
204
|
}
|
|
204
|
-
getTypeName(ctx, builder, schemaId) {
|
|
205
|
+
getTypeName(ctx, builder, schemaId, fallback) {
|
|
205
206
|
if (!schemaId) {
|
|
206
|
-
return this.getAnyType(ctx);
|
|
207
|
+
return fallback ? fallback : this.getAnyType(ctx);
|
|
207
208
|
}
|
|
208
209
|
const modelInfo = ctx.input.models[schemaId];
|
|
209
210
|
if (!modelInfo) {
|
|
@@ -237,5 +238,10 @@ class DefaultTypeScriptFetchClientGenerator extends file_generator_1.TypeScriptF
|
|
|
237
238
|
getUtilPath(ctx, fileName) {
|
|
238
239
|
return (0, path_1.resolve)(ctx.config.outputDir, ctx.config.utilsDirPath, fileName);
|
|
239
240
|
}
|
|
241
|
+
getResponseSchema(ctx, endpoint) {
|
|
242
|
+
var _a, _b, _c;
|
|
243
|
+
const successResponse = (_a = endpoint.responses.find((x) => x.statusCode === 200)) !== null && _a !== void 0 ? _a : endpoint.responses.find((x) => x.statusCode && x.statusCode > 200 && x.statusCode < 300);
|
|
244
|
+
return (_c = (_b = successResponse === null || successResponse === void 0 ? void 0 : successResponse.contentOptions) === null || _b === void 0 ? void 0 : _b.find((x) => x.schema !== undefined)) === null || _c === void 0 ? void 0 : _c.schema;
|
|
245
|
+
}
|
|
240
246
|
}
|
|
241
247
|
exports.DefaultTypeScriptFetchClientGenerator = DefaultTypeScriptFetchClientGenerator;
|
|
@@ -165,16 +165,15 @@ export class DefaultTypeScriptFetchClientGenerator extends TypeScriptFileGenerat
|
|
|
165
165
|
.appendLineIf(params.length > 0 && !!bodySchemaId);
|
|
166
166
|
}
|
|
167
167
|
generateServiceMethodReturnValue(ctx, builder, endpoint) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
builder.append('Promise').parenthesize('<>', (builder) => this.generateTypedResponse(ctx, builder, schema));
|
|
168
|
+
builder
|
|
169
|
+
.append('Promise')
|
|
170
|
+
.parenthesize('<>', (builder) => this.generateTypedResponse(ctx, builder, this.getResponseSchema(ctx, endpoint)));
|
|
172
171
|
}
|
|
173
172
|
generateTypedResponse(ctx, builder, schema) {
|
|
174
173
|
builder
|
|
175
174
|
.addImport('TypedResponse', this.getUtilPath(ctx, 'types.ts'))
|
|
176
175
|
.append('TypedResponse')
|
|
177
|
-
.parenthesize('<>', (builder) => builder.append(this.getTypeName(ctx, builder, schema === null || schema === void 0 ? void 0 : schema.id)));
|
|
176
|
+
.parenthesize('<>', (builder) => builder.append(this.getTypeName(ctx, builder, schema === null || schema === void 0 ? void 0 : schema.id, 'void')));
|
|
178
177
|
}
|
|
179
178
|
generateServiceMethodContent(ctx, builder, endpoint) {
|
|
180
179
|
builder
|
|
@@ -188,7 +187,7 @@ export class DefaultTypeScriptFetchClientGenerator extends TypeScriptFileGenerat
|
|
|
188
187
|
.parenthesize('()', `${this.toStringLiteral(ctx, parameter.name)}, params.${this.toPropertyName(ctx, parameter.name)}`)
|
|
189
188
|
.appendLine())
|
|
190
189
|
.appendLine('.build();'))
|
|
191
|
-
.append('
|
|
190
|
+
.append('const response = (this.options.fetch ?? fetch)')
|
|
192
191
|
.parenthesize('()', (builder) => builder.append('url, ').parenthesize('{}', (builder) => {
|
|
193
192
|
var _a, _b;
|
|
194
193
|
return builder
|
|
@@ -196,11 +195,13 @@ export class DefaultTypeScriptFetchClientGenerator extends TypeScriptFileGenerat
|
|
|
196
195
|
.appendLine('headers: this.options.headers,')
|
|
197
196
|
.appendLineIf(!!((_b = (_a = endpoint.requestBody) === null || _a === void 0 ? void 0 : _a.content[0]) === null || _b === void 0 ? void 0 : _b.schema), 'body: JSON.stringify(body),');
|
|
198
197
|
}, { indent: true, multiline: true }), { indent: false })
|
|
199
|
-
.appendLine('
|
|
198
|
+
.appendLine(';')
|
|
199
|
+
.appendLine("Object.defineProperty(response, 'isVoidResponse', { value: ", this.getResponseSchema(ctx, endpoint) ? 'false' : 'true', ' });')
|
|
200
|
+
.appendLine('return response as unknown as ', (builder) => this.generateServiceMethodReturnValue(ctx, builder, endpoint), ';');
|
|
200
201
|
}
|
|
201
|
-
getTypeName(ctx, builder, schemaId) {
|
|
202
|
+
getTypeName(ctx, builder, schemaId, fallback) {
|
|
202
203
|
if (!schemaId) {
|
|
203
|
-
return this.getAnyType(ctx);
|
|
204
|
+
return fallback ? fallback : this.getAnyType(ctx);
|
|
204
205
|
}
|
|
205
206
|
const modelInfo = ctx.input.models[schemaId];
|
|
206
207
|
if (!modelInfo) {
|
|
@@ -234,4 +235,9 @@ export class DefaultTypeScriptFetchClientGenerator extends TypeScriptFileGenerat
|
|
|
234
235
|
getUtilPath(ctx, fileName) {
|
|
235
236
|
return resolve(ctx.config.outputDir, ctx.config.utilsDirPath, fileName);
|
|
236
237
|
}
|
|
238
|
+
getResponseSchema(ctx, endpoint) {
|
|
239
|
+
var _a, _b, _c;
|
|
240
|
+
const successResponse = (_a = endpoint.responses.find((x) => x.statusCode === 200)) !== null && _a !== void 0 ? _a : endpoint.responses.find((x) => x.statusCode && x.statusCode > 200 && x.statusCode < 300);
|
|
241
|
+
return (_c = (_b = successResponse === null || successResponse === void 0 ? void 0 : successResponse.contentOptions) === null || _b === void 0 ? void 0 : _b.find((x) => x.schema !== undefined)) === null || _c === void 0 ? void 0 : _c.schema;
|
|
242
|
+
}
|
|
237
243
|
}
|
package/package.json
CHANGED
|
@@ -30,7 +30,7 @@ export declare class DefaultTypeScriptFetchClientGenerator extends TypeScriptFil
|
|
|
30
30
|
protected generateServiceMethodReturnValue(ctx: Context, builder: Builder, endpoint: ApiEndpoint): void;
|
|
31
31
|
protected generateTypedResponse(ctx: Context, builder: Builder, schema: ApiSchema | undefined): void;
|
|
32
32
|
protected generateServiceMethodContent(ctx: Context, builder: Builder, endpoint: ApiEndpoint): void;
|
|
33
|
-
protected getTypeName(ctx: Context, builder: Builder, schemaId: string | undefined): string;
|
|
33
|
+
protected getTypeName(ctx: Context, builder: Builder, schemaId: string | undefined, fallback?: string): string;
|
|
34
34
|
protected shouldGenerateInterface(ctx: Context): boolean;
|
|
35
35
|
protected shouldGenerateClass(ctx: Context): boolean;
|
|
36
36
|
protected getInterfaceName(ctx: Context): string;
|
|
@@ -39,5 +39,6 @@ export declare class DefaultTypeScriptFetchClientGenerator extends TypeScriptFil
|
|
|
39
39
|
protected getInterfaceFilePath(ctx: Context): string;
|
|
40
40
|
protected getClassFilePath(ctx: Context): string;
|
|
41
41
|
protected getUtilPath(ctx: Context, fileName: string): string;
|
|
42
|
+
private getResponseSchema;
|
|
42
43
|
}
|
|
43
44
|
export {};
|