@tahanabavi/typefetch 1.3.0 → 1.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1038 -212
- package/dist/index.d.mts +293 -96
- package/dist/index.d.ts +293 -96
- package/dist/index.js +596 -4332
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +561 -4316
- package/dist/index.mjs.map +1 -1
- package/package.json +49 -15
package/dist/index.d.mts
CHANGED
|
@@ -1,54 +1,198 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
1
|
+
import z$1, { z } from 'zod';
|
|
2
2
|
|
|
3
|
+
type EncryptionMethod = "AES" | "DES" | "RSA" | "Base64" | "Custom";
|
|
4
|
+
type Method = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
3
5
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
6
|
+
* DeepEncryptionMap<T>
|
|
7
|
+
* --------------------
|
|
8
|
+
* Recursively describes which fields should be encrypted/decrypted.
|
|
9
|
+
* Supports:
|
|
10
|
+
* - Primitive fields → boolean | method
|
|
11
|
+
* - Objects → recursively typed maps
|
|
12
|
+
* - Arrays → mapping applies to element type (U)
|
|
13
|
+
* - Array-map override (optional but supported)
|
|
14
|
+
*/
|
|
15
|
+
type DeepEncryptionMap = boolean | EncryptionMethod | {
|
|
16
|
+
[key: string]: DeepEncryptionMap;
|
|
17
|
+
} | DeepEncryptionMap[];
|
|
18
|
+
/**
|
|
19
|
+
* EncryptionConfig
|
|
20
|
+
* ================
|
|
21
|
+
* Defines the encryption/decryption strategy for an endpoint.
|
|
22
|
+
* Both request and response maps are strictly typed based on their respective Zod schemas.
|
|
23
|
+
*/
|
|
24
|
+
type EncryptionConfig<TReq, TRes> = {
|
|
25
|
+
method: EncryptionMethod | {
|
|
26
|
+
request?: EncryptionMethod;
|
|
27
|
+
response?: EncryptionMethod;
|
|
28
|
+
};
|
|
29
|
+
/** Map of request fields to encrypt before sending to the server */
|
|
30
|
+
request?: DeepEncryptionMap;
|
|
31
|
+
/** Map of response fields to decrypt after receiving from the server */
|
|
32
|
+
response?: DeepEncryptionMap;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Base types for Zod schemas representing request and response structures.
|
|
36
|
+
* These are abstract—each concrete endpoint will define its own Zod object for these.
|
|
11
37
|
*/
|
|
12
38
|
type RequestSchema = z.ZodTypeAny;
|
|
13
39
|
type ResponseSchema = z.ZodTypeAny;
|
|
40
|
+
/**
|
|
41
|
+
* EndpointDef
|
|
42
|
+
* ============
|
|
43
|
+
* Defines the structure of a **single API endpoint**, including:
|
|
44
|
+
* - HTTP method and path
|
|
45
|
+
* - request/response validation schemas
|
|
46
|
+
* - optional authentication requirement
|
|
47
|
+
* - optional mock or static mock data
|
|
48
|
+
* - optional custom headers and body format
|
|
49
|
+
*/
|
|
14
50
|
type EndpointDef<TReq extends RequestSchema, TRes extends ResponseSchema> = {
|
|
15
|
-
|
|
51
|
+
/** HTTP method used by this endpoint */
|
|
52
|
+
method: Method;
|
|
53
|
+
/** URL path for this endpoint, e.g. "/users/:id" */
|
|
16
54
|
path: string;
|
|
55
|
+
/** Whether this endpoint requires an Authorization token */
|
|
17
56
|
auth?: boolean;
|
|
57
|
+
/** Zod schema describing the expected request structure */
|
|
18
58
|
request: TReq;
|
|
59
|
+
/** Zod schema describing the expected response structure */
|
|
19
60
|
response: TRes;
|
|
61
|
+
/**
|
|
62
|
+
* Mock data support — enables quick testing or local dev mode:
|
|
63
|
+
* - Either a function returning a mock response object
|
|
64
|
+
* - Or a static mock response object
|
|
65
|
+
*/
|
|
20
66
|
mockData?: (() => z.infer<TRes>) | z.infer<TRes>;
|
|
67
|
+
/**
|
|
68
|
+
* Field-level encryption configuration.
|
|
69
|
+
* Allows selecting specific fields in request/response to be encrypted/decrypted.
|
|
70
|
+
*/
|
|
71
|
+
encryption?: EncryptionConfig<z.infer<TReq>, z.infer<TRes>>;
|
|
72
|
+
/**
|
|
73
|
+
* Optional custom headers. Can be:
|
|
74
|
+
* - A fixed record of header key/values
|
|
75
|
+
* - A function returning headers derived from the input data
|
|
76
|
+
*/
|
|
21
77
|
headers?: Record<string, string> | ((input: z.infer<TReq>) => Record<string, string>);
|
|
78
|
+
/**
|
|
79
|
+
* Defines how the request body should be sent:
|
|
80
|
+
* - `"json"` (default): serialized as JSON
|
|
81
|
+
* - `"form-data"`: multipart form
|
|
82
|
+
*/
|
|
22
83
|
bodyType?: "json" | "form-data";
|
|
23
84
|
};
|
|
85
|
+
/**
|
|
86
|
+
* Contracts
|
|
87
|
+
* =========
|
|
88
|
+
* A collection of modules, each containing one or more endpoints.
|
|
89
|
+
* This defines a **hierarchical API contract**.
|
|
90
|
+
*
|
|
91
|
+
* For example:
|
|
92
|
+
* {
|
|
93
|
+
* users: {
|
|
94
|
+
* getUser: EndpointDef(...),
|
|
95
|
+
* updateUser: EndpointDef(...)
|
|
96
|
+
* },
|
|
97
|
+
* posts: {
|
|
98
|
+
* createPost: EndpointDef(...),
|
|
99
|
+
* listPosts: EndpointDef(...)
|
|
100
|
+
* }
|
|
101
|
+
* }
|
|
102
|
+
*/
|
|
24
103
|
type Contracts = {
|
|
25
104
|
[ModuleName: string]: {
|
|
26
105
|
[EndpointName: string]: EndpointDef<RequestSchema, ResponseSchema>;
|
|
27
106
|
};
|
|
28
107
|
};
|
|
29
|
-
|
|
108
|
+
/**
|
|
109
|
+
* Convenience alias that pins both generic types
|
|
110
|
+
* to `z.ZodTypeAny`, simplifying the contract declarations.
|
|
111
|
+
*/
|
|
112
|
+
type EndpointDefZ = EndpointDef<RequestSchema, ResponseSchema>;
|
|
113
|
+
/**
|
|
114
|
+
* Context passed to all middleware functions.
|
|
115
|
+
* Contains the current request URL, initialization object,
|
|
116
|
+
* and the specific endpoint definition for metadata access.
|
|
117
|
+
*/
|
|
118
|
+
type RequestParts = {
|
|
119
|
+
path?: Record<string, unknown>;
|
|
120
|
+
query?: Record<string, unknown>;
|
|
121
|
+
body?: unknown;
|
|
122
|
+
headers: Record<string, string>;
|
|
123
|
+
isStructured: boolean;
|
|
124
|
+
rawInput?: unknown;
|
|
125
|
+
};
|
|
126
|
+
interface MiddlewareContext<TReq extends RequestSchema = RequestSchema, TRes extends ResponseSchema = ResponseSchema> {
|
|
30
127
|
url: string;
|
|
31
128
|
init: RequestInit;
|
|
129
|
+
endpoint: EndpointDef<TReq, TRes>;
|
|
130
|
+
request?: RequestParts;
|
|
32
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* The `next()` function type signature used inside middleware.
|
|
134
|
+
* When called, it executes the next function in the chain
|
|
135
|
+
* or finally performs the fetch request.
|
|
136
|
+
*/
|
|
33
137
|
type MiddlewareNext = () => Promise<Response>;
|
|
34
|
-
|
|
138
|
+
/**
|
|
139
|
+
* Middleware
|
|
140
|
+
* ==========
|
|
141
|
+
* Defines the standard structure for a request middleware.
|
|
142
|
+
* Middlewares can intercept, modify, or even short-circuit requests.
|
|
143
|
+
*
|
|
144
|
+
* @example Logging Example:
|
|
145
|
+
* const logMiddleware: Middleware = async (ctx, next) => {
|
|
146
|
+
* console.log("Request:", ctx.url);
|
|
147
|
+
* const res = await next();
|
|
148
|
+
* console.log("Response:", res.status);
|
|
149
|
+
* return res;
|
|
150
|
+
* };
|
|
151
|
+
*/
|
|
152
|
+
type Middleware<TReq extends RequestSchema = RequestSchema, TRes extends ResponseSchema = ResponseSchema, Options = any> = (ctx: MiddlewareContext<TReq, TRes>, next: MiddlewareNext, options?: Options) => Promise<Response>;
|
|
153
|
+
/**
|
|
154
|
+
* ErrorLike
|
|
155
|
+
* =========
|
|
156
|
+
* Represents the normalized error shape used across the client.
|
|
157
|
+
* Provides consistency for error handling modules such as RichError.
|
|
158
|
+
*/
|
|
35
159
|
type ErrorLike = {
|
|
36
160
|
message: string;
|
|
37
161
|
status?: number;
|
|
38
162
|
code?: string;
|
|
39
163
|
[key: string]: any;
|
|
40
164
|
};
|
|
41
|
-
type EndpointDefZ = EndpointDef<RequestSchema, ResponseSchema>;
|
|
42
165
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
166
|
+
* RequestOptions
|
|
167
|
+
* ==============
|
|
168
|
+
* Per-request options passed to the client execution:
|
|
169
|
+
* - Optional AbortSignal (for cancellation)
|
|
170
|
+
* - Optional timeout (in milliseconds)
|
|
171
|
+
*/
|
|
172
|
+
type RequestOptions = {
|
|
173
|
+
signal?: AbortSignal;
|
|
174
|
+
timeout?: number;
|
|
175
|
+
};
|
|
176
|
+
/**
|
|
177
|
+
* EndpointMethods
|
|
178
|
+
* ================
|
|
179
|
+
* Automatically generated method signatures for all endpoints
|
|
180
|
+
* within a module, based on the Zod contract definitions.
|
|
45
181
|
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
182
|
+
* Each endpoint method:
|
|
183
|
+
* - Validates input against its request schema
|
|
184
|
+
* - Returns a Promise of the parsed and validated response type
|
|
48
185
|
*/
|
|
49
186
|
type EndpointMethods<M extends Record<string, EndpointDefZ>> = {
|
|
50
|
-
[K in keyof M]: (input: z.infer<M[K]["request"]
|
|
187
|
+
[K in keyof M]: (input: z.infer<M[K]["request"]>, // Auto‑derived input type from Zod schema
|
|
188
|
+
options?: RequestOptions) => Promise<z.infer<M[K]["response"]>>;
|
|
51
189
|
};
|
|
190
|
+
/**
|
|
191
|
+
* TokenProvider
|
|
192
|
+
* =============
|
|
193
|
+
* Specifies the contract for a function that supplies authentication tokens.
|
|
194
|
+
* Can be synchronous or async, e.g. fetching from localStorage or refreshing with an API.
|
|
195
|
+
*/
|
|
52
196
|
type TokenProvider = () => string | Promise<string>;
|
|
53
197
|
|
|
54
198
|
declare class RichError extends Error implements ErrorLike {
|
|
@@ -61,9 +205,6 @@ declare class RichError extends Error implements ErrorLike {
|
|
|
61
205
|
message: string;
|
|
62
206
|
});
|
|
63
207
|
}
|
|
64
|
-
/**
|
|
65
|
-
* Strongly-typed HTTP client built from Zod contracts.
|
|
66
|
-
*/
|
|
67
208
|
declare class ApiClient<C extends Contracts, E extends ErrorLike = RichError> {
|
|
68
209
|
private config;
|
|
69
210
|
private contracts;
|
|
@@ -74,6 +215,7 @@ declare class ApiClient<C extends Contracts, E extends ErrorLike = RichError> {
|
|
|
74
215
|
private mockDelay;
|
|
75
216
|
private responseWrapper?;
|
|
76
217
|
private tokenProvider?;
|
|
218
|
+
private retryConfig?;
|
|
77
219
|
private _modules;
|
|
78
220
|
constructor(config: {
|
|
79
221
|
baseUrl: string;
|
|
@@ -85,92 +227,35 @@ declare class ApiClient<C extends Contracts, E extends ErrorLike = RichError> {
|
|
|
85
227
|
max: number;
|
|
86
228
|
};
|
|
87
229
|
}, contracts: C);
|
|
88
|
-
/**
|
|
89
|
-
* Builds the strongly-typed `modules` API from the provided contracts.
|
|
90
|
-
* Must be called once after constructing the client.
|
|
91
|
-
*/
|
|
92
230
|
init(): void;
|
|
93
|
-
/**
|
|
94
|
-
* Type-safe entrypoint for calling API endpoints.
|
|
95
|
-
* Populated by `init()` based on the `contracts` passed to the constructor.
|
|
96
|
-
*/
|
|
97
231
|
get modules(): { [M in keyof C]: EndpointMethods<C[M]>; };
|
|
98
|
-
|
|
99
|
-
* Registers a middleware in the pipeline.
|
|
100
|
-
* Middlewares are executed in reverse order of registration.
|
|
101
|
-
*/
|
|
102
|
-
use<T>(middleware: Middleware<T>, options?: T): void;
|
|
103
|
-
/**
|
|
104
|
-
* Registers a global error handler.
|
|
105
|
-
* The handler is invoked for normalized errors before they are re-thrown.
|
|
106
|
-
*/
|
|
232
|
+
use<T>(middleware: Middleware<any, any, T>, options?: T): void;
|
|
107
233
|
onError(handler: (error: E) => void): void;
|
|
108
|
-
/**
|
|
109
|
-
* Registers a transformation function applied to all successful responses
|
|
110
|
-
* after Zod parsing.
|
|
111
|
-
*/
|
|
112
234
|
useResponseTransform(fn: (data: any) => any): void;
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
* return mocked responses instead of performing network requests.
|
|
116
|
-
*/
|
|
235
|
+
setRetryConfig(config: ApiClient<C>["retryConfig"]): void;
|
|
236
|
+
setTokenProvider(provider: TokenProvider): void;
|
|
117
237
|
setMockMode(enabled: boolean, delay?: {
|
|
118
238
|
min: number;
|
|
119
239
|
max: number;
|
|
120
240
|
}): void;
|
|
121
|
-
/**
|
|
122
|
-
* Registers a schema wrapper for APIs that wrap data in an envelope.
|
|
123
|
-
* Example: { success, data, message, code, ... }.
|
|
124
|
-
*/
|
|
125
241
|
setResponseWrapper(wrapper: (successResponse: z.ZodTypeAny) => z.ZodTypeAny): void;
|
|
126
|
-
/**
|
|
127
|
-
* Sets or updates the token provider used for authenticated endpoints.
|
|
128
|
-
* Overrides any static token provided in the constructor.
|
|
129
|
-
*/
|
|
130
|
-
setTokenProvider(provider: TokenProvider): void;
|
|
131
|
-
/**
|
|
132
|
-
* Returns the current token, preferring the tokenProvider if present,
|
|
133
|
-
* otherwise falling back to the static token from the constructor.
|
|
134
|
-
*/
|
|
135
242
|
getCurrentToken(): Promise<string | undefined>;
|
|
136
|
-
/**
|
|
137
|
-
* Executes a single endpoint request.
|
|
138
|
-
*
|
|
139
|
-
* Expected request shape (new style):
|
|
140
|
-
* z.object({
|
|
141
|
-
* path: z.object({...}).optional(),
|
|
142
|
-
* query: z.object({...}).optional(),
|
|
143
|
-
* body: z.any().optional(),
|
|
144
|
-
* header: z.object({...}).optional(),
|
|
145
|
-
* })
|
|
146
|
-
*
|
|
147
|
-
* If the parsed request does not contain `path`, `header`,`query` or `body`,
|
|
148
|
-
* the entire input is treated as the legacy flat request body.
|
|
149
|
-
*/
|
|
150
243
|
private request;
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
*/
|
|
244
|
+
private performRequestLogic;
|
|
245
|
+
private executeWithRetry;
|
|
246
|
+
private getBackoffDelay;
|
|
155
247
|
private buildUrlAndBody;
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
private
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
private getRandomDelay;
|
|
165
|
-
/**
|
|
166
|
-
* Creates a RichError instance from a partial error description.
|
|
167
|
-
*/
|
|
248
|
+
private extractRequestParts;
|
|
249
|
+
private isStructuredRequestInput;
|
|
250
|
+
private isObjectRecord;
|
|
251
|
+
private applyPathParams;
|
|
252
|
+
private appendQueryParams;
|
|
253
|
+
private appendQueryValue;
|
|
254
|
+
private appendFormValue;
|
|
255
|
+
private normalizeHeaders;
|
|
168
256
|
private createError;
|
|
169
|
-
/**
|
|
170
|
-
* Normalizes unknown errors into a RichError instance.
|
|
171
|
-
* Zod validation errors are converted into a standardized validation error.
|
|
172
|
-
*/
|
|
173
257
|
private normalizeError;
|
|
258
|
+
private handleMockRequest;
|
|
174
259
|
}
|
|
175
260
|
|
|
176
261
|
type LoggingOptions = {
|
|
@@ -178,7 +263,7 @@ type LoggingOptions = {
|
|
|
178
263
|
logResponse?: boolean;
|
|
179
264
|
debug?: boolean;
|
|
180
265
|
};
|
|
181
|
-
declare const loggingMiddleware: Middleware<LoggingOptions>;
|
|
266
|
+
declare const loggingMiddleware: Middleware<z$1.ZodTypeAny, z$1.ZodTypeAny, LoggingOptions>;
|
|
182
267
|
|
|
183
268
|
type RetryOptions = {
|
|
184
269
|
maxRetries?: number;
|
|
@@ -186,17 +271,129 @@ type RetryOptions = {
|
|
|
186
271
|
};
|
|
187
272
|
declare const retryMiddleware: (options?: RetryOptions) => Middleware;
|
|
188
273
|
|
|
274
|
+
/**
|
|
275
|
+
* TokenManagementOptions
|
|
276
|
+
* ======================
|
|
277
|
+
* Configuration structure for supplying credentials to the authentication layer.
|
|
278
|
+
*
|
|
279
|
+
* @property refreshToken A required asynchronous function responsible for
|
|
280
|
+
* obtaining a current, valid access token string. This allows
|
|
281
|
+
* for token fetching from storage or re-issuance upon expiry.
|
|
282
|
+
*/
|
|
189
283
|
type AuthOptions = {
|
|
190
284
|
refreshToken?: () => Promise<string>;
|
|
191
285
|
};
|
|
192
|
-
|
|
286
|
+
/**
|
|
287
|
+
* AuthenticationInjectorMiddleware
|
|
288
|
+
* ==================================
|
|
289
|
+
* This middleware operates early in the request pipeline to ensure every
|
|
290
|
+
* outgoing request is properly authorized by prepending an Authorization header.
|
|
291
|
+
*
|
|
292
|
+
* Core Logic:
|
|
293
|
+
* -----------
|
|
294
|
+
* 1. It checks if an explicit `refreshToken` supplier was configured in its options.
|
|
295
|
+
* 2. If present, it synchronously calls this supplier to obtain the latest token.
|
|
296
|
+
* 3. The resulting token is formatted as a standard 'Bearer' token and merged
|
|
297
|
+
* into the request's `init.headers`.
|
|
298
|
+
* 4. The request context (`ctx`) is then passed downstream.
|
|
299
|
+
*
|
|
300
|
+
* Note on Error Handling:
|
|
301
|
+
* -----------------------
|
|
302
|
+
* Any failure during the token retrieval process (e.g., if `refreshToken` throws)
|
|
303
|
+
* results in the error being caught, and the request proceeds **without** an
|
|
304
|
+
* Authorization header. This design defers failure response handling to
|
|
305
|
+
* subsequent middleware or the final network fetcher.
|
|
306
|
+
*
|
|
307
|
+
* @param ctx The current request context object, including mutable `init` properties.
|
|
308
|
+
* @param next The function to execute the rest of the middleware chain.
|
|
309
|
+
* @param options The specific configuration passed to this middleware instance.
|
|
310
|
+
*
|
|
311
|
+
* @returns The final `Response` object after the network call completes.
|
|
312
|
+
*
|
|
313
|
+
* @example
|
|
314
|
+
* // Assuming token retrieval logic is defined elsewhere
|
|
315
|
+
* const tokenSupplier = () => fetchTokenFromSecureStorage();
|
|
316
|
+
*
|
|
317
|
+
* client.addInterceptor(
|
|
318
|
+
* authMiddleware({ refreshToken: tokenSupplier })
|
|
319
|
+
* );
|
|
320
|
+
*/
|
|
321
|
+
declare const authMiddleware: Middleware<z$1.ZodTypeAny, z$1.ZodTypeAny, AuthOptions>;
|
|
193
322
|
|
|
323
|
+
/**
|
|
324
|
+
* CacheOptions
|
|
325
|
+
* ============
|
|
326
|
+
* Options for configuring the cache middleware.
|
|
327
|
+
* - `ttl` (Time To Live): Duration (in milliseconds) to keep cached GET responses.
|
|
328
|
+
* After this time, cached data expires and a fresh network call is performed.
|
|
329
|
+
*/
|
|
194
330
|
type CacheOptions = {
|
|
195
331
|
ttl?: number;
|
|
196
332
|
};
|
|
333
|
+
/**
|
|
334
|
+
* cacheMiddleware
|
|
335
|
+
* ===============
|
|
336
|
+
* A generic caching middleware for GET requests in the ApiClient.
|
|
337
|
+
* It stores successful responses in memory based on URL and method,
|
|
338
|
+
* returning cached data for subsequent identical requests until the TTL expires.
|
|
339
|
+
*
|
|
340
|
+
* Purpose:
|
|
341
|
+
* --------
|
|
342
|
+
* - Reduces redundant network calls
|
|
343
|
+
* - Improves performance for frequently fetched resources
|
|
344
|
+
* - Useful for lightweight front-end caching (not suitable for sensitive data)
|
|
345
|
+
*
|
|
346
|
+
* Behavior:
|
|
347
|
+
* ---------
|
|
348
|
+
* 1. Only applies to `GET` requests; all other HTTP methods bypass caching.
|
|
349
|
+
* 2. Caches the parsed JSON response in a simple in-memory Map.
|
|
350
|
+
* 3. On subsequent requests:
|
|
351
|
+
* - If the cache entry exists and hasn’t expired, returns a synthetic
|
|
352
|
+
* `Response` object built from cached JSON.
|
|
353
|
+
* - Otherwise performs the network call and refreshes the cache.
|
|
354
|
+
*
|
|
355
|
+
* @param options - Optional cache configuration (TTL in ms)
|
|
356
|
+
*
|
|
357
|
+
* @returns Middleware function compatible with the ApiClient pipeline.
|
|
358
|
+
*
|
|
359
|
+
* @example
|
|
360
|
+
* client.use(
|
|
361
|
+
* cacheMiddleware({ ttl: 120000 }) // cache GET results for 2 minutes
|
|
362
|
+
* );
|
|
363
|
+
*
|
|
364
|
+
* @note Each middleware instance maintains its own internal cache
|
|
365
|
+
* and is memory-scoped (not persistent between reloads).
|
|
366
|
+
*/
|
|
197
367
|
declare const cacheMiddleware: (options?: CacheOptions) => (ctx: MiddlewareContext, next: MiddlewareNext) => Promise<Response>;
|
|
198
368
|
|
|
199
|
-
|
|
369
|
+
type SymmetricKeyMaterial = {
|
|
370
|
+
type: "symmetric";
|
|
371
|
+
key: string;
|
|
372
|
+
};
|
|
373
|
+
type RSAKeyMaterial = {
|
|
374
|
+
type: "rsa";
|
|
375
|
+
publicKey: string;
|
|
376
|
+
privateKey: string;
|
|
377
|
+
};
|
|
378
|
+
type KeyMaterial = SymmetricKeyMaterial | RSAKeyMaterial;
|
|
379
|
+
type CustomHandlers = {
|
|
380
|
+
encrypt: (value: string, key: KeyMaterial) => string | Promise<string>;
|
|
381
|
+
decrypt: (value: string, key: KeyMaterial) => string | Promise<string>;
|
|
382
|
+
};
|
|
383
|
+
interface EncryptionOptions {
|
|
384
|
+
keyProvider: () => KeyMaterial | Promise<KeyMaterial>;
|
|
385
|
+
customHandlers?: CustomHandlers;
|
|
386
|
+
/**
|
|
387
|
+
* true = throw when encryption/decryption fails, which avoids leaking plaintext.
|
|
388
|
+
* false = log and continue/fallback to the original response.
|
|
389
|
+
* Default: true
|
|
390
|
+
*/
|
|
391
|
+
failClosed?: boolean;
|
|
392
|
+
}
|
|
393
|
+
declare function processDeep<T = unknown>(data: unknown, map: DeepEncryptionMap | null | undefined, defaultMethod: EncryptionMethod, transform: (value: unknown, method: EncryptionMethod) => Promise<unknown>): Promise<T>;
|
|
394
|
+
declare const encryptionMiddleware: Middleware<z.ZodTypeAny, z.ZodTypeAny, EncryptionOptions>;
|
|
395
|
+
|
|
396
|
+
declare const makeRequestSchema: <TPath extends z.ZodRawShape = {}, TQuery extends z.ZodRawShape = {}, TBody extends z.ZodTypeAny = z.ZodUndefined>() => (defs?: {
|
|
200
397
|
path?: z.ZodObject<TPath>;
|
|
201
398
|
query?: z.ZodObject<TQuery>;
|
|
202
399
|
body?: TBody;
|
|
@@ -218,4 +415,4 @@ declare const makeRequestSchema: <TPath extends z.ZodRawShape = {}, TQuery exten
|
|
|
218
415
|
headers: any;
|
|
219
416
|
}> extends infer T_9 ? { [k_5 in keyof T_9]: T_9[k_5]; } : never>;
|
|
220
417
|
|
|
221
|
-
export { ApiClient, type AuthOptions, type CacheOptions, type Contracts, type EndpointDef, type EndpointDefZ, type EndpointMethods, type ErrorLike, type LoggingOptions, type Middleware, type MiddlewareContext, type MiddlewareNext, type RequestSchema, type ResponseSchema, type RetryOptions, RichError, type TokenProvider, authMiddleware, cacheMiddleware, loggingMiddleware, makeRequestSchema, retryMiddleware };
|
|
418
|
+
export { ApiClient, type AuthOptions, type CacheOptions, type Contracts, type DeepEncryptionMap, type EncryptionConfig, type EncryptionMethod, type EncryptionOptions, type EndpointDef, type EndpointDefZ, type EndpointMethods, type ErrorLike, type LoggingOptions, type Method, type Middleware, type MiddlewareContext, type MiddlewareNext, type RequestOptions, type RequestParts, type RequestSchema, type ResponseSchema, type RetryOptions, RichError, type TokenProvider, authMiddleware, cacheMiddleware, encryptionMiddleware, loggingMiddleware, makeRequestSchema, processDeep, retryMiddleware };
|