endpoint-fetcher 1.0.0 → 1.0.2

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 CHANGED
@@ -5,7 +5,7 @@ A type-safe API client builder using the Fetch API with full TypeScript support.
5
5
  ## Installation
6
6
 
7
7
  ```bash
8
- npm install fetcher
8
+ npm install endpoint-fetcher
9
9
  ```
10
10
 
11
11
  ## Features
@@ -20,7 +20,7 @@ npm install fetcher
20
20
  ## Basic Usage
21
21
 
22
22
  ```typescript
23
- import { createApiClient, EndpointConfig } from 'fetcher';
23
+ import { createApiClient, EndpointConfig } from 'endpoint-fetcher';
24
24
 
25
25
  type User = { id: string; name: string; email: string };
26
26
 
@@ -146,7 +146,7 @@ await api.deleteUser({ id: '123' });
146
146
 
147
147
  ### 6. Custom Handler for Special Cases
148
148
 
149
- Use custom handlers when you need full control over the request (e.g., file uploads, custom headers, non-JSON responses).
149
+ Use custom handlers when you need full control over the request (e.g., file uploads, custom headers, non-JSON responses). The handler receives `{ input, fetch, method, path, baseUrl }`.
150
150
 
151
151
  ```typescript
152
152
  const api = createApiClient(
@@ -154,41 +154,41 @@ const api = createApiClient(
154
154
  uploadFile: {
155
155
  method: 'POST',
156
156
  path: '/upload',
157
- handler: async ({ input, fetch }) => {
157
+ handler: async ({ input, fetch, path, baseUrl }) => {
158
158
  const formData = new FormData();
159
159
  formData.append('file', input.file);
160
160
  formData.append('category', input.category);
161
-
162
- const response = await fetch('https://api.example.com/upload', {
161
+
162
+ const response = await fetch(`${baseUrl}${path}`, {
163
163
  method: 'POST',
164
164
  body: formData,
165
165
  // Note: Don't set Content-Type for FormData
166
166
  });
167
-
167
+
168
168
  if (!response.ok) {
169
169
  throw new Error('Upload failed');
170
170
  }
171
-
171
+
172
172
  return response.json();
173
173
  },
174
- } as EndpointConfig
174
+ } as EndpointConfig<
175
175
  { file: File; category: string },
176
176
  { url: string; id: string }
177
177
  >,
178
178
 
179
179
  downloadFile: {
180
180
  method: 'GET',
181
- path: '/download',
182
- handler: async ({ input, fetch }) => {
181
+ path: (input: { id: string }) => `/files/${input.id}`,
182
+ handler: async ({ input, fetch, path, baseUrl }) => {
183
183
  const response = await fetch(
184
- `https://api.example.com/files/${input.id}`,
184
+ `${baseUrl}${path}`,
185
185
  { method: 'GET' }
186
186
  );
187
-
187
+
188
188
  if (!response.ok) {
189
189
  throw new Error('Download failed');
190
190
  }
191
-
191
+
192
192
  return response.blob();
193
193
  },
194
194
  } as EndpointConfig<{ id: string }, Blob>,
@@ -301,7 +301,7 @@ try {
301
301
  ### 10. Complex Example - Full API Client
302
302
 
303
303
  ```typescript
304
- import { createApiClient, EndpointConfig } from 'fetcher';
304
+ import { createApiClient, EndpointConfig } from 'endpoint-fetcher';
305
305
 
306
306
  // Types
307
307
  type User = { id: string; name: string; email: string };
@@ -353,21 +353,21 @@ const api = createApiClient(
353
353
  searchPosts: {
354
354
  method: 'GET',
355
355
  path: '/posts/search',
356
- handler: async ({ input, fetch, path }) => {
356
+ handler: async ({ input, fetch, path, baseUrl }) => {
357
357
  const params = new URLSearchParams({
358
358
  q: input.query,
359
359
  ...(input.limit && { limit: input.limit.toString() }),
360
360
  });
361
-
361
+
362
362
  const response = await fetch(
363
- `https://api.example.com${path}?${params}`,
363
+ `${baseUrl}${path}?${params}`,
364
364
  { method: 'GET' }
365
365
  );
366
-
366
+
367
367
  if (!response.ok) throw new Error('Search failed');
368
368
  return response.json();
369
369
  },
370
- } as EndpointConfig
370
+ } as EndpointConfig<
371
371
  { query: string; limit?: number },
372
372
  Post[],
373
373
  ApiError
@@ -398,18 +398,28 @@ Creates a type-safe API client.
398
398
  **Parameters:**
399
399
 
400
400
  - `endpoints`: Object mapping endpoint names to configurations
401
-
402
401
  - `method`: HTTP method ('GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE')
403
402
  - `path`: Static string or function `(input) => string`
404
- - `handler`: Optional custom handler function (receives `{ input, fetch, method, path }`)
403
+ - `handler`: Optional custom handler function (receives `{ input, fetch, method, path, baseUrl }`)
405
404
  - `config`: Client configuration
406
-
407
405
  - `baseUrl`: Base URL for all requests
408
406
  - `fetch`: Optional custom fetch instance
409
407
  - `defaultHeaders`: Optional headers applied to all requests
410
408
 
411
409
  **Returns:** Type-safe client object with methods for each endpoint
412
410
 
411
+ ### Custom Handler Parameters
412
+
413
+ When using a custom handler, you receive an object with:
414
+
415
+ - `input`: The typed input parameters passed to the endpoint
416
+ - `fetch`: The fetch instance (either custom or global)
417
+ - `method`: The HTTP method for this endpoint
418
+ - `path`: The resolved path (after applying input to path function if applicable)
419
+ - `baseUrl`: The base URL from the client configuration
420
+
421
+ This allows you to construct full URLs using `${baseUrl}${path}` in your custom handlers.
422
+
413
423
  ## TypeScript Support
414
424
 
415
425
  Full TypeScript support with generic types:
@@ -424,4 +434,4 @@ EndpointConfig<TInput, TOutput, TError>
424
434
 
425
435
  ## License
426
436
 
427
- MIT
437
+ MIT
package/dist/index.d.mts CHANGED
@@ -7,6 +7,7 @@ type EndpointConfig<TInput = any, TOutput = any, TError = any> = {
7
7
  fetch: typeof fetch;
8
8
  method: HttpMethod;
9
9
  path: string;
10
+ baseUrl: string;
10
11
  }) => Promise<TOutput>;
11
12
  };
