Skip to content

Commit

Permalink
Merge pull request #125 from groupon/abort
Browse files Browse the repository at this point in the history
feat: support AbortController signal
  • Loading branch information
aaarichter authored Jul 13, 2021
2 parents 23c1c97 + 317c604 commit 4d6e6a1
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 171 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node-version: [12.x, 14.x] # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
node-version: [12.x, 14.x, 16.x] # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v2
Expand Down
12 changes: 0 additions & 12 deletions .travis.yml

This file was deleted.

1 change: 1 addition & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const { fetch } = require('gofer');
* `keepAlive`: if set to `true`, enables HTTP keep-alive
⚠ Enabling `keepAlive` can lead to `MaxListenersExceededWarning: Possible EventEmitter memory leak detected.` warnings.
* `captureAsyncStack`: Extends error trace with stack trace before call. (Default: `false`) **Capturing the stack is expensive! Set only for debugging purposes**
* `signal` - AbortController signal (Node 15+)

[basicauth]: https://en.wikipedia.org/wiki/Basic_access_authentication
[tls]: https://nodejs.org/api/tls.html#tls_new_tls_tlssocket_socket_options
Expand Down
2 changes: 2 additions & 0 deletions lib/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ function fetchUrlObj(urlObj, options) {
updateSearch(urlObj.searchParams, qs);

const method = options.method || 'GET';

return request({
agent,
protocol: urlObj.protocol,
Expand All @@ -241,6 +242,7 @@ function fetchUrlObj(urlObj, options) {
methodName: options.methodName || method.toLowerCase(),
pathParams: options.pathParams,
captureAsyncStack: options.captureAsyncStack,
signal: options.signal,
});
}

Expand Down
14 changes: 7 additions & 7 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
'use strict';

/**
* @typedef {string | QSVal[] | { [name: string]: QSVal }} QSVal
*
* @param {URLSearchParams} query
* @param {{ [name: string]: QSVal }} qs
* @param {Record<string, any> | URLSearchParams} qs
* @param {Array<string>} [path]
*/
function updateSearch(query, qs, path = []) {
if (qs instanceof URLSearchParams) {
for (const [key, val] of qs.entries()) query.append(key, val);
for (const [key, val] of qs.entries()) {
query.append(key, val);
}
return query;
}

Expand All @@ -58,8 +58,8 @@ function updateSearch(query, qs, path = []) {
exports.updateSearch = updateSearch;

/**
* @param {string} pathname
* @param {{ [name: string]: string }} pathParams
* @param {URL.pathname} pathname
* @param {Record<string, string>} pathParams
*/
function replacePathParams(pathname, pathParams) {
pathParams = pathParams || {};
Expand All @@ -78,6 +78,6 @@ function replacePathParams(pathname, pathParams) {
return encodeURIComponent(value);
}

return pathname.replace(/{(\w+)}|%7B(\w+)%7D/g, onPlaceHolder);
return (pathname || '').replace(/{(\w+)}|%7B(\w+)%7D/g, onPlaceHolder);
}
exports.replacePathParams = replacePathParams;
Loading

0 comments on commit 4d6e6a1

Please sign in to comment.