38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { ApiClient, type CreateBookingInput, type FetchLike } from "@personal-brand/api-client";
|
|
import { HTTP_STATUS } from "@personal-brand/types";
|
|
|
|
export interface MiniappRequestAdapter {
|
|
request(options: {
|
|
url: string;
|
|
method: string;
|
|
header: Record<string, string>;
|
|
data?: string;
|
|
}): Promise<{ statusCode: number; data: unknown }>;
|
|
}
|
|
|
|
export function createMiniappClient(
|
|
baseUrl: string,
|
|
adapter: MiniappRequestAdapter,
|
|
token?: string,
|
|
): ApiClient {
|
|
const fetcher: FetchLike = async (url, init) => {
|
|
const response = await adapter.request({
|
|
url,
|
|
method: init?.method ?? "GET",
|
|
header: init?.headers ?? {},
|
|
...(init?.body ? { data: init.body } : {}),
|
|
});
|
|
return {
|
|
status: response.statusCode,
|
|
ok: response.statusCode >= HTTP_STATUS.OK && response.statusCode < HTTP_STATUS.BAD_REQUEST,
|
|
json: () => Promise.resolve(response.data),
|
|
};
|
|
};
|
|
return new ApiClient({ baseUrl, fetcher, ...(token ? { token } : {}) });
|
|
}
|
|
|
|
export async function submitBooking(client: ApiClient, input: CreateBookingInput): Promise<void> {
|
|
await client.createCustomerBooking(input);
|
|
}
|
|
export const miniappName = "personal-brand-miniapp";
|