Skip to content

Commit

Permalink
Fix search documents vtex (#759)
Browse files Browse the repository at this point in the history
* fix(searchDocuments): set range size dinamically

* linting

* chore(searchdocuments): semantic ajusts

* chore(searchdocuments): limits

* Update vtex/utils/ajustLimits.ts

Co-authored-by: Marcos Candeia <marrcooos@gmail.com>

* chore(searchdocuments): function name

---------

Co-authored-by: Marcos Candeia <marrcooos@gmail.com>
  • Loading branch information
aka-sacci-ccr and mcandeia authored Aug 12, 2024
1 parent 1c51c06 commit ca15cfd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
15 changes: 12 additions & 3 deletions vtex/loaders/masterdata/searchDocuments.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AppContext } from "../../mod.ts";
import { resourceRange } from "../../utils/resourceRange.ts";
import type { Document } from "../../utils/types.ts";
import { parseCookie } from "../../utils/vtexId.ts";

Expand All @@ -25,7 +26,14 @@ interface Props {
* @maxValue 100
* @minValue 1
*/
count?: number;
take?: number;
/**
* @description Skip how many documents
* @default 0
* @maxValue 100
* @minValue 0
*/
skip?: number;
}

/**
Expand All @@ -38,8 +46,9 @@ export default async function loader(
ctx: AppContext,
): Promise<Document[]> {
const { vcs } = ctx;
const { acronym, fields, where, sort, count = 10 } = props;
const { acronym, fields, where, sort, skip = 0, take = 10 } = props;
const { cookie } = parseCookie(req.headers, ctx.account);
const limits = resourceRange(skip, take);

const documents = await vcs["GET /api/dataentities/:acronym/search"]({
acronym,
Expand All @@ -51,7 +60,7 @@ export default async function loader(
accept: "application/vnd.vtex.ds.v10+json",
"content-type": "application/json",
cookie,
"REST-Range": `resources=1-${Math.max(1, Math.min(count, 100))}`,
"REST-Range": `resources=${limits.from}-${limits.to}`,
},
}).then((response) => response.json());

Expand Down
13 changes: 13 additions & 0 deletions vtex/utils/resourceRange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

export function resourceRange(
skip: number,
take: number,
) {
const from = Math.max(skip, 0);
const to = from + Math.min(100, take);

return {
from,
to,
};
}

0 comments on commit ca15cfd

Please sign in to comment.