如何在同一个函数中处理分页和非分页获取结果的类型?

huangapple go评论66阅读模式
英文:

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&lt;Workflow[] | paginatedWorkflow&gt;(uri, {
 method: &quot;GET&quot;,
 baseURL: runtimeConfig.public.apiBaseURL,
 headers: {
 &quot;Content-Type&quot;: &quot;application/json&quot;,
&#160; &#160; },
 params: {
 paginate: paginate,
 search: search,
 sort: sort,
 order: order,
 page: page,
 size: size,
&#160; &#160; },
&#160; });
}

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(&quot;Error retrieving workflows: &quot; + error.value);
      }
    },

答案1

得分: 1

定义一个名为PaginatedWorkflow的新类型/接口,具有两个属性:meta(元数据)和data(工作流数组)。

创建一个联合类型称为FetchResult,它可以表示工作流数组或分页工作流对象。

修改useGetWorkflows函数,将FetchResult类型用作useFetch函数的泛型类型参数。更新函数签名以包括获取查询所需的参数。

确保在useFetch调用内提供适当的值,如uriruntimeConfig.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&lt;FetchResult&gt;(uri, {
    method: &quot;GET&quot;,
    baseURL: runtimeConfig.public.apiBaseURL,
    headers: {
      &quot;Content-Type&quot;: &quot;application/json&quot;,
    },
    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(&quot;Error retrieving workflows: &quot; + error);
  }
}

huangapple
  • 本文由 发表于 2023年6月19日 12:53:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定