Skip to content

Commit 91552c2

Browse files
committed
provide composable for uploading images to 8base
1 parent 6bf1e5f commit 91552c2

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

components.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import '@vue/runtime-core'
66
declare module '@vue/runtime-core' {
77
export interface GlobalComponents {
88
AppButton: typeof import('./src/components/AppButton.vue')['default']
9+
AppImageDropzone: typeof import('./src/components/AppImageDropzone.vue')['default']
10+
AppLoader: typeof import('./src/components/AppLoader.vue')['default']
911
AppPageHeading: typeof import('./src/components/AppPageHeading.vue')['default']
1012
BoardCard: typeof import('./src/components/BoardCard.vue')['default']
1113
BoardDragAndDrop: typeof import('./src/components/BoardDragAndDrop.vue')['default']
14+
BoardMenu: typeof import('./src/components/BoardMenu.vue')['default']
1215
HelloWorld: typeof import('./src/components/HelloWorld.vue')['default']
1316
IconCommunity: typeof import('./src/components/icons/IconCommunity.vue')['default']
1417
IconDocumentation: typeof import('./src/components/icons/IconDocumentation.vue')['default']
@@ -18,6 +21,7 @@ declare module '@vue/runtime-core' {
1821
RouterLink: typeof import('vue-router')['RouterLink']
1922
RouterView: typeof import('vue-router')['RouterView']
2023
TaskCard: typeof import('./src/components/TaskCard.vue')['default']
24+
TaskCreator: typeof import('./src/components/TaskCreator.vue')['default']
2125
TheAlerts: typeof import('./src/components/TheAlerts.vue')['default']
2226
TheDrawer: typeof import('./src/components/TheDrawer.vue')['default']
2327
TheWelcome: typeof import('./src/components/TheWelcome.vue')['default']

src/composables/use8baseStorage.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import gql from "graphql-tag";
2+
import { useMutation, useQuery } from "@vue/apollo-composable";
3+
4+
const IMAGE_UPLOAD_QUERY = gql`
5+
query {
6+
fileUploadInfo {
7+
policy
8+
signature
9+
apiKey
10+
path
11+
}
12+
}
13+
`;
14+
15+
const FILE_CREATE_MUTATION = gql`
16+
mutation CREATE_FILE($fileId: String!, $filename: String!) {
17+
fileCreate(data: { fileId: $fileId, filename: $filename }) {
18+
id
19+
}
20+
}
21+
`;
22+
23+
export function useStorage() {
24+
const { result } = useQuery(IMAGE_UPLOAD_QUERY);
25+
const { mutate: createFileIn8base } = useMutation(FILE_CREATE_MUTATION);
26+
27+
async function uploadAsset(file: File) {
28+
if (!result.value || !result.value.fileUploadInfo) {
29+
throw new Error("File Upload info not yet available");
30+
}
31+
const res = await fetch(
32+
`https://www.filestackapi.com/api/store/S3?key=${result.value.fileUploadInfo.apiKey}&policy=${result.value.fileUploadInfo.policy}&signature=${result.value.fileUploadInfo.signature}&path=${result.value.fileUploadInfo.path}`,
33+
{
34+
method: "POST",
35+
headers: {
36+
"Content-Type": file.type,
37+
},
38+
body: file,
39+
}
40+
);
41+
const data = await res.json();
42+
return createFileIn8base({
43+
fileId: data.url.split("/").at(-1),
44+
filename: data.filename,
45+
});
46+
}
47+
return {
48+
uploadAsset,
49+
};
50+
}
51+
export default useStorage;

0 commit comments

Comments
 (0)