refactor: clean code

This commit is contained in:
Fu Diwei
2026-05-22 22:42:11 +08:00
committed by RHQYZ
parent e4a4a1a9b5
commit d9b303457c
22 changed files with 191 additions and 171 deletions
+39
View File
@@ -0,0 +1,39 @@
import { ClientResponseError, type SendOptions } from "pocketbase";
import { getPocketBase } from "@/repository/_pocketbase";
const pb = getPocketBase();
type Options = Pick<SendOptions, "body" | "headers"> & {
url: string;
};
export async function get<T = any>({ url, ...opts }: Options) {
const resp = await pb.send<BaseResponse<T>>(url, {
method: "GET",
...opts,
});
if (resp.code != 0) {
throw new ClientResponseError({ status: resp.code, response: resp, data: {} });
}
return resp;
}
export async function post<T = any>({ url, headers, ...opts }: Options) {
const resp = await pb.send<BaseResponse<T>>(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
...headers,
},
...opts,
});
if (resp.code != 0) {
throw new ClientResponseError({ status: resp.code, response: resp, data: {} });
}
return resp;
}
+10 -32
View File
@@ -1,44 +1,22 @@
import { ClientResponseError } from "pocketbase";
import { type CertificateFormatType } from "@/domain/certificate";
import { getPocketBase } from "@/repository/_pocketbase";
export const download = async (certificateId: string, format?: CertificateFormatType) => {
const pb = getPocketBase();
import { post as httpPost } from "./_api";
export const download = (certificateId: string, format?: CertificateFormatType) => {
type RespData = {
fileBytes: string;
zipBytes: string;
};
const resp = await pb.send<BaseResponse<RespData>>(`/api/certificates/${encodeURIComponent(certificateId)}/download`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
return httpPost<RespData>({
url: `/api/certificates/${encodeURIComponent(certificateId)}/download`,
body: {
format: format,
fileFormat: format,
},
});
if (resp.code != 0) {
throw new ClientResponseError({ status: resp.code, response: resp, data: {} });
}
return resp;
};
export const revoke = async (certificateId: string) => {
const pb = getPocketBase();
const resp = await pb.send<BaseResponse>(`/api/certificates/${encodeURIComponent(certificateId)}/revoke`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
export const revoke = (certificateId: string) => {
return httpPost({
url: `/api/certificates/${encodeURIComponent(certificateId)}/revoke`,
});
if (resp.code != 0) {
throw new ClientResponseError({ status: resp.code, response: resp, data: {} });
}
return resp;
};
+3 -16
View File
@@ -1,24 +1,11 @@
import { ClientResponseError } from "pocketbase";
import { getPocketBase } from "@/repository/_pocketbase";
import { post as httpPost } from "./_api";
export const testPushNotification = async ({ provider, accessId }: { provider: string; accessId: string }) => {
const pb = getPocketBase();
const resp = await pb.send<BaseResponse>("/api/notifications/test", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
return httpPost({
url: `/api/notifications/test`,
body: {
provider,
accessId,
},
});
if (resp.code != 0) {
throw new ClientResponseError({ status: resp.code, response: resp, data: {} });
}
return resp;
};
+5 -12
View File
@@ -1,18 +1,11 @@
import { ClientResponseError } from "pocketbase";
import { type Statistics } from "@/domain/statistics";
import { getPocketBase } from "@/repository/_pocketbase";
import { get as httpGet } from "./_api";
export const get = async () => {
const pb = getPocketBase();
type RespData = Statistics;
const resp = await pb.send<BaseResponse<Statistics>>("/api/statistics", {
method: "GET",
return httpGet<RespData>({
url: `/api/statistics`,
});
if (resp.code != 0) {
throw new ClientResponseError({ status: resp.code, response: resp, data: {} });
}
return resp;
};
+11 -44
View File
@@ -1,63 +1,30 @@
import { ClientResponseError } from "pocketbase";
import { WORKFLOW_TRIGGERS } from "@/domain/workflow";
import { getPocketBase } from "@/repository/_pocketbase";
export const getStats = async () => {
const pb = getPocketBase();
import { get as httpGet, post as httpPost } from "./_api";
export const getStats = () => {
type RespData = {
concurrency: number;
pendingRunIds: string[];
processingRunIds: string[];
};
const resp = await pb.send<BaseResponse<RespData>>(`/api/workflows/stats`, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
return httpGet<RespData>({
url: `/api/workflows/stats`,
});
if (resp.code != 0) {
throw new ClientResponseError({ status: resp.code, response: resp, data: {} });
}
return resp;
};
export const startRun = async (workflowId: string) => {
const pb = getPocketBase();
const resp = await pb.send<BaseResponse>(`/api/workflows/${encodeURIComponent(workflowId)}/runs`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
export const startRun = (workflowId: string) => {
return httpPost({
url: `/api/workflows/${encodeURIComponent(workflowId)}/runs`,
body: {
trigger: WORKFLOW_TRIGGERS.MANUAL,
},
});
if (resp.code != 0) {
throw new ClientResponseError({ status: resp.code, response: resp, data: {} });
}
return resp;
};
export const cancelRun = async (workflowId: string, runId: string) => {
const pb = getPocketBase();
const resp = await pb.send<BaseResponse>(`/api/workflows/${encodeURIComponent(workflowId)}/runs/${encodeURIComponent(runId)}/cancel`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
export const cancelRun = (workflowId: string, runId: string) => {
return httpPost({
url: `/api/workflows/${encodeURIComponent(workflowId)}/runs/${encodeURIComponent(runId)}/cancel`,
});
if (resp.code != 0) {
throw new ClientResponseError({ status: resp.code, response: resp, data: {} });
}
return resp;
};
@@ -22,7 +22,7 @@ const CertificateDetail = ({ data, ...props }: CertificateDetailProps) => {
const handleDownloadClick = async (format: CertificateFormatType) => {
try {
const res = await downloadCertificate(data.id, format);
const bstr = atob(res.data.fileBytes);
const bstr = atob(res.data.zipBytes);
const u8arr = Uint8Array.from(bstr, (ch) => ch.charCodeAt(0));
const blob = new Blob([u8arr], { type: "application/zip" });
saveAs(blob, `${data.id}-${data.subjectAltNames}.zip`);