universal-client 0.0.14 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +99 -71
- package/dist/index.d.ts +99 -71
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -58,6 +58,55 @@ interface HooksInternal {
|
|
|
58
58
|
};
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
/**
|
|
62
|
+
* Context for request interception, allows modifying request properties
|
|
63
|
+
*/
|
|
64
|
+
interface RequestInterceptorContext {
|
|
65
|
+
method: string;
|
|
66
|
+
url: string;
|
|
67
|
+
headers?: Record<string, string>;
|
|
68
|
+
body?: unknown;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Context for response interception
|
|
72
|
+
*/
|
|
73
|
+
interface ResponseInterceptorContext<T = unknown> {
|
|
74
|
+
method: string;
|
|
75
|
+
url: string;
|
|
76
|
+
response: T;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Interceptor for HTTP delegate methods
|
|
80
|
+
*/
|
|
81
|
+
interface HttpInterceptor {
|
|
82
|
+
onBeforeRequest?: (context: RequestInterceptorContext) => undefined | Partial<RequestInterceptorContext> | Promise<Partial<RequestInterceptorContext> | undefined>;
|
|
83
|
+
onAfterResponse?: <T>(context: ResponseInterceptorContext<T>) => undefined | Partial<T> | Promise<Partial<T> | undefined>;
|
|
84
|
+
onError?: (method: string, url: string, error: Error, body?: unknown) => void | Promise<void>;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Interceptor for WebSocket delegate methods
|
|
88
|
+
*/
|
|
89
|
+
interface WebSocketInterceptor {
|
|
90
|
+
onBeforeConnect?: () => void | Promise<void>;
|
|
91
|
+
onAfterConnect?: () => void | Promise<void>;
|
|
92
|
+
onBeforeSend?: (message: unknown) => void | Promise<void>;
|
|
93
|
+
onAfterSend?: (message: unknown) => void | Promise<void>;
|
|
94
|
+
onBeforeClose?: () => void | Promise<void>;
|
|
95
|
+
onAfterClose?: () => void | Promise<void>;
|
|
96
|
+
onError?: (error: Error) => void | Promise<void>;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Interceptor for Server-Sent Event delegate methods
|
|
100
|
+
*/
|
|
101
|
+
interface ServerSentEventInterceptor {
|
|
102
|
+
onBeforeOpen?: (options?: SseOpenOptions) => undefined | Partial<SseOpenOptions> | Promise<Partial<SseOpenOptions> | undefined>;
|
|
103
|
+
onAfterOpen?: (options?: SseOpenOptions) => void | Promise<void>;
|
|
104
|
+
onBeforeClose?: () => void | Promise<void>;
|
|
105
|
+
onAfterClose?: () => void | Promise<void>;
|
|
106
|
+
onError?: (error: Error) => void | Promise<void>;
|
|
107
|
+
onMessage?: (data: unknown) => void | Promise<void>;
|
|
108
|
+
}
|
|
109
|
+
|
|
61
110
|
type CreateAxiosDelegateOptions = {
|
|
62
111
|
type: 'http';
|
|
63
112
|
impl: 'axios';
|
|
@@ -97,32 +146,6 @@ type WebSocketDelegateOptions = {
|
|
|
97
146
|
|
|
98
147
|
type DelegateOptions = HttpDelegateOptions | WebSocketDelegateOptions | ServerSentEventDelegateOptions;
|
|
99
148
|
|
|
100
|
-
/**
|
|
101
|
-
* Context for request interception, allows modifying request properties
|
|
102
|
-
*/
|
|
103
|
-
interface RequestInterceptorContext {
|
|
104
|
-
method: string;
|
|
105
|
-
url: string;
|
|
106
|
-
headers?: Record<string, string>;
|
|
107
|
-
body?: unknown;
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Context for response interception
|
|
111
|
-
*/
|
|
112
|
-
interface ResponseInterceptorContext<T = unknown> {
|
|
113
|
-
method: string;
|
|
114
|
-
url: string;
|
|
115
|
-
response: T;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Interceptor for HTTP delegate methods
|
|
119
|
-
*/
|
|
120
|
-
interface HttpInterceptor {
|
|
121
|
-
before?: (context: RequestInterceptorContext) => undefined | Partial<RequestInterceptorContext> | Promise<Partial<RequestInterceptorContext> | undefined>;
|
|
122
|
-
after?: <T>(context: ResponseInterceptorContext<T>) => undefined | Partial<T> | Promise<Partial<T> | undefined>;
|
|
123
|
-
error?: (method: string, url: string, error: Error, body?: unknown) => void | Promise<void>;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
149
|
type DelegateFromOptions<O extends DelegateOptions> = O['type'] extends 'http' ? HttpDelegate : O['type'] extends 'websocket' ? WebSocketDelegate : O['type'] extends 'server-sent-event' ? ServerSentEventDelegate : never;
|
|
127
150
|
/**
|
|
128
151
|
* Add a delegate to the client.
|
|
@@ -139,63 +162,70 @@ declare function withDelegate<const Options extends DelegateOptions & {
|
|
|
139
162
|
declare function withDelegate<const Options extends DelegateOptions>(options: Options, ...features: DelegateFeature[]): Feature<unknown, {
|
|
140
163
|
delegate: DelegateFromOptions<Options>;
|
|
141
164
|
}>;
|
|
142
|
-
type
|
|
165
|
+
type HttpDelegateShortcutOptions = Omit<HttpDelegateOptions, 'type' | 'baseURL'> & {
|
|
166
|
+
impl: 'fetch' | 'axios' | 'better-fetch';
|
|
143
167
|
name?: string;
|
|
144
168
|
};
|
|
145
|
-
type
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
type
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
type
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
169
|
+
type AxiosDelegateShortcutOptions = Omit<CreateAxiosDelegateOptions, 'type' | 'impl' | 'baseURL'> & {
|
|
170
|
+
name?: string;
|
|
171
|
+
};
|
|
172
|
+
type BetterFetchDelegateShortcutOptions = Omit<CreateBetterFetchDelegateOptions, 'type' | 'impl' | 'baseURL'> & {
|
|
173
|
+
name?: string;
|
|
174
|
+
};
|
|
175
|
+
type WebSocketDelegateShortcutOptions = Omit<CreateWebSocketDelegateOptions, 'baseURL'> & {
|
|
176
|
+
name?: string;
|
|
177
|
+
};
|
|
178
|
+
declare function withHttpDelegate<const Name extends string>(baseURL: string, options: HttpDelegateShortcutOptions & {
|
|
179
|
+
name: Name;
|
|
180
|
+
}, ...features: DelegateFeature[]): Feature<unknown, {
|
|
181
|
+
[key in Name]: HttpDelegate;
|
|
157
182
|
}>;
|
|
158
|
-
declare function withHttpDelegate(options: HttpDelegateShortcutOptions, ...features: DelegateFeature[]): Feature<unknown, {
|
|
183
|
+
declare function withHttpDelegate(baseURL: string, options: HttpDelegateShortcutOptions, ...features: DelegateFeature[]): Feature<unknown, {
|
|
159
184
|
delegate: HttpDelegate;
|
|
160
185
|
}>;
|
|
161
|
-
declare function withFetchDelegate<const
|
|
162
|
-
|
|
163
|
-
}>(options: Options, ...features: DelegateFeature[]): Feature<unknown, {
|
|
164
|
-
[key in Options['name']]: HttpDelegate;
|
|
186
|
+
declare function withFetchDelegate<const Name extends string>(baseURL: string, name: Name, ...features: DelegateFeature[]): Feature<unknown, {
|
|
187
|
+
[key in Name]: HttpDelegate;
|
|
165
188
|
}>;
|
|
166
|
-
declare function withFetchDelegate(
|
|
189
|
+
declare function withFetchDelegate(baseURL: string, ...features: DelegateFeature[]): Feature<unknown, {
|
|
167
190
|
delegate: HttpDelegate;
|
|
168
191
|
}>;
|
|
169
|
-
declare function withAxiosDelegate<const
|
|
170
|
-
|
|
171
|
-
}>(options: Options, ...features: DelegateFeature[]): Feature<unknown, {
|
|
172
|
-
[key in Options['name']]: HttpDelegate;
|
|
192
|
+
declare function withAxiosDelegate<const Name extends string>(baseURL: string, name: Name, ...features: DelegateFeature[]): Feature<unknown, {
|
|
193
|
+
[key in Name]: HttpDelegate;
|
|
173
194
|
}>;
|
|
174
|
-
declare function withAxiosDelegate(
|
|
195
|
+
declare function withAxiosDelegate<const Name extends string>(baseURL: string, options: AxiosDelegateShortcutOptions & {
|
|
196
|
+
name: Name;
|
|
197
|
+
}, ...features: DelegateFeature[]): Feature<unknown, {
|
|
198
|
+
[key in Name]: HttpDelegate;
|
|
199
|
+
}>;
|
|
200
|
+
declare function withAxiosDelegate(baseURL: string, ...features: DelegateFeature[]): Feature<unknown, {
|
|
175
201
|
delegate: HttpDelegate;
|
|
176
202
|
}>;
|
|
177
|
-
declare function withBetterFetchDelegate<const
|
|
178
|
-
|
|
179
|
-
}
|
|
180
|
-
|
|
203
|
+
declare function withBetterFetchDelegate<const Name extends string>(baseURL: string, name: Name, ...features: DelegateFeature[]): Feature<unknown, {
|
|
204
|
+
[key in Name]: HttpDelegate;
|
|
205
|
+
}>;
|
|
206
|
+
declare function withBetterFetchDelegate<const Name extends string>(baseURL: string, options: BetterFetchDelegateShortcutOptions & {
|
|
207
|
+
name: Name;
|
|
208
|
+
}, ...features: DelegateFeature[]): Feature<unknown, {
|
|
209
|
+
[key in Name]: HttpDelegate;
|
|
181
210
|
}>;
|
|
182
|
-
declare function withBetterFetchDelegate(
|
|
211
|
+
declare function withBetterFetchDelegate(baseURL: string, ...features: DelegateFeature[]): Feature<unknown, {
|
|
183
212
|
delegate: HttpDelegate;
|
|
184
213
|
}>;
|
|
185
|
-
declare function withSseDelegate<const
|
|
186
|
-
|
|
187
|
-
}>(options: Options, ...features: DelegateFeature[]): Feature<unknown, {
|
|
188
|
-
[key in Options['name']]: ServerSentEventDelegate;
|
|
214
|
+
declare function withSseDelegate<const Name extends string>(baseURL: string, name: Name, ...features: DelegateFeature[]): Feature<unknown, {
|
|
215
|
+
[key in Name]: ServerSentEventDelegate;
|
|
189
216
|
}>;
|
|
190
|
-
declare function withSseDelegate(
|
|
217
|
+
declare function withSseDelegate(baseURL: string, ...features: DelegateFeature[]): Feature<unknown, {
|
|
191
218
|
delegate: ServerSentEventDelegate;
|
|
192
219
|
}>;
|
|
193
|
-
declare function withWebSocketDelegate<const
|
|
194
|
-
|
|
195
|
-
}
|
|
196
|
-
|
|
220
|
+
declare function withWebSocketDelegate<const Name extends string>(baseURL: string, name: Name, ...features: DelegateFeature[]): Feature<unknown, {
|
|
221
|
+
[key in Name]: WebSocketDelegate;
|
|
222
|
+
}>;
|
|
223
|
+
declare function withWebSocketDelegate<const Name extends string>(baseURL: string, options: WebSocketDelegateShortcutOptions & {
|
|
224
|
+
name: Name;
|
|
225
|
+
}, ...features: DelegateFeature[]): Feature<unknown, {
|
|
226
|
+
[key in Name]: WebSocketDelegate;
|
|
197
227
|
}>;
|
|
198
|
-
declare function withWebSocketDelegate(
|
|
228
|
+
declare function withWebSocketDelegate(baseURL: string, ...features: DelegateFeature[]): Feature<unknown, {
|
|
199
229
|
delegate: WebSocketDelegate;
|
|
200
230
|
}>;
|
|
201
231
|
|
|
@@ -234,14 +264,12 @@ declare function withEnvironments<Input, Environment extends Environments>(confi
|
|
|
234
264
|
environments: EnvironmentManager<Environment>;
|
|
235
265
|
}>;
|
|
236
266
|
|
|
237
|
-
|
|
238
|
-
name?: string;
|
|
239
|
-
}
|
|
267
|
+
type InterceptorConfig = HttpInterceptor | ServerSentEventInterceptor | WebSocketInterceptor;
|
|
240
268
|
/**
|
|
241
|
-
* Add
|
|
269
|
+
* Add interceptor to the delegate.
|
|
242
270
|
*
|
|
243
271
|
* @param config - The interceptor configuration.
|
|
244
|
-
* @returns A delegate feature that wraps the
|
|
272
|
+
* @returns A delegate feature that wraps the delegate with the interceptor.
|
|
245
273
|
*/
|
|
246
274
|
declare function withInterceptor(config: InterceptorConfig): DelegateFeature;
|
|
247
275
|
|
|
@@ -322,4 +350,4 @@ declare function universalClient<A, B, C, D, E, F, G, H, I>(featureA: Feature<un
|
|
|
322
350
|
declare function universalClient<A, B, C, D, E, F, G, H, I, J>(featureA: Feature<unknown, A>, featureB: Feature<A, B>, featureC: Feature<Merge<A, B>, C>, featureD: Feature<Merge<Merge<A, B>, C>, D>, featureE: Feature<Merge<Merge<Merge<A, B>, C>, D>, E>, featureF: Feature<Merge<Merge<Merge<Merge<A, B>, C>, D>, E>, F>, featureG: Feature<Merge<Merge<Merge<Merge<Merge<A, B>, C>, D>, E>, F>, G>, featureH: Feature<Merge<Merge<Merge<Merge<Merge<Merge<A, B>, C>, D>, E>, F>, G>, H>, featureI: Feature<Merge<Merge<Merge<Merge<Merge<Merge<Merge<A, B>, C>, D>, E>, F>, G>, H>, I>, featureJ: Feature<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<A, B>, C>, D>, E>, F>, G>, H>, I>, J>): Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<A, B>, C>, D>, E>, F>, G>, H>, I>, J>;
|
|
323
351
|
declare function universalClient(...withFeatures: Feature<unknown, unknown>[]): unknown;
|
|
324
352
|
|
|
325
|
-
export { type Delegate, type DelegateFeature, type Feature, type Hooks$1 as Hooks, type HooksInternal, type HttpDelegate, type HttpRequestOptions, type InterceptorConfig, type ServerSentEventDelegate, type SseOpenOptions, type WebSocketDelegate, universalClient, withAxiosDelegate, withBetterFetchDelegate, withDelegate, withEnvironments, withFetchDelegate, withHooks, withHttpDelegate, withInterceptor, withMethods, withOffline, withSseDelegate, withTelemetry, withWebSocketDelegate };
|
|
353
|
+
export { type Delegate, type DelegateFeature, type Feature, type Hooks$1 as Hooks, type HooksInternal, type HttpDelegate, type HttpInterceptor, type HttpRequestOptions, type InterceptorConfig, type RequestInterceptorContext, type ResponseInterceptorContext, type ServerSentEventDelegate, type ServerSentEventInterceptor, type SseOpenOptions, type WebSocketDelegate, type WebSocketInterceptor, universalClient, withAxiosDelegate, withBetterFetchDelegate, withDelegate, withEnvironments, withFetchDelegate, withHooks, withHttpDelegate, withInterceptor, withMethods, withOffline, withSseDelegate, withTelemetry, withWebSocketDelegate };
|
package/dist/index.d.ts
CHANGED
|
@@ -58,6 +58,55 @@ interface HooksInternal {
|
|
|
58
58
|
};
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
/**
|
|
62
|
+
* Context for request interception, allows modifying request properties
|
|
63
|
+
*/
|
|
64
|
+
interface RequestInterceptorContext {
|
|
65
|
+
method: string;
|
|
66
|
+
url: string;
|
|
67
|
+
headers?: Record<string, string>;
|
|
68
|
+
body?: unknown;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Context for response interception
|
|
72
|
+
*/
|
|
73
|
+
interface ResponseInterceptorContext<T = unknown> {
|
|
74
|
+
method: string;
|
|
75
|
+
url: string;
|
|
76
|
+
response: T;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Interceptor for HTTP delegate methods
|
|
80
|
+
*/
|
|
81
|
+
interface HttpInterceptor {
|
|
82
|
+
onBeforeRequest?: (context: RequestInterceptorContext) => undefined | Partial<RequestInterceptorContext> | Promise<Partial<RequestInterceptorContext> | undefined>;
|
|
83
|
+
onAfterResponse?: <T>(context: ResponseInterceptorContext<T>) => undefined | Partial<T> | Promise<Partial<T> | undefined>;
|
|
84
|
+
onError?: (method: string, url: string, error: Error, body?: unknown) => void | Promise<void>;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Interceptor for WebSocket delegate methods
|
|
88
|
+
*/
|
|
89
|
+
interface WebSocketInterceptor {
|
|
90
|
+
onBeforeConnect?: () => void | Promise<void>;
|
|
91
|
+
onAfterConnect?: () => void | Promise<void>;
|
|
92
|
+
onBeforeSend?: (message: unknown) => void | Promise<void>;
|
|
93
|
+
onAfterSend?: (message: unknown) => void | Promise<void>;
|
|
94
|
+
onBeforeClose?: () => void | Promise<void>;
|
|
95
|
+
onAfterClose?: () => void | Promise<void>;
|
|
96
|
+
onError?: (error: Error) => void | Promise<void>;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Interceptor for Server-Sent Event delegate methods
|
|
100
|
+
*/
|
|
101
|
+
interface ServerSentEventInterceptor {
|
|
102
|
+
onBeforeOpen?: (options?: SseOpenOptions) => undefined | Partial<SseOpenOptions> | Promise<Partial<SseOpenOptions> | undefined>;
|
|
103
|
+
onAfterOpen?: (options?: SseOpenOptions) => void | Promise<void>;
|
|
104
|
+
onBeforeClose?: () => void | Promise<void>;
|
|
105
|
+
onAfterClose?: () => void | Promise<void>;
|
|
106
|
+
onError?: (error: Error) => void | Promise<void>;
|
|
107
|
+
onMessage?: (data: unknown) => void | Promise<void>;
|
|
108
|
+
}
|
|
109
|
+
|
|
61
110
|
type CreateAxiosDelegateOptions = {
|
|
62
111
|
type: 'http';
|
|
63
112
|
impl: 'axios';
|
|
@@ -97,32 +146,6 @@ type WebSocketDelegateOptions = {
|
|
|
97
146
|
|
|
98
147
|
type DelegateOptions = HttpDelegateOptions | WebSocketDelegateOptions | ServerSentEventDelegateOptions;
|
|
99
148
|
|
|
100
|
-
/**
|
|
101
|
-
* Context for request interception, allows modifying request properties
|
|
102
|
-
*/
|
|
103
|
-
interface RequestInterceptorContext {
|
|
104
|
-
method: string;
|
|
105
|
-
url: string;
|
|
106
|
-
headers?: Record<string, string>;
|
|
107
|
-
body?: unknown;
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Context for response interception
|
|
111
|
-
*/
|
|
112
|
-
interface ResponseInterceptorContext<T = unknown> {
|
|
113
|
-
method: string;
|
|
114
|
-
url: string;
|
|
115
|
-
response: T;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Interceptor for HTTP delegate methods
|
|
119
|
-
*/
|
|
120
|
-
interface HttpInterceptor {
|
|
121
|
-
before?: (context: RequestInterceptorContext) => undefined | Partial<RequestInterceptorContext> | Promise<Partial<RequestInterceptorContext> | undefined>;
|
|
122
|
-
after?: <T>(context: ResponseInterceptorContext<T>) => undefined | Partial<T> | Promise<Partial<T> | undefined>;
|
|
123
|
-
error?: (method: string, url: string, error: Error, body?: unknown) => void | Promise<void>;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
149
|
type DelegateFromOptions<O extends DelegateOptions> = O['type'] extends 'http' ? HttpDelegate : O['type'] extends 'websocket' ? WebSocketDelegate : O['type'] extends 'server-sent-event' ? ServerSentEventDelegate : never;
|
|
127
150
|
/**
|
|
128
151
|
* Add a delegate to the client.
|
|
@@ -139,63 +162,70 @@ declare function withDelegate<const Options extends DelegateOptions & {
|
|
|
139
162
|
declare function withDelegate<const Options extends DelegateOptions>(options: Options, ...features: DelegateFeature[]): Feature<unknown, {
|
|
140
163
|
delegate: DelegateFromOptions<Options>;
|
|
141
164
|
}>;
|
|
142
|
-
type
|
|
165
|
+
type HttpDelegateShortcutOptions = Omit<HttpDelegateOptions, 'type' | 'baseURL'> & {
|
|
166
|
+
impl: 'fetch' | 'axios' | 'better-fetch';
|
|
143
167
|
name?: string;
|
|
144
168
|
};
|
|
145
|
-
type
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
type
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
type
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
169
|
+
type AxiosDelegateShortcutOptions = Omit<CreateAxiosDelegateOptions, 'type' | 'impl' | 'baseURL'> & {
|
|
170
|
+
name?: string;
|
|
171
|
+
};
|
|
172
|
+
type BetterFetchDelegateShortcutOptions = Omit<CreateBetterFetchDelegateOptions, 'type' | 'impl' | 'baseURL'> & {
|
|
173
|
+
name?: string;
|
|
174
|
+
};
|
|
175
|
+
type WebSocketDelegateShortcutOptions = Omit<CreateWebSocketDelegateOptions, 'baseURL'> & {
|
|
176
|
+
name?: string;
|
|
177
|
+
};
|
|
178
|
+
declare function withHttpDelegate<const Name extends string>(baseURL: string, options: HttpDelegateShortcutOptions & {
|
|
179
|
+
name: Name;
|
|
180
|
+
}, ...features: DelegateFeature[]): Feature<unknown, {
|
|
181
|
+
[key in Name]: HttpDelegate;
|
|
157
182
|
}>;
|
|
158
|
-
declare function withHttpDelegate(options: HttpDelegateShortcutOptions, ...features: DelegateFeature[]): Feature<unknown, {
|
|
183
|
+
declare function withHttpDelegate(baseURL: string, options: HttpDelegateShortcutOptions, ...features: DelegateFeature[]): Feature<unknown, {
|
|
159
184
|
delegate: HttpDelegate;
|
|
160
185
|
}>;
|
|
161
|
-
declare function withFetchDelegate<const
|
|
162
|
-
|
|
163
|
-
}>(options: Options, ...features: DelegateFeature[]): Feature<unknown, {
|
|
164
|
-
[key in Options['name']]: HttpDelegate;
|
|
186
|
+
declare function withFetchDelegate<const Name extends string>(baseURL: string, name: Name, ...features: DelegateFeature[]): Feature<unknown, {
|
|
187
|
+
[key in Name]: HttpDelegate;
|
|
165
188
|
}>;
|
|
166
|
-
declare function withFetchDelegate(
|
|
189
|
+
declare function withFetchDelegate(baseURL: string, ...features: DelegateFeature[]): Feature<unknown, {
|
|
167
190
|
delegate: HttpDelegate;
|
|
168
191
|
}>;
|
|
169
|
-
declare function withAxiosDelegate<const
|
|
170
|
-
|
|
171
|
-
}>(options: Options, ...features: DelegateFeature[]): Feature<unknown, {
|
|
172
|
-
[key in Options['name']]: HttpDelegate;
|
|
192
|
+
declare function withAxiosDelegate<const Name extends string>(baseURL: string, name: Name, ...features: DelegateFeature[]): Feature<unknown, {
|
|
193
|
+
[key in Name]: HttpDelegate;
|
|
173
194
|
}>;
|
|
174
|
-
declare function withAxiosDelegate(
|
|
195
|
+
declare function withAxiosDelegate<const Name extends string>(baseURL: string, options: AxiosDelegateShortcutOptions & {
|
|
196
|
+
name: Name;
|
|
197
|
+
}, ...features: DelegateFeature[]): Feature<unknown, {
|
|
198
|
+
[key in Name]: HttpDelegate;
|
|
199
|
+
}>;
|
|
200
|
+
declare function withAxiosDelegate(baseURL: string, ...features: DelegateFeature[]): Feature<unknown, {
|
|
175
201
|
delegate: HttpDelegate;
|
|
176
202
|
}>;
|
|
177
|
-
declare function withBetterFetchDelegate<const
|
|
178
|
-
|
|
179
|
-
}
|
|
180
|
-
|
|
203
|
+
declare function withBetterFetchDelegate<const Name extends string>(baseURL: string, name: Name, ...features: DelegateFeature[]): Feature<unknown, {
|
|
204
|
+
[key in Name]: HttpDelegate;
|
|
205
|
+
}>;
|
|
206
|
+
declare function withBetterFetchDelegate<const Name extends string>(baseURL: string, options: BetterFetchDelegateShortcutOptions & {
|
|
207
|
+
name: Name;
|
|
208
|
+
}, ...features: DelegateFeature[]): Feature<unknown, {
|
|
209
|
+
[key in Name]: HttpDelegate;
|
|
181
210
|
}>;
|
|
182
|
-
declare function withBetterFetchDelegate(
|
|
211
|
+
declare function withBetterFetchDelegate(baseURL: string, ...features: DelegateFeature[]): Feature<unknown, {
|
|
183
212
|
delegate: HttpDelegate;
|
|
184
213
|
}>;
|
|
185
|
-
declare function withSseDelegate<const
|
|
186
|
-
|
|
187
|
-
}>(options: Options, ...features: DelegateFeature[]): Feature<unknown, {
|
|
188
|
-
[key in Options['name']]: ServerSentEventDelegate;
|
|
214
|
+
declare function withSseDelegate<const Name extends string>(baseURL: string, name: Name, ...features: DelegateFeature[]): Feature<unknown, {
|
|
215
|
+
[key in Name]: ServerSentEventDelegate;
|
|
189
216
|
}>;
|
|
190
|
-
declare function withSseDelegate(
|
|
217
|
+
declare function withSseDelegate(baseURL: string, ...features: DelegateFeature[]): Feature<unknown, {
|
|
191
218
|
delegate: ServerSentEventDelegate;
|
|
192
219
|
}>;
|
|
193
|
-
declare function withWebSocketDelegate<const
|
|
194
|
-
|
|
195
|
-
}
|
|
196
|
-
|
|
220
|
+
declare function withWebSocketDelegate<const Name extends string>(baseURL: string, name: Name, ...features: DelegateFeature[]): Feature<unknown, {
|
|
221
|
+
[key in Name]: WebSocketDelegate;
|
|
222
|
+
}>;
|
|
223
|
+
declare function withWebSocketDelegate<const Name extends string>(baseURL: string, options: WebSocketDelegateShortcutOptions & {
|
|
224
|
+
name: Name;
|
|
225
|
+
}, ...features: DelegateFeature[]): Feature<unknown, {
|
|
226
|
+
[key in Name]: WebSocketDelegate;
|
|
197
227
|
}>;
|
|
198
|
-
declare function withWebSocketDelegate(
|
|
228
|
+
declare function withWebSocketDelegate(baseURL: string, ...features: DelegateFeature[]): Feature<unknown, {
|
|
199
229
|
delegate: WebSocketDelegate;
|
|
200
230
|
}>;
|
|
201
231
|
|
|
@@ -234,14 +264,12 @@ declare function withEnvironments<Input, Environment extends Environments>(confi
|
|
|
234
264
|
environments: EnvironmentManager<Environment>;
|
|
235
265
|
}>;
|
|
236
266
|
|
|
237
|
-
|
|
238
|
-
name?: string;
|
|
239
|
-
}
|
|
267
|
+
type InterceptorConfig = HttpInterceptor | ServerSentEventInterceptor | WebSocketInterceptor;
|
|
240
268
|
/**
|
|
241
|
-
* Add
|
|
269
|
+
* Add interceptor to the delegate.
|
|
242
270
|
*
|
|
243
271
|
* @param config - The interceptor configuration.
|
|
244
|
-
* @returns A delegate feature that wraps the
|
|
272
|
+
* @returns A delegate feature that wraps the delegate with the interceptor.
|
|
245
273
|
*/
|
|
246
274
|
declare function withInterceptor(config: InterceptorConfig): DelegateFeature;
|
|
247
275
|
|
|
@@ -322,4 +350,4 @@ declare function universalClient<A, B, C, D, E, F, G, H, I>(featureA: Feature<un
|
|
|
322
350
|
declare function universalClient<A, B, C, D, E, F, G, H, I, J>(featureA: Feature<unknown, A>, featureB: Feature<A, B>, featureC: Feature<Merge<A, B>, C>, featureD: Feature<Merge<Merge<A, B>, C>, D>, featureE: Feature<Merge<Merge<Merge<A, B>, C>, D>, E>, featureF: Feature<Merge<Merge<Merge<Merge<A, B>, C>, D>, E>, F>, featureG: Feature<Merge<Merge<Merge<Merge<Merge<A, B>, C>, D>, E>, F>, G>, featureH: Feature<Merge<Merge<Merge<Merge<Merge<Merge<A, B>, C>, D>, E>, F>, G>, H>, featureI: Feature<Merge<Merge<Merge<Merge<Merge<Merge<Merge<A, B>, C>, D>, E>, F>, G>, H>, I>, featureJ: Feature<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<A, B>, C>, D>, E>, F>, G>, H>, I>, J>): Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<Merge<A, B>, C>, D>, E>, F>, G>, H>, I>, J>;
|
|
323
351
|
declare function universalClient(...withFeatures: Feature<unknown, unknown>[]): unknown;
|
|
324
352
|
|
|
325
|
-
export { type Delegate, type DelegateFeature, type Feature, type Hooks$1 as Hooks, type HooksInternal, type HttpDelegate, type HttpRequestOptions, type InterceptorConfig, type ServerSentEventDelegate, type SseOpenOptions, type WebSocketDelegate, universalClient, withAxiosDelegate, withBetterFetchDelegate, withDelegate, withEnvironments, withFetchDelegate, withHooks, withHttpDelegate, withInterceptor, withMethods, withOffline, withSseDelegate, withTelemetry, withWebSocketDelegate };
|
|
353
|
+
export { type Delegate, type DelegateFeature, type Feature, type Hooks$1 as Hooks, type HooksInternal, type HttpDelegate, type HttpInterceptor, type HttpRequestOptions, type InterceptorConfig, type RequestInterceptorContext, type ResponseInterceptorContext, type ServerSentEventDelegate, type ServerSentEventInterceptor, type SseOpenOptions, type WebSocketDelegate, type WebSocketInterceptor, universalClient, withAxiosDelegate, withBetterFetchDelegate, withDelegate, withEnvironments, withFetchDelegate, withHooks, withHttpDelegate, withInterceptor, withMethods, withOffline, withSseDelegate, withTelemetry, withWebSocketDelegate };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
'use strict';/**
|
|
2
|
-
* universal-client v0.
|
|
2
|
+
* universal-client v0.1.1
|
|
3
3
|
* (c) 2026 Kevin Bonnoron
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
6
|
-
var
|
|
7
|
-
`);if(g===""||g==="message")for(let i of
|
|
8
|
-
`);i=M.pop()??"";for(let S of M){let w=S.endsWith("\r")?S.slice(0,-1):S;if(w===""){p.length>0&&(l(c,p),c="",p=[]);continue}if(w.startsWith(":"))continue;let y=w.indexOf(":"),C,F;switch(y===-1?(C=w,F=""):(C=w.slice(0,y),F=w[y+1]===" "?w.slice(y+2):w.slice(y+1)),C){case "data":p.push(F);break;case "event":c=F;break}}}}catch(h){if(h instanceof DOMException&&h.name==="AbortError")return;let D=new Event("error");for(let M of n)M(D);}}return {open(g){u();let d=g?.url??t,f=g?.method??"GET";if(f==="GET"){if(typeof EventSource>"u")throw new Error("EventSource is not available. This delegate only works in browser environments.");o=new EventSource(d),o.addEventListener("open",i=>{for(let c of e)c(i);}),o.addEventListener("error",i=>{for(let c of n)c(i);}),o.addEventListener("message",i=>{for(let c of s)c(i.data);});for(let[i,c]of r)for(let p of c){let h=D=>p(D.data);o.addEventListener(i,h);}}else {a=new AbortController;let i={method:f,headers:{Accept:"text/event-stream","Content-Type":"application/json",...g?.headers},signal:a.signal};g?.body!==void 0&&(i.body=JSON.stringify(g.body)),fetch(d,i).then(c=>{if(!c.ok){let h=new Event("error");for(let D of n)D(h);return}if(!c.body){let h=new Event("error");for(let D of n)D(h);return}let p=new Event("open");for(let h of e)h(p);m(c.body);}).catch(c=>{if(c instanceof DOMException&&c.name==="AbortError")return;let p=new Event("error");for(let h of n)h(p);});}},close(){u();},onOpen(g){return e.add(g),()=>e.delete(g)},onError(g){return n.add(g),()=>n.delete(g)},onMessage(g){return s.add(g),()=>s.delete(g)},subscribe(g,d){let f=r.get(g);if(f||(f=new Set,r.set(g,f)),f.add(d),o){let i=c=>d(c.data);return o.addEventListener(g,i),()=>{f.delete(d),o&&o.removeEventListener(g,i);}}return ()=>{f.delete(d);}}}}function W({baseURL:t,protocols:e,reconnectInterval:s=5e3,maxReconnectAttempts:n=10}){let r=null,o=null,a=0,u=new Set,l=new Set,m=new Set,g=new Set;function d(){o!==null||a>=n||(a++,o=window.setTimeout(()=>{o=null,f.connect();},s));}let f={connect:()=>{r&&r.close(),r=new WebSocket(t,e),r.onopen=i=>{a=0;for(let c of u)c(i);},r.onclose=i=>{for(let c of m)c(i);d();},r.onerror=i=>{for(let c of g)c(i);},r.onmessage=i=>{let c;try{c=JSON.parse(i.data);}catch{c=i.data;}for(let p of l)p(c);};},close(){r&&(r.close(),r=null),o!==null&&(window.clearTimeout(o),o=null);},send(i){r&&r.readyState===WebSocket.OPEN?r.send(JSON.stringify(i)):console.warn("WebSocket is not connected. Message not sent.");},onOpen(i){return u.add(i),()=>u.delete(i)},onClose(i){return m.add(i),()=>m.delete(i)},onError(i){return g.add(i),()=>g.delete(i)},onMessage(i){return l.add(i),()=>l.delete(i)}};return f.connect(),f}function v(t){return typeof t=="object"&&t!==null&&"get"in t&&"post"in t&&"patch"in t&&"put"in t&&"delete"in t}function O(t){return typeof t=="object"&&t!==null&&"connect"in t&&"send"in t&&"close"in t}var K=t=>typeof t=="object"&&t!==null&&"type"in t;function L(t){if(K(t)){if(t.type==="http")return _(t);if(t.type==="websocket")return W(t);if(t.type==="server-sent-event")return z(t);throw new Error("Unsupported delegate type")}return t}function _(t){let e=null,s=()=>(e||(e=I(t)),e);return {async get(n,r){return (await s()).get(n,r)},async post(n,r,o){return (await s()).post(n,r,o)},async patch(n,r,o){return (await s()).patch(n,r,o)},async put(n,r,o){return (await s()).put(n,r,o)},async delete(n,r){return (await s()).delete(n,r)}}}function z(t){let e=null,s=()=>(e||(e=P(t)),e);return {open(n){s().then(r=>r.open(n)).catch(r=>console.error("[SSE] Failed to open connection:",r));},close(){e&&e.then(n=>n.close()).catch(console.error);},onOpen(n){let r=null;return s().then(o=>{r=o.onOpen(n);}).catch(o=>console.error("[SSE] Failed to attach onOpen handler:",o)),()=>{r&&r();}},onError(n){let r=null;return s().then(o=>{r=o.onError(n);}).catch(o=>console.error("[SSE] Failed to attach onError handler:",o)),()=>{r&&r();}},onMessage(n){let r=null;return s().then(o=>{r=o.onMessage(n);}).catch(o=>console.error("[SSE] Failed to attach onMessage handler:",o)),()=>{r&&r();}},subscribe(n,r){let o=null;return s().then(a=>{o=a.subscribe(n,r);}).catch(a=>console.error(`[SSE] Failed to subscribe to event "${n}":`,a)),()=>{o&&o();}}}}function k(t,e){let s=n=>{let r=t[n];return async(o,a,u)=>{let l=n==="get"||n==="delete",m=l?void 0:a,g=l?a:u,d={method:n,url:o,headers:g?.headers??{},body:m};try{if(e.before){let c=await e.before(d);c&&typeof c=="object"&&(d={...d,...c});}let f={...g,headers:d.headers},i=l?await r(d.url,f):await r(d.url,d.body,f);if(e.after){let c=await e.after({method:n,url:d.url,response:i});c&&typeof c=="object"&&(i={...i,...c});}return i}catch(f){throw e.error&&await e.error(n,d.url,f,d.body),f}}};return {get:s("get"),post:s("post"),patch:s("patch"),put:s("put"),delete:s("delete")}}function $(t,e){return {...t,connect:()=>{e.beforeConnect&&Promise.resolve(e.beforeConnect()).catch(console.error),t.connect(),e.afterConnect&&Promise.resolve(e.afterConnect()).catch(console.error);},send:s=>{e.beforeSend&&Promise.resolve(e.beforeSend(s)).catch(console.error),t.send(s),e.afterSend&&Promise.resolve(e.afterSend(s)).catch(console.error);},close:()=>{e.beforeClose&&Promise.resolve(e.beforeClose()).catch(console.error),t.close(),e.afterClose&&Promise.resolve(e.afterClose()).catch(console.error);}}}function E(t,...e){return s=>{let{delegate:n,...r}=e.reduce((a,u)=>{let l=u(a);return Object.assign({},a,l)},{...s,delegate:L(t)});return {[t.name??"delegate"]:n,...r}}}function Oe(t,...e){return E(t,...e)}function Se(t,...e){return E({...t,type:"http"},...e)}function Ce(t,...e){return E({...t,type:"http"},...e)}function be(t,...e){return E({...t,type:"http",impl:"axios"},...e)}function xe(t,...e){return E({...t,type:"http",impl:"better-fetch"},...e)}function Te(t,...e){return E({...t,type:"server-sent-event"},...e)}function Ae(t,...e){return E({...t,type:"websocket"},...e)}function Be(t){return ()=>({...t})}function Ie(t){return e=>({...t(e),...e})}function V(t){let e=Object.keys(t.environments),s=t.default||e[0],n=s;function r(){return n}function o(u){if(t.environments[u])n=u,console.info(`[ENVIRONMENTS] Switched to environment: ${u} (${t.environments[u]})`);else {let l=t.fallback||s;console.warn(`[ENVIRONMENTS] Environment "${u}" not found, falling back to "${String(l)}"`),n=l;}}function a(){return t.environments[n]||t.environments[Object.keys(t.environments)[0]]}return {getCurrentEnvironment:r,setEnvironment:o,getBaseURL:a}}function X(t,e){let s=n=>{let r=e.getBaseURL();if(n.startsWith("http"))return n;let o=r.replace(/\/$/,""),a=n.startsWith("/")?n:`/${n}`,u=`${o}${a}`;return console.info(`[ENVIRONMENTS] Building URL: ${o} + ${a} = ${u}`),u};return k(t,{before:n=>({url:s(n.url)})})}function Y(t,e){return $(t,{beforeConnect:()=>{console.info(`[ENVIRONMENTS] WebSocket connecting to: ${e.getBaseURL()}`);}})}function Q(t,e){return v(t)?X(t,e):O(t)?Y(t,e):t}function Le(t){return e=>{let{name:s}=t,n=V(t);return {...e,[s]:Q(e[s],n),environments:n}}}function Ne(t){return ({delegate:e,...s})=>{let{name:n,...r}=t;return v(e)?{...s,delegate:k(e,r)}:{...s,delegate:e}}}function Z(){return typeof navigator<"u"&&navigator.onLine!==void 0?navigator.onLine:true}function ee(t){let e=new Map,s=t.cacheTTL??3e5;function n(a,u,l){let m=l?JSON.stringify(l):"";return `${a}:${u}:${m}`}function r(a){let u=e.get(a);return u?Date.now()-u.timestamp>u.ttl?(e.delete(a),null):u.data:null}function o(a,u){e.set(a,{data:u,timestamp:Date.now(),ttl:s});}return {isOnline:Z,clearCache:()=>e.clear(),getCacheSize:()=>e.size,getFromCache:r,setInCache:o,generateCacheKey:n}}function te(t,e,s){let n=s.strategy??"cache-first",r=o=>{let a=t[o];return async(u,l,m)=>{let g=o==="get"||o==="delete",d=g?void 0:l,f=g?l:m,i=e.generateCacheKey(String(o),u,d);if(n==="cache-first"){let c=e.getFromCache(i);if(c)return console.info(`[OFFLINE] Cache hit for ${o} ${u}`),c;if(e.isOnline())try{let p=g?await a(u,f):await a(u,d,f);return e.setInCache(i,p),p}catch(p){throw console.error(`[OFFLINE] Network error for ${o} ${u}:`,p),p}throw new Error(`[OFFLINE] No cache available and offline for ${o} ${u}`)}if(n==="network-first"){if(e.isOnline())try{let p=g?await a(u,f):await a(u,d,f);return e.setInCache(i,p),p}catch(p){let h=e.getFromCache(i);if(h)return console.warn(`[OFFLINE] Network failed, using cache for ${o} ${u}`),h;throw p}let c=e.getFromCache(i);if(c)return c;throw new Error(`[OFFLINE] Offline and no cache for ${o} ${u}`)}if(n==="network-only"){if(!e.isOnline())throw new Error(`[OFFLINE] Network unavailable for ${o} ${u}`);return g?await a(u,f):await a(u,d,f)}throw new Error(`[OFFLINE] Unknown strategy: ${n}`)}};return {get:r("get"),post:r("post"),patch:r("patch"),put:r("put"),delete:r("delete")}}function ne(t,e,s){return v(t)?te(t,e,s):t}function je(t={}){return ({delegate:e,...s})=>{let n=ee(t);return {...s,delegate:ne(e,n,t),offline:n}}}function re({enableMetrics:t=false,enableTracing:e=false,enableLogging:s=false,onEvent:n=()=>{},onMetrics:r=()=>{}}){let o=[],a={requests:{total:0,success:0,errors:0},latency:{avg:0,min:Number.MAX_SAFE_INTEGER,max:Number.MIN_SAFE_INTEGER}};function u(i){o.push(i),g(i),s&&f(i),n(i);}function l(){return e?Math.random().toString(36).substring(2,15):void 0}function m(){return {requests:t?a.requests:{total:0,success:0,errors:0},latency:t?a.latency:{avg:0,min:0,max:0}}}function g(i){if(t){switch(i.type){case "request":a.requests.total++;break;case "response":a.requests.success++,i.duration&&d(i.duration);break;case "error":a.requests.errors++,i.duration&&d(i.duration);break}r(a);}}function d(i){let c=o.filter(p=>p.duration!==void 0).map(p=>p.duration).filter(p=>p>0);c.length>0&&(a.latency.avg=c.reduce((p,h)=>p+h,0)/c.length,a.latency.min=Math.min(...c),a.latency.max=Math.max(...c));}function f(i){let c=i.type==="error"?"error":"info",p=`[TELEMETRY] ${i.operation} ${i.duration?`(${i.duration}ms)`:""}`;i.type==="error"&&i.error?console[c](p,i.error):console[c](p);}return {recordEvent:u,generateTraceId:l,getMetrics:m}}function oe(t){let e=new Map;return {before:s=>{let n=t.generateTraceId(),r=`${s.method.toUpperCase()} ${s.url}`,o=Date.now();e.set(r,{time:o,traceId:n}),t.recordEvent({timestamp:o,type:"request",operation:r,traceId:n});},after:s=>{let n=`${s.method.toUpperCase()} ${s.url}`,r=e.get(n);if(r){let o=Date.now()-r.time;t.recordEvent({timestamp:Date.now(),type:"response",operation:n,duration:o,status:200,traceId:r.traceId}),e.delete(n);}return s.response},error:(s,n,r)=>{let o=`${s.toUpperCase()} ${n}`,a=e.get(o);if(a){let u=Date.now()-a.time;t.recordEvent({timestamp:Date.now(),type:"error",operation:o,duration:u,error:r,traceId:a.traceId}),e.delete(o);}}}}function se(t,e){let s=n=>{let r=e.generateTraceId(),o=Date.now();return e.recordEvent({timestamp:o,type:"request",operation:n,traceId:r}),{success:()=>{let a=Date.now()-o;e.recordEvent({timestamp:Date.now(),type:"response",operation:n,duration:a,status:200,traceId:r});},error:a=>{let u=Date.now()-o;e.recordEvent({timestamp:Date.now(),type:"error",operation:n,duration:u,error:a,traceId:r});}}};return {...t,connect:()=>{let n=s("connect");try{t.connect(),n.success();}catch(r){throw n.error(r),r}},send:n=>{let r=s("send");try{t.send(n),r.success();}catch(o){throw r.error(o),o}},close:()=>{let n=s("close");try{t.close(),n.success();}catch(r){throw n.error(r),r}},onMessage:n=>{let r=s("onMessage");try{let o=t.onMessage(n);return r.success(),o}catch(o){throw r.error(o),o}},onError:n=>t.onError(r=>{s("onError").error(new Error("WebSocket error")),n(r);})}}function ae(t,e){return v(t)?k(t,oe(e)):O(t)?se(t,e):t}function _e(t={}){return ({delegate:e,...s})=>{let n=re(t);return {...s,delegate:ae(e,n),telemetry:n}}}function it(...t){let e=t.reduce((r,o)=>Object.assign(r,o(r)),{}),{onInit:s,...n}=e;return typeof s=="function"&&s(n),n}exports.universalClient=it;exports.withAxiosDelegate=be;exports.withBetterFetchDelegate=xe;exports.withDelegate=Oe;exports.withEnvironments=Le;exports.withFetchDelegate=Ce;exports.withHooks=Be;exports.withHttpDelegate=Se;exports.withInterceptor=Ne;exports.withMethods=Ie;exports.withOffline=je;exports.withSseDelegate=Te;exports.withTelemetry=_e;exports.withWebSocketDelegate=Ae;//# sourceMappingURL=index.js.map
|
|
6
|
+
var G=Object.defineProperty;var T=(t,e)=>()=>(t&&(e=t(t=0)),e);var A=(t,e)=>{for(var o in e)G(t,o,{get:e[o],enumerable:true});};var B={};A(B,{createAxiosDelegate:()=>z});async function z(t){let e;try{e=await new Function("moduleName","return import(moduleName)")("axios");}catch{e=null;}if(!e)throw new Error("Axios is not installed. Please install axios to use the axios delegate.");let o=e.default,{baseURL:n,...r}=t,s=o.create({baseURL:n,...r});return {get(a,u){return s.get(a,{headers:u?.headers}).then(l=>l.data)},post(a,u,l){return s.post(a,u,{headers:l?.headers}).then(m=>m.data)},patch(a,u,l){return s.patch(a,u,{headers:l?.headers}).then(m=>m.data)},put(a,u,l){return s.put(a,u,{headers:l?.headers}).then(m=>m.data)},delete(a,u){return s.delete(a,{headers:u?.headers}).then(l=>l.data)}}}var H=T(()=>{});var I={};A(I,{createBetterFetchDelegate:()=>_});async function _(t){let e;try{e=await new Function("moduleName","return import(moduleName)")("@better-fetch/fetch");}catch{e=null;}if(!e)throw new Error("@better-fetch/fetch is not installed. Please install @better-fetch/fetch to use the better-fetch delegate.");let{createFetch:o}=e,{baseURL:n,...r}=t,s=o({baseURL:n,...r});return {get(a,u){return s(a,{headers:u?.headers}).then(l=>l.data)},post(a,u,l){return s(a,{method:"POST",body:u,headers:l?.headers}).then(m=>m.data)},patch(a,u,l){return s(a,{method:"PATCH",body:u,headers:l?.headers}).then(m=>m.data)},put(a,u,l){return s(a,{method:"PUT",body:u,headers:l?.headers}).then(m=>m.data)},delete(a,u){return s(a,{method:"DELETE",headers:u?.headers}).then(l=>l.data)}}}var L=T(()=>{});function j(t){let e=t.headers.get("Content-Type")??"";return e.includes("application/json")?"json":e.startsWith("text/")||e.includes("application/xml")||e.includes("application/javascript")?"text":e.startsWith("image/")||e.startsWith("audio/")||e.startsWith("video/")||e.startsWith("application/")?"blob":"raw"}function J(t){return async e=>{if(!e.ok){let n=`HTTP ${e.status}: ${e.statusText}`;try{let r=await e.clone().json();r.error?n=r.error:r.message&&(n=r.message);}catch{}throw new Error(n)}let o=t??j(e);return o==="json"?e.json():o==="text"?e.text():o==="blob"?e.blob():e}}function K(t){let e=new URLSearchParams;for(let[o,n]of Object.entries(t??{}))if(n!=null)if(n instanceof Date)e.append(o,n.toISOString());else if(Array.isArray(n))for(let r of n)e.append(`${o}[]`,r?.toString()??"");else e.append(o,n.toString());return e.size?`?${e}`:""}function R({baseURL:t}){function e(o,n,r,{params:s={},headers:a={},format:u,signal:l}={}){let m={method:o,headers:{...a}};return l&&(m.signal=l),r!=null&&(r instanceof FormData||r instanceof Blob||r instanceof ArrayBuffer||r instanceof URLSearchParams||r instanceof ReadableStream||typeof r=="string"?m.body=r:(m.body=JSON.stringify(r),m.headers["Content-Type"]??="application/json")),fetch(`${t}${n}${K(s)}`,m).then(J(u))}return {get(o,n){return e("GET",o,void 0,n)},post(o,n,r){return e("POST",o,n,r)},patch(o,n,r){return e("PATCH",o,n,r)},put(o,n,r){return e("PUT",o,n,r)},delete(o,n){return e("DELETE",o,void 0,n)}}}async function P(t){if(t.impl==="axios"){let{createAxiosDelegate:e}=await Promise.resolve().then(()=>(H(),B));return e(t)}if(t.impl==="better-fetch"){let{createBetterFetchDelegate:e}=await Promise.resolve().then(()=>(L(),I));return e(t)}return R(t)}async function W({baseURL:t}){let e=new Set,o=new Set,n=new Set,r=new Map,s=null,a=null;function u(){s&&(s.close(),s=null),a&&(a.abort(),a=null);}function l(g,d){let f=d.join(`
|
|
7
|
+
`);if(g===""||g==="message")for(let i of o)i(f);if(g!==""&&g!=="message"){let i=r.get(g);if(i)for(let c of i)c(f);}}async function m(g){let d=g.getReader(),f=new TextDecoder,i="",c="",p=[];try{for(;;){let{done:D,value:w}=await d.read();if(D){p.length>0&&l(c,p);break}i+=f.decode(w,{stream:!0});let S=i.split(`
|
|
8
|
+
`);i=S.pop()??"";for(let O of S){let h=O.endsWith("\r")?O.slice(0,-1):O;if(h===""){p.length>0&&(l(c,p),c="",p=[]);continue}if(h.startsWith(":"))continue;let F=h.indexOf(":"),x,b;switch(F===-1?(x=h,b=""):(x=h.slice(0,F),b=h[F+1]===" "?h.slice(F+2):h.slice(F+1)),x){case "data":p.push(b);break;case "event":c=b;break}}}}catch(D){if(D instanceof DOMException&&D.name==="AbortError")return;let w=new Event("error");for(let S of n)S(w);}}return {open(g){u();let d=g?.url??t,f=g?.method??"GET";if(f==="GET"){if(typeof EventSource>"u")throw new Error("EventSource is not available. This delegate only works in browser environments.");s=new EventSource(d),s.addEventListener("open",i=>{for(let c of e)c(i);}),s.addEventListener("error",i=>{for(let c of n)c(i);}),s.addEventListener("message",i=>{for(let c of o)c(i.data);});for(let[i,c]of r)for(let p of c){let D=w=>p(w.data);s.addEventListener(i,D);}}else {a=new AbortController;let i={method:f,headers:{Accept:"text/event-stream","Content-Type":"application/json",...g?.headers},signal:a.signal};g?.body!==void 0&&(i.body=JSON.stringify(g.body)),fetch(d,i).then(c=>{if(!c.ok){let D=new Event("error");for(let w of n)w(D);return}if(!c.body){let D=new Event("error");for(let w of n)w(D);return}let p=new Event("open");for(let D of e)D(p);m(c.body);}).catch(c=>{if(c instanceof DOMException&&c.name==="AbortError")return;let p=new Event("error");for(let D of n)D(p);});}},close(){u();},onOpen(g){return e.add(g),()=>e.delete(g)},onError(g){return n.add(g),()=>n.delete(g)},onMessage(g){return o.add(g),()=>o.delete(g)},subscribe(g,d){let f=r.get(g);if(f||(f=new Set,r.set(g,f)),f.add(d),s){let i=c=>d(c.data);return s.addEventListener(g,i),()=>{f.delete(d),s&&s.removeEventListener(g,i);}}return ()=>{f.delete(d);}}}}function N({baseURL:t,protocols:e,reconnectInterval:o=5e3,maxReconnectAttempts:n=10}){let r=null,s=null,a=0,u=new Set,l=new Set,m=new Set,g=new Set;function d(){s!==null||a>=n||(a++,s=window.setTimeout(()=>{s=null,f.connect();},o));}let f={connect:()=>{r&&r.close(),r=new WebSocket(t,e),r.onopen=i=>{a=0;for(let c of u)c(i);},r.onclose=i=>{for(let c of m)c(i);d();},r.onerror=i=>{for(let c of g)c(i);},r.onmessage=i=>{let c;try{c=JSON.parse(i.data);}catch{c=i.data;}for(let p of l)p(c);};},close(){r&&(r.close(),r=null),s!==null&&(window.clearTimeout(s),s=null);},send(i){r&&r.readyState===WebSocket.OPEN?r.send(JSON.stringify(i)):console.warn("WebSocket is not connected. Message not sent.");},onOpen(i){return u.add(i),()=>u.delete(i)},onClose(i){return m.add(i),()=>m.delete(i)},onError(i){return g.add(i),()=>g.delete(i)},onMessage(i){return l.add(i),()=>l.delete(i)}};return f.connect(),f}function v(t){return typeof t=="object"&&t!==null&&"get"in t&&"post"in t&&"patch"in t&&"put"in t&&"delete"in t}function k(t){return typeof t=="object"&&t!==null&&"connect"in t&&"send"in t&&"close"in t}function $(t){return typeof t=="object"&&t!==null&&"open"in t&&"onMessage"in t&&"onError"in t&&"onOpen"in t&&"subscribe"in t}var V=t=>typeof t=="object"&&t!==null&&"type"in t;function q(t){if(V(t)){if(t.type==="http")return X(t);if(t.type==="websocket")return N(t);if(t.type==="server-sent-event")return Y(t);throw new Error("Unsupported delegate type")}return t}function X(t){let e=null,o=()=>(e||(e=P(t)),e);return {async get(n,r){return (await o()).get(n,r)},async post(n,r,s){return (await o()).post(n,r,s)},async patch(n,r,s){return (await o()).patch(n,r,s)},async put(n,r,s){return (await o()).put(n,r,s)},async delete(n,r){return (await o()).delete(n,r)}}}function Y(t){let e=null,o=()=>(e||(e=W(t)),e);return {open(n){o().then(r=>r.open(n)).catch(r=>console.error("[SSE] Failed to open connection:",r));},close(){e&&e.then(n=>n.close()).catch(console.error);},onOpen(n){let r=null;return o().then(s=>{r=s.onOpen(n);}).catch(s=>console.error("[SSE] Failed to attach onOpen handler:",s)),()=>{r&&r();}},onError(n){let r=null;return o().then(s=>{r=s.onError(n);}).catch(s=>console.error("[SSE] Failed to attach onError handler:",s)),()=>{r&&r();}},onMessage(n){let r=null;return o().then(s=>{r=s.onMessage(n);}).catch(s=>console.error("[SSE] Failed to attach onMessage handler:",s)),()=>{r&&r();}},subscribe(n,r){let s=null;return o().then(a=>{s=a.subscribe(n,r);}).catch(a=>console.error(`[SSE] Failed to subscribe to event "${n}":`,a)),()=>{s&&s();}}}}function y(t,e){let o=n=>{let r=t[n];return async(s,a,u)=>{let l=n==="get"||n==="delete",m=l?void 0:a,g=l?a:u,d={method:n,url:s,headers:g?.headers??{},body:m};try{if(e.onBeforeRequest){let c=await e.onBeforeRequest(d);c&&typeof c=="object"&&(d={...d,...c});}let f={...g,headers:d.headers},i=l?await r(d.url,f):await r(d.url,d.body,f);if(e.onAfterResponse){let c=await e.onAfterResponse({method:n,url:d.url,response:i});c&&typeof c=="object"&&(i={...i,...c});}return i}catch(f){throw e.onError&&await e.onError(n,d.url,f,d.body),f}}};return {get:o("get"),post:o("post"),patch:o("patch"),put:o("put"),delete:o("delete")}}function C(t,e){return {...t,connect:()=>{e.onBeforeConnect&&Promise.resolve(e.onBeforeConnect()).catch(console.error),t.connect(),e.onAfterConnect&&Promise.resolve(e.onAfterConnect()).catch(console.error);},send:o=>{e.onBeforeSend&&Promise.resolve(e.onBeforeSend(o)).catch(console.error),t.send(o),e.onAfterSend&&Promise.resolve(e.onAfterSend(o)).catch(console.error);},close:()=>{e.onBeforeClose&&Promise.resolve(e.onBeforeClose()).catch(console.error),t.close(),e.onAfterClose&&Promise.resolve(e.onAfterClose()).catch(console.error);}}}function U(t,e){return {...t,open:async o=>{let n=o;if(e.onBeforeOpen){let r=await e.onBeforeOpen(n);r&&typeof r=="object"&&(n={...n,...r});}t.open(n),e.onAfterOpen&&Promise.resolve(e.onAfterOpen(n)).catch(console.error);},close:()=>{e.onBeforeClose&&Promise.resolve(e.onBeforeClose()).catch(console.error),t.close(),e.onAfterClose&&Promise.resolve(e.onAfterClose()).catch(console.error);},onError:o=>t.onError(n=>{e.onError&&Promise.resolve(e.onError(new Error(n.type))).catch(console.error),o(n);}),onMessage:o=>t.onMessage(n=>{e.onMessage&&Promise.resolve(e.onMessage(n)).catch(console.error),o(n);})}}function E(t,...e){return o=>{let{delegate:n,...r}=e.reduce((a,u)=>{let l=u(a);return Object.assign({},a,l)},{...o,delegate:q(t)});return {[t.name??"delegate"]:n,...r}}}function Oe(t,...e){return E(t,...e)}function M(t,e){return typeof t=="string"?{options:{name:t},allFeatures:e}:typeof t=="function"?{options:{},allFeatures:[t,...e]}:t!==void 0?{options:t,allFeatures:e}:{options:{},allFeatures:e}}function xe(t,e,...o){return E({...e,baseURL:t,type:"http"},...o)}function Te(t,e,...o){let{options:n,allFeatures:r}=M(e,o);return E({...n,baseURL:t,type:"http"},...r)}function Ae(t,e,...o){let{options:n,allFeatures:r}=M(e,o);return E({...n,baseURL:t,type:"http",impl:"axios"},...r)}function Re(t,e,...o){let{options:n,allFeatures:r}=M(e,o);return E({...n,baseURL:t,type:"http",impl:"better-fetch"},...r)}function Be(t,e,...o){let{options:n,allFeatures:r}=M(e,o);return E({...n,baseURL:t,type:"server-sent-event"},...r)}function He(t,e,...o){let{options:n,allFeatures:r}=M(e,o);return E({...n,baseURL:t,type:"websocket"},...r)}function Le(t){return ()=>({...t})}function We(t){return e=>({...t(e),...e})}function Q(t){let e=Object.keys(t.environments),o=t.default||e[0],n=o;function r(){return n}function s(u){if(t.environments[u])n=u,console.info(`[ENVIRONMENTS] Switched to environment: ${u} (${t.environments[u]})`);else {let l=t.fallback||o;console.warn(`[ENVIRONMENTS] Environment "${u}" not found, falling back to "${String(l)}"`),n=l;}}function a(){return t.environments[n]||t.environments[Object.keys(t.environments)[0]]}return {getCurrentEnvironment:r,setEnvironment:s,getBaseURL:a}}function Z(t,e){let o=n=>{let r=e.getBaseURL();if(n.startsWith("http"))return n;let s=r.replace(/\/$/,""),a=n.startsWith("/")?n:`/${n}`,u=`${s}${a}`;return console.info(`[ENVIRONMENTS] Building URL: ${s} + ${a} = ${u}`),u};return y(t,{onBeforeRequest:n=>({url:o(n.url)})})}function ee(t,e){return C(t,{onBeforeConnect:()=>{console.info(`[ENVIRONMENTS] WebSocket connecting to: ${e.getBaseURL()}`);}})}function te(t,e){return v(t)?Z(t,e):k(t)?ee(t,e):t}function qe(t){return e=>{let{name:o}=t,n=Q(t);return {...e,[o]:te(e[o],n),environments:n}}}function je(t){return ({delegate:e,...o})=>v(e)?{...o,delegate:y(e,t)}:$(e)?{...o,delegate:U(e,t)}:k(e)?{...o,delegate:C(e,t)}:{...o,delegate:e}}function ne(){return typeof navigator<"u"&&navigator.onLine!==void 0?navigator.onLine:true}function re(t){let e=new Map,o=t.cacheTTL??3e5;function n(a,u,l){let m=l?JSON.stringify(l):"";return `${a}:${u}:${m}`}function r(a){let u=e.get(a);return u?Date.now()-u.timestamp>u.ttl?(e.delete(a),null):u.data:null}function s(a,u){e.set(a,{data:u,timestamp:Date.now(),ttl:o});}return {isOnline:ne,clearCache:()=>e.clear(),getCacheSize:()=>e.size,getFromCache:r,setInCache:s,generateCacheKey:n}}function oe(t,e,o){let n=o.strategy??"cache-first",r=s=>{let a=t[s];return async(u,l,m)=>{let g=s==="get"||s==="delete",d=g?void 0:l,f=g?l:m,i=e.generateCacheKey(String(s),u,d);if(n==="cache-first"){let c=e.getFromCache(i);if(c)return console.info(`[OFFLINE] Cache hit for ${s} ${u}`),c;if(e.isOnline())try{let p=g?await a(u,f):await a(u,d,f);return e.setInCache(i,p),p}catch(p){throw console.error(`[OFFLINE] Network error for ${s} ${u}:`,p),p}throw new Error(`[OFFLINE] No cache available and offline for ${s} ${u}`)}if(n==="network-first"){if(e.isOnline())try{let p=g?await a(u,f):await a(u,d,f);return e.setInCache(i,p),p}catch(p){let D=e.getFromCache(i);if(D)return console.warn(`[OFFLINE] Network failed, using cache for ${s} ${u}`),D;throw p}let c=e.getFromCache(i);if(c)return c;throw new Error(`[OFFLINE] Offline and no cache for ${s} ${u}`)}if(n==="network-only"){if(!e.isOnline())throw new Error(`[OFFLINE] Network unavailable for ${s} ${u}`);return g?await a(u,f):await a(u,d,f)}throw new Error(`[OFFLINE] Unknown strategy: ${n}`)}};return {get:r("get"),post:r("post"),patch:r("patch"),put:r("put"),delete:r("delete")}}function se(t,e,o){return v(t)?oe(t,e,o):t}function ze(t={}){return ({delegate:e,...o})=>{let n=re(t);return {...o,delegate:se(e,n,t),offline:n}}}function ae({enableMetrics:t=false,enableTracing:e=false,enableLogging:o=false,onEvent:n=()=>{},onMetrics:r=()=>{}}){let s=[],a={requests:{total:0,success:0,errors:0},latency:{avg:0,min:Number.MAX_SAFE_INTEGER,max:Number.MIN_SAFE_INTEGER}};function u(i){s.push(i),g(i),o&&f(i),n(i);}function l(){return e?Math.random().toString(36).substring(2,15):void 0}function m(){return {requests:t?a.requests:{total:0,success:0,errors:0},latency:t?a.latency:{avg:0,min:0,max:0}}}function g(i){if(t){switch(i.type){case "request":a.requests.total++;break;case "response":a.requests.success++,i.duration&&d(i.duration);break;case "error":a.requests.errors++,i.duration&&d(i.duration);break}r(a);}}function d(i){let c=s.filter(p=>p.duration!==void 0).map(p=>p.duration).filter(p=>p>0);c.length>0&&(a.latency.avg=c.reduce((p,D)=>p+D,0)/c.length,a.latency.min=Math.min(...c),a.latency.max=Math.max(...c));}function f(i){let c=i.type==="error"?"error":"info",p=`[TELEMETRY] ${i.operation} ${i.duration?`(${i.duration}ms)`:""}`;i.type==="error"&&i.error?console[c](p,i.error):console[c](p);}return {recordEvent:u,generateTraceId:l,getMetrics:m}}function ie(t){let e=new Map;return {onBeforeRequest:o=>{let n=t.generateTraceId(),r=`${o.method.toUpperCase()} ${o.url}`,s=Date.now();e.set(r,{time:s,traceId:n}),t.recordEvent({timestamp:s,type:"request",operation:r,traceId:n});},onAfterResponse:o=>{let n=`${o.method.toUpperCase()} ${o.url}`,r=e.get(n);if(r){let s=Date.now()-r.time;t.recordEvent({timestamp:Date.now(),type:"response",operation:n,duration:s,status:200,traceId:r.traceId}),e.delete(n);}return o.response},onError:(o,n,r)=>{let s=`${o.toUpperCase()} ${n}`,a=e.get(s);if(a){let u=Date.now()-a.time;t.recordEvent({timestamp:Date.now(),type:"error",operation:s,duration:u,error:r,traceId:a.traceId}),e.delete(s);}}}}function ue(t,e){let o=n=>{let r=e.generateTraceId(),s=Date.now();return e.recordEvent({timestamp:s,type:"request",operation:n,traceId:r}),{success:()=>{let a=Date.now()-s;e.recordEvent({timestamp:Date.now(),type:"response",operation:n,duration:a,status:200,traceId:r});},error:a=>{let u=Date.now()-s;e.recordEvent({timestamp:Date.now(),type:"error",operation:n,duration:u,error:a,traceId:r});}}};return {...t,connect:()=>{let n=o("connect");try{t.connect(),n.success();}catch(r){throw n.error(r),r}},send:n=>{let r=o("send");try{t.send(n),r.success();}catch(s){throw r.error(s),s}},close:()=>{let n=o("close");try{t.close(),n.success();}catch(r){throw n.error(r),r}},onMessage:n=>{let r=o("onMessage");try{let s=t.onMessage(n);return r.success(),s}catch(s){throw r.error(s),s}},onError:n=>t.onError(r=>{o("onError").error(new Error("WebSocket error")),n(r);})}}function ce(t,e){return v(t)?y(t,ie(e)):k(t)?ue(t,e):t}function Xe(t={}){return ({delegate:e,...o})=>{let n=ae(t);return {...o,delegate:ce(e,n),telemetry:n}}}function gt(...t){let e=t.reduce((r,s)=>Object.assign(r,s(r)),{}),{onInit:o,...n}=e;return typeof o=="function"&&o(n),n}exports.universalClient=gt;exports.withAxiosDelegate=Ae;exports.withBetterFetchDelegate=Re;exports.withDelegate=Oe;exports.withEnvironments=qe;exports.withFetchDelegate=Te;exports.withHooks=Le;exports.withHttpDelegate=xe;exports.withInterceptor=je;exports.withMethods=We;exports.withOffline=ze;exports.withSseDelegate=Be;exports.withTelemetry=Xe;exports.withWebSocketDelegate=He;//# sourceMappingURL=index.js.map
|
|
9
9
|
//# sourceMappingURL=index.js.map
|