Skip to content

chore(types): start comprehensive typing #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (/).',
)
Expand Down
7 changes: 4 additions & 3 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface NamedStoreOptions extends BaseStoreOptions {
}

export type StoreOptions = DeployStoreOptions | NamedStoreOptions
type Key<T> = T extends string ? (T extends '' | `/${string}` ? never : T) : never
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we want to go the route of stronger types, I'd love some input on naming conventions - this is more or less a POC/idea PR for consideration


export interface GetWithMetadataOptions {
etag?: string
Expand Down Expand Up @@ -257,7 +258,7 @@ export class Store {
)
}

async set(key: string, data: BlobInput, { metadata }: SetOptions = {}) {
async set<K>(key: Key<K>, data: BlobInput, { metadata }: SetOptions = {}) {
Store.validateKey(key)

const res = await this.client.makeRequest({
Expand All @@ -273,7 +274,7 @@ export class Store {
}
}

async setJSON(key: string, data: unknown, { metadata }: SetOptions = {}) {
async setJSON<K>(key: Key<K>, data: unknown, { metadata }: SetOptions = {}) {
Store.validateKey(key)

const payload = JSON.stringify(data)
Expand Down Expand Up @@ -306,7 +307,7 @@ export class Store {
}
}

private static validateKey(key: string) {
private static validateKey<K>(key: Key<K>) {
if (key === '') {
throw new Error('Blob key must not be empty.')
}
Expand Down