feat: rpc client

This commit is contained in:
Max Koon
2025-11-29 00:57:32 -05:00
parent 3ebb7ee796
commit 74f4da1d3d
9 changed files with 214 additions and 105 deletions

View File

@@ -1,26 +1,6 @@
import { z } from "zod";
import { Schema } from "effect";
export const sessionSchema = z.object({
id: z.string(),
userId: z.string(),
expiresAt: z.date(),
});
export const userSchema = z.object({
id: z.string(),
email: z.string(),
emailVerified: z.boolean(),
name: z.string(),
createdAt: z.date(),
updatedAt: z.date(),
});
export const authDataSchema = z.object({
session: sessionSchema,
user: userSchema,
});
const DateFromDateOrString = Schema.Union(
Schema.DateFromString,
Schema.DateFromSelf,
@@ -53,7 +33,3 @@ export const AuthSchema = Schema.Struct({
});
export type AuthSchemaType = Schema.Schema.Type<typeof AuthSchema>;
export type Session = z.infer<typeof sessionSchema>;
export type User = z.infer<typeof userSchema>;
export type AuthData = z.infer<typeof authDataSchema>;

View File

@@ -1,13 +1,29 @@
import { Schema } from "effect";
import { Rpc, RpcGroup } from "@effect/rpc";
import { Context, Schema } from "effect";
import { Rpc, RpcGroup, RpcMiddleware } from "@effect/rpc";
import type { AuthSchema } from "./auth";
export class Link extends Schema.Class<Link>("Link")({
href: Schema.String,
}) {}
export class CurrentSession extends Context.Tag("CurrentSession")<
CurrentSession,
{ readonly auth: Schema.Schema.Type<typeof AuthSchema> | null }
>() {}
export class AuthMiddleware extends RpcMiddleware.Tag<AuthMiddleware>()(
"AuthMiddleware",
{
// This middleware will provide the current user context
provides: CurrentSession,
// This middleware requires a client implementation too
requiredForClient: true,
},
) {}
export class LinkRpcs extends RpcGroup.make(
Rpc.make("CreateLink", {
success: Link,
error: Schema.String,
}),
) {}
).middleware(AuthMiddleware) {}