diff --git a/src/main.test.ts b/src/main.test.ts index 140f5d5..f2da9a0 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -781,7 +781,9 @@ describe('set', () => { siteID, }) + // @ts-expect-error The `key` paramater is typed to not allow this expect(async () => await blobs.set('', 'value')).rejects.toThrowError('Blob key must not be empty.') + // @ts-expect-error The `key` paramater is typed to not allow this expect(async () => await blobs.set('/key', 'value')).rejects.toThrowError( 'Blob key must not start with forward slash (/).', ) diff --git a/src/store.ts b/src/store.ts index 1e0aa23..869f38c 100644 --- a/src/store.ts +++ b/src/store.ts @@ -19,6 +19,7 @@ interface NamedStoreOptions extends BaseStoreOptions { } export type StoreOptions = DeployStoreOptions | NamedStoreOptions +type Key = T extends string ? (T extends '' | `/${string}` ? never : T) : never export interface GetWithMetadataOptions { etag?: string @@ -257,7 +258,7 @@ export class Store { ) } - async set(key: string, data: BlobInput, { metadata }: SetOptions = {}) { + async set(key: Key, data: BlobInput, { metadata }: SetOptions = {}) { Store.validateKey(key) const res = await this.client.makeRequest({ @@ -273,7 +274,7 @@ export class Store { } } - async setJSON(key: string, data: unknown, { metadata }: SetOptions = {}) { + async setJSON(key: Key, data: unknown, { metadata }: SetOptions = {}) { Store.validateKey(key) const payload = JSON.stringify(data) @@ -306,7 +307,7 @@ export class Store { } } - private static validateKey(key: string) { + private static validateKey(key: Key) { if (key === '') { throw new Error('Blob key must not be empty.') }