feat: add @effect/rpc
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
"dependencies": {
|
||||
"@effect/platform": "^0.93.2",
|
||||
"@effect/platform-node": "^0.101.1",
|
||||
"@effect/rpc": "^0.72.2",
|
||||
"@money/shared": "workspace:*",
|
||||
"better-auth": "^1.3.27",
|
||||
"effect": "^3.19.4",
|
||||
|
||||
@@ -5,11 +5,12 @@ import * as HttpServerResponse from "@effect/platform/HttpServerResponse";
|
||||
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
|
||||
import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
|
||||
import { createServer } from "http";
|
||||
import { CorsMiddleware } from "./middleware/cors";
|
||||
import { AuthRoute } from "./auth/handler";
|
||||
import { BetterAuthLive } from "./auth/better-auth";
|
||||
import { WebhookReceiverRoute } from "./webhook";
|
||||
import { ZeroMutateRoute, ZeroQueryRoute } from "./zero/handler";
|
||||
import { RpcRoute } from "./rpc/handler";
|
||||
import { BASE_URL } from "@money/shared";
|
||||
|
||||
const RootRoute = HttpLayerRouter.add(
|
||||
"GET",
|
||||
@@ -24,13 +25,22 @@ const AllRoutes = Layer.mergeAll(
|
||||
AuthRoute,
|
||||
ZeroQueryRoute,
|
||||
ZeroMutateRoute,
|
||||
RpcRoute,
|
||||
WebhookReceiverRoute,
|
||||
).pipe(
|
||||
Layer.provide(
|
||||
HttpLayerRouter.cors({
|
||||
allowedOrigins: ["https://money.koon.us", `${BASE_URL}:8081`],
|
||||
allowedMethods: ["POST", "GET", "OPTIONS"],
|
||||
// allowedHeaders: ["Content-Type", "Authorization", ""],
|
||||
credentials: true,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
HttpLayerRouter.serve(AllRoutes).pipe(
|
||||
Layer.provide(NodeHttpServer.layer(createServer, { port: 3000 })),
|
||||
Layer.provide(BetterAuthLive),
|
||||
Layer.provide(CorsMiddleware.layer),
|
||||
Layer.launch,
|
||||
NodeRuntime.runMain,
|
||||
);
|
||||
|
||||
@@ -8,4 +8,5 @@ export const CorsMiddleware = HttpLayerRouter.middleware(
|
||||
allowedHeaders: ["Content-Type", "Authorization"],
|
||||
credentials: true,
|
||||
}),
|
||||
{ global: true },
|
||||
);
|
||||
|
||||
13
apps/api/src/rpc/handler.ts
Normal file
13
apps/api/src/rpc/handler.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { RpcSerialization, RpcServer } from "@effect/rpc";
|
||||
import { Effect, Layer, Schema } from "effect";
|
||||
import { LinkRpcs, Link } from "@money/shared/rpc";
|
||||
|
||||
const LinkHandlers = LinkRpcs.toLayer({
|
||||
CreateLink: () => Effect.succeed(new Link({ href: "hi" })),
|
||||
});
|
||||
|
||||
export const RpcRoute = RpcServer.layerHttpRouter({
|
||||
group: LinkRpcs,
|
||||
path: "/rpc",
|
||||
protocol: "http",
|
||||
}).pipe(Layer.provide(LinkHandlers), Layer.provide(RpcSerialization.layerJson));
|
||||
30
apps/expo/app/rpc.tsx
Normal file
30
apps/expo/app/rpc.tsx
Normal file
@@ -0,0 +1,30 @@
|
||||
import { Button, Text, View } from "react-native";
|
||||
import { AtomRpc, useAtomSet } from "@effect-atom/atom-react";
|
||||
import { Layer } from "effect";
|
||||
import { LinkRpcs } from "@money/shared/rpc";
|
||||
import { FetchHttpClient } from "@effect/platform";
|
||||
import { RpcClient, RpcSerialization } from "@effect/rpc";
|
||||
|
||||
class Client extends AtomRpc.Tag<Client>()("RpcClient", {
|
||||
group: LinkRpcs,
|
||||
protocol: RpcClient.layerProtocolHttp({
|
||||
url: "http://laptop:3000/rpc",
|
||||
}).pipe(Layer.provide([RpcSerialization.layerJson, FetchHttpClient.layer])),
|
||||
}) {}
|
||||
|
||||
export default function Page() {
|
||||
const create = useAtomSet(Client.mutation("CreateLink"));
|
||||
|
||||
const onPress = () => {
|
||||
create({
|
||||
payload: void 0,
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<View>
|
||||
<Text>RPC Test</Text>
|
||||
<Button onPress={onPress} title="Create link" />
|
||||
</View>
|
||||
);
|
||||
}
|
||||
@@ -15,6 +15,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@better-auth/expo": "^1.3.27",
|
||||
"@effect-atom/atom-react": "^0.4.0",
|
||||
"@expo/vector-icons": "^15.0.2",
|
||||
"@money/shared": "workspace:*",
|
||||
"@money/ui": "workspace:*",
|
||||
|
||||
Reference in New Issue
Block a user