@journeybee/sdk 0.1.0 → 0.2.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.
@@ -0,0 +1,344 @@
1
+ type AuthToken = string | undefined;
2
+ interface Auth {
3
+ /**
4
+ * Which part of the request do we use to send the auth?
5
+ *
6
+ * @default 'header'
7
+ */
8
+ in?: 'header' | 'query' | 'cookie';
9
+ /**
10
+ * A unique identifier for the security scheme.
11
+ *
12
+ * Defined only when there are multiple security schemes whose `Auth`
13
+ * shape would otherwise be identical.
14
+ */
15
+ key?: string;
16
+ /**
17
+ * Header or query parameter name.
18
+ *
19
+ * @default 'Authorization'
20
+ */
21
+ name?: string;
22
+ scheme?: 'basic' | 'bearer';
23
+ type: 'apiKey' | 'http';
24
+ }
25
+
26
+ interface SerializerOptions<T> {
27
+ /**
28
+ * @default true
29
+ */
30
+ explode: boolean;
31
+ style: T;
32
+ }
33
+ type ArrayStyle = 'form' | 'spaceDelimited' | 'pipeDelimited';
34
+ type ObjectStyle = 'form' | 'deepObject';
35
+
36
+ type QuerySerializer = (query: Record<string, unknown>) => string;
37
+ type BodySerializer = (body: unknown) => unknown;
38
+ type QuerySerializerOptionsObject = {
39
+ allowReserved?: boolean;
40
+ array?: Partial<SerializerOptions<ArrayStyle>>;
41
+ object?: Partial<SerializerOptions<ObjectStyle>>;
42
+ };
43
+ type QuerySerializerOptions = QuerySerializerOptionsObject & {
44
+ /**
45
+ * Per-parameter serialization overrides. When provided, these settings
46
+ * override the global array/object settings for specific parameter names.
47
+ */
48
+ parameters?: Record<string, QuerySerializerOptionsObject>;
49
+ };
50
+
51
+ type HttpMethod = 'connect' | 'delete' | 'get' | 'head' | 'options' | 'patch' | 'post' | 'put' | 'trace';
52
+ type Client$1<RequestFn = never, Config = unknown, MethodFn = never, BuildUrlFn = never, SseFn = never> = {
53
+ /**
54
+ * Returns the final request URL.
55
+ */
56
+ buildUrl: BuildUrlFn;
57
+ getConfig: () => Config;
58
+ request: RequestFn;
59
+ setConfig: (config: Config) => Config;
60
+ } & {
61
+ [K in HttpMethod]: MethodFn;
62
+ } & ([SseFn] extends [never] ? {
63
+ sse?: never;
64
+ } : {
65
+ sse: {
66
+ [K in HttpMethod]: SseFn;
67
+ };
68
+ });
69
+ interface Config$1 {
70
+ /**
71
+ * Auth token or a function returning auth token. The resolved value will be
72
+ * added to the request payload as defined by its `security` array.
73
+ */
74
+ auth?: ((auth: Auth) => Promise<AuthToken> | AuthToken) | AuthToken;
75
+ /**
76
+ * A function for serializing request body parameter. By default,
77
+ * {@link JSON.stringify()} will be used.
78
+ */
79
+ bodySerializer?: BodySerializer | null;
80
+ /**
81
+ * An object containing any HTTP headers that you want to pre-populate your
82
+ * `Headers` object with.
83
+ *
84
+ * {@link https://developer.mozilla.org/docs/Web/API/Headers/Headers#init See more}
85
+ */
86
+ headers?: RequestInit['headers'] | Record<string, string | number | boolean | (string | number | boolean)[] | null | undefined | unknown>;
87
+ /**
88
+ * The request method.
89
+ *
90
+ * {@link https://developer.mozilla.org/docs/Web/API/fetch#method See more}
91
+ */
92
+ method?: Uppercase<HttpMethod>;
93
+ /**
94
+ * A function for serializing request query parameters. By default, arrays
95
+ * will be exploded in form style, objects will be exploded in deepObject
96
+ * style, and reserved characters are percent-encoded.
97
+ *
98
+ * This method will have no effect if the native `paramsSerializer()` Axios
99
+ * API function is used.
100
+ *
101
+ * {@link https://swagger.io/docs/specification/serialization/#query View examples}
102
+ */
103
+ querySerializer?: QuerySerializer | QuerySerializerOptions;
104
+ /**
105
+ * A function validating request data. This is useful if you want to ensure
106
+ * the request conforms to the desired shape, so it can be safely sent to
107
+ * the server.
108
+ */
109
+ requestValidator?: (data: unknown) => Promise<unknown>;
110
+ /**
111
+ * A function transforming response data before it's returned. This is useful
112
+ * for post-processing data, e.g., converting ISO strings into Date objects.
113
+ */
114
+ responseTransformer?: (data: unknown) => Promise<unknown>;
115
+ /**
116
+ * A function validating response data. This is useful if you want to ensure
117
+ * the response conforms to the desired shape, so it can be safely passed to
118
+ * the transformers and returned to the user.
119
+ */
120
+ responseValidator?: (data: unknown) => Promise<unknown>;
121
+ }
122
+ /**
123
+ * Arbitrary metadata passed through the `meta` request option.
124
+ */
125
+ interface ClientMeta {
126
+ }
127
+
128
+ type ServerSentEventsOptions<TData = unknown> = Omit<RequestInit, 'method'> & Pick<Config$1, 'method' | 'responseTransformer' | 'responseValidator'> & {
129
+ /**
130
+ * Fetch API implementation. You can use this option to provide a custom
131
+ * fetch instance.
132
+ *
133
+ * @default globalThis.fetch
134
+ */
135
+ fetch?: typeof fetch;
136
+ /**
137
+ * Implementing clients can call request interceptors inside this hook.
138
+ */
139
+ onRequest?: (url: string, init: RequestInit) => Promise<Request>;
140
+ /**
141
+ * Callback invoked when a network or parsing error occurs during streaming.
142
+ *
143
+ * This option applies only if the endpoint returns a stream of events.
144
+ *
145
+ * @param error The error that occurred.
146
+ */
147
+ onSseError?: (error: unknown) => void;
148
+ /**
149
+ * Callback invoked when an event is streamed from the server.
150
+ *
151
+ * This option applies only if the endpoint returns a stream of events.
152
+ *
153
+ * @param event Event streamed from the server.
154
+ * @returns Nothing (void).
155
+ */
156
+ onSseEvent?: (event: StreamEvent<TData>) => void;
157
+ serializedBody?: RequestInit['body'];
158
+ /**
159
+ * Default retry delay in milliseconds.
160
+ *
161
+ * This option applies only if the endpoint returns a stream of events.
162
+ *
163
+ * @default 3000
164
+ */
165
+ sseDefaultRetryDelay?: number;
166
+ /**
167
+ * Maximum number of retry attempts before giving up.
168
+ */
169
+ sseMaxRetryAttempts?: number;
170
+ /**
171
+ * Maximum retry delay in milliseconds.
172
+ *
173
+ * Applies only when exponential backoff is used.
174
+ *
175
+ * This option applies only if the endpoint returns a stream of events.
176
+ *
177
+ * @default 30000
178
+ */
179
+ sseMaxRetryDelay?: number;
180
+ /**
181
+ * Optional sleep function for retry backoff.
182
+ *
183
+ * Defaults to using `setTimeout`.
184
+ */
185
+ sseSleepFn?: (ms: number) => Promise<void>;
186
+ url: string;
187
+ };
188
+ interface StreamEvent<TData = unknown> {
189
+ data: TData;
190
+ event?: string;
191
+ id?: string;
192
+ retry?: number;
193
+ }
194
+ type ServerSentEventsResult<TData = unknown, TReturn = void, TNext = unknown> = {
195
+ stream: AsyncGenerator<TData extends Record<string, unknown> ? TData[keyof TData] : TData, TReturn, TNext>;
196
+ };
197
+
198
+ type ErrInterceptor<Err, Res, Req, Options> = (error: Err,
199
+ /** response may be undefined due to a network error where no response object is produced */
200
+ response: Res | undefined,
201
+ /** request may be undefined, because error may be from building the request object itself */
202
+ request: Req | undefined, options: Options) => Err | Promise<Err>;
203
+ type ReqInterceptor<Req, Options> = (request: Req, options: Options) => Req | Promise<Req>;
204
+ type ResInterceptor<Res, Req, Options> = (response: Res, request: Req, options: Options) => Res | Promise<Res>;
205
+ declare class Interceptors<Interceptor> {
206
+ fns: Array<Interceptor | null>;
207
+ clear(): void;
208
+ eject(id: number | Interceptor): void;
209
+ exists(id: number | Interceptor): boolean;
210
+ getInterceptorIndex(id: number | Interceptor): number;
211
+ update(id: number | Interceptor, fn: Interceptor): number | Interceptor | false;
212
+ use(fn: Interceptor): number;
213
+ }
214
+ interface Middleware<Req, Res, Err, Options> {
215
+ error: Interceptors<ErrInterceptor<Err, Res, Req, Options>>;
216
+ request: Interceptors<ReqInterceptor<Req, Options>>;
217
+ response: Interceptors<ResInterceptor<Res, Req, Options>>;
218
+ }
219
+ declare const createConfig: <T extends ClientOptions = ClientOptions>(override?: Config<Omit<ClientOptions, keyof T> & T>) => Config<Omit<ClientOptions, keyof T> & T>;
220
+
221
+ type ResponseStyle = 'data' | 'fields';
222
+ interface Config<T extends ClientOptions = ClientOptions> extends Omit<RequestInit, 'body' | 'headers' | 'method'>, Config$1 {
223
+ /**
224
+ * Base URL for all requests made by this client.
225
+ */
226
+ baseUrl?: T['baseUrl'];
227
+ /**
228
+ * Fetch API implementation. You can use this option to provide a custom
229
+ * fetch instance.
230
+ *
231
+ * @default globalThis.fetch
232
+ */
233
+ fetch?: typeof fetch;
234
+ /**
235
+ * Please don't use the Fetch client for Next.js applications. The `next`
236
+ * options won't have any effect.
237
+ *
238
+ * Install {@link https://www.npmjs.com/package/@hey-api/client-next `@hey-api/client-next`} instead.
239
+ */
240
+ next?: never;
241
+ /**
242
+ * Return the response data parsed in a specified format. By default, `auto`
243
+ * will infer the appropriate method from the `Content-Type` response header.
244
+ * You can override this behavior with any of the {@link Body} methods.
245
+ * Select `stream` if you don't want to parse response data at all.
246
+ *
247
+ * @default 'auto'
248
+ */
249
+ parseAs?: 'arrayBuffer' | 'auto' | 'blob' | 'formData' | 'json' | 'stream' | 'text';
250
+ /**
251
+ * Should we return only data or multiple fields (data, error, response, etc.)?
252
+ *
253
+ * @default 'fields'
254
+ */
255
+ responseStyle?: ResponseStyle;
256
+ /**
257
+ * Throw an error instead of returning it in the response?
258
+ *
259
+ * @default false
260
+ */
261
+ throwOnError?: T['throwOnError'];
262
+ }
263
+ interface RequestOptions<TData = unknown, TResponseStyle extends ResponseStyle = 'fields', ThrowOnError extends boolean = boolean, Url extends string = string> extends Config<{
264
+ responseStyle: TResponseStyle;
265
+ throwOnError: ThrowOnError;
266
+ }>, Pick<ServerSentEventsOptions<TData>, 'onRequest' | 'onSseError' | 'onSseEvent' | 'sseDefaultRetryDelay' | 'sseMaxRetryAttempts' | 'sseMaxRetryDelay'> {
267
+ /**
268
+ * Any body that you want to add to your request.
269
+ *
270
+ * {@link https://developer.mozilla.org/docs/Web/API/fetch#body}
271
+ */
272
+ body?: unknown;
273
+ path?: Record<string, unknown>;
274
+ query?: Record<string, unknown>;
275
+ /**
276
+ * Security mechanism(s) to use for the request.
277
+ */
278
+ security?: ReadonlyArray<Auth>;
279
+ url: Url;
280
+ }
281
+ interface ResolvedRequestOptions<TResponseStyle extends ResponseStyle = 'fields', ThrowOnError extends boolean = boolean, Url extends string = string> extends RequestOptions<unknown, TResponseStyle, ThrowOnError, Url> {
282
+ headers: Headers;
283
+ serializedBody?: string;
284
+ }
285
+ 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 : {
286
+ data: TData extends Record<string, unknown> ? TData[keyof TData] : TData;
287
+ request: Request;
288
+ response: Response;
289
+ }> : Promise<TResponseStyle extends 'data' ? (TData extends Record<string, unknown> ? TData[keyof TData] : TData) | undefined : ({
290
+ data: TData extends Record<string, unknown> ? TData[keyof TData] : TData;
291
+ error: undefined;
292
+ } | {
293
+ data: undefined;
294
+ error: TError extends Record<string, unknown> ? TError[keyof TError] : TError;
295
+ }) & {
296
+ /** request may be undefined, because error may be from building the request object itself */
297
+ request?: Request;
298
+ /** response may be undefined, because error may be from building the request object itself or from a network error */
299
+ response?: Response;
300
+ }>;
301
+ interface ClientOptions {
302
+ baseUrl?: string;
303
+ responseStyle?: ResponseStyle;
304
+ throwOnError?: boolean;
305
+ }
306
+ type MethodFn = <TData = unknown, TError = unknown, ThrowOnError extends boolean = false, TResponseStyle extends ResponseStyle = 'fields'>(options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'>) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
307
+ type SseFn = <TData = unknown, _TError = unknown, ThrowOnError extends boolean = false, TResponseStyle extends ResponseStyle = 'fields'>(options: Omit<RequestOptions<never, TResponseStyle, ThrowOnError>, 'method'>) => Promise<ServerSentEventsResult<TData>>;
308
+ 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>;
309
+ type BuildUrlFn = <TData extends {
310
+ body?: unknown;
311
+ path?: Record<string, unknown>;
312
+ query?: Record<string, unknown>;
313
+ url: string;
314
+ }>(options: TData & Options<TData>) => string;
315
+ type Client = Client$1<RequestFn, Config, MethodFn, BuildUrlFn, SseFn> & {
316
+ interceptors: Middleware<Request, Response, unknown, ResolvedRequestOptions>;
317
+ };
318
+ interface TDataShape {
319
+ body?: unknown;
320
+ headers?: unknown;
321
+ path?: unknown;
322
+ query?: unknown;
323
+ url: string;
324
+ }
325
+ type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>;
326
+ 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'> & ([TData] extends [never] ? unknown : Omit<TData, 'url'>);
327
+
328
+ /** Public logical operation id; reuse only to retry the exact API operation. */
329
+ type LogicalOperationId = string;
330
+ /** Create a new caller-controlled logical operation id for a mutation. */
331
+ declare const createLogicalOperationId: () => LogicalOperationId;
332
+ /**
333
+ * Build a request header object for an explicit retry. The same id must be
334
+ * reused only for a retry of the same logical API operation.
335
+ */
336
+ declare const idempotencyHeaders: (operationId: LogicalOperationId) => {
337
+ "Idempotency-Key": string;
338
+ };
339
+ /** Adds automatic mutation idempotency without overwriting a caller's key. */
340
+ declare const addIdempotencyInterceptor: (client: Client) => Client;
341
+ /** Create a per-request SDK client with the same idempotency behaviour. */
342
+ declare const createIdempotentClient: (config?: Config) => Client;
343
+
344
+ export { type Config as C, type LogicalOperationId as L, type Options as O, type RequestResult as R, type TDataShape as T, type Client as a, type ClientOptions as b, type ClientMeta as c, addIdempotencyInterceptor as d, createConfig as e, createIdempotentClient as f, createLogicalOperationId as g, idempotencyHeaders as i };