Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How could I modify the response of this promise in this js function result before it being sent?

So I have This function

import service from "services/catalogs";

    export const getPaginatedAdminPublications = async (params: URLSearchParams) => {
      params.delete("sort");
      const searchParams = new URLSearchParams(params.get("search"));
      if (searchParams) {
        const publicationId = searchParams.get("id");
        publicationId && params.append("ids[]", publicationId);
        params.delete("search");
        params.delete("id");
      }
      return service.getPaginatedAdminPublications(params);
    };

IT modifies the params and sends it to a function that returns a promise and this promise returns a result

  getPaginatedAdminPublications: (params: URLSearchParams) =>
    HttpService.get<API.PaginatedResponse<API.Publication>>(
      `${CATALOG_SERVICE_URL}/${CATALOG_VERSION_V3}/xxxxx`,
      params
    ),

The result returned is similar to

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
 data: [...],
 metadata: {
 page: 1,
 total_pages: 10
 }
}

But I want it to be

{
 data: [...],
 metadata: {
 page: 1,
 total_pages: 10
 }
pagination: {
 page: 1,
 total_pages: 10
}
}

SO ideally I want to somehow get the results and then still return some kind of promise but with the modified data.
With this pseudocode

export const getPaginatedAdminPublications = async (params: URLSearchParams) => {
  params.delete("sort");
  const searchParams = new URLSearchParams(params.get("search"));
  if (searchParams) {
    const publicationId = searchParams.get("id");
    publicationId && params.append("ids[]", publicationId);
    params.delete("search");
    params.delete("id");
  }
  return service.getPaginatedAdminPublications(params).then(data => {
    return {
        ...data,
        pagination: {
            total: data.data.metadata.total_results,
        }
    }
  });
};

Is something like this possible? DOesnt seem to work

>Solution :

As you already use async function:

export const getPaginatedAdminPublications = async (params: URLSearchParams) => {
      params.delete("sort");
      const searchParams = new URLSearchParams(params.get("search"));
      if (searchParams) {
        const publicationId = searchParams.get("id");
        publicationId && params.append("ids[]", publicationId);
        params.delete("search");
        params.delete("id");
      }
      const data = await service.getPaginatedAdminPublications(params);
      return {...data, pagination: {
            total: data.data.metadata.total_results,
        }}
    };
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading