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: h3 adapter #358

Merged
merged 31 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
8800f07
feat: h3 adapter
juliusmarminge Sep 16, 2023
415dff0
example
juliusmarminge Sep 16, 2023
9486619
json.body
juliusmarminge Sep 16, 2023
154bccc
format
juliusmarminge Sep 16, 2023
598f395
fix type to event
juliusmarminge Sep 16, 2023
da6a7a9
Apply suggestions from code review
juliusmarminge Sep 16, 2023
4bc0812
lint
juliusmarminge Sep 16, 2023
2ce840f
Merge branch 'main' into h3-adapter
juliusmarminge Sep 16, 2023
a18797a
labeler
juliusmarminge Sep 16, 2023
edaeb0b
skiplibcheck in right place (temp)
juliusmarminge Sep 16, 2023
79efa48
Merge branch 'main' into h3-adapter
juliusmarminge Sep 17, 2023
cceb6ce
fix test
juliusmarminge Sep 17, 2023
154d387
prettier-ignore
juliusmarminge Sep 17, 2023
cc1fbd0
event duhh
juliusmarminge Sep 17, 2023
cd4c998
route -> event
juliusmarminge Sep 17, 2023
3e7025d
doc
juliusmarminge Sep 17, 2023
ef9d4a3
Merge branch 'main' into h3-adapter
juliusmarminge Sep 18, 2023
3cc954a
Merge branch 'main' into h3-adapter
juliusmarminge Sep 28, 2023
165c25e
fixy
juliusmarminge Sep 28, 2023
84d43a7
rm nitro for h3
juliusmarminge Sep 29, 2023
3af7aa7
rev
juliusmarminge Sep 29, 2023
62d81ed
more rev
juliusmarminge Sep 29, 2023
e5af263
Merge branch 'main' into h3-adapter
juliusmarminge Sep 29, 2023
28b441d
fix: rewrite to avoid use of router in `createH3EventHandler` (#399)
danielroe Sep 29, 2023
e45ea22
readme
juliusmarminge Sep 29, 2023
d481c66
Apply suggestions from code review
juliusmarminge Sep 29, 2023
8343a77
simplify
juliusmarminge Sep 29, 2023
4d3adae
simplify
juliusmarminge Sep 29, 2023
b1e6d93
fmt
juliusmarminge Sep 29, 2023
38f8a15
🤦‍♂️
juliusmarminge Sep 29, 2023
63d83a6
Apply suggestions from code review
juliusmarminge Sep 29, 2023
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
1 change: 1 addition & 0 deletions packages/uploadthing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"eslint": "^8.47.0",
"express": "^4.18.2",
"fastify": "^4.22.0",
"h3": "^1.8.1",
"next": "13.4.4",
"tailwindcss": "^3.3.2",
"tsup": "6.7.0",
Expand Down
67 changes: 67 additions & 0 deletions packages/uploadthing/src/h3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
createRouter,
defineEventHandler,
getRequestHeaders,
getRequestURL,
readBody,
setHeaders,
setResponseStatus,
} from "h3";

import { getStatusCodeFromError, UploadThingError } from "@uploadthing/shared";

import { UPLOADTHING_VERSION } from "./constants";
import { defaultErrorFormatter } from "./internal/error-formatter";
import {
buildPermissionsInfoHandler,
buildRequestHandler,
} from "./internal/handler";
import type { RouterWithConfig } from "./internal/handler";
import type { FileRouter } from "./internal/types";

export const createH3RouteHandler = <TRouter extends FileRouter>(
opts: RouterWithConfig<TRouter>,
) => {
const requestHandler = buildRequestHandler(opts);
const POST = defineEventHandler(async (event) => {
const response = await requestHandler({
req: {
url: getRequestURL(event).href,
headers: getRequestHeaders(event),
json: () => Promise.resolve(readBody(event)),
},
// res: event.node.res, // <-- DO WE NEED THIS?
juliusmarminge marked this conversation as resolved.
Show resolved Hide resolved
});

setHeaders(event, { "x-uploadthing-version": UPLOADTHING_VERSION });

if (response instanceof UploadThingError) {
setResponseStatus(event, getStatusCodeFromError(response));
const errorFormatter =
opts.router[Object.keys(opts.router)[0]]?._def.errorFormatter ??
defaultErrorFormatter;
return JSON.stringify(errorFormatter(response));
juliusmarminge marked this conversation as resolved.
Show resolved Hide resolved
}

if (response.status !== 200) {
// We messed up - this should never happen
setResponseStatus(event, 500);
return "An unknown error occurred";
}

setResponseStatus(event, 200);
juliusmarminge marked this conversation as resolved.
Show resolved Hide resolved
return JSON.stringify(response);
juliusmarminge marked this conversation as resolved.
Show resolved Hide resolved
});

const getBuildPerms = buildPermissionsInfoHandler<TRouter>(opts);
const GET = defineEventHandler((event) => {
setResponseStatus(event, 200);
setHeaders(event, { "x-uploadthing-version": UPLOADTHING_VERSION });
return JSON.stringify(getBuildPerms());
juliusmarminge marked this conversation as resolved.
Show resolved Hide resolved
});

const router = createRouter()
.post("/api/uploadthing", POST)
.get("/api/uploadthing", GET);
return router.handler;
juliusmarminge marked this conversation as resolved.
Show resolved Hide resolved
};
72 changes: 67 additions & 5 deletions pnpm-lock.yaml

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

Loading