Skip to content
This repository was archived by the owner on Apr 30, 2022. It is now read-only.

Commit c320355

Browse files
Feature: TzKT object query string (#1)
1 parent e351383 commit c320355

19 files changed

+133
-58
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "openapi-typescript-codegen",
2+
"name": "@baking-bad/openapi-typescript-codegen",
33
"version": "0.9.3",
44
"description": "Library that generates Typescript clients based on the OpenAPI specification.",
55
"author": "Ferdi Koomen",

src/openApi/v2/parser/getOperationPath.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ export function getOperationPath(path: string): string {
1212
.replace(/\{(.*?)\}/g, (_, w: string) => {
1313
return `\${${getOperationParameterName(w)}}`;
1414
})
15-
.replace('${apiVersion}', '${OpenAPI.VERSION}');
15+
.replace('${apiVersion}', '${this.config.version}');
1616
}

src/openApi/v3/parser/getOperationPath.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ export function getOperationPath(path: string): string {
1212
.replace(/\{(.*?)\}/g, (_, w: string) => {
1313
return `\${${getOperationParameterName(w)}}`;
1414
})
15-
.replace('${apiVersion}', '${OpenAPI.VERSION}');
15+
.replace('${apiVersion}', '${this.config.version}');
1616
}

src/templates/core/ApiOptions.hbs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{{>header}}
2+
3+
import type { Resolver, Headers } from './ApiRequestOptions';
4+
5+
export type ApiOptions = {
6+
baseUrl: string;
7+
version: string;
8+
withCredentials: boolean;
9+
token?: string | Resolver<string>;
10+
username?: string | Resolver<string>;
11+
password?: string | Resolver<string>;
12+
defaultHeaders?: Headers | Resolver<Headers>;
13+
}
14+
15+
export const DefaultApiOptions: ApiOptions = {
16+
baseUrl: '{{{server}}}',
17+
version: '{{{version}}}',
18+
withCredentials: false,
19+
token: undefined,
20+
username: undefined,
21+
password: undefined,
22+
defaultHeaders: undefined,
23+
};

src/templates/core/ApiRequestOptions.hbs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
{{>header}}
22

3+
export type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
4+
export type Headers = Record<string, string>;
5+
36
export type ApiRequestOptions = {
7+
readonly baseUrl: string;
8+
readonly withCredentials?: boolean;
9+
readonly version: string;
10+
readonly token?: string | Resolver<string>;
11+
readonly username?: string | Resolver<string>;
12+
readonly password?: string | Resolver<string>;
13+
readonly defaultHeaders?: Headers | Resolver<Headers>;
414
readonly method: 'GET' | 'PUT' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH';
515
readonly path: string;
616
readonly cookies?: Record<string, any>;

src/templates/core/OpenAPI.hbs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/templates/core/fetch/getHeaders.hbs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
async function getHeaders(options: ApiRequestOptions): Promise<Headers> {
2-
const token = await resolve(options, OpenAPI.TOKEN);
3-
const username = await resolve(options, OpenAPI.USERNAME);
4-
const password = await resolve(options, OpenAPI.PASSWORD);
5-
const defaultHeaders = await resolve(options, OpenAPI.HEADERS);
2+
const token = await resolve(options, options.token);
3+
const username = await resolve(options, options.username);
4+
const password = await resolve(options, options.password);
5+
const defaultHeaders = await resolve(options, options.headers);
66

77
const headers = new Headers({
88
Accept: 'application/json',

src/templates/core/fetch/request.hbs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import { ApiError } from './ApiError';
44
import type { ApiRequestOptions } from './ApiRequestOptions';
55
import type { ApiResult } from './ApiResult';
6-
import { OpenAPI } from './OpenAPI';
76

87
{{>functions/isDefined}}
98

src/templates/core/fetch/sendRequest.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ async function sendRequest(options: ApiRequestOptions, url: string): Promise<Res
44
headers: await getHeaders(options),
55
body: getRequestBody(options),
66
};
7-
if (OpenAPI.WITH_CREDENTIALS) {
7+
if (options.withCredentials) {
88
request.credentials = 'include';
99
}
1010
return await fetch(url, request);

src/templates/core/functions/getQueryString.hbs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,32 @@ function getQueryString(params: Record<string, any>): string {
77
value.forEach(value => {
88
qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
99
});
10+
} else if (typeof value === 'object') {
11+
switch (key) {
12+
case 'anyof': {
13+
qs.push(`${encodeURIComponent(key)}`);
14+
if (value.fields) {
15+
qs.push(`.${encodeURIComponent(Array(value.fileds).join('.'))}`);
16+
}
17+
qs.push(`=`);
18+
if (value.value) {
19+
qs.push(`${encodeURIComponent(value.value)}`);
20+
}
21+
}
22+
default: {
23+
Object.entries(value).map(raw => {
24+
let val = '';
25+
if (raw[1] instanceof Date) {
26+
val = (raw[1] as Date).toISOString();
27+
} else if (Array.isArray(raw[1])) {
28+
val = Array(raw[1]).join(',');
29+
} else {
30+
val = String(raw[1]);
31+
}
32+
qs.push(`${encodeURIComponent(key)}.${encodeURIComponent(String(raw[0]))}=${encodeURIComponent(val)}`);
33+
});
34+
}
35+
}
1036
} else {
1137
qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
1238
}

0 commit comments

Comments
 (0)