mirror of
https://github.com/nestriness/nestri.git
synced 2026-08-01 17:34:15 +03:00
fix: Use Zod in the actor.ts file
This commit is contained in:
@@ -1,45 +1,64 @@
|
|||||||
|
import { z } from "zod/v4";
|
||||||
import { Log } from "./utils/log.js";
|
import { Log } from "./utils/log.js";
|
||||||
import { Context } from "./context.js";
|
import { Context } from "./context.js";
|
||||||
import { ErrorCodes, VisibleError } from "./error.js";
|
import { ErrorCodes, VisibleError } from "./error.js";
|
||||||
|
|
||||||
export namespace Actor {
|
export namespace Actor {
|
||||||
export interface User {
|
export const User = z.object({
|
||||||
type: "user";
|
type: z.literal("user"),
|
||||||
properties: {
|
properties: z.object({
|
||||||
userID: string;
|
userID: z.string(),
|
||||||
email: string;
|
email: z.string(),
|
||||||
};
|
}),
|
||||||
}
|
});
|
||||||
|
|
||||||
export interface Steam {
|
export type User = z.Infer<typeof User>;
|
||||||
type: "steam";
|
|
||||||
properties: {
|
|
||||||
userID: string;
|
|
||||||
steamID: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface System {
|
export const Steam = z.object({
|
||||||
type: "system";
|
type: z.literal("steam"),
|
||||||
properties: {
|
properties: z.object({
|
||||||
userID: string;
|
userID: z.string(),
|
||||||
};
|
steamID: z.string(),
|
||||||
}
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
export interface Token {
|
export type Steam = z.Infer<typeof Steam>;
|
||||||
type: "token";
|
|
||||||
properties: {
|
|
||||||
teamID: string;
|
|
||||||
tokenID: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Public {
|
export const System = z.object({
|
||||||
type: "public";
|
type: z.literal("system"),
|
||||||
properties: {};
|
properties: z.object({
|
||||||
}
|
userID: z.string(),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
export type Info = Steam | User | Public | Token | System;
|
export type System = z.Infer<typeof System>;
|
||||||
|
|
||||||
|
export const Token = z.object({
|
||||||
|
type: z.literal("token"),
|
||||||
|
properties: z.object({
|
||||||
|
teamID: z.string(),
|
||||||
|
tokenID: z.string(),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type Token = z.Infer<typeof Token>;
|
||||||
|
|
||||||
|
export const Public = z.object({
|
||||||
|
type: z.literal("public"),
|
||||||
|
properties: z.object({}),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type Public = z.Infer<typeof Public>;
|
||||||
|
|
||||||
|
export const Info = z.discriminatedUnion("type", [
|
||||||
|
User,
|
||||||
|
Token,
|
||||||
|
Steam,
|
||||||
|
Public,
|
||||||
|
System,
|
||||||
|
]);
|
||||||
|
|
||||||
|
export type Info = z.Infer<typeof Info>;
|
||||||
|
|
||||||
export const ctx = Context.create<Info>();
|
export const ctx = Context.create<Info>();
|
||||||
|
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ export namespace Steam {
|
|||||||
export const fromID = fn(Info.shape.id, async (id) => {
|
export const fromID = fn(Info.shape.id, async (id) => {
|
||||||
const userID = Actor.userID();
|
const userID = Actor.userID();
|
||||||
|
|
||||||
return Database.transaction((tx) =>
|
return Database.use((tx) =>
|
||||||
tx
|
tx
|
||||||
.select()
|
.select()
|
||||||
.from(steamTable)
|
.from(steamTable)
|
||||||
|
|||||||
@@ -28,8 +28,9 @@ app
|
|||||||
});
|
});
|
||||||
})
|
})
|
||||||
.use(
|
.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(auth)
|
||||||
.use(cors());
|
.use(cors());
|
||||||
|
|
||||||
@@ -86,15 +87,15 @@ app.get(
|
|||||||
scheme: "bearer",
|
scheme: "bearer",
|
||||||
bearerFormat: "JWT",
|
bearerFormat: "JWT",
|
||||||
},
|
},
|
||||||
MemberID: {
|
SteamID: {
|
||||||
type: "apiKey",
|
type: "apiKey",
|
||||||
description: "The member ID to use for this query",
|
description: "The steamID to use for this query",
|
||||||
in: "header",
|
in: "header",
|
||||||
name: "x-nestri-member",
|
name: "x-nestri-steam",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
security: [{ Bearer: [], MemberID: [] }],
|
security: [{ Bearer: [], SteamID: [] }],
|
||||||
servers: [
|
servers: [
|
||||||
{ description: "Production", url: "https://api.nestri.io" },
|
{ description: "Production", url: "https://api.nestri.io" },
|
||||||
{ description: "Sandbox", url: "https://api.dev.nestri.io" },
|
{ description: "Sandbox", url: "https://api.dev.nestri.io" },
|
||||||
@@ -102,3 +103,8 @@ app.get(
|
|||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export default {
|
||||||
|
port: 3001,
|
||||||
|
fetch: app.fetch,
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user