@studiocms/cfetch 0.1.5 → 0.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/README.md +268 -25
- package/dist/cache.d.ts +78 -0
- package/dist/cache.js +89 -0
- package/dist/consts.d.ts +4 -8
- package/dist/consts.js +7 -2
- package/dist/index.d.ts +47 -20
- package/dist/index.js +7 -1
- package/dist/stub.js +145 -68
- package/dist/types.d.ts +26 -41
- package/dist/utils/integration.js +2 -3
- package/dist/wrappers.d.ts +241 -11
- package/dist/wrappers.js +79 -39
- package/package.json +8 -6
- package/dist/utils/isOlderThan.d.ts +0 -13
- package/dist/utils/isOlderThan.js +0 -13
package/dist/stub.js
CHANGED
|
@@ -2,90 +2,167 @@ import { createResolver } from "./utils/integration.js";
|
|
|
2
2
|
const { resolve } = createResolver(import.meta.url);
|
|
3
3
|
const stub = `
|
|
4
4
|
declare module 'virtual:cfetch/config' {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*
|
|
8
|
-
* @property lifetime - Specifies the duration for which the cache is valid.
|
|
9
|
-
* The format should be a template literal string representing
|
|
10
|
-
* either minutes (\`<number>m\`) or hours (\`<number>h\`).
|
|
11
|
-
* For example: "5m" for 5 minutes or "2h" for 2 hours.
|
|
12
|
-
*/
|
|
13
|
-
const defaultConfig: import(${resolve("./types")}).CacheConfig;
|
|
5
|
+
type CacheConfig = import("${resolve("./types.js")}").CacheConfigLive;
|
|
6
|
+
const defaultConfig: CacheConfig;
|
|
14
7
|
export default defaultConfig;
|
|
15
8
|
}
|
|
16
9
|
|
|
17
10
|
declare module 'c:fetch' {
|
|
11
|
+
export type CacheConfig = import("${resolve("./types.js")}").CacheConfig;
|
|
12
|
+
export type CachedResponse<T> = import("${resolve("./wrappers.js")}").CachedResponse<T>;
|
|
13
|
+
export type CFetchConfig = import("${resolve("./wrappers.js")}").CFetchConfig;
|
|
14
|
+
|
|
15
|
+
export const Duration: typeof import("${resolve("./wrappers.js")}").Duration;
|
|
16
|
+
|
|
17
|
+
declare const FetchError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => import("effect/Cause").YieldableError & {
|
|
18
|
+
readonly _tag: "FetchError";
|
|
19
|
+
} & Readonly<A>;
|
|
20
|
+
|
|
18
21
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* This type is derived from the built-in \`fetch\` function, allowing you to
|
|
22
|
-
* use it as a reference for type-safe operations involving \`fetch\`.
|
|
22
|
+
* Custom error type for fetch-related errors.
|
|
23
23
|
*/
|
|
24
|
-
|
|
24
|
+
export declare class FetchError extends FetchError_base<{
|
|
25
|
+
message: string;
|
|
26
|
+
cause?: unknown;
|
|
27
|
+
}> {
|
|
28
|
+
}
|
|
29
|
+
|
|
25
30
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
31
|
+
* No-op parser for HEAD requests.
|
|
32
|
+
*
|
|
33
|
+
* @template U - The type of the parsed data
|
|
34
|
+
* @param _ - The Response object (ignored)
|
|
35
|
+
* @returns A Promise that resolves to undefined
|
|
28
36
|
*/
|
|
29
|
-
|
|
37
|
+
export const noOpParser: typeof import("${resolve("./wrappers.js")}").noOpParser;
|
|
38
|
+
|
|
30
39
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
40
|
+
* Fetches data from a URL with caching capabilities using Effect.
|
|
41
|
+
*
|
|
42
|
+
* This function performs an HTTP request with built-in caching logic. It first checks
|
|
43
|
+
* the cache for existing data, and if not found, fetches from the network, parses the
|
|
44
|
+
* response, and caches successful responses for future use.
|
|
45
|
+
*
|
|
46
|
+
* @template T - The type of the parsed response data
|
|
47
|
+
*
|
|
48
|
+
* @param url - The URL to fetch data from
|
|
49
|
+
* @param parser - A function that parses the Response object into type T
|
|
50
|
+
* @param options - Optional RequestInit configuration for the fetch request
|
|
51
|
+
* @param cacheConfig - Optional cache configuration object
|
|
52
|
+
* @param cacheConfig.ttl - Time-to-live duration for the cached entry
|
|
53
|
+
* @param cacheConfig.tags - Tags to associate with the cached entry for invalidation
|
|
54
|
+
* @param cacheConfig.key - Custom cache key (defaults to URL and options hash)
|
|
55
|
+
*
|
|
56
|
+
* @returns An Effect that yields a CachedResponse containing the parsed data and response metadata
|
|
57
|
+
*
|
|
58
|
+
* @throws {FetchError} When the network request fails or response parsing fails
|
|
59
|
+
*
|
|
60
|
+
* @remarks
|
|
61
|
+
* - Cache keys are automatically generated from the URL and options if not provided
|
|
62
|
+
* - Only successful responses (response.ok === true) are cached
|
|
63
|
+
* - Cache hits are logged to console for debugging
|
|
64
|
+
* - The effect is provided with CacheLive layer automatically
|
|
34
65
|
*/
|
|
35
|
-
|
|
66
|
+
export const cFetchEffect: typeof import("${resolve("./wrappers.js")}").cFetchEffect;
|
|
67
|
+
|
|
36
68
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* @
|
|
40
|
-
* @
|
|
69
|
+
* Creates an Effect that fetches JSON data from a URL with caching support.
|
|
70
|
+
*
|
|
71
|
+
* @template T - The expected type of the JSON response data
|
|
72
|
+
* @param url - The URL to fetch data from
|
|
73
|
+
* @param options - Optional fetch configuration options (headers, method, etc.)
|
|
74
|
+
* @param cacheConfig - Optional cache configuration
|
|
75
|
+
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
76
|
+
* @param cacheConfig.tags - Tags to associate with the cached entry for invalidation
|
|
77
|
+
* @param cacheConfig.key - Custom cache key to use instead of the default URL-based key
|
|
78
|
+
* @returns An Effect that yields a CachedResponse containing the parsed JSON data, or fails with a FetchError
|
|
41
79
|
*/
|
|
42
|
-
|
|
80
|
+
export const cFetchEffectJson: typeof import("${resolve("./wrappers.js")}").cFetchEffectJson;
|
|
81
|
+
|
|
82
|
+
|
|
43
83
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* @
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
84
|
+
* Fetches a resource from the specified URL and returns the response body as text.
|
|
85
|
+
*
|
|
86
|
+
* @param url - The URL to fetch the resource from.
|
|
87
|
+
* @param options - Optional fetch configuration including method, headers, body, etc.
|
|
88
|
+
* @param cacheConfig - Optional cache configuration.
|
|
89
|
+
* @param cacheConfig.ttl - Time-to-live duration for the cached response.
|
|
90
|
+
* @param cacheConfig.tags - Array of tags to associate with the cached entry for invalidation purposes.
|
|
91
|
+
* @param cacheConfig.key - Custom cache key. If not provided, the URL will be used as the key.
|
|
92
|
+
*
|
|
93
|
+
* @returns An Effect that resolves to a CachedResponse containing the response text,
|
|
94
|
+
* or fails with a FetchError if the request fails.
|
|
50
95
|
*/
|
|
51
|
-
export
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
96
|
+
export const cFetchEffectText: typeof import("${resolve("./wrappers.js")}").cFetchEffectText;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Fetches a resource and returns it as a Blob with caching support.
|
|
100
|
+
*
|
|
101
|
+
* @param url - The URL of the resource to fetch
|
|
102
|
+
* @param options - Optional fetch request configuration (headers, method, etc.)
|
|
103
|
+
* @param cacheConfig - Optional cache configuration object
|
|
104
|
+
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
105
|
+
* @param cacheConfig.tags - Array of tags to associate with the cached entry
|
|
106
|
+
* @param cacheConfig.key - Custom cache key to use instead of the default
|
|
107
|
+
*
|
|
108
|
+
* @returns An Effect that resolves to a CachedResponse containing a Blob, or fails with a FetchError
|
|
109
|
+
*/
|
|
110
|
+
export const cFetchEffectBlob: typeof import("${resolve("./wrappers.js")}").cFetchEffectBlob;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Executes a cached fetch request with configurable caching behavior.
|
|
114
|
+
*
|
|
115
|
+
* @template T - The type of data returned after parsing the response
|
|
116
|
+
* @param url - The URL to fetch from
|
|
117
|
+
* @param parser - A function that parses the Response object into the desired type T
|
|
118
|
+
* @param options - Optional fetch request configuration (headers, method, body, etc.)
|
|
119
|
+
* @param cacheConfig - Optional cache configuration object
|
|
120
|
+
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
121
|
+
* @param cacheConfig.tags - Array of tags to associate with the cached entry for invalidation purposes
|
|
122
|
+
* @param cacheConfig.key - Custom cache key; if not provided, a key will be generated from the URL and options
|
|
123
|
+
* @returns A Promise that resolves to a CachedResponse containing the parsed data
|
|
124
|
+
*/
|
|
125
|
+
export const cFetch: typeof import("${resolve("./wrappers.js")}").cFetch;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Fetches and parses JSON data from the specified URL with caching support.
|
|
129
|
+
*
|
|
130
|
+
* @template T - The expected type of the parsed JSON response data
|
|
131
|
+
* @param url - The URL to fetch data from
|
|
132
|
+
* @param options - Optional fetch configuration options (headers, method, body, etc.)
|
|
133
|
+
* @param cacheConfig - Optional cache configuration object
|
|
134
|
+
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
135
|
+
* @param cacheConfig.tags - Array of tags to associate with the cached entry for invalidation purposes
|
|
136
|
+
* @param cacheConfig.key - Custom cache key to use instead of the default
|
|
137
|
+
* @returns A Promise that resolves to a CachedResponse containing the parsed JSON data of type T
|
|
138
|
+
*/
|
|
139
|
+
export const cFetchJson: typeof import("${resolve("./wrappers.js")}").cFetchJson;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Fetches a URL and returns the response as text with caching support.
|
|
143
|
+
*
|
|
144
|
+
* @param url - The URL to fetch
|
|
145
|
+
* @param options - Optional fetch configuration options (headers, method, etc.)
|
|
146
|
+
* @param cacheConfig - Optional cache configuration
|
|
147
|
+
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
148
|
+
* @param cacheConfig.tags - Tags to associate with the cached response for invalidation
|
|
149
|
+
* @param cacheConfig.key - Custom cache key (defaults to URL if not provided)
|
|
150
|
+
* @returns A promise that resolves to a CachedResponse containing the response text
|
|
151
|
+
*/
|
|
152
|
+
export const cFetchText: typeof import("${resolve("./wrappers.js")}").cFetchText;
|
|
153
|
+
|
|
60
154
|
/**
|
|
61
|
-
* Fetches
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
* @param
|
|
66
|
-
* @param
|
|
67
|
-
* @param cacheConfig -
|
|
68
|
-
* @param
|
|
69
|
-
*
|
|
70
|
-
* @param metadata - A boolean indicating whether to return the full cached object (including metadata)
|
|
71
|
-
* or just the data. Defaults to \`false\`.
|
|
72
|
-
* @returns The fetched or cached data. If \`full\` is \`true\`, returns an object containing
|
|
73
|
-
* both the data and metadata (e.g., \`lastCheck\`).
|
|
74
|
-
* @throws An error if fetching new data fails and no cached data is available.
|
|
155
|
+
* Fetches a Blob resource from the specified URL with optional caching configuration.
|
|
156
|
+
*
|
|
157
|
+
* @param url - The URL to fetch the Blob resource from
|
|
158
|
+
* @param options - Optional fetch request configuration (headers, method, etc.)
|
|
159
|
+
* @param cacheConfig - Optional cache configuration object
|
|
160
|
+
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
161
|
+
* @param cacheConfig.tags - Array of cache tags for cache invalidation
|
|
162
|
+
* @param cacheConfig.key - Custom cache key for storing the response
|
|
163
|
+
* @returns A Promise that resolves to a CachedResponse containing a Blob
|
|
75
164
|
*/
|
|
76
|
-
export
|
|
77
|
-
input: Input,
|
|
78
|
-
init?: Init,
|
|
79
|
-
cacheConfig?: Partial<CacheConfig>,
|
|
80
|
-
type?: 'json' | 'text'
|
|
81
|
-
): Promise<Response>;
|
|
82
|
-
export function cFetch(
|
|
83
|
-
input: Input,
|
|
84
|
-
init?: Init,
|
|
85
|
-
cacheConfig?: Partial<CacheConfig>,
|
|
86
|
-
type?: 'json' | 'text',
|
|
87
|
-
metadata?: boolean
|
|
88
|
-
): Promise<CacheDataValue>;
|
|
165
|
+
export const cFetchBlob: typeof import("${resolve("./wrappers.js")}").cFetchBlob;
|
|
89
166
|
}
|
|
90
167
|
|
|
91
168
|
`;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,49 +1,34 @@
|
|
|
1
|
+
import type { Duration } from 'effect';
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
-
* @module
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* Represents the type of the global `fetch` function.
|
|
3
|
+
* Configuration options for caching behavior.
|
|
7
4
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*/
|
|
11
|
-
export type FetchType = typeof fetch;
|
|
12
|
-
/**
|
|
13
|
-
* Represents the input parameter type for the `FetchType` function.
|
|
14
|
-
* This type is derived from the first parameter of the `FetchType` function.
|
|
15
|
-
*/
|
|
16
|
-
export type Input = Parameters<FetchType>[0];
|
|
17
|
-
/**
|
|
18
|
-
* Represents the `init` parameter of the `fetch` function, which is the second parameter
|
|
19
|
-
* in the `FetchType` function signature. This type is used to configure the request,
|
|
20
|
-
* including options such as method, headers, body, and other settings.
|
|
21
|
-
*/
|
|
22
|
-
export type Init = Parameters<FetchType>[1];
|
|
23
|
-
/**
|
|
24
|
-
* Represents the structure of cached data.
|
|
5
|
+
* @remarks
|
|
6
|
+
* This type defines how long cached data should be retained before being considered stale.
|
|
25
7
|
*
|
|
26
|
-
* @
|
|
27
|
-
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const config: CacheConfig = {
|
|
11
|
+
* lifetime: Duration.seconds(60)
|
|
12
|
+
* };
|
|
13
|
+
* ```
|
|
28
14
|
*/
|
|
29
|
-
export type
|
|
30
|
-
|
|
31
|
-
data: Response;
|
|
15
|
+
export type CacheConfig = {
|
|
16
|
+
lifetime: Duration.DurationInput;
|
|
32
17
|
};
|
|
33
18
|
/**
|
|
34
|
-
*
|
|
19
|
+
* Configuration options for cache lifetime management.
|
|
35
20
|
*
|
|
36
|
-
* @
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
21
|
+
* @remarks
|
|
22
|
+
* This type defines the configuration for controlling how long cached data remains valid
|
|
23
|
+
* before it needs to be refreshed or invalidated.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const cacheConfig: CacheConfigLive = {
|
|
28
|
+
* lifetime: 3600000 // Cache valid for 1 hour (in milliseconds)
|
|
29
|
+
* };
|
|
30
|
+
* ```
|
|
40
31
|
*/
|
|
41
|
-
export
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
* The format should be a template literal string representing
|
|
45
|
-
* either minutes (`<number>m`) or hours (`<number>h`).
|
|
46
|
-
* For example: "5m" for 5 minutes or "2h" for 2 hours.
|
|
47
|
-
*/
|
|
48
|
-
lifetime: `${number}m` | `${number}h`;
|
|
49
|
-
}
|
|
32
|
+
export type CacheConfigLive = {
|
|
33
|
+
lifetime: number;
|
|
34
|
+
};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { fileURLToPath } from "node:url";
|
|
2
|
-
import { AstroError } from "astro/errors";
|
|
3
2
|
const _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/;
|
|
4
3
|
const _DRIVE_LETTER_RE = /^[A-Za-z]:$/;
|
|
5
4
|
const _DRIVE_LETTER_START_RE = /^[A-Za-z]:\//;
|
|
@@ -200,7 +199,7 @@ const createVirtualModule = (name, _imports, bypassCoreValidation) => {
|
|
|
200
199
|
}
|
|
201
200
|
for (const [id, contexts] of Object.entries(duplicatedImports)) {
|
|
202
201
|
if (contexts.length !== [...new Set(contexts)].length) {
|
|
203
|
-
throw new
|
|
202
|
+
throw new Error(
|
|
204
203
|
`Virtual import with id "${id}" has been registered several times with conflicting contexts.`
|
|
205
204
|
);
|
|
206
205
|
}
|
|
@@ -208,7 +207,7 @@ const createVirtualModule = (name, _imports, bypassCoreValidation) => {
|
|
|
208
207
|
const resolutionMap = Object.fromEntries(
|
|
209
208
|
imports.map(({ id }) => {
|
|
210
209
|
if (!bypassCoreValidation && id.startsWith("astro:")) {
|
|
211
|
-
throw new
|
|
210
|
+
throw new Error(
|
|
212
211
|
`Virtual import name prefix can't be "astro:" (for "${id}") because it's reserved for Astro core.`
|
|
213
212
|
);
|
|
214
213
|
}
|
package/dist/wrappers.d.ts
CHANGED
|
@@ -1,15 +1,245 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* @module cfetch/wrappers
|
|
3
|
+
*
|
|
4
|
+
* Provides cached fetch wrappers using Effect for caching capabilities.
|
|
5
|
+
*
|
|
6
|
+
* This module exports functions to perform HTTP requests with built-in caching logic.
|
|
7
|
+
* It defines types for cached responses and errors, and implements functions to fetch
|
|
8
|
+
* data with various parsing options (JSON, text, Blob) while leveraging an in-memory
|
|
9
|
+
* cache layer.
|
|
4
10
|
*/
|
|
5
|
-
import
|
|
6
|
-
export type { CacheConfig };
|
|
11
|
+
import { type Duration, Effect } from 'effect';
|
|
12
|
+
export type { CacheConfig } from './types.js';
|
|
13
|
+
export { Duration } from 'effect';
|
|
14
|
+
declare const FetchError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => import("effect/Cause").YieldableError & {
|
|
15
|
+
readonly _tag: "FetchError";
|
|
16
|
+
} & Readonly<A>;
|
|
7
17
|
/**
|
|
8
|
-
*
|
|
18
|
+
* Custom error type for fetch-related errors.
|
|
9
19
|
*/
|
|
10
|
-
export declare
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export
|
|
20
|
+
export declare class FetchError extends FetchError_base<{
|
|
21
|
+
message: string;
|
|
22
|
+
cause?: unknown;
|
|
23
|
+
}> {
|
|
24
|
+
}
|
|
25
|
+
export interface CachedResponse<T> {
|
|
26
|
+
data: T;
|
|
27
|
+
ok: boolean;
|
|
28
|
+
status: number;
|
|
29
|
+
statusText: string;
|
|
30
|
+
headers: Record<string, string>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Configuration options for cached fetch requests.
|
|
34
|
+
*/
|
|
35
|
+
export interface CFetchConfig {
|
|
36
|
+
ttl?: Duration.DurationInput;
|
|
37
|
+
tags?: string[];
|
|
38
|
+
key?: string;
|
|
39
|
+
verbose?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* No-op parser for HEAD requests.
|
|
43
|
+
*
|
|
44
|
+
* @template U - The type of the parsed data
|
|
45
|
+
* @param _ - The Response object (ignored)
|
|
46
|
+
* @returns A Promise that resolves to undefined
|
|
47
|
+
*/
|
|
48
|
+
export declare const noOpParser: <U>(_: Response) => Promise<U>;
|
|
49
|
+
/**
|
|
50
|
+
* Fetches data from a URL with caching capabilities using Effect.
|
|
51
|
+
*
|
|
52
|
+
* This function performs an HTTP request with built-in caching logic. It first checks
|
|
53
|
+
* the cache for existing data, and if not found, fetches from the network, parses the
|
|
54
|
+
* response, and caches successful responses for future use.
|
|
55
|
+
*
|
|
56
|
+
* @template T - The type of the parsed response data
|
|
57
|
+
*
|
|
58
|
+
* @param url - The URL to fetch data from
|
|
59
|
+
* @param parser - A function that parses the Response object into type T
|
|
60
|
+
* @param options - Optional RequestInit configuration for the fetch request
|
|
61
|
+
* @param cacheConfig - Optional cache configuration object
|
|
62
|
+
* @param cacheConfig.ttl - Time-to-live duration for the cached entry
|
|
63
|
+
* @param cacheConfig.tags - Tags to associate with the cached entry for invalidation
|
|
64
|
+
* @param cacheConfig.key - Custom cache key (defaults to URL and options hash)
|
|
65
|
+
*
|
|
66
|
+
* @returns An Effect that yields a CachedResponse containing the parsed data and response metadata
|
|
67
|
+
*
|
|
68
|
+
* @throws {FetchError} When the network request fails or response parsing fails
|
|
69
|
+
*
|
|
70
|
+
* @remarks
|
|
71
|
+
* - Cache keys are automatically generated from the URL and options if not provided
|
|
72
|
+
* - Only successful responses (response.ok === true) are cached
|
|
73
|
+
* - Only 2xx responses are cached
|
|
74
|
+
* - Users needing negative caching should implement custom logic
|
|
75
|
+
* - The effect is provided with CacheLive layer automatically
|
|
76
|
+
* - Non-cacheable HTTP methods (e.g. POST, PUT) bypass the cache entirely
|
|
77
|
+
* - The parser function is bypassed for HEAD requests, returning undefined data
|
|
78
|
+
* - Verbose logging can be enabled via cacheConfig.verbose
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* const effect = cFetchEffect(
|
|
83
|
+
* 'https://api.example.com/data',
|
|
84
|
+
* (res) => res.json(),
|
|
85
|
+
* { method: 'GET' },
|
|
86
|
+
* { ttl: Duration.minutes(5), tags: ['api-data'] }
|
|
87
|
+
* );
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export declare const cFetchEffect: <T>(url: string | URL, parser: (response: Response) => Promise<T>, options?: RequestInit, cacheConfig?: CFetchConfig) => Effect.Effect<CachedResponse<T>, FetchError, never>;
|
|
91
|
+
/**
|
|
92
|
+
* Creates an Effect that fetches JSON data from a URL with caching support.
|
|
93
|
+
*
|
|
94
|
+
* @template T - The expected type of the JSON response data
|
|
95
|
+
* @param url - The URL to fetch data from
|
|
96
|
+
* @param options - Optional fetch configuration options (headers, method, etc.)
|
|
97
|
+
* @param cacheConfig - Optional cache configuration
|
|
98
|
+
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
99
|
+
* @param cacheConfig.tags - Tags to associate with the cached entry for invalidation
|
|
100
|
+
* @param cacheConfig.key - Custom cache key to use instead of the default URL-based key
|
|
101
|
+
* @returns An Effect that yields a CachedResponse containing the parsed JSON data, or fails with a FetchError
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* const effect = cFetchEffectJson<User>(
|
|
106
|
+
* 'https://api.example.com/user/123',
|
|
107
|
+
* { method: 'GET' },
|
|
108
|
+
* { ttl: Duration.minutes(5), tags: ['user'] }
|
|
109
|
+
* );
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export declare const cFetchEffectJson: <T>(url: string | URL, options?: RequestInit, cacheConfig?: CFetchConfig) => Effect.Effect<CachedResponse<T>, FetchError, never>;
|
|
113
|
+
/**
|
|
114
|
+
* Fetches a resource from the specified URL and returns the response body as text.
|
|
115
|
+
*
|
|
116
|
+
* @param url - The URL to fetch the resource from.
|
|
117
|
+
* @param options - Optional fetch configuration including method, headers, body, etc.
|
|
118
|
+
* @param cacheConfig - Optional cache configuration.
|
|
119
|
+
* @param cacheConfig.ttl - Time-to-live duration for the cached response.
|
|
120
|
+
* @param cacheConfig.tags - Array of tags to associate with the cached entry for invalidation purposes.
|
|
121
|
+
* @param cacheConfig.key - Custom cache key. If not provided, the URL will be used as the key.
|
|
122
|
+
*
|
|
123
|
+
* @returns An Effect that resolves to a CachedResponse containing the response text,
|
|
124
|
+
* or fails with a FetchError if the request fails.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```typescript
|
|
128
|
+
* const textEffect = cFetchEffectText(
|
|
129
|
+
* 'https://api.example.com/data',
|
|
130
|
+
* { method: 'GET' },
|
|
131
|
+
* { ttl: Duration.minutes(5), tags: ['api', 'data'] }
|
|
132
|
+
* );
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
export declare const cFetchEffectText: (url: string | URL, options?: RequestInit, cacheConfig?: CFetchConfig) => Effect.Effect<CachedResponse<string>, FetchError, never>;
|
|
136
|
+
/**
|
|
137
|
+
* Fetches a resource and returns it as a Blob with caching support.
|
|
138
|
+
*
|
|
139
|
+
* @param url - The URL of the resource to fetch
|
|
140
|
+
* @param options - Optional fetch request configuration (headers, method, etc.)
|
|
141
|
+
* @param cacheConfig - Optional cache configuration object
|
|
142
|
+
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
143
|
+
* @param cacheConfig.tags - Array of tags to associate with the cached entry
|
|
144
|
+
* @param cacheConfig.key - Custom cache key to use instead of the default
|
|
145
|
+
*
|
|
146
|
+
* @returns An Effect that resolves to a CachedResponse containing a Blob, or fails with a FetchError
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```typescript
|
|
150
|
+
* const imageEffect = cFetchEffectBlob(
|
|
151
|
+
* 'https://example.com/image.png',
|
|
152
|
+
* { method: 'GET' },
|
|
153
|
+
* { ttl: Duration.hours(1), tags: ['images'] }
|
|
154
|
+
* );
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
export declare const cFetchEffectBlob: (url: string | URL, options?: RequestInit, cacheConfig?: CFetchConfig) => Effect.Effect<CachedResponse<Blob>, FetchError, never>;
|
|
158
|
+
/**
|
|
159
|
+
* Executes a cached fetch request with configurable caching behavior.
|
|
160
|
+
*
|
|
161
|
+
* @template T - The type of data returned after parsing the response
|
|
162
|
+
* @param url - The URL to fetch from
|
|
163
|
+
* @param parser - A function that parses the Response object into the desired type T
|
|
164
|
+
* @param options - Optional fetch request configuration (headers, method, body, etc.)
|
|
165
|
+
* @param cacheConfig - Optional cache configuration object
|
|
166
|
+
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
167
|
+
* @param cacheConfig.tags - Array of tags to associate with the cached entry for invalidation purposes
|
|
168
|
+
* @param cacheConfig.key - Custom cache key; if not provided, a key will be generated from the URL and options
|
|
169
|
+
* @returns A Promise that resolves to a CachedResponse containing the parsed data
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```typescript
|
|
173
|
+
* const result = await cFetch(
|
|
174
|
+
* 'https://api.example.com/data',
|
|
175
|
+
* (res) => res.json(),
|
|
176
|
+
* { method: 'GET' },
|
|
177
|
+
* { ttl: Duration.minutes(5), tags: ['api-data'] }
|
|
178
|
+
* );
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
export declare const cFetch: <T>(url: string | URL, parser: (response: Response) => Promise<T>, options?: RequestInit, cacheConfig?: CFetchConfig) => Promise<CachedResponse<T>>;
|
|
182
|
+
/**
|
|
183
|
+
* Fetches and parses JSON data from the specified URL with caching support.
|
|
184
|
+
*
|
|
185
|
+
* @template T - The expected type of the parsed JSON response data
|
|
186
|
+
* @param url - The URL to fetch data from
|
|
187
|
+
* @param options - Optional fetch configuration options (headers, method, body, etc.)
|
|
188
|
+
* @param cacheConfig - Optional cache configuration object
|
|
189
|
+
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
190
|
+
* @param cacheConfig.tags - Array of tags to associate with the cached entry for invalidation purposes
|
|
191
|
+
* @param cacheConfig.key - Custom cache key to use instead of the default
|
|
192
|
+
* @returns A Promise that resolves to a CachedResponse containing the parsed JSON data of type T
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```typescript
|
|
196
|
+
* const result = await cFetchJson<User>('https://api.example.com/user/123', {
|
|
197
|
+
* method: 'GET',
|
|
198
|
+
* headers: { 'Authorization': 'Bearer token' }
|
|
199
|
+
* }, {
|
|
200
|
+
* ttl: Duration.minutes(5),
|
|
201
|
+
* tags: ['user-data']
|
|
202
|
+
* });
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
export declare const cFetchJson: <T>(url: string | URL, options?: RequestInit, cacheConfig?: CFetchConfig) => Promise<CachedResponse<T>>;
|
|
206
|
+
/**
|
|
207
|
+
* Fetches a URL and returns the response as text with caching support.
|
|
208
|
+
*
|
|
209
|
+
* @param url - The URL to fetch
|
|
210
|
+
* @param options - Optional fetch configuration options (headers, method, etc.)
|
|
211
|
+
* @param cacheConfig - Optional cache configuration
|
|
212
|
+
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
213
|
+
* @param cacheConfig.tags - Tags to associate with the cached response for invalidation
|
|
214
|
+
* @param cacheConfig.key - Custom cache key (defaults to URL if not provided)
|
|
215
|
+
* @returns A promise that resolves to a CachedResponse containing the response text
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```typescript
|
|
219
|
+
* const result = await cFetchText('https://api.example.com/data', {
|
|
220
|
+
* method: 'GET'
|
|
221
|
+
* }, {
|
|
222
|
+
* ttl: Duration.hours(1),
|
|
223
|
+
* tags: ['api', 'data']
|
|
224
|
+
* });
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
export declare const cFetchText: (url: string | URL, options?: RequestInit, cacheConfig?: CFetchConfig) => Promise<CachedResponse<string>>;
|
|
228
|
+
/**
|
|
229
|
+
* Fetches a Blob resource from the specified URL with optional caching configuration.
|
|
230
|
+
*
|
|
231
|
+
* @param url - The URL to fetch the Blob resource from
|
|
232
|
+
* @param options - Optional fetch request configuration (headers, method, etc.)
|
|
233
|
+
* @param cacheConfig - Optional cache configuration object
|
|
234
|
+
* @param cacheConfig.ttl - Time-to-live duration for the cached response
|
|
235
|
+
* @param cacheConfig.tags - Array of cache tags for cache invalidation
|
|
236
|
+
* @param cacheConfig.key - Custom cache key for storing the response
|
|
237
|
+
* @returns A Promise that resolves to a CachedResponse containing a Blob
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```typescript
|
|
241
|
+
* const response = await cFetchBlob('https://example.com/image.png', {}, { ttl: Duration.minutes(5) });
|
|
242
|
+
* const blob = response.data;
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
export declare const cFetchBlob: (url: string | URL, options?: RequestInit, cacheConfig?: CFetchConfig) => Promise<CachedResponse<Blob>>;
|