From 3d3a303eb97a819583ea0f23c28feb772238703d Mon Sep 17 00:00:00 2001 From: Wanjohi Date: Mon, 27 Oct 2025 05:51:44 +0300 Subject: [PATCH] fix: Use Zod in the actor.ts file --- packages/core/src/actor.ts | 83 ++++++++++++++++++------------ packages/core/src/steam/index.ts | 2 +- packages/function/src/api/index.ts | 16 ++++-- 3 files changed, 63 insertions(+), 38 deletions(-) diff --git a/packages/core/src/actor.ts b/packages/core/src/actor.ts index e4ff5ead..ab2e4ac2 100644 --- a/packages/core/src/actor.ts +++ b/packages/core/src/actor.ts @@ -1,45 +1,64 @@ +import { z } from "zod/v4"; import { Log } from "./utils/log.js"; import { Context } from "./context.js"; import { ErrorCodes, VisibleError } from "./error.js"; export namespace Actor { - export interface User { - type: "user"; - properties: { - userID: string; - email: string; - }; - } + export const User = z.object({ + type: z.literal("user"), + properties: z.object({ + userID: z.string(), + email: z.string(), + }), + }); - export interface Steam { - type: "steam"; - properties: { - userID: string; - steamID: string; - }; - } + export type User = z.Infer; - export interface System { - type: "system"; - properties: { - userID: string; - }; - } + export const Steam = z.object({ + type: z.literal("steam"), + properties: z.object({ + userID: z.string(), + steamID: z.string(), + }), + }); - export interface Token { - type: "token"; - properties: { - teamID: string; - tokenID: string; - }; - } + export type Steam = z.Infer; - export interface Public { - type: "public"; - properties: {}; - } + export const System = z.object({ + type: z.literal("system"), + properties: z.object({ + userID: z.string(), + }), + }); - export type Info = Steam | User | Public | Token | System; + export type System = z.Infer; + + export const Token = z.object({ + type: z.literal("token"), + properties: z.object({ + teamID: z.string(), + tokenID: z.string(), + }), + }); + + export type Token = z.Infer; + + export const Public = z.object({ + type: z.literal("public"), + properties: z.object({}), + }); + + export type Public = z.Infer; + + export const Info = z.discriminatedUnion("type", [ + User, + Token, + Steam, + Public, + System, + ]); + + export type Info = z.Infer; export const ctx = Context.create(); diff --git a/packages/core/src/steam/index.ts b/packages/core/src/steam/index.ts index c2994555..55b3f63f 100644 --- a/packages/core/src/steam/index.ts +++ b/packages/core/src/steam/index.ts @@ -58,7 +58,7 @@ export namespace Steam { export const fromID = fn(Info.shape.id, async (id) => { const userID = Actor.userID(); - return Database.transaction((tx) => + return Database.use((tx) => tx .select() .from(steamTable) diff --git a/packages/function/src/api/index.ts b/packages/function/src/api/index.ts index 4eb6b289..8de33ca7 100644 --- a/packages/function/src/api/index.ts +++ b/packages/function/src/api/index.ts @@ -28,8 +28,9 @@ app }); }) .use( - validator("header", z.object({ "x-nestri-member": z.string().optional() })), + validator("header", z.object({ "x-nestri-steam": z.string().optional() })), ) + .use(validator("query", z.object({ steamID: z.string().optional() }))) .use(auth) .use(cors()); @@ -86,15 +87,15 @@ app.get( scheme: "bearer", bearerFormat: "JWT", }, - MemberID: { + SteamID: { type: "apiKey", - description: "The member ID to use for this query", + description: "The steamID to use for this query", in: "header", - name: "x-nestri-member", + name: "x-nestri-steam", }, }, }, - security: [{ Bearer: [], MemberID: [] }], + security: [{ Bearer: [], SteamID: [] }], servers: [ { description: "Production", url: "https://api.nestri.io" }, { description: "Sandbox", url: "https://api.dev.nestri.io" }, @@ -102,3 +103,8 @@ app.get( }, }), ); + +export default { + port: 3001, + fetch: app.fetch, +};