-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
@effect/platform
adapter (#861)
Co-authored-by: Davletov Almir <davletovalmir@gmail.com> Co-authored-by: Mark R. Florkowski <mark.florkowski@gmail.com>
- Loading branch information
1 parent
0862a6d
commit 78c755d
Showing
9 changed files
with
284 additions
and
83 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 @@ | ||
--- | ||
"uploadthing": minor | ||
--- | ||
|
||
feat: `@effect/platform` adapter |
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,61 @@ | ||
import "dotenv/config"; | ||
|
||
import { createServer } from "node:http"; | ||
import { NodeHttpServer, NodeRuntime } from "@effect/platform-node"; | ||
import * as Http from "@effect/platform/HttpServer"; | ||
import { Config, Effect, Layer } from "effect"; | ||
|
||
import { createRouteHandler } from "uploadthing/effect-platform"; | ||
|
||
import { uploadRouter } from "./router"; | ||
|
||
const uploadthingRouter = createRouteHandler({ | ||
router: uploadRouter, | ||
}); | ||
|
||
/** | ||
* Simple CORS middleware that allows everything | ||
* Adjust to your needs. | ||
*/ | ||
const cors = Http.middleware.make((app) => | ||
Effect.gen(function* () { | ||
const req = yield* Http.request.ServerRequest; | ||
|
||
const corsHeaders = { | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Allow-Methods": "*", | ||
"Access-Control-Allow-Headers": "*", | ||
}; | ||
|
||
if (req.method === "OPTIONS") { | ||
return Http.response.empty({ | ||
status: 204, | ||
headers: Http.headers.fromInput(corsHeaders), | ||
}); | ||
} | ||
|
||
const response = yield* app; | ||
|
||
return response.pipe(Http.response.setHeaders(corsHeaders)); | ||
}), | ||
); | ||
|
||
const router = Http.router.empty.pipe( | ||
Http.router.get("/api", Http.response.text("Hello from Effect")), | ||
Http.router.mount("/api/uploadthing", uploadthingRouter), | ||
); | ||
|
||
const app = router.pipe( | ||
cors, | ||
Http.server.serve(Http.middleware.logger), | ||
Http.server.withLogAddress, | ||
); | ||
|
||
const Port = Config.integer("PORT").pipe(Config.withDefault(3000)); | ||
const ServerLive = Layer.unwrapEffect( | ||
Effect.map(Port, (port) => | ||
NodeHttpServer.server.layer(() => createServer(), { port }), | ||
), | ||
); | ||
|
||
NodeRuntime.runMain(Layer.launch(Layer.provide(app, ServerLive))); |
This file was deleted.
Oops, something went wrong.
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,104 @@ | ||
import * as Http from "@effect/platform/HttpServer"; | ||
import * as Effect from "effect/Effect"; | ||
|
||
import type { Json } from "@uploadthing/shared"; | ||
import { | ||
FetchContext, | ||
getStatusCodeFromError, | ||
UploadThingError, | ||
} from "@uploadthing/shared"; | ||
|
||
import { UPLOADTHING_VERSION } from "./internal/constants"; | ||
import { formatError } from "./internal/error-formatter"; | ||
import { | ||
buildPermissionsInfoHandler, | ||
buildRequestHandler, | ||
} from "./internal/handler"; | ||
import { incompatibleNodeGuard } from "./internal/incompat-node-guard"; | ||
import { toWebRequest } from "./internal/to-web-request"; | ||
import type { FileRouter, RouteHandlerOptions } from "./internal/types"; | ||
import type { CreateBuilderOptions } from "./internal/upload-builder"; | ||
import { createBuilder } from "./internal/upload-builder"; | ||
|
||
export { UTFiles } from "./internal/types"; | ||
export type { FileRouter }; | ||
|
||
type MiddlewareArgs = { | ||
req: Http.request.ServerRequest; | ||
res: undefined; | ||
event: undefined; | ||
}; | ||
|
||
export const createUploadthing = <TErrorShape extends Json>( | ||
opts?: CreateBuilderOptions<TErrorShape>, | ||
) => createBuilder<MiddlewareArgs, TErrorShape>(opts); | ||
|
||
export const createRouteHandler = <TRouter extends FileRouter>( | ||
opts: RouteHandlerOptions<TRouter>, | ||
): Http.router.Router<Http.body.BodyError, never> => { | ||
incompatibleNodeGuard(); | ||
|
||
const requestHandler = buildRequestHandler<TRouter, MiddlewareArgs>( | ||
opts, | ||
"effect-platform", | ||
); | ||
const getBuildPerms = buildPermissionsInfoHandler<TRouter>(opts); | ||
|
||
const appendUploadThingResponseHeaders = Http.middleware.make( | ||
Effect.map( | ||
Http.response.setHeader("x-uploadthing-version", UPLOADTHING_VERSION), | ||
), | ||
); | ||
|
||
return Http.router.empty.pipe( | ||
Http.router.get("/", Http.response.json(getBuildPerms())), | ||
Http.router.post( | ||
"/", | ||
Effect.flatMap(Http.request.ServerRequest, (req) => | ||
requestHandler({ | ||
/** | ||
* TODO: Redo this to be more cross-platform | ||
* This should handle WinterCG and Node.js runtimes, | ||
* unsure about others... | ||
* Perhaps we can use `Http.request.ServerRequest` internally? | ||
*/ | ||
req: Effect.if(req.source instanceof Request, { | ||
onTrue: () => Effect.succeed(req.source as Request), | ||
onFalse: () => | ||
req.json.pipe( | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
Effect.flatMap((body) => toWebRequest(req.source as any, body)), | ||
Effect.catchTags({ | ||
RequestError: (error) => | ||
new UploadThingError({ | ||
code: "BAD_REQUEST", | ||
message: "INVALID_JSON", | ||
cause: error, | ||
}), | ||
}), | ||
), | ||
}), | ||
middlewareArgs: { req, res: undefined, event: undefined }, | ||
}).pipe( | ||
Effect.provideService(FetchContext, { | ||
fetch: opts.config?.fetch ?? globalThis.fetch, | ||
baseHeaders: { | ||
"x-uploadthing-version": UPLOADTHING_VERSION, | ||
// These are filled in later in `parseAndValidateRequest` | ||
"x-uploadthing-api-key": undefined, | ||
"x-uploadthing-be-adapter": undefined, | ||
"x-uploadthing-fe-package": undefined, | ||
}, | ||
}), | ||
Effect.andThen((response) => Http.response.json(response.body)), | ||
Effect.catchTag("UploadThingError", (error) => | ||
Http.response.json(formatError(error, opts.router), { | ||
status: getStatusCodeFromError(error), | ||
}), | ||
), | ||
), | ||
), | ||
), | ||
Http.router.use(appendUploadThingResponseHeaders), | ||
); | ||
}; |
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
"build": { | ||
"outputs": [ | ||
"client/**", | ||
"effect-platform/**", | ||
"express/**", | ||
"fastify/**", | ||
"h3/**", | ||
|
Oops, something went wrong.