Skip to content

Commit 55f144f

Browse files
author
Lubos ​
committed
chore: update custom third-party client
1 parent a9bffe7 commit 55f144f

27 files changed

+583
-52
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"scripts": {
2323
"build": "turbo run build --filter=\"!@example/openapi-ts-sample\"",
2424
"changeset": "changeset",
25-
"client": "sh ./scripts/client.sh",
2625
"example": "sh ./scripts/example.sh",
2726
"format": "prettier --write .",
2827
"lint:fix": "prettier --check --write . && eslint . --fix",
@@ -46,7 +45,7 @@
4645
"@changesets/changelog-github": "0.5.0",
4746
"@changesets/cli": "2.27.8",
4847
"@config/vite-base": "workspace:*",
49-
"@hey-api/client-custom": "workspace:*",
48+
"@hey-api/custom-client": "workspace:*",
5049
"@types/node": "22.10.5",
5150
"@typescript-eslint/eslint-plugin": "8.29.1",
5251
"@vitest/coverage-v8": "3.1.1",
File renamed without changes.
File renamed without changes.

packages/client-custom/package.json renamed to packages/custom-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@hey-api/client-custom",
2+
"name": "@hey-api/custom-client",
33
"version": "0.1.1",
44
"description": "Custom client for `@hey-api/openapi-ts` codegen. Used for testing.",
55
"private": true,

packages/client-custom/rollup.config.mjs renamed to packages/custom-client/rollup.config.mjs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ export default files.map((file) =>
99
defineConfig({
1010
external: (id) => {
1111
const normalizedId = id.split(path.sep).join('/');
12-
if (normalizedId === '@hey-api/client-core') {
13-
return false;
14-
}
1512
return (
1613
!normalizedId.startsWith('/') && !/^[a-zA-Z]:\//.test(normalizedId)
1714
);

packages/client-custom/src/__tests__/utils.test.ts renamed to packages/custom-client/src/__tests__/utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { Auth } from '@hey-api/client-core';
21
import { describe, expect, it, vi } from 'vitest';
32

3+
import type { Auth } from '../core/auth';
44
import { getParseAs, setAuthParams } from '../utils';
55

66
describe('getParseAs', () => {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
export type AuthToken = string | undefined;
2+
3+
export interface Auth {
4+
/**
5+
* Which part of the request do we use to send the auth?
6+
*
7+
* @default 'header'
8+
*/
9+
in?: 'header' | 'query' | 'cookie';
10+
/**
11+
* Header or query parameter name.
12+
*
13+
* @default 'Authorization'
14+
*/
15+
name?: string;
16+
scheme?: 'basic' | 'bearer';
17+
type: 'apiKey' | 'http';
18+
}
19+
20+
export const getAuthToken = async (
21+
auth: Auth,
22+
callback: ((auth: Auth) => Promise<AuthToken> | AuthToken) | AuthToken,
23+
): Promise<string | undefined> => {
24+
const token =
25+
typeof callback === 'function' ? await callback(auth) : callback;
26+
27+
if (!token) {
28+
return;
29+
}
30+
31+
if (auth.scheme === 'bearer') {
32+
return `Bearer ${token}`;
33+
}
34+
35+
if (auth.scheme === 'basic') {
36+
return `Basic ${btoa(token)}`;
37+
}
38+
39+
return token;
40+
};
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import type {
2+
ArrayStyle,
3+
ObjectStyle,
4+
SerializerOptions,
5+
} from './pathSerializer';
6+
7+
export type QuerySerializer = (query: Record<string, unknown>) => string;
8+
9+
export type BodySerializer = (body: any) => any;
10+
11+
export interface QuerySerializerOptions {
12+
allowReserved?: boolean;
13+
array?: SerializerOptions<ArrayStyle>;
14+
object?: SerializerOptions<ObjectStyle>;
15+
}
16+
17+
const serializeFormDataPair = (data: FormData, key: string, value: unknown) => {
18+
if (typeof value === 'string' || value instanceof Blob) {
19+
data.append(key, value);
20+
} else {
21+
data.append(key, JSON.stringify(value));
22+
}
23+
};
24+
25+
const serializeUrlSearchParamsPair = (
26+
data: URLSearchParams,
27+
key: string,
28+
value: unknown,
29+
) => {
30+
if (typeof value === 'string') {
31+
data.append(key, value);
32+
} else {
33+
data.append(key, JSON.stringify(value));
34+
}
35+
};
36+
37+
export const formDataBodySerializer = {
38+
bodySerializer: <T extends Record<string, any> | Array<Record<string, any>>>(
39+
body: T,
40+
) => {
41+
const data = new FormData();
42+
43+
Object.entries(body).forEach(([key, value]) => {
44+
if (value === undefined || value === null) {
45+
return;
46+
}
47+
if (Array.isArray(value)) {
48+
value.forEach((v) => serializeFormDataPair(data, key, v));
49+
} else {
50+
serializeFormDataPair(data, key, value);
51+
}
52+
});
53+
54+
return data;
55+
},
56+
};
57+
58+
export const jsonBodySerializer = {
59+
bodySerializer: <T>(body: T) =>
60+
JSON.stringify(body, (key, value) =>
61+
typeof value === 'bigint' ? value.toString() : value,
62+
),
63+
};
64+
65+
export const urlSearchParamsBodySerializer = {
66+
bodySerializer: <T extends Record<string, any> | Array<Record<string, any>>>(
67+
body: T,
68+
) => {
69+
const data = new URLSearchParams();
70+
71+
Object.entries(body).forEach(([key, value]) => {
72+
if (value === undefined || value === null) {
73+
return;
74+
}
75+
if (Array.isArray(value)) {
76+
value.forEach((v) => serializeUrlSearchParamsPair(data, key, v));
77+
} else {
78+
serializeUrlSearchParamsPair(data, key, value);
79+
}
80+
});
81+
82+
return data.toString();
83+
},
84+
};

0 commit comments

Comments
 (0)