mirror of
https://github.com/certimate-go/certimate.git
synced 2026-10-30 18:29:52 +08:00
refactor: clean code
This commit is contained in:
@@ -78,13 +78,13 @@ func (d *Deployer) SetLogger(logger *slog.Logger) {
|
||||
|
||||
func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*deployer.DeployResult, error) {
|
||||
switch d.config.ServiceVersion {
|
||||
case "3", "3.0":
|
||||
if err := d.deployToFC3(ctx, certPEM, privkeyPEM); err != nil {
|
||||
case "2", "2.0":
|
||||
if err := d.deployToFC2(ctx, certPEM, privkeyPEM); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
case "2", "2.0":
|
||||
if err := d.deployToFC2(ctx, certPEM, privkeyPEM); err != nil {
|
||||
case "3", "3.0":
|
||||
if err := d.deployToFC3(ctx, certPEM, privkeyPEM); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ func (d *Deployer) deployToWebsite(ctx context.Context, certPEM, privkeyPEM stri
|
||||
|
||||
// 上传网站证书
|
||||
// REF: https://open-api.netlify.com/#tag/sniCertificate/operation/provisionSiteTLSCertificate
|
||||
provisionSiteTLSCertificateReq := &netlifysdk.ProvisionSiteTLSCertificateParams{
|
||||
provisionSiteTLSCertificateReq := &netlifysdk.ProvisionSiteTLSCertificateRequest{
|
||||
Certificate: serverCertPEM,
|
||||
CACertificates: intermediaCertPEM,
|
||||
Key: privkeyPEM,
|
||||
|
||||
@@ -59,7 +59,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*dep
|
||||
|
||||
// 上传证书
|
||||
// REF: https://vercel.com/docs/rest-api/certs/upload-a-cert
|
||||
uploadCertReq := &vercelsdk.UploadCertParams{
|
||||
uploadCertReq := &vercelsdk.UploadCertRequest{
|
||||
CA: intermediaCertPEM,
|
||||
Cert: serverCertPEM,
|
||||
Key: privkeyPEM,
|
||||
|
||||
+14
-11
@@ -2,6 +2,7 @@ package logging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"sync"
|
||||
)
|
||||
@@ -102,12 +103,22 @@ func (h *HookHandler) Handle(ctx context.Context, r slog.Record) error {
|
||||
return h.parent.Handle(ctx, r)
|
||||
}
|
||||
|
||||
errs := make([]error, 0)
|
||||
|
||||
if h.base != nil {
|
||||
h.base.Handle(ctx, r)
|
||||
if err := h.base.Handle(ctx, r); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := h.writeRecord(ctx, Record{Record: r}); err != nil {
|
||||
return err
|
||||
if h.options.WriteFunc != nil {
|
||||
if err := h.options.WriteFunc(ctx, Record{Record: r}); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(errs) > 0 {
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -118,11 +129,3 @@ func (h *HookHandler) SetLevel(level slog.Level) {
|
||||
h.options.Level = level
|
||||
h.mutex.Unlock()
|
||||
}
|
||||
|
||||
func (h *HookHandler) writeRecord(ctx context.Context, r Record) error {
|
||||
if h.parent != nil {
|
||||
return h.parent.writeRecord(ctx, r)
|
||||
}
|
||||
|
||||
return h.options.WriteFunc(ctx, r)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package btpanel
|
||||
package btpanelgo
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package btpanel
|
||||
package btpanelgo
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package btpanel
|
||||
package btpanelgo
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package btpanel
|
||||
package btpanelgo
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package btpanel
|
||||
package btpanelgo
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package btpanel
|
||||
package btpanelgo
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package btpanel
|
||||
package btpanelgo
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package btpanel
|
||||
package btpanelgo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package btpanel
|
||||
package btpanelgo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package baishan
|
||||
package cpanel
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package baishan
|
||||
package cpanel
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package baishan
|
||||
package cpanel
|
||||
|
||||
type sdkResponse interface {
|
||||
GetStatus() int
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
package common
|
||||
package gcore
|
||||
|
||||
const BASE_URL = "https://api.gcore.com"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package common
|
||||
package gcore
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type ProvisionSiteTLSCertificateParams struct {
|
||||
type ProvisionSiteTLSCertificateRequest struct {
|
||||
Certificate string `json:"certificate"`
|
||||
CACertificates string `json:"ca_certificates"`
|
||||
Key string `json:"key"`
|
||||
@@ -22,11 +22,11 @@ type ProvisionSiteTLSCertificateResponse struct {
|
||||
UpdatedAt string `json:"updated_at,omitempty"`
|
||||
}
|
||||
|
||||
func (c *Client) ProvisionSiteTLSCertificate(siteId string, req *ProvisionSiteTLSCertificateParams) (*ProvisionSiteTLSCertificateResponse, error) {
|
||||
func (c *Client) ProvisionSiteTLSCertificate(siteId string, req *ProvisionSiteTLSCertificateRequest) (*ProvisionSiteTLSCertificateResponse, error) {
|
||||
return c.ProvisionSiteTLSCertificateWithContext(context.Background(), siteId, req)
|
||||
}
|
||||
|
||||
func (c *Client) ProvisionSiteTLSCertificateWithContext(ctx context.Context, siteId string, req *ProvisionSiteTLSCertificateParams) (*ProvisionSiteTLSCertificateResponse, error) {
|
||||
func (c *Client) ProvisionSiteTLSCertificateWithContext(ctx context.Context, siteId string, req *ProvisionSiteTLSCertificateRequest) (*ProvisionSiteTLSCertificateResponse, error) {
|
||||
if siteId == "" {
|
||||
return nil, fmt.Errorf("sdkerr: unset siteId")
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type UploadCertParams struct {
|
||||
type UploadCertRequest struct {
|
||||
CA string `json:"ca"`
|
||||
Cert string `json:"cert"`
|
||||
Key string `json:"key"`
|
||||
@@ -19,11 +19,11 @@ type UploadCertResponse struct {
|
||||
CreatedAt string `json:"createdAt,omitempty"`
|
||||
}
|
||||
|
||||
func (c *Client) UploadCert(req *UploadCertParams) (*UploadCertResponse, error) {
|
||||
func (c *Client) UploadCert(req *UploadCertRequest) (*UploadCertResponse, error) {
|
||||
return c.UploadCertWithContext(context.Background(), req)
|
||||
}
|
||||
|
||||
func (c *Client) UploadCertWithContext(ctx context.Context, req *UploadCertParams) (*UploadCertResponse, error) {
|
||||
func (c *Client) UploadCertWithContext(ctx context.Context, req *UploadCertRequest) (*UploadCertResponse, error) {
|
||||
httpreq, err := c.newRequest(http.MethodPut, "/certs")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
+14
-15
@@ -3,18 +3,19 @@ import dayjs from "dayjs";
|
||||
import { type AccessModel } from "@/domain/access";
|
||||
import { COLLECTION_NAME_ACCESS, getPocketBase } from "./_pocketbase";
|
||||
|
||||
const pb = getPocketBase();
|
||||
const pbco = pb.collection(COLLECTION_NAME_ACCESS);
|
||||
|
||||
const _commonFields = ["id", "name", "provider", "reserve", "created", "updated", "deleted"];
|
||||
|
||||
export const list = async () => {
|
||||
const list = await getPocketBase()
|
||||
.collection(COLLECTION_NAME_ACCESS)
|
||||
.getFullList<AccessModel>({
|
||||
batch: 65535,
|
||||
fields: [..._commonFields].join(","),
|
||||
filter: "deleted=null",
|
||||
sort: "-created",
|
||||
requestKey: null,
|
||||
});
|
||||
const list = await pbco.getFullList<AccessModel>({
|
||||
batch: 65535,
|
||||
fields: [..._commonFields].join(","),
|
||||
filter: "deleted=null",
|
||||
sort: "-created",
|
||||
requestKey: null,
|
||||
});
|
||||
return {
|
||||
totalItems: list.length,
|
||||
items: list,
|
||||
@@ -22,22 +23,20 @@ export const list = async () => {
|
||||
};
|
||||
|
||||
export const get = async (id: string) => {
|
||||
return await getPocketBase().collection(COLLECTION_NAME_ACCESS).getOne<AccessModel>(id, {
|
||||
return await pbco.getOne<AccessModel>(id, {
|
||||
requestKey: null,
|
||||
});
|
||||
};
|
||||
|
||||
export const save = async (record: MaybeModelRecord<AccessModel>) => {
|
||||
if (record.id) {
|
||||
return await getPocketBase().collection(COLLECTION_NAME_ACCESS).update<AccessModel>(record.id, record);
|
||||
return await pbco.update<AccessModel>(record.id, record);
|
||||
}
|
||||
|
||||
return await getPocketBase().collection(COLLECTION_NAME_ACCESS).create<AccessModel>(record);
|
||||
return await pbco.create<AccessModel>(record);
|
||||
};
|
||||
|
||||
export const remove = async (record: MaybeModelRecordWithId<AccessModel> | MaybeModelRecordWithId<AccessModel>[]) => {
|
||||
const pb = getPocketBase();
|
||||
|
||||
const deletedAt = dayjs.utc().format("YYYY-MM-DD HH:mm:ss");
|
||||
|
||||
if (Array.isArray(record)) {
|
||||
@@ -48,7 +47,7 @@ export const remove = async (record: MaybeModelRecordWithId<AccessModel> | Maybe
|
||||
const res = await batch.send();
|
||||
return res.every((e) => e.status >= 200 && e.status < 400);
|
||||
} else {
|
||||
await pb.collection(COLLECTION_NAME_ACCESS).update<AccessModel>(record.id!, { deleted: deletedAt });
|
||||
await pbco.update<AccessModel>(record.id!, { deleted: deletedAt });
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
import { COLLECTION_NAME_ADMIN, getPocketBase } from "./_pocketbase";
|
||||
|
||||
const pb = getPocketBase();
|
||||
const pbco = pb.collection(COLLECTION_NAME_ADMIN);
|
||||
|
||||
export const authWithPassword = (username: string, password: string) => {
|
||||
return getPocketBase().collection(COLLECTION_NAME_ADMIN).authWithPassword(username, password);
|
||||
return pbco.authWithPassword(username, password);
|
||||
};
|
||||
|
||||
export const getAuthStore = () => {
|
||||
return getPocketBase().authStore;
|
||||
return pb.authStore;
|
||||
};
|
||||
|
||||
export const save = (data: { email: string } | { password: string; passwordConfirm: string }) => {
|
||||
return getPocketBase()
|
||||
.collection(COLLECTION_NAME_ADMIN)
|
||||
.update(getAuthStore().record?.id || "", data);
|
||||
return pbco.update(getAuthStore().record?.id || "", data);
|
||||
};
|
||||
|
||||
@@ -3,6 +3,9 @@ import dayjs from "dayjs";
|
||||
import { type CertificateModel } from "@/domain/certificate";
|
||||
import { COLLECTION_NAME_CERTIFICATE, getPocketBase } from "./_pocketbase";
|
||||
|
||||
const pb = getPocketBase();
|
||||
const pbco = pb.collection(COLLECTION_NAME_CERTIFICATE);
|
||||
|
||||
const _commonFields = [
|
||||
"id",
|
||||
"source",
|
||||
@@ -37,8 +40,6 @@ export const list = async ({
|
||||
page?: number;
|
||||
perPage?: number;
|
||||
}) => {
|
||||
const pb = getPocketBase();
|
||||
|
||||
const filters: string[] = ["deleted=null"];
|
||||
if (keyword) {
|
||||
filters.push(pb.filter("(id={:keyword} || serialNumber={:keyword} || subjectAltNames~{:keyword})", { keyword: keyword }));
|
||||
@@ -51,7 +52,7 @@ export const list = async ({
|
||||
filters.push(pb.filter("validityNotAfter<=@now"));
|
||||
}
|
||||
|
||||
return pb.collection(COLLECTION_NAME_CERTIFICATE).getList<CertificateModel>(page, perPage, {
|
||||
return pbco.getList<CertificateModel>(page, perPage, {
|
||||
expand: ["workflowRef"].join(","),
|
||||
fields: [..._commonFields, ..._expandFields].join(","),
|
||||
filter: filters.join(" && "),
|
||||
@@ -61,16 +62,13 @@ export const list = async ({
|
||||
};
|
||||
|
||||
export const listByWorkflowRunId = async (workflowRunId: string) => {
|
||||
const pb = getPocketBase();
|
||||
|
||||
const list = await pb.collection(COLLECTION_NAME_CERTIFICATE).getFullList<CertificateModel>({
|
||||
const list = await pbco.getFullList<CertificateModel>({
|
||||
batch: 65535,
|
||||
fields: [..._commonFields, ..._expandFields, "certificate", "privateKey"].join(","),
|
||||
filter: pb.filter("workflowRunRef={:workflowRunId}", { workflowRunId }),
|
||||
sort: "created",
|
||||
requestKey: null,
|
||||
});
|
||||
|
||||
return {
|
||||
totalItems: list.length,
|
||||
items: list,
|
||||
@@ -78,18 +76,14 @@ export const listByWorkflowRunId = async (workflowRunId: string) => {
|
||||
};
|
||||
|
||||
export const get = async (id: string) => {
|
||||
return await getPocketBase()
|
||||
.collection(COLLECTION_NAME_CERTIFICATE)
|
||||
.getOne<CertificateModel>(id, {
|
||||
expand: ["workflowRef"].join(","),
|
||||
fields: ["*", ..._expandFields].join(","),
|
||||
requestKey: null,
|
||||
});
|
||||
return pbco.getOne<CertificateModel>(id, {
|
||||
expand: ["workflowRef"].join(","),
|
||||
fields: ["*", ..._expandFields].join(","),
|
||||
requestKey: null,
|
||||
});
|
||||
};
|
||||
|
||||
export const remove = async (record: MaybeModelRecordWithId<CertificateModel> | MaybeModelRecordWithId<CertificateModel>[]) => {
|
||||
const pb = getPocketBase();
|
||||
|
||||
const deletedAt = dayjs.utc().format("YYYY-MM-DD HH:mm:ss");
|
||||
|
||||
if (Array.isArray(record)) {
|
||||
@@ -100,7 +94,7 @@ export const remove = async (record: MaybeModelRecordWithId<CertificateModel> |
|
||||
const res = await batch.send();
|
||||
return res.every((e) => e.status >= 200 && e.status < 400);
|
||||
} else {
|
||||
await pb.collection(COLLECTION_NAME_CERTIFICATE).update<CertificateModel>(record.id!, { deleted: deletedAt });
|
||||
await pbco.update<CertificateModel>(record.id!, { deleted: deletedAt });
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -14,6 +14,9 @@ import {
|
||||
|
||||
import { COLLECTION_NAME_SETTINGS, getPocketBase } from "./_pocketbase";
|
||||
|
||||
const pb = getPocketBase();
|
||||
const pbco = pb.collection(COLLECTION_NAME_SETTINGS);
|
||||
|
||||
interface SettingsContentMap {
|
||||
[SETTINGS_NAMES.EMAILS]: EmailsSettingsContent;
|
||||
[SETTINGS_NAMES.NOTIFY_TEMPLATE]: NotifyTemplateContent;
|
||||
@@ -27,7 +30,7 @@ export const get = async <K extends SettingsNames | string, T extends NonNullabl
|
||||
): Promise<K extends keyof SettingsContentMap ? SettingsModel<SettingsContentMap[K]> : SettingsModel<T>> => {
|
||||
let resp: K extends keyof SettingsContentMap ? SettingsModel<SettingsContentMap[K]> : SettingsModel<T>;
|
||||
try {
|
||||
resp = await getPocketBase().collection(COLLECTION_NAME_SETTINGS).getFirstListItem<typeof resp>(`name='${name}'`, {
|
||||
resp = await pbco.getFirstListItem<typeof resp>(`name='${name}'`, {
|
||||
requestKey: null,
|
||||
});
|
||||
return resp;
|
||||
@@ -87,8 +90,8 @@ export const get = async <K extends SettingsNames | string, T extends NonNullabl
|
||||
|
||||
export const save = async <T extends NonNullable<unknown>>(record: MaybeModelRecordWithId<SettingsModel<T>>) => {
|
||||
if (record.id) {
|
||||
return await getPocketBase().collection(COLLECTION_NAME_SETTINGS).update<SettingsModel<T>>(record.id, record);
|
||||
return await pbco.update<SettingsModel<T>>(record.id, record);
|
||||
}
|
||||
|
||||
return await getPocketBase().collection(COLLECTION_NAME_SETTINGS).create<SettingsModel<T>>(record);
|
||||
return await pbco.create<SettingsModel<T>>(record);
|
||||
};
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { getPocketBase } from "./_pocketbase";
|
||||
|
||||
const pb = getPocketBase();
|
||||
|
||||
export const listCronJobs = () => {
|
||||
return getPocketBase()
|
||||
.crons.getFullList({
|
||||
return pb.crons
|
||||
.getFullList({
|
||||
requestKey: null,
|
||||
})
|
||||
.then((res) => {
|
||||
@@ -29,8 +31,8 @@ export const listLogs = (request: ListLogsRequest) => {
|
||||
const page = request.page || 1;
|
||||
const perPage = request.perPage || 10;
|
||||
|
||||
return getPocketBase()
|
||||
.logs.getList(page, perPage, {
|
||||
return pb.logs
|
||||
.getList(page, perPage, {
|
||||
filter: 'data.type!="request"',
|
||||
sort: "-@rowid",
|
||||
skipTotal: true,
|
||||
|
||||
@@ -3,6 +3,9 @@ import { type RecordSubscription } from "pocketbase";
|
||||
import { type WorkflowModel } from "@/domain/workflow";
|
||||
import { COLLECTION_NAME_WORKFLOW, getPocketBase } from "./_pocketbase";
|
||||
|
||||
const pb = getPocketBase();
|
||||
const pbco = pb.collection(COLLECTION_NAME_WORKFLOW);
|
||||
|
||||
const _commonFields = [
|
||||
"id",
|
||||
"name",
|
||||
@@ -43,8 +46,6 @@ export const list = async ({
|
||||
perPage?: number;
|
||||
expand?: boolean;
|
||||
}) => {
|
||||
const pb = getPocketBase();
|
||||
|
||||
const filters: string[] = [];
|
||||
if (keyword) {
|
||||
filters.push(pb.filter("(id={:keyword} || name~{:keyword})", { keyword: keyword }));
|
||||
@@ -53,7 +54,7 @@ export const list = async ({
|
||||
filters.push(pb.filter("enabled={:enabled}", { enabled: enabled }));
|
||||
}
|
||||
|
||||
return await pb.collection(COLLECTION_NAME_WORKFLOW).getList<WorkflowModel>(page, perPage, {
|
||||
return await pbco.getList<WorkflowModel>(page, perPage, {
|
||||
expand: expand ? ["lastRunRef"].join(",") : void 0,
|
||||
fields: [..._commonFields, ..._expandFields].join(","),
|
||||
filter: filters.join(" && "),
|
||||
@@ -63,28 +64,22 @@ export const list = async ({
|
||||
};
|
||||
|
||||
export const get = async (id: string) => {
|
||||
return await getPocketBase()
|
||||
.collection(COLLECTION_NAME_WORKFLOW)
|
||||
.getOne<WorkflowModel>(id, {
|
||||
expand: ["lastRunRef"].join(","),
|
||||
fields: ["*", ..._expandFields].join(","),
|
||||
requestKey: null,
|
||||
});
|
||||
return await pbco.getOne<WorkflowModel>(id, {
|
||||
expand: ["lastRunRef"].join(","),
|
||||
fields: ["*", ..._expandFields].join(","),
|
||||
requestKey: null,
|
||||
});
|
||||
};
|
||||
|
||||
export const save = async (record: MaybeModelRecord<WorkflowModel>) => {
|
||||
if (record.id) {
|
||||
return await getPocketBase()
|
||||
.collection(COLLECTION_NAME_WORKFLOW)
|
||||
.update<WorkflowModel>(record.id as string, record);
|
||||
return await pbco.update<WorkflowModel>(record.id as string, record);
|
||||
}
|
||||
|
||||
return await getPocketBase().collection(COLLECTION_NAME_WORKFLOW).create<WorkflowModel>(record);
|
||||
return await pbco.create<WorkflowModel>(record);
|
||||
};
|
||||
|
||||
export const remove = async (record: MaybeModelRecordWithId<WorkflowModel> | MaybeModelRecordWithId<WorkflowModel>[]) => {
|
||||
const pb = getPocketBase();
|
||||
|
||||
if (Array.isArray(record)) {
|
||||
const batch = pb.createBatch();
|
||||
for (const item of record) {
|
||||
@@ -93,14 +88,14 @@ export const remove = async (record: MaybeModelRecordWithId<WorkflowModel> | May
|
||||
const res = await batch.send();
|
||||
return res.every((e) => e.status >= 200 && e.status < 400);
|
||||
} else {
|
||||
return await pb.collection(COLLECTION_NAME_WORKFLOW).delete(record.id);
|
||||
return await pbco.delete(record.id);
|
||||
}
|
||||
};
|
||||
|
||||
export const subscribe = async (id: string, cb: (e: RecordSubscription<WorkflowModel>) => void) => {
|
||||
return getPocketBase().collection(COLLECTION_NAME_WORKFLOW).subscribe(id, cb);
|
||||
return pbco.subscribe(id, cb);
|
||||
};
|
||||
|
||||
export const unsubscribe = async (id: string) => {
|
||||
return getPocketBase().collection(COLLECTION_NAME_WORKFLOW).unsubscribe(id);
|
||||
return pbco.unsubscribe(id);
|
||||
};
|
||||
|
||||
@@ -2,16 +2,16 @@
|
||||
|
||||
import { COLLECTION_NAME_WORKFLOW_LOG, getPocketBase } from "./_pocketbase";
|
||||
|
||||
export const listByWorkflowRunId = async (workflowRunId: string) => {
|
||||
const pb = getPocketBase();
|
||||
const pb = getPocketBase();
|
||||
const pbco = pb.collection(COLLECTION_NAME_WORKFLOW_LOG);
|
||||
|
||||
const list = await pb.collection(COLLECTION_NAME_WORKFLOW_LOG).getFullList<WorkflowLogModel>({
|
||||
export const listByWorkflowRunId = async (workflowRunId: string) => {
|
||||
const list = await pbco.getFullList<WorkflowLogModel>({
|
||||
batch: 65535,
|
||||
filter: pb.filter("runRef={:workflowRunId}", { workflowRunId }),
|
||||
sort: "timestamp",
|
||||
requestKey: null,
|
||||
});
|
||||
|
||||
return {
|
||||
totalItems: list.length,
|
||||
items: list,
|
||||
|
||||
@@ -4,6 +4,9 @@ import { type WorkflowRunModel } from "@/domain/workflowRun";
|
||||
|
||||
import { COLLECTION_NAME_WORKFLOW_OUTPUT, COLLECTION_NAME_WORKFLOW_RUN, getPocketBase } from "./_pocketbase";
|
||||
|
||||
const pb = getPocketBase();
|
||||
const pbco = pb.collection(COLLECTION_NAME_WORKFLOW_RUN);
|
||||
|
||||
const _commonFields = ["id", "status", "trigger", "startedAt", "endedAt", "error", "created", "updated", "deleted"];
|
||||
const _expandFields = ["expand.workflowRef.id", "expand.workflowRef.name", "expand.workflowRef.description"];
|
||||
|
||||
@@ -18,14 +21,12 @@ export const list = async ({
|
||||
perPage?: number;
|
||||
expand?: boolean;
|
||||
}) => {
|
||||
const pb = getPocketBase();
|
||||
|
||||
const filters: string[] = [];
|
||||
if (workflowId) {
|
||||
filters.push(pb.filter("workflowRef={:workflowId}", { workflowId: workflowId }));
|
||||
}
|
||||
|
||||
const list = await pb.collection(COLLECTION_NAME_WORKFLOW_RUN).getList<WorkflowRunModel>(page, perPage, {
|
||||
const list = await pbco.getList<WorkflowRunModel>(page, perPage, {
|
||||
expand: expand ? ["workflowRef"].join(",") : void 0,
|
||||
fields: [..._commonFields, ..._expandFields].join(","),
|
||||
filter: filters.join(" && "),
|
||||
@@ -37,20 +38,16 @@ export const list = async ({
|
||||
};
|
||||
|
||||
export const get = async (id: string) => {
|
||||
const record = await getPocketBase()
|
||||
.collection(COLLECTION_NAME_WORKFLOW_RUN)
|
||||
.getOne<WorkflowRunModel>(id, {
|
||||
expand: ["workflowRef"].join(","),
|
||||
fields: ["*", ..._expandFields].join(","),
|
||||
requestKey: null,
|
||||
});
|
||||
const record = await pbco.getOne<WorkflowRunModel>(id, {
|
||||
expand: ["workflowRef"].join(","),
|
||||
fields: ["*", ..._expandFields].join(","),
|
||||
requestKey: null,
|
||||
});
|
||||
await enrichOutputs(record);
|
||||
return record;
|
||||
};
|
||||
|
||||
export const remove = async (record: MaybeModelRecordWithId<WorkflowRunModel> | MaybeModelRecordWithId<WorkflowRunModel>[]) => {
|
||||
const pb = getPocketBase();
|
||||
|
||||
if (Array.isArray(record)) {
|
||||
const batch = pb.createBatch();
|
||||
for (const item of record) {
|
||||
@@ -59,17 +56,17 @@ export const remove = async (record: MaybeModelRecordWithId<WorkflowRunModel> |
|
||||
const res = await batch.send();
|
||||
return res.every((e) => e.status >= 200 && e.status < 400);
|
||||
} else {
|
||||
await pb.collection(COLLECTION_NAME_WORKFLOW_RUN).delete(record.id!);
|
||||
await pbco.delete(record.id!);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
export const subscribe = async (id: string, cb: (e: RecordSubscription<WorkflowRunModel>) => void) => {
|
||||
return getPocketBase().collection(COLLECTION_NAME_WORKFLOW_RUN).subscribe(id, cb);
|
||||
return pbco.subscribe(id, cb);
|
||||
};
|
||||
|
||||
export const unsubscribe = async (id: string) => {
|
||||
return getPocketBase().collection(COLLECTION_NAME_WORKFLOW_RUN).unsubscribe(id);
|
||||
return pbco.unsubscribe(id);
|
||||
};
|
||||
|
||||
const enrichOutputs = async (records: WorkflowRunModel | WorkflowRunModel[]) => {
|
||||
@@ -82,7 +79,6 @@ const enrichOutputs = async (records: WorkflowRunModel | WorkflowRunModel[]) =>
|
||||
return;
|
||||
}
|
||||
|
||||
const pb = getPocketBase();
|
||||
const list = await pb.collection(COLLECTION_NAME_WORKFLOW_OUTPUT).getFullList({
|
||||
batch: 65535,
|
||||
fields: ["id", "runRef", "outputs"].join(","),
|
||||
|
||||
Reference in New Issue
Block a user