-
Notifications
You must be signed in to change notification settings - Fork 121
/
netlify-blobs.ts
114 lines (105 loc) 路 3.53 KB
/
netlify-blobs.ts
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
import { createError, createRequiredError, defineDriver } from "./utils";
import { getStore, getDeployStore } from "@netlify/blobs";
import type {
Store,
BlobResponseType,
SetOptions,
ListOptions,
} from "@netlify/blobs";
import { fetch } from "ofetch";
const DRIVER_NAME = "netlify-blobs";
type GetOptions = { type?: BlobResponseType };
export interface NetlifyBaseStoreOptions {
/** The name of the store to use. It is created if needed. This is required except for deploy-scoped stores. */
name?: string;
/** If set to `true`, the store is scoped to the deploy. This means that it is only available from that deploy, and will be deleted or rolled-back alongside it. */
deployScoped?: boolean;
/** Required during builds, where it is available as `constants.SITE_ID`. At runtime this is set automatically. */
siteID?: string;
/** Required during builds, where it is available as `constants.NETLIFY_API_TOKEN`. At runtime this is set automatically. */
token?: string;
/** Used for advanced use cases and unit tests */
apiURL?: string;
/** Used for advanced use cases and unit tests */
edgeURL?: string;
}
export interface NetlifyDeployStoreOptions extends NetlifyBaseStoreOptions {
name?: never;
deployScoped: true;
deployID?: string;
}
export interface NetlifyNamedStoreOptions extends NetlifyBaseStoreOptions {
name: string;
deployScoped?: false;
}
export type NetlifyStoreOptions =
| NetlifyDeployStoreOptions
| NetlifyNamedStoreOptions;
export default defineDriver(
({ deployScoped, name, ...opts }: NetlifyStoreOptions) => {
let store: Store;
const getClient = () => {
if (!store) {
if (deployScoped) {
if (name) {
throw createError(
DRIVER_NAME,
"deploy-scoped stores cannot have a name"
);
}
store = getDeployStore({ fetch, ...opts });
} else {
if (!name) {
throw createRequiredError(DRIVER_NAME, "name");
}
// Ensures that reserved characters are encoded
store = getStore({ name: encodeURIComponent(name), fetch, ...opts });
}
}
return store;
};
return {
name: DRIVER_NAME,
options: {},
async hasItem(key) {
return getClient().getMetadata(key).then(Boolean);
},
getItem: (key, tops?: GetOptions) => {
// @ts-expect-error has trouble with the overloaded types
return getClient().get(key, tops);
},
getMeta(key) {
return getClient().getMetadata(key);
},
getItemRaw(key, topts?: GetOptions) {
// @ts-expect-error has trouble with the overloaded types
return getClient().get(key, { type: topts?.type ?? "arrayBuffer" });
},
setItem(key, value, topts?: SetOptions) {
return getClient().set(key, value, topts);
},
setItemRaw(key, value: string | ArrayBuffer | Blob, topts?: SetOptions) {
return getClient().set(key, value, topts);
},
removeItem(key) {
return getClient().delete(key);
},
async getKeys(
base?: string,
tops?: Omit<ListOptions, "prefix" | "paginate">
) {
return (await getClient().list({ ...tops, prefix: base })).blobs.map(
(item) => item.key
);
},
async clear(base?: string) {
const client = getClient();
return Promise.allSettled(
(await client.list({ prefix: base })).blobs.map((item) =>
client.delete(item.key)
)
).then(() => {});
},
};
}
);