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: add uploadthing driver #390

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"types-cloudflare-worker": "^1.2.0",
"typescript": "^5.3.3",
"unbuild": "^2.0.0",
"uploadthing": "^6.3.0",
"vite": "^5.0.11",
"vitest": "^1.2.1",
"vue": "^3.4.14"
Expand All @@ -108,7 +109,8 @@
"@planetscale/database": "^1.13.0",
"@upstash/redis": "^1.28.1",
"@vercel/kv": "^0.2.4",
"idb-keyval": "^6.2.1"
"idb-keyval": "^6.2.1",
"uploadthing": "^6.0.0"
Copy link
Contributor Author

@juliusmarminge juliusmarminge Jan 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"uploadthing": "^6.0.0"
"uploadthing": "^6.4.0"

},
"peerDependenciesMeta": {
"@azure/app-configuration": {
Expand Down Expand Up @@ -146,6 +148,9 @@
},
"idb-keyval": {
"optional": true
},
"uploadthing": {
"optional": true
}
},
"packageManager": "[email protected]"
Expand Down
29 changes: 29 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions src/drivers/uploadthing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { defineDriver } from "./utils";
import { ofetch, $Fetch } from "ofetch";
import { UTApi } from "uploadthing/server";

export interface UploadThingOptions {
apiKey: string;
}

export default defineDriver<UploadThingOptions>((opts) => {
let client: UTApi;
let utFetch: $Fetch;

const getClient = () => {
return (client ??= new UTApi({
apiKey: opts.apiKey,
fetch: ofetch.native,
}));
};

const getUTFetch = () => {
return (utFetch ??= ofetch.create({
baseURL: "https://uploadthing.com/api",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it makes sense to support an optional option for baseURL? (mainly to allow unit testing)

headers: {
"x-uploadthing-api-key": opts.apiKey,
},
}));
};

return {
hasItem(key) {
// This is the best endpoint we got currently...
return getUTFetch()("/getFileUrl", {
body: { fileKeys: [key] },
}).then((res) => res.ok);
},
getItem(key) {
return ofetch(`https://utfs.io/f/${key}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, somehow make it configurable too? (not sure about /api and this API difference but just a taught to again make testing easier)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utfs is our cloudflare rewriter that handles caching and mapping keys to different regions.

uploadthing.com/api is where everything else goes.

can make it configurable for testing though!

},
getItemRaw(key) {
return ofetch
.native(`https://utfs.io/f/${key}`)
.then((res) => res.arrayBuffer());
},
getItems(items) {
return Promise.all(
items.map((item) =>
ofetch(`https://utfs.io/f/${item.key}`).then((res) => ({
pi0 marked this conversation as resolved.
Show resolved Hide resolved
key: item.key,
value: res,
}))
)
);
},
getKeys() {
return getClient()
.listFiles({})
pi0 marked this conversation as resolved.
Show resolved Hide resolved
.then((res) => res.map((file) => file.key));
},
setItem(key, value, opts) {
return getClient()
.uploadFiles(new Blob([value]), {
metadata: opts.metadata,
})
.then(() => {});
},
setItems(items, opts) {
return getClient()
.uploadFiles(
items.map((item) => new Blob([item.value])),
{
metadata: opts?.metadata,
}
)
.then(() => {});
},
removeItem(key, opts) {
return getClient()
.deleteFiles([key])
.then(() => {});
},
async clear() {
const client = getClient();
const keys = await client.listFiles({}).then((r) => r.map((f) => f.key));
return client.deleteFiles(keys).then(() => {});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clear(base) is suported too. . I guess we might need to support it (client-side or server side) otherwise one call drops entire collection of uploads πŸ—‘οΈ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea πŸ˜…

not sure how though as currently we just have delete by key and all keys are random (no directories (yet))

},
// getMeta(key, opts) {
// // TODO: We don't currently have an endpoint to fetch metadata, but it does exist
// },
};
});