Skip to content

Commit

Permalink
fix(easy): improved the ApiGateway get() function, to now consider al…
Browse files Browse the repository at this point in the history
…l props from meta.
  • Loading branch information
aahoogendoorn committed Jul 9, 2023
1 parent c95a143 commit 3f795b7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
12 changes: 2 additions & 10 deletions packages/easy/src/services/ApiGateway.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Api, RouteOptions } from './Api';
import { FetchOptions, Filter, Gateway, Json, Optional, PageList, toPageList, Uri } from '../types';
import { FetchOptions, Filter, Gateway, Json, Optional, PageList, toPageList, Uri, use } from "../types";
import { RequestOptions, toPageOptions } from '../http';

export class ApiGateway extends Gateway<RouteOptions> {
Expand All @@ -8,15 +8,7 @@ export class ApiGateway extends Gateway<RouteOptions> {
}

get(uri: Uri, options?: RouteOptions): Promise<PageList<Json>> {
return this.api.get(uri, options).then(r =>
toPageList<Json>(
r.body.data?.items,
toPageOptions(options) && {
total: r.body.data?.totalItems,
filters: r.body.data?.meta?.filters as Filter[],
}
)
);
return this.api.get(uri, options).then(r => use(r.body.data, d => toPageList(d?.items, {total: d?.totalItems, ...d?.meta})));
}

getOne(uri: Uri, options?: RouteOptions): Promise<Optional<Json>> {
Expand Down
18 changes: 13 additions & 5 deletions packages/easy/test/services/ApiGateway.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,24 @@ describe('ApiGateway', () => {
const filters = [{ field: 'A', values: [{ value: 1 }, { value: 2 }] }];
const sorts = ['alpha-asc', 'alpha-desc'];

test('get calls api correctly with options (but missing sorts)', async () => {
api.get = mock.resolve({ body: {data: { totalItems: 42, items: [{id: 1, name: 'Sander'}], meta: { filters, sorts, skip: 0, take: 250 }}}});
test('get calls api correctly with no options but with totalItems', async () => {
api.get = mock.resolve({ body: {data: { totalItems: 42, items: [{id: 1, name: 'Sander'}]}}});
const pl = await gateway.get(DevUri.Developers, {skip: 0, take: 5});
expect(pl).toHaveLength(1);
expect(pl[0].name).toBe('Sander');
expect(pl.total).toBe(42);
expect(pl.skip).toBe(0);
expect(pl.take).toBe(250);
expect(pl.filters).toBeUndefined();
expect(pl.sorts).toBeUndefined();
});

test('get calls api correctly with options', async () => {
api.get = mock.resolve({ body: {data: { totalItems: 42, items: [{id: 1, name: 'Sander'}], meta: { filters, total: 41, sorts, skip: 0, take: 250 }}}});
const pl = await gateway.get(DevUri.Developers, {skip: 0, take: 5});
expect(pl).toHaveLength(1);
expect(pl[0].name).toBe('Sander');
expect(pl.total).toBe(41);
expect(pl.filters).toStrictEqual(filters);
// expect(pl.sorts).toStrictEqual(sorts);
expect(pl.sorts).toStrictEqual(sorts);
});

test('post calls api correctly', async () => {
Expand Down

0 comments on commit 3f795b7

Please sign in to comment.