Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Per-request OpenAPI config overrides #1777

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/authorization.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,22 @@ const getToken = async () => {

OpenAPI.TOKEN = getToken;
```

Also, you can pass `TOKEN` or any OpenAPI configuration params to method call.
```typescript
const organizations = new OrganizationsService();
OpenAPI.BASE = 'https://example.com/api';

express.use(async (req, res) => {
const response = await organizations.createOrganization(
{
name: 'OrgName',
description: 'OrgDescription',
},
{
TOKEN: getToken(req)
}
);
res.send(response);
})
```
7 changes: 5 additions & 2 deletions src/templates/core/BaseHttpRequest.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ export abstract class BaseHttpRequest {
constructor(public readonly config: OpenAPIConfig) {}
{{/equals}}

public abstract request<T>(
options: ApiRequestOptions,
configOverrides?: Partial<OpenAPIConfig>,
{{#equals @root.httpClient 'angular'}}
public abstract request<T>(options: ApiRequestOptions): Observable<T>;
): Observable<T>;
{{else}}
public abstract request<T>(options: ApiRequestOptions): CancelablePromise<T>;
): CancelablePromise<T>;
{{/equals}}
}
30 changes: 18 additions & 12 deletions src/templates/core/HttpRequest.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,31 @@ export class {{httpRequest}} extends BaseHttpRequest {
}
{{/equals}}

{{#equals @root.httpClient 'angular'}}
/**
* Request method
* @param options The request options from the service
* @param [configOverrides] Overrides OpenAPIConfig
{{#equals @root.httpClient 'angular'}}
* @returns Observable<T>
* @throws ApiError
*/
public override request<T>(options: ApiRequestOptions): Observable<T> {
return __request(this.config, this.http, options);
}
{{else}}
/**
* Request method
* @param options The request options from the service
* @returns CancelablePromise<T>
{{/equals}}
* @throws ApiError
*/
public override request<T>(options: ApiRequestOptions): CancelablePromise<T> {
return __request(this.config, options);
}
public override request<T>(
options: ApiRequestOptions,
configOverrides?: Partial<OpenAPIConfig>,
{{#equals @root.httpClient 'angular'}}
): Observable<T> {
{{else}}
): CancelablePromise<T> {
{{/equals}}
return __request(
{...this.config, ...configOverrides},
{{#equals @root.httpClient 'angular'}}
this.http,
{{/equals}}
options,
);
}
}
151 changes: 85 additions & 66 deletions src/templates/exportService.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import { BaseHttpRequest } from '../core/BaseHttpRequest';
{{else}}
import type { BaseHttpRequest } from '../core/BaseHttpRequest';
{{/equals}}
import type { OpenAPIConfig } from '../core/OpenAPI';
{{else}}
import type { OpenAPIConfig } from '../core/OpenAPI';
import { OpenAPI } from '../core/OpenAPI';
import { request as __request } from '../core/request';
{{/if}}
Expand Down Expand Up @@ -64,88 +66,105 @@ export class {{{name}}}{{{@root.postfix}}} {
* @param {{{name}}} {{#if description}}{{{escapeComment description}}}{{/if}}
{{/each}}
{{/if}}
* @param [configOverrides] Overrides OpenAPIConfig
{{/unless}}
{{#each results}}
* @returns {{{type}}} {{#if description}}{{{escapeComment description}}}{{/if}}
{{/each}}
* @throws ApiError
*/
{{#if @root.exportClient}}
public {{{name}}}(
{{>parameters}}
configOverrides?: Partial<OpenAPIConfig>,
{{#equals @root.httpClient 'angular'}}
public {{{name}}}({{>parameters}}): Observable<{{>result}}> {
return this.httpRequest.request({
): Observable<{{>result}}> {
{{else}}
public {{{name}}}({{>parameters}}): CancelablePromise<{{>result}}> {
return this.httpRequest.request({
): CancelablePromise<{{>result}}> {
{{/equals}}
return this.httpRequest.request(
{{else}}
{{#equals @root.httpClient 'angular'}}
public {{{name}}}({{>parameters}}): Observable<{{>result}}> {
return __request(OpenAPI, this.http, {
public {{{name}}}(
{{>parameters}}
configOverrides?: Partial<OpenAPIConfig>,
): Observable<{{>result}}> {
return __request(
{...OpenAPI, ...configOverrides},
this.http,
{{else}}
public static {{{name}}}({{>parameters}}): CancelablePromise<{{>result}}> {
return __request(OpenAPI, {
public static {{{name}}}(
{{>parameters}}
configOverrides?: Partial<OpenAPIConfig>,
): CancelablePromise<{{>result}}> {
return __request(
{...OpenAPI, ...configOverrides},
{{/equals}}
{{/if}}
method: '{{{method}}}',
url: '{{{path}}}',
{{#if parametersPath}}
path: {
{{#each parametersPath}}
'{{{prop}}}': {{{name}}},
{{/each}}
{
method: '{{{method}}}',
url: '{{{path}}}',
{{#if parametersPath}}
path: {
{{#each parametersPath}}
'{{{prop}}}': {{{name}}},
{{/each}}
},
{{/if}}
{{#if parametersCookie}}
cookies: {
{{#each parametersCookie}}
'{{{prop}}}': {{{name}}},
{{/each}}
},
{{/if}}
{{#if parametersHeader}}
headers: {
{{#each parametersHeader}}
'{{{prop}}}': {{{name}}},
{{/each}}
},
{{/if}}
{{#if parametersQuery}}
query: {
{{#each parametersQuery}}
'{{{prop}}}': {{{name}}},
{{/each}}
},
{{/if}}
{{#if parametersForm}}
formData: {
{{#each parametersForm}}
'{{{prop}}}': {{{name}}},
{{/each}}
},
{{/if}}
{{#if parametersBody}}
{{#equals parametersBody.in 'formData'}}
formData: {{{parametersBody.name}}},
{{/equals}}
{{#equals parametersBody.in 'body'}}
body: {{{parametersBody.name}}},
{{/equals}}
{{#if parametersBody.mediaType}}
mediaType: '{{{parametersBody.mediaType}}}',
{{/if}}
{{/if}}
{{#if responseHeader}}
responseHeader: '{{{responseHeader}}}',
{{/if}}
{{#if errors}}
errors: {
{{#each errors}}
{{{code}}}: `{{{escapeDescription description}}}`,
{{/each}}
},
{{/if}}
},
{{/if}}
{{#if parametersCookie}}
cookies: {
{{#each parametersCookie}}
'{{{prop}}}': {{{name}}},
{{/each}}
},
{{/if}}
{{#if parametersHeader}}
headers: {
{{#each parametersHeader}}
'{{{prop}}}': {{{name}}},
{{/each}}
},
{{/if}}
{{#if parametersQuery}}
query: {
{{#each parametersQuery}}
'{{{prop}}}': {{{name}}},
{{/each}}
},
{{/if}}
{{#if parametersForm}}
formData: {
{{#each parametersForm}}
'{{{prop}}}': {{{name}}},
{{/each}}
},
{{/if}}
{{#if parametersBody}}
{{#equals parametersBody.in 'formData'}}
formData: {{{parametersBody.name}}},
{{/equals}}
{{#equals parametersBody.in 'body'}}
body: {{{parametersBody.name}}},
{{/equals}}
{{#if parametersBody.mediaType}}
mediaType: '{{{parametersBody.mediaType}}}',
{{/if}}
{{/if}}
{{#if responseHeader}}
responseHeader: '{{{responseHeader}}}',
{{/if}}
{{#if errors}}
errors: {
{{#each errors}}
{{{code}}}: `{{{escapeDescription description}}}`,
{{/each}}
},
{{/if}}
});
{{#if @root.exportClient}}
configOverrides,
{{/if}}
);
}

{{/each}}
Expand Down
7 changes: 3 additions & 4 deletions src/templates/partials/parameters.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{#if parameters}}
{{#if @root.useOptions~}}
{{#if @root.useOptions}}
{
{{#each parameters}}
{{{name}}}{{#if default}} = {{{default}}}{{/if}},
Expand All @@ -18,9 +18,8 @@
{{/ifdef}}
{{{name}}}{{>isRequired}}: {{>type}},
{{/each}}
}
{{~else}}

},
{{else}}
{{#each parameters}}
{{{name}}}{{>isRequired}}: {{>type}}{{#if default}} = {{{default}}}{{/if}},
{{/each}}
Expand Down
Loading