1
1
import { BaseOverride , LazyLoadedOverride , OpenNextConfig } from "@opennextjs/aws/types/open-next" ;
2
2
import type { IncrementalCache , Queue , TagCache } from "@opennextjs/aws/types/overrides" ;
3
3
4
- export type CloudflareConfigOptions = {
4
+ export type Override < T extends BaseOverride > = "dummy" | T | LazyLoadedOverride < T > ;
5
+
6
+ /**
7
+ * Cloudflare specific overrides.
8
+ *
9
+ * See the [Caching documentation](https://opennext.js.org/cloudflare/caching))
10
+ */
11
+ export type CloudflareOverrides = {
5
12
/**
6
- * The incremental cache implementation to use, for more details see the [Caching documentation](https://opennext.js.org/cloudflare/caching))
7
- *
8
- * `@opennextjs/cloudflare` offers a kv incremental cache implementation ready
9
- * to use which can be imported from `"@opennextjs/cloudflare/kv-cache"`
10
- *
11
- * @example
12
- * import { defineCloudflareConfig } from "@opennextjs/cloudflare";
13
- * import kvIncrementalCache from "@opennextjs/cloudflare/kv-cache";
14
- *
15
- * export default defineCloudflareConfig({
16
- * incrementalCache: kvIncrementalCache,
17
- * });
13
+ * Sets the incremental cache implementation.
18
14
*/
19
- incrementalCache ?: "dummy" | IncrementalCache | ( ( ) => IncrementalCache | Promise < IncrementalCache > ) ;
15
+ incrementalCache ?: Override < IncrementalCache > ;
20
16
21
17
/**
22
- * The tag cache implementation to use, for more details see the [Caching documentation](https://opennext.js.org/cloudflare/caching))
23
- *
24
- * `@opennextjs/cloudflare` offers a d1 tag cache implementation ready
25
- * to use which can be imported from `"@opennextjs/cloudflare/d1-tag-cache"`
26
- *
27
- * @example
28
- * import { defineCloudflareConfig } from "@opennextjs/cloudflare";
29
- * import d1TagCache from "@opennextjs/cloudflare/d1-tag-cache";
30
- *
31
- * export default defineCloudflareConfig({
32
- * tagCache: d1TagCache,
33
- * });
18
+ * Sets the tag cache implementation.
34
19
*/
35
- tagCache ?: "dummy" | TagCache | ( ( ) => TagCache | Promise < TagCache > ) ;
20
+ tagCache ?: Override < TagCache > ;
36
21
37
22
/**
38
- * The revalidation queue implementation to use, for more details see the [Caching documentation](https://opennext.js.org/cloudflare/caching))
39
- *
40
- * `@opennextjs/cloudflare` offers an in memory queue implementation ready
41
- * to use which can be imported from `"@opennextjs/cloudflare/memory-queue"`
42
- *
43
- * @example
44
- * import { defineCloudflareConfig } from "@opennextjs/cloudflare";
45
- * import memoryQueue from "@opennextjs/cloudflare/memory-queue";
46
- *
47
- * export default defineCloudflareConfig({
48
- * queue: memoryQueue,
49
- * });
23
+ * Sets the revalidation queue implementation
50
24
*/
51
- queue ?: "dummy" | " direct" | Queue | ( ( ) => Queue | Promise < Queue > ) ;
25
+ queue ?: "direct" | Override < Queue > ;
52
26
} ;
53
27
54
28
/**
55
29
* Defines the OpenNext configuration that targets the Cloudflare adapter
56
30
*
57
- * @param options options that enabled you to configure the application's behavior
31
+ * @param config options that enabled you to configure the application's behavior
58
32
* @returns the OpenNext configuration object
59
33
*/
60
- export function defineCloudflareConfig ( options : CloudflareConfigOptions = { } ) : OpenNextConfig {
61
- const { incrementalCache, tagCache, queue } = options ;
34
+ export function defineCloudflareConfig ( config : CloudflareOverrides = { } ) : OpenNextConfig {
35
+ const { incrementalCache, tagCache, queue } = config ;
62
36
63
37
return {
64
38
default : {
65
39
override : {
66
40
wrapper : "cloudflare-node" ,
67
41
converter : "edge" ,
68
- incrementalCache : resolveOverride ( incrementalCache ) ,
69
- tagCache : resolveOverride ( tagCache ) ,
70
- queue : resolveOverride ( queue ) ,
42
+ incrementalCache : resolveIncrementalCache ( incrementalCache ) ,
43
+ tagCache : resolveTagCache ( tagCache ) ,
44
+ queue : resolveQueue ( queue ) ,
71
45
} ,
72
46
} ,
73
47
@@ -82,22 +56,26 @@ export function defineCloudflareConfig(options: CloudflareConfigOptions = {}): O
82
56
} ;
83
57
}
84
58
85
- type DummyOrLazyLoadedOverride < T extends BaseOverride > = "dummy" | LazyLoadedOverride < T > ;
59
+ function resolveIncrementalCache ( value : CloudflareOverrides [ "incrementalCache" ] = "dummy" ) {
60
+ if ( typeof value === "string" ) {
61
+ return value ;
62
+ }
86
63
87
- type ResolveOverrideReturn < T extends IncrementalCache | TagCache | Queue > = T extends Queue
88
- ? "direct" | DummyOrLazyLoadedOverride < T >
89
- : DummyOrLazyLoadedOverride < T > ;
64
+ return typeof value === "function" ? value : ( ) => value ;
65
+ }
90
66
91
- function resolveOverride < T extends IncrementalCache | TagCache | Queue > (
92
- value : undefined | "dummy" | "direct" | T | ( ( ) => T | Promise < T > )
93
- ) : ResolveOverrideReturn < T > {
94
- if ( ! value || value === "dummy" ) {
95
- return "dummy" as ResolveOverrideReturn < T > ;
67
+ function resolveTagCache ( value : CloudflareOverrides [ "tagCache" ] = "dummy" ) {
68
+ if ( typeof value === "string" ) {
69
+ return value ;
96
70
}
97
71
98
- if ( value === "direct" ) {
99
- return "direct" as ResolveOverrideReturn < T > ;
72
+ return typeof value === "function" ? value : ( ) => value ;
73
+ }
74
+
75
+ function resolveQueue ( value : CloudflareOverrides [ "queue" ] = "dummy" ) {
76
+ if ( typeof value === "string" ) {
77
+ return value ;
100
78
}
101
79
102
- return ( typeof value === "function" ? value : ( ) => value ) as ResolveOverrideReturn < T > ;
80
+ return typeof value === "function" ? value : ( ) => value ;
103
81
}
0 commit comments