feat: 完成模板目录导入与生命周期管理
This commit is contained in:
1 parent
553a07c4ec
commit
413413efda
25 files changed
+1313
-24
No files matched your search
@@ -19,22 +19,33 @@ export interface ApiClientOptions {
|
||||
|
||||
export type RequestFn = <T>(
|
||||
path: string,
|
||||
options?: { method?: string; body?: unknown },
|
||||
options?: { method?: string; body?: unknown; rawBody?: unknown },
|
||||
) => Promise<T>;
|
||||
|
||||
export async function request<T>(
|
||||
state: { baseUrl: string; fetcher: FetchLike; token?: string | undefined },
|
||||
path: string,
|
||||
options: { method?: string; body?: unknown } = {},
|
||||
options: { method?: string; body?: unknown; rawBody?: unknown } = {},
|
||||
): Promise<T> {
|
||||
const headers: Record<string, string> = { accept: "application/json" };
|
||||
if (options.body !== undefined) headers["content-type"] = "application/json";
|
||||
if (options.rawBody === undefined && options.body !== undefined)
|
||||
headers["content-type"] = "application/json";
|
||||
if (state.token) headers.authorization = `Bearer ${state.token}`;
|
||||
const response = await state.fetcher(`${state.baseUrl}${path}`, {
|
||||
const init = {
|
||||
method: options.method ?? "GET",
|
||||
headers,
|
||||
...(options.body === undefined ? {} : { body: JSON.stringify(options.body) }),
|
||||
});
|
||||
...(options.rawBody !== undefined
|
||||
? { body: options.rawBody }
|
||||
: options.body === undefined
|
||||
? {}
|
||||
: { body: JSON.stringify(options.body) }),
|
||||
};
|
||||
const response = await (
|
||||
state.fetcher as unknown as (
|
||||
input: string,
|
||||
init?: { method?: string; headers?: Record<string, string>; body?: unknown },
|
||||
) => Promise<FetchResponse>
|
||||
)(`${state.baseUrl}${path}`, init);
|
||||
const body = await response.json();
|
||||
if (!response.ok) throw new ApiClientError(response.status, body as ApiErrorBody);
|
||||
return body as T;
|
||||
|
||||
@@ -36,6 +36,14 @@ export type {
|
||||
TemplateAdminField,
|
||||
TemplateAdminSection,
|
||||
} from "./types/template.js";
|
||||
export type {
|
||||
TemplateMethods,
|
||||
TemplatePackageActionResult,
|
||||
TemplatePackageAuditEntry,
|
||||
TemplatePackageImportResult,
|
||||
TemplatePackageList,
|
||||
TemplatePackageVersion,
|
||||
} from "./resources/template.js";
|
||||
export type { TrustContent, TrustContentInput } from "./types/trust.js";
|
||||
export type { PublicWorkspace } from "./types/workspace.js";
|
||||
export { projectBrandAliases, projectTrustPresentation } from "@personal-brand/types";
|
||||
@@ -90,7 +98,7 @@ export class ApiClient
|
||||
private readonly state: ClientState;
|
||||
private readonly requestFn: <T>(
|
||||
path: string,
|
||||
options?: { method?: string; body?: unknown },
|
||||
options?: { method?: string; body?: unknown; rawBody?: unknown },
|
||||
) => Promise<T>;
|
||||
private resources!: {
|
||||
booking: BookingMethods;
|
||||
@@ -229,6 +237,21 @@ export class ApiClient
|
||||
updateAdminMiniAppSettings = (
|
||||
...arguments_: Parameters<BrandMethods["updateAdminMiniAppSettings"]>
|
||||
) => this.resources.brand.updateAdminMiniAppSettings(...arguments_);
|
||||
listAdminTemplatePackages = (
|
||||
...arguments_: Parameters<TemplateMethods["listAdminTemplatePackages"]>
|
||||
) => this.resources.template.listAdminTemplatePackages(...arguments_);
|
||||
importAdminTemplatePackage = (
|
||||
...arguments_: Parameters<TemplateMethods["importAdminTemplatePackage"]>
|
||||
) => this.resources.template.importAdminTemplatePackage(...arguments_);
|
||||
publishAdminTemplatePackage = (
|
||||
...arguments_: Parameters<TemplateMethods["publishAdminTemplatePackage"]>
|
||||
) => this.resources.template.publishAdminTemplatePackage(...arguments_);
|
||||
retireAdminTemplatePackage = (
|
||||
...arguments_: Parameters<TemplateMethods["retireAdminTemplatePackage"]>
|
||||
) => this.resources.template.retireAdminTemplatePackage(...arguments_);
|
||||
listAdminTemplatePackageAudit = (
|
||||
...arguments_: Parameters<TemplateMethods["listAdminTemplatePackageAudit"]>
|
||||
) => this.resources.template.listAdminTemplatePackageAudit(...arguments_);
|
||||
getPublicTemplate = (...arguments_: Parameters<TemplateMethods["getPublicTemplate"]>) =>
|
||||
this.resources.template.getPublicTemplate(...arguments_);
|
||||
listPublicTemplates = (...arguments_: Parameters<TemplateMethods["listPublicTemplates"]>) =>
|
||||
|
||||
@@ -9,6 +9,7 @@ export interface SessionMethods {
|
||||
displayName: string;
|
||||
mustChangePassword: boolean;
|
||||
role: string;
|
||||
platformTemplateRole: "none" | "template_admin";
|
||||
}>;
|
||||
changeAdminPassword(currentPassword: string, newPassword: string): Promise<{ changed: true }>;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,14 @@ import type { AdminTemplateContent } from "../types/template.js";
|
||||
import type { BrandTemplateDefinition } from "../types/template-definition.js";
|
||||
|
||||
export interface TemplateMethods {
|
||||
listAdminTemplatePackages(params?: {
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
}): Promise<TemplatePackageList>;
|
||||
importAdminTemplatePackage(body: unknown): Promise<TemplatePackageImportResult>;
|
||||
publishAdminTemplatePackage(id: string): Promise<TemplatePackageActionResult>;
|
||||
retireAdminTemplatePackage(id: string): Promise<TemplatePackageActionResult>;
|
||||
listAdminTemplatePackageAudit(id: string): Promise<TemplatePackageAuditEntry[]>;
|
||||
getPublicTemplate(key: BrandTemplateKey): Promise<BrandTemplateDefinition>;
|
||||
listPublicTemplates(): Promise<{
|
||||
industries: BrandTemplateDefinition[];
|
||||
@@ -86,8 +94,92 @@ export interface TemplateMethods {
|
||||
rollbackAdminTemplate(): Promise<unknown>;
|
||||
}
|
||||
|
||||
export interface TemplatePackageVersion {
|
||||
id: string;
|
||||
templatePackageId: string;
|
||||
packageKey: string;
|
||||
version: string;
|
||||
displayName: string;
|
||||
visualTemplateKey: string | null;
|
||||
status: "imported" | "published" | "retired" | "disabled";
|
||||
contentChecksum: string;
|
||||
archiveChecksum: string;
|
||||
validationReport: Record<string, unknown>;
|
||||
importedAt: string;
|
||||
updatedAt: string;
|
||||
importedBy: { id: string; username: string; displayName: string };
|
||||
}
|
||||
|
||||
export interface TemplatePackageList {
|
||||
items: TemplatePackageVersion[];
|
||||
page: number;
|
||||
pageSize: number;
|
||||
total: number;
|
||||
}
|
||||
|
||||
export interface TemplatePackageActionResult {
|
||||
changed: boolean;
|
||||
version: Pick<
|
||||
TemplatePackageVersion,
|
||||
| "id"
|
||||
| "packageKey"
|
||||
| "version"
|
||||
| "displayName"
|
||||
| "visualTemplateKey"
|
||||
| "status"
|
||||
| "contentChecksum"
|
||||
| "archiveChecksum"
|
||||
| "importedAt"
|
||||
>;
|
||||
}
|
||||
|
||||
export interface TemplatePackageAuditEntry {
|
||||
id: string;
|
||||
action: string;
|
||||
fromStatus: TemplatePackageVersion["status"] | null;
|
||||
toStatus: TemplatePackageVersion["status"] | null;
|
||||
requestId: string | null;
|
||||
metadata: Record<string, unknown> | null;
|
||||
createdAt: string;
|
||||
actor: { id: string; username: string; displayName: string };
|
||||
}
|
||||
|
||||
export interface TemplatePackageImportResult {
|
||||
imported: boolean;
|
||||
idempotent: boolean;
|
||||
version: Pick<
|
||||
TemplatePackageVersion,
|
||||
| "id"
|
||||
| "packageKey"
|
||||
| "version"
|
||||
| "displayName"
|
||||
| "visualTemplateKey"
|
||||
| "status"
|
||||
| "contentChecksum"
|
||||
| "archiveChecksum"
|
||||
| "importedAt"
|
||||
>;
|
||||
}
|
||||
|
||||
export function templateMethods({ request }: { request: RequestFn }): TemplateMethods {
|
||||
return {
|
||||
listAdminTemplatePackages: (params = {}) => {
|
||||
const query = [
|
||||
params.page === undefined ? null : `page=${encodeURIComponent(String(params.page))}`,
|
||||
params.pageSize === undefined
|
||||
? null
|
||||
: `pageSize=${encodeURIComponent(String(params.pageSize))}`,
|
||||
].filter((item): item is string => item !== null);
|
||||
return request(`/admin/templates${query.length ? `?${query.join("&")}` : ""}`);
|
||||
},
|
||||
importAdminTemplatePackage: (body) =>
|
||||
request("/admin/templates/import", { method: "POST", rawBody: body }),
|
||||
publishAdminTemplatePackage: (id) =>
|
||||
request(`/admin/templates/${encodeURIComponent(id)}/publish`, { method: "POST" }),
|
||||
retireAdminTemplatePackage: (id) =>
|
||||
request(`/admin/templates/${encodeURIComponent(id)}/retire`, { method: "POST" }),
|
||||
listAdminTemplatePackageAudit: (id) =>
|
||||
request(`/admin/templates/${encodeURIComponent(id)}/audit`),
|
||||
getPublicTemplate: (key) => request(`/public/templates/${key}`),
|
||||
listPublicTemplates: () =>
|
||||
request<{
|
||||
|
||||
@@ -95,3 +95,29 @@ test("小程序上传配置会提交请求地址", async () => {
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test("模板目录客户端发送分页并支持版本发布和审计", async () => {
|
||||
const calls: Array<{ url: string; method?: string }> = [];
|
||||
const client = new ApiClient({
|
||||
baseUrl: "http://api.test",
|
||||
token: "template-admin-token",
|
||||
fetcher: async (url, init) => {
|
||||
calls.push({ url, method: init?.method });
|
||||
return response(
|
||||
url.includes("/audit")
|
||||
? []
|
||||
: url.endsWith("/publish")
|
||||
? { changed: true, version: { id: "version-1", status: "published" } }
|
||||
: { items: [], page: 2, pageSize: 10, total: 0 },
|
||||
);
|
||||
},
|
||||
});
|
||||
await client.listAdminTemplatePackages({ page: 2, pageSize: 10 });
|
||||
await client.publishAdminTemplatePackage("version/1");
|
||||
await client.listAdminTemplatePackageAudit("version/1");
|
||||
assert.deepEqual(calls, [
|
||||
{ url: "http://api.test/admin/templates?page=2&pageSize=10", method: "GET" },
|
||||
{ url: "http://api.test/admin/templates/version%2F1/publish", method: "POST" },
|
||||
{ url: "http://api.test/admin/templates/version%2F1/audit", method: "GET" },
|
||||
]);
|
||||
});
|
||||
Reference in new issue
Block a user