Skip to content

Commit

Permalink
feat: add option to pass page options
Browse files Browse the repository at this point in the history
BREAKING: This changes the signature of findAll()
  • Loading branch information
dnlkoch committed Jul 13, 2023
1 parent de68643 commit dc2dad3
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 6 deletions.
87 changes: 87 additions & 0 deletions src/service/GenericEntityService/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,93 @@ describe('GenericService', () => {
});
});

it('sends all required parameters to return all paged entities (findAll)', async () => {
fetchMock = fetchSpy(successResponse([]));

await service.findAll({
page: 0
});

expect(fetchMock).toHaveBeenCalledWith('/dummy?page=0', {
headers: {},
method: 'GET'
});

fetchMock.mockClear();

await service.findAll({
page: 0,
size: 10
});

expect(fetchMock).toHaveBeenCalledWith('/dummy?page=0&size=10', {
headers: {},
method: 'GET'
});

fetchMock.mockClear();

await service.findAll({
page: 0,
size: 10,
sort: {
properties: ['a']
}
});

expect(fetchMock).toHaveBeenCalledWith('/dummy?page=0&size=10&sort=a', {
headers: {},
method: 'GET'
});

fetchMock.mockClear();

await service.findAll({
page: 0,
size: 10,
sort: {
properties: ['a', 'b']
}
});

expect(fetchMock).toHaveBeenCalledWith('/dummy?page=0&size=10&sort=a%2Cb', {
headers: {},
method: 'GET'
});

fetchMock.mockClear();

await service.findAll({
page: 0,
size: 10,
sort: {
properties: ['a', 'b'],
order: 'asc'
}
});

expect(fetchMock).toHaveBeenCalledWith('/dummy?page=0&size=10&sort=a%2Cb%2Casc', {
headers: {},
method: 'GET'
});

fetchMock.mockClear();

await service.findAll({
page: 0,
size: 10,
sort: {
properties: ['a', 'b'],
order: 'desc'
}
});

expect(fetchMock).toHaveBeenCalledWith('/dummy?page=0&size=10&sort=a%2Cb%2Cdesc', {
headers: {},
method: 'GET'
});
});

it('returns all entities (findAll)', async () => {
const response = {
content: [
Expand Down
6 changes: 3 additions & 3 deletions src/service/GenericEntityService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import BaseEntity from '../../model/BaseEntity';
import { Page } from '../../model/Page';
import { getBearerTokenHeader } from '../../security/getBearerTokenHeader';
import { getCsrfTokenHeader } from '../../security/getCsrfTokenHeader';
import { GenericServiceOpts } from '../GenericService';
import { GenericServiceOpts, PageOpts } from '../GenericService';
import PermissionService from '../PermissionService';

export type GenericEntityServiceOpts = GenericServiceOpts;
Expand All @@ -13,9 +13,9 @@ export abstract class GenericEntityService<T extends BaseEntity> extends Permiss
super(opts);
}

async findAll(fetchOpts?: RequestInit): Promise<Page<T>> {
async findAll(pageOpts?: PageOpts, fetchOpts?: RequestInit): Promise<Page<T>> {
try {
const response = await fetch(this.basePath, {
const response = await fetch(this.getPageUrl(pageOpts), {
method: 'GET',
headers: {
...getBearerTokenHeader(this.keycloak)
Expand Down
6 changes: 3 additions & 3 deletions src/service/GenericFileService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SHOGunFile from '../../model/File';
import { Page } from '../../model/Page';
import { getBearerTokenHeader } from '../../security/getBearerTokenHeader';
import { getCsrfTokenHeader } from '../../security/getCsrfTokenHeader';
import { GenericService, GenericServiceOpts } from '../GenericService';
import { GenericService, GenericServiceOpts, PageOpts } from '../GenericService';

export type GenericFileServiceOpts = GenericServiceOpts;

Expand All @@ -12,9 +12,9 @@ export abstract class GenericFileService<T extends SHOGunFile> extends GenericSe
super(opts);
}

async findAll(fetchOpts?: RequestInit): Promise<Page<T>> {
async findAll(pageOpts?: PageOpts, fetchOpts?: RequestInit): Promise<Page<T>> {
try {
const response = await fetch(this.basePath, {
const response = await fetch(this.getPageUrl(pageOpts), {
method: 'GET',
headers: {
...getBearerTokenHeader(this.keycloak)
Expand Down
36 changes: 36 additions & 0 deletions src/service/GenericService/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { UrlUtil } from '@terrestris/base-util';
import Keycloak from 'keycloak-js';

export type PageSorter = {
properties: string[];
order?: 'asc' | 'desc';
};

export type PageOpts = {
page?: number;
size?: number;
sort?: PageSorter;
};

export type GenericServiceOpts = {
basePath: string;
keycloak?: Keycloak;
Expand All @@ -15,6 +27,30 @@ export abstract class GenericService {
this.keycloak = opts.keycloak;
}

protected getPageUrl(pageOpts?: PageOpts) {
const opts: any = {};

if (Number.isFinite(pageOpts?.page)) {
opts.page = pageOpts?.page;
}

if (Number.isFinite(pageOpts?.size)) {
opts.size = pageOpts?.size;
}

if (pageOpts?.sort) {
const sortValue = [...pageOpts.sort.properties, pageOpts.sort.order].filter(a => a);
if (sortValue.length > 0) {
opts.sort = `${sortValue.join(',')}`;
}
}

if (Object.keys(opts).length === 0) {
return this.basePath;
}

return `${this.basePath}?${UrlUtil.objectToRequestString(opts)}`;
}
}

export default GenericService;

0 comments on commit dc2dad3

Please sign in to comment.