Skip to content

Commit

Permalink
feat: improve function return types (#60)
Browse files Browse the repository at this point in the history
* feat: improve types

* chore: better error handling

* feat: custom storage api error

* chore: use custom error type

* chore: update custom error type

* chore: replace instanceof with isStorageError

* chore: move isStorageError to a non-static method
  • Loading branch information
alaister committed Jun 16, 2022
1 parent 1a7d98c commit 767e1df
Show file tree
Hide file tree
Showing 9 changed files with 445 additions and 90 deletions.
98 changes: 76 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"docs:json": "typedoc --json docs/spec.json --mode modules --includeDeclarations --excludeExternals"
},
"dependencies": {
"cross-fetch": "^3.1.0"
"cross-fetch": "^3.1.5"
},
"devDependencies": {
"@types/jest": "^26.0.13",
Expand All @@ -51,7 +51,7 @@
"ts-jest": "^26.3.0",
"ts-loader": "^8.0.11",
"typedoc": "^0.19.1",
"typescript": "^4.0.2",
"typescript": "^4.6.3",
"webpack": "^5.4.0",
"webpack-cli": "^4.2.0"
},
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export {
StorageClient as SupabaseStorageClient,
} from './StorageClient'
export * from './lib/types'
export * from './lib/errors'
105 changes: 93 additions & 12 deletions src/lib/StorageBucketApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DEFAULT_HEADERS } from './constants'
import { isStorageError, StorageError } from './errors'
import { Fetch, get, post, put, remove } from './fetch'
import { resolveFetch } from './helpers'
import { Bucket } from './types'
Expand All @@ -17,12 +18,25 @@ export class StorageBucketApi {
/**
* Retrieves the details of all Storage buckets within an existing project.
*/
async listBuckets(): Promise<{ data: Bucket[] | null; error: Error | null }> {
async listBuckets(): Promise<
| {
data: Bucket[]
error: null
}
| {
data: null
error: StorageError
}
> {
try {
const data = await get(this.fetch, `${this.url}/bucket`, { headers: this.headers })
return { data, error: null }
} catch (error) {
return { data: null, error }
if (isStorageError(error)) {
return { data: null, error }
}

throw error
}
}

Expand All @@ -31,12 +45,27 @@ export class StorageBucketApi {
*
* @param id The unique identifier of the bucket you would like to retrieve.
*/
async getBucket(id: string): Promise<{ data: Bucket | null; error: Error | null }> {
async getBucket(
id: string
): Promise<
| {
data: Bucket
error: null
}
| {
data: null
error: StorageError
}
> {
try {
const data = await get(this.fetch, `${this.url}/bucket/${id}`, { headers: this.headers })
return { data, error: null }
} catch (error) {
return { data: null, error }
if (isStorageError(error)) {
return { data: null, error }
}

throw error
}
}

Expand All @@ -49,7 +78,16 @@ export class StorageBucketApi {
async createBucket(
id: string,
options: { public: boolean } = { public: false }
): Promise<{ data: string | null; error: Error | null }> {
): Promise<
| {
data: string
error: null
}
| {
data: null
error: StorageError
}
> {
try {
const data = await post(
this.fetch,
Expand All @@ -59,7 +97,11 @@ export class StorageBucketApi {
)
return { data: data.name, error: null }
} catch (error) {
return { data: null, error }
if (isStorageError(error)) {
return { data: null, error }
}

throw error
}
}

Expand All @@ -71,7 +113,16 @@ export class StorageBucketApi {
async updateBucket(
id: string,
options: { public: boolean }
): Promise<{ data: { message: string } | null; error: Error | null }> {
): Promise<
| {
data: { message: string }
error: null
}
| {
data: null
error: StorageError
}
> {
try {
const data = await put(
this.fetch,
Expand All @@ -81,7 +132,11 @@ export class StorageBucketApi {
)
return { data, error: null }
} catch (error) {
return { data: null, error }
if (isStorageError(error)) {
return { data: null, error }
}

throw error
}
}

Expand All @@ -92,7 +147,16 @@ export class StorageBucketApi {
*/
async emptyBucket(
id: string
): Promise<{ data: { message: string } | null; error: Error | null }> {
): Promise<
| {
data: { message: string }
error: null
}
| {
data: null
error: StorageError
}
> {
try {
const data = await post(
this.fetch,
Expand All @@ -102,7 +166,11 @@ export class StorageBucketApi {
)
return { data, error: null }
} catch (error) {
return { data: null, error }
if (isStorageError(error)) {
return { data: null, error }
}

throw error
}
}

Expand All @@ -114,7 +182,16 @@ export class StorageBucketApi {
*/
async deleteBucket(
id: string
): Promise<{ data: { message: string } | null; error: Error | null }> {
): Promise<
| {
data: { message: string }
error: null
}
| {
data: null
error: StorageError
}
> {
try {
const data = await remove(
this.fetch,
Expand All @@ -124,7 +201,11 @@ export class StorageBucketApi {
)
return { data, error: null }
} catch (error) {
return { data: null, error }
if (isStorageError(error)) {
return { data: null, error }
}

throw error
}
}
}
Loading

0 comments on commit 767e1df

Please sign in to comment.