@talqing/sdk 0.1.0
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/LICENSE +21 -0
- package/README.md +193 -0
- package/dist/browser.d.ts +153 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +440 -0
- package/dist/browser.js.map +1 -0
- package/dist/client.d.ts +101 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +88 -0
- package/dist/client.js.map +1 -0
- package/dist/gen/apiVersion.d.ts +3 -0
- package/dist/gen/apiVersion.d.ts.map +1 -0
- package/dist/gen/apiVersion.js +4 -0
- package/dist/gen/apiVersion.js.map +1 -0
- package/dist/gen/client/client.gen.d.ts +3 -0
- package/dist/gen/client/client.gen.d.ts.map +1 -0
- package/dist/gen/client/client.gen.js +217 -0
- package/dist/gen/client/client.gen.js.map +1 -0
- package/dist/gen/client/index.d.ts +11 -0
- package/dist/gen/client/index.d.ts.map +1 -0
- package/dist/gen/client/index.js +7 -0
- package/dist/gen/client/index.js.map +1 -0
- package/dist/gen/client/types.gen.d.ts +121 -0
- package/dist/gen/client/types.gen.d.ts.map +1 -0
- package/dist/gen/client/types.gen.js +3 -0
- package/dist/gen/client/types.gen.js.map +1 -0
- package/dist/gen/client/utils.gen.d.ts +38 -0
- package/dist/gen/client/utils.gen.d.ts.map +1 -0
- package/dist/gen/client/utils.gen.js +231 -0
- package/dist/gen/client/utils.gen.js.map +1 -0
- package/dist/gen/client.gen.d.ts +13 -0
- package/dist/gen/client.gen.d.ts.map +1 -0
- package/dist/gen/client.gen.js +4 -0
- package/dist/gen/client.gen.js.map +1 -0
- package/dist/gen/core/auth.gen.d.ts +26 -0
- package/dist/gen/core/auth.gen.d.ts.map +1 -0
- package/dist/gen/core/auth.gen.js +15 -0
- package/dist/gen/core/auth.gen.js.map +1 -0
- package/dist/gen/core/bodySerializer.gen.d.ts +26 -0
- package/dist/gen/core/bodySerializer.gen.d.ts.map +1 -0
- package/dist/gen/core/bodySerializer.gen.js +58 -0
- package/dist/gen/core/bodySerializer.gen.js.map +1 -0
- package/dist/gen/core/params.gen.d.ts +44 -0
- package/dist/gen/core/params.gen.d.ts.map +1 -0
- package/dist/gen/core/params.gen.js +110 -0
- package/dist/gen/core/params.gen.js.map +1 -0
- package/dist/gen/core/pathSerializer.gen.d.ts +34 -0
- package/dist/gen/core/pathSerializer.gen.d.ts.map +1 -0
- package/dist/gen/core/pathSerializer.gen.js +107 -0
- package/dist/gen/core/pathSerializer.gen.js.map +1 -0
- package/dist/gen/core/queryKeySerializer.gen.d.ts +19 -0
- package/dist/gen/core/queryKeySerializer.gen.d.ts.map +1 -0
- package/dist/gen/core/queryKeySerializer.gen.js +93 -0
- package/dist/gen/core/queryKeySerializer.gen.js.map +1 -0
- package/dist/gen/core/serverSentEvents.gen.d.ts +72 -0
- package/dist/gen/core/serverSentEvents.gen.d.ts.map +1 -0
- package/dist/gen/core/serverSentEvents.gen.js +133 -0
- package/dist/gen/core/serverSentEvents.gen.js.map +1 -0
- package/dist/gen/core/types.gen.d.ts +84 -0
- package/dist/gen/core/types.gen.d.ts.map +1 -0
- package/dist/gen/core/types.gen.js +3 -0
- package/dist/gen/core/types.gen.js.map +1 -0
- package/dist/gen/core/utils.gen.d.ts +20 -0
- package/dist/gen/core/utils.gen.d.ts.map +1 -0
- package/dist/gen/core/utils.gen.js +88 -0
- package/dist/gen/core/utils.gen.js.map +1 -0
- package/dist/gen/index.d.ts +3 -0
- package/dist/gen/index.d.ts.map +1 -0
- package/dist/gen/index.js +3 -0
- package/dist/gen/index.js.map +1 -0
- package/dist/gen/sdk.gen.d.ts +2330 -0
- package/dist/gen/sdk.gen.d.ts.map +1 -0
- package/dist/gen/sdk.gen.js +4320 -0
- package/dist/gen/sdk.gen.js.map +1 -0
- package/dist/gen/types.gen.d.ts +17288 -0
- package/dist/gen/types.gen.d.ts.map +1 -0
- package/dist/gen/types.gen.js +3 -0
- package/dist/gen/types.gen.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/vocabulary.d.ts +67 -0
- package/dist/vocabulary.d.ts.map +1 -0
- package/dist/vocabulary.js +12 -0
- package/dist/vocabulary.js.map +1 -0
- package/package.json +67 -0
- package/src/browser.tsx +675 -0
- package/src/client.ts +189 -0
- package/src/gen/apiVersion.ts +4 -0
- package/src/gen/client/client.gen.ts +277 -0
- package/src/gen/client/index.ts +27 -0
- package/src/gen/client/types.gen.ts +218 -0
- package/src/gen/client/utils.gen.ts +316 -0
- package/src/gen/client.gen.ts +16 -0
- package/src/gen/core/auth.gen.ts +48 -0
- package/src/gen/core/bodySerializer.gen.ts +82 -0
- package/src/gen/core/params.gen.ts +178 -0
- package/src/gen/core/pathSerializer.gen.ts +171 -0
- package/src/gen/core/queryKeySerializer.gen.ts +117 -0
- package/src/gen/core/serverSentEvents.gen.ts +242 -0
- package/src/gen/core/types.gen.ts +110 -0
- package/src/gen/core/utils.gen.ts +140 -0
- package/src/gen/index.ts +4 -0
- package/src/gen/sdk.gen.ts +5008 -0
- package/src/gen/types.gen.ts +18418 -0
- package/src/index.ts +13 -0
- package/src/vocabulary.ts +134 -0
package/src/client.ts
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/** The hand-written half of the SDK: everything the OpenAPI document cannot say.
|
|
2
|
+
*
|
|
3
|
+
* The API surface itself is generated into `./gen` from `openapi/openapi.json`
|
|
4
|
+
* and is never edited here. What is left is the client object that carries
|
|
5
|
+
* credentials, the error type, the two browser-redirect URLs that are
|
|
6
|
+
* deliberately absent from the document, and a pagination helper.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { TalqingApi } from "./gen/index.js";
|
|
10
|
+
import { createClient, createConfig } from "./gen/client/index.js";
|
|
11
|
+
import type { Client } from "./gen/client/index.js";
|
|
12
|
+
import type { ErrorResponse } from "./gen/types.gen.js";
|
|
13
|
+
|
|
14
|
+
/* Free-form JSON: userdata, tool arguments, `vars`, a tool's JSON Schema.
|
|
15
|
+
*
|
|
16
|
+
* `JsonObject` is exactly what the document says such a field is — an object of
|
|
17
|
+
* `unknown` — so a value read off a response drops straight into it. Naming it
|
|
18
|
+
* anything narrower would be the SDK asserting a guarantee the API does not
|
|
19
|
+
* make, and every read would then need a cast to get back to the truth.
|
|
20
|
+
*
|
|
21
|
+
* `JsonValue` is the recursive form, for a payload the CALLER builds: there it
|
|
22
|
+
* is a real constraint, because it refuses a `Date` or a class instance that
|
|
23
|
+
* would not survive `JSON.stringify`. */
|
|
24
|
+
export type JsonPrimitive = string | number | boolean | null;
|
|
25
|
+
export type JsonValue = JsonPrimitive | JsonValue[] | { [key: string]: JsonValue };
|
|
26
|
+
export type JsonObject = { [key: string]: unknown };
|
|
27
|
+
|
|
28
|
+
type Awaitable<T> = T | Promise<T>;
|
|
29
|
+
type TokenProvider = string | (() => Awaitable<string | null | undefined>);
|
|
30
|
+
|
|
31
|
+
/** A page of results. The document spells each instantiation out by hand
|
|
32
|
+
* (`PageAgentResponse`) because OpenAPI has no generics; TypeScript is
|
|
33
|
+
* structural, so those satisfy this and generic helpers still work. */
|
|
34
|
+
export interface Page<T> {
|
|
35
|
+
items: T[];
|
|
36
|
+
has_more: boolean;
|
|
37
|
+
limit: number;
|
|
38
|
+
offset: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Every generated method, with the `| undefined` that `throwOnError` makes
|
|
43
|
+
* impossible removed.
|
|
44
|
+
*
|
|
45
|
+
* The generator emits `<ThrowOnError extends boolean = false>` on every method,
|
|
46
|
+
* so a client configured to throw still returns `T | undefined` at the type
|
|
47
|
+
* level and every call site would need a `!`. There is no setting for it. This
|
|
48
|
+
* re-types the whole tree once instead.
|
|
49
|
+
*
|
|
50
|
+
* `Exclude<R, undefined>` rather than `NonNullable<R>`: the latter turns the
|
|
51
|
+
* `void` of a 204 into `{}`. `...args: infer A` rather than named parameters:
|
|
52
|
+
* it keeps each method's optional-argument arity, so `agents.list()` with no
|
|
53
|
+
* argument still compiles.
|
|
54
|
+
*/
|
|
55
|
+
type Throwing<T> = {
|
|
56
|
+
[K in keyof T]: T[K] extends (...args: infer A) => Promise<infer R>
|
|
57
|
+
? (...args: A) => Promise<Exclude<R, undefined>>
|
|
58
|
+
: Throwing<T[K]>;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** Every error this API returns, in the one shape it returns them in. */
|
|
62
|
+
export class TalqingApiError extends Error {
|
|
63
|
+
readonly name = "TalqingApiError";
|
|
64
|
+
readonly status: number;
|
|
65
|
+
/** Per-field problems, when the failure had more than one. Always present. */
|
|
66
|
+
readonly errors: string[];
|
|
67
|
+
readonly response: Response;
|
|
68
|
+
|
|
69
|
+
constructor(body: ErrorResponse, response: Response) {
|
|
70
|
+
super(body.detail.message);
|
|
71
|
+
this.status = response.status;
|
|
72
|
+
this.errors = body.detail.errors;
|
|
73
|
+
this.response = response;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface TalqingClientOptions {
|
|
78
|
+
/** Base URL of the Talqing API, e.g. `https://api.example.com`. Required,
|
|
79
|
+
* with no default: a bundler inlines this at build time, so a localhost
|
|
80
|
+
* fallback would silently ship a production bundle calling nothing. */
|
|
81
|
+
baseUrl: string;
|
|
82
|
+
/** A personal access token, or a function returning one. Server-side use. */
|
|
83
|
+
token?: TokenProvider;
|
|
84
|
+
/** `"include"` for the dashboard's cookie session. */
|
|
85
|
+
credentials?: RequestCredentials;
|
|
86
|
+
headers?: Record<string, string>;
|
|
87
|
+
fetch?: typeof fetch;
|
|
88
|
+
/**
|
|
89
|
+
* Called when the API answers 401. The credentials are gone, so every other
|
|
90
|
+
* call will fail identically — this is a session-level fact, not a per-call
|
|
91
|
+
* one, and handling it here keeps every caller from re-deriving it. The error
|
|
92
|
+
* is still thrown afterwards.
|
|
93
|
+
*/
|
|
94
|
+
onUnauthorized?: (error: TalqingApiError) => void;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
interface TalqingClientExtras {
|
|
98
|
+
readonly baseUrl: string;
|
|
99
|
+
/** The underlying transport, for interceptors and one-off requests. */
|
|
100
|
+
readonly http: Client;
|
|
101
|
+
/** Where to send the browser to start Google sign-in. */
|
|
102
|
+
googleLoginUrl(): string;
|
|
103
|
+
/** Where to send the browser to authorize an integration. */
|
|
104
|
+
oauthStartUrl(provider: string, integrationId?: string): string;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface TalqingClient extends Throwing<TalqingApi>, TalqingClientExtras {}
|
|
108
|
+
|
|
109
|
+
class TalqingClientImpl extends TalqingApi implements TalqingClientExtras {
|
|
110
|
+
readonly baseUrl: string;
|
|
111
|
+
readonly http: Client;
|
|
112
|
+
|
|
113
|
+
constructor(options: TalqingClientOptions) {
|
|
114
|
+
// Types do not reach a JavaScript caller, or a value that is `string` but empty.
|
|
115
|
+
if (!options?.baseUrl?.trim()) {
|
|
116
|
+
throw new Error("TalqingClient requires a baseUrl (e.g. https://api.in.talqing.com)");
|
|
117
|
+
}
|
|
118
|
+
const baseUrl = options.baseUrl.replace(/\/+$/, "");
|
|
119
|
+
const http = createClient(
|
|
120
|
+
createConfig({
|
|
121
|
+
baseUrl,
|
|
122
|
+
throwOnError: true,
|
|
123
|
+
responseStyle: "data",
|
|
124
|
+
credentials: options.credentials,
|
|
125
|
+
headers: options.headers,
|
|
126
|
+
...(options.fetch ? { fetch: options.fetch } : {}),
|
|
127
|
+
...(options.token ? { auth: () => resolveToken(options.token) } : {}),
|
|
128
|
+
}),
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
// The transport throws the parsed error body — a plain object with no
|
|
132
|
+
// status and no stack. Replacing it here is what makes `catch (e)` see a
|
|
133
|
+
// real Error everywhere, including inside the generated methods.
|
|
134
|
+
http.interceptors.error.use((error, response) => {
|
|
135
|
+
// No response means the request never completed — a DNS failure, an
|
|
136
|
+
// abort, an offline tab. There is no status to carry, so the transport's
|
|
137
|
+
// own error is already the truthful one.
|
|
138
|
+
if (!response) return error;
|
|
139
|
+
const failure = new TalqingApiError(error as ErrorResponse, response);
|
|
140
|
+
if (failure.status === 401) options.onUnauthorized?.(failure);
|
|
141
|
+
return failure;
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
super({ client: http });
|
|
145
|
+
this.baseUrl = baseUrl;
|
|
146
|
+
this.http = http;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
googleLoginUrl(): string {
|
|
150
|
+
return `${this.baseUrl}/v1/auth/google/start`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
oauthStartUrl(provider: string, integrationId?: string): string {
|
|
154
|
+
const url = new URL(`${this.baseUrl}/v1/integrations/oauth/${encodeURIComponent(provider)}/start`);
|
|
155
|
+
if (integrationId) url.searchParams.set("integration_id", integrationId);
|
|
156
|
+
return url.toString();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/* `Throwing` is a type-level narrowing of the same object — the runtime value
|
|
161
|
+
* is the generated class, unchanged — so the constructor is re-typed rather
|
|
162
|
+
* than the 155 methods being re-declared. */
|
|
163
|
+
export const TalqingClient = TalqingClientImpl as unknown as new (
|
|
164
|
+
options: TalqingClientOptions,
|
|
165
|
+
) => TalqingClient;
|
|
166
|
+
|
|
167
|
+
async function resolveToken(token: TokenProvider | undefined): Promise<string | undefined> {
|
|
168
|
+
const value = typeof token === "function" ? await token() : token;
|
|
169
|
+
return value ?? undefined;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Walk every page of a list endpoint.
|
|
174
|
+
*
|
|
175
|
+
* for await (const agent of paginate((q) => talqing.agents.list(q))) { … }
|
|
176
|
+
*
|
|
177
|
+
* Replaces the one-off `listAllAgents` the previous client carried: the page
|
|
178
|
+
* shape is the same on all 28 list endpoints, so the helper is too.
|
|
179
|
+
*/
|
|
180
|
+
export async function* paginate<T>(
|
|
181
|
+
page: (query: { limit: number; offset: number }) => Promise<Page<T>>,
|
|
182
|
+
{ limit = 200 }: { limit?: number } = {},
|
|
183
|
+
): AsyncGenerator<T> {
|
|
184
|
+
for (let offset = 0; ; offset += limit) {
|
|
185
|
+
const result = await page({ limit, offset });
|
|
186
|
+
yield* result.items;
|
|
187
|
+
if (!result.has_more) return;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
// This file is auto-generated by @hey-api/openapi-ts
|
|
2
|
+
|
|
3
|
+
import { createSseClient } from '../core/serverSentEvents.gen.js';
|
|
4
|
+
import type { HttpMethod } from '../core/types.gen.js';
|
|
5
|
+
import { getValidRequestBody } from '../core/utils.gen.js';
|
|
6
|
+
import type { Client, Config, RequestOptions, ResolvedRequestOptions } from './types.gen.js';
|
|
7
|
+
import {
|
|
8
|
+
buildUrl,
|
|
9
|
+
createConfig,
|
|
10
|
+
createInterceptors,
|
|
11
|
+
getParseAs,
|
|
12
|
+
mergeConfigs,
|
|
13
|
+
mergeHeaders,
|
|
14
|
+
setAuthParams,
|
|
15
|
+
} from './utils.gen.js';
|
|
16
|
+
|
|
17
|
+
type ReqInit = Omit<RequestInit, 'body' | 'headers'> & {
|
|
18
|
+
body?: any;
|
|
19
|
+
headers: ReturnType<typeof mergeHeaders>;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const createClient = (config: Config = {}): Client => {
|
|
23
|
+
let _config = mergeConfigs(createConfig(), config);
|
|
24
|
+
|
|
25
|
+
const getConfig = (): Config => ({ ..._config });
|
|
26
|
+
|
|
27
|
+
const setConfig = (config: Config): Config => {
|
|
28
|
+
_config = mergeConfigs(_config, config);
|
|
29
|
+
return getConfig();
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const interceptors = createInterceptors<Request, Response, unknown, ResolvedRequestOptions>();
|
|
33
|
+
|
|
34
|
+
const beforeRequest = async <
|
|
35
|
+
TData = unknown,
|
|
36
|
+
TResponseStyle extends 'data' | 'fields' = 'fields',
|
|
37
|
+
ThrowOnError extends boolean = boolean,
|
|
38
|
+
Url extends string = string,
|
|
39
|
+
>(
|
|
40
|
+
options: RequestOptions<TData, TResponseStyle, ThrowOnError, Url>,
|
|
41
|
+
) => {
|
|
42
|
+
const opts = {
|
|
43
|
+
..._config,
|
|
44
|
+
...options,
|
|
45
|
+
fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,
|
|
46
|
+
headers: mergeHeaders(_config.headers, options.headers),
|
|
47
|
+
serializedBody: undefined as string | undefined,
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
if (opts.security) {
|
|
51
|
+
await setAuthParams(opts);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (opts.requestValidator) {
|
|
55
|
+
await opts.requestValidator(opts);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (opts.body !== undefined && opts.bodySerializer) {
|
|
59
|
+
opts.serializedBody = opts.bodySerializer(opts.body) as string | undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// remove Content-Type header if body is empty to avoid sending invalid requests
|
|
63
|
+
if (opts.body === undefined || opts.serializedBody === '') {
|
|
64
|
+
opts.headers.delete('Content-Type');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const resolvedOpts = opts as typeof opts &
|
|
68
|
+
ResolvedRequestOptions<TResponseStyle, ThrowOnError, Url>;
|
|
69
|
+
const url = buildUrl(resolvedOpts);
|
|
70
|
+
|
|
71
|
+
return { opts: resolvedOpts, url };
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const request: Client['request'] = async (options) => {
|
|
75
|
+
const throwOnError = options.throwOnError ?? _config.throwOnError;
|
|
76
|
+
const responseStyle = options.responseStyle ?? _config.responseStyle;
|
|
77
|
+
|
|
78
|
+
let request: Request | undefined;
|
|
79
|
+
let response: Response | undefined;
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
const { opts, url } = await beforeRequest(options);
|
|
83
|
+
const requestInit: ReqInit = {
|
|
84
|
+
redirect: 'follow',
|
|
85
|
+
...opts,
|
|
86
|
+
body: getValidRequestBody(opts),
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
request = new Request(url, requestInit);
|
|
90
|
+
|
|
91
|
+
for (const fn of interceptors.request.fns) {
|
|
92
|
+
if (fn) {
|
|
93
|
+
request = await fn(request, opts);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// fetch must be assigned here, otherwise it would throw the error:
|
|
98
|
+
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
|
|
99
|
+
const _fetch = opts.fetch!;
|
|
100
|
+
|
|
101
|
+
response = await _fetch(request);
|
|
102
|
+
|
|
103
|
+
for (const fn of interceptors.response.fns) {
|
|
104
|
+
if (fn) {
|
|
105
|
+
response = await fn(response, request, opts);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const result = {
|
|
110
|
+
request,
|
|
111
|
+
response,
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
if (response.ok) {
|
|
115
|
+
const parseAs =
|
|
116
|
+
(opts.parseAs === 'auto'
|
|
117
|
+
? getParseAs(response.headers.get('Content-Type'))
|
|
118
|
+
: opts.parseAs) ?? 'json';
|
|
119
|
+
|
|
120
|
+
if (response.status === 204 || response.headers.get('Content-Length') === '0') {
|
|
121
|
+
let emptyData: any;
|
|
122
|
+
switch (parseAs) {
|
|
123
|
+
case 'arrayBuffer':
|
|
124
|
+
case 'blob':
|
|
125
|
+
case 'text':
|
|
126
|
+
emptyData = await response[parseAs]();
|
|
127
|
+
break;
|
|
128
|
+
case 'formData':
|
|
129
|
+
emptyData = new FormData();
|
|
130
|
+
break;
|
|
131
|
+
case 'stream':
|
|
132
|
+
emptyData = response.body;
|
|
133
|
+
break;
|
|
134
|
+
case 'json':
|
|
135
|
+
default:
|
|
136
|
+
emptyData = {};
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
return opts.responseStyle === 'data'
|
|
140
|
+
? emptyData
|
|
141
|
+
: {
|
|
142
|
+
data: emptyData,
|
|
143
|
+
...result,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
let data: any;
|
|
148
|
+
switch (parseAs) {
|
|
149
|
+
case 'arrayBuffer':
|
|
150
|
+
case 'blob':
|
|
151
|
+
case 'formData':
|
|
152
|
+
case 'text':
|
|
153
|
+
data = await response[parseAs]();
|
|
154
|
+
break;
|
|
155
|
+
case 'json': {
|
|
156
|
+
// Some servers return 200 with no Content-Length and empty body.
|
|
157
|
+
// response.json() would throw; read as text and parse if non-empty.
|
|
158
|
+
const text = await response.text();
|
|
159
|
+
data = text ? JSON.parse(text) : {};
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
case 'stream':
|
|
163
|
+
return opts.responseStyle === 'data'
|
|
164
|
+
? response.body
|
|
165
|
+
: {
|
|
166
|
+
data: response.body,
|
|
167
|
+
...result,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (parseAs === 'json') {
|
|
172
|
+
if (opts.responseValidator) {
|
|
173
|
+
await opts.responseValidator(data);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (opts.responseTransformer) {
|
|
177
|
+
data = await opts.responseTransformer(data);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return opts.responseStyle === 'data'
|
|
182
|
+
? data
|
|
183
|
+
: {
|
|
184
|
+
data,
|
|
185
|
+
...result,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const textError = await response.text();
|
|
190
|
+
let jsonError: unknown;
|
|
191
|
+
|
|
192
|
+
try {
|
|
193
|
+
jsonError = JSON.parse(textError);
|
|
194
|
+
} catch {
|
|
195
|
+
// noop
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
throw jsonError ?? textError;
|
|
199
|
+
} catch (error) {
|
|
200
|
+
let finalError = error;
|
|
201
|
+
|
|
202
|
+
for (const fn of interceptors.error.fns) {
|
|
203
|
+
if (fn) {
|
|
204
|
+
finalError = await fn(finalError, response, request, options as ResolvedRequestOptions);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
finalError = finalError || {};
|
|
209
|
+
|
|
210
|
+
if (throwOnError) {
|
|
211
|
+
throw finalError;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// TODO: we probably want to return error and improve types
|
|
215
|
+
return responseStyle === 'data'
|
|
216
|
+
? undefined
|
|
217
|
+
: {
|
|
218
|
+
error: finalError,
|
|
219
|
+
request,
|
|
220
|
+
response,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const makeMethodFn = (method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
|
|
226
|
+
request({ ...options, method });
|
|
227
|
+
|
|
228
|
+
const makeSseFn = (method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
|
|
229
|
+
const { opts, url } = await beforeRequest(options);
|
|
230
|
+
return createSseClient({
|
|
231
|
+
...opts,
|
|
232
|
+
body: opts.body as BodyInit | null | undefined,
|
|
233
|
+
method,
|
|
234
|
+
onRequest: async (url, init) => {
|
|
235
|
+
let request = new Request(url, init);
|
|
236
|
+
for (const fn of interceptors.request.fns) {
|
|
237
|
+
if (fn) {
|
|
238
|
+
request = await fn(request, opts);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return request;
|
|
242
|
+
},
|
|
243
|
+
serializedBody: getValidRequestBody(opts) as BodyInit | null | undefined,
|
|
244
|
+
url,
|
|
245
|
+
});
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const _buildUrl: Client['buildUrl'] = (options) => buildUrl({ ..._config, ...options });
|
|
249
|
+
|
|
250
|
+
return {
|
|
251
|
+
buildUrl: _buildUrl,
|
|
252
|
+
connect: makeMethodFn('CONNECT'),
|
|
253
|
+
delete: makeMethodFn('DELETE'),
|
|
254
|
+
get: makeMethodFn('GET'),
|
|
255
|
+
getConfig,
|
|
256
|
+
head: makeMethodFn('HEAD'),
|
|
257
|
+
interceptors,
|
|
258
|
+
options: makeMethodFn('OPTIONS'),
|
|
259
|
+
patch: makeMethodFn('PATCH'),
|
|
260
|
+
post: makeMethodFn('POST'),
|
|
261
|
+
put: makeMethodFn('PUT'),
|
|
262
|
+
request,
|
|
263
|
+
setConfig,
|
|
264
|
+
sse: {
|
|
265
|
+
connect: makeSseFn('CONNECT'),
|
|
266
|
+
delete: makeSseFn('DELETE'),
|
|
267
|
+
get: makeSseFn('GET'),
|
|
268
|
+
head: makeSseFn('HEAD'),
|
|
269
|
+
options: makeSseFn('OPTIONS'),
|
|
270
|
+
patch: makeSseFn('PATCH'),
|
|
271
|
+
post: makeSseFn('POST'),
|
|
272
|
+
put: makeSseFn('PUT'),
|
|
273
|
+
trace: makeSseFn('TRACE'),
|
|
274
|
+
},
|
|
275
|
+
trace: makeMethodFn('TRACE'),
|
|
276
|
+
} as Client;
|
|
277
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// This file is auto-generated by @hey-api/openapi-ts
|
|
2
|
+
|
|
3
|
+
export type { Auth } from '../core/auth.gen.js';
|
|
4
|
+
export type { QuerySerializerOptions } from '../core/bodySerializer.gen.js';
|
|
5
|
+
export {
|
|
6
|
+
formDataBodySerializer,
|
|
7
|
+
jsonBodySerializer,
|
|
8
|
+
urlSearchParamsBodySerializer,
|
|
9
|
+
} from '../core/bodySerializer.gen.js';
|
|
10
|
+
export { buildClientParams } from '../core/params.gen.js';
|
|
11
|
+
export { serializeQueryKeyValue } from '../core/queryKeySerializer.gen.js';
|
|
12
|
+
export type { ServerSentEventsResult } from '../core/serverSentEvents.gen.js';
|
|
13
|
+
export type { ClientMeta } from '../core/types.gen.js';
|
|
14
|
+
export { createClient } from './client.gen.js';
|
|
15
|
+
export type {
|
|
16
|
+
Client,
|
|
17
|
+
ClientOptions,
|
|
18
|
+
Config,
|
|
19
|
+
CreateClientConfig,
|
|
20
|
+
Options,
|
|
21
|
+
RequestOptions,
|
|
22
|
+
RequestResult,
|
|
23
|
+
ResolvedRequestOptions,
|
|
24
|
+
ResponseStyle,
|
|
25
|
+
TDataShape,
|
|
26
|
+
} from './types.gen.js';
|
|
27
|
+
export { createConfig, mergeHeaders } from './utils.gen.js';
|