-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathcache-types.cts
134 lines (114 loc) · 4.35 KB
/
cache-types.cts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import type {
CacheHandler,
CacheHandlerValue,
} from 'next/dist/server/lib/incremental-cache/index.js'
import type {
CachedFetchValue,
CachedRouteValue,
IncrementalCachedAppPageValue,
IncrementalCachedPageValue,
IncrementalCacheValue,
} from 'next/dist/server/response-cache/types.js'
export type { CacheHandlerContext } from 'next/dist/server/lib/incremental-cache/index.js'
/**
* Shape of the cache value that is returned from CacheHandler.get or passed to CacheHandler.set
*/
type CachedRouteValueForMultipleVersions = Omit<CachedRouteValue, 'kind'> & {
kind: 'ROUTE' | 'APP_ROUTE'
}
/**
* Used for storing in blobs and reading from blobs
*/
export type NetlifyCachedRouteValue = Omit<CachedRouteValueForMultipleVersions, 'body'> & {
// Next.js stores body as buffer, while we store it as base64 encoded string
body: string
// Next.js doesn't produce cache-control tag we use to generate cdn cache control
// so store needed values as part of cached response data
revalidate?: Parameters<CacheHandler['set']>[2]['revalidate']
}
/**
* Shape of the cache value that is returned from CacheHandler.get or passed to CacheHandler.set
*/
type IncrementalCachedAppPageValueForMultipleVersions = Omit<
IncrementalCachedAppPageValue,
'kind'
> & {
kind: 'APP_PAGE'
}
/**
* Used for storing in blobs and reading from blobs
*/
export type NetlifyCachedAppPageValue = Omit<
IncrementalCachedAppPageValueForMultipleVersions,
'rscData'
> & {
// Next.js stores rscData as buffer, while we store it as base64 encoded string
rscData: string | undefined
revalidate?: Parameters<CacheHandler['set']>[2]['revalidate']
}
/**
* Shape of the cache value that is returned from CacheHandler.get or passed to CacheHandler.set
*/
type IncrementalCachedPageValueForMultipleVersions = Omit<IncrementalCachedPageValue, 'kind'> & {
kind: 'PAGE' | 'PAGES'
}
/**
* Used for storing in blobs and reading from blobs
*/
export type NetlifyCachedPageValue = IncrementalCachedPageValueForMultipleVersions & {
revalidate?: Parameters<CacheHandler['set']>[2]['revalidate']
}
export type CachedFetchValueForMultipleVersions = Omit<CachedFetchValue, 'kind'> & {
kind: 'FETCH'
}
type CachedRouteValueToNetlify<T> = T extends CachedRouteValue
? NetlifyCachedRouteValue
: T extends IncrementalCachedPageValue
? NetlifyCachedPageValue
: T extends IncrementalCachedAppPageValue
? NetlifyCachedAppPageValue
: T
type MapCachedRouteValueToNetlify<T> = { [K in keyof T]: CachedRouteValueToNetlify<T[K]> } & {
lastModified: number
}
/**
* Used for storing in blobs and reading from blobs
*/
export type NetlifyCacheHandlerValue = MapCachedRouteValueToNetlify<CacheHandlerValue>
/**
* Used for storing in blobs and reading from blobs
*/
export type NetlifyIncrementalCacheValue = NetlifyCacheHandlerValue['value']
type IncrementalCacheValueToMultipleVersions<T> = T extends CachedRouteValue
? CachedRouteValueForMultipleVersions
: T extends IncrementalCachedPageValue
? IncrementalCachedPageValueForMultipleVersions
: T extends IncrementalCachedAppPageValue
? IncrementalCachedAppPageValueForMultipleVersions
: T extends CachedFetchValue
? CachedFetchValueForMultipleVersions
: T extends CacheHandlerValue
? {
[K in keyof T]: IncrementalCacheValueToMultipleVersions<T[K]>
}
: T
type IncrementalCacheValueForMultipleVersions =
IncrementalCacheValueToMultipleVersions<IncrementalCacheValue>
export const isCachedPageValue = (
value: IncrementalCacheValueForMultipleVersions,
): value is IncrementalCachedPageValueForMultipleVersions =>
value.kind === 'PAGE' || value.kind === 'PAGES'
export const isCachedRouteValue = (
value: IncrementalCacheValueForMultipleVersions,
): value is CachedRouteValueForMultipleVersions =>
value.kind === 'ROUTE' || value.kind === 'APP_ROUTE'
type MapArgsOrReturn<T> = T extends readonly unknown[]
? { [K in keyof T]: MapArgsOrReturn<T[K]> }
: T extends Promise<infer P>
? Promise<MapArgsOrReturn<P>>
: IncrementalCacheValueToMultipleVersions<T>
type MapCacheHandlerClassMethod<T> = T extends (...args: infer Args) => infer Ret
? (...args: MapArgsOrReturn<Args>) => MapArgsOrReturn<Ret>
: T
type MapCacheHandlerClass<T> = { [K in keyof T]: MapCacheHandlerClassMethod<T[K]> }
export type CacheHandlerForMultipleVersions = MapCacheHandlerClass<CacheHandler>