@blankline/dropstone-sdk 1.0.8
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/README.md +146 -0
- package/dist/api.d.ts +118 -0
- package/dist/api.js +125 -0
- package/dist/client.d.ts +7 -0
- package/dist/client.js +51 -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 +3380 -0
- package/dist/gen/types.gen.js +2 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +17 -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 +96 -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 +1549 -0
- package/dist/v2/gen/sdk.gen.js +3146 -0
- package/dist/v2/gen/types.gen.d.ts +5749 -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 +86 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from "./client.js";
|
|
2
|
+
export * from "./server.js";
|
|
3
|
+
export * from "./api.js";
|
|
4
|
+
import type { ServerOptions } from "./server.js";
|
|
5
|
+
export declare function createDropstone(options?: ServerOptions): Promise<{
|
|
6
|
+
client: import("./client.js").DropstoneClient;
|
|
7
|
+
server: {
|
|
8
|
+
url: string;
|
|
9
|
+
close(): void;
|
|
10
|
+
};
|
|
11
|
+
}>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export * from "./client.js";
|
|
2
|
+
export * from "./server.js";
|
|
3
|
+
export * from "./api.js";
|
|
4
|
+
import { createDropstoneClient } from "./client.js";
|
|
5
|
+
import { createDropstoneServer } from "./server.js";
|
|
6
|
+
export async function createDropstone(options) {
|
|
7
|
+
const server = await createDropstoneServer({
|
|
8
|
+
...options,
|
|
9
|
+
});
|
|
10
|
+
const client = createDropstoneClient({
|
|
11
|
+
baseUrl: server.url,
|
|
12
|
+
});
|
|
13
|
+
return {
|
|
14
|
+
client,
|
|
15
|
+
server,
|
|
16
|
+
};
|
|
17
|
+
}
|
package/dist/process.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
// Duplicated from `packages/dropstone/src/util/process.ts` because the SDK cannot
|
|
3
|
+
// import `dropstone` without creating a cycle (`dropstone` depends on `@blankline/dropstone-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 createDropstoneServer(options?: ServerOptions): Promise<{
|
|
18
|
+
url: string;
|
|
19
|
+
close(): void;
|
|
20
|
+
}>;
|
|
21
|
+
export declare function createDropstoneTui(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 createDropstoneServer(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(`dropstone`, args, {
|
|
13
|
+
env: {
|
|
14
|
+
...process.env,
|
|
15
|
+
DROPSTONE_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("dropstone 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 createDropstoneTui(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(`dropstone`, args, {
|
|
92
|
+
stdio: "inherit",
|
|
93
|
+
env: {
|
|
94
|
+
...process.env,
|
|
95
|
+
DROPSTONE_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 { DropstoneClient } from "./gen/sdk.gen.js";
|
|
4
|
+
export { type Config as DropstoneClientConfig, DropstoneClient };
|
|
5
|
+
export declare function createDropstoneClient(config?: Config & {
|
|
6
|
+
directory?: string;
|
|
7
|
+
experimental_workspaceID?: string;
|
|
8
|
+
}): DropstoneClient;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
export * from "./gen/types.gen.js";
|
|
2
|
+
import { createClient } from "./gen/client/client.gen.js";
|
|
3
|
+
import { DropstoneClient } from "./gen/sdk.gen.js";
|
|
4
|
+
export { DropstoneClient };
|
|
5
|
+
function pick(value, fallback, encode) {
|
|
6
|
+
if (!value)
|
|
7
|
+
return;
|
|
8
|
+
if (!fallback)
|
|
9
|
+
return value;
|
|
10
|
+
if (value === fallback)
|
|
11
|
+
return fallback;
|
|
12
|
+
if (encode && value === encode(fallback))
|
|
13
|
+
return fallback;
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
function rewrite(request, values) {
|
|
17
|
+
if (request.method !== "GET" && request.method !== "HEAD")
|
|
18
|
+
return request;
|
|
19
|
+
const url = new URL(request.url);
|
|
20
|
+
let changed = false;
|
|
21
|
+
for (const [name, key] of [
|
|
22
|
+
["x-dropstone-directory", "directory"],
|
|
23
|
+
["x-dropstone-workspace", "workspace"],
|
|
24
|
+
]) {
|
|
25
|
+
const value = pick(request.headers.get(name), key === "directory" ? values.directory : values.workspace, key === "directory" ? encodeURIComponent : undefined);
|
|
26
|
+
if (!value)
|
|
27
|
+
continue;
|
|
28
|
+
if (!url.searchParams.has(key)) {
|
|
29
|
+
url.searchParams.set(key, value);
|
|
30
|
+
}
|
|
31
|
+
changed = true;
|
|
32
|
+
}
|
|
33
|
+
if (!changed)
|
|
34
|
+
return request;
|
|
35
|
+
const next = new Request(url, request);
|
|
36
|
+
next.headers.delete("x-dropstone-directory");
|
|
37
|
+
next.headers.delete("x-dropstone-workspace");
|
|
38
|
+
return next;
|
|
39
|
+
}
|
|
40
|
+
export function createDropstoneClient(config) {
|
|
41
|
+
if (!config?.fetch) {
|
|
42
|
+
const customFetch = (req) => {
|
|
43
|
+
// @ts-ignore
|
|
44
|
+
req.timeout = false;
|
|
45
|
+
return fetch(req);
|
|
46
|
+
};
|
|
47
|
+
config = {
|
|
48
|
+
...config,
|
|
49
|
+
fetch: customFetch,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
if (config?.directory) {
|
|
53
|
+
config.headers = {
|
|
54
|
+
...config.headers,
|
|
55
|
+
"x-dropstone-directory": encodeURIComponent(config.directory),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
if (config?.experimental_workspaceID) {
|
|
59
|
+
config.headers = {
|
|
60
|
+
...config.headers,
|
|
61
|
+
"x-dropstone-workspace": config.experimental_workspaceID,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
const client = createClient(config);
|
|
65
|
+
client.interceptors.request.use((request) => rewrite(request, {
|
|
66
|
+
directory: config?.directory,
|
|
67
|
+
workspace: config?.experimental_workspaceID,
|
|
68
|
+
}));
|
|
69
|
+
client.interceptors.response.use((response) => {
|
|
70
|
+
const contentType = response.headers.get("content-type");
|
|
71
|
+
if (contentType === "text/html")
|
|
72
|
+
throw new Error("Request is not supported by this version of Dropstone Server (Server responded with text/html)");
|
|
73
|
+
return response;
|
|
74
|
+
});
|
|
75
|
+
// The generated client falls back to throwing a literal `{}` when the server
|
|
76
|
+
// responds with an empty / unparseable error body, which surfaces as a bare
|
|
77
|
+
// `{}` in TUI / CLI error output. Wrap ONLY that case in a real Error so
|
|
78
|
+
// downstream formatters get a useful message — but pass through any parsed
|
|
79
|
+
// JSON error body unchanged so existing consumers can still inspect fields.
|
|
80
|
+
client.interceptors.error.use((error, response, request) => {
|
|
81
|
+
const isEmpty = error === undefined ||
|
|
82
|
+
error === null ||
|
|
83
|
+
error === "" ||
|
|
84
|
+
(typeof error === "object" && !(error instanceof Error) && Object.keys(error).length === 0);
|
|
85
|
+
if (!isEmpty)
|
|
86
|
+
return error;
|
|
87
|
+
const method = request?.method ?? "?";
|
|
88
|
+
const url = request?.url ?? "?";
|
|
89
|
+
if (!response)
|
|
90
|
+
return new Error(`dropstone server ${method} ${url}: network error (no response)`);
|
|
91
|
+
const status = response.status;
|
|
92
|
+
const statusText = response.statusText ? " " + response.statusText : "";
|
|
93
|
+
return new Error(`dropstone server ${method} ${url} → ${status}${statusText}: (empty response body)`);
|
|
94
|
+
});
|
|
95
|
+
return new DropstoneClient({ client });
|
|
96
|
+
}
|
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";
|