Skip to content

Commit

Permalink
feat: add getQueryParam utility method to support pagination (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Kistler committed Mar 13, 2021
1 parent bb87d92 commit d4f067a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ export function stripTrailingSlash(url: string): string {
return url.replace(/\/$/, '');
}

/**
* Return a query parameter value from a URL
*
* @param {string} urlStr - the url string.
* @param {string} param - the name of the query parameter
* whose value should be returned
* @returns {string} the value of the `param` query parameter
* @throws if urlStr is an invalid URL
*/
export function getQueryParam(urlStr: string, param: string): string {
// The base URL is a dummy value just so we can process relative URLs
const url = new URL(urlStr, 'https://foo.bar');
return url.searchParams.get(param);
}

/**
* Validates that all required params are provided
* @param params - the method parameters.
Expand Down
52 changes: 52 additions & 0 deletions test/unit/get-query-param.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

const { getQueryParam } = require('../../dist/lib/helper');

describe('getQueryParam', function() {
it('should return the parm value from a relative URL', function() {
const nextUrl = '/api/v1/offerings?start=foo&limit=10';
expect(getQueryParam(nextUrl, 'start')).toBe('foo');
});

it('should return the param value from an absolute URL', function() {
const nextUrl = 'https://acme.com/api/v1/offerings?start=bar&limit=10';
expect(getQueryParam(nextUrl, 'start')).toBe('bar');
});

it('should return null when the requested param is not present', function() {
const nextUrl = 'https://acme.com/api/v1/offerings?start=bar&limit=10';
expect(getQueryParam(nextUrl, 'token')).toBeNull();
});

it('should return null when urlStr is null', function() {
const nextUrl = null;
expect(getQueryParam(nextUrl, 'start')).toBeNull();
});

it('should return null when urlStr is the empty string', function() {
const nextUrl = '';
expect(getQueryParam(nextUrl, 'start')).toBeNull();
});

it('should return null when urlStr has no query string', function() {
const nextUrl = '/api/v1/offerings';
expect(getQueryParam(nextUrl, 'start')).toBeNull();
});

it('should throw and exception when urlStr is an invalid URL', function() {
const nextUrl = 'https://foo.bar:baz/api/v1/offerings?start=foo';
expect(() => {
getQueryParam(nextUrl, 'start');
}).toThrow(/Invalid URL/);
});

it('should return null when the query string is invalid', function() {
const nextUrl = '/api/v1/offerings?start%XXfoo';
expect(getQueryParam(nextUrl, 'start')).toBeNull();
});

it('should return the first value when the query string has duplicate parameters', function() {
const nextUrl = '/api/v1/offerings?start=foo&start=bar&limit=10';
expect(getQueryParam(nextUrl, 'start')).toBe('foo');
});
});

0 comments on commit d4f067a

Please sign in to comment.