@dingmenghua/opencode-sdk 1.15.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.d.ts +7 -0
- package/dist/client.js +53 -0
- package/dist/error-interceptor.d.ts +15 -0
- package/dist/error-interceptor.js +43 -0
- package/dist/gen/client/client.gen.d.ts +2 -0
- package/dist/gen/client/client.gen.js +165 -0
- package/dist/gen/client/index.d.ts +7 -0
- package/dist/gen/client/index.js +5 -0
- package/dist/gen/client/types.gen.d.ts +127 -0
- package/dist/gen/client/types.gen.js +2 -0
- package/dist/gen/client/utils.gen.d.ts +38 -0
- package/dist/gen/client/utils.gen.js +226 -0
- package/dist/gen/client.gen.d.ts +12 -0
- package/dist/gen/client.gen.js +5 -0
- package/dist/gen/core/auth.gen.d.ts +18 -0
- package/dist/gen/core/auth.gen.js +14 -0
- package/dist/gen/core/bodySerializer.gen.d.ts +17 -0
- package/dist/gen/core/bodySerializer.gen.js +57 -0
- package/dist/gen/core/params.gen.d.ts +33 -0
- package/dist/gen/core/params.gen.js +89 -0
- package/dist/gen/core/pathSerializer.gen.d.ts +33 -0
- package/dist/gen/core/pathSerializer.gen.js +106 -0
- package/dist/gen/core/queryKeySerializer.gen.d.ts +18 -0
- package/dist/gen/core/queryKeySerializer.gen.js +93 -0
- package/dist/gen/core/serverSentEvents.gen.d.ts +59 -0
- package/dist/gen/core/serverSentEvents.gen.js +117 -0
- package/dist/gen/core/types.gen.d.ts +78 -0
- package/dist/gen/core/types.gen.js +2 -0
- package/dist/gen/core/utils.gen.d.ts +14 -0
- package/dist/gen/core/utils.gen.js +69 -0
- package/dist/gen/sdk.gen.d.ts +403 -0
- package/dist/gen/sdk.gen.js +881 -0
- package/dist/gen/types.gen.d.ts +3383 -0
- package/dist/gen/types.gen.js +2 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +16 -0
- package/dist/process.d.ts +3 -0
- package/dist/process.js +33 -0
- package/dist/server.d.ts +23 -0
- package/dist/server.js +105 -0
- package/dist/v2/client.d.ts +8 -0
- package/dist/v2/client.js +78 -0
- package/dist/v2/data.d.ts +9 -0
- package/dist/v2/data.js +22 -0
- package/dist/v2/gen/client/client.gen.d.ts +2 -0
- package/dist/v2/gen/client/client.gen.js +232 -0
- package/dist/v2/gen/client/index.d.ts +8 -0
- package/dist/v2/gen/client/index.js +6 -0
- package/dist/v2/gen/client/types.gen.d.ts +117 -0
- package/dist/v2/gen/client/types.gen.js +2 -0
- package/dist/v2/gen/client/utils.gen.d.ts +33 -0
- package/dist/v2/gen/client/utils.gen.js +226 -0
- package/dist/v2/gen/client.gen.d.ts +12 -0
- package/dist/v2/gen/client.gen.js +3 -0
- package/dist/v2/gen/core/auth.gen.d.ts +18 -0
- package/dist/v2/gen/core/auth.gen.js +14 -0
- package/dist/v2/gen/core/bodySerializer.gen.d.ts +25 -0
- package/dist/v2/gen/core/bodySerializer.gen.js +57 -0
- package/dist/v2/gen/core/params.gen.d.ts +43 -0
- package/dist/v2/gen/core/params.gen.js +102 -0
- package/dist/v2/gen/core/pathSerializer.gen.d.ts +33 -0
- package/dist/v2/gen/core/pathSerializer.gen.js +106 -0
- package/dist/v2/gen/core/queryKeySerializer.gen.d.ts +18 -0
- package/dist/v2/gen/core/queryKeySerializer.gen.js +93 -0
- package/dist/v2/gen/core/serverSentEvents.gen.d.ts +71 -0
- package/dist/v2/gen/core/serverSentEvents.gen.js +133 -0
- package/dist/v2/gen/core/types.gen.d.ts +78 -0
- package/dist/v2/gen/core/types.gen.js +2 -0
- package/dist/v2/gen/core/utils.gen.d.ts +19 -0
- package/dist/v2/gen/core/utils.gen.js +87 -0
- package/dist/v2/gen/sdk.gen.d.ts +1610 -0
- package/dist/v2/gen/sdk.gen.js +3234 -0
- package/dist/v2/gen/types.gen.d.ts +6222 -0
- package/dist/v2/gen/types.gen.js +2 -0
- package/dist/v2/index.d.ts +11 -0
- package/dist/v2/index.js +17 -0
- package/dist/v2/server.d.ts +23 -0
- package/dist/v2/server.js +105 -0
- package/package.json +55 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./client.js";
|
|
2
|
+
export * from "./server.js";
|
|
3
|
+
import type { ServerOptions } from "./server.js";
|
|
4
|
+
export declare function createOpencode(options?: ServerOptions): Promise<{
|
|
5
|
+
client: import("./client.js").OpencodeClient;
|
|
6
|
+
server: {
|
|
7
|
+
url: string;
|
|
8
|
+
close(): void;
|
|
9
|
+
};
|
|
10
|
+
}>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export * from "./client.js";
|
|
2
|
+
export * from "./server.js";
|
|
3
|
+
import { createOpencodeClient } from "./client.js";
|
|
4
|
+
import { createOpencodeServer } from "./server.js";
|
|
5
|
+
export async function createOpencode(options) {
|
|
6
|
+
const server = await createOpencodeServer({
|
|
7
|
+
...options,
|
|
8
|
+
});
|
|
9
|
+
const client = createOpencodeClient({
|
|
10
|
+
baseUrl: server.url,
|
|
11
|
+
});
|
|
12
|
+
return {
|
|
13
|
+
client,
|
|
14
|
+
server,
|
|
15
|
+
};
|
|
16
|
+
}
|
package/dist/process.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
// Duplicated from `packages/opencode/src/util/process.ts` because the SDK cannot
|
|
3
|
+
// import `opencode` without creating a cycle (`opencode` depends on `@opencode-ai/sdk`).
|
|
4
|
+
export function stop(proc) {
|
|
5
|
+
if (proc.exitCode !== null || proc.signalCode !== null)
|
|
6
|
+
return;
|
|
7
|
+
if (process.platform === "win32" && proc.pid) {
|
|
8
|
+
const out = spawnSync("taskkill", ["/pid", String(proc.pid), "/T", "/F"], { windowsHide: true });
|
|
9
|
+
if (!out.error && out.status === 0)
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
proc.kill();
|
|
13
|
+
}
|
|
14
|
+
export function bindAbort(proc, signal, onAbort) {
|
|
15
|
+
if (!signal)
|
|
16
|
+
return () => { };
|
|
17
|
+
const abort = () => {
|
|
18
|
+
clear();
|
|
19
|
+
stop(proc);
|
|
20
|
+
onAbort?.();
|
|
21
|
+
};
|
|
22
|
+
const clear = () => {
|
|
23
|
+
signal.removeEventListener("abort", abort);
|
|
24
|
+
proc.off("exit", clear);
|
|
25
|
+
proc.off("error", clear);
|
|
26
|
+
};
|
|
27
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
28
|
+
proc.on("exit", clear);
|
|
29
|
+
proc.on("error", clear);
|
|
30
|
+
if (signal.aborted)
|
|
31
|
+
abort();
|
|
32
|
+
return clear;
|
|
33
|
+
}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Config } from "./gen/types.gen.js";
|
|
2
|
+
export type ServerOptions = {
|
|
3
|
+
hostname?: string;
|
|
4
|
+
port?: number;
|
|
5
|
+
signal?: AbortSignal;
|
|
6
|
+
timeout?: number;
|
|
7
|
+
config?: Config;
|
|
8
|
+
};
|
|
9
|
+
export type TuiOptions = {
|
|
10
|
+
project?: string;
|
|
11
|
+
model?: string;
|
|
12
|
+
session?: string;
|
|
13
|
+
agent?: string;
|
|
14
|
+
signal?: AbortSignal;
|
|
15
|
+
config?: Config;
|
|
16
|
+
};
|
|
17
|
+
export declare function createOpencodeServer(options?: ServerOptions): Promise<{
|
|
18
|
+
url: string;
|
|
19
|
+
close(): void;
|
|
20
|
+
}>;
|
|
21
|
+
export declare function createOpencodeTui(options?: TuiOptions): {
|
|
22
|
+
close(): void;
|
|
23
|
+
};
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import launch from "cross-spawn";
|
|
2
|
+
import { stop, bindAbort } from "./process.js";
|
|
3
|
+
export async function createOpencodeServer(options) {
|
|
4
|
+
options = Object.assign({
|
|
5
|
+
hostname: "127.0.0.1",
|
|
6
|
+
port: 4096,
|
|
7
|
+
timeout: 5000,
|
|
8
|
+
}, options ?? {});
|
|
9
|
+
const args = [`serve`, `--hostname=${options.hostname}`, `--port=${options.port}`];
|
|
10
|
+
if (options.config?.logLevel)
|
|
11
|
+
args.push(`--log-level=${options.config.logLevel}`);
|
|
12
|
+
const proc = launch(`opencode`, args, {
|
|
13
|
+
env: {
|
|
14
|
+
...process.env,
|
|
15
|
+
OPENCODE_CONFIG_CONTENT: JSON.stringify(options.config ?? {}),
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
let clear = () => { };
|
|
19
|
+
const url = await new Promise((resolve, reject) => {
|
|
20
|
+
const id = setTimeout(() => {
|
|
21
|
+
clear();
|
|
22
|
+
stop(proc);
|
|
23
|
+
reject(new Error(`Timeout waiting for server to start after ${options.timeout}ms`));
|
|
24
|
+
}, options.timeout);
|
|
25
|
+
let output = "";
|
|
26
|
+
let resolved = false;
|
|
27
|
+
proc.stdout?.on("data", (chunk) => {
|
|
28
|
+
if (resolved)
|
|
29
|
+
return;
|
|
30
|
+
output += chunk.toString();
|
|
31
|
+
const lines = output.split("\n");
|
|
32
|
+
for (const line of lines) {
|
|
33
|
+
if (line.startsWith("opencode server listening")) {
|
|
34
|
+
const match = line.match(/on\s+(https?:\/\/[^\s]+)/);
|
|
35
|
+
if (!match) {
|
|
36
|
+
clear();
|
|
37
|
+
stop(proc);
|
|
38
|
+
clearTimeout(id);
|
|
39
|
+
reject(new Error(`Failed to parse server url from output: ${line}`));
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
clearTimeout(id);
|
|
43
|
+
resolved = true;
|
|
44
|
+
resolve(match[1]);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
proc.stderr?.on("data", (chunk) => {
|
|
50
|
+
output += chunk.toString();
|
|
51
|
+
});
|
|
52
|
+
proc.on("exit", (code) => {
|
|
53
|
+
clearTimeout(id);
|
|
54
|
+
let msg = `Server exited with code ${code}`;
|
|
55
|
+
if (output.trim()) {
|
|
56
|
+
msg += `\nServer output: ${output}`;
|
|
57
|
+
}
|
|
58
|
+
reject(new Error(msg));
|
|
59
|
+
});
|
|
60
|
+
proc.on("error", (error) => {
|
|
61
|
+
clearTimeout(id);
|
|
62
|
+
reject(error);
|
|
63
|
+
});
|
|
64
|
+
clear = bindAbort(proc, options.signal, () => {
|
|
65
|
+
clearTimeout(id);
|
|
66
|
+
reject(options.signal?.reason);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
return {
|
|
70
|
+
url,
|
|
71
|
+
close() {
|
|
72
|
+
clear();
|
|
73
|
+
stop(proc);
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
export function createOpencodeTui(options) {
|
|
78
|
+
const args = [];
|
|
79
|
+
if (options?.project) {
|
|
80
|
+
args.push(`--project=${options.project}`);
|
|
81
|
+
}
|
|
82
|
+
if (options?.model) {
|
|
83
|
+
args.push(`--model=${options.model}`);
|
|
84
|
+
}
|
|
85
|
+
if (options?.session) {
|
|
86
|
+
args.push(`--session=${options.session}`);
|
|
87
|
+
}
|
|
88
|
+
if (options?.agent) {
|
|
89
|
+
args.push(`--agent=${options.agent}`);
|
|
90
|
+
}
|
|
91
|
+
const proc = launch(`opencode`, args, {
|
|
92
|
+
stdio: "inherit",
|
|
93
|
+
env: {
|
|
94
|
+
...process.env,
|
|
95
|
+
OPENCODE_CONFIG_CONTENT: JSON.stringify(options?.config ?? {}),
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
const clear = bindAbort(proc, options?.signal);
|
|
99
|
+
return {
|
|
100
|
+
close() {
|
|
101
|
+
clear();
|
|
102
|
+
stop(proc);
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./gen/types.gen.js";
|
|
2
|
+
import { type Config } from "./gen/client/types.gen.js";
|
|
3
|
+
import { OpencodeClient } from "./gen/sdk.gen.js";
|
|
4
|
+
export { type Config as OpencodeClientConfig, OpencodeClient };
|
|
5
|
+
export declare function createOpencodeClient(config?: Config & {
|
|
6
|
+
directory?: string;
|
|
7
|
+
experimental_workspaceID?: string;
|
|
8
|
+
}): OpencodeClient;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export * from "./gen/types.gen.js";
|
|
2
|
+
import { createClient } from "./gen/client/client.gen.js";
|
|
3
|
+
import { OpencodeClient } from "./gen/sdk.gen.js";
|
|
4
|
+
import { wrapClientError } from "../error-interceptor.js";
|
|
5
|
+
export { OpencodeClient };
|
|
6
|
+
function pick(value, fallback, encode) {
|
|
7
|
+
if (!value)
|
|
8
|
+
return;
|
|
9
|
+
if (!fallback)
|
|
10
|
+
return value;
|
|
11
|
+
if (value === fallback)
|
|
12
|
+
return fallback;
|
|
13
|
+
if (encode && value === encode(fallback))
|
|
14
|
+
return fallback;
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
function rewrite(request, values) {
|
|
18
|
+
if (request.method !== "GET" && request.method !== "HEAD")
|
|
19
|
+
return request;
|
|
20
|
+
const url = new URL(request.url);
|
|
21
|
+
let changed = false;
|
|
22
|
+
for (const [name, key] of [
|
|
23
|
+
["x-opencode-directory", "directory"],
|
|
24
|
+
["x-opencode-workspace", "workspace"],
|
|
25
|
+
]) {
|
|
26
|
+
const value = pick(request.headers.get(name), key === "directory" ? values.directory : values.workspace, key === "directory" ? encodeURIComponent : undefined);
|
|
27
|
+
if (!value)
|
|
28
|
+
continue;
|
|
29
|
+
if (!url.searchParams.has(key)) {
|
|
30
|
+
url.searchParams.set(key, value);
|
|
31
|
+
}
|
|
32
|
+
changed = true;
|
|
33
|
+
}
|
|
34
|
+
if (!changed)
|
|
35
|
+
return request;
|
|
36
|
+
const next = new Request(url, request);
|
|
37
|
+
next.headers.delete("x-opencode-directory");
|
|
38
|
+
next.headers.delete("x-opencode-workspace");
|
|
39
|
+
return next;
|
|
40
|
+
}
|
|
41
|
+
export function createOpencodeClient(config) {
|
|
42
|
+
if (!config?.fetch) {
|
|
43
|
+
const customFetch = (req) => {
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
req.timeout = false;
|
|
46
|
+
return fetch(req);
|
|
47
|
+
};
|
|
48
|
+
config = {
|
|
49
|
+
...config,
|
|
50
|
+
fetch: customFetch,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
if (config?.directory) {
|
|
54
|
+
config.headers = {
|
|
55
|
+
...config.headers,
|
|
56
|
+
"x-opencode-directory": encodeURIComponent(config.directory),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
if (config?.experimental_workspaceID) {
|
|
60
|
+
config.headers = {
|
|
61
|
+
...config.headers,
|
|
62
|
+
"x-opencode-workspace": config.experimental_workspaceID,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
const client = createClient(config);
|
|
66
|
+
client.interceptors.request.use((request) => rewrite(request, {
|
|
67
|
+
directory: config?.directory,
|
|
68
|
+
workspace: config?.experimental_workspaceID,
|
|
69
|
+
}));
|
|
70
|
+
client.interceptors.response.use((response) => {
|
|
71
|
+
const contentType = response.headers.get("content-type");
|
|
72
|
+
if (contentType === "text/html")
|
|
73
|
+
throw new Error("Request is not supported by this version of OpenCode Server (Server responded with text/html)");
|
|
74
|
+
return response;
|
|
75
|
+
});
|
|
76
|
+
client.interceptors.error.use(wrapClientError);
|
|
77
|
+
return new OpencodeClient({ client });
|
|
78
|
+
}
|
package/dist/v2/data.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export const message = {
|
|
2
|
+
user(input) {
|
|
3
|
+
const { parts: _parts, ...rest } = input;
|
|
4
|
+
const info = {
|
|
5
|
+
...rest,
|
|
6
|
+
id: "asdasd",
|
|
7
|
+
time: {
|
|
8
|
+
created: Date.now(),
|
|
9
|
+
},
|
|
10
|
+
role: "user",
|
|
11
|
+
};
|
|
12
|
+
return {
|
|
13
|
+
info,
|
|
14
|
+
parts: input.parts.map((part) => ({
|
|
15
|
+
...part,
|
|
16
|
+
id: "asdasd",
|
|
17
|
+
messageID: info.id,
|
|
18
|
+
sessionID: info.sessionID,
|
|
19
|
+
})),
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
};
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
// This file is auto-generated by @hey-api/openapi-ts
|
|
2
|
+
import { createSseClient } from "../core/serverSentEvents.gen.js";
|
|
3
|
+
import { getValidRequestBody } from "../core/utils.gen.js";
|
|
4
|
+
import { buildUrl, createConfig, createInterceptors, getParseAs, mergeConfigs, mergeHeaders, setAuthParams, } from "./utils.gen.js";
|
|
5
|
+
export const createClient = (config = {}) => {
|
|
6
|
+
let _config = mergeConfigs(createConfig(), config);
|
|
7
|
+
const getConfig = () => ({ ..._config });
|
|
8
|
+
const setConfig = (config) => {
|
|
9
|
+
_config = mergeConfigs(_config, config);
|
|
10
|
+
return getConfig();
|
|
11
|
+
};
|
|
12
|
+
const interceptors = createInterceptors();
|
|
13
|
+
const beforeRequest = async (options) => {
|
|
14
|
+
const opts = {
|
|
15
|
+
..._config,
|
|
16
|
+
...options,
|
|
17
|
+
fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,
|
|
18
|
+
headers: mergeHeaders(_config.headers, options.headers),
|
|
19
|
+
serializedBody: undefined,
|
|
20
|
+
};
|
|
21
|
+
if (opts.security) {
|
|
22
|
+
await setAuthParams({
|
|
23
|
+
...opts,
|
|
24
|
+
security: opts.security,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
if (opts.requestValidator) {
|
|
28
|
+
await opts.requestValidator(opts);
|
|
29
|
+
}
|
|
30
|
+
if (opts.body !== undefined && opts.bodySerializer) {
|
|
31
|
+
opts.serializedBody = opts.bodySerializer(opts.body);
|
|
32
|
+
}
|
|
33
|
+
// remove Content-Type header if body is empty to avoid sending invalid requests
|
|
34
|
+
if (opts.body === undefined || opts.serializedBody === "") {
|
|
35
|
+
opts.headers.delete("Content-Type");
|
|
36
|
+
}
|
|
37
|
+
const url = buildUrl(opts);
|
|
38
|
+
return { opts, url };
|
|
39
|
+
};
|
|
40
|
+
const request = async (options) => {
|
|
41
|
+
// @ts-expect-error
|
|
42
|
+
const { opts, url } = await beforeRequest(options);
|
|
43
|
+
const requestInit = {
|
|
44
|
+
redirect: "follow",
|
|
45
|
+
...opts,
|
|
46
|
+
body: getValidRequestBody(opts),
|
|
47
|
+
};
|
|
48
|
+
let request = new Request(url, requestInit);
|
|
49
|
+
for (const fn of interceptors.request.fns) {
|
|
50
|
+
if (fn) {
|
|
51
|
+
request = await fn(request, opts);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// fetch must be assigned here, otherwise it would throw the error:
|
|
55
|
+
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
|
|
56
|
+
const _fetch = opts.fetch;
|
|
57
|
+
let response;
|
|
58
|
+
try {
|
|
59
|
+
response = await _fetch(request);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
// Handle fetch exceptions (AbortError, network errors, etc.)
|
|
63
|
+
let finalError = error;
|
|
64
|
+
for (const fn of interceptors.error.fns) {
|
|
65
|
+
if (fn) {
|
|
66
|
+
finalError = (await fn(error, undefined, request, opts));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
finalError = finalError || {};
|
|
70
|
+
if (opts.throwOnError) {
|
|
71
|
+
throw finalError;
|
|
72
|
+
}
|
|
73
|
+
// Return error response
|
|
74
|
+
return opts.responseStyle === "data"
|
|
75
|
+
? undefined
|
|
76
|
+
: {
|
|
77
|
+
error: finalError,
|
|
78
|
+
request,
|
|
79
|
+
response: undefined,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
for (const fn of interceptors.response.fns) {
|
|
83
|
+
if (fn) {
|
|
84
|
+
response = await fn(response, request, opts);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const result = {
|
|
88
|
+
request,
|
|
89
|
+
response,
|
|
90
|
+
};
|
|
91
|
+
if (response.ok) {
|
|
92
|
+
const parseAs = (opts.parseAs === "auto" ? getParseAs(response.headers.get("Content-Type")) : opts.parseAs) ?? "json";
|
|
93
|
+
if (response.status === 204 || response.headers.get("Content-Length") === "0") {
|
|
94
|
+
let emptyData;
|
|
95
|
+
switch (parseAs) {
|
|
96
|
+
case "arrayBuffer":
|
|
97
|
+
case "blob":
|
|
98
|
+
case "text":
|
|
99
|
+
emptyData = await response[parseAs]();
|
|
100
|
+
break;
|
|
101
|
+
case "formData":
|
|
102
|
+
emptyData = new FormData();
|
|
103
|
+
break;
|
|
104
|
+
case "stream":
|
|
105
|
+
emptyData = response.body;
|
|
106
|
+
break;
|
|
107
|
+
case "json":
|
|
108
|
+
default:
|
|
109
|
+
emptyData = {};
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
return opts.responseStyle === "data"
|
|
113
|
+
? emptyData
|
|
114
|
+
: {
|
|
115
|
+
data: emptyData,
|
|
116
|
+
...result,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
let data;
|
|
120
|
+
switch (parseAs) {
|
|
121
|
+
case "arrayBuffer":
|
|
122
|
+
case "blob":
|
|
123
|
+
case "formData":
|
|
124
|
+
case "text":
|
|
125
|
+
data = await response[parseAs]();
|
|
126
|
+
break;
|
|
127
|
+
case "json": {
|
|
128
|
+
// Some servers return 200 with no Content-Length and empty body.
|
|
129
|
+
// response.json() would throw; read as text and parse if non-empty.
|
|
130
|
+
const text = await response.text();
|
|
131
|
+
data = text ? JSON.parse(text) : {};
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
case "stream":
|
|
135
|
+
return opts.responseStyle === "data"
|
|
136
|
+
? response.body
|
|
137
|
+
: {
|
|
138
|
+
data: response.body,
|
|
139
|
+
...result,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
if (parseAs === "json") {
|
|
143
|
+
if (opts.responseValidator) {
|
|
144
|
+
await opts.responseValidator(data);
|
|
145
|
+
}
|
|
146
|
+
if (opts.responseTransformer) {
|
|
147
|
+
data = await opts.responseTransformer(data);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return opts.responseStyle === "data"
|
|
151
|
+
? data
|
|
152
|
+
: {
|
|
153
|
+
data,
|
|
154
|
+
...result,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
const textError = await response.text();
|
|
158
|
+
let jsonError;
|
|
159
|
+
try {
|
|
160
|
+
jsonError = JSON.parse(textError);
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
// noop
|
|
164
|
+
}
|
|
165
|
+
const error = jsonError ?? textError;
|
|
166
|
+
let finalError = error;
|
|
167
|
+
for (const fn of interceptors.error.fns) {
|
|
168
|
+
if (fn) {
|
|
169
|
+
finalError = (await fn(error, response, request, opts));
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
finalError = finalError || {};
|
|
173
|
+
if (opts.throwOnError) {
|
|
174
|
+
throw finalError;
|
|
175
|
+
}
|
|
176
|
+
// TODO: we probably want to return error and improve types
|
|
177
|
+
return opts.responseStyle === "data"
|
|
178
|
+
? undefined
|
|
179
|
+
: {
|
|
180
|
+
error: finalError,
|
|
181
|
+
...result,
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
const makeMethodFn = (method) => (options) => request({ ...options, method });
|
|
185
|
+
const makeSseFn = (method) => async (options) => {
|
|
186
|
+
const { opts, url } = await beforeRequest(options);
|
|
187
|
+
return createSseClient({
|
|
188
|
+
...opts,
|
|
189
|
+
body: opts.body,
|
|
190
|
+
headers: opts.headers,
|
|
191
|
+
method,
|
|
192
|
+
onRequest: async (url, init) => {
|
|
193
|
+
let request = new Request(url, init);
|
|
194
|
+
for (const fn of interceptors.request.fns) {
|
|
195
|
+
if (fn) {
|
|
196
|
+
request = await fn(request, opts);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return request;
|
|
200
|
+
},
|
|
201
|
+
serializedBody: getValidRequestBody(opts),
|
|
202
|
+
url,
|
|
203
|
+
});
|
|
204
|
+
};
|
|
205
|
+
return {
|
|
206
|
+
buildUrl,
|
|
207
|
+
connect: makeMethodFn("CONNECT"),
|
|
208
|
+
delete: makeMethodFn("DELETE"),
|
|
209
|
+
get: makeMethodFn("GET"),
|
|
210
|
+
getConfig,
|
|
211
|
+
head: makeMethodFn("HEAD"),
|
|
212
|
+
interceptors,
|
|
213
|
+
options: makeMethodFn("OPTIONS"),
|
|
214
|
+
patch: makeMethodFn("PATCH"),
|
|
215
|
+
post: makeMethodFn("POST"),
|
|
216
|
+
put: makeMethodFn("PUT"),
|
|
217
|
+
request,
|
|
218
|
+
setConfig,
|
|
219
|
+
sse: {
|
|
220
|
+
connect: makeSseFn("CONNECT"),
|
|
221
|
+
delete: makeSseFn("DELETE"),
|
|
222
|
+
get: makeSseFn("GET"),
|
|
223
|
+
head: makeSseFn("HEAD"),
|
|
224
|
+
options: makeSseFn("OPTIONS"),
|
|
225
|
+
patch: makeSseFn("PATCH"),
|
|
226
|
+
post: makeSseFn("POST"),
|
|
227
|
+
put: makeSseFn("PUT"),
|
|
228
|
+
trace: makeSseFn("TRACE"),
|
|
229
|
+
},
|
|
230
|
+
trace: makeMethodFn("TRACE"),
|
|
231
|
+
};
|
|
232
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type { Auth } from "../core/auth.gen.js";
|
|
2
|
+
export type { QuerySerializerOptions } from "../core/bodySerializer.gen.js";
|
|
3
|
+
export { formDataBodySerializer, jsonBodySerializer, urlSearchParamsBodySerializer, } from "../core/bodySerializer.gen.js";
|
|
4
|
+
export { buildClientParams } from "../core/params.gen.js";
|
|
5
|
+
export { serializeQueryKeyValue } from "../core/queryKeySerializer.gen.js";
|
|
6
|
+
export { createClient } from "./client.gen.js";
|
|
7
|
+
export type { Client, ClientOptions, Config, CreateClientConfig, Options, RequestOptions, RequestResult, ResolvedRequestOptions, ResponseStyle, TDataShape, } from "./types.gen.js";
|
|
8
|
+
export { createConfig, mergeHeaders } from "./utils.gen.js";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// This file is auto-generated by @hey-api/openapi-ts
|
|
2
|
+
export { formDataBodySerializer, jsonBodySerializer, urlSearchParamsBodySerializer, } from "../core/bodySerializer.gen.js";
|
|
3
|
+
export { buildClientParams } from "../core/params.gen.js";
|
|
4
|
+
export { serializeQueryKeyValue } from "../core/queryKeySerializer.gen.js";
|
|
5
|
+
export { createClient } from "./client.gen.js";
|
|
6
|
+
export { createConfig, mergeHeaders } from "./utils.gen.js";
|