@foundatiofx/fetchclient 0.47.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/esm/mod.js +5 -0
- package/esm/package.json +3 -0
- package/esm/src/Counter.js +36 -0
- package/esm/src/DefaultHelpers.js +132 -0
- package/esm/src/FetchClient.js +543 -0
- package/esm/src/FetchClientCache.js +88 -0
- package/esm/src/FetchClientContext.js +1 -0
- package/esm/src/FetchClientMiddleware.js +1 -0
- package/esm/src/FetchClientOptions.js +1 -0
- package/esm/src/FetchClientProvider.js +200 -0
- package/esm/src/FetchClientResponse.js +1 -0
- package/esm/src/LinkHeader.js +70 -0
- package/esm/src/ObjectEvent.js +15 -0
- package/esm/src/ProblemDetails.js +47 -0
- package/esm/src/RateLimitMiddleware.js +115 -0
- package/esm/src/RateLimiter.js +347 -0
- package/esm/src/RequestOptions.js +1 -0
- package/license +20 -0
- package/package.json +50 -0
- package/readme.md +303 -0
- package/script/mod.js +27 -0
- package/script/package.json +3 -0
- package/script/src/Counter.js +40 -0
- package/script/src/DefaultHelpers.js +149 -0
- package/script/src/FetchClient.js +547 -0
- package/script/src/FetchClientCache.js +92 -0
- package/script/src/FetchClientContext.js +2 -0
- package/script/src/FetchClientMiddleware.js +2 -0
- package/script/src/FetchClientOptions.js +2 -0
- package/script/src/FetchClientProvider.js +204 -0
- package/script/src/FetchClientResponse.js +2 -0
- package/script/src/LinkHeader.js +72 -0
- package/script/src/ObjectEvent.js +19 -0
- package/script/src/ProblemDetails.js +51 -0
- package/script/src/RateLimitMiddleware.js +120 -0
- package/script/src/RateLimiter.js +356 -0
- package/script/src/RequestOptions.js +2 -0
- package/types/_dnt.test_shims.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/almost_equals.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/array_includes.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/assert.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/assertion_error.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/equal.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/equals.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/exists.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/fail.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/false.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/greater.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/greater_or_equal.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/instance_of.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/is_error.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/less.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/less_or_equal.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/match.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/mod.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/not_equals.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/not_instance_of.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/not_match.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/not_strict_equals.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/object_match.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/rejects.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/strict_equals.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/string_includes.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/throws.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/unimplemented.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/assert/1.0.14/unreachable.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/internal/1.0.10/build_message.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/internal/1.0.10/diff.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/internal/1.0.10/diff_str.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/internal/1.0.10/format.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/internal/1.0.10/styles.d.ts.map +1 -0
- package/types/deps/jsr.io/@std/internal/1.0.10/types.d.ts.map +1 -0
- package/types/mod.d.ts +11 -0
- package/types/mod.d.ts.map +1 -0
- package/types/src/Counter.d.ts +27 -0
- package/types/src/Counter.d.ts.map +1 -0
- package/types/src/DefaultHelpers.d.ts +106 -0
- package/types/src/DefaultHelpers.d.ts.map +1 -0
- package/types/src/FetchClient.d.ts +156 -0
- package/types/src/FetchClient.d.ts.map +1 -0
- package/types/src/FetchClient.test.d.ts.map +1 -0
- package/types/src/FetchClientCache.d.ts +62 -0
- package/types/src/FetchClientCache.d.ts.map +1 -0
- package/types/src/FetchClientContext.d.ts +8 -0
- package/types/src/FetchClientContext.d.ts.map +1 -0
- package/types/src/FetchClientMiddleware.d.ts +9 -0
- package/types/src/FetchClientMiddleware.d.ts.map +1 -0
- package/types/src/FetchClientOptions.d.ts +53 -0
- package/types/src/FetchClientOptions.d.ts.map +1 -0
- package/types/src/FetchClientProvider.d.ts +109 -0
- package/types/src/FetchClientProvider.d.ts.map +1 -0
- package/types/src/FetchClientResponse.d.ts +29 -0
- package/types/src/FetchClientResponse.d.ts.map +1 -0
- package/types/src/LinkHeader.d.ts +15 -0
- package/types/src/LinkHeader.d.ts.map +1 -0
- package/types/src/ObjectEvent.d.ts +20 -0
- package/types/src/ObjectEvent.d.ts.map +1 -0
- package/types/src/ProblemDetails.d.ts +43 -0
- package/types/src/ProblemDetails.d.ts.map +1 -0
- package/types/src/RateLimit.test.d.ts.map +1 -0
- package/types/src/RateLimitMiddleware.d.ts +50 -0
- package/types/src/RateLimitMiddleware.d.ts.map +1 -0
- package/types/src/RateLimiter.d.ts +179 -0
- package/types/src/RateLimiter.d.ts.map +1 -0
- package/types/src/RequestOptions.d.ts +64 -0
- package/types/src/RequestOptions.d.ts.map +1 -0
package/readme.md
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+

