Skip to content

Commit 6146129

Browse files
RobbilieRobert Schuh
andauthored
[typescript] add http info calls to access headers (#16260)
* [typescript] add http info calls to access headers * [typescript] add http info calls to access headers * [typescript] add http info calls to access headers * [typescript] add http info calls to access headers * [typescript] add http info calls to access headers --------- Co-authored-by: Robert Schuh <robert.schuh@valtech.com>
1 parent 3e95001 commit 6146129

61 files changed

Lines changed: 4295 additions & 646 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-generator/src/main/resources/typescript/api/api.mustache

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// TODO: better import syntax?
22
import {BaseAPIRequestFactory, RequiredError, COLLECTION_FORMATS} from './baseapi{{importFileExtension}}';
33
import {Configuration} from '../configuration{{importFileExtension}}';
4-
import {RequestContext, HttpMethod, ResponseContext, HttpFile} from '../http/http{{importFileExtension}}';
4+
import {RequestContext, HttpMethod, ResponseContext, HttpFile, HttpInfo} from '../http/http{{importFileExtension}}';
55
{{#platforms}}
66
{{#node}}
77
import {{^supportsES6}}* as{{/supportsES6}} FormData from "form-data";
@@ -190,7 +190,7 @@ export class {{classname}}ResponseProcessor {
190190
* @params response Response returned by the server for a request to {{nickname}}
191191
* @throws ApiException if the response code was not in [200, 299]
192192
*/
193-
public async {{nickname}}(response: ResponseContext): Promise<{{{returnType}}} {{^returnType}}void{{/returnType}}> {
193+
public async {{nickname}}WithHttpInfo(response: ResponseContext): Promise<HttpInfo<{{{returnType}}} {{^returnType}}void{{/returnType}}>> {
194194
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
195195
{{#responses}}
196196
if (isCodeInRange("{{code}}", response.httpStatusCode)) {
@@ -205,15 +205,15 @@ export class {{classname}}ResponseProcessor {
205205
) as {{{dataType}}};
206206
{{/isBinary}}
207207
{{#is2xx}}
208-
return body;
208+
return new HttpInfo(response.httpStatusCode, response.headers, response.body, body);
209209
{{/is2xx}}
210210
{{^is2xx}}
211211
throw new ApiException<{{{dataType}}}>(response.httpStatusCode, "{{message}}", body, response.headers);
212212
{{/is2xx}}
213213
{{/dataType}}
214214
{{^dataType}}
215215
{{#is2xx}}
216-
return;
216+
return new HttpInfo(response.httpStatusCode, response.headers, response.body, undefined);
217217
{{/is2xx}}
218218
{{^is2xx}}
219219
throw new ApiException<undefined>(response.httpStatusCode, "{{message}}", undefined, response.headers);
@@ -234,10 +234,10 @@ export class {{classname}}ResponseProcessor {
234234
"{{{returnType}}}", "{{returnFormat}}"
235235
) as {{{returnType}}};
236236
{{/isBinary}}
237-
return body;
237+
return new HttpInfo(response.httpStatusCode, response.headers, response.body, body);
238238
{{/returnType}}
239239
{{^returnType}}
240-
return;
240+
return new HttpInfo(response.httpStatusCode, response.headers, response.body, undefined);
241241
{{/returnType}}
242242
}
243243

modules/openapi-generator/src/main/resources/typescript/http/http.mustache

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,14 @@ export function wrapHttpLibrary(promiseHttpLibrary: PromiseHttpLibrary): HttpLib
308308
}
309309
}
310310
}
311+
312+
export class HttpInfo<T> extends ResponseContext {
313+
public constructor(
314+
public httpStatusCode: number,
315+
public headers: { [key: string]: string },
316+
public body: ResponseBody,
317+
public data: T,
318+
) {
319+
super(httpStatusCode, headers, body);
320+
}
321+
}

modules/openapi-generator/src/main/resources/typescript/services/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Configuration } from "../configuration";
2-
import type { HttpFile, RequestContext, ResponseContext } from "../http/http";
2+
import type { HttpFile, RequestContext, ResponseContext, HttpInfo } from "../http/http";
33

44
{{#imports}}
55
import { {{classname}} } from "{{filename}}";
@@ -16,7 +16,7 @@ export abstract class Abstract{{classname}}RequestFactory {
1616

1717
export abstract class Abstract{{classname}}ResponseProcessor {
1818
{{#operation}}
19-
public abstract {{nickname}}(response: ResponseContext): Promise<{{{returnType}}} {{^returnType}}void{{/returnType}}>;
19+
public abstract {{nickname}}WithHttpInfo(response: ResponseContext): Promise<HttpInfo<{{{returnType}}} {{^returnType}}void{{/returnType}}>>;
2020

2121
{{/operation}}
2222
}

modules/openapi-generator/src/main/resources/typescript/types/ObjectParamAPI.mustache

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ResponseContext, RequestContext, HttpFile } from '../http/http{{importFileExtension}}';
1+
import { ResponseContext, RequestContext, HttpFile, HttpInfo } from '../http/http{{importFileExtension}}';
22
import { Configuration} from '../configuration{{importFileExtension}}'
33
{{#useRxJS}}
44
import { Observable } from 'rxjs';
@@ -37,6 +37,19 @@ export class Object{{classname}} {
3737
}
3838

3939
{{#operation}}
40+
/**
41+
{{#notes}}
42+
* {{&notes}}
43+
{{/notes}}
44+
{{#summary}}
45+
* {{&summary}}
46+
{{/summary}}
47+
* @param param the request object
48+
*/
49+
public {{nickname}}WithHttpInfo(param: {{classname}}{{operationIdCamelCase}}Request{{^hasRequiredParams}} = {}{{/hasRequiredParams}}, options?: Configuration): {{#useRxJS}}Observable{{/useRxJS}}{{^useRxJS}}Promise{{/useRxJS}}<HttpInfo<{{{returnType}}}{{^returnType}}void{{/returnType}}>> {
50+
return this.api.{{nickname}}WithHttpInfo({{#allParams}}param.{{paramName}}, {{/allParams}} options){{^useRxJS}}.toPromise(){{/useRxJS}};
51+
}
52+
4053
/**
4154
{{#notes}}
4255
* {{&notes}}

modules/openapi-generator/src/main/resources/typescript/types/ObservableAPI.mustache

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ResponseContext, RequestContext, HttpFile } from '../http/http{{importFileExtension}}';
1+
import { ResponseContext, RequestContext, HttpFile, HttpInfo } from '../http/http{{importFileExtension}}';
22
import { Configuration} from '../configuration{{importFileExtension}}'
33
import { Observable, of, from } from {{#useRxJS}}'rxjs'{{/useRxJS}}{{^useRxJS}}'../rxjsStub{{importFileExtension}}'{{/useRxJS}};
44
import {mergeMap, map} from {{#useRxJS}}'rxjs/operators'{{/useRxJS}}{{^useRxJS}}'../rxjsStub{{importFileExtension}}'{{/useRxJS}};
@@ -61,7 +61,7 @@ export class Observable{{classname}} {
6161
* @param {{paramName}} {{description}}
6262
{{/allParams}}
6363
*/
64-
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration): Observable<{{{returnType}}}{{^returnType}}void{{/returnType}}> {
64+
public {{nickname}}WithHttpInfo({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration): Observable<HttpInfo<{{{returnType}}}{{^returnType}}void{{/returnType}}>> {
6565
const requestContextPromise = this.requestFactory.{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}_options);
6666

6767
// build promise chain
@@ -76,10 +76,25 @@ export class Observable{{classname}} {
7676
for (let middleware of this.configuration.middleware) {
7777
middlewarePostObservable = middlewarePostObservable.pipe(mergeMap((rsp: ResponseContext) => middleware.post(rsp)));
7878
}
79-
return middlewarePostObservable.pipe(map((rsp: ResponseContext) => this.responseProcessor.{{nickname}}(rsp)));
79+
return middlewarePostObservable.pipe(map((rsp: ResponseContext) => this.responseProcessor.{{nickname}}WithHttpInfo(rsp)));
8080
}));
8181
}
8282

83+
/**
84+
{{#notes}}
85+
* {{&notes}}
86+
{{/notes}}
87+
{{#summary}}
88+
* {{&summary}}
89+
{{/summary}}
90+
{{#allParams}}
91+
* @param {{paramName}} {{description}}
92+
{{/allParams}}
93+
*/
94+
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration): Observable<{{{returnType}}}{{^returnType}}void{{/returnType}}> {
95+
return this.{{nickname}}WithHttpInfo({{#allParams}}{{paramName}}, {{/allParams}}_options).pipe(map((apiResponse: HttpInfo<{{{returnType}}}{{^returnType}}void{{/returnType}}>) => apiResponse.data));
96+
}
97+
8398
{{/operation}}
8499
}
85100
{{/operations}}

modules/openapi-generator/src/main/resources/typescript/types/PromiseAPI.mustache

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ResponseContext, RequestContext, HttpFile } from '../http/http{{importFileExtension}}';
1+
import { ResponseContext, RequestContext, HttpFile, HttpInfo } from '../http/http{{importFileExtension}}';
22
import { Configuration} from '../configuration{{importFileExtension}}'
33
{{#useInversify}}
44
import { injectable, inject, optional } from "inversify";
@@ -40,6 +40,22 @@ export class Promise{{classname}} {
4040
}
4141

4242
{{#operation}}
43+
/**
44+
{{#notes}}
45+
* {{&notes}}
46+
{{/notes}}
47+
{{#summary}}
48+
* {{&summary}}
49+
{{/summary}}
50+
{{#allParams}}
51+
* @param {{paramName}} {{description}}
52+
{{/allParams}}
53+
*/
54+
public {{nickname}}WithHttpInfo({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration): Promise<HttpInfo<{{{returnType}}}{{^returnType}}void{{/returnType}}>> {
55+
const result = this.api.{{nickname}}WithHttpInfo({{#allParams}}{{paramName}}, {{/allParams}}_options);
56+
return result.toPromise();
57+
}
58+
4359
/**
4460
{{#notes}}
4561
* {{&notes}}

samples/client/others/typescript/builds/with-unique-items/apis/DefaultApi.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// TODO: better import syntax?
22
import {BaseAPIRequestFactory, RequiredError, COLLECTION_FORMATS} from './baseapi';
33
import {Configuration} from '../configuration';
4-
import {RequestContext, HttpMethod, ResponseContext, HttpFile} from '../http/http';
4+
import {RequestContext, HttpMethod, ResponseContext, HttpFile, HttpInfo} from '../http/http';
55
import {ObjectSerializer} from '../models/ObjectSerializer';
66
import {ApiException} from './exception';
77
import {canConsumeForm, isCodeInRange} from '../util';
@@ -48,14 +48,14 @@ export class DefaultApiResponseProcessor {
4848
* @params response Response returned by the server for a request to uniqueItems
4949
* @throws ApiException if the response code was not in [200, 299]
5050
*/
51-
public async uniqueItems(response: ResponseContext): Promise<Response > {
51+
public async uniqueItemsWithHttpInfo(response: ResponseContext): Promise<HttpInfo<Response >> {
5252
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
5353
if (isCodeInRange("200", response.httpStatusCode)) {
5454
const body: Response = ObjectSerializer.deserialize(
5555
ObjectSerializer.parse(await response.body.text(), contentType),
5656
"Response", ""
5757
) as Response;
58-
return body;
58+
return new HttpInfo(response.httpStatusCode, response.headers, response.body, body);
5959
}
6060

6161
// Work around for missing responses in specification, e.g. for petstore.yaml
@@ -64,7 +64,7 @@ export class DefaultApiResponseProcessor {
6464
ObjectSerializer.parse(await response.body.text(), contentType),
6565
"Response", ""
6666
) as Response;
67-
return body;
67+
return new HttpInfo(response.httpStatusCode, response.headers, response.body, body);
6868
}
6969

7070
throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny(), response.headers);

samples/client/others/typescript/builds/with-unique-items/http/http.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,14 @@ export function wrapHttpLibrary(promiseHttpLibrary: PromiseHttpLibrary): HttpLib
237237
}
238238
}
239239
}
240+
241+
export class HttpInfo<T> extends ResponseContext {
242+
public constructor(
243+
public httpStatusCode: number,
244+
public headers: { [key: string]: string },
245+
public body: ResponseBody,
246+
public data: T,
247+
) {
248+
super(httpStatusCode, headers, body);
249+
}
250+
}

samples/client/others/typescript/builds/with-unique-items/types/ObjectParamAPI.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ResponseContext, RequestContext, HttpFile } from '../http/http';
1+
import { ResponseContext, RequestContext, HttpFile, HttpInfo } from '../http/http';
22
import { Configuration} from '../configuration'
33

44
import { Response } from '../models/Response';
@@ -16,6 +16,13 @@ export class ObjectDefaultApi {
1616
this.api = new ObservableDefaultApi(configuration, requestFactory, responseProcessor);
1717
}
1818

19+
/**
20+
* @param param the request object
21+
*/
22+
public uniqueItemsWithHttpInfo(param: DefaultApiUniqueItemsRequest = {}, options?: Configuration): Promise<HttpInfo<Response>> {
23+
return this.api.uniqueItemsWithHttpInfo( options).toPromise();
24+
}
25+
1926
/**
2027
* @param param the request object
2128
*/

samples/client/others/typescript/builds/with-unique-items/types/ObservableAPI.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ResponseContext, RequestContext, HttpFile } from '../http/http';
1+
import { ResponseContext, RequestContext, HttpFile, HttpInfo } from '../http/http';
22
import { Configuration} from '../configuration'
33
import { Observable, of, from } from '../rxjsStub';
44
import {mergeMap, map} from '../rxjsStub';
@@ -22,7 +22,7 @@ export class ObservableDefaultApi {
2222

2323
/**
2424
*/
25-
public uniqueItems(_options?: Configuration): Observable<Response> {
25+
public uniqueItemsWithHttpInfo(_options?: Configuration): Observable<HttpInfo<Response>> {
2626
const requestContextPromise = this.requestFactory.uniqueItems(_options);
2727

2828
// build promise chain
@@ -37,8 +37,14 @@ export class ObservableDefaultApi {
3737
for (let middleware of this.configuration.middleware) {
3838
middlewarePostObservable = middlewarePostObservable.pipe(mergeMap((rsp: ResponseContext) => middleware.post(rsp)));
3939
}
40-
return middlewarePostObservable.pipe(map((rsp: ResponseContext) => this.responseProcessor.uniqueItems(rsp)));
40+
return middlewarePostObservable.pipe(map((rsp: ResponseContext) => this.responseProcessor.uniqueItemsWithHttpInfo(rsp)));
4141
}));
4242
}
4343

44+
/**
45+
*/
46+
public uniqueItems(_options?: Configuration): Observable<Response> {
47+
return this.uniqueItemsWithHttpInfo(_options).pipe(map((apiResponse: HttpInfo<Response>) => apiResponse.data));
48+
}
49+
4450
}

0 commit comments

Comments
 (0)