forked from refinedev/refine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sdk storage support (refinedev#2522)
* feat(sdk): upload and getPublicUrl support * chore(cloud-example): add upload example * chore(sdk): changeset * chore(sdk): refactor applicationClientId data
- Loading branch information
1 parent
5d21008
commit e0dce4d
Showing
10 changed files
with
246 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@pankod/refine-sdk": minor | ||
--- | ||
|
||
Add storage support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { UploadFile } from "@pankod/refine-antd"; | ||
|
||
interface UploadResponse { | ||
url: string; | ||
} | ||
interface EventArgs<T = UploadResponse> { | ||
file: UploadFile<T>; | ||
fileList: Array<UploadFile<T>>; | ||
} | ||
|
||
export const normalizeFile = (event: EventArgs) => { | ||
const { fileList } = event; | ||
|
||
return fileList.map((item) => { | ||
const { uid, name, type, size, response, percent, status } = item; | ||
|
||
return { | ||
uid, | ||
name, | ||
url: item.url || response?.url, | ||
type, | ||
size, | ||
percent, | ||
status, | ||
}; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Client } from "../client"; | ||
|
||
class Storage { | ||
private client: Client; | ||
|
||
constructor(client: Client) { | ||
this.client = client; | ||
} | ||
|
||
async getPublicUrl(payload: { | ||
bucket: string; | ||
path: string; | ||
}): Promise<{ url: string }> { | ||
const { bucket, path } = payload; | ||
|
||
return await this.client.call({ | ||
method: "post", | ||
url: `/storage/generate-public-url/${bucket}`, | ||
data: { | ||
path, | ||
}, | ||
}); | ||
} | ||
|
||
async upload(payload: { | ||
bucket: string; | ||
file: | ||
| ArrayBuffer | ||
| ArrayBufferView | ||
| Blob | ||
| Buffer | ||
| File | ||
| FormData | ||
| NodeJS.ReadableStream | ||
| ReadableStream<Uint8Array> | ||
| URLSearchParams | ||
| string; | ||
}): Promise<any> { | ||
const { bucket, file } = payload; | ||
|
||
let body; | ||
if (typeof Blob !== "undefined" && file instanceof Blob) { | ||
body = new FormData(); | ||
body.append("file", file); | ||
} else if ( | ||
typeof FormData !== "undefined" && | ||
file instanceof FormData | ||
) { | ||
body = file; | ||
} else { | ||
body = file; | ||
} | ||
|
||
return await this.client.call({ | ||
method: "post", | ||
url: `/storage/upload/${bucket}`, | ||
data: body, | ||
headers: { | ||
"Content-Type": "multipart/form-data", | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
export { Storage }; |