zlient 1.0.9 → 1.0.11
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/endpoint/base-endpoint.d.ts +33 -11
- package/dist/endpoint/base-endpoint.d.ts.map +1 -1
- package/dist/http/http-client.d.ts +13 -0
- package/dist/http/http-client.d.ts.map +1 -1
- package/dist/index.cjs +79 -41
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +79 -41
- package/dist/index.js.map +1 -1
- package/dist/logger.d.ts.map +1 -1
- package/dist/metrics.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -6,20 +6,22 @@ import { HTTPMethod } from '../types';
|
|
|
6
6
|
* Wrapper object containing all request parameters.
|
|
7
7
|
*
|
|
8
8
|
* @template ReqSchema - Zod schema for request validation
|
|
9
|
+
* @template PathParams - Type for path parameters
|
|
10
|
+
* @template QueryParams - Type for query parameters
|
|
9
11
|
*/
|
|
10
|
-
export type EndpointCallConfig<ReqSchema extends z.ZodType> = {
|
|
12
|
+
export type EndpointCallConfig<ReqSchema extends z.ZodType, PathParams = never, QueryParams = never> = {
|
|
11
13
|
/** Request body data (for POST, PUT, PATCH, etc.) or request args */
|
|
12
14
|
data?: z.infer<ReqSchema>;
|
|
13
15
|
/** Path parameters for dynamic path construction */
|
|
14
|
-
pathParams?:
|
|
16
|
+
pathParams?: PathParams;
|
|
15
17
|
/** Query string parameters */
|
|
16
|
-
query?:
|
|
18
|
+
query?: QueryParams;
|
|
17
19
|
/** Request headers */
|
|
18
20
|
headers?: Record<string, string>;
|
|
19
21
|
/** Override base URL for this call */
|
|
20
22
|
baseUrlKey?: string;
|
|
21
23
|
/** Abort controller signal for cancellation */
|
|
22
|
-
signal?: AbortSignal;
|
|
24
|
+
signal?: globalThis.AbortSignal;
|
|
23
25
|
};
|
|
24
26
|
/**
|
|
25
27
|
* Generic, strongly-typed endpoint with Zod schemas for request and response validation.
|
|
@@ -27,15 +29,25 @@ export type EndpointCallConfig<ReqSchema extends z.ZodType> = {
|
|
|
27
29
|
*
|
|
28
30
|
* @template ReqSchema - Zod schema for request validation
|
|
29
31
|
* @template ResSchema - Zod schema for response validation
|
|
32
|
+
* @template PathParams - Type for path parameters (optional)
|
|
33
|
+
* @template QueryParams - Type for query parameters (optional)
|
|
30
34
|
*
|
|
31
35
|
* @example
|
|
32
36
|
* ```ts
|
|
33
37
|
* const UserSchema = z.object({ id: z.number(), name: z.string() });
|
|
34
38
|
* const CreateUserSchema = z.object({ name: z.string() });
|
|
35
39
|
*
|
|
36
|
-
*
|
|
40
|
+
* type UserPathParams = { id: string };
|
|
41
|
+
* type UserQueryParams = { include?: string; limit?: number };
|
|
42
|
+
*
|
|
43
|
+
* class GetUser extends BaseEndpoint<
|
|
44
|
+
* typeof CreateUserSchema,
|
|
45
|
+
* typeof UserSchema,
|
|
46
|
+
* UserPathParams,
|
|
47
|
+
* UserQueryParams
|
|
48
|
+
* > {
|
|
37
49
|
* protected method = 'GET' as const;
|
|
38
|
-
* protected path = (
|
|
50
|
+
* protected path = (params: UserPathParams) => `/users/${params.id}`;
|
|
39
51
|
*
|
|
40
52
|
* constructor(client: HttpClient) {
|
|
41
53
|
* super(client, {
|
|
@@ -44,14 +56,20 @@ export type EndpointCallConfig<ReqSchema extends z.ZodType> = {
|
|
|
44
56
|
* });
|
|
45
57
|
* }
|
|
46
58
|
* }
|
|
59
|
+
*
|
|
60
|
+
* // Usage:
|
|
61
|
+
* const user = await endpoint.call({
|
|
62
|
+
* pathParams: { id: '123' },
|
|
63
|
+
* query: { include: 'posts', limit: 10 }
|
|
64
|
+
* });
|
|
47
65
|
* ```
|
|
48
66
|
*/
|
|
49
|
-
export declare abstract class BaseEndpoint<ReqSchema extends z.ZodType, ResSchema extends z.ZodType> {
|
|
67
|
+
export declare abstract class BaseEndpoint<ReqSchema extends z.ZodType, ResSchema extends z.ZodType, PathParams = never, QueryParams = never> {
|
|
50
68
|
protected client: HttpClient;
|
|
51
69
|
/** HTTP method for this endpoint */
|
|
52
70
|
protected abstract readonly method: keyof typeof HTTPMethod;
|
|
53
71
|
/** URL path (can be a function for dynamic paths) */
|
|
54
|
-
protected abstract readonly path: string | ((params:
|
|
72
|
+
protected abstract readonly path: string | ((params: PathParams) => string);
|
|
55
73
|
/** Additional options for the request */
|
|
56
74
|
protected readonly options?: {
|
|
57
75
|
/** Override base URL for this call */
|
|
@@ -81,15 +99,19 @@ export declare abstract class BaseEndpoint<ReqSchema extends z.ZodType, ResSchem
|
|
|
81
99
|
* @example
|
|
82
100
|
* ```ts
|
|
83
101
|
* const endpoint = new GetUser(client);
|
|
84
|
-
* const user = await endpoint.call({
|
|
102
|
+
* const user = await endpoint.call({
|
|
103
|
+
* pathParams: { id: '123' },
|
|
104
|
+
* query: { include: 'posts' }
|
|
105
|
+
* });
|
|
85
106
|
* // With additional options:
|
|
86
107
|
* const user = await endpoint.call({
|
|
87
|
-
* data: {
|
|
108
|
+
* data: { name: 'John' },
|
|
109
|
+
* pathParams: { id: '123' },
|
|
88
110
|
* headers: { 'X-Custom': 'value' },
|
|
89
111
|
* query: { include: 'posts' }
|
|
90
112
|
* });
|
|
91
113
|
* ```
|
|
92
114
|
*/
|
|
93
|
-
call(config?: EndpointCallConfig<ReqSchema>): Promise<z.infer<ResSchema>>;
|
|
115
|
+
call(config?: EndpointCallConfig<ReqSchema, PathParams, QueryParams>): Promise<z.infer<ResSchema>>;
|
|
94
116
|
}
|
|
95
117
|
//# sourceMappingURL=base-endpoint.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base-endpoint.d.ts","sourceRoot":"","sources":["../../lib/endpoint/base-endpoint.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAGtC
|
|
1
|
+
{"version":3,"file":"base-endpoint.d.ts","s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CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;QAC3B,sCAAsC;QACtC,UAAU,CAAC,EAAE,MAAM,CAAC;KACrB,CAAC;IACF,6CAA6C;IAC7C,SAAS,CAAC,QAAQ,CAAC,aAAa,CAAC,EAAE,SAAS,CAAC;IAC7C,qCAAqC;IACrC,SAAS,CAAC,QAAQ,CAAC,cAAc,EAAE,SAAS,CAAC;IAE7C;;;OAGG;gBAES,MAAM,EAAE,UAAU,EAC5B,GAAG,EAAE;QACH,aAAa,CAAC,EAAE,SAAS,CAAC;QAC1B,cAAc,EAAE,SAAS,CAAC;KAC3B;IAMH;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACG,IAAI,CACR,MAAM,GAAE,kBAAkB,CAAC,SAAS,EAAE,UAAU,EAAE,WAAW,CAI5D,GACA,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;CAgC/B"}
|
|
@@ -64,6 +64,19 @@ export declare class HttpClient {
|
|
|
64
64
|
* @private
|
|
65
65
|
*/
|
|
66
66
|
private runAfterHooks;
|
|
67
|
+
/**
|
|
68
|
+
* Get all configured base URLs.
|
|
69
|
+
*
|
|
70
|
+
* @returns Object mapping base URL keys to their resolved URLs
|
|
71
|
+
*/
|
|
72
|
+
getBaseUrls(): import("..").BaseUrlMap;
|
|
73
|
+
/**
|
|
74
|
+
* Get the resolved base URL for a given key.
|
|
75
|
+
*
|
|
76
|
+
* @param key - Base URL key (defaults to 'default' if not provided)
|
|
77
|
+
* @returns Resolved base URL string
|
|
78
|
+
*/
|
|
79
|
+
getBaseUrl(key: string): string;
|
|
67
80
|
/**
|
|
68
81
|
* Make an HTTP request with automatic retry, authentication, and validation.
|
|
69
82
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-client.d.ts","sourceRoot":"","sources":["../../lib/http/http-client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAI5C,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EAAE,cAAc,GACvB,OAAO,CAAC;QAAE,IAAI,EAAE,CAAC,CAAC;QAAC,QAAQ,EAAE,QAAQ,CAAA;KAAE,CAAC;IAI3C;;;;;;;OAOG;IACG,GAAG,CAAC,CAAC,GAAG,OAAO,EACnB,IAAI,EAAE,MAAM,EACZ,IAAI,CAAC,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,cAAc,GACvB,OAAO,CAAC;QAAE,IAAI,EAAE,CAAC,CAAC;QAAC,QAAQ,EAAE,QAAQ,CAAA;KAAE,CAAC;IAI3C;;;;;;;OAOG;IACG,KAAK,CAAC,CAAC,GAAG,OAAO,EACrB,IAAI,EAAE,MAAM,EACZ,IAAI,CAAC,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,cAAc,GACvB,OAAO,CAAC;QAAE,IAAI,EAAE,CAAC,CAAC;QAAC,QAAQ,EAAE,QAAQ,CAAA;KAAE,CAAC;IAI3C;;;;;;;OAOG;IACG,MAAM,CAAC,CAAC,GAAG,OAAO,EACtB,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,cAAc,GACvB,OAAO,CAAC;QAAE,IAAI,EAAE,CAAC,CAAC;QAAC,QAAQ,EAAE,QAAQ,CAAA;KAAE,CAAC;CAG5C"}
|
package/dist/index.cjs
CHANGED
|
@@ -37,7 +37,7 @@ const HTTPMethod = {
|
|
|
37
37
|
/**
|
|
38
38
|
* Custom error class for API-related errors.
|
|
39
39
|
* Includes HTTP status codes, response details, and validation errors.
|
|
40
|
-
*
|
|
40
|
+
*
|
|
41
41
|
* @example
|
|
42
42
|
* ```ts
|
|
43
43
|
* throw new ApiError('Invalid request', {
|
|
@@ -103,10 +103,10 @@ const PaginationSchema = zod.z.object({
|
|
|
103
103
|
/**
|
|
104
104
|
* Converts query parameters to a URL query string.
|
|
105
105
|
* Filters out undefined values automatically.
|
|
106
|
-
*
|
|
106
|
+
*
|
|
107
107
|
* @param q - Query parameters as URLSearchParams or object
|
|
108
108
|
* @returns Query string with leading '?' or empty string
|
|
109
|
-
*
|
|
109
|
+
*
|
|
110
110
|
* @example
|
|
111
111
|
* ```ts
|
|
112
112
|
* toQueryString({ page: 1, filter: 'active' }) // "?page=1&filter=active"
|
|
@@ -139,12 +139,12 @@ var NoAuth = class {
|
|
|
139
139
|
/**
|
|
140
140
|
* API Key authentication provider.
|
|
141
141
|
* Supports both header-based and query parameter-based authentication.
|
|
142
|
-
*
|
|
142
|
+
*
|
|
143
143
|
* @example
|
|
144
144
|
* ```ts
|
|
145
145
|
* // Header-based
|
|
146
146
|
* const auth = new ApiKeyAuth({ header: 'X-API-Key', value: 'secret' });
|
|
147
|
-
*
|
|
147
|
+
*
|
|
148
148
|
* // Query parameter-based
|
|
149
149
|
* const auth = new ApiKeyAuth({ query: 'apiKey', value: 'secret' });
|
|
150
150
|
* ```
|
|
@@ -170,12 +170,12 @@ var ApiKeyAuth = class {
|
|
|
170
170
|
/**
|
|
171
171
|
* Bearer token authentication provider.
|
|
172
172
|
* Supports both static tokens and dynamic token fetching (e.g., for OAuth2 refresh).
|
|
173
|
-
*
|
|
173
|
+
*
|
|
174
174
|
* @example
|
|
175
175
|
* ```ts
|
|
176
176
|
* // Static token
|
|
177
177
|
* const auth = new BearerTokenAuth(() => 'my-token');
|
|
178
|
-
*
|
|
178
|
+
*
|
|
179
179
|
* // Dynamic token with refresh
|
|
180
180
|
* const auth = new BearerTokenAuth(async () => {
|
|
181
181
|
* return await refreshAccessToken();
|
|
@@ -201,11 +201,11 @@ var BearerTokenAuth = class {
|
|
|
201
201
|
/**
|
|
202
202
|
* Safely parse data with a Zod schema without throwing.
|
|
203
203
|
* Returns a result object with success status and data or error.
|
|
204
|
-
*
|
|
204
|
+
*
|
|
205
205
|
* @param schema - Zod schema to validate against
|
|
206
206
|
* @param data - Data to validate
|
|
207
207
|
* @returns Result object with success flag and data/error
|
|
208
|
-
*
|
|
208
|
+
*
|
|
209
209
|
* @example
|
|
210
210
|
* ```ts
|
|
211
211
|
* const result = safeParse(UserSchema, userData);
|
|
@@ -230,12 +230,12 @@ function safeParse(schema, data) {
|
|
|
230
230
|
/**
|
|
231
231
|
* Parse data with a Zod schema, throwing an ApiError on validation failure.
|
|
232
232
|
* Use this when you want to fail fast on invalid data.
|
|
233
|
-
*
|
|
233
|
+
*
|
|
234
234
|
* @param schema - Zod schema to validate against
|
|
235
235
|
* @param data - Data to validate
|
|
236
236
|
* @returns Validated and typed data
|
|
237
237
|
* @throws {ApiError} If validation fails
|
|
238
|
-
*
|
|
238
|
+
*
|
|
239
239
|
* @example
|
|
240
240
|
* ```ts
|
|
241
241
|
* try {
|
|
@@ -428,7 +428,7 @@ var ConsoleMetricsCollector = class {
|
|
|
428
428
|
/**
|
|
429
429
|
* HTTP client with built-in retry logic, authentication, and interceptors.
|
|
430
430
|
* Supports multiple base URLs, type-safe requests, and comprehensive error handling.
|
|
431
|
-
*
|
|
431
|
+
*
|
|
432
432
|
* @example
|
|
433
433
|
* ```ts
|
|
434
434
|
* const client = new HttpClient({
|
|
@@ -437,7 +437,7 @@ var ConsoleMetricsCollector = class {
|
|
|
437
437
|
* retry: { maxRetries: 3, baseDelayMs: 1000 },
|
|
438
438
|
* timeout: { requestTimeoutMs: 30000 }
|
|
439
439
|
* });
|
|
440
|
-
*
|
|
440
|
+
*
|
|
441
441
|
* const { data } = await client.request('GET', '/users', undefined, { query: { page: 1 } });
|
|
442
442
|
* ```
|
|
443
443
|
*/
|
|
@@ -453,7 +453,7 @@ var HttpClient = class {
|
|
|
453
453
|
metrics;
|
|
454
454
|
/**
|
|
455
455
|
* Creates a new HTTP client instance.
|
|
456
|
-
*
|
|
456
|
+
*
|
|
457
457
|
* @param opts - Client configuration options
|
|
458
458
|
* @throws {Error} If no fetch implementation is available
|
|
459
459
|
*/
|
|
@@ -482,7 +482,7 @@ var HttpClient = class {
|
|
|
482
482
|
}
|
|
483
483
|
/**
|
|
484
484
|
* Set or update the authentication provider.
|
|
485
|
-
*
|
|
485
|
+
*
|
|
486
486
|
* @param auth - Authentication provider instance
|
|
487
487
|
* @example
|
|
488
488
|
* ```ts
|
|
@@ -550,15 +550,32 @@ var HttpClient = class {
|
|
|
550
550
|
});
|
|
551
551
|
}
|
|
552
552
|
/**
|
|
553
|
+
* Get all configured base URLs.
|
|
554
|
+
*
|
|
555
|
+
* @returns Object mapping base URL keys to their resolved URLs
|
|
556
|
+
*/
|
|
557
|
+
getBaseUrls() {
|
|
558
|
+
return this.baseUrls;
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Get the resolved base URL for a given key.
|
|
562
|
+
*
|
|
563
|
+
* @param key - Base URL key (defaults to 'default' if not provided)
|
|
564
|
+
* @returns Resolved base URL string
|
|
565
|
+
*/
|
|
566
|
+
getBaseUrl(key) {
|
|
567
|
+
return this.resolveBaseUrl(key);
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
553
570
|
* Make an HTTP request with automatic retry, authentication, and validation.
|
|
554
|
-
*
|
|
571
|
+
*
|
|
555
572
|
* @param method - HTTP method (GET, POST, PUT, etc.)
|
|
556
573
|
* @param path - Request path (will be appended to base URL)
|
|
557
574
|
* @param body - Request body (will be JSON.stringify'd if Content-Type is json)
|
|
558
575
|
* @param options - Additional request options (headers, query params, etc.)
|
|
559
576
|
* @returns Promise resolving to response data and Response object
|
|
560
577
|
* @throws {ApiError} If request fails or response validation fails
|
|
561
|
-
*
|
|
578
|
+
*
|
|
562
579
|
* @example
|
|
563
580
|
* ```ts
|
|
564
581
|
* const { data, response } = await client.request('GET', '/users', undefined, {
|
|
@@ -673,7 +690,7 @@ var HttpClient = class {
|
|
|
673
690
|
}
|
|
674
691
|
/**
|
|
675
692
|
* Convenience method for GET requests.
|
|
676
|
-
*
|
|
693
|
+
*
|
|
677
694
|
* @example
|
|
678
695
|
* ```ts
|
|
679
696
|
* const { data } = await client.get('/users', { query: { page: 1 } });
|
|
@@ -684,7 +701,7 @@ var HttpClient = class {
|
|
|
684
701
|
}
|
|
685
702
|
/**
|
|
686
703
|
* Convenience method for POST requests.
|
|
687
|
-
*
|
|
704
|
+
*
|
|
688
705
|
* @example
|
|
689
706
|
* ```ts
|
|
690
707
|
* const { data } = await client.post('/users', { name: 'John' });
|
|
@@ -695,7 +712,7 @@ var HttpClient = class {
|
|
|
695
712
|
}
|
|
696
713
|
/**
|
|
697
714
|
* Convenience method for PUT requests.
|
|
698
|
-
*
|
|
715
|
+
*
|
|
699
716
|
* @example
|
|
700
717
|
* ```ts
|
|
701
718
|
* const { data } = await client.put('/users/1', { name: 'John Updated' });
|
|
@@ -706,7 +723,7 @@ var HttpClient = class {
|
|
|
706
723
|
}
|
|
707
724
|
/**
|
|
708
725
|
* Convenience method for PATCH requests.
|
|
709
|
-
*
|
|
726
|
+
*
|
|
710
727
|
* @example
|
|
711
728
|
* ```ts
|
|
712
729
|
* const { data } = await client.patch('/users/1', { name: 'John' });
|
|
@@ -717,7 +734,7 @@ var HttpClient = class {
|
|
|
717
734
|
}
|
|
718
735
|
/**
|
|
719
736
|
* Convenience method for DELETE requests.
|
|
720
|
-
*
|
|
737
|
+
*
|
|
721
738
|
* @example
|
|
722
739
|
* ```ts
|
|
723
740
|
* const { data } = await client.delete('/users/1');
|
|
@@ -733,26 +750,42 @@ var HttpClient = class {
|
|
|
733
750
|
/**
|
|
734
751
|
* Generic, strongly-typed endpoint with Zod schemas for request and response validation.
|
|
735
752
|
* Extend this class to create type-safe API endpoints.
|
|
736
|
-
*
|
|
753
|
+
*
|
|
737
754
|
* @template ReqSchema - Zod schema for request validation
|
|
738
755
|
* @template ResSchema - Zod schema for response validation
|
|
739
|
-
*
|
|
756
|
+
* @template PathParams - Type for path parameters (optional)
|
|
757
|
+
* @template QueryParams - Type for query parameters (optional)
|
|
758
|
+
*
|
|
740
759
|
* @example
|
|
741
760
|
* ```ts
|
|
742
761
|
* const UserSchema = z.object({ id: z.number(), name: z.string() });
|
|
743
762
|
* const CreateUserSchema = z.object({ name: z.string() });
|
|
744
|
-
*
|
|
745
|
-
*
|
|
763
|
+
*
|
|
764
|
+
* type UserPathParams = { id: string };
|
|
765
|
+
* type UserQueryParams = { include?: string; limit?: number };
|
|
766
|
+
*
|
|
767
|
+
* class GetUser extends BaseEndpoint<
|
|
768
|
+
* typeof CreateUserSchema,
|
|
769
|
+
* typeof UserSchema,
|
|
770
|
+
* UserPathParams,
|
|
771
|
+
* UserQueryParams
|
|
772
|
+
* > {
|
|
746
773
|
* protected method = 'GET' as const;
|
|
747
|
-
* protected path = (
|
|
748
|
-
*
|
|
774
|
+
* protected path = (params: UserPathParams) => `/users/${params.id}`;
|
|
775
|
+
*
|
|
749
776
|
* constructor(client: HttpClient) {
|
|
750
|
-
* super(client, {
|
|
777
|
+
* super(client, {
|
|
751
778
|
* requestSchema: CreateUserSchema,
|
|
752
|
-
* responseSchema: UserSchema
|
|
779
|
+
* responseSchema: UserSchema
|
|
753
780
|
* });
|
|
754
781
|
* }
|
|
755
782
|
* }
|
|
783
|
+
*
|
|
784
|
+
* // Usage:
|
|
785
|
+
* const user = await endpoint.call({
|
|
786
|
+
* pathParams: { id: '123' },
|
|
787
|
+
* query: { include: 'posts', limit: 10 }
|
|
788
|
+
* });
|
|
756
789
|
* ```
|
|
757
790
|
*/
|
|
758
791
|
var BaseEndpoint = class {
|
|
@@ -774,19 +807,23 @@ var BaseEndpoint = class {
|
|
|
774
807
|
/**
|
|
775
808
|
* Call the endpoint with strong typing derived from schemas.
|
|
776
809
|
* Validates request data before sending and response data after receiving.
|
|
777
|
-
*
|
|
810
|
+
*
|
|
778
811
|
* @param config - Request configuration object containing all parameters
|
|
779
812
|
* @returns Promise resolving to validated response data (typed by ResSchema)
|
|
780
813
|
* @throws {ZodError} If request validation fails
|
|
781
814
|
* @throws {ApiError} If response validation fails or request fails
|
|
782
|
-
*
|
|
815
|
+
*
|
|
783
816
|
* @example
|
|
784
817
|
* ```ts
|
|
785
818
|
* const endpoint = new GetUser(client);
|
|
786
|
-
* const user = await endpoint.call({
|
|
819
|
+
* const user = await endpoint.call({
|
|
820
|
+
* pathParams: { id: '123' },
|
|
821
|
+
* query: { include: 'posts' }
|
|
822
|
+
* });
|
|
787
823
|
* // With additional options:
|
|
788
|
-
* const user = await endpoint.call({
|
|
789
|
-
* data: {
|
|
824
|
+
* const user = await endpoint.call({
|
|
825
|
+
* data: { name: 'John' },
|
|
826
|
+
* pathParams: { id: '123' },
|
|
790
827
|
* headers: { 'X-Custom': 'value' },
|
|
791
828
|
* query: { include: 'posts' }
|
|
792
829
|
* });
|
|
@@ -801,8 +838,9 @@ var BaseEndpoint = class {
|
|
|
801
838
|
const pathArgs = pathParams ?? data;
|
|
802
839
|
const path = typeof this.path === "function" ? this.path(pathArgs) : this.path;
|
|
803
840
|
const body = this.method !== "GET" && this.method !== "HEAD" ? data : void 0;
|
|
841
|
+
const queryForRequest = query;
|
|
804
842
|
const { data: responseData } = await this.client.request(this.method, path, body, {
|
|
805
|
-
query,
|
|
843
|
+
query: queryForRequest,
|
|
806
844
|
headers,
|
|
807
845
|
baseUrlKey: baseUrlKey ?? this.options?.baseUrlKey,
|
|
808
846
|
signal
|
|
@@ -816,7 +854,7 @@ var BaseEndpoint = class {
|
|
|
816
854
|
/**
|
|
817
855
|
* Common ID type that supports strings, numbers, or UUIDs.
|
|
818
856
|
* Use this for entity identifiers in your schemas.
|
|
819
|
-
*
|
|
857
|
+
*
|
|
820
858
|
* @example
|
|
821
859
|
* ```ts
|
|
822
860
|
* const UserSchema = z.object({ id: Id, name: z.string() });
|
|
@@ -830,7 +868,7 @@ const Id = zod.z.union([
|
|
|
830
868
|
/**
|
|
831
869
|
* Common timestamp fields for entities.
|
|
832
870
|
* Use this for database models with creation/update tracking.
|
|
833
|
-
*
|
|
871
|
+
*
|
|
834
872
|
* @example
|
|
835
873
|
* ```ts
|
|
836
874
|
* const UserSchema = z.object({
|
|
@@ -872,14 +910,14 @@ const ApiErrorSchema = zod.z.object({
|
|
|
872
910
|
/**
|
|
873
911
|
* Generic envelope wrapper for API responses.
|
|
874
912
|
* Provides consistent structure with success flag, data, error, and metadata.
|
|
875
|
-
*
|
|
913
|
+
*
|
|
876
914
|
* @param inner - Zod schema for the response data
|
|
877
915
|
* @returns Envelope schema wrapping the inner schema
|
|
878
|
-
*
|
|
916
|
+
*
|
|
879
917
|
* @example
|
|
880
918
|
* ```ts
|
|
881
919
|
* const UserResponseSchema = Envelope(z.object({ id: Id, name: z.string() }));
|
|
882
|
-
*
|
|
920
|
+
*
|
|
883
921
|
* // Response structure:
|
|
884
922
|
* // {
|
|
885
923
|
* // success: true,
|