Skip to content

Commit 06ffbe7

Browse files
authored
Merge pull request #2491 from hey-api/fix/api-registry-shorthand-scalar
feat(parser): input supports Scalar API Registry with scalar: prefix
2 parents 572bba3 + ce602fe commit 06ffbe7

File tree

22 files changed

+411
-73
lines changed

22 files changed

+411
-73
lines changed

.changeset/twelve-mice-cheer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hey-api/openapi-ts': patch
3+
---
4+
5+
feat(parser): input supports Scalar API Registry with `scalar:` prefix

docs/openapi-ts/configuration/input.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ export default {
104104

105105
We also provide shorthands for other registries:
106106

107+
::: details Scalar
108+
Prefix your input with `scalar:` to use the Scalar API Registry.
109+
110+
```js [long]
111+
export default {
112+
input: 'scalar:@scalar/access-service', // [!code ++]
113+
};
114+
```
115+
116+
:::
117+
107118
::: details ReadMe
108119
Prefix your input with `readme:` to use the ReadMe API Registry.
109120

examples/openapi-ts-axios/src/client/client/client.ts renamed to examples/openapi-ts-axios/src/client/client/client.gen.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
1-
import type { AxiosError, RawAxiosRequestHeaders } from 'axios';
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios';
24
import axios from 'axios';
35

4-
import type { Client, Config } from './types';
6+
import type { Client, Config } from './types.gen';
57
import {
68
buildUrl,
79
createConfig,
810
mergeConfigs,
911
mergeHeaders,
1012
setAuthParams,
11-
} from './utils';
13+
} from './utils.gen';
1214

1315
export const createClient = (config: Config = {}): Client => {
1416
let _config = mergeConfigs(createConfig(), config);
1517

16-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
17-
const { auth, ...configWithoutAuth } = _config;
18-
const instance = axios.create(configWithoutAuth);
18+
let instance: AxiosInstance;
19+
20+
if (_config.axios && !('Axios' in _config.axios)) {
21+
instance = _config.axios;
22+
} else {
23+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
24+
const { auth, ...configWithoutAuth } = _config;
25+
instance = axios.create(configWithoutAuth);
26+
}
1927

2028
const getConfig = (): Config => ({ ..._config });
2129

@@ -46,6 +54,10 @@ export const createClient = (config: Config = {}): Client => {
4654
});
4755
}
4856

57+
if (opts.requestValidator) {
58+
await opts.requestValidator(opts);
59+
}
60+
4961
if (opts.body && opts.bodySerializer) {
5062
opts.body = opts.bodySerializer(opts.body);
5163
}
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
export type { Auth } from '../core/auth';
2-
export type { QuerySerializerOptions } from '../core/bodySerializer';
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
export type { Auth } from '../core/auth.gen';
4+
export type { QuerySerializerOptions } from '../core/bodySerializer.gen';
35
export {
46
formDataBodySerializer,
57
jsonBodySerializer,
68
urlSearchParamsBodySerializer,
7-
} from '../core/bodySerializer';
8-
export { buildClientParams } from '../core/params';
9-
export { createClient } from './client';
9+
} from '../core/bodySerializer.gen';
10+
export { buildClientParams } from '../core/params.gen';
11+
export { createClient } from './client.gen';
1012
export type {
1113
Client,
1214
ClientOptions,
@@ -17,5 +19,5 @@ export type {
1719
RequestOptions,
1820
RequestResult,
1921
TDataShape,
20-
} from './types';
21-
export { createConfig } from './utils';
22+
} from './types.gen';
23+
export { createConfig } from './utils.gen';

examples/openapi-ts-axios/src/client/client/types.ts renamed to examples/openapi-ts-axios/src/client/client/types.gen.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
13
import type {
24
AxiosError,
35
AxiosInstance,
6+
AxiosRequestHeaders,
47
AxiosResponse,
58
AxiosStatic,
69
CreateAxiosDefaults,
710
} from 'axios';
811

9-
import type { Auth } from '../core/auth';
10-
import type { Client as CoreClient, Config as CoreConfig } from '../core/types';
12+
import type { Auth } from '../core/auth.gen';
13+
import type {
14+
Client as CoreClient,
15+
Config as CoreConfig,
16+
} from '../core/types.gen';
1117

1218
export interface Config<T extends ClientOptions = ClientOptions>
1319
extends Omit<CreateAxiosDefaults, 'auth' | 'baseURL' | 'headers' | 'method'>,
1420
CoreConfig {
1521
/**
16-
* Axios implementation. You can use this option to provide a custom
17-
* Axios instance.
22+
* Axios implementation. You can use this option to provide either an
23+
* `AxiosStatic` or an `AxiosInstance`.
1824
*
1925
* @default axios
2026
*/
21-
axios?: AxiosStatic;
27+
axios?: AxiosStatic | AxiosInstance;
2228
/**
2329
* Base URL for all requests made by this client.
2430
*/
@@ -30,7 +36,7 @@ export interface Config<T extends ClientOptions = ClientOptions>
3036
* {@link https://developer.mozilla.org/docs/Web/API/Headers/Headers#init See more}
3137
*/
3238
headers?:
33-
| CreateAxiosDefaults['headers']
39+
| AxiosRequestHeaders
3440
| Record<
3541
string,
3642
| string

examples/openapi-ts-axios/src/client/client/utils.ts renamed to examples/openapi-ts-axios/src/client/client/utils.gen.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
import { getAuthToken } from '../core/auth';
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
import { getAuthToken } from '../core/auth.gen';
24
import type {
35
QuerySerializer,
46
QuerySerializerOptions,
5-
} from '../core/bodySerializer';
6-
import type { ArraySeparatorStyle } from '../core/pathSerializer';
7+
} from '../core/bodySerializer.gen';
8+
import type { ArraySeparatorStyle } from '../core/pathSerializer.gen';
79
import {
810
serializeArrayParam,
911
serializeObjectParam,
1012
serializePrimitiveParam,
11-
} from '../core/pathSerializer';
12-
import type { Client, ClientOptions, Config, RequestOptions } from './types';
13+
} from '../core/pathSerializer.gen';
14+
import type {
15+
Client,
16+
ClientOptions,
17+
Config,
18+
RequestOptions,
19+
} from './types.gen';
1320

