Skip to content

Commit

Permalink
feat: add chart and query service
Browse files Browse the repository at this point in the history
  • Loading branch information
hungtcs committed Mar 14, 2024
1 parent 8fefced commit ce9f627
Show file tree
Hide file tree
Showing 20 changed files with 204 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules/

dist/

config.json
11 changes: 11 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Datadata SDK Javascript</title>
</head>
<body>
<script type="module" src="./src/main.ts"></script>
</body>
</html>
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions lib/api-key/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./api-key";
export * from "./api-key.interface";
export * from "./api-key.service";
export * from "./api-token-payload";
export * from "./api-token-payload.interface";
export * from "./crypto";
31 changes: 31 additions & 0 deletions lib/chart/chart.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export interface Chart {
id: string;
name: string;
tags: string[];
createdAt: string;
updatedAt: string;
description?: string;
datasetQuery: DatasetQuery;
visualizationSettings: any;
}

export interface DatasetQuery {
type: "native";
native: {
query: string;
};
dataSourceId: string;
}

export type GetChartsParams = {
tag?: Array<string>;
sort?: "name:asc" | "name:desc" | "create_at:asc" | "create_at:desc" | "updated_at:asc" | "updated_at:desc";
limit?: number;
offset?: number;
keyword?: string;
archived?: boolean;
};

export type CreateChartBody = Pick<Chart, "name" | "description" | "datasetQuery" | "visualizationSettings">;

export type UpdateChartBody = Pick<Chart, "name" | "description" | "datasetQuery" | "visualizationSettings">;
56 changes: 56 additions & 0 deletions lib/chart/chart.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { BaseService, type PaginatedData } from "../common";
import type { Chart, CreateChartBody, GetChartsParams, UpdateChartBody } from "./chart.interface";

/**
* Chart 相关 Rest API 接口
*/
export class ChartService extends BaseService {
/**
* 分页获取当前用户的 Chart 资源
*/
public getCharts(params?: GetChartsParams, signal?: AbortSignal) {
return this.http.get<PaginatedData<Chart>>(`/api/v1/charts`, { params, signal });
}

/**
* 创建 Chart 资源
*/
public createChart(body: CreateChartBody) {
return this.http.post<Chart>(`/api/v1/charts`, body);
}

/**
* 获取指定 Chart 资源
*/
public getChart(id: string, signal?: AbortSignal) {
return this.http.get<Chart>(`/api/v1/charts/${id}`, { signal });
}

/**
* 更新指定 Chart 资源
*/
public updateChart(id: string, body: UpdateChartBody) {
return this.http.put<Chart>(`/api/v1/charts/${id}`, body);
}

/**
* 归档指定 Chart 资源
*/
public deleteChart(id: string) {
return this.http.delete<void>(`/api/v1/charts/${id}`);
}

/**
* 分页获取子用户的 Chart 资源,通过 API-Token 中的 UID 区分子用户
*/
public getSubUserCharts(params?: GetChartsParams, signal?: AbortSignal) {
return this.http.get<PaginatedData<Chart>>(`/api/v1/charts/by-uid`, { params, signal });
}

/**
* 分页获取指定子用户的 Chart 资源
*/
public getChartsByUID(uid: string, params?: GetChartsParams, signal?: AbortSignal) {
return this.http.get<PaginatedData<Chart>>(`/api/v1/charts/uid/${uid}`, { params, signal });
}
}
2 changes: 2 additions & 0 deletions lib/chart/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./chart.interface";
export * from "./chart.service";
14 changes: 12 additions & 2 deletions lib/common/base-service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import axios, { type AxiosInstance } from "axios";

export type BaseServiceOptions = {
token: string;
baseURL: string;
};

export class BaseService {
public http: AxiosInstance;

constructor() {
this.http = axios.create();
constructor(public readonly options: BaseServiceOptions) {
this.http = axios.create({
baseURL: options.baseURL,
headers: {
"x-datadata-api-token": options.token,
},
});
}
}
1 change: 1 addition & 0 deletions lib/common/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./base-service";
export * from "./paginated-data.interface";
4 changes: 4 additions & 0 deletions lib/common/paginated-data.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface PaginatedData<T> {
count: number;
data: T[];
}
4 changes: 4 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export * from "./common";

export * from "./api-key";
export * from "./chart";
export * from "./query";

export * from "./services";
1 change: 1 addition & 0 deletions lib/query/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./query.service";
3 changes: 3 additions & 0 deletions lib/query/query.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { BaseService } from "../common";

export class QueryService extends BaseService {}
14 changes: 9 additions & 5 deletions lib/services.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { APIKeyService } from "./api-key";
import { ChartService } from "./chart";
import type { BaseServiceOptions } from "./common";
import { QueryService } from "./query";

export type CreateServicesOptions = {
token: string;
};
export const BASE_URL_CN = "https://www.datadata.cn";
export const BASE_URL_GLOBAL = "https://www.datadata.com";

export function createServices(options: CreateServicesOptions) {
export function createServices(options: BaseServiceOptions) {
return {
apiKey: new APIKeyService(),
apiKey: new APIKeyService(options),
chart: new ChartService(options),
query: new QueryService(options),
};
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"vite-plugin-checker": "^0.6.4",
"vite-plugin-dts": "^3.7.3",
"vite-plugin-externalize-deps": "^0.8.0",
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^1.3.1"
},
"dependencies": {
Expand Down
37 changes: 37 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { BASE_URL_CN, createServices } from "@lib";
import Config from "../config.json";

async function main() {
const services = createServices({
token: Config.apiToken,
baseURL: BASE_URL_CN,
});

console.log(services);
}

main().catch((err) => console.error(err));
9 changes: 8 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@
"declaration": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"removeComments": false,
"resolveJsonModule": true,
"strict": true,
"skipLibCheck": true,
"verbatimModuleSyntax": true
"verbatimModuleSyntax": true,
"paths": {
"@lib": [
"./lib/index.ts"
]
}
},
"references": [
{
Expand Down
8 changes: 7 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { defineConfig } from "vite";
import checker from "vite-plugin-checker";
import dts from "vite-plugin-dts";
import { externalizeDeps } from "vite-plugin-externalize-deps";
import tsconfigPaths from "vite-tsconfig-paths";

export default defineConfig({
test: {
Expand All @@ -22,5 +23,10 @@ export default defineConfig({
// external: ["axios", "crypto-js"],
// },
},
plugins: [dts({ tsconfigPath: "./tsconfig.lib.json" }), checker({ typescript: true }), externalizeDeps()],
plugins: [
dts({ tsconfigPath: "./tsconfig.lib.json" }),
checker({ typescript: true }),
tsconfigPaths(),
externalizeDeps(),
],
});

0 comments on commit ce9f627

Please sign in to comment.