|
|
2
|
+

|
|
3
|
+
|
|
4
|
+
[](https://www.npmjs.com/package/@foundatiofx/fetchclient)
|
|
5
|
+
[](https://jsr.io/@foundatiofx/fetchclient)
|
|
6
|
+
[](https://github.com/foundatiofx/foundatio/actions)
|
|
7
|
+
[](https://discord.gg/6HxgFCx)
|
|
8
|
+
|
|
9
|
+
<!-- deno-fmt-ignore-file -->
|
|
10
|
+
|
|
11
|
+
FetchClient is a library that makes it easier to use the fetch API for JSON
|
|
12
|
+
APIs. It provides the following features:
|
|
13
|
+
|
|
14
|
+
- [Typed Response](#typed-response) - Full TypeScript support with strongly
|
|
15
|
+
typed responses
|
|
16
|
+
- [Functional](#functional) - Standalone functions for simple usage
|
|
17
|
+
- [Model Validator](#model-validator) - Built-in validation with Problem Details
|
|
18
|
+
support
|
|
19
|
+
- [Caching](#caching) - Response caching with TTL and programmatic invalidation
|
|
20
|
+
- [Middleware](#middleware) - Extensible middleware pipeline for
|
|
21
|
+
request/response handling
|
|
22
|
+
- [Rate Limiting](#rate-limiting) - Built-in rate limiting with per-domain
|
|
23
|
+
support
|
|
24
|
+
- [Request Timeout](#request-timeout) - Configurable timeouts with AbortSignal
|
|
25
|
+
support
|
|
26
|
+
- [Error Handling](#error-handling) - Comprehensive error handling with Problem
|
|
27
|
+
Details
|
|
28
|
+
- [Authentication](#authentication) - Built-in Bearer token support
|
|
29
|
+
- [Base URL](#base-url) - Global base URL configuration
|
|
30
|
+
- [Loading State](#loading-state) - Track request loading state with events
|
|
31
|
+
|
|
32
|
+
## Install
|
|
33
|
+
|
|
34
|
+
```shell
|
|
35
|
+
npm install --save @foundatiofx/fetchclient
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Docs
|
|
39
|
+
|
|
40
|
+
[API Documentation](https://jsr.io/@foundatiofx/fetchclient/doc)
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
### Typed Response
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { FetchClient } from "@foundatiofx/fetchclient";
|
|
48
|
+
|
|
49
|
+
type Products = {
|
|
50
|
+
products: Array<{ id: number; name: string }>;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const client = new FetchClient();
|
|
54
|
+
const response = await client.getJSON<Products>(
|
|
55
|
+
`https://dummyjson.com/products/search?q=iphone&limit=10`,
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const products = response.data;
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Functional
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { getJSON, postJSON } from "@foundatiofx/fetchclient";
|
|
65
|
+
|
|
66
|
+
type Product = { id: number; title: string };
|
|
67
|
+
type Products = { products: Product[] };
|
|
68
|
+
|
|
69
|
+
const response = await postJSON<Product>(
|
|
70
|
+
"https://dummyjson.com/products/add",
|
|
71
|
+
{
|
|
72
|
+
name: "iPhone 13",
|
|
73
|
+
},
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
const product = await getJSON<Product>(
|
|
77
|
+
`https://dummyjson.com/products/${response.data!.id}`,
|
|
78
|
+
);
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Model Validator
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { FetchClient, setModelValidator } from "@foundatiofx/fetchclient";
|
|
85
|
+
|
|
86
|
+
setModelValidator(async (data: object | null) => {
|
|
87
|
+
// use zod or any other validator
|
|
88
|
+
const problem = new ProblemDetails();
|
|
89
|
+
const d = data as { password: string };
|
|
90
|
+
if (d?.password?.length < 6) {
|
|
91
|
+
problem.errors.password = [
|
|
92
|
+
"Password must be longer than or equal to 6 characters.",
|
|
93
|
+
];
|
|
94
|
+
}
|
|
95
|
+
return problem;
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const client = new FetchClient();
|
|
99
|
+
const data = {
|
|
100
|
+
email: "test@test",
|
|
101
|
+
password: "test",
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const response = await client.postJSON(
|
|
105
|
+
"https://jsonplaceholder.typicode.com/todos/1",
|
|
106
|
+
data,
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
if (!response.ok) {
|
|
110
|
+
// check response problem
|
|
111
|
+
console.log(response.problem.detail);
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Caching
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
import { FetchClient } from "@foundatiofx/fetchclient";
|
|
119
|
+
|
|
120
|
+
type Todo = { userId: number; id: number; title: string; completed: boolean };
|
|
121
|
+
|
|
122
|
+
const client = new FetchClient();
|
|
123
|
+
const response = await client.getJSON<Todo>(
|
|
124
|
+
`https://jsonplaceholder.typicode.com/todos/1`,
|
|
125
|
+
{
|
|
126
|
+
cacheKey: ["todos", "1"],
|
|
127
|
+
cacheDuration: 1000 * 60, // expires in 1 minute
|
|
128
|
+
},
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
// invalidate programmatically
|
|
132
|
+
client.cache.delete(["todos", "1"]);
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Middleware
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { FetchClient, useMiddleware } from "@foundatiofx/fetchclient";
|
|
139
|
+
|
|
140
|
+
type Products = {
|
|
141
|
+
products: Array<{ id: number; name: string }>;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
useMiddleware(async (ctx, next) => {
|
|
145
|
+
console.log("starting request");
|
|
146
|
+
await next();
|
|
147
|
+
console.log("completed request");
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const client = new FetchClient();
|
|
151
|
+
const response = await client.getJSON<Products>(
|
|
152
|
+
`https://dummyjson.com/products/search?q=iphone&limit=10`,
|
|
153
|
+
);
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Rate Limiting
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
import { FetchClient, useRateLimit } from "@foundatiofx/fetchclient";
|
|
160
|
+
|
|
161
|
+
// Enable rate limiting globally with 100 requests per minute
|
|
162
|
+
useRateLimit({
|
|
163
|
+
maxRequests: 100,
|
|
164
|
+
windowSeconds: 60,
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const client = new FetchClient();
|
|
168
|
+
const response = await client.getJSON(
|
|
169
|
+
`https://api.example.com/data`,
|
|
170
|
+
);
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Request Timeout
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
import { FetchClient } from "@foundatiofx/fetchclient";
|
|
177
|
+
|
|
178
|
+
const client = new FetchClient();
|
|
179
|
+
|
|
180
|
+
// Set timeout for individual requests
|
|
181
|
+
const response = await client.getJSON(
|
|
182
|
+
`https://api.example.com/data`,
|
|
183
|
+
{ timeout: 5000 }, // 5 seconds
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
// Use AbortSignal for cancellation
|
|
187
|
+
const controller = new AbortController();
|
|
188
|
+
setTimeout(() => controller.abort(), 1000);
|
|
189
|
+
|
|
190
|
+
const response2 = await client.getJSON(
|
|
191
|
+
`https://api.example.com/data`,
|
|
192
|
+
{ signal: controller.signal },
|
|
193
|
+
);
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Error Handling
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
import { FetchClient } from "@foundatiofx/fetchclient";
|
|
200
|
+
|
|
201
|
+
const client = new FetchClient();
|
|
202
|
+
|
|
203
|
+
try {
|
|
204
|
+
const response = await client.getJSON(`https://api.example.com/data`);
|
|
205
|
+
} catch (error) {
|
|
206
|
+
// Handle HTTP errors (4xx, 5xx)
|
|
207
|
+
if (error.problem) {
|
|
208
|
+
console.log(error.problem.title);
|
|
209
|
+
console.log(error.problem.detail);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Or handle specific status codes
|
|
214
|
+
const response = await client.getJSON(
|
|
215
|
+
`https://api.example.com/data`,
|
|
216
|
+
{
|
|
217
|
+
expectedStatusCodes: [404, 500],
|
|
218
|
+
errorCallback: (response) => {
|
|
219
|
+
if (response.status === 404) {
|
|
220
|
+
console.log("Resource not found");
|
|
221
|
+
return true; // Don't throw
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
);
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Authentication
|
|
229
|
+
|
|
230
|
+
```ts
|
|
231
|
+
import { FetchClient, setAccessTokenFunc } from "@foundatiofx/fetchclient";
|
|
232
|
+
|
|
233
|
+
// Set global access token function
|
|
234
|
+
setAccessTokenFunc(() => localStorage.getItem("token"));
|
|
235
|
+
|
|
236
|
+
const client = new FetchClient();
|
|
237
|
+
const response = await client.getJSON(`https://api.example.com/data`);
|
|
238
|
+
// Automatically adds Authorization: Bearer <token> header
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### Base URL
|
|
242
|
+
|
|
243
|
+
```ts
|
|
244
|
+
import { FetchClient, setBaseUrl } from "@foundatiofx/fetchclient";
|
|
245
|
+
|
|
246
|
+
// Set global base URL
|
|
247
|
+
setBaseUrl("https://api.example.com");
|
|
248
|
+
|
|
249
|
+
const client = new FetchClient();
|
|
250
|
+
const response = await client.getJSON(`/users/123`);
|
|
251
|
+
// Requests to https://api.example.com/users/123
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### Loading State
|
|
255
|
+
|
|
256
|
+
```ts
|
|
257
|
+
import { FetchClient } from "@foundatiofx/fetchclient";
|
|
258
|
+
|
|
259
|
+
const client = new FetchClient();
|
|
260
|
+
|
|
261
|
+
// Track loading state
|
|
262
|
+
client.loading.on((isLoading) => {
|
|
263
|
+
console.log(`Loading: ${isLoading}`);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
// Check current loading state
|
|
267
|
+
console.log(client.isLoading);
|
|
268
|
+
console.log(client.requestCount);
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
Also, take a look at the tests:
|
|
272
|
+
|
|
273
|
+
[FetchClient Tests](src/FetchClient.test.ts)
|
|
274
|
+
|
|
275
|
+
## Contributing
|
|
276
|
+
|
|
277
|
+
Run tests:
|
|
278
|
+
|
|
279
|
+
```shell
|
|
280
|
+
deno run test
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
Lint code:
|
|
284
|
+
|
|
285
|
+
```shell
|
|
286
|
+
deno lint
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
Format code:
|
|
290
|
+
|
|
291
|
+
```shell
|
|
292
|
+
deno fmt
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
Type check code:
|
|
296
|
+
|
|
297
|
+
```shell
|
|
298
|
+
deno run check
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
## License
|
|
302
|
+
|
|
303
|
+
MIT © [Foundatio](https://exceptionless.com)
|
package/script/mod.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.FetchClientProvider = exports.defaultProviderInstance = exports.FetchClientCache = exports.ProblemDetails = exports.FetchClient = void 0;
|
|
18
|
+
var FetchClient_js_1 = require("./src/FetchClient.js");
|
|
19
|
+
Object.defineProperty(exports, "FetchClient", { enumerable: true, get: function () { return FetchClient_js_1.FetchClient; } });
|
|
20
|
+
var ProblemDetails_js_1 = require("./src/ProblemDetails.js");
|
|
21
|
+
Object.defineProperty(exports, "ProblemDetails", { enumerable: true, get: function () { return ProblemDetails_js_1.ProblemDetails; } });
|
|
22
|
+
var FetchClientCache_js_1 = require("./src/FetchClientCache.js");
|
|
23
|
+
Object.defineProperty(exports, "FetchClientCache", { enumerable: true, get: function () { return FetchClientCache_js_1.FetchClientCache; } });
|
|
24
|
+
var FetchClientProvider_js_1 = require("./src/FetchClientProvider.js");
|
|
25
|
+
Object.defineProperty(exports, "defaultProviderInstance", { enumerable: true, get: function () { return FetchClientProvider_js_1.defaultInstance; } });
|
|
26
|
+
Object.defineProperty(exports, "FetchClientProvider", { enumerable: true, get: function () { return FetchClientProvider_js_1.FetchClientProvider; } });
|
|
27
|
+
__exportStar(require("./src/DefaultHelpers.js"), exports);
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Counter = void 0;
|
|
4
|
+
const ObjectEvent_js_1 = require("./ObjectEvent.js");
|
|
5
|
+
/**
|
|
6
|
+
* Represents a counter that can be incremented and decremented.
|
|
7
|
+
*/
|
|
8
|
+
class Counter {
|
|
9
|
+
#count = 0;
|
|
10
|
+
#onChange = new ObjectEvent_js_1.ObjectEvent();
|
|
11
|
+
/**
|
|
12
|
+
* Gets the current count.
|
|
13
|
+
*/
|
|
14
|
+
get count() {
|
|
15
|
+
return this.#count;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Gets an event that is triggered when the count changes.
|
|
19
|
+
*/
|
|
20
|
+
get changed() {
|
|
21
|
+
return this.#onChange.expose();
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Increments the count by 1.
|
|
25
|
+
*/
|
|
26
|
+
increment() {
|
|
27
|
+
const previous = this.#count;
|
|
28
|
+
this.#count++;
|
|
29
|
+
this.#onChange.trigger({ previous, value: this.#count });
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Decrements the count by 1.
|
|
33
|
+
*/
|
|
34
|
+
decrement() {
|
|
35
|
+
const previous = this.#count;
|
|
36
|
+
this.#count--;
|
|
37
|
+
this.#onChange.trigger({ previous, value: this.#count });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.Counter = Counter;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useFetchClient = useFetchClient;
|
|
4
|
+
exports.getJSON = getJSON;
|
|
5
|
+
exports.postJSON = postJSON;
|
|
6
|
+
exports.putJSON = putJSON;
|
|
7
|
+
exports.patchJSON = patchJSON;
|
|
8
|
+
exports.deleteJSON = deleteJSON;
|
|
9
|
+
exports.getCurrentProvider = getCurrentProvider;
|
|
10
|
+
exports.setCurrentProviderFunc = setCurrentProviderFunc;
|
|
11
|
+
exports.setBaseUrl = setBaseUrl;
|
|
12
|
+
exports.setAccessTokenFunc = setAccessTokenFunc;
|
|
13
|
+
exports.setModelValidator = setModelValidator;
|
|
14
|
+
exports.useMiddleware = useMiddleware;
|
|
15
|
+
exports.setRequestOptions = setRequestOptions;
|
|
16
|
+
exports.useRateLimit = useRateLimit;
|
|
17
|
+
exports.usePerDomainRateLimit = usePerDomainRateLimit;
|
|
18
|
+
const FetchClientProvider_js_1 = require("./FetchClientProvider.js");
|
|
19
|
+
let getCurrentProviderFunc = () => null;
|
|
20
|
+
/**
|
|
21
|
+
* Gets a FetchClient instance from the current provider.
|
|
22
|
+
* @returns The FetchClient instance.
|
|
23
|
+
*/
|
|
24
|
+
function useFetchClient(options) {
|
|
25
|
+
return getCurrentProvider().getFetchClient(options);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Sends a GET request to the specified URL using the default client and provider and returns the response as JSON.
|
|
29
|
+
* @param url - The URL to send the GET request to.
|
|
30
|
+
* @param options - Optional request options.
|
|
31
|
+
* @returns A promise that resolves to the response as JSON.
|
|
32
|
+
*/
|
|
33
|
+
function getJSON(url, options) {
|
|
34
|
+
return useFetchClient().getJSON(url, options);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Sends a POST request with JSON payload using the default client and provider to the specified URL.
|
|
38
|
+
*
|
|
39
|
+
* @template T - The type of the response data.
|
|
40
|
+
* @param {string} url - The URL to send the request to.
|
|
41
|
+
* @param {object | string | FormData} [body] - The JSON payload or form data to send with the request.
|
|
42
|
+
* @param {RequestOptions} [options] - Additional options for the request.
|
|
43
|
+
* @returns {Promise<FetchClientResponse<T>>} - A promise that resolves to the response data.
|
|
44
|
+
*/
|
|
45
|
+
function postJSON(url, body, options) {
|
|
46
|
+
return useFetchClient().postJSON(url, body, options);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Sends a PUT request with JSON payload using the default client and provider to the specified URL.
|
|
50
|
+
*
|
|
51
|
+
* @template T - The type of the response data.
|
|
52
|
+
* @param {string} url - The URL to send the request to.
|
|
53
|
+
* @param {object | string} [body] - The JSON payload to send with the request.
|
|
54
|
+
* @param {RequestOptions} [options] - Additional options for the request.
|
|
55
|
+
* @returns {Promise<FetchClientResponse<T>>} - A promise that resolves to the response data.
|
|
56
|
+
*/
|
|
57
|
+
function putJSON(url, body, options) {
|
|
58
|
+
return useFetchClient().putJSON(url, body, options);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Sends a PATCH request with JSON payload using the default client and provider to the specified URL.
|
|
62
|
+
*
|
|
63
|
+
* @template T - The type of the response data.
|
|
64
|
+
* @param {string} url - The URL to send the request to.
|
|
65
|
+
* @param {object | string} [body] - The JSON payload to send with the request.
|
|
66
|
+
* @param {RequestOptions} [options] - Additional options for the request.
|
|
67
|
+
* @returns {Promise<FetchClientResponse<T>>} - A promise that resolves to the response data.
|
|
68
|
+
*/
|
|
69
|
+
function patchJSON(url, body, options) {
|
|
70
|
+
return useFetchClient().patchJSON(url, body, options);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Sends a DELETE request with JSON payload using the default client and provider to the specified URL.
|
|
74
|
+
*
|
|
75
|
+
* @template T - The type of the response data.
|
|
76
|
+
* @param {string} url - The URL to send the request to.
|
|
77
|
+
* @param {RequestOptions} [options] - Additional options for the request.
|
|
78
|
+
* @returns {Promise<FetchClientResponse<T>>} - A promise that resolves to the response data.
|
|
79
|
+
*/
|
|
80
|
+
function deleteJSON(url, options) {
|
|
81
|
+
return useFetchClient().deleteJSON(url, options);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Gets the current FetchClientProvider.
|
|
85
|
+
* @returns The current FetchClientProvider.
|
|
86
|
+
*/
|
|
87
|
+
function getCurrentProvider() {
|
|
88
|
+
if (getCurrentProviderFunc === null) {
|
|
89
|
+
return FetchClientProvider_js_1.defaultInstance;
|
|
90
|
+
}
|
|
91
|
+
return getCurrentProviderFunc() ?? FetchClientProvider_js_1.defaultInstance;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Sets the function that retrieves the current FetchClientProvider using whatever scoping mechanism is available.
|
|
95
|
+
* @param getProviderFunc - The function that retrieves the current FetchClientProvider.
|
|
96
|
+
* @returns void
|
|
97
|
+
*/
|
|
98
|
+
function setCurrentProviderFunc(getProviderFunc) {
|
|
99
|
+
getCurrentProviderFunc = getProviderFunc;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Sets the base URL for any FetchClient instances created by the current provider.
|
|
103
|
+
* @param baseUrl - The base URL to use for requests.
|
|
104
|
+
*/
|
|
105
|
+
function setBaseUrl(baseUrl) {
|
|
106
|
+
getCurrentProvider().setBaseUrl(baseUrl);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Sets the access token function for any FetchClient instances created by the current provider.
|
|
110
|
+
* @param accessTokenFunc - The function that retrieves the access token.
|
|
111
|
+
*/
|
|
112
|
+
function setAccessTokenFunc(accessTokenFunc) {
|
|
113
|
+
getCurrentProvider().setAccessTokenFunc(accessTokenFunc);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Sets the model validator function for any FetchClient instances created by the current provider.
|
|
117
|
+
* @param validate - The function that validates the model.
|
|
118
|
+
*/
|
|
119
|
+
function setModelValidator(validate) {
|
|
120
|
+
getCurrentProvider().setModelValidator(validate);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Adds a middleware to any FetchClient instances created by the current provider.
|
|
124
|
+
* @param middleware - The middleware function to be added.
|
|
125
|
+
*/
|
|
126
|
+
function useMiddleware(middleware) {
|
|
127
|
+
getCurrentProvider().useMiddleware(middleware);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Sets the default request options for any FetchClient instances created by the current provider.
|
|
131
|
+
* @param options - The options to set as the default request options.
|
|
132
|
+
*/
|
|
133
|
+
function setRequestOptions(options) {
|
|
134
|
+
getCurrentProvider().applyOptions({ defaultRequestOptions: options });
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Enables rate limiting for any FetchClient instances created by the current provider.
|
|
138
|
+
* @param options - The rate limiting configuration options.
|
|
139
|
+
*/
|
|
140
|
+
function useRateLimit(options) {
|
|
141
|
+
getCurrentProvider().useRateLimit(options);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Enables per-domain rate limiting for any FetchClient instances created by the current provider.
|
|
145
|
+
* @param options - The rate limiting configuration options.
|
|
146
|
+
*/
|
|
147
|
+
function usePerDomainRateLimit(options) {
|
|
148
|
+
getCurrentProvider().usePerDomainRateLimit(options);
|
|
149
|
+
}
|