Skip to content

Commit

Permalink
fix(types): Export TypeScript types (#8)
Browse files Browse the repository at this point in the history
When `formatUrl` (and probably other utilities) are wrapped in functions, those functions will likely take arguments that will be passed through to the utilities. This means that those arguments will need to be typed correctly. Typing as `Object` will not work, it needs to be `UrlParams` specifically.

So this PR just exports `UrlParams`, `UrlValue` & `NullableUrlParams` so that they can be used as necessary. I added an integration test for this as well. The test failed with `Object` and passes with the newly exported `UrlParams`.
  • Loading branch information
benmvp authored Jul 17, 2019
1 parent 3c712eb commit a85287a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
20 changes: 20 additions & 0 deletions integration-tests/src/tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getCacheDefeatStr,
parseQuery,
parseUrl,
UrlParams,
} from 'url-lib'

describe('formatQuery', () => {
Expand Down Expand Up @@ -149,6 +150,25 @@ describe('formatUrl', () => {

expect(url).toEqual('http://www.benmvp.com/search?sort=popular&type=all&results=20&category=holiday')
})

it('properly types function wrappers', () => {
const API_BASE = 'http://api.benmvp.com'
const buildUrl = (apiName: string, command: string, params: UrlParams): string => {
const apiUrl = formatUrl(`${API_BASE}/${apiName}`, [
{
cmd: command,
key: 'MW9S-E7SL-26DU-VV8V',
},
params,
])

return apiUrl
}

expect(
buildUrl('events', 'get', {q: 'javascript', pg: 2})
).toEqual('http://api.benmvp.com/events?cmd=get&key=MW9S-E7SL-26DU-VV8V&q=javascript&pg=2')
})
})

describe('getCacheDefeatStr', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export {default as parseQuery} from './parseQuery'
export {default as parseUrl} from './parseUrl'
export {default as formatQuery} from './formatQuery'
export {default as formatUrl} from './formatUrl'

export {UrlParamValue, UrlParams, NullableUrlParams} from './types'

0 comments on commit a85287a

Please sign in to comment.