Skip to content

Commit

Permalink
fix(files): use fetch instead of invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexcited committed Aug 12, 2023
1 parent b6a3508 commit 4e56ec3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
7 changes: 6 additions & 1 deletion src/supabase/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ export const getProfileWithSession = async (session: Session) => {

let user_profile = _profile as UserProfile;
return user_profile;
}
}

export const getFunctionUrl = (function_name: string) => {
const url = new URL(`/functions/v1/${function_name}`, import.meta.env.VITE_SUPABASE_PROJECT_URL as string)
return url.href;
};
26 changes: 11 additions & 15 deletions src/utils/files.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { auth } from "@/stores/auth";
import { supabase } from "@/supabase/client";
import { getFunctionUrl, supabase } from "@/supabase/client";
import { UploadedFile } from "@/types/api";

export const createFileImporter = (onFileUploaded: (files: FileList) => unknown) => {
Expand Down Expand Up @@ -54,30 +54,26 @@ export const makeFileUpload = async (files: FileList | Array<File>, options?: {
headers.authorization = auth.profile.api_token;
}

for (const file of files) {
await supabase.storage
.from("test-up")
.upload(`${auth.session?.user.id ?? "anon"}/${file.name}`, file);
}
const response = await fetch(getFunctionUrl("upload-file"), {
method: "PUT",
headers,
body
});

const { data: response } = await supabase.functions.invoke<(
const json = await response.json() as (
| { success: false, message: string }
| { success: true, data: {
uploaded: UploadedFile[]
}}
)>("upload-file", {
method: "PUT",
headers,
body,
});
);

// Aww... something went wrong :'(
if (!response || !response.success) {
throw new Error(response ? response.message : "Failed to request.");
if (!json.success) {
throw new Error(json.message);
}

// // All's good, we send back the new rows in `uploads` table.
return response.data.uploaded;
return json.data.uploaded;
};

export const getUploadedFileURL = (file: UploadedFile) => {
Expand Down

0 comments on commit 4e56ec3

Please sign in to comment.