@marianmeres/http-utils 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/API.md CHANGED
@@ -84,61 +84,66 @@ HTTP API client class. Usually created via `createHttpApi()`.
84
84
 
85
85
  ### Methods
86
86
 
87
- #### `get(path, options?)`
87
+ #### `get<T>(path, options?)`
88
88
 
89
89
  Performs a GET request.
90
90
 
91
91
  **New Options API (recommended):**
92
92
  ```ts
93
- async get(path: string, options?: GetOptions): Promise<unknown>
93
+ async get<T = unknown>(path: string, options?: GetOptions): Promise<T>
94
94
  ```
95
95
 
96
96
  **Legacy API:**
97
97
  ```ts
98
- async get(
98
+ async get<T = unknown>(
99
99
  path: string,
100
100
  params?: FetchParams,
101
101
  respHeaders?: ResponseHeaders | null,
102
102
  errorMessageExtractor?: ErrorMessageExtractor | null
103
- ): Promise<unknown>
103
+ ): Promise<T>
104
104
  ```
105
105
 
106
106
  **Example:**
107
107
  ```ts
108
- // New API
109
- const data = await api.get("/users", {
108
+ // New API with type parameter
109
+ interface User { id: number; name: string; }
110
+ const user = await api.get<User>("/users/1", {
110
111
  params: { headers: { "X-Custom": "value" } },
111
112
  respHeaders: {}
112
113
  });
113
114
 
115
+ // Without type parameter (returns unknown)
116
+ const data = await api.get("/users");
117
+
114
118
  // Legacy API
115
119
  const data = await api.get("/users", { headers: { "X-Custom": "value" } });
116
120
  ```
117
121
 
118
- #### `post(path, options?)`
122
+ #### `post<T>(path, options?)`
119
123
 
120
124
  Performs a POST request.
121
125
 
122
126
  **New Options API (recommended):**
123
127
  ```ts
124
- async post(path: string, options?: DataOptions): Promise<unknown>
128
+ async post<T = unknown>(path: string, options?: DataOptions): Promise<T>
125
129
  ```
126
130
 
127
131
  **Legacy API:**
128
132
  ```ts
129
- async post(
133
+ async post<T = unknown>(
130
134
  path: string,
131
135
  data?: RequestData,
132
136
  params?: FetchParams,
133
137
  respHeaders?: ResponseHeaders | null,
134
138
  errorMessageExtractor?: ErrorMessageExtractor | null
135
- ): Promise<unknown>
139
+ ): Promise<T>
136
140
  ```
137
141
 
138
142
  **Example:**
139
143
  ```ts
140
- // New API
141
- const result = await api.post("/users", {
144
+ // New API with type parameter
145
+ interface User { id: number; name: string; }
146
+ const user = await api.post<User>("/users", {
142
147
  data: { name: "John" },
143
148
  params: { headers: { "X-Custom": "value" } }
144
149
  });
@@ -147,17 +152,17 @@ const result = await api.post("/users", {
147
152
  const result = await api.post("/users", { name: "John" });
148
153
  ```
149
154
 
150
- #### `put(path, options?)`
155
+ #### `put<T>(path, options?)`
151
156
 
152
- Performs a PUT request. Same signature as `post()`.
157
+ Performs a PUT request. Same signature as `post<T>()`.
153
158
 
154
- #### `patch(path, options?)`
159
+ #### `patch<T>(path, options?)`
155
160
 
156
- Performs a PATCH request. Same signature as `post()`.
161
+ Performs a PATCH request. Same signature as `post<T>()`.
157
162
 
158
- #### `del(path, options?)`
163
+ #### `del<T>(path, options?)`
159
164
 
160
- Performs a DELETE request. Same signature as `post()`.
165
+ Performs a DELETE request. Same signature as `post<T>()`.
161
166
 
162
167
  #### `url(path)`
163
168
 
package/README.md CHANGED
@@ -13,6 +13,7 @@ Opinionated, lightweight HTTP client wrapper for `fetch` with type-safe errors a
13
13
  - 🪶 **Lightweight** - Zero dependencies, thin wrapper over native `fetch`
14
14
  - 🎨 **Flexible error handling** - Three-tier error message extraction (local → factory → global)
15
15
  - 📦 **Deno & Node.js** - Works in both runtimes
16
+ - 🦾 **Generic return types** - Optional type parameters for typed responses
16
17
 
17
18
  ## Installation
18
19
 
