Skip to content

Commit

Permalink
throw error when both cursor/size and limit is set
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Weirich <matthias.weirich@selectcode.de>
  • Loading branch information
vavido committed Dec 6, 2022
1 parent 763431a commit 6f4fc2a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
15 changes: 13 additions & 2 deletions javascript/lib/api/src/options/request.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ export interface HasFilterAndNamespace<T extends HasFilterAndNamespace<T>> {

export interface RequestOptionsWithMatchOptions<T extends RequestOptionsWithMatchOptions<T>>
extends AddRequestOptions<RequestOptionsWithMatchOptions<T>>,
HasMatch<RequestOptionsWithMatchOptions<T>> { }
HasMatch<RequestOptionsWithMatchOptions<T>> {
}


export abstract class AbstractRequestOptionsWithMatchOptions<T extends AbstractRequestOptionsWithMatchOptions<T>>
Expand Down Expand Up @@ -178,7 +179,8 @@ export abstract class AbstractRequestOptionsWithMatchOptions<T extends AbstractR
/**
* Option provider for If-Match / If-None-Match headers
*/
export interface MatchOptions extends RequestOptionsWithMatchOptions<MatchOptions> {}
export interface MatchOptions extends RequestOptionsWithMatchOptions<MatchOptions> {
}

export class DefaultMatchOptions extends AbstractRequestOptionsWithMatchOptions<DefaultMatchOptions> implements MatchOptions {

Expand Down Expand Up @@ -344,6 +346,9 @@ export class DefaultSearchOptions extends AbstractRequestOptions<DefaultSearchOp
}

public withLimit(offset: number, count: number): DefaultSearchOptions {
if (this.optionParameters.has('cursor') || this.optionParameters.has('size')) {
throw new Error('Cursor/size and limit options cannot be set at the same time');
}
this.optionParameters.set('limit', `${offset},${count}`);
return this.setOption();
}
Expand All @@ -354,11 +359,17 @@ export class DefaultSearchOptions extends AbstractRequestOptions<DefaultSearchOp
}

public withCursor(cursor: string): DefaultSearchOptions {
if (this.optionParameters.has('limit')) {
throw new Error('Limit and cursor options cannot be set at the same time');
}
this.optionParameters.set(`cursor`, cursor);
return this.setOption();
}

public withPageSize(pageSize: number): DefaultSearchOptions {
if (this.optionParameters.has('limit')) {
throw new Error('Limit and cursor options cannot be set at the same time');
}
this.optionParameters.set('size', pageSize.toFixed(0));
return this.setOption();
}
Expand Down
13 changes: 13 additions & 0 deletions javascript/lib/api/tests/options/request.options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,19 @@ describe('Search Options', () => {
searchOptions.withCursor('eJylkD1PwzAQhv-LpxQc5YM0SbNBBBJDp0oslMGxz82JYJfLBQbEf8cpQupGKfZ29zz3Su').withPageSize(42);
expect(searchOptions.getOptions().get('option')).toEqual(encodeURIComponent('cursor(eJylkD1PwzAQhv-LpxQc5YM0SbNBBBJDp0oslMGxz82JYJfLBQbEf8cpQupGKfZ29zz3Su),size(42)'));
});
it('throws an error when setting limit and cursor is already set', () => {
searchOptions.withCursor('foo');
expect(() => searchOptions.withLimit(10, 10)).toThrowError();
});
it('throws an error when setting pageSize and limit is already set', () => {
searchOptions.withLimit(10, 10);
expect(() => searchOptions.withPageSize(10)).toThrowError();
});
it('throws an error when setting cursor and limit is already set', () => {
searchOptions.withLimit(10, 10);
expect(() => searchOptions.withCursor('foo')).toThrowError();
});

});

describe('Get Things Options', () => {
Expand Down

0 comments on commit 6f4fc2a

Please sign in to comment.