12
13
  type ApiConfig = {
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ type EndpointConfig<TInput = any, TOutput = any, TError = any> = {
7
7
  fetch: typeof fetch;
8
8
  method: HttpMethod;
9
9
  path: string;
10
+ baseUrl: string;
10
11
  }) => Promise<TOutput>;
11
12
  };
12
13
  type ApiConfig = {
package/dist/index.js CHANGED
@@ -61,7 +61,8 @@ function createApiClient(endpoints, config) {
61
61
  input,
62
62
  fetch: fetchInstance,
63
63
  method: endpoint.method,
64
- path
64
+ path,
65
+ baseUrl: config.baseUrl
65
66
  });
66
67
  }
67
68
  return defaultHandler(endpoint.method, path, input);
package/dist/index.mjs CHANGED
@@ -38,7 +38,8 @@ function createApiClient(endpoints, config) {
38
38
  input,
39
39
  fetch: fetchInstance,
40
40
  method: endpoint.method,
41
- path
41
+ path,
42
+ baseUrl: config.baseUrl
42
43
  });
43
44
  }
44
45
  return defaultHandler(endpoint.method, path, input);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "endpoint-fetcher",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Type-safe API client builder using fetch",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -52,4 +52,4 @@
52
52
  "tsup": "^8.5.1",
53
53
  "typescript": "^5.0.0"
54
54
  }
55
- }
55
+ }