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

refactor: typed the response types #177

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { StorageError } from './errors'

export interface Bucket {
id: string
name: string
Expand Down Expand Up @@ -109,3 +111,28 @@ export interface TransformOptions {
*/
format?: 'origin'
}

type StorageUploadResponseData =
| { path: string }
| { message: string }
| { signedUrl: string }
| {
signedUrl: string
token: string
path: string
}
| {
error: string | null
path: string | null
signedUrl: string
}[]

export type StorageUploadResponse =
| {
data: StorageUploadResponseData
error: null
}
| {
data: null
error: StorageError
}
39 changes: 4 additions & 35 deletions src/packages/StorageBucketApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DEFAULT_HEADERS } from '../lib/constants'
import { isStorageError, StorageError } from '../lib/errors'
import { Fetch, get, post, put, remove } from '../lib/fetch'
import { resolveFetch } from '../lib/helpers'
import { Bucket } from '../lib/types'
import { Bucket, StorageUploadResponse } from '../lib/types'

export default class StorageBucketApi {
protected url: string
Expand Down Expand Up @@ -143,16 +143,7 @@ export default class StorageBucketApi {
fileSizeLimit?: number | string | null
allowedMimeTypes?: string[] | null
}
): Promise<
| {
data: { message: string }
error: null
}
| {
data: null
error: StorageError
}
> {
): Promise<StorageUploadResponse> {
try {
const data = await put(
this.fetch,
Expand Down Expand Up @@ -181,18 +172,7 @@ export default class StorageBucketApi {
*
* @param id The unique identifier of the bucket you would like to empty.
*/
async emptyBucket(
id: string
): Promise<
| {
data: { message: string }
error: null
}
| {
data: null
error: StorageError
}
> {
async emptyBucket(id: string): Promise<StorageUploadResponse> {
try {
const data = await post(
this.fetch,
Expand All @@ -216,18 +196,7 @@ export default class StorageBucketApi {
*
* @param id The unique identifier of the bucket you would like to delete.
*/
async deleteBucket(
id: string
): Promise<
| {
data: { message: string }
error: null
}
| {
data: null
error: StorageError
}
> {
async deleteBucket(id: string): Promise<StorageUploadResponse> {
try {
const data = await remove(
this.fetch,
Expand Down
97 changes: 9 additions & 88 deletions src/packages/StorageFileApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
SearchOptions,
FetchParameters,
TransformOptions,
StorageUploadResponse,
} from '../lib/types'

const DEFAULT_SEARCH_OPTIONS = {
Expand Down Expand Up @@ -66,16 +67,7 @@ export default class StorageFileApi {
path: string,
fileBody: FileBody,
fileOptions?: FileOptions
): Promise<
| {
data: { id: string, path: string, fullPath: string }
error: null
}
| {
data: null
error: StorageError
}
> {
): Promise<StorageUploadResponse> {
try {
let body
const options = { ...DEFAULT_FILE_OPTIONS, ...fileOptions }
Expand Down Expand Up @@ -136,16 +128,7 @@ export default class StorageFileApi {
path: string,
fileBody: FileBody,
fileOptions?: FileOptions
): Promise<
| {
data: { path: string }
error: null
}
| {
data: null
error: StorageError
}
> {
): Promise<StorageUploadResponse> {
return this.uploadOrUpdate('POST', path, fileBody, fileOptions)
}

Expand Down Expand Up @@ -220,18 +203,7 @@ export default class StorageFileApi {
* They are valid for 2 hours.
* @param path The file path, including the current file name. For example `folder/image.png`.
*/
async createSignedUploadUrl(
path: string
): Promise<
| {
data: { signedUrl: string; token: string; path: string }
error: null
}
| {
data: null
error: StorageError
}
> {
async createSignedUploadUrl(path: string): Promise<StorageUploadResponse> {
try {
let _path = this._getFinalPath(path)

Expand Down Expand Up @@ -280,16 +252,7 @@ export default class StorageFileApi {
| URLSearchParams
| string,
fileOptions?: FileOptions
): Promise<
| {
data: { path: string }
error: null
}
| {
data: null
error: StorageError
}
> {
): Promise<StorageUploadResponse> {
return this.uploadOrUpdate('PUT', path, fileBody, fileOptions)
}

Expand All @@ -299,19 +262,7 @@ export default class StorageFileApi {
* @param fromPath The original file path, including the current file name. For example `folder/image.png`.
* @param toPath The new file path, including the new file name. For example `folder/image-new.png`.
*/
async move(
fromPath: string,
toPath: string
): Promise<
| {
data: { message: string }
error: null
}
| {
data: null
error: StorageError
}
> {
async move(fromPath: string, toPath: string): Promise<StorageUploadResponse> {
try {
const data = await post(
this.fetch,
Expand All @@ -335,19 +286,7 @@ export default class StorageFileApi {
* @param fromPath The original file path, including the current file name. For example `folder/image.png`.
* @param toPath The new file path, including the new file name. For example `folder/image-copy.png`.
*/
async copy(
fromPath: string,
toPath: string
): Promise<
| {
data: { path: string }
error: null
}
| {
data: null
error: StorageError
}
> {
async copy(fromPath: string, toPath: string): Promise<StorageUploadResponse> {
try {
const data = await post(
this.fetch,
Expand Down Expand Up @@ -377,16 +316,7 @@ export default class StorageFileApi {
path: string,
expiresIn: number,
options?: { download?: string | boolean; transform?: TransformOptions }
): Promise<
| {
data: { signedUrl: string }
error: null
}
| {
data: null
error: StorageError
}
> {
): Promise<StorageUploadResponse> {
try {
let _path = this._getFinalPath(path)

Expand Down Expand Up @@ -422,16 +352,7 @@ export default class StorageFileApi {
paths: string[],
expiresIn: number,
options?: { download: string | boolean }
): Promise<
| {
data: { error: string | null; path: string | null; signedUrl: string }[]
error: null
}
| {
data: null
error: StorageError
}
> {
): Promise<StorageUploadResponse> {
try {
const data = await post(
this.fetch,
Expand Down