511 lines
15 KiB
TypeScript
511 lines
15 KiB
TypeScript
import type {
|
|
BookingRequestType,
|
|
BookingStatus,
|
|
ContactPreference,
|
|
FulfillmentType,
|
|
PublicOffering,
|
|
BrandTemplateKey,
|
|
} from "@personal-brand/types";
|
|
export type { PublicOffering } from "@personal-brand/types";
|
|
export type { BookingStatus } from "@personal-brand/types";
|
|
|
|
export interface BrandProfile {
|
|
id: string;
|
|
name: string;
|
|
templateKey: BrandTemplateKey;
|
|
tagline: string | null;
|
|
description: string | null;
|
|
avatarUrl: string | null;
|
|
coverUrl: string | null;
|
|
hours: string | null;
|
|
address: string | null;
|
|
deliveryNote: string | null;
|
|
contactPhone: string | null;
|
|
contactWechat: string | null;
|
|
config?: Record<string, unknown> | null;
|
|
}
|
|
export type BrandUpdate = Partial<Omit<BrandProfile, "id">>;
|
|
export interface OfferingUpdate {
|
|
name?: string;
|
|
description?: string;
|
|
coverUrl?: string;
|
|
priceText?: string;
|
|
priceAmount?: number;
|
|
sortOrder?: number;
|
|
bookingEnabled?: boolean;
|
|
isEnabled?: boolean;
|
|
categoryKey?: string;
|
|
config?: Record<string, unknown>;
|
|
}
|
|
export interface TrustContent {
|
|
id: string;
|
|
type: string;
|
|
title: string;
|
|
summary?: string | null;
|
|
content: string;
|
|
coverUrl?: string | null;
|
|
sortOrder: number;
|
|
isPublished: boolean;
|
|
config?: Record<string, unknown> | null;
|
|
}
|
|
export interface TrustContentInput {
|
|
type: string;
|
|
title: string;
|
|
summary?: string;
|
|
content: string;
|
|
coverUrl?: string;
|
|
sortOrder?: number;
|
|
isPublished?: boolean;
|
|
config?: Record<string, unknown>;
|
|
}
|
|
|
|
export interface BookingItemInput {
|
|
offeringId: string;
|
|
quantity: number;
|
|
}
|
|
export interface CreateBookingInput {
|
|
requestType: BookingRequestType;
|
|
customerName: string;
|
|
phone?: string;
|
|
wechatId?: string;
|
|
contactPreference: ContactPreference;
|
|
fulfillmentType?: FulfillmentType;
|
|
offeringId?: string;
|
|
requestedDate?: string;
|
|
requestedTime?: string;
|
|
deliveryAddress?: string;
|
|
note?: string;
|
|
extraData?: Record<string, unknown>;
|
|
items: BookingItemInput[];
|
|
}
|
|
export interface BookingSummary {
|
|
bookingNo: string;
|
|
status: BookingStatus;
|
|
estimatedAmount: number | null;
|
|
createdAt: string;
|
|
}
|
|
export interface Booking extends BookingSummary {
|
|
id: string;
|
|
customerName: string;
|
|
adminNote?: string | null;
|
|
}
|
|
export interface BookingTag {
|
|
id: string;
|
|
name: string;
|
|
isPreset: boolean;
|
|
isEnabled: boolean;
|
|
}
|
|
export interface AdminBooking extends Booking {
|
|
tags?: Array<{ tag: BookingTag }>;
|
|
}
|
|
export interface ApiErrorBody {
|
|
data: null;
|
|
error: { code: string; message: string };
|
|
requestId: string | null;
|
|
}
|
|
export interface BrandTemplateDefinition {
|
|
key: "personal" | "food";
|
|
version: string;
|
|
name: string;
|
|
description: string;
|
|
serviceCategories: string[];
|
|
trustContentTypes: string[];
|
|
modules: Array<{
|
|
key: string;
|
|
type: string;
|
|
title: string;
|
|
required: boolean;
|
|
}>;
|
|
fields: Array<{
|
|
scope: string;
|
|
key: string;
|
|
label: string;
|
|
type: string;
|
|
required: boolean;
|
|
}>;
|
|
capabilities: string[];
|
|
runtimeCapabilities: Array<{
|
|
key: string;
|
|
version: string;
|
|
workflowKey?: string;
|
|
}>;
|
|
}
|
|
export interface OfferingSection {
|
|
type: "text" | "image-list" | "timeline" | "faq";
|
|
title: string;
|
|
content?: string;
|
|
items?: unknown[];
|
|
}
|
|
export interface OfferingMedia {
|
|
url: string;
|
|
alt?: string;
|
|
type?: "image" | "video";
|
|
}
|
|
export interface OfferingCta {
|
|
type: "inquiry" | "booking" | "contact" | "external-link";
|
|
label: string;
|
|
config?: Record<string, unknown>;
|
|
}
|
|
export interface BrandModule {
|
|
id: string;
|
|
type: string;
|
|
title: string;
|
|
isEnabled: boolean;
|
|
sortOrder: number;
|
|
source: string;
|
|
config?: Record<string, unknown> | null;
|
|
}
|
|
export interface BrandFieldDefinition {
|
|
id: string;
|
|
scope: string;
|
|
key: string;
|
|
label: string;
|
|
type: string;
|
|
required: boolean;
|
|
isVisible: boolean;
|
|
sortOrder: number;
|
|
source: string;
|
|
}
|
|
export interface InquiryForm {
|
|
id: string;
|
|
name: string;
|
|
offeringId?: string | null;
|
|
version: number;
|
|
isEnabled: boolean;
|
|
fieldsSnapshot: unknown[];
|
|
purpose: "inquiry" | "booking" | "quote-request";
|
|
targetType: "brand" | "offering";
|
|
submissionAction: "create-inquiry" | "create-booking" | "create-quote";
|
|
}
|
|
export interface InquirySubmission {
|
|
id: string;
|
|
inquiryFormId: string;
|
|
formVersion: number;
|
|
fieldsSnapshot: unknown[];
|
|
values: Record<string, unknown>;
|
|
activeFields: Record<string, boolean>;
|
|
createdAt: string;
|
|
status: "new" | "reviewing" | "replied" | "qualified" | "converted" | "closed";
|
|
adminNote?: string | null;
|
|
inquiryForm?: { name: string };
|
|
offering?: { id: string; name: string } | null;
|
|
}
|
|
export interface PublicWorkspace {
|
|
schemaVersion?: string;
|
|
navigation?: { tabs: Array<{ key: string; label: string; page: string }> };
|
|
theme?: {
|
|
colors: Record<string, string>;
|
|
typography?: Record<string, string>;
|
|
spacing?: Record<string, string>;
|
|
radius?: Record<string, string>;
|
|
};
|
|
contractVersion: string;
|
|
brand: BrandProfile | null;
|
|
template: { key: BrandTemplateKey; version: string; name: string } | null;
|
|
capabilities: string[];
|
|
modules: BrandModule[];
|
|
runtimeModules: Array<{
|
|
key: string;
|
|
type: string;
|
|
version?: string;
|
|
propsSchema?: string;
|
|
dataSource?: string;
|
|
source?: string;
|
|
title?: string;
|
|
schemaVersion?: string;
|
|
sortOrder?: number;
|
|
config?: Record<string, unknown> | null;
|
|
}>;
|
|
fields: BrandFieldDefinition[];
|
|
offerings: Offering[];
|
|
inquiryForm: InquiryForm | null;
|
|
runtimeOfferings?: Array<Record<string, unknown>>;
|
|
inquiryForms?: Array<Record<string, unknown>>;
|
|
runtimeCapabilities?: Array<{ key: string; version: string; status?: string }>;
|
|
}
|
|
export interface Offering {
|
|
id: string;
|
|
brandId?: string;
|
|
kind: "service" | "product" | "package" | "subscription";
|
|
name: string;
|
|
summary?: string | null;
|
|
description?: string | null;
|
|
coverUrl?: string | null;
|
|
priceText?: string | null;
|
|
priceAmount?: number | null;
|
|
isEnabled: boolean;
|
|
sortOrder: number;
|
|
config?: Record<string, unknown> | null;
|
|
sections?: OfferingSection[];
|
|
media?: OfferingMedia[];
|
|
cta?: OfferingCta;
|
|
inquiryForms?: Array<Pick<InquiryForm, "id" | "name" | "purpose" | "targetType" | "version">>;
|
|
}
|
|
|
|
export class ApiClientError extends Error {
|
|
constructor(
|
|
readonly status: number,
|
|
readonly body: ApiErrorBody,
|
|
) {
|
|
super(body.error.message);
|
|
this.name = "ApiClientError";
|
|
}
|
|
get code(): string {
|
|
return this.body.error.code;
|
|
}
|
|
get requestId(): string | null {
|
|
return this.body.requestId;
|
|
}
|
|
}
|
|
|
|
export interface FetchResponse {
|
|
readonly status: number;
|
|
readonly ok: boolean;
|
|
json(): Promise<unknown>;
|
|
}
|
|
export type FetchLike = (
|
|
input: string,
|
|
init?: { method?: string; headers?: Record<string, string>; body?: string },
|
|
) => Promise<FetchResponse>;
|
|
export interface ApiClientOptions {
|
|
baseUrl: string;
|
|
fetcher: FetchLike;
|
|
token?: string;
|
|
}
|
|
|
|
export class ApiClient {
|
|
private readonly baseUrl: string;
|
|
private readonly fetcher: FetchLike;
|
|
private token: string | undefined;
|
|
|
|
constructor(options: ApiClientOptions) {
|
|
this.baseUrl = options.baseUrl.replace(/\/$/, "");
|
|
this.fetcher = options.fetcher;
|
|
this.token = options.token;
|
|
}
|
|
setToken(token: string | undefined): void {
|
|
this.token = token;
|
|
}
|
|
getBrand(): Promise<BrandProfile> {
|
|
return this.request("/public/brand");
|
|
}
|
|
listOfferings(): Promise<PublicOffering[]> {
|
|
return this.request("/public/offerings");
|
|
}
|
|
getPublicWorkspace(): Promise<PublicWorkspace> {
|
|
return this.request("/public/workspace");
|
|
}
|
|
getPublicInquiryForm(id: string): Promise<InquiryForm> {
|
|
return this.request(`/public/inquiry-forms/${encodeURIComponent(id)}`);
|
|
}
|
|
createCustomerInquirySubmission(
|
|
formId: string,
|
|
values: Record<string, unknown>,
|
|
): Promise<unknown> {
|
|
return this.request("/customer/inquiry-submissions", {
|
|
method: "POST",
|
|
body: { formId, values },
|
|
});
|
|
}
|
|
createCustomerSession(code: string): Promise<{ token: string }> {
|
|
return this.request("/customer/session/wechat", { method: "POST", body: { code } });
|
|
}
|
|
listCustomerBookings(): Promise<Booking[]> {
|
|
return this.request("/customer/bookings");
|
|
}
|
|
createCustomerBooking(input: CreateBookingInput): Promise<BookingSummary> {
|
|
return this.request("/customer/bookings", { method: "POST", body: input });
|
|
}
|
|
cancelCustomerBooking(id: string): Promise<Booking> {
|
|
return this.request(`/customer/bookings/${encodeURIComponent(id)}/cancel`, { method: "POST" });
|
|
}
|
|
loginAdmin(username: string, password: string): Promise<{ token: string; displayName: string }> {
|
|
return this.request("/admin/session/login", { method: "POST", body: { username, password } });
|
|
}
|
|
listAdminBookings(
|
|
filters: { status?: BookingStatus; unreadOnly?: boolean; tagId?: string } = {},
|
|
): Promise<AdminBooking[]> {
|
|
const params: string[] = [];
|
|
if (filters.status) params.push(`status=${encodeURIComponent(filters.status)}`);
|
|
if (filters.unreadOnly !== undefined) params.push(`unreadOnly=${filters.unreadOnly}`);
|
|
if (filters.tagId) params.push(`tagId=${encodeURIComponent(filters.tagId)}`);
|
|
const query = params.join("&");
|
|
return this.request(`/admin/bookings${query ? `?${query}` : ""}`);
|
|
}
|
|
getAdminBooking(id: string): Promise<AdminBooking> {
|
|
return this.request(`/admin/bookings/${encodeURIComponent(id)}`);
|
|
}
|
|
getAdminUnreadCount(): Promise<{ count: number }> {
|
|
return this.request("/admin/bookings/unread-count");
|
|
}
|
|
updateAdminBookingStatus(
|
|
id: string,
|
|
status: BookingStatus,
|
|
adminNote?: string,
|
|
): Promise<Booking> {
|
|
return this.request(`/admin/bookings/${encodeURIComponent(id)}/status`, {
|
|
method: "POST",
|
|
body: { status, ...(adminNote === undefined ? {} : { adminNote }) },
|
|
});
|
|
}
|
|
markAdminBookingRead(id: string): Promise<Booking> {
|
|
return this.request(`/admin/bookings/${encodeURIComponent(id)}/read`, { method: "POST" });
|
|
}
|
|
listAdminTags(): Promise<BookingTag[]> {
|
|
return this.request("/admin/bookings/tags");
|
|
}
|
|
createAdminTag(name: string): Promise<BookingTag> {
|
|
return this.request("/admin/bookings/tags", { method: "POST", body: { name } });
|
|
}
|
|
getAdminBrand(): Promise<BrandProfile> {
|
|
return this.request("/admin/config/brand");
|
|
}
|
|
updateAdminBrand(body: BrandUpdate): Promise<BrandProfile> {
|
|
return this.request("/admin/config/brand", { method: "PATCH", body });
|
|
}
|
|
getPublicTemplate(key: "personal" | "food"): Promise<BrandTemplateDefinition> {
|
|
return this.request(`/public/templates/${key}`);
|
|
}
|
|
getPublicOffering(id: string): Promise<Offering> {
|
|
return this.request(`/public/offerings/${encodeURIComponent(id)}`);
|
|
}
|
|
listPublicOfferings(): Promise<Offering[]> {
|
|
return this.request("/public/offerings");
|
|
}
|
|
getAdminWorkspace(): Promise<{
|
|
brand: BrandProfile;
|
|
installations: unknown[];
|
|
modules: BrandModule[];
|
|
fields: BrandFieldDefinition[];
|
|
offerings: Offering[];
|
|
forms: unknown[];
|
|
}> {
|
|
return this.request("/admin/workspace");
|
|
}
|
|
installAdminTemplate(templateKey: "personal" | "food"): Promise<unknown> {
|
|
return this.request("/admin/workspace/install", { method: "POST", body: { templateKey } });
|
|
}
|
|
getAdminTemplateUpgradePreview(): Promise<{
|
|
currentVersion: string;
|
|
latestVersion: string;
|
|
canUpgrade: boolean;
|
|
canRollback: boolean;
|
|
missingModules: unknown[];
|
|
missingFields: unknown[];
|
|
}> {
|
|
return this.request("/admin/workspace/template/preview");
|
|
}
|
|
upgradeAdminTemplate(): Promise<unknown> {
|
|
return this.request("/admin/workspace/template/upgrade", { method: "POST" });
|
|
}
|
|
rollbackAdminTemplate(): Promise<unknown> {
|
|
return this.request("/admin/workspace/template/rollback", { method: "POST" });
|
|
}
|
|
updateAdminModule(id: string, body: Partial<BrandModule>): Promise<BrandModule> {
|
|
return this.request(`/admin/workspace/modules/${encodeURIComponent(id)}`, {
|
|
method: "PATCH",
|
|
body,
|
|
});
|
|
}
|
|
createAdminField(
|
|
body: Omit<BrandFieldDefinition, "id" | "source">,
|
|
): Promise<BrandFieldDefinition> {
|
|
return this.request("/admin/workspace/fields", { method: "POST", body });
|
|
}
|
|
deleteAdminField(id: string): Promise<{ deleted: boolean }> {
|
|
return this.request(`/admin/workspace/fields/${encodeURIComponent(id)}`, { method: "DELETE" });
|
|
}
|
|
listAdminInquiryForms(): Promise<InquiryForm[]> {
|
|
return this.request("/admin/workspace/forms");
|
|
}
|
|
listAdminInquirySubmissions(): Promise<InquirySubmission[]> {
|
|
return this.request("/admin/workspace/submissions");
|
|
}
|
|
updateAdminInquirySubmission(
|
|
id: string,
|
|
body: { status: InquirySubmission["status"]; adminNote?: string },
|
|
): Promise<InquirySubmission> {
|
|
return this.request(`/admin/workspace/submissions/${encodeURIComponent(id)}`, {
|
|
method: "PATCH",
|
|
body,
|
|
});
|
|
}
|
|
createAdminInquiryForm(body: {
|
|
name: string;
|
|
offeringId?: string;
|
|
fieldsSnapshot: unknown[];
|
|
isEnabled?: boolean;
|
|
}): Promise<InquiryForm> {
|
|
return this.request("/admin/workspace/forms", { method: "POST", body });
|
|
}
|
|
updateAdminInquiryForm(
|
|
id: string,
|
|
body: Partial<{
|
|
name: string;
|
|
offeringId: string;
|
|
fieldsSnapshot: unknown[];
|
|
isEnabled: boolean;
|
|
}>,
|
|
): Promise<InquiryForm> {
|
|
return this.request(`/admin/workspace/forms/${encodeURIComponent(id)}`, {
|
|
method: "PATCH",
|
|
body,
|
|
});
|
|
}
|
|
listAdminOfferings(): Promise<Offering[]> {
|
|
return this.request("/admin/workspace/offerings");
|
|
}
|
|
createAdminOffering(body: Omit<Offering, "id" | "brandId">): Promise<Offering> {
|
|
return this.request("/admin/workspace/offerings", { method: "POST", body });
|
|
}
|
|
updateAdminOffering(
|
|
id: string,
|
|
body: Partial<Omit<Offering, "id" | "brandId">>,
|
|
): Promise<Offering> {
|
|
return this.request(`/admin/workspace/offerings/${encodeURIComponent(id)}`, {
|
|
method: "PATCH",
|
|
body,
|
|
});
|
|
}
|
|
deleteAdminOffering(id: string): Promise<{ deleted: boolean }> {
|
|
return this.request(`/admin/workspace/offerings/${encodeURIComponent(id)}`, {
|
|
method: "DELETE",
|
|
});
|
|
}
|
|
listAdminTrustContent(): Promise<TrustContent[]> {
|
|
return this.request("/admin/config/trust-content");
|
|
}
|
|
createAdminTrustContent(body: TrustContentInput): Promise<TrustContent> {
|
|
return this.request("/admin/config/trust-content", { method: "POST", body });
|
|
}
|
|
updateAdminTrustContent(id: string, body: Partial<TrustContentInput>): Promise<TrustContent> {
|
|
return this.request(`/admin/config/trust-content/${encodeURIComponent(id)}`, {
|
|
method: "PATCH",
|
|
body,
|
|
});
|
|
}
|
|
deleteAdminTrustContent(id: string): Promise<{ deleted: boolean }> {
|
|
return this.request(`/admin/config/trust-content/${encodeURIComponent(id)}`, {
|
|
method: "DELETE",
|
|
});
|
|
}
|
|
private async request<T>(
|
|
path: string,
|
|
options: { method?: string; body?: unknown } = {},
|
|
): Promise<T> {
|
|
const headers: Record<string, string> = { accept: "application/json" };
|
|
if (options.body !== undefined) headers["content-type"] = "application/json";
|
|
if (this.token) headers.authorization = `Bearer ${this.token}`;
|
|
const response = await this.fetcher(`${this.baseUrl}${path}`, {
|
|
method: options.method ?? "GET",
|
|
headers,
|
|
...(options.body === undefined ? {} : { body: JSON.stringify(options.body) }),
|
|
});
|
|
const body = await response.json();
|
|
if (!response.ok) throw new ApiClientError(response.status, body as ApiErrorBody);
|
|
return body as T;
|
|
}
|
|
}
|
|
|
|
export const apiClientName = "personal-brand-api-client";
|