Skip to content

Commit

Permalink
Adds token generation and handling for grouping autocomplete and deta…
Browse files Browse the repository at this point in the history
…ils calls
  • Loading branch information
coconutcraig committed Aug 22, 2019
1 parent b0c536d commit 72a25ce
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"version": "0.6.6",
"description": "Coconut Calendar JS Open API SDK",
"main": "dist/index.js",
"files": ["dist"],
"files": [
"dist"
],
"types": "dist/index.d.ts",
"scripts": {
"test": "jest --config jestconfig.json",
Expand Down Expand Up @@ -48,6 +50,8 @@
"typescript": "^3.1.6"
},
"dependencies": {
"axios": "^0.18.0"
"@types/uuid": "^3.4.5",
"axios": "^0.18.0",
"uuid": "^3.3.3"
}
}
19 changes: 19 additions & 0 deletions src/helpers/token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import uuid from 'uuid/v4';

export const STORAGE_KEY = 'location-details-token';

export function remove() {
sessionStorage.removeItem(STORAGE_KEY);
}

export function retrieve() {
let token = sessionStorage.getItem(STORAGE_KEY);

if (token) {
return token;
}

sessionStorage.setItem(STORAGE_KEY, token = uuid());

return token;
}
25 changes: 25 additions & 0 deletions src/resources/location.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import mockAxios from 'axios';

import Location from './location';
import { STORAGE_KEY } from '../helpers/token';

it('will set assigned filter', async () => {
const resource = new Location(mockAxios);
Expand Down Expand Up @@ -223,17 +224,41 @@ it('can supply suggestions based on the provided query', async () => {

expect(mockAxios.get).toHaveBeenCalledTimes(1);
expect(mockAxios.get).toHaveBeenCalledWith('location-suggestions', {
headers: {
'x-location-details-token': sessionStorage.getItem('location-details-token'),
},
params: {
'filter[query]': 'Fake City',
},
});
});

it('will reuse a token if it is already in session storage when fetching suggestions', async () => {
sessionStorage.setItem(STORAGE_KEY, 'value');

const resource = new Location(mockAxios);

await resource.suggest('Fake City');

expect(mockAxios.get).toHaveBeenCalledTimes(1);
expect(mockAxios.get).toHaveBeenCalledWith('location-suggestions', {
headers: {
'x-location-details-token': 'value',
},
params: {
'filter[query]': 'Fake City',
},
});
});

it('can fetch details for a given location identifier received from a suggestion', async () => {
sessionStorage.setItem(STORAGE_KEY, 'value');

const resource = new Location(mockAxios);

await resource.details('random_string_of_characters');

expect(mockAxios.get).toHaveBeenCalledTimes(1);
expect(mockAxios.get).toHaveBeenCalledWith('location-details/random_string_of_characters');
expect(sessionStorage.getItem(STORAGE_KEY)).toBeNull();
});
9 changes: 8 additions & 1 deletion src/resources/location.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AxiosInstance } from 'axios';

import { combine } from '../helpers/filters';
import { remove, retrieve } from '../helpers/token';
import { Filterable, Pageable } from '../index';
import Conditional, { ConditionalResource } from './conditional';

Expand Down Expand Up @@ -83,6 +84,8 @@ export default class Location extends Conditional implements LocationResource {
}

public async details(identifier: string): Promise<any> {
remove();

return await this.client.get(`location-details/${identifier}`);
}

Expand Down Expand Up @@ -150,9 +153,13 @@ export default class Location extends Conditional implements LocationResource {
}

public async suggest(query: string): Promise<any> {
const headers = {
'x-location-details-token': retrieve(),
};

const params: Filterable<LocationFilter> = combine({}, { query });

return await this.client.get('location-suggestions', { params });
return await this.client.get('location-suggestions', { headers, params });
}

public take(limit: number): this {
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.8.tgz#d0a3ab5a6e61458c492304e2776ac136b81db927"
integrity sha512-INamyRZG4rW3lDCUmwVd5Xho/bXvQm/v1yP8V0UN1RuInU7RoWoaO570b+yLX4Ia/0szsx1wa8VzcsVlsvbWLA==

"@types/uuid@^3.4.5":
version "3.4.5"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-3.4.5.tgz#d4dc10785b497a1474eae0ba7f0cb09c0ddfd6eb"
integrity sha512-MNL15wC3EKyw1VLF+RoVO4hJJdk9t/Hlv3rt1OL65Qvuadm4BYo6g9ZJQqoq7X8NBFSsQXgAujWciovh2lpVjA==
dependencies:
"@types/node" "*"

abab@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.0.tgz#aba0ab4c5eee2d4c79d3487d85450fb2376ebb0f"
Expand Down Expand Up @@ -3612,6 +3619,11 @@ uuid@^3.3.2:
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.2.tgz#1b4af4955eb3077c501c23872fc6513811587131"
integrity sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==

uuid@^3.3.3:
version "3.3.3"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.3.tgz#4568f0216e78760ee1dbf3a4d2cf53e224112866"
integrity sha512-pW0No1RGHgzlpHJO1nsVrHKpOEIxkGg1xB+v0ZmdNH5OAeAwzAVrCnI2/6Mtx+Uys6iaylxa+D3g4j63IKKjSQ==

validate-npm-package-license@^3.0.1:
version "3.0.4"
resolved "https://registry.yarnpkg.com/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz#fc91f6b9c7ba15c857f4cb2c5defeec39d4f410a"
Expand Down

0 comments on commit 72a25ce

Please sign in to comment.