@opencode-ai/sdk 0.5.28 → 0.6.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.
Files changed (31) hide show
  1. package/dist/client.d.ts +1 -1
  2. package/dist/client.js +1 -1
  3. package/dist/gen/client/{client.d.ts → client.gen.d.ts} +1 -1
  4. package/dist/gen/client/{client.js → client.gen.js} +36 -13
  5. package/dist/gen/client/index.d.ts +7 -7
  6. package/dist/gen/client/index.js +5 -4
  7. package/dist/gen/client/{types.d.ts → types.gen.d.ts} +19 -11
  8. package/dist/gen/client/types.gen.js +2 -0
  9. package/dist/gen/client/{utils.d.ts → utils.gen.d.ts} +2 -9
  10. package/dist/gen/client/{utils.js → utils.gen.js} +26 -83
  11. package/dist/gen/client.gen.d.ts +1 -1
  12. package/dist/gen/core/{auth.js → auth.gen.js} +1 -0
  13. package/dist/gen/core/{bodySerializer.d.ts → bodySerializer.gen.d.ts} +1 -1
  14. package/dist/gen/core/{bodySerializer.js → bodySerializer.gen.js} +4 -0
  15. package/dist/gen/core/{params.js → params.gen.js} +1 -0
  16. package/dist/gen/core/{pathSerializer.js → pathSerializer.gen.js} +1 -0
  17. package/dist/gen/core/serverSentEvents.gen.d.ts +59 -0
  18. package/dist/gen/core/serverSentEvents.gen.js +117 -0
  19. package/dist/gen/core/{types.d.ts → types.gen.d.ts} +2 -2
  20. package/dist/gen/core/types.gen.js +2 -0
  21. package/dist/gen/core/utils.gen.d.ts +14 -0
  22. package/dist/gen/core/utils.gen.js +69 -0
  23. package/dist/gen/sdk.gen.d.ts +70 -56
  24. package/dist/gen/sdk.gen.js +62 -38
  25. package/dist/gen/types.gen.d.ts +204 -104
  26. package/package.json +2 -2
  27. package/dist/gen/client/types.js +0 -1
  28. package/dist/gen/core/types.js +0 -1
  29. /package/dist/gen/core/{auth.d.ts → auth.gen.d.ts} +0 -0
  30. /package/dist/gen/core/{params.d.ts → params.gen.d.ts} +0 -0
  31. /package/dist/gen/core/{pathSerializer.d.ts → pathSerializer.gen.d.ts} +0 -0
package/dist/client.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from "./gen/types.gen.js";
2
2
  export { type Config as OpencodeClientConfig, OpencodeClient };
3
- import { type Config } from "./gen/client/types.js";
3
+ import { type Config } from "./gen/client/types.gen.js";
4
4
  import { OpencodeClient } from "./gen/sdk.gen.js";
5
5
  export declare function createOpencodeClient(config?: Config): OpencodeClient;
package/dist/client.js CHANGED
@@ -1,6 +1,6 @@
1
1
  export * from "./gen/types.gen.js";
2
2
  export { OpencodeClient };
3
- import { createClient } from "./gen/client/client.js";
3
+ import { createClient } from "./gen/client/client.gen.js";
4
4
  import { OpencodeClient } from "./gen/sdk.gen.js";
