@goast/typescript 0.0.3 → 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/README.md CHANGED
@@ -1,11 +1,11 @@
1
- # typescript
2
-
3
- This library was generated with [Nx](https://nx.dev).
4
-
5
- ## Building
6
-
7
- Run `nx build typescript` to build the library.
8
-
9
- ## Running unit tests
10
-
11
- Run `nx test typescript` to execute the unit tests via [Jest](https://jestjs.io).
1
+ # typescript
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build typescript` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test typescript` to execute the unit tests via [Jest](https://jestjs.io).
@@ -1,9 +1,14 @@
1
- export type TypedResponse<T> = Response & {
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
- readonly baseUrl?: string;
7
- readonly fetch?: typeof fetch;
8
- readonly headers?: Record<string, string>;
11
+ baseUrl?: string;
12
+ fetch?: typeof fetch;
13
+ headers?: Record<string, string>;
9
14
  };
@@ -104,19 +104,19 @@ class DefaultTypeScriptFetchClientGenerator extends file_generator_1.TypeScriptF
104
104
  .forEach(ctx.service.endpoints, (builder, endpoint) => this.generateClassServiceMethod(ctx, builder, endpoint));
105
105
  }
106
106
  generateClassFields(ctx, builder) {
107
- builder.ensureCurrentLineEmpty().appendLine('private readonly _options: FetchClientOptions;');
107
+ builder.ensureCurrentLineEmpty().appendLine('public options: FetchClientOptions;');
108
108
  }
109
109
  generateClassConstructor(ctx, builder) {
110
110
  builder
111
111
  .ensurePreviousLineEmpty()
112
- .append('constructor(options?: FetchClientOptions)')
112
+ .append('constructor(options?: Partial<FetchClientOptions>)')
113
113
  .parenthesize('{}', (builder) => this.generateClassConstructorContent(ctx, builder), {
114
114
  indent: true,
115
115
  multiline: true,
116
116
  });
117
117
  }
118
118
  generateClassConstructorContent(ctx, builder) {
119
- builder.appendLine('this._options = { ...', this.getDefaultOptionsConstantName(ctx), ', ...options };');
119
+ builder.appendLine('this.options = { ...', this.getDefaultOptionsConstantName(ctx), ', ...options };');
120
120
  }
121
121
  generateClassServiceMethod(ctx, builder, endpoint) {
122
122
  builder
@@ -168,21 +168,20 @@ class DefaultTypeScriptFetchClientGenerator extends file_generator_1.TypeScriptF
168
168
  .appendLineIf(params.length > 0 && !!bodySchemaId);
169
169
  }
170
170
  generateServiceMethodReturnValue(ctx, builder, endpoint) {
171
- var _a, _b, _c;
172
- 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);
173
- const schema = (_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;
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
184
183
  .addImport('UrlBuilder', this.getUtilPath(ctx, 'url-builder.ts'))
185
- .appendLine('const url = new UrlBuilder(this._options.baseUrl)')
184
+ .appendLine('const url = new UrlBuilder(this.options.baseUrl)')
186
185
  .indent((builder) => builder
187
186
  .appendLine(`.withPath(${this.toStringLiteral(ctx, endpoint.path)})`)
188
187
  .forEach(endpoint.parameters.filter((x) => x.target === 'path' || x.target === 'query'), (builder, parameter) => builder
@@ -191,19 +190,21 @@ 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('return (this._options.fetch ?? fetch)')
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
198
197
  .appendLine('method: ', this.toStringLiteral(ctx, (0, core_1.toCasing)(endpoint.method, 'all-upper')), ',')
199
- .appendLine('headers: this._options.headers,')
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(' as ', (builder) => this.generateServiceMethodReturnValue(ctx, builder, endpoint), ';');
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;
@@ -101,19 +101,19 @@ export class DefaultTypeScriptFetchClientGenerator extends TypeScriptFileGenerat
101
101
  .forEach(ctx.service.endpoints, (builder, endpoint) => this.generateClassServiceMethod(ctx, builder, endpoint));
102
102
  }
103
103
  generateClassFields(ctx, builder) {
104
- builder.ensureCurrentLineEmpty().appendLine('private readonly _options: FetchClientOptions;');
104
+ builder.ensureCurrentLineEmpty().appendLine('public options: FetchClientOptions;');
105
105
  }
106
106
  generateClassConstructor(ctx, builder) {
107
107
  builder
108
108
  .ensurePreviousLineEmpty()
109
- .append('constructor(options?: FetchClientOptions)')
109
+ .append('constructor(options?: Partial<FetchClientOptions>)')
110
110
  .parenthesize('{}', (builder) => this.generateClassConstructorContent(ctx, builder), {
111
111
  indent: true,
112
112
  multiline: true,
113
113
  });
114
114
  }
115
115
  generateClassConstructorContent(ctx, builder) {
116
- builder.appendLine('this._options = { ...', this.getDefaultOptionsConstantName(ctx), ', ...options };');
116
+ builder.appendLine('this.options = { ...', this.getDefaultOptionsConstantName(ctx), ', ...options };');
117
117
  }
118
118
  generateClassServiceMethod(ctx, builder, endpoint) {
119
119
  builder
@@ -165,21 +165,20 @@ export class DefaultTypeScriptFetchClientGenerator extends TypeScriptFileGenerat
165
165
  .appendLineIf(params.length > 0 && !!bodySchemaId);
166
166
  }
167
167
  generateServiceMethodReturnValue(ctx, builder, endpoint) {
168
- var _a, _b, _c;
169
- 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);
170
- const schema = (_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;
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
181
180
  .addImport('UrlBuilder', this.getUtilPath(ctx, 'url-builder.ts'))
182
- .appendLine('const url = new UrlBuilder(this._options.baseUrl)')
181
+ .appendLine('const url = new UrlBuilder(this.options.baseUrl)')
183
182
  .indent((builder) => builder
184
183
  .appendLine(`.withPath(${this.toStringLiteral(ctx, endpoint.path)})`)
185
184
  .forEach(endpoint.parameters.filter((x) => x.target === 'path' || x.target === 'query'), (builder, parameter) => builder
@@ -188,19 +187,21 @@ 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('return (this._options.fetch ?? fetch)')
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
195
194
  .appendLine('method: ', this.toStringLiteral(ctx, toCasing(endpoint.method, 'all-upper')), ',')
196
- .appendLine('headers: this._options.headers,')
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(' as ', (builder) => this.generateServiceMethodReturnValue(ctx, builder, endpoint), ';');
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goast/typescript",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "repository": "https://github.com/MaSch0212/goast.git",
5
5
  "author": {
6
6
  "name": "Marc Schmidt (MaSch0212)",
@@ -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 {};