Skip to content

Commit

Permalink
feat(lobby): Add support for listMatches filtering to client
Browse files Browse the repository at this point in the history
#740 added the ability to pass query string parameters to the 
listMatches API endpoint to filter the matches returned. This adds 
support for building the relevant query string to the lobby client’s 
listMatches method.
  • Loading branch information
delucis committed Jun 17, 2020
1 parent accc63e commit c6d6c52
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
44 changes: 44 additions & 0 deletions src/lobby/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,50 @@ describe('LobbyClient', () => {
});

test('validates gameName', throwsWithInvalidGameName(client.listMatches));

describe('builds filter queries', () => {
test('kitchen sink', async () => {
await client.listMatches('chess', {
isGameover: false,
updatedBefore: 3000,
updatedAfter: 1000,
});
expect(fetch).toBeCalledWith(
'/games/chess?isGameover=false&updatedBefore=3000&updatedAfter=1000',
undefined
);
});

test('isGameover', async () => {
await client.listMatches('chess', { isGameover: undefined });
expect(fetch).toBeCalledWith('/games/chess', undefined);
await client.listMatches('chess', { isGameover: false });
expect(fetch).toBeCalledWith(
'/games/chess?isGameover=false',
undefined
);
await client.listMatches('chess', { isGameover: true });
expect(fetch).toBeCalledWith('/games/chess?isGameover=true', undefined);
});

test('updatedBefore', async () => {
const updatedBefore = 1989;
await client.listMatches('chess', { updatedBefore });
expect(fetch).toBeCalledWith(
'/games/chess?updatedBefore=1989',
undefined
);
});

test('updatedAfter', async () => {
const updatedAfter = 1970;
await client.listMatches('chess', { updatedAfter });
expect(fetch).toBeCalledWith(
'/games/chess?updatedAfter=1970',
undefined
);
});
});
});

describe('getMatch', () => {
Expand Down
32 changes: 30 additions & 2 deletions src/lobby/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ export class LobbyClient {
/**
* Get a list of the matches for a specific game type on the server.
* @param gameName The game to list for, e.g. 'tic-tac-toe'.
* @param where Options to filter matches by update time or gameover state
* @param init Optional RequestInit interface to override defaults.
* @return Array of match metadata objects.
*
* @example
* lobbyClient.listMatches('tic-tac-toe')
* lobbyClient.listMatches('tic-tac-toe', where: { isGameover: false })
* .then(data => console.log(data.matches));
* // => [
* // {
Expand All @@ -93,10 +94,37 @@ export class LobbyClient {
*/
async listMatches(
gameName: string,
where?: {
/**
* If true, only games that have ended will be returned.
* If false, only games that have not yet ended will be returned.
* Leave undefined to receive both finished and unfinished games.
*/
isGameover?: boolean;
/**
* List matches last updated before a specific time.
* Value should be a timestamp in milliseconds after January 1, 1970.
*/
updatedBefore?: number;
/**
* List matches last updated after a specific time.
* Value should be a timestamp in milliseconds after January 1, 1970.
*/
updatedAfter?: number;
},
init?: RequestInit
): Promise<LobbyAPI.MatchList> {
assertGameName(gameName);
return this.request(`/games/${gameName}`, init);
let query = '';
if (where) {
const queries = [];
const { isGameover, updatedBefore, updatedAfter } = where;
if (isGameover !== undefined) queries.push(`isGameover=${isGameover}`);
if (updatedBefore) queries.push(`updatedBefore=${updatedBefore}`);
if (updatedAfter) queries.push(`updatedAfter=${updatedAfter}`);
if (queries.length) query = '?' + queries.join('&');
}
return this.request(`/games/${gameName}${query}`, init);
}

/**
Expand Down

0 comments on commit c6d6c52

Please sign in to comment.