5
5
  export function createOpencodeClient(config) {
6
6
  const client = createClient(config);
@@ -1,2 +1,2 @@
1
- import type { Client, Config } from "./types.js";
1
+ import type { Client, Config } from "./types.gen.js";
2
2
  export declare const createClient: (config?: Config) => Client;
@@ -1,4 +1,6 @@
1
- import { buildUrl, createConfig, createInterceptors, getParseAs, mergeConfigs, mergeHeaders, setAuthParams, } from "./utils.js";
1
+ // This file is auto-generated by @hey-api/openapi-ts
2
+ import { createSseClient } from "../core/serverSentEvents.gen.js";
3
+ import { buildUrl, createConfig, createInterceptors, getParseAs, mergeConfigs, mergeHeaders, setAuthParams, } from "./utils.gen.js";
2
4
  export const createClient = (config = {}) => {
3
5
  let _config = mergeConfigs(createConfig(), config);
4
6
  const getConfig = () => ({ ..._config });
@@ -7,12 +9,13 @@ export const createClient = (config = {}) => {
7
9
  return getConfig();
8
10
  };
9
11
  const interceptors = createInterceptors();
10
- const request = async (options) => {
12
+ const beforeRequest = async (options) => {
11
13
  const opts = {
12
14
  ..._config,
13
15
  ...options,
14
16
  fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,
15
17
  headers: mergeHeaders(_config.headers, options.headers),
18
+ serializedBody: undefined,
16
19
  };
17
20
  if (opts.security) {
18
21
  await setAuthParams({
@@ -24,16 +27,22 @@ export const createClient = (config = {}) => {
24
27
  await opts.requestValidator(opts);
25
28
  }
26
29
  if (opts.body && opts.bodySerializer) {
27
- opts.body = opts.bodySerializer(opts.body);
30
+ opts.serializedBody = opts.bodySerializer(opts.body);
28
31
  }
29
32
  // remove Content-Type header if body is empty to avoid sending invalid requests
30
- if (opts.body === undefined || opts.body === "") {
33
+ if (opts.serializedBody === undefined || opts.serializedBody === "") {
31
34
  opts.headers.delete("Content-Type");
32
35
  }
33
36
  const url = buildUrl(opts);
37
+ return { opts, url };
38
+ };
39
+ const request = async (options) => {
40
+ // @ts-expect-error
41
+ const { opts, url } = await beforeRequest(options);
34
42
  const requestInit = {
35
43
  redirect: "follow",
36
44
  ...opts,
45
+ body: opts.serializedBody,
37
46
  };
38
47
  let request = new Request(url, requestInit);
39
48
  for (const fn of interceptors.request._fns) {
@@ -123,20 +132,34 @@ export const createClient = (config = {}) => {
123
132
  ...result,
124
133
  };
125
134
  };
135
+ const makeMethod = (method) => {
136
+ const fn = (options) => request({ ...options, method });
137
+ fn.sse = async (options) => {
138
+ const { opts, url } = await beforeRequest(options);
139
+ return createSseClient({
140
+ ...opts,
141
+ body: opts.body,
142
+ headers: opts.headers,
143
+ method,
144
+ url,
145
+ });
146
+ };
147
+ return fn;
148
+ };
126
149
  return {
127
150
  buildUrl,
128
- connect: (options) => request({ ...options, method: "CONNECT" }),
129
- delete: (options) => request({ ...options, method: "DELETE" }),
130
- get: (options) => request({ ...options, method: "GET" }),
151
+ connect: makeMethod("CONNECT"),
152
+ delete: makeMethod("DELETE"),
153
+ get: makeMethod("GET"),
131
154
  getConfig,
132
- head: (options) => request({ ...options, method: "HEAD" }),
155
+ head: makeMethod("HEAD"),
133
156
  interceptors,
134
- options: (options) => request({ ...options, method: "OPTIONS" }),
135
- patch: (options) => request({ ...options, method: "PATCH" }),
136
- post: (options) => request({ ...options, method: "POST" }),
137
- put: (options) => request({ ...options, method: "PUT" }),
157
+ options: makeMethod("OPTIONS"),
158
+ patch: makeMethod("PATCH"),
159
+ post: makeMethod("POST"),
160
+ put: makeMethod("PUT"),
138
161
  request,
139
162
  setConfig,
140
- trace: (options) => request({ ...options, method: "TRACE" }),
163
+ trace: makeMethod("TRACE"),
141
164
  };
142
165
  };
@@ -1,7 +1,7 @@
1
- export type { Auth } from "../core/auth.js";
2
- export type { QuerySerializerOptions } from "../core/bodySerializer.js";
3
- export { formDataBodySerializer, jsonBodySerializer, urlSearchParamsBodySerializer } from "../core/bodySerializer.js";
4
- export { buildClientParams } from "../core/params.js";
5
- export { createClient } from "./client.js";
6
- export type { Client, ClientOptions, Config, CreateClientConfig, Options, OptionsLegacyParser, RequestOptions, RequestResult, ResponseStyle, TDataShape, } from "./types.js";
7
- export { createConfig, mergeHeaders } from "./utils.js";
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 { createClient } from "./client.gen.js";
6
+ export type { Client, ClientOptions, Config, CreateClientConfig, Options, OptionsLegacyParser, RequestOptions, RequestResult, ResolvedRequestOptions, ResponseStyle, TDataShape, } from "./types.gen.js";
7
+ export { createConfig, mergeHeaders } from "./utils.gen.js";
@@ -1,4 +1,5 @@
1
- export { formDataBodySerializer, jsonBodySerializer, urlSearchParamsBodySerializer } from "../core/bodySerializer.js";
2
- export { buildClientParams } from "../core/params.js";
3
- export { createClient } from "./client.js";
4
- export { createConfig, mergeHeaders } from "./utils.js";
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 { createClient } from "./client.gen.js";
5
+ export { createConfig, mergeHeaders } from "./utils.gen.js";
@@ -1,6 +1,7 @@
1
- import type { Auth } from "../core/auth.js";
2
- import type { Client as CoreClient, Config as CoreConfig } from "../core/types.js";
3
- import type { Middleware } from "./utils.js";
1
+ import type { Auth } from "../core/auth.gen.js";
2
+ import type { ServerSentEventsOptions, ServerSentEventsResult } from "../core/serverSentEvents.gen.js";
3
+ import type { Client as CoreClient, Config as CoreConfig } from "../core/types.gen.js";
4
+ import type { Middleware } from "./utils.gen.js";
4
5
  export type ResponseStyle = "data" | "fields";
5
6
  export interface Config<T extends ClientOptions = ClientOptions> extends Omit<RequestInit, "body" | "headers" | "method">, CoreConfig {
6
7
  /**
@@ -43,10 +44,10 @@ export interface Config<T extends ClientOptions = ClientOptions> extends Omit<Re
43
44
  */
44
45
  throwOnError?: T["throwOnError"];
45
46
  }
46
- export interface RequestOptions<TResponseStyle extends ResponseStyle = "fields", ThrowOnError extends boolean = boolean, Url extends string = string> extends Config<{
47
+ export interface RequestOptions<TData = unknown, TResponseStyle extends ResponseStyle = "fields", ThrowOnError extends boolean = boolean, Url extends string = string> extends Config<{
47
48
  responseStyle: TResponseStyle;
48
49
  throwOnError: ThrowOnError;
49
- }> {
50
+ }>, Pick<ServerSentEventsOptions<TData>, "onSseError" | "onSseEvent" | "sseDefaultRetryDelay" | "sseMaxRetryAttempts" | "sseMaxRetryDelay"> {
50
51
  /**
51
52
  * Any body that you want to add to your request.
52
53
  *
@@ -61,6 +62,9 @@ export interface RequestOptions<TResponseStyle extends ResponseStyle = "fields",
61
62
  security?: ReadonlyArray<Auth>;
62
63
  url: Url;
63
64
  }
65
+ export interface ResolvedRequestOptions<TResponseStyle extends ResponseStyle = "fields", ThrowOnError extends boolean = boolean, Url extends string = string> extends RequestOptions<unknown, TResponseStyle, ThrowOnError, Url> {
66
+ serializedBody?: string;
67
+ }
64
68
  export type RequestResult<TData = unknown, TError = unknown, ThrowOnError extends boolean = boolean, TResponseStyle extends ResponseStyle = "fields"> = ThrowOnError extends true ? Promise<TResponseStyle extends "data" ? TData extends Record<string, unknown> ? TData[keyof TData] : TData : {
65
69
  data: TData extends Record<string, unknown> ? TData[keyof TData] : TData;
66
70
  request: Request;
@@ -80,8 +84,12 @@ export interface ClientOptions {
80
84
  responseStyle?: ResponseStyle;
81
85
  throwOnError?: boolean;
82
86
  }
83
- type MethodFn = <TData = unknown, TError = unknown, ThrowOnError extends boolean = false, TResponseStyle extends ResponseStyle = "fields">(options: Omit<RequestOptions<TResponseStyle, ThrowOnError>, "method">) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
84
- type RequestFn = <TData = unknown, TError = unknown, ThrowOnError extends boolean = false, TResponseStyle extends ResponseStyle = "fields">(options: Omit<RequestOptions<TResponseStyle, ThrowOnError>, "method"> & Pick<Required<RequestOptions<TResponseStyle, ThrowOnError>>, "method">) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
87
+ type MethodFnBase = <TData = unknown, TError = unknown, ThrowOnError extends boolean = false, TResponseStyle extends ResponseStyle = "fields">(options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, "method">) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
88
+ type MethodFnServerSentEvents = <TData = unknown, TError = unknown, ThrowOnError extends boolean = false, TResponseStyle extends ResponseStyle = "fields">(options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, "method">) => Promise<ServerSentEventsResult<TData, TError>>;
89
+ type MethodFn = MethodFnBase & {
90
+ sse: MethodFnServerSentEvents;
91
+ };
92
+ type RequestFn = <TData = unknown, TError = unknown, ThrowOnError extends boolean = false, TResponseStyle extends ResponseStyle = "fields">(options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, "method"> & Pick<Required<RequestOptions<TData, TResponseStyle, ThrowOnError>>, "method">) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
85
93
  type BuildUrlFn = <TData extends {
86
94
  body?: unknown;
87
95
  path?: Record<string, unknown>;
@@ -89,7 +97,7 @@ type BuildUrlFn = <TData extends {
89
97
  url: string;
90
98
  }>(options: Pick<TData, "url"> & Options<TData>) => string;
91
99
  export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn> & {
92
- interceptors: Middleware<Request, Response, unknown, RequestOptions>;
100
+ interceptors: Middleware<Request, Response, unknown, ResolvedRequestOptions>;
93
101
  };
94
102
  /**
95
103
  * The `createClientConfig()` function will be called on client initialization
@@ -108,12 +116,12 @@ export interface TDataShape {
108
116
  url: string;
109
117
  }
110
118
  type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>;
111
- export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean, TResponseStyle extends ResponseStyle = "fields"> = OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, "body" | "path" | "query" | "url"> & Omit<TData, "url">;
119
+ export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean, TResponse = unknown, TResponseStyle extends ResponseStyle = "fields"> = OmitKeys<RequestOptions<TResponse, TResponseStyle, ThrowOnError>, "body" | "path" | "query" | "url"> & Omit<TData, "url">;
112
120
  export type OptionsLegacyParser<TData = unknown, ThrowOnError extends boolean = boolean, TResponseStyle extends ResponseStyle = "fields"> = TData extends {
113
121
  body?: any;
114
122
  } ? TData extends {
115
123
  headers?: any;
116
- } ? OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, "body" | "headers" | "url"> & TData : OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, "body" | "url"> & TData & Pick<RequestOptions<TResponseStyle, ThrowOnError>, "headers"> : TData extends {
124
+ } ? OmitKeys<RequestOptions<unknown, TResponseStyle, ThrowOnError>, "body" | "headers" | "url"> & TData : OmitKeys<RequestOptions<unknown, TResponseStyle, ThrowOnError>, "body" | "url"> & TData & Pick<RequestOptions<unknown, TResponseStyle, ThrowOnError>, "headers"> : TData extends {
117
125
  headers?: any;
118
- } ? OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, "headers" | "url"> & TData & Pick<RequestOptions<TResponseStyle, ThrowOnError>, "body"> : OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, "url"> & TData;
126
+ } ? OmitKeys<RequestOptions<unknown, TResponseStyle, ThrowOnError>, "headers" | "url"> & TData & Pick<RequestOptions<unknown, TResponseStyle, ThrowOnError>, "body"> : OmitKeys<RequestOptions<unknown, TResponseStyle, ThrowOnError>, "url"> & TData;
119
127
  export {};
@@ -0,0 +1,2 @@
1
+ // This file is auto-generated by @hey-api/openapi-ts
2
+ export {};
@@ -1,5 +1,5 @@
1
- import type { QuerySerializer, QuerySerializerOptions } from "../core/bodySerializer.js";
2
- import type { Client, ClientOptions, Config, RequestOptions } from "./types.js";
1
+ import type { QuerySerializerOptions } from "../core/bodySerializer.gen.js";
2
+ import type { Client, ClientOptions, Config, RequestOptions } from "./types.gen.js";
3
3
  export declare const createQuerySerializer: <T = unknown>({ allowReserved, array, object }?: QuerySerializerOptions) => (queryParams: T) => string;
4
4
  /**
5
5
  * Infers parseAs value from provided Content-Type header.
@@ -9,13 +9,6 @@ export declare const setAuthParams: ({ security, ...options }: Pick<Required<Req
9
9
  headers: Headers;
10
10
  }) => Promise<void>;
11
11
  export declare const buildUrl: Client["buildUrl"];
12
- export declare const getUrl: ({ baseUrl, path, query, querySerializer, url: _url, }: {
13
- baseUrl?: string;
14
- path?: Record<string, unknown>;
15
- query?: Record<string, unknown>;
16
- querySerializer: QuerySerializer;
17
- url: string;
18
- }) => string;
19
12
  export declare const mergeConfigs: (a: Config, b: Config) => Config;
20
13
  export declare const mergeHeaders: (...headers: Array<Required<Config>["headers"] | undefined>) => Headers;
21
14
  type ErrInterceptor<Err, Res, Req, Options> = (error: Err, response: Res, request: Req, options: Options) => Err | Promise<Err>;
@@ -1,58 +1,8 @@
1
- import { getAuthToken } from "../core/auth.js";
2
- import { jsonBodySerializer } from "../core/bodySerializer.js";
3
- import { serializeArrayParam, serializeObjectParam, serializePrimitiveParam } from "../core/pathSerializer.js";
4
- const PATH_PARAM_RE = /\{[^{}]+\}/g;
5
- const defaultPathSerializer = ({ path, url: _url }) => {
6
- let url = _url;
7
- const matches = _url.match(PATH_PARAM_RE);
8
- if (matches) {
9
- for (const match of matches) {
10
- let explode = false;
11
- let name = match.substring(1, match.length - 1);
12
- let style = "simple";
13
- if (name.endsWith("*")) {
14
- explode = true;
15
- name = name.substring(0, name.length - 1);
16
- }
17
- if (name.startsWith(".")) {
18
- name = name.substring(1);
19
- style = "label";
20
- }
21
- else if (name.startsWith(";")) {
22
- name = name.substring(1);
23
- style = "matrix";
24
- }
25
- const value = path[name];
26
- if (value === undefined || value === null) {
27
- continue;
28
- }
29
- if (Array.isArray(value)) {
30
- url = url.replace(match, serializeArrayParam({ explode, name, style, value }));
31
- continue;
32
- }
33
- if (typeof value === "object") {
34
- url = url.replace(match, serializeObjectParam({
35
- explode,
36
- name,
37
- style,
38
- value: value,
39
- valueOnly: true,
40
- }));
41
- continue;
42
- }
43
- if (style === "matrix") {
44
- url = url.replace(match, `;${serializePrimitiveParam({
45
- name,
46
- value: value,
47
- })}`);
48
- continue;
49
- }
50
- const replaceValue = encodeURIComponent(style === "label" ? `.${value}` : value);
51
- url = url.replace(match, replaceValue);
52
- }
53
- }
54
- return url;
55
- };
1
+ // This file is auto-generated by @hey-api/openapi-ts
2
+ import { getAuthToken } from "../core/auth.gen.js";
3
+ import { jsonBodySerializer } from "../core/bodySerializer.gen.js";
4
+ import { serializeArrayParam, serializeObjectParam, serializePrimitiveParam } from "../core/pathSerializer.gen.js";
5
+ import { getUrl } from "../core/utils.gen.js";
56
6
  export const createQuerySerializer = ({ allowReserved, array, object } = {}) => {
57
7
  const querySerializer = (queryParams) => {
58
8
  const search = [];
@@ -128,8 +78,20 @@ export const getParseAs = (contentType) => {
128
78
  }
129
79
  return;
130
80
  };
81
+ const checkForExistence = (options, name) => {
82
+ if (!name) {
83
+ return false;
84
+ }
85
+ if (options.headers.has(name) || options.query?.[name] || options.headers.get("Cookie")?.includes(`${name}=`)) {
86
+ return true;
87
+ }
88
+ return false;
89
+ };
131
90
  export const setAuthParams = async ({ security, ...options }) => {
132
91
  for (const auth of security) {
92
+ if (checkForExistence(options, auth.name)) {
93
+ continue;
94
+ }
133
95
  const token = await getAuthToken(auth, options.auth);
134
96
  if (!token) {
135
97
  continue;
@@ -150,36 +112,17 @@ export const setAuthParams = async ({ security, ...options }) => {
150
112
  options.headers.set(name, token);
151
113
  break;
152
114
  }
153
- return;
154
115
  }
155
116
  };
156
- export const buildUrl = (options) => {
157
- const url = getUrl({
158
- baseUrl: options.baseUrl,
159
- path: options.path,
160
- query: options.query,
161
- querySerializer: typeof options.querySerializer === "function"
162
- ? options.querySerializer
163
- : createQuerySerializer(options.querySerializer),
164
- url: options.url,
165
- });
166
- return url;
167
- };
168
- export const getUrl = ({ baseUrl, path, query, querySerializer, url: _url, }) => {
169
- const pathUrl = _url.startsWith("/") ? _url : `/${_url}`;
170
- let url = (baseUrl ?? "") + pathUrl;
171
- if (path) {
172
- url = defaultPathSerializer({ path, url });
173
- }
174
- let search = query ? querySerializer(query) : "";
175
- if (search.startsWith("?")) {
176
- search = search.substring(1);
177
- }
178
- if (search) {
179
- url += `?${search}`;
180
- }
181
- return url;
182
- };
117
+ export const buildUrl = (options) => getUrl({
118
+ baseUrl: options.baseUrl,
119
+ path: options.path,
120
+ query: options.query,
121
+ querySerializer: typeof options.querySerializer === "function"
122
+ ? options.querySerializer
123
+ : createQuerySerializer(options.querySerializer),
124
+ url: options.url,
125
+ });
183
126
  export const mergeConfigs = (a, b) => {
184
127
  const config = { ...a, ...b };
185
128
  if (config.baseUrl?.endsWith("/")) {
@@ -9,4 +9,4 @@ import { type Config, type ClientOptions as DefaultClientOptions } from "./clien
9
9
  * to ensure your client always has the correct values.
10
10
  */
11
11
  export type CreateClientConfig<T extends DefaultClientOptions = ClientOptions> = (override?: Config<DefaultClientOptions & T>) => Config<Required<DefaultClientOptions> & T>;
12
- export declare const client: import("./client/types.js").Client;
12
+ export declare const client: import("./client/types.gen.js").Client;
@@ -1,3 +1,4 @@
1
+ // This file is auto-generated by @hey-api/openapi-ts
1
2
  export const getAuthToken = async (auth, callback) => {
2
3
  const token = typeof callback === "function" ? await callback(auth) : callback;
3
4
  if (!token) {
@@ -1,4 +1,4 @@
1
- import type { ArrayStyle, ObjectStyle, SerializerOptions } from "./pathSerializer.js";
1
+ import type { ArrayStyle, ObjectStyle, SerializerOptions } from "./pathSerializer.gen.js";
2
2
  export type QuerySerializer = (query: Record<string, unknown>) => string;
3
3
  export type BodySerializer = (body: any) => any;
4
4
  export interface QuerySerializerOptions {
@@ -1,7 +1,11 @@
1
+ // This file is auto-generated by @hey-api/openapi-ts
1
2
  const serializeFormDataPair = (data, key, value) => {
2
3
  if (typeof value === "string" || value instanceof Blob) {
3
4
  data.append(key, value);
4
5
  }
6
+ else if (value instanceof Date) {
7
+ data.append(key, value.toISOString());
8
+ }
5
9
  else {
6
10
  data.append(key, JSON.stringify(value));
7
11
  }
@@ -1,3 +1,4 @@
1
+ // This file is auto-generated by @hey-api/openapi-ts
1
2
  const extraPrefixesMap = {
2
3
  $body_: "body",
3
4
  $headers_: "headers",
@@ -1,3 +1,4 @@
1
+ // This file is auto-generated by @hey-api/openapi-ts
1
2
  export const separatorArrayExplode = (style) => {
2
3
  switch (style) {
3
4
  case "label":
@@ -0,0 +1,59 @@
1
+ import type { Config } from "./types.gen.js";
2
+ export type ServerSentEventsOptions<TData = unknown> = Omit<RequestInit, "method"> & Pick<Config, "method" | "responseTransformer" | "responseValidator"> & {
3
+ /**
4
+ * Callback invoked when a network or parsing error occurs during streaming.
5
+ *
6
+ * This option applies only if the endpoint returns a stream of events.
7
+ *
8
+ * @param error The error that occurred.
9
+ */
10
+ onSseError?: (error: unknown) => void;
11
+ /**
12
+ * Callback invoked when an event is streamed from the server.
13
+ *
14
+ * This option applies only if the endpoint returns a stream of events.
15
+ *
16
+ * @param event Event streamed from the server.
17
+ * @returns Nothing (void).
18
+ */
19
+ onSseEvent?: (event: StreamEvent<TData>) => void;
20
+ /**
21
+ * Default retry delay in milliseconds.
22
+ *
23
+ * This option applies only if the endpoint returns a stream of events.
24
+ *
25
+ * @default 3000
26
+ */
27
+ sseDefaultRetryDelay?: number;
28
+ /**
29
+ * Maximum number of retry attempts before giving up.
30
+ */
31
+ sseMaxRetryAttempts?: number;
32
+ /**
33
+ * Maximum retry delay in milliseconds.
34
+ *
35
+ * Applies only when exponential backoff is used.
36
+ *
37
+ * This option applies only if the endpoint returns a stream of events.
38
+ *
39
+ * @default 30000
40
+ */
41
+ sseMaxRetryDelay?: number;
42
+ /**
43
+ * Optional sleep function for retry backoff.
44
+ *
45
+ * Defaults to using `setTimeout`.
46
+ */
47
+ sseSleepFn?: (ms: number) => Promise<void>;
48
+ url: string;
49
+ };
50
+ export interface StreamEvent<TData = unknown> {
51
+ data: TData;
52
+ event?: string;
53
+ id?: string;
54
+ retry?: number;
55
+ }
56
+ export type ServerSentEventsResult<TData = unknown, TReturn = void, TNext = unknown> = {
57
+ stream: AsyncGenerator<TData extends Record<string, unknown> ? TData[keyof TData] : TData, TReturn, TNext>;
58
+ };
59
+ export declare const createSseClient: <TData = unknown>({ onSseError, onSseEvent, responseTransformer, responseValidator, sseDefaultRetryDelay, sseMaxRetryAttempts, sseMaxRetryDelay, sseSleepFn, url, ...options }: ServerSentEventsOptions) => ServerSentEventsResult<TData>;
@@ -0,0 +1,117 @@
1
+ // This file is auto-generated by @hey-api/openapi-ts
2
+ export const createSseClient = ({ onSseError, onSseEvent, responseTransformer, responseValidator, sseDefaultRetryDelay, sseMaxRetryAttempts, sseMaxRetryDelay, sseSleepFn, url, ...options }) => {
3
+ let lastEventId;
4
+ const sleep = sseSleepFn ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
5
+ const createStream = async function* () {
6
+ let retryDelay = sseDefaultRetryDelay ?? 3000;
7
+ let attempt = 0;
8
+ const signal = options.signal ?? new AbortController().signal;
9
+ while (true) {
10
+ if (signal.aborted)
11
+ break;
12
+ attempt++;
13
+ const headers = options.headers instanceof Headers
14
+ ? options.headers
15
+ : new Headers(options.headers);
16
+ if (lastEventId !== undefined) {
17
+ headers.set("Last-Event-ID", lastEventId);
18
+ }
19
+ try {
20
+ const response = await fetch(url, { ...options, headers, signal });
21
+ if (!response.ok)
22
+ throw new Error(`SSE failed: ${response.status} ${response.statusText}`);
23
+ if (!response.body)
24
+ throw new Error("No body in SSE response");
25
+ const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
26
+ let buffer = "";
27
+ const abortHandler = () => {
28
+ try {
29
+ reader.cancel();
30
+ }
31
+ catch {
32
+ // noop
33
+ }
34
+ };
35
+ signal.addEventListener("abort", abortHandler);
36
+ try {
37
+ while (true) {
38
+ const { done, value } = await reader.read();
39
+ if (done)
40
+ break;
41
+ buffer += value;
42
+ const chunks = buffer.split("\n\n");
43
+ buffer = chunks.pop() ?? "";
44
+ for (const chunk of chunks) {
45
+ const lines = chunk.split("\n");
46
+ const dataLines = [];
47
+ let eventName;
48
+ for (const line of lines) {
49
+ if (line.startsWith("data:")) {
50
+ dataLines.push(line.replace(/^data:\s*/, ""));
51
+ }
52
+ else if (line.startsWith("event:")) {
53
+ eventName = line.replace(/^event:\s*/, "");
54
+ }
55
+ else if (line.startsWith("id:")) {
56
+ lastEventId = line.replace(/^id:\s*/, "");
57
+ }
58
+ else if (line.startsWith("retry:")) {
59
+ const parsed = Number.parseInt(line.replace(/^retry:\s*/, ""), 10);
60
+ if (!Number.isNaN(parsed)) {
61
+ retryDelay = parsed;
62
+ }
63
+ }
64
+ }
65
+ let data;
66
+ let parsedJson = false;
67
+ if (dataLines.length) {
68
+ const rawData = dataLines.join("\n");
69
+ try {
70
+ data = JSON.parse(rawData);
71
+ parsedJson = true;
72
+ }
73
+ catch {
74
+ data = rawData;
75
+ }
76
+ }
77
+ if (parsedJson) {
78
+ if (responseValidator) {
79
+ await responseValidator(data);
80
+ }
81
+ if (responseTransformer) {
82
+ data = await responseTransformer(data);
83
+ }
84
+ }
85
+ onSseEvent?.({
86
+ data,
87
+ event: eventName,
88
+ id: lastEventId,
89
+ retry: retryDelay,
90
+ });
91
+ if (dataLines.length) {
92
+ yield data;
93
+ }
94
+ }
95
+ }
96
+ }
97
+ finally {
98
+ signal.removeEventListener("abort", abortHandler);
99
+ reader.releaseLock();
100
+ }
101
+ break; // exit loop on normal completion
102
+ }
103
+ catch (error) {
104
+ // connection failed or aborted; retry after delay
105
+ onSseError?.(error);
106
+ if (sseMaxRetryAttempts !== undefined && attempt >= sseMaxRetryAttempts) {
107
+ break; // stop after firing error
108
+ }
109
+ // exponential backoff: double retry each attempt, cap at 30s
110
+ const backoff = Math.min(retryDelay * 2 ** (attempt - 1), sseMaxRetryDelay ?? 30000);
111
+ await sleep(backoff);
112
+ }
113
+ }
114
+ };
115
+ const stream = createStream();
116
+ return { stream };
117
+ };
@@ -1,5 +1,5 @@
1
- import type { Auth, AuthToken } from "./auth.js";
2
- import type { BodySerializer, QuerySerializer, QuerySerializerOptions } from "./bodySerializer.js";
1
+ import type { Auth, AuthToken } from "./auth.gen.js";
2
+ import type { BodySerializer, QuerySerializer, QuerySerializerOptions } from "./bodySerializer.gen.js";
3
3
  export interface Client<RequestFn = never, Config = unknown, MethodFn = never, BuildUrlFn = never> {
4
4
  /**
5
5
  * Returns the final request URL.
@@ -0,0 +1,2 @@
1
+ // This file is auto-generated by @hey-api/openapi-ts
2
+ export {};
@@ -0,0 +1,14 @@
1
+ import type { QuerySerializer } from "./bodySerializer.gen.js";
2
+ export interface PathSerializer {
3
+ path: Record<string, unknown>;
4
+ url: string;
5
+ }
6
+ export declare const PATH_PARAM_RE: RegExp;
7
+ export declare const defaultPathSerializer: ({ path, url: _url }: PathSerializer) => string;
8
+ export declare const getUrl: ({ baseUrl, path, query, querySerializer, url: _url, }: {
9
+ baseUrl?: string;
10
+ path?: Record<string, unknown>;
11
+ query?: Record<string, unknown>;
12
+ querySerializer: QuerySerializer;
13
+ url: string;
14
+ }) => string;