feat: add shared infiplot workspace packages
Signed-off-by: baizhi958216 <1475289190@qq.com>
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "@infiplot/api-client",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./src/index.ts",
|
||||
"default": "./src/index.ts"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"@infiplot/types": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^6.0.3"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import type { SceneRequest, SceneResponse } from "@infiplot/types";
|
||||
|
||||
export interface InfiplotApiClientOptions {
|
||||
baseUrl: string;
|
||||
fetcher?: typeof fetch;
|
||||
}
|
||||
|
||||
export class InfiplotApiClient {
|
||||
private readonly baseUrl: string;
|
||||
private readonly fetcher: typeof fetch;
|
||||
|
||||
constructor(options: InfiplotApiClientOptions) {
|
||||
this.baseUrl = options.baseUrl.replace(/\/$/, "");
|
||||
this.fetcher = options.fetcher ?? fetch;
|
||||
}
|
||||
|
||||
async createScene(request: SceneRequest): Promise<SceneResponse> {
|
||||
return this.post<SceneResponse>("/api/scene", request);
|
||||
}
|
||||
|
||||
private async post<TResponse>(path: string, body: unknown): Promise<TResponse> {
|
||||
const response = await this.fetcher(`${this.baseUrl}${path}`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Infiplot API request failed: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json() as Promise<TResponse>;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"lib": ["ES2022", "DOM"],
|
||||
"noEmit": true
|
||||
},
|
||||
"include": ["src/**/*.ts"]
|
||||
}
|
||||
Reference in New Issue
Block a user