英文:
How do I handle the types for both paginated and non-paginated fetch results in the same function?
问题
以下是翻译好的部分:
我有一个用于查询后端API的fetch查询,可以返回分页或非分页数据。
分页数据将返回为一个带有meta和data子对象的对象。
非分页数据只会作为一个数组返回。
对于非分页数据,我可以将其类型设置为数组。当分页数据返回时,我该如何处理?
我想要做的是设置一个分页接口,然后说明结果可以是workflow[]或paginatedWorkflow。
但是我的IDE说分页的 'data' 不存在。
以下是代码:
interface paginatedWorkflow {
meta: {};
data: Workflow[];
}
export function useGetWorkflows(
paginate: boolean,
search: string,
sort: string,
order: string,
page: number,
size: number
) {
return useFetch<Workflow[] | paginatedWorkflow>(uri, {
method: "GET",
baseURL: runtimeConfig.public.apiBaseURL,
headers: {
"Content-Type": "application/json",
},
params: {
paginate: paginate,
search: search,
sort: sort,
order: order,
page: page,
size: size,
},
});
}
我从组件或Pinia store中调用该函数,例如:
async getWorkflows(
paginate: boolean,
search: string,
sort: string,
order: string,
page: number,
size: number
) {
const { data , pending, refresh, error } =
await useGetWorkflows(
paginate,
search,
sort,
order,
page,
size
);
if (paginate) {
this.workflows = data?.value?.data;
} else {
this.workflows = data?.value?? [];
}
if (error.value) {
useNegativeNotify("Error retrieving workflows: " + error.value);
}
}
请注意,以上是已翻译的部分。如果您需要进一步的帮助,请随时提出具体问题。
英文:
I have a fetch query that queries a backend API that can return paginated or non-paginated data.
The paginated data will come back as an object with meta and data sub-objects.
Non-paginated data just comes back as an array.
I can set the type for the non-paginated data as an array. How do I handle the case when paginated data comes back?
I want to do something like set a paginated interface then state that the result can be a workflow[] OR paginatedWorkflow
But my IDE says the paginated 'data' does not exist.
Code below:
interface paginatedWorkflow {
meta: {};
data: Workflow[];
}
export function useGetWorkflows(
paginate: boolean,
search: string,
sort: string,
order: string,
page: number,
size: number
) {
return useFetch<Workflow[] | paginatedWorkflow>(uri, {
method: "GET",
baseURL: runtimeConfig.public.apiBaseURL,
headers: {
"Content-Type": "application/json",
    },
params: {
paginate: paginate,
search: search,
sort: sort,
order: order,
page: page,
size: size,
    },
  });
}
I call the function from within component or Pinia store, i.e.:
async getWorkflows(
paginate: boolean,
search: string,
sort: string,
order: string,
page: number,
size: number
) {
const { data , pending, refresh, error } =
await useGetWorkflows(
paginate,
search,
sort,
order,
page,
size
);
if (paginate) {
this.workflows = data?.value?.data;
} else {
this.workflows = data?.value?? [];
}
if (error.value) {
useNegativeNotify("Error retrieving workflows: " + error.value);
}
},
答案1
得分: 1
定义一个名为PaginatedWorkflow
的新类型/接口,具有两个属性:meta
(元数据)和data
(工作流数组)。
创建一个联合类型称为FetchResult
,它可以表示工作流数组或分页工作流对象。
修改useGetWorkflows
函数,将FetchResult
类型用作useFetch
函数的泛型类型参数。更新函数签名以包括获取查询所需的参数。
确保在useFetch
调用内提供适当的值,如uri
、runtimeConfig.public.apiBaseURL
和其他必要属性的值。
通过这些更改,useGetWorkflows
函数可以通过指定FetchResult
类型来处理分页和非分页的获取响应。
以下是您修改后的代码,希望对您有帮助:
interface PaginatedWorkflow {
meta: {};
data: Workflow[];
}
type FetchResult = Workflow[] | PaginatedWorkflow;
export function useGetWorkflows(
paginate: boolean,
search: string,
sort: string,
order: string,
page: number,
size: number
) {
return useFetch<FetchResult>(uri, {
method: "GET",
baseURL: runtimeConfig.public.apiBaseURL,
headers: {
"Content-Type": "application/json",
},
params: {
paginate: paginate,
search: search,
sort: sort,
order: order,
page: page,
size: size,
},
});
}
希望这对您有所帮助。
英文:
Define a new type/interface called PaginatedWorkflow
with two properties: meta
(metadata) and data
(an array of workflows).
Create a union type called FetchResult
that can represent either an array of workflows or a paginated workflow object.
Modify the useGetWorkflows
function to use the FetchResult
type as the generic type argument for the useFetch
function.
Update the function signature to include the required parameters for the fetch query.
Make sure to provide the appropriate values for the uri
, runtimeConfig.public.apiBaseURL
, and other necessary properties within the useFetch call.
With these changes, the useGetWorkflows
function can handle both paginated and non-paginated fetch responses by specifying the FetchResult
type.
Below is the your code that I modified, hope it helps
interface PaginatedWorkflow {
meta: {};
data: Workflow[];
}
type FetchResult = Workflow[] | PaginatedWorkflow;
export function useGetWorkflows(
paginate: boolean,
search: string,
sort: string,
order: string,
page: number,
size: number
) {
return useFetch<FetchResult>(uri, {
method: "GET",
baseURL: runtimeConfig.public.apiBaseURL,
headers: {
"Content-Type": "application/json",
},
params: {
paginate: paginate,
search: search,
sort: sort,
order: order,
page: page,
size: size,
},
});
}
I hope this works for you
async getWorkflows(
paginate: boolean,
search: string,
sort: string,
order: string,
page: number,
size: number
) {
const { data, pending, refresh, error } = await useGetWorkflows(
paginate,
search,
sort,
order,
page,
size
);
if (paginate) {
if (Array.isArray(data)) {
this.workflows = data;
} else {
this.workflows = data?.data ?? [];
}
} else {
this.workflows = data ?? [];
}
if (error) {
useNegativeNotify("Error retrieving workflows: " + error);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论