Skip to content

Commit 02f2c7a

Browse files
authored
refactor: code cleanup (#495)
1 parent 9b0db4d commit 02f2c7a

File tree

1 file changed

+36
-58
lines changed

1 file changed

+36
-58
lines changed

packages/cloudflare/src/api/config.ts

+36-58
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,47 @@
11
import { BaseOverride, LazyLoadedOverride, OpenNextConfig } from "@opennextjs/aws/types/open-next";
22
import type { IncrementalCache, Queue, TagCache } from "@opennextjs/aws/types/overrides";
33

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 = {
512
/**
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.
1814
*/
19-
incrementalCache?: "dummy" | IncrementalCache | (() => IncrementalCache | Promise<IncrementalCache>);
15+
incrementalCache?: Override<IncrementalCache>;
2016

2117
/**
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.
3419
*/
35-
tagCache?: "dummy" | TagCache | (() => TagCache | Promise<TagCache>);
20+
tagCache?: Override<TagCache>;
3621

3722
/**
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
5024
*/
51-
queue?: "dummy" | "direct" | Queue | (() => Queue | Promise<Queue>);
25+
queue?: "direct" | Override<Queue>;
5226
};
5327

5428
/**
5529
* Defines the OpenNext configuration that targets the Cloudflare adapter
5630
*
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
5832
* @returns the OpenNext configuration object
5933
*/
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;
6236

6337
return {
6438
default: {
6539
override: {
6640
wrapper: "cloudflare-node",
6741
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),
7145
},
7246
},
7347

@@ -82,22 +56,26 @@ export function defineCloudflareConfig(options: CloudflareConfigOptions = {}): O
8256
};
8357
}
8458

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+
}
8663

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+
}
9066

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;
9670
}
9771

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;
10078
}
10179

102-
return (typeof value === "function" ? value : () => value) as ResolveOverrideReturn<T>;
80+
return typeof value === "function" ? value : () => value;
10381
}

0 commit comments

Comments
 (0)