Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: compatible with paged issue list response (#2404)

Liu Ji932d317daf88
diff --git a/frontend/src/store/modules/issue.ts b/frontend/src/store/modules/issue.ts
index a86ba4794c..891af4b212 100644
--- a/frontend/src/store/modules/issue.ts
+++ b/frontend/src/store/modules/issue.ts
@@ -3,6 +3,7 @@ import axios from "axios";
import {
empty,
EMPTY_ID,
+ isPagedResponse,
Issue,
IssueCreate,
IssueId,
@@ -25,6 +26,7 @@ import { useDatabaseStore } from "./database";
import { useInstanceStore } from "./instance";
import { usePipelineStore } from "./pipeline";
import { useProjectStore } from "./project";
+import { convertEntityList } from "./utils";
function convert(issue: ResourceObject, includedList: ResourceObject[]): Issue {
const projectId = (issue.relationships!.project.data as ResourceIdentifier)
@@ -90,6 +92,27 @@ function convert(issue: ResourceObject, includedList: ResourceObject[]): Issue {
};
}
+function getIssueFromIncludedList(
+ data:
+ | ResourceIdentifier<ResourceObject>
+ | ResourceIdentifier<ResourceObject>[]
+ | undefined,
+ includedList: ResourceObject[]
+): Issue {
+ if (data == null) {
+ return empty("ISSUE");
+ }
+ for (const item of includedList || []) {
+ if (item.type !== "issue") {
+ continue;
+ }
+ if (item.id == (data as ResourceIdentifier).id) {
+ return convert(item, includedList);
+ }
+ }
+ return empty("ISSUE");
+}
+
export const useIssueStore = defineStore("issue", {
state: (): IssueState => ({
issueById: new Map(),
@@ -105,19 +128,21 @@ export const useIssueStore = defineStore("issue", {
setIssueById({ issueId, issue }: { issueId: IssueId; issue: Issue }) {
this.issueById.set(issueId, issue);
},
- async fetchIssueList({
+ async fetchPagedIssueList({
issueStatusList,
userId,
projectId,
limit,
+ token,
}: {
issueStatusList?: IssueStatus[];
userId?: PrincipalId;
projectId?: ProjectId;
limit?: number;
+ token?: string;
}) {
const queryList = [];
- if (issueStatusList) {
+ if (issueStatusList && issueStatusList.length > 0) {
queryList.push(`status=${issueStatusList.join(",")}`);
}
if (userId) {
@@ -129,17 +154,42 @@ export const useIssueStore = defineStore("issue", {
if (limit) {
queryList.push(`limit=${limit}`);
}
+ if (token) {
+ queryList.push(`token=${token}`);
+ }
+
let url = "/api/issue";
if (queryList.length > 0) {
url += `?${queryList.join("&")}`;
}
- const data = (await axios.get(url)).data;
- const issueList: Issue[] = data.data.map((issue: ResourceObject) => {
- return convert(issue, data.included);
- });
+ const responseData = (await axios.get(url)).data;
+ const issueList = convertEntityList(
+ responseData,
+ "issues",
+ convert,
+ getIssueFromIncludedList
+ );
+
+ issueList.forEach((issue) =>
+ this.setIssueById({ issueId: issue.id, issue })
+ );
- // The caller consumes directly, so we don't store it.
- return issueList;
+ const nextToken = isPagedResponse(responseData, "issues")
+ ? responseData.data.attributes.nextToken
+ : "";
+ return {
+ nextToken,
+ issueList,
+ };
+ },
+ async fetchIssueList(params: {
+ issueStatusList?: IssueStatus[];
+ userId?: PrincipalId;
+ projectId?: ProjectId;
+ limit?: number;
+ }) {
+ const result = await this.fetchPagedIssueList(params);
+ return result.issueList;
},
async fetchIssueById(issueId: IssueId) {
const data = (await axios.get(`/api/issue/${issueId}`)).data;
diff --git a/frontend/src/store/modules/utils.ts b/frontend/src/store/modules/utils.ts
new file mode 100644
index 0000000000..3e0232effa
--- /dev/null
+++ b/frontend/src/store/modules/utils.ts
@@ -0,0 +1,42 @@
+import {
+ isPagedResponse,
+ PagedResponse,
+ ResourceIdentifier,
+ ResourceObject,
+ ResponseWithData,
+} from "@/types";
+
+type ConvertEntityFn<T> = (
+ data: ResourceObject,
+ includedList: ResourceObject[]
+) => T;
+
+type ConvertEntityFromIncludedListFn<T> = (
+ date:
+ | ResourceIdentifier<ResourceObject>
+ | ResourceIdentifier<ResourceObject>[]
+ | undefined,
+ includedList: ResourceObject[]
+) => T;
+
+// convert entity list from response data
+// works for normal array response data and paged response data
+export function convertEntityList<T, K extends string>(
+ responseData: ResponseWithData | PagedResponse<K>,
+ key: K,
+ convert: ConvertEntityFn<T>,
+ convertFromIncludedList: ConvertEntityFromIncludedListFn<T>
+) {
+ if (isPagedResponse(responseData, key)) {
+ const resourceIdentifierList =
+ responseData.data.relationships?.[key].data ?? [];
+ return resourceIdentifierList.map((item) => {
+ return convertFromIncludedList(item, responseData.included ?? []);
+ });
+ }
+
+ const resourceList = responseData.data as ResourceObject[];
+ return resourceList.map((obj: ResourceObject) => {
+ return convert(obj, responseData.included ?? []);
+ });
+}
diff --git a/frontend/src/types/jsonapi/index.ts b/frontend/src/types/jsonapi/index.ts
index 5dd955f1fc..daa08ea74c 100644
--- a/frontend/src/types/jsonapi/index.ts
+++ b/frontend/src/types/jsonapi/index.ts
@@ -3,3 +3,4 @@ export * from "./request";
export * from "./resourceObject";
export * from "./response";
export * from "./shared";
+export * from "./pagedResponse";
diff --git a/frontend/src/types/jsonapi/pagedResponse.ts b/frontend/src/types/jsonapi/pagedResponse.ts
new file mode 100644
index 0000000000..ec4dfb1944
--- /dev/null
+++ b/frontend/src/types/jsonapi/pagedResponse.ts
@@ -0,0 +1,51 @@
+import { ResourceIdentifier, ResourceObject } from "./resourceObject";
+import { ResponseWithData } from "./response";
+/**
+ * example
+ {
+ "data": {
+ "attributes": { "nextToken": "MTAy" },
+ "relationships": {
+ "issues": {
+ "data": [
+ { "type": "issue", "id": "109" },
+ { "type": "issue", "id": "108" },
+ ...
+ ]
+ }
+ }
+ },
+ "included": [
+ ...
+ ]
+ }
+ */
+export type PagedResponse<K extends string> = {
+ data: {
+ attributes: {
+ nextToken: string;
+ };
+ relationships?: {
+ [key in K]: {
+ data: ResourceIdentifier[];
+ };
+ };
+ };
+ included?: ResourceObject[];
+};
+
+export function isPagedResponse<K extends string>(
+ responseData: ResponseWithData | PagedResponse<K>,
+ key: K
+): responseData is PagedResponse<K> {
+ const { data } = responseData;
+ if (Array.isArray(data)) return false;
+ if (typeof data === "object") {
+ return (
+ typeof data.attributes?.nextToken === "string" &&
+ Array.isArray(data.relationships?.[key]?.data)
+ );
+ }
+
+ return false;
+}