Skip to content
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

feat: improve function return types #60

Merged
merged 7 commits into from
Jun 16, 2022
Merged
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
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 product.
*/
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