1421
interface PathSerializer {
1522
path: Record<string, unknown>;
@@ -138,6 +145,28 @@ export const createQuerySerializer = <T = unknown>({
138145
return querySerializer;
139146
};
140147

148+
const checkForExistence = (
149+
options: Pick<RequestOptions, 'auth' | 'query'> & {
150+
headers: Record<any, unknown>;
151+
},
152+
name?: string,
153+
): boolean => {
154+
if (!name) {
155+
return false;
156+
}
157+
if (name in options.headers || options.query?.[name]) {
158+
return true;
159+
}
160+
if (
161+
'Cookie' in options.headers &&
162+
options.headers['Cookie'] &&
163+
typeof options.headers['Cookie'] === 'string'
164+
) {
165+
return options.headers['Cookie'].includes(`${name}=`);
166+
}
167+
return false;
168+
};
169+
141170
export const setAuthParams = async ({
142171
security,
143172
...options
@@ -146,6 +175,9 @@ export const setAuthParams = async ({
146175
headers: Record<any, unknown>;
147176
}) => {
148177
for (const auth of security) {
178+
if (checkForExistence(options, auth.name)) {
179+
continue;
180+
}
149181
const token = await getAuthToken(auth, options.auth);
150182

151183
if (!token) {
@@ -175,8 +207,6 @@ export const setAuthParams = async ({
175207
options.headers[name] = token;
176208
break;
177209
}
178-
179-
return;
180210
}
181211
};
182212

examples/openapi-ts-axios/src/client/core/auth.ts renamed to examples/openapi-ts-axios/src/client/core/auth.gen.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
13
export type AuthToken = string | undefined;
24

35
export interface Auth {

examples/openapi-ts-axios/src/client/core/bodySerializer.ts renamed to examples/openapi-ts-axios/src/client/core/bodySerializer.gen.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
13
import type {
24
ArrayStyle,
35
ObjectStyle,
46
SerializerOptions,
5-
} from './pathSerializer';
7+
} from './pathSerializer.gen';
68

79
export type QuerySerializer = (query: Record<string, unknown>) => string;
810

@@ -14,9 +16,15 @@ export interface QuerySerializerOptions {
1416
object?: SerializerOptions<ObjectStyle>;
1517
}
1618

17-
const serializeFormDataPair = (data: FormData, key: string, value: unknown) => {
19+
const serializeFormDataPair = (
20+
data: FormData,
21+
key: string,
22+
value: unknown,
23+
): void => {
1824
if (typeof value === 'string' || value instanceof Blob) {
1925
data.append(key, value);
26+
} else if (value instanceof Date) {
27+
data.append(key, value.toISOString());
2028
} else {
2129
data.append(key, JSON.stringify(value));
2230
}
@@ -26,7 +34,7 @@ const serializeUrlSearchParamsPair = (
2634
data: URLSearchParams,
2735
key: string,
2836
value: unknown,
29-
) => {
37+
): void => {
3038
if (typeof value === 'string') {
3139
data.append(key, value);
3240
} else {
@@ -37,7 +45,7 @@ const serializeUrlSearchParamsPair = (
3745
export const formDataBodySerializer = {
3846
bodySerializer: <T extends Record<string, any> | Array<Record<string, any>>>(
3947
body: T,
40-
) => {
48+
): FormData => {
4149
const data = new FormData();
4250

4351
Object.entries(body).forEach(([key, value]) => {
@@ -56,16 +64,16 @@ export const formDataBodySerializer = {
5664
};
5765

5866
export const jsonBodySerializer = {
59-
bodySerializer: <T>(body: T) =>
60-
JSON.stringify(body, (key, value) =>
67+
bodySerializer: <T>(body: T): string =>
68+
JSON.stringify(body, (_key, value) =>
6169
typeof value === 'bigint' ? value.toString() : value,
6270
),
6371
};
6472

6573
export const urlSearchParamsBodySerializer = {
6674
bodySerializer: <T extends Record<string, any> | Array<Record<string, any>>>(
6775
body: T,
68-
) => {
76+
): string => {
6977
const data = new URLSearchParams();
7078

7179
Object.entries(body).forEach(([key, value]) => {

examples/openapi-ts-axios/src/client/core/params.ts renamed to examples/openapi-ts-axios/src/client/core/params.gen.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
13
type Slot = 'body' | 'headers' | 'path' | 'query';
24

35
export type Field =
46
| {
57
in: Exclude<Slot, 'body'>;
8+
/**
9+
* Field name. This is the name we want the user to see and use.
10+
*/
611
key: string;
12+
/**
13+
* Field mapped name. This is the name we want to use in the request.
14+
* If omitted, we use the same value as `key`.
15+
*/
716
map?: string;
817
}
918
| {
1019
in: Extract<Slot, 'body'>;
20+
/**
21+
* Key isn't required for bodies.
22+
*/
1123
key?: string;
1224
map?: string;
1325
};

examples/openapi-ts-axios/src/client/core/pathSerializer.ts renamed to examples/openapi-ts-axios/src/client/core/pathSerializer.gen.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
13
interface SerializeOptions<T>
24
extends SerializePrimitiveOptions,
35
SerializerOptions<T> {}

0 commit comments

Comments
 (0)