infinum / datx

DatX is an opinionated JS/TS data store. It features support for simple property definition, references to other models and first-class TypeScript support.
https://datx.dev
MIT License
140 stars 7 forks source link

Query parameters doubled #1173

Closed doroz0 closed 1 year ago

doroz0 commented 1 year ago

Used libraries

core, jsonapi, swr

Library version(s)

@datx/core 2.5.1, @datx/jsonapi 2.5.1, @datx/swr 2.5.1

Sample API response (if relevant)

No response

Environments with the issue

All

Environments without the issue

No response

Current behavior

const getPostComentsRelationshipPageQuery = (id?: string, index = 1, size = 4) =>
  id
    ? ({
        id,
        op: "getRelatedResources",
        type: `posts`,
        relation: "comments",
        queryParams: {
          sort: "-created",
          include: "post",
          fields: {
            posts: "id",
          },
          custom: [
            { key: "page[number]", value: String(index) },
            { key: "page[size]", value: String(size) },
          ],
        },
      } as const satisfies IGetRelatedResourcesExpression<typeof Post>)
    : null;
  const getKey = (pageIndex: number, previousPageData: CollectionResponse | null) => {
    if (!showComments || (previousPageData && previousPageData.data.length === 0)) return null;
    return getPostComentsRelationshipPageQuery(post?.id, pageIndex + 1, 4);
  };

  const {
    data: comments,
    mutate: mutateComments,
    size,
    setSize,
  } = useDatxInfinite(getKey, {
    revalidateAll: true,
  });

image

Expected behavior

image

Reproduction steps

You can try it out at https://github.com/doroz0/soc-net-clone-nextjs, just remove the workaround (https://github.com/doroz0/soc-net-clone-nextjs/blob/main/src/datx/createClient.ts#L21)