Skip to content

Commit

Permalink
feat: Per-request OpenAPI config overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
eakorolev committed Sep 21, 2023
1 parent 3dee68a commit bb8e92d
Show file tree
Hide file tree
Showing 25 changed files with 6,270 additions and 658 deletions.
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?: 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?: OpenAPIConfig
{{#equals @root.httpClient 'angular'}}
): Observable<T> {
{{else}}
): CancelablePromise<T> {
{{/equals}}
return __request(
{...this.config, ...configOverrides},
{{#equals @root.httpClient 'angular'}}
this.http,
{{/equals}}
options
);
}
}
159 changes: 93 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 @@ -63,6 +65,7 @@ export class {{{name}}}{{{@root.postfix}}} {
{{#each parameters}}
* @param {{{name}}} {{#if description}}{{{escapeComment description}}}{{/if}}
{{/each}}
* @param [configOverrides] Overrides OpenAPIConfig
{{/if}}
{{/unless}}
{{#each results}}
Expand All @@ -71,81 +74,105 @@ export class {{{name}}}{{{@root.postfix}}} {
* @throws ApiError
*/
{{#if @root.exportClient}}
public {{{name}}}(
{{>parameters}}
configOverrides?: 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?: OpenAPIConfig,
): Observable<{{>result}}> {
return __request(
{
...OpenAPI,
...configOverrides,
},
this.http,
{{else}}
public static {{{name}}}({{>parameters}}): CancelablePromise<{{>result}}> {
return __request(OpenAPI, {
public static {{{name}}}(
{{>parameters}}
configOverrides?: OpenAPIConfig,
): CancelablePromise<{{>result}}> {
return __request(
{
...OpenAPI,
...configOverrides,
},
{{/equals}}
{{/if}}
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}}
{
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 @root.exportClient}}
},
{{/if}}
});
configOverrides,
{{else}}
}
{{/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

0 comments on commit bb8e92d

Please sign in to comment.