chore: initialize bun ts config

This commit is contained in:
gitea committed 2026-06-05 16:03:46 +08:00
commit 25e4dd7714
8 files changed
+447

No files matched your search

+19
View File
@@ -0,0 +1,19 @@
import { existsSync, readFileSync, readdirSync } from "node:fs";
import { join } from "node:path";
import { CorpConfig, CorpConfigSchema } from "./types.js";
const ENTERPRISES_DIR = "enterprises";
export function loadCorpConfigs(): CorpConfig[] {
if (!existsSync(ENTERPRISES_DIR)) {
return [];
}
return readdirSync(ENTERPRISES_DIR)
.filter((file) => file.endsWith(".json"))
.filter((file) => !file.startsWith("example."))
.map((file) => {
const content = readFileSync(join(ENTERPRISES_DIR, file), "utf-8");
return CorpConfigSchema.parse(JSON.parse(content));
});
}
+33
View File
@@ -0,0 +1,33 @@
import { CorpConfig } from "./types.js";
export class CorpContext {
private current: CorpConfig | null = null;
private readonly corps = new Map<string, CorpConfig>();
constructor(corps: CorpConfig[]) {
for (const corp of corps) {
this.corps.set(corp.id, corp);
}
this.current = corps[0] ?? null;
}
getCurrent(): CorpConfig | null {
return this.current;
}
switchCorp(corpId: string): boolean {
const corp = this.corps.get(corpId);
if (!corp) return false;
this.current = corp;
return true;
}
listCorps(): Array<{ id: string; name: string; sourceType: string; current: boolean }> {
return Array.from(this.corps.values()).map((corp) => ({
id: corp.id,
name: corp.name,
sourceType: corp.source.type,
current: corp.id === this.current?.id
}));
}
}
+60
View File
@@ -0,0 +1,60 @@
import { describe, expect, test } from "bun:test";
import { CorpConfigSchema } from "./types.js";
describe("CorpConfigSchema", () => {
test("accepts Hose key/password aliases", () => {
const config = CorpConfigSchema.parse({
id: "corp-local",
name: "Local Corp",
source: {
type: "hose",
domain: "https://app.ekuaibao.com",
key: "app-key",
password: "app-security",
corpId: "ID01",
staffId: "ID01:STAFF01"
}
});
expect(config.source.type).toBe("hose");
if (config.source.type !== "hose") {
throw new Error("expected hose source");
}
expect(config.source.key).toBe("app-key");
expect(config.source.password).toBe("app-security");
expect(config.auth.refreshTtl).toBe(300);
});
test("requires a Hose key and secret", () => {
const result = CorpConfigSchema.safeParse({
id: "corp-local",
name: "Local Corp",
source: {
type: "hose",
domain: "https://app.ekuaibao.com",
corpId: "ID01",
staffId: "ID01:STAFF01"
}
});
expect(result.success).toBe(false);
});
test("accepts direct Seal bearer source", () => {
const config = CorpConfigSchema.parse({
id: "direct",
name: "Direct",
source: {
type: "direct",
token: "seal-token",
sealUrl: "https://direct.sealai.cc"
}
});
expect(config.source.type).toBe("direct");
if (config.source.type !== "direct") {
throw new Error("expected direct source");
}
expect(config.source.expiresIn).toBe(3600);
});
});
+75
View File
@@ -0,0 +1,75 @@
import { z } from "zod";
export const HoseSealSourceSchema = z.object({
type: z.literal("hose"),
domain: z.string().url().default("https://app.ekuaibao.com"),
appKey: z.string().optional(),
appSecurity: z.string().optional(),
key: z.string().optional(),
password: z.string().optional(),
staffId: z.string(),
corpId: z.string(),
sealUrl: z.string().url().optional()
}).superRefine((source, ctx) => {
if (!source.appKey && !source.key) {
ctx.addIssue({
code: "custom",
path: ["appKey"],
message: "Hose source requires appKey or key"
});
}
if (!source.appSecurity && !source.password) {
ctx.addIssue({
code: "custom",
path: ["appSecurity"],
message: "Hose source requires appSecurity or password"
});
}
});
export const DirectSealSourceSchema = z.object({
type: z.literal("direct"),
token: z.string(),
sealUrl: z.string().url(),
expiresIn: z.number().int().positive().default(3600),
staffId: z.string().default("direct"),
corpId: z.string().optional()
});
export const SealSourceSchema = z.discriminatedUnion("type", [
HoseSealSourceSchema,
DirectSealSourceSchema
]);
export const SealEndpointSchema = z.object({
url: z.string().url().optional(),
tenantSlug: z.string().optional(),
endpoints: z.object({
approvalStylePreferences: z.string().default("api/v1/approval/style-preferences")
}).default({
approvalStylePreferences: "api/v1/approval/style-preferences"
})
}).default({
endpoints: {
approvalStylePreferences: "api/v1/approval/style-preferences"
}
});
export const CorpAuthConfigSchema = z.object({
refreshTtl: z.number().int().positive().default(300)
}).default({ refreshTtl: 300 });
export const CorpConfigSchema = z.object({
id: z.string(),
name: z.string(),
seal: SealEndpointSchema,
source: SealSourceSchema,
auth: CorpAuthConfigSchema
});
export type HoseSealSource = z.infer<typeof HoseSealSourceSchema>;
export type DirectSealSource = z.infer<typeof DirectSealSourceSchema>;
export type SealSource = z.infer<typeof SealSourceSchema>;
export type CorpAuthConfig = z.infer<typeof CorpAuthConfigSchema>;
export type CorpConfig = z.infer<typeof CorpConfigSchema>;