Skip to content

Commit f95ce3f

Browse files
committed
chore(types): start comprehensive typing
1 parent 3fdde97 commit f95ce3f

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/main.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,9 @@ describe('set', () => {
781781
siteID,
782782
})
783783

784+
// @ts-expect-error The `key` paramater is typed to not allow this
784785
expect(async () => await blobs.set('', 'value')).rejects.toThrowError('Blob key must not be empty.')
786+
// @ts-expect-error The `key` paramater is typed to not allow this
785787
expect(async () => await blobs.set('/key', 'value')).rejects.toThrowError(
786788
'Blob key must not start with forward slash (/).',
787789
)

src/store.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ interface NamedStoreOptions extends BaseStoreOptions {
1919
}
2020

2121
export type StoreOptions = DeployStoreOptions | NamedStoreOptions
22+
type Key<T> = T extends string ? (T extends '' | `/${string}` ? never : T) : never
2223

2324
export interface GetWithMetadataOptions {
2425
etag?: string
@@ -257,7 +258,7 @@ export class Store {
257258
)
258259
}
259260

260-
async set(key: string, data: BlobInput, { metadata }: SetOptions = {}) {
261+
async set<K>(key: Key<K>, data: BlobInput, { metadata }: SetOptions = {}) {
261262
Store.validateKey(key)
262263

263264
const res = await this.client.makeRequest({
@@ -273,7 +274,7 @@ export class Store {
273274
}
274275
}
275276

276-
async setJSON(key: string, data: unknown, { metadata }: SetOptions = {}) {
277+
async setJSON<K>(key: Key<K>, data: unknown, { metadata }: SetOptions = {}) {
277278
Store.validateKey(key)
278279

279280
const payload = JSON.stringify(data)
@@ -306,7 +307,7 @@ export class Store {
306307
}
307308
}
308309

309-
private static validateKey(key: string) {
310+
private static validateKey<K>(key: Key<K>) {
310311
if (key === '') {
311312
throw new Error('Blob key must not be empty.')
312313
}

0 commit comments

Comments
 (0)