feat: 接入小程序 API 客户端
This commit is contained in:
1 parent
a6d30d1222
commit
868a4251ce
2 files changed
+16
No files matched your search
@@ -2,6 +2,7 @@
|
|||||||
"name": "@personal-brand/miniapp",
|
"name": "@personal-brand/miniapp",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
|
"dependencies": { "@personal-brand/api-client": "workspace:*" },
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"lint": "eslint . --ext .ts",
|
"lint": "eslint . --ext .ts",
|
||||||
"typecheck": "tsc -p tsconfig.json --noEmit",
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
||||||
|
|||||||
@@ -1 +1,16 @@
|
|||||||
|
import { ApiClient, type CreateBookingInput, type FetchLike } from "@personal-brand/api-client";
|
||||||
|
|
||||||
|
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 >= 200 && response.statusCode < 300, 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";
|
export const miniappName = "personal-brand-miniapp";
|
||||||
Reference in new issue
Block a user