Skip to content

Commit

Permalink
fix sorting bug
Browse files Browse the repository at this point in the history
  • Loading branch information
eluce2 committed Nov 4, 2024
1 parent 5e7f434 commit 9b448b0
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @proofgeist/fmdapi

## 4.1.4

### Patch Changes

- Fix sorting query in base fetch adapter

## 4.1.3

### Patch Changes
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@proofgeist/fmdapi",
"version": "4.1.3",
"version": "4.1.4",
"description": "FileMaker Data API client",
"main": "dist/index.js",
"repository": "[email protected]:proofgeist/fm-dapi.git",
Expand Down Expand Up @@ -40,6 +40,7 @@
"dayjs": "^1.11.13",
"dotenv": "^16.4.5",
"fs-extra": "^11.1.1",
"infinite-table": "link:/Users/ericluce/Documents/Code/work/test-alpaca-projects/infinite-table",
"ts-morph": "^24.0.0",
"ts-toolbelt": "^9.6.0"
},
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/adapters/fetch-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export class BaseFetchAdapter implements Adapter {
const url = new URL(`${this.baseUrl}${params.url}`);

if (query) {
const searchParams = new URLSearchParams(query);
const { _sort, ...rest } = query;
const searchParams = new URLSearchParams(rest);
if (query.portalRanges && typeof query.portalRanges === "object") {
for (const [portalName, value] of Object.entries(
query.portalRanges as PortalRanges,
Expand All @@ -92,6 +93,9 @@ export class BaseFetchAdapter implements Adapter {
}
}
}
if (_sort) {
searchParams.set("_sort", JSON.stringify(_sort));
}
searchParams.delete("portalRanges");
url.search = searchParams.toString();
}
Expand Down Expand Up @@ -171,6 +175,7 @@ export class BaseFetchAdapter implements Adapter {

public list = async (opts: ListOptions): Promise<GetResponse> => {
const { data, layout } = opts;

const resp = await this.request({
url: `/layouts/${layout}/records`,
query: data as Record<string, string>,
Expand Down
3 changes: 2 additions & 1 deletion src/client-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ export type GetParams<U extends GenericPortalData = GenericPortalData> =

export type Sort<T extends FieldData = FieldData> = {
fieldName: keyof T;
sortOrder: "ascend" | "descend" | string;
// eslint-disable-next-line @typescript-eslint/ban-types
sortOrder?: "ascend" | "descend" | (string & {});
};

export type ListParams<
Expand Down
21 changes: 21 additions & 0 deletions test/client-methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ import {
import { config, layoutClient, weirdPortalClient } from "./setup";
import { describe, test, expect, it } from "vitest";

describe("sort methods", () => {
test("should sort descending", async () => {
const resp = await layoutClient.list({
sort: { fieldName: "recordId", sortOrder: "descend" },
});
expect(resp.data.length).toBe(3);
const firstRecord = parseInt(resp.data[0].fieldData.recordId as string);
const secondRecord = parseInt(resp.data[1].fieldData.recordId as string);
expect(firstRecord).toBeGreaterThan(secondRecord);
});
test("should sort ascending by default", async () => {
const resp = await layoutClient.list({
sort: { fieldName: "recordId" },
});

const firstRecord = parseInt(resp.data[0].fieldData.recordId as string);
const secondRecord = parseInt(resp.data[1].fieldData.recordId as string);
expect(secondRecord).toBeGreaterThan(firstRecord);
});
});

describe("find methods", () => {
const client = DataApi({
adapter: new OttoAdapter({
Expand Down

0 comments on commit 9b448b0

Please sign in to comment.