@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/esm/mod.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { FetchClient } from "./src/FetchClient.js";
|
|
2
|
+
export { ProblemDetails } from "./src/ProblemDetails.js";
|
|
3
|
+
export { FetchClientCache } from "./src/FetchClientCache.js";
|
|
4
|
+
export { defaultInstance as defaultProviderInstance, FetchClientProvider, } from "./src/FetchClientProvider.js";
|
|
5
|
+
export * from "./src/DefaultHelpers.js";
|
package/esm/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ObjectEvent } from "./ObjectEvent.js";
|
|
2
|
+
/**
|
|
3
|
+
* Represents a counter that can be incremented and decremented.
|
|
4
|
+
*/
|
|
5
|
+
export class Counter {
|
|
6
|
+
#count = 0;
|
|
7
|
+
#onChange = new ObjectEvent();
|
|
8
|
+
/**
|
|
9
|
+
* Gets the current count.
|
|
10
|
+
*/
|
|
11
|
+
get count() {
|
|
12
|
+
return this.#count;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Gets an event that is triggered when the count changes.
|
|
16
|
+
*/
|
|
17
|
+
get changed() {
|
|
18
|
+
return this.#onChange.expose();
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Increments the count by 1.
|
|
22
|
+
*/
|
|
23
|
+
increment() {
|
|
24
|
+
const previous = this.#count;
|
|
25
|
+
this.#count++;
|
|
26
|
+
this.#onChange.trigger({ previous, value: this.#count });
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Decrements the count by 1.
|
|
30
|
+
*/
|
|
31
|
+
decrement() {
|
|
32
|
+
const previous = this.#count;
|
|
33
|
+
this.#count--;
|
|
34
|
+
this.#onChange.trigger({ previous, value: this.#count });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { defaultInstance as defaultProvider, } from "./FetchClientProvider.js";
|
|
2
|
+
let getCurrentProviderFunc = () => null;
|
|
3
|
+
/**
|
|
4
|
+
* Gets a FetchClient instance from the current provider.
|
|
5
|
+
* @returns The FetchClient instance.
|
|
6
|
+
*/
|
|
7
|
+
export function useFetchClient(options) {
|
|
8
|
+
return getCurrentProvider().getFetchClient(options);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Sends a GET request to the specified URL using the default client and provider and returns the response as JSON.
|
|
12
|
+
* @param url - The URL to send the GET request to.
|
|
13
|
+
* @param options - Optional request options.
|
|
14
|
+
* @returns A promise that resolves to the response as JSON.
|
|
15
|
+
*/
|
|
16
|
+
export function getJSON(url, options) {
|
|
17
|
+
return useFetchClient().getJSON(url, options);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Sends a POST request with JSON payload using the default client and provider to the specified URL.
|
|
21
|
+
*
|
|
22
|
+
* @template T - The type of the response data.
|
|
23
|
+
* @param {string} url - The URL to send the request to.
|
|
24
|
+
* @param {object | string | FormData} [body] - The JSON payload or form data to send with the request.
|
|
25
|
+
* @param {RequestOptions} [options] - Additional options for the request.
|
|
26
|
+
* @returns {Promise<FetchClientResponse<T>>} - A promise that resolves to the response data.
|
|
27
|
+
*/
|
|
28
|
+
export function postJSON(url, body, options) {
|
|
29
|
+
return useFetchClient().postJSON(url, body, options);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Sends a PUT request with JSON payload using the default client and provider to the specified URL.
|
|
33
|
+
*
|
|
34
|
+
* @template T - The type of the response data.
|
|
35
|
+
* @param {string} url - The URL to send the request to.
|
|
36
|
+
* @param {object | string} [body] - The JSON payload to send with the request.
|
|
37
|
+
* @param {RequestOptions} [options] - Additional options for the request.
|
|
38
|
+
* @returns {Promise<FetchClientResponse<T>>} - A promise that resolves to the response data.
|
|
39
|
+
*/
|
|
40
|
+
export function putJSON(url, body, options) {
|
|
41
|
+
return useFetchClient().putJSON(url, body, options);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Sends a PATCH request with JSON payload using the default client and provider to the specified URL.
|
|
45
|
+
*
|
|
46
|
+
* @template T - The type of the response data.
|
|
47
|
+
* @param {string} url - The URL to send the request to.
|
|
48
|
+
* @param {object | string} [body] - The JSON payload to send with the request.
|
|
49
|
+
* @param {RequestOptions} [options] - Additional options for the request.
|
|
50
|
+
* @returns {Promise<FetchClientResponse<T>>} - A promise that resolves to the response data.
|
|
51
|
+
*/
|
|
52
|
+
export function patchJSON(url, body, options) {
|
|
53
|
+
return useFetchClient().patchJSON(url, body, options);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Sends a DELETE request with JSON payload using the default client and provider to the specified URL.
|
|
57
|
+
*
|
|
58
|
+
* @template T - The type of the response data.
|
|
59
|
+
* @param {string} url - The URL to send the request to.
|
|
60
|
+
* @param {RequestOptions} [options] - Additional options for the request.
|
|
61
|
+
* @returns {Promise<FetchClientResponse<T>>} - A promise that resolves to the response data.
|
|
62
|
+
*/
|
|
63
|
+
export function deleteJSON(url, options) {
|
|
64
|
+
return useFetchClient().deleteJSON(url, options);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Gets the current FetchClientProvider.
|
|
68
|
+
* @returns The current FetchClientProvider.
|
|
69
|
+
*/
|
|
70
|
+
export function getCurrentProvider() {
|
|
71
|
+
if (getCurrentProviderFunc === null) {
|
|
72
|
+
return defaultProvider;
|
|
73
|
+
}
|
|
74
|
+
return getCurrentProviderFunc() ?? defaultProvider;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Sets the function that retrieves the current FetchClientProvider using whatever scoping mechanism is available.
|
|
78
|
+
* @param getProviderFunc - The function that retrieves the current FetchClientProvider.
|
|
79
|
+
* @returns void
|
|
80
|
+
*/
|
|
81
|
+
export function setCurrentProviderFunc(getProviderFunc) {
|
|
82
|
+
getCurrentProviderFunc = getProviderFunc;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Sets the base URL for any FetchClient instances created by the current provider.
|
|
86
|
+
* @param baseUrl - The base URL to use for requests.
|
|
87
|
+
*/
|
|
88
|
+
export function setBaseUrl(baseUrl) {
|
|
89
|
+
getCurrentProvider().setBaseUrl(baseUrl);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Sets the access token function for any FetchClient instances created by the current provider.
|
|
93
|
+
* @param accessTokenFunc - The function that retrieves the access token.
|
|
94
|
+
*/
|
|
95
|
+
export function setAccessTokenFunc(accessTokenFunc) {
|
|
96
|
+
getCurrentProvider().setAccessTokenFunc(accessTokenFunc);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Sets the model validator function for any FetchClient instances created by the current provider.
|
|
100
|
+
* @param validate - The function that validates the model.
|
|
101
|
+
*/
|
|
102
|
+
export function setModelValidator(validate) {
|
|
103
|
+
getCurrentProvider().setModelValidator(validate);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Adds a middleware to any FetchClient instances created by the current provider.
|
|
107
|
+
* @param middleware - The middleware function to be added.
|
|
108
|
+
*/
|
|
109
|
+
export function useMiddleware(middleware) {
|
|
110
|
+
getCurrentProvider().useMiddleware(middleware);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Sets the default request options for any FetchClient instances created by the current provider.
|
|
114
|
+
* @param options - The options to set as the default request options.
|
|
115
|
+
*/
|
|
116
|
+
export function setRequestOptions(options) {
|
|
117
|
+
getCurrentProvider().applyOptions({ defaultRequestOptions: options });
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Enables rate limiting for any FetchClient instances created by the current provider.
|
|
121
|
+
* @param options - The rate limiting configuration options.
|
|
122
|
+
*/
|
|
123
|
+
export function useRateLimit(options) {
|
|
124
|
+
getCurrentProvider().useRateLimit(options);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Enables per-domain rate limiting for any FetchClient instances created by the current provider.
|
|
128
|
+
* @param options - The rate limiting configuration options.
|
|
129
|
+
*/
|
|
130
|
+
export function usePerDomainRateLimit(options) {
|
|
131
|
+
getCurrentProvider().usePerDomainRateLimit(options);
|
|
132
|
+
}
|