Skip to content

Commit 592b3e2

Browse files
committed
upgrade hey-api and ServerWallet improvements
1 parent f82a048 commit 592b3e2

25 files changed

+3084
-1612
lines changed

packages/engine/biome.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
{
2-
"$schema": "https://biomejs.dev/schemas/2.0.0/schema.json",
3-
"extends": "//"
2+
"$schema": "https://biomejs.dev/schemas/2.0.0/schema.json",
3+
"linter": {
4+
"rules": {
5+
"correctness": {
6+
"useImportExtensions": {
7+
"fix": "safe",
8+
"level": "error",
9+
"options": {
10+
"forceJsExtensions": true
11+
}
12+
}
13+
}
14+
}
15+
},
16+
"root": true
417
}

packages/engine/openapi-ts.config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { defineConfig } from "@hey-api/openapi-ts";
22

33
export default defineConfig({
44
// input: "https://engine.thirdweb.com/openapi",
5-
input: "http://localhost:3009/openapi",
5+
input: "http://localhost:3001/openapi",
66
output: { format: "biome", lint: "biome", path: "src/client" },
7-
plugins: ["@hey-api/client-fetch"],
87
});

packages/engine/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"devDependencies": {
1010
"@biomejs/biome": "2.0.4",
11-
"@hey-api/openapi-ts": "0.72.1",
11+
"@hey-api/openapi-ts": "0.77.0",
1212
"rimraf": "6.0.1",
1313
"tslib": "^2.8.1"
1414
},
@@ -47,11 +47,11 @@
4747
"build": "pnpm clean && pnpm build:cjs && pnpm build:esm && pnpm build:types",
4848
"build:cjs": "tsc --project ./tsconfig.build.json --module commonjs --outDir ./dist/cjs --verbatimModuleSyntax false && printf '{\"type\":\"commonjs\"}' > ./dist/cjs/package.json",
4949
"build:esm": "tsc --project ./tsconfig.build.json --module es2020 --outDir ./dist/esm && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./dist/esm/package.json",
50-
"build:generate": "openapi-ts && pnpm format",
50+
"build:generate": "openapi-ts && pnpm format && pnpm fix",
5151
"build:types": "tsc --project ./tsconfig.build.json --module esnext --declarationDir ./dist/types --emitDeclarationOnly --declaration --declarationMap",
5252
"clean": "rimraf dist",
53-
"fix": "biome check ./src --fix",
54-
"format": "biome format ./src --write",
53+
"fix": "biome check --write ./src",
54+
"format": "biome format --write ./src",
5555
"lint": "biome check ./src"
5656
},
5757
"type": "module",

packages/engine/src/client/client.gen.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
createClient,
66
createConfig,
77
type ClientOptions as DefaultClientOptions,
8-
} from "@hey-api/client-fetch";
8+
} from "./client/index.js";
99
import type { ClientOptions } from "./types.gen.js";
1010

