Skip to content

Commit f1f2c25

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

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/main.test.ts

Lines changed: 2 additions & 0 deletions
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

Lines changed: 7 additions & 3 deletions
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,10 @@ export class Store {
257258
)
258259
}
259260

260-
async set(key: string, data: BlobInput, { metadata }: SetOptions = {}) {
261+
async set(key: `Key must not be empty and must not start with /`, data: BlobInput, opts?: SetOptions): Promise<void>
262+
async set<K>(key: Key<K>, data: BlobInput): Promise<void>
263+
async set<K>(key: Key<K>, data: BlobInput, { metadata }: SetOptions): Promise<void>
264+
async set<K>(key: Key<K>, data: BlobInput, { metadata }: SetOptions = {}) {
261265
Store.validateKey(key)
262266

263267
const res = await this.client.makeRequest({
@@ -273,7 +277,7 @@ export class Store {
273277
}
274278
}
275279

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

279283
const payload = JSON.stringify(data)
@@ -306,7 +310,7 @@ export class Store {
306310
}
307311
}
308312

309-
private static validateKey(key: string) {
313+
private static validateKey<K>(key: Key<K>) {
310314
if (key === '') {
311315
throw new Error('Blob key must not be empty.')
312316
}

0 commit comments

Comments
 (0)