Skip to content

Commit

Permalink
[typescript] add http info calls to access headers (OpenAPITools#16260)
Browse files Browse the repository at this point in the history
* [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 <[email protected]>
  • Loading branch information
Robbilie and Robert Schuh authored Aug 23, 2023
1 parent 3e95001 commit 6146129
Show file tree
Hide file tree
Showing 61 changed files with 4,295 additions and 646 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// TODO: better import syntax?
import {BaseAPIRequestFactory, RequiredError, COLLECTION_FORMATS} from './baseapi{{importFileExtension}}';
import {Configuration} from '../configuration{{importFileExtension}}';
import {RequestContext, HttpMethod, ResponseContext, HttpFile} from '../http/http{{importFileExtension}}';
import {RequestContext, HttpMethod, ResponseContext, HttpFile, HttpInfo} from '../http/http{{importFileExtension}}';
{{#platforms}}
{{#node}}
import {{^supportsES6}}* as{{/supportsES6}} FormData from "form-data";
Expand Down Expand Up @@ -190,7 +190,7 @@ export class {{classname}}ResponseProcessor {
* @params response Response returned by the server for a request to {{nickname}}
* @throws ApiException if the response code was not in [200, 299]
*/
public async {{nickname}}(response: ResponseContext): Promise<{{{returnType}}} {{^returnType}}void{{/returnType}}> {
public async {{nickname}}WithHttpInfo(response: ResponseContext): Promise<HttpInfo<{{{returnType}}} {{^returnType}}void{{/returnType}}>> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
{{#responses}}
if (isCodeInRange("{{code}}", response.httpStatusCode)) {
Expand All @@ -205,15 +205,15 @@ export class {{classname}}ResponseProcessor {
) as {{{dataType}}};
{{/isBinary}}
{{#is2xx}}
return body;
return new HttpInfo(response.httpStatusCode, response.headers, response.body, body);
{{/is2xx}}
{{^is2xx}}
throw new ApiException<{{{dataType}}}>(response.httpStatusCode, "{{message}}", body, response.headers);
{{/is2xx}}
{{/dataType}}
{{^dataType}}
{{#is2xx}}
return;
return new HttpInfo(response.httpStatusCode, response.headers, response.body, undefined);
{{/is2xx}}
{{^is2xx}}
throw new ApiException<undefined>(response.httpStatusCode, "{{message}}", undefined, response.headers);
Expand All @@ -234,10 +234,10 @@ export class {{classname}}ResponseProcessor {
"{{{returnType}}}", "{{returnFormat}}"
) as {{{returnType}}};
{{/isBinary}}
return body;
return new HttpInfo(response.httpStatusCode, response.headers, response.body, body);
{{/returnType}}
{{^returnType}}
return;
return new HttpInfo(response.httpStatusCode, response.headers, response.body, undefined);
{{/returnType}}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,14 @@ export function wrapHttpLibrary(promiseHttpLibrary: PromiseHttpLibrary): HttpLib
}
}
}

export class HttpInfo<T> extends ResponseContext {
public constructor(
public httpStatusCode: number,
public headers: { [key: string]: string },
public body: ResponseBody,
public data: T,
) {
super(httpStatusCode, headers, body);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Configuration } from "../configuration";
import type { HttpFile, RequestContext, ResponseContext } from "../http/http";
import type { HttpFile, RequestContext, ResponseContext, HttpInfo } from "../http/http";

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

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

{{/operation}}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ResponseContext, RequestContext, HttpFile } from '../http/http{{importFileExtension}}';
import { ResponseContext, RequestContext, HttpFile, HttpInfo } from '../http/http{{importFileExtension}}';
import { Configuration} from '../configuration{{importFileExtension}}'
{{#useRxJS}}
import { Observable } from 'rxjs';
Expand Down Expand Up @@ -37,6 +37,19 @@ export class Object{{classname}} {
}

{{#operation}}
/**
{{#notes}}
* {{&notes}}
{{/notes}}
{{#summary}}
* {{&summary}}
{{/summary}}
* @param param the request object
*/
public {{nickname}}WithHttpInfo(param: {{classname}}{{operationIdCamelCase}}Request{{^hasRequiredParams}} = {}{{/hasRequiredParams}}, options?: Configuration): {{#useRxJS}}Observable{{/useRxJS}}{{^useRxJS}}Promise{{/useRxJS}}<HttpInfo<{{{returnType}}}{{^returnType}}void{{/returnType}}>> {
return this.api.{{nickname}}WithHttpInfo({{#allParams}}param.{{paramName}}, {{/allParams}} options){{^useRxJS}}.toPromise(){{/useRxJS}};
}

/**
{{#notes}}
* {{&notes}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ResponseContext, RequestContext, HttpFile } from '../http/http{{importFileExtension}}';
import { ResponseContext, RequestContext, HttpFile, HttpInfo } from '../http/http{{importFileExtension}}';
import { Configuration} from '../configuration{{importFileExtension}}'
import { Observable, of, from } from {{#useRxJS}}'rxjs'{{/useRxJS}}{{^useRxJS}}'../rxjsStub{{importFileExtension}}'{{/useRxJS}};
import {mergeMap, map} from {{#useRxJS}}'rxjs/operators'{{/useRxJS}}{{^useRxJS}}'../rxjsStub{{importFileExtension}}'{{/useRxJS}};
Expand Down Expand Up @@ -61,7 +61,7 @@ export class Observable{{classname}} {
* @param {{paramName}} {{description}}
{{/allParams}}
*/
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration): Observable<{{{returnType}}}{{^returnType}}void{{/returnType}}> {
public {{nickname}}WithHttpInfo({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration): Observable<HttpInfo<{{{returnType}}}{{^returnType}}void{{/returnType}}>> {
const requestContextPromise = this.requestFactory.{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}_options);

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

/**
{{#notes}}
* {{&notes}}
{{/notes}}
{{#summary}}
* {{&summary}}
{{/summary}}
{{#allParams}}
* @param {{paramName}} {{description}}
{{/allParams}}
*/
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration): Observable<{{{returnType}}}{{^returnType}}void{{/returnType}}> {
return this.{{nickname}}WithHttpInfo({{#allParams}}{{paramName}}, {{/allParams}}_options).pipe(map((apiResponse: HttpInfo<{{{returnType}}}{{^returnType}}void{{/returnType}}>) => apiResponse.data));
}

{{/operation}}
}
{{/operations}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ResponseContext, RequestContext, HttpFile } from '../http/http{{importFileExtension}}';
import { ResponseContext, RequestContext, HttpFile, HttpInfo } from '../http/http{{importFileExtension}}';
import { Configuration} from '../configuration{{importFileExtension}}'
{{#useInversify}}
import { injectable, inject, optional } from "inversify";
Expand Down Expand Up @@ -40,6 +40,22 @@ export class Promise{{classname}} {
}

{{#operation}}
/**
{{#notes}}
* {{&notes}}
{{/notes}}
{{#summary}}
* {{&summary}}
{{/summary}}
{{#allParams}}
* @param {{paramName}} {{description}}
{{/allParams}}
*/
public {{nickname}}WithHttpInfo({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration): Promise<HttpInfo<{{{returnType}}}{{^returnType}}void{{/returnType}}>> {
const result = this.api.{{nickname}}WithHttpInfo({{#allParams}}{{paramName}}, {{/allParams}}_options);
return result.toPromise();
}

/**
{{#notes}}
* {{&notes}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// TODO: better import syntax?
import {BaseAPIRequestFactory, RequiredError, COLLECTION_FORMATS} from './baseapi';
import {Configuration} from '../configuration';
import {RequestContext, HttpMethod, ResponseContext, HttpFile} from '../http/http';
import {RequestContext, HttpMethod, ResponseContext, HttpFile, HttpInfo} from '../http/http';
import {ObjectSerializer} from '../models/ObjectSerializer';
import {ApiException} from './exception';
import {canConsumeForm, isCodeInRange} from '../util';
Expand Down Expand Up @@ -48,14 +48,14 @@ export class DefaultApiResponseProcessor {
* @params response Response returned by the server for a request to uniqueItems
* @throws ApiException if the response code was not in [200, 299]
*/
public async uniqueItems(response: ResponseContext): Promise<Response > {
public async uniqueItemsWithHttpInfo(response: ResponseContext): Promise<HttpInfo<Response >> {
const contentType = ObjectSerializer.normalizeMediaType(response.headers["content-type"]);
if (isCodeInRange("200", response.httpStatusCode)) {
const body: Response = ObjectSerializer.deserialize(
ObjectSerializer.parse(await response.body.text(), contentType),
"Response", ""
) as Response;
return body;
return new HttpInfo(response.httpStatusCode, response.headers, response.body, body);
}

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

throw new ApiException<string | Blob | undefined>(response.httpStatusCode, "Unknown API Status Code!", await response.getBodyAsAny(), response.headers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,14 @@ export function wrapHttpLibrary(promiseHttpLibrary: PromiseHttpLibrary): HttpLib
}
}
}

export class HttpInfo<T> extends ResponseContext {
public constructor(
public httpStatusCode: number,
public headers: { [key: string]: string },
public body: ResponseBody,
public data: T,
) {
super(httpStatusCode, headers, body);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ResponseContext, RequestContext, HttpFile } from '../http/http';
import { ResponseContext, RequestContext, HttpFile, HttpInfo } from '../http/http';
import { Configuration} from '../configuration'

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

/**
* @param param the request object
*/
public uniqueItemsWithHttpInfo(param: DefaultApiUniqueItemsRequest = {}, options?: Configuration): Promise<HttpInfo<Response>> {
return this.api.uniqueItemsWithHttpInfo( options).toPromise();
}

/**
* @param param the request object
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ResponseContext, RequestContext, HttpFile } from '../http/http';
import { ResponseContext, RequestContext, HttpFile, HttpInfo } from '../http/http';
import { Configuration} from '../configuration'
import { Observable, of, from } from '../rxjsStub';
import {mergeMap, map} from '../rxjsStub';
Expand All @@ -22,7 +22,7 @@ export class ObservableDefaultApi {

/**
*/
public uniqueItems(_options?: Configuration): Observable<Response> {
public uniqueItemsWithHttpInfo(_options?: Configuration): Observable<HttpInfo<Response>> {
const requestContextPromise = this.requestFactory.uniqueItems(_options);

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

/**
*/
public uniqueItems(_options?: Configuration): Observable<Response> {
return this.uniqueItemsWithHttpInfo(_options).pipe(map((apiResponse: HttpInfo<Response>) => apiResponse.data));
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ResponseContext, RequestContext, HttpFile } from '../http/http';
import { ResponseContext, RequestContext, HttpFile, HttpInfo } from '../http/http';
import { Configuration} from '../configuration'

import { Response } from '../models/Response';
Expand All @@ -16,6 +16,13 @@ export class PromiseDefaultApi {
this.api = new ObservableDefaultApi(configuration, requestFactory, responseProcessor);
}

/**
*/
public uniqueItemsWithHttpInfo(_options?: Configuration): Promise<HttpInfo<Response>> {
const result = this.api.uniqueItemsWithHttpInfo(_options);
return result.toPromise();
}

/**
*/
public uniqueItems(_options?: Configuration): Promise<Response> {
Expand Down
Loading

0 comments on commit 6146129

Please sign in to comment.