1111
/**
@@ -23,6 +23,6 @@ export type CreateClientConfig<T extends DefaultClientOptions = ClientOptions> =
2323

2424
export const client = createClient(
2525
createConfig<ClientOptions>({
26-
baseUrl: "http://localhost:3009",
26+
baseUrl: "http://localhost:3001",
2727
}),
2828
);
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
import type { Client, Config, RequestOptions } from "./types.js";
2+
import {
3+
buildUrl,
4+
createConfig,
5+
createInterceptors,
6+
getParseAs,
7+
mergeConfigs,
8+
mergeHeaders,
9+
setAuthParams,
10+
} from "./utils.js";
11+
12+
type ReqInit = Omit<RequestInit, "body" | "headers"> & {
13+
body?: any;
14+
headers: ReturnType<typeof mergeHeaders>;
15+
};
16+
17+
export const createClient = (config: Config = {}): Client => {
18+
let _config = mergeConfigs(createConfig(), config);
19+
20+
const getConfig = (): Config => ({ ..._config });
21+
22+
const setConfig = (config: Config): Config => {
23+
_config = mergeConfigs(_config, config);
24+
return getConfig();
25+
};
26+
27+
const interceptors = createInterceptors<
28+
Request,
29+
Response,
30+
unknown,
31+
RequestOptions
32+
>();
33+
34+
const request: Client["request"] = async (options) => {
35+
const opts = {
36+
..._config,
37+
...options,
38+
fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,
39+
headers: mergeHeaders(_config.headers, options.headers),
40+
};
41+
42+
if (opts.security) {
43+
await setAuthParams({
44+
...opts,
45+
security: opts.security,
46+
});
47+
}
48+
49+
if (opts.requestValidator) {
50+
await opts.requestValidator(opts);
51+
}
52+
53+
if (opts.body && opts.bodySerializer) {
54+
opts.body = opts.bodySerializer(opts.body);
55+
}
56+
57+
// remove Content-Type header if body is empty to avoid sending invalid requests
58+
if (opts.body === undefined || opts.body === "") {
59+
opts.headers.delete("Content-Type");
60+
}
61+
62+
const url = buildUrl(opts);
63+
const requestInit: ReqInit = {
64+
redirect: "follow",
65+
...opts,
66+
};
67+
68+
let request = new Request(url, requestInit);
69+
70+
for (const fn of interceptors.request._fns) {
71+
if (fn) {
72+
request = await fn(request, opts);
73+
}
74+
}
75+
76+
// fetch must be assigned here, otherwise it would throw the error:
77+
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
78+
const _fetch = opts.fetch!;
79+
let response = await _fetch(request);
80+
81+
for (const fn of interceptors.response._fns) {
82+
if (fn) {
83+
response = await fn(response, request, opts);
84+
}
85+
}
86+
87+
const result = {
88+
request,
89+
response,
90+
};
91+
92+
if (response.ok) {
93+
if (
94+
response.status === 204 ||
95+
response.headers.get("Content-Length") === "0"
96+
) {
97+
return opts.responseStyle === "data"
98+
? {}
99+
: {
100+
data: {},
101+
...result,
102+
};
103+
}
104+
105+
const parseAs =
106+
(opts.parseAs === "auto"
107+
? getParseAs(response.headers.get("Content-Type"))
108+
: opts.parseAs) ?? "json";
109+
110+
let data: any;
111+
switch (parseAs) {
112+
case "arrayBuffer":
113+
case "blob":
114+
case "formData":
115+
case "json":
116+
case "text":
117+
data = await response[parseAs]();
118+
break;
119+
case "stream":
120+
return opts.responseStyle === "data"
121+
? response.body
122+
: {
123+
data: response.body,
124+
...result,
125+
};
126+
}
127+
128+
if (parseAs === "json") {
129+
if (opts.responseValidator) {
130+
await opts.responseValidator(data);
131+
}
132+
133+
if (opts.responseTransformer) {
134+
data = await opts.responseTransformer(data);
135+
}
136+
}
137+
138+
return opts.responseStyle === "data"
139+
? data
140+
: {
141+
data,
142+
...result,
143+
};
144+
}
145+
146+
let error = await response.text();
147+
148+
try {
149+
error = JSON.parse(error);
150+
} catch {
151+
// noop
152+
}
153+
154+
let finalError = error;
155+
156+
for (const fn of interceptors.error._fns) {
157+
if (fn) {
158+
finalError = (await fn(error, response, request, opts)) as string;
159+
}
160+
}
161+
162+
finalError = finalError || ({} as string);
163+
164+
if (opts.throwOnError) {
165+
throw finalError;
166+
}
167+
168+
// TODO: we probably want to return error and improve types
169+
return opts.responseStyle === "data"
170+
? undefined
171+
: {
172+
error: finalError,
173+
...result,
174+
};
175+
};
176+
177+
return {
178+
buildUrl,
179+
connect: (options) => request({ ...options, method: "CONNECT" }),
180+
delete: (options) => request({ ...options, method: "DELETE" }),
181+
get: (options) => request({ ...options, method: "GET" }),
182+
getConfig,
183+
head: (options) => request({ ...options, method: "HEAD" }),
184+
interceptors,
185+
options: (options) => request({ ...options, method: "OPTIONS" }),
186+
patch: (options) => request({ ...options, method: "PATCH" }),
187+
post: (options) => request({ ...options, method: "POST" }),
188+
put: (options) => request({ ...options, method: "PUT" }),
189+
request,
190+
setConfig,
191+
trace: (options) => request({ ...options, method: "TRACE" }),
192+
};
193+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export type { Auth } from "../core/auth.js";
2+
export type { QuerySerializerOptions } from "../core/bodySerializer.js";
3+
export {
4+
formDataBodySerializer,
5+
jsonBodySerializer,
6+
urlSearchParamsBodySerializer,
7+
} from "../core/bodySerializer.js";
8+
export { buildClientParams } from "../core/params.js";
9+
export { createClient } from "./client.js";
10+
export type {
11+
Client,
12+
ClientOptions,
13+
Config,
14+
CreateClientConfig,
15+
Options,
16+
OptionsLegacyParser,
17+
RequestOptions,
18+
RequestResult,
19+
ResponseStyle,
20+
TDataShape,
21+
} from "./types.js";
22+
export { createConfig, mergeHeaders } from "./utils.js";

0 commit comments

Comments
 (0)