@goast/typescript 0.0.2 → 0.0.4
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.
|
@@ -3,7 +3,7 @@ export type TypedResponse<T> = Response & {
|
|
|
3
3
|
};
|
|
4
4
|
|
|
5
5
|
export type FetchClientOptions = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
fetch?: typeof fetch;
|
|
8
|
+
headers?: Record<string, string>;
|
|
9
9
|
};
|
|
@@ -31,6 +31,7 @@ export class UrlBuilder {
|
|
|
31
31
|
.map((key) => `${key}=${this.queryParams[key]}`)
|
|
32
32
|
.join('&');
|
|
33
33
|
const path = this.path.replace(/\{([^}]+)\}/g, (_, name) => this.pathParams[name]);
|
|
34
|
-
|
|
34
|
+
const url = `${this.baseUrl}${path || '/'}${queryString ? '?' + queryString : ''}`;
|
|
35
|
+
return url.replace(/(?<!:)[\\/]+(?=\/)|[\\/](?=\?)/g, '');
|
|
35
36
|
}
|
|
36
37
|
}
|
|
@@ -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('
|
|
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.
|
|
119
|
+
builder.appendLine('this.options = { ...', this.getDefaultOptionsConstantName(ctx), ', ...options };');
|
|
120
120
|
}
|
|
121
121
|
generateClassServiceMethod(ctx, builder, endpoint) {
|
|
122
122
|
builder
|
|
@@ -161,7 +161,7 @@ class DefaultTypeScriptFetchClientGenerator extends file_generator_1.TypeScriptF
|
|
|
161
161
|
.append('params: ')
|
|
162
162
|
.parenthesize('{}', (builder) => builder.forEach(params, (builder, parameter) => {
|
|
163
163
|
var _a;
|
|
164
|
-
return builder.appendLine(
|
|
164
|
+
return builder.appendLine(this.toPropertyName(ctx, parameter.name), parameter.required ? ': ' : '?: ', this.getTypeName(ctx, builder, (_a = parameter.schema) === null || _a === void 0 ? void 0 : _a.id), ';');
|
|
165
165
|
}), { indent: true, multiline: true }))
|
|
166
166
|
.appendLineIf(params.length > 0 && !!bodySchemaId, ',')
|
|
167
167
|
.appendIf(!!bodySchemaId, (builder) => builder.append(`body: ${this.getTypeName(ctx, builder, bodySchemaId)}`))
|
|
@@ -182,7 +182,7 @@ class DefaultTypeScriptFetchClientGenerator extends file_generator_1.TypeScriptF
|
|
|
182
182
|
generateServiceMethodContent(ctx, builder, endpoint) {
|
|
183
183
|
builder
|
|
184
184
|
.addImport('UrlBuilder', this.getUtilPath(ctx, 'url-builder.ts'))
|
|
185
|
-
.appendLine('const url = new UrlBuilder(this.
|
|
185
|
+
.appendLine('const url = new UrlBuilder(this.options.baseUrl)')
|
|
186
186
|
.indent((builder) => builder
|
|
187
187
|
.appendLine(`.withPath(${this.toStringLiteral(ctx, endpoint.path)})`)
|
|
188
188
|
.forEach(endpoint.parameters.filter((x) => x.target === 'path' || x.target === 'query'), (builder, parameter) => builder
|
|
@@ -191,12 +191,12 @@ class DefaultTypeScriptFetchClientGenerator extends file_generator_1.TypeScriptF
|
|
|
191
191
|
.parenthesize('()', `${this.toStringLiteral(ctx, parameter.name)}, params.${this.toPropertyName(ctx, parameter.name)}`)
|
|
192
192
|
.appendLine())
|
|
193
193
|
.appendLine('.build();'))
|
|
194
|
-
.append('return (this.
|
|
194
|
+
.append('return (this.options.fetch ?? fetch)')
|
|
195
195
|
.parenthesize('()', (builder) => builder.append('url, ').parenthesize('{}', (builder) => {
|
|
196
196
|
var _a, _b;
|
|
197
197
|
return builder
|
|
198
198
|
.appendLine('method: ', this.toStringLiteral(ctx, (0, core_1.toCasing)(endpoint.method, 'all-upper')), ',')
|
|
199
|
-
.appendLine('headers: this.
|
|
199
|
+
.appendLine('headers: this.options.headers,')
|
|
200
200
|
.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
201
|
}, { indent: true, multiline: true }), { indent: false })
|
|
202
202
|
.appendLine(' as ', (builder) => this.generateServiceMethodReturnValue(ctx, builder, endpoint), ';');
|
|
@@ -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('
|
|
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.
|
|
116
|
+
builder.appendLine('this.options = { ...', this.getDefaultOptionsConstantName(ctx), ', ...options };');
|
|
117
117
|
}
|
|
118
118
|
generateClassServiceMethod(ctx, builder, endpoint) {
|
|
119
119
|
builder
|
|
@@ -158,7 +158,7 @@ export class DefaultTypeScriptFetchClientGenerator extends TypeScriptFileGenerat
|
|
|
158
158
|
.append('params: ')
|
|
159
159
|
.parenthesize('{}', (builder) => builder.forEach(params, (builder, parameter) => {
|
|
160
160
|
var _a;
|
|
161
|
-
return builder.appendLine(
|
|
161
|
+
return builder.appendLine(this.toPropertyName(ctx, parameter.name), parameter.required ? ': ' : '?: ', this.getTypeName(ctx, builder, (_a = parameter.schema) === null || _a === void 0 ? void 0 : _a.id), ';');
|
|
162
162
|
}), { indent: true, multiline: true }))
|
|
163
163
|
.appendLineIf(params.length > 0 && !!bodySchemaId, ',')
|
|
164
164
|
.appendIf(!!bodySchemaId, (builder) => builder.append(`body: ${this.getTypeName(ctx, builder, bodySchemaId)}`))
|
|
@@ -179,7 +179,7 @@ export class DefaultTypeScriptFetchClientGenerator extends TypeScriptFileGenerat
|
|
|
179
179
|
generateServiceMethodContent(ctx, builder, endpoint) {
|
|
180
180
|
builder
|
|
181
181
|
.addImport('UrlBuilder', this.getUtilPath(ctx, 'url-builder.ts'))
|
|
182
|
-
.appendLine('const url = new UrlBuilder(this.
|
|
182
|
+
.appendLine('const url = new UrlBuilder(this.options.baseUrl)')
|
|
183
183
|
.indent((builder) => builder
|
|
184
184
|
.appendLine(`.withPath(${this.toStringLiteral(ctx, endpoint.path)})`)
|
|
185
185
|
.forEach(endpoint.parameters.filter((x) => x.target === 'path' || x.target === 'query'), (builder, parameter) => builder
|
|
@@ -188,12 +188,12 @@ export class DefaultTypeScriptFetchClientGenerator extends TypeScriptFileGenerat
|
|
|
188
188
|
.parenthesize('()', `${this.toStringLiteral(ctx, parameter.name)}, params.${this.toPropertyName(ctx, parameter.name)}`)
|
|
189
189
|
.appendLine())
|
|
190
190
|
.appendLine('.build();'))
|
|
191
|
-
.append('return (this.
|
|
191
|
+
.append('return (this.options.fetch ?? fetch)')
|
|
192
192
|
.parenthesize('()', (builder) => builder.append('url, ').parenthesize('{}', (builder) => {
|
|
193
193
|
var _a, _b;
|
|
194
194
|
return builder
|
|
195
195
|
.appendLine('method: ', this.toStringLiteral(ctx, toCasing(endpoint.method, 'all-upper')), ',')
|
|
196
|
-
.appendLine('headers: this.
|
|
196
|
+
.appendLine('headers: this.options.headers,')
|
|
197
197
|
.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
198
|
}, { indent: true, multiline: true }), { indent: false })
|
|
199
199
|
.appendLine(' as ', (builder) => this.generateServiceMethodReturnValue(ctx, builder, endpoint), ';');
|