Comparing version 0.8.0 to 0.9.0
@@ -7,2 +7,6 @@ 'use strict'; | ||
var _regeneratorRuntime = _interopDefault(require('babel-runtime/regenerator')); | ||
var _extends = _interopDefault(require('babel-runtime/helpers/extends')); | ||
var _asyncToGenerator = _interopDefault(require('babel-runtime/helpers/asyncToGenerator')); | ||
var _typeof = _interopDefault(require('babel-runtime/helpers/typeof')); | ||
var axios = _interopDefault(require('axios')); | ||
@@ -12,19 +16,14 @@ var R = _interopDefault(require('ramda')); | ||
var _extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
var _this = undefined; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
// check for an invalid config | ||
var isInvalidConfig = R.anyPass([R.isNil, R.isEmpty, R.complement(R.has('baseURL')), R.complement(R.propIs(String, 'baseURL')), R.propSatisfies(R.isEmpty, 'baseURL')]); | ||
return target; | ||
/** | ||
* Are we dealing with a promise? | ||
*/ | ||
var isPromise = function isPromise(obj) { | ||
return !!obj && ((typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object' || typeof obj === 'function') && typeof obj.then === 'function'; | ||
}; | ||
// check for an invalid config | ||
var isInvalidConfig = R.anyPass([R.isNil, R.isEmpty, R.complement(R.has('baseURL')), R.complement(R.propIs(String, 'baseURL')), R.propSatisfies(R.isEmpty, 'baseURL')]); | ||
// the default headers given to axios | ||
@@ -76,2 +75,3 @@ var DEFAULT_HEADERS = { | ||
var requestTransforms = []; | ||
var asyncRequestTransforms = []; | ||
var responseTransforms = []; | ||
@@ -82,2 +82,5 @@ | ||
}; | ||
var addAsyncRequestTransform = function addAsyncRequestTransform(transform) { | ||
return asyncRequestTransforms.push(transform); | ||
}; | ||
var addResponseTransform = function addResponseTransform(transform) { | ||
@@ -103,2 +106,17 @@ return responseTransforms.push(transform); | ||
/** | ||
* Sets a new base URL. | ||
*/ | ||
var setBaseURL = function setBaseURL(newURL) { | ||
instance.defaults.baseURL = newURL; | ||
return instance; | ||
}; | ||
/** | ||
* Gets the current base URL used by axios. | ||
*/ | ||
var getBaseURL = function getBaseURL() { | ||
return instance.defaults.baseURL; | ||
}; | ||
/** | ||
Make the request for GET, HEAD, DELETE | ||
@@ -126,29 +144,77 @@ */ | ||
*/ | ||
var doRequest = function doRequest(axiosRequestConfig) { | ||
var startedAt = RS.toNumber(new Date()); | ||
var doRequest = function () { | ||
var _ref = _asyncToGenerator(_regeneratorRuntime.mark(function _callee(axiosRequestConfig) { | ||
var index, transform, chain; | ||
return _regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
axiosRequestConfig.headers = _extends({}, headers, axiosRequestConfig.headers); | ||
axiosRequestConfig.headers = _extends({}, headers, axiosRequestConfig.headers); | ||
// add the request transforms | ||
if (requestTransforms.length > 0) { | ||
(function () { | ||
// create an object to feed through the request transforms | ||
var request = R.pick(['url', 'method', 'data', 'headers', 'params'], axiosRequestConfig); | ||
// add the request transforms | ||
if (requestTransforms.length > 0) { | ||
// overwrite our axios request with whatever our object looks like now | ||
// axiosRequestConfig = doRequestTransforms(requestTransforms, axiosRequestConfig) | ||
R.forEach(function (transform) { | ||
return transform(axiosRequestConfig); | ||
}, requestTransforms); | ||
} | ||
// go go go! | ||
R.forEach(function (transform) { | ||
return transform(request); | ||
}, requestTransforms); | ||
// add the async request transforms | ||
// overwrite our axios request with whatever our object looks like now | ||
axiosRequestConfig = R.merge(axiosRequestConfig, request); | ||
})(); | ||
} | ||
if (!(asyncRequestTransforms.length > 0)) { | ||
_context.next = 16; | ||
break; | ||
} | ||
// first convert the axios response, then execute our callback | ||
var chain = R.pipe(R.partial(convertResponse, [startedAt]), runMonitors); | ||
index = 0; | ||
// Make the request and execute the identical pipeline for both promise paths. | ||
return instance.request(axiosRequestConfig).then(chain).catch(chain); | ||
}; | ||
case 4: | ||
if (!(index < asyncRequestTransforms.length)) { | ||
_context.next = 16; | ||
break; | ||
} | ||
transform = asyncRequestTransforms[index](axiosRequestConfig); | ||
if (!isPromise(transform)) { | ||
_context.next = 11; | ||
break; | ||
} | ||
_context.next = 9; | ||
return transform; | ||
case 9: | ||
_context.next = 13; | ||
break; | ||
case 11: | ||
_context.next = 13; | ||
return transform(axiosRequestConfig); | ||
case 13: | ||
index++; | ||
_context.next = 4; | ||
break; | ||
case 16: | ||
// after the call, convert the axios response, then execute our monitors | ||
chain = R.pipe(R.partial(convertResponse, [RS.toNumber(new Date())]), runMonitors); | ||
return _context.abrupt('return', instance.request(axiosRequestConfig).then(chain).catch(chain)); | ||
case 18: | ||
case 'end': | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, _this); | ||
})); | ||
return function doRequest(_x5) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
/** | ||
@@ -159,5 +225,5 @@ Fires after we convert from axios' response into our response. Exceptions | ||
var runMonitors = function runMonitors(ourResponse) { | ||
monitors.forEach(function (fn) { | ||
monitors.forEach(function (monitor) { | ||
try { | ||
fn(ourResponse); | ||
monitor(ourResponse); | ||
} catch (error) { | ||
@@ -229,4 +295,6 @@ // all monitor complaints will be ignored | ||
requestTransforms: requestTransforms, | ||
asyncRequestTransforms: asyncRequestTransforms, | ||
responseTransforms: responseTransforms, | ||
addRequestTransform: addRequestTransform, | ||
addAsyncRequestTransform: addAsyncRequestTransform, | ||
addResponseTransform: addResponseTransform, | ||
@@ -236,2 +304,4 @@ setHeader: setHeader, | ||
headers: headers, | ||
setBaseURL: setBaseURL, | ||
getBaseURL: getBaseURL, | ||
get: R.partial(doRequestWithoutBody, ['get']), | ||
@@ -238,0 +308,0 @@ delete: R.partial(doRequestWithoutBody, ['delete']), |
{ | ||
"name": "apisauce", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"description": "A JSON API client for JavaScript. No, no. Not like that. Like this.", | ||
@@ -33,2 +33,3 @@ "repository": { | ||
"axios": "^0.15.3", | ||
"babel-runtime": "^6.22.0", | ||
"ramda": "^0.23.0", | ||
@@ -38,8 +39,9 @@ "ramdasauce": "^1.1.1" | ||
"devDependencies": { | ||
"ava": "^0.17.0", | ||
"ava": "^0.18.1", | ||
"babel-cli": "^6.18.0", | ||
"babel-core": "^6.21.0", | ||
"babel-core": "^6.22.1", | ||
"babel-eslint": "^7.1.1", | ||
"babel-plugin-transform-runtime": "^6.15.0", | ||
"babel-preset-es2015": "^6.18.0", | ||
"babel-plugin-transform-async-to-generator": "^6.22.0", | ||
"babel-plugin-transform-runtime": "^6.22.0", | ||
"babel-preset-es2015": "^6.22.0", | ||
"babel-preset-es2015-rollup": "^3.0.0", | ||
@@ -46,0 +48,0 @@ "babel-preset-stage-0": "^6.16.0", |
@@ -154,2 +154,11 @@ # Apisauce | ||
## Changing Base URL | ||
You can change the URL your api is connecting to. | ||
```js | ||
api.setBaseURL('https://some.other.place.com/api/v100') | ||
console.log(`omg i am now at ${api.getBaseURL()}`) | ||
``` | ||
## Changing Headers | ||
@@ -251,2 +260,4 @@ | ||
Request transforms can be a function: | ||
```js | ||
@@ -264,3 +275,23 @@ api.addRequestTransform(request => { | ||
And you can also add an async version for use with Promises or `async/await`. When you resolve | ||
your promise, ensure you pass the request along. | ||
```js | ||
api.addAsyncRequestTransform(request => { | ||
return new Promise(resolve => setTimeout(resolve, 2000)) | ||
}) | ||
``` | ||
```js | ||
api.addAsyncRequestTransform(request => async () => { | ||
await AsyncStorage.load('something') | ||
request. | ||
}}) | ||
``` | ||
This is great if you need to fetch an API key from storage for example. | ||
Multiple async transforms will be run one at a time in succession, not parallel. | ||
# Using Async/Await | ||
@@ -305,2 +336,8 @@ | ||
### 0.9.0 - February 6, 2017 | ||
* [NEW] Adds async request transforms. - #31 by @skibz | ||
* [NEW] Adds a way to change the base URL of an API. - #55 by @skellock | ||
* [NEW] Upgrades dependencies including an [odd corner case](https://github.com/skellock/ramdasauce/pull/7). - #52 by @skellock | ||
### 0.8.0 - January 15, 2017 | ||
@@ -307,0 +344,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25271
282
402
4
16
+ Addedbabel-runtime@^6.22.0
+ Addedbabel-runtime@6.26.0(transitive)
+ Addedcore-js@2.6.12(transitive)
+ Addedregenerator-runtime@0.11.1(transitive)