@@ -53,6 +54,11 @@ const newUser = await api.post("/users", {
53
54
  const legacyUsers = await api.get("/users", { headers: { "X-Custom": "value" } });
54
55
  const legacyUser = await api.post("/users", { name: "John Doe" });
55
56
 
57
+ // With type parameters for typed responses
58
+ interface User { id: number; name: string; }
59
+ const user = await api.get<User>("/users/1");
60
+ const created = await api.post<User>("/users", { data: { name: "Jane" } });
61
+
56
62
  // Error handling
57
63
  try {
58
64
  await api.get("/not-found");
@@ -123,6 +129,7 @@ try {
123
129
  - **Raw response**: Use `raw: true` to get the raw Response object
124
130
  - **Non-throwing**: Use `assert: false` to prevent throwing on errors
125
131
  - **AbortController**: Pass `signal` for request cancellation
132
+ - **Typed responses**: Use generics for type-safe responses: `api.get<User>("/users/1")`
126
133
 
127
134
  ## Full API Reference
128
135
 
package/dist/api.d.ts CHANGED
@@ -93,7 +93,7 @@ export declare class HttpApi {
93
93
  * });
94
94
  * ```
95
95
  */
96
- get(path: string, options: GetOptions): Promise<unknown>;
96
+ get<T = unknown>(path: string, options: GetOptions): Promise<T>;
97
97
  /**
98
98
  * Performs a GET request (legacy API).
99
99
  *
@@ -105,7 +105,7 @@ export declare class HttpApi {
105
105
  * @returns The response body (auto-parsed as JSON if possible), or Response if `raw: true`.
106
106
  * @throws {HttpError} When the response is not OK and `assert` is true (default).
107
107
  */
108
- get(path: string, params?: FetchParams, respHeaders?: ResponseHeaders | null, errorMessageExtractor?: ErrorMessageExtractor | null, _dumpParams?: boolean): Promise<unknown>;
108
+ get<T = unknown>(path: string, params?: FetchParams, respHeaders?: ResponseHeaders | null, errorMessageExtractor?: ErrorMessageExtractor | null, _dumpParams?: boolean): Promise<T>;
109
109
  /**
110
110
  * Performs a POST request (new options API - recommended).
111
111
  *
@@ -123,7 +123,7 @@ export declare class HttpApi {
123
123
  * });
124
124
  * ```
125
125
  */
126
- post(path: string, options: DataOptions): Promise<unknown>;
126
+ post<T = unknown>(path: string, options: DataOptions): Promise<T>;
127
127
  /**
128
128
  * Performs a POST request (legacy API).
129
129
  *
@@ -136,23 +136,23 @@ export declare class HttpApi {
136
136
  * @returns The response body (auto-parsed as JSON if possible), or Response if `raw: true`.
137
137
  * @throws {HttpError} When the response is not OK and `assert` is true (default).
138
138
  */
139
- post(path: string, data?: RequestData, params?: FetchParams, respHeaders?: ResponseHeaders | null, errorMessageExtractor?: ErrorMessageExtractor | null, _dumpParams?: boolean): Promise<unknown>;
139
+ post<T = unknown>(path: string, data?: RequestData, params?: FetchParams, respHeaders?: ResponseHeaders | null, errorMessageExtractor?: ErrorMessageExtractor | null, _dumpParams?: boolean): Promise<T>;
140
140
  /** Performs a PUT request (new options API). @see post */
141
- put(path: string, options: DataOptions): Promise<unknown>;
141
+ put<T = unknown>(path: string, options: DataOptions): Promise<T>;
142
142
  /** Performs a PUT request (legacy API). @see post */
143
- put(path: string, data?: RequestData, params?: FetchParams, respHeaders?: ResponseHeaders | null, errorMessageExtractor?: ErrorMessageExtractor | null, _dumpParams?: boolean): Promise<unknown>;
143
+ put<T = unknown>(path: string, data?: RequestData, params?: FetchParams, respHeaders?: ResponseHeaders | null, errorMessageExtractor?: ErrorMessageExtractor | null, _dumpParams?: boolean): Promise<T>;
144
144
  /** Performs a PATCH request (new options API). @see post */
145
- patch(path: string, options: DataOptions): Promise<unknown>;
145
+ patch<T = unknown>(path: string, options: DataOptions): Promise<T>;
146
146
  /** Performs a PATCH request (legacy API). @see post */
147
- patch(path: string, data?: RequestData, params?: FetchParams, respHeaders?: ResponseHeaders | null, errorMessageExtractor?: ErrorMessageExtractor | null, _dumpParams?: boolean): Promise<unknown>;
147
+ patch<T = unknown>(path: string, data?: RequestData, params?: FetchParams, respHeaders?: ResponseHeaders | null, errorMessageExtractor?: ErrorMessageExtractor | null, _dumpParams?: boolean): Promise<T>;
148
148
  /**
149
149
  * Performs a DELETE request (new options API).
150
150
  * Note: Request body in DELETE is allowed per HTTP spec.
151
151
  * @see post
152
152
  */
153
- del(path: string, options: DataOptions): Promise<unknown>;
153
+ del<T = unknown>(path: string, options: DataOptions): Promise<T>;
154
154
  /** Performs a DELETE request (legacy API). @see post */
155
- del(path: string, data?: RequestData, params?: FetchParams, respHeaders?: ResponseHeaders | null, errorMessageExtractor?: ErrorMessageExtractor | null, _dumpParams?: boolean): Promise<unknown>;
155
+ del<T = unknown>(path: string, data?: RequestData, params?: FetchParams, respHeaders?: ResponseHeaders | null, errorMessageExtractor?: ErrorMessageExtractor | null, _dumpParams?: boolean): Promise<T>;
156
156
  /**
157
157
  * Helper method to build the full URL from a path.
158
158
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marianmeres/http-utils",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "type": "module",
5
5
  "main": "dist/mod.js",
6
6
  "types": "dist/mod.d.ts",