Skip to content

Commit ce9f627

Browse files
committed
feat: add chart and query service
1 parent 8fefced commit ce9f627

20 files changed

+204
-11
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
node_modules/
22

33
dist/
4+
5+
config.json

index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Datadata SDK Javascript</title>
7+
</head>
8+
<body>
9+
<script type="module" src="./src/main.ts"></script>
10+
</body>
11+
</html>
File renamed without changes.

lib/api-key/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * from "./api-key";
1+
export * from "./api-key.interface";
22
export * from "./api-key.service";
3-
export * from "./api-token-payload";
3+
export * from "./api-token-payload.interface";
44
export * from "./crypto";

lib/chart/chart.interface.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export interface Chart {
2+
id: string;
3+
name: string;
4+
tags: string[];
5+
createdAt: string;
6+
updatedAt: string;
7+
description?: string;
8+
datasetQuery: DatasetQuery;
9+
visualizationSettings: any;
10+
}
11+
12+
export interface DatasetQuery {
13+
type: "native";
14+
native: {
15+
query: string;
16+
};
17+
dataSourceId: string;
18+
}
19+
20+
export type GetChartsParams = {
21+
tag?: Array<string>;
22+
sort?: "name:asc" | "name:desc" | "create_at:asc" | "create_at:desc" | "updated_at:asc" | "updated_at:desc";
23+
limit?: number;
24+
offset?: number;
25+
keyword?: string;
26+
archived?: boolean;
27+
};
28+
29+
export type CreateChartBody = Pick<Chart, "name" | "description" | "datasetQuery" | "visualizationSettings">;
30+
31+
export type UpdateChartBody = Pick<Chart, "name" | "description" | "datasetQuery" | "visualizationSettings">;

lib/chart/chart.service.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { BaseService, type PaginatedData } from "../common";
2+
import type { Chart, CreateChartBody, GetChartsParams, UpdateChartBody } from "./chart.interface";
3+
4+
/**
5+
* Chart 相关 Rest API 接口
6+
*/
7+
export class ChartService extends BaseService {
8+
/**
9+
* 分页获取当前用户的 Chart 资源
10+
*/
11+
public getCharts(params?: GetChartsParams, signal?: AbortSignal) {
12+
return this.http.get<PaginatedData<Chart>>(`/api/v1/charts`, { params, signal });
13+
}
14+
15+
/**
16+
* 创建 Chart 资源
17+
*/
18+
public createChart(body: CreateChartBody) {
19+
return this.http.post<Chart>(`/api/v1/charts`, body);
20+
}
21+
22+
/**
23+
* 获取指定 Chart 资源
24+
*/
25+
public getChart(id: string, signal?: AbortSignal) {
26+
return this.http.get<Chart>(`/api/v1/charts/${id}`, { signal });
27+
}
28+
29+
/**
30+
* 更新指定 Chart 资源
31+
*/
32+
public updateChart(id: string, body: UpdateChartBody) {
33+
return this.http.put<Chart>(`/api/v1/charts/${id}`, body);
34+
}
35+
36+
/**
37+
* 归档指定 Chart 资源
38+
*/
39+
public deleteChart(id: string) {
40+
return this.http.delete<void>(`/api/v1/charts/${id}`);
41+
}
42+
43+
/**
44+
* 分页获取子用户的 Chart 资源,通过 API-Token 中的 UID 区分子用户
45+
*/
46+
public getSubUserCharts(params?: GetChartsParams, signal?: AbortSignal) {
47+
return this.http.get<PaginatedData<Chart>>(`/api/v1/charts/by-uid`, { params, signal });
48+
}
49+
50+
/**
51+
* 分页获取指定子用户的 Chart 资源
52+
*/
53+
public getChartsByUID(uid: string, params?: GetChartsParams, signal?: AbortSignal) {
54+
return this.http.get<PaginatedData<Chart>>(`/api/v1/charts/uid/${uid}`, { params, signal });
55+
}
56+
}

lib/chart/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./chart.interface";
2+
export * from "./chart.service";

lib/common/base-service.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import axios, { type AxiosInstance } from "axios";
22

3+
export type BaseServiceOptions = {
4+
token: string;
5+
baseURL: string;
6+
};
7+
38
export class BaseService {
49
public http: AxiosInstance;
510

6-
constructor() {
7-
this.http = axios.create();
11+
constructor(public readonly options: BaseServiceOptions) {
12+
this.http = axios.create({
13+
baseURL: options.baseURL,
14+
headers: {
15+
"x-datadata-api-token": options.token,
16+
},
17+
});
818
}
919
}

lib/common/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./base-service";
2+
export * from "./paginated-data.interface";

0 commit comments

Comments
 (0)