parrot-fetch
Advanced tools
Comparing version 3.1.0 to 4.0.1
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,33 +6,43 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.PARROT_STATE = undefined; | ||
exports.default = init; | ||
exports.PARROT_STATE = void 0; | ||
var _ParrotFetch = require('./ParrotFetch'); | ||
var _ParrotFetch = _interopRequireDefault(require("./ParrotFetch")); | ||
var _ParrotFetch2 = _interopRequireDefault(_ParrotFetch); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/* | ||
* Copyright (c) 2018 American Express Travel Related Services Company, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
/* istanbul ignore next */ | ||
var context = typeof global === 'undefined' ? self : global; /* | ||
* Copyright (c) 2018 American Express Travel Related Services Company, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
var context = typeof global === 'undefined' ? self : global; // eslint-disable-line no-restricted-globals | ||
var contextFetch = context.fetch.bind(context); | ||
var PARROT_STATE = exports.PARROT_STATE = 'PARROT_STATE'; | ||
var PARROT_STATE = 'PARROT_STATE'; | ||
exports.PARROT_STATE = PARROT_STATE; | ||
function init(scenarios) { | ||
var parrotFetch = new _ParrotFetch2.default(scenarios, contextFetch); | ||
context.fetch = parrotFetch.resolve; | ||
function init(scenarios, fetchWrapperParam) { | ||
var fetchWrapper = fetchWrapperParam; | ||
var parrotFetch; // option to mock a fetchClient that is different from the global fetch by passing the fetchClient in through a fetchWrapper object. | ||
if (fetchWrapper) { | ||
parrotFetch = new _ParrotFetch.default(scenarios, fetchWrapper.fetchClient); | ||
fetchWrapper.fetchClient = parrotFetch.resolve; | ||
} else { | ||
parrotFetch = new _ParrotFetch.default(scenarios, contextFetch); | ||
context.fetch = parrotFetch.resolve; | ||
} | ||
return parrotFetch; | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,91 +6,65 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.default = void 0; | ||
var _stringify = require('babel-runtime/core-js/json/stringify'); | ||
var _parrotCore = _interopRequireDefault(require("parrot-core")); | ||
var _stringify2 = _interopRequireDefault(_stringify); | ||
var _urlParse = _interopRequireDefault(require("url-parse")); | ||
var _promise = require('babel-runtime/core-js/promise'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _promise2 = _interopRequireDefault(_promise); | ||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
var _keys = require('babel-runtime/core-js/object/keys'); | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
var _keys2 = _interopRequireDefault(_keys); | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
var _extends2 = require('babel-runtime/helpers/extends'); | ||
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } | ||
var _extends3 = _interopRequireDefault(_extends2); | ||
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } | ||
var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties'); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2); | ||
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } | ||
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); | ||
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } | ||
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } | ||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } | ||
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); | ||
var ParrotFetch = | ||
/*#__PURE__*/ | ||
function (_Parrot) { | ||
_inherits(ParrotFetch, _Parrot); | ||
var _inherits2 = require('babel-runtime/helpers/inherits'); | ||
var _inherits3 = _interopRequireDefault(_inherits2); | ||
var _parrotCore = require('parrot-core'); | ||
var _parrotCore2 = _interopRequireDefault(_parrotCore); | ||
var _urlParse = require('url-parse'); | ||
var _urlParse2 = _interopRequireDefault(_urlParse); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/* | ||
* Copyright (c) 2018 American Express Travel Related Services Company, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
var ParrotFetch = function (_Parrot) { | ||
(0, _inherits3.default)(ParrotFetch, _Parrot); | ||
function ParrotFetch(scenarios, contextFetch) { | ||
(0, _classCallCheck3.default)(this, ParrotFetch); | ||
var _this; | ||
var _this = (0, _possibleConstructorReturn3.default)(this, (ParrotFetch.__proto__ || (0, _getPrototypeOf2.default)(ParrotFetch)).call(this, scenarios)); | ||
_classCallCheck(this, ParrotFetch); | ||
_this.normalizeRequest = function (input) { | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
_this = _possibleConstructorReturn(this, _getPrototypeOf(ParrotFetch).call(this, scenarios)); | ||
var _ref$method = _ref.method, | ||
method = _ref$method === undefined ? 'GET' : _ref$method, | ||
init = (0, _objectWithoutProperties3.default)(_ref, ['method']); | ||
_defineProperty(_assertThisInitialized(_this), "normalizeRequest", function (input) { | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
_ref$method = _ref.method, | ||
method = _ref$method === void 0 ? 'GET' : _ref$method, | ||
init = _objectWithoutProperties(_ref, ["method"]); | ||
var _parse = (0, _urlParse2.default)(input, true), | ||
var _parse = (0, _urlParse.default)(input, true), | ||
path = _parse.pathname, | ||
query = _parse.query, | ||
parsed = (0, _objectWithoutProperties3.default)(_parse, ['pathname', 'query']); | ||
parsed = _objectWithoutProperties(_parse, ["pathname", "query"]); | ||
return (0, _extends3.default)({}, init, parsed, { | ||
return _objectSpread({}, init, {}, parsed, { | ||
path: path, | ||
query: (0, _keys2.default)(query) && query, | ||
query: Object.keys(query) && query, | ||
method: method && method.toUpperCase() | ||
}); | ||
}; | ||
}); | ||
_this.resolver = function (input, init) { | ||
_defineProperty(_assertThisInitialized(_this), "resolver", function (input, init) { | ||
return function (response) { | ||
@@ -100,8 +74,11 @@ if (!response) { | ||
} | ||
var body = response.body, | ||
status = response.status; | ||
return _promise2.default.resolve(new Response(new Blob([(0, _stringify2.default)(body)], { status: status, type: 'application/json' }))); | ||
return Promise.resolve(new Response(new Blob([JSON.stringify(body)], { | ||
status: status, | ||
type: 'application/json' | ||
}))); | ||
}; | ||
}; | ||
}); | ||
@@ -113,5 +90,5 @@ _this.contextFetch = contextFetch; | ||
return ParrotFetch; | ||
}(_parrotCore2.default); | ||
}(_parrotCore.default); | ||
exports.default = ParrotFetch; | ||
module.exports = exports['default']; | ||
var _default = ParrotFetch; | ||
exports.default = _default; |
{ | ||
"name": "parrot-fetch", | ||
"version": "3.1.0", | ||
"version": "4.0.1", | ||
"contributors": [ | ||
@@ -23,10 +23,12 @@ "Jack Cross <jack.cross1@aexp.com>", | ||
"dependencies": { | ||
"babel-runtime": "^6.0.0", | ||
"parrot-core": "^3.1.0", | ||
"parrot-core": "^4.0.1", | ||
"url-parse": "^1.2.0" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.24.1", | ||
"rimraf": "^2.2.8" | ||
} | ||
"@babel/cli": "^7.8.4", | ||
"@babel/core": "^7.7.7", | ||
"babel-preset-amex": "^3.3.0", | ||
"rimraf": "^3.0.0" | ||
}, | ||
"gitHead": "05e11da9241f5737ada24cfeb613f4f98e2abb89" | ||
} |
@@ -1,2 +0,4 @@ | ||
# parrot-fetch | ||
<h1 align="center"> | ||
<img src="./parrot-fetch.png" alt="Parrot-Fetch" width="50%" /> | ||
</h1> | ||
@@ -16,2 +18,36 @@ parrot-fetch is an implementation of Parrot that mocks the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). | ||
parrot.setActiveScenario('has a ship log'); | ||
``` | ||
``` | ||
## Mocking a non-global fetch example | ||
```js | ||
import parrotFetch from 'parrot-fetch'; | ||
import scenarios from './scenarios'; | ||
// include a fetchClient inside of a fetchWrapper object and pass it into the parrotFetch function to mock the fetchClient | ||
const fetchWrapper = { | ||
fetchClient, | ||
}; | ||
const parrot = parrotFetch(scenarios, fetchWrapper); | ||
// set the scenario to be used | ||
parrot.setActiveScenario('has a ship log'); | ||
``` | ||
### Mocking a non-global fetch - use case example | ||
An example use case for mocking a non-global fetch could be mocking a fetchClient that is passed in as an extra thunk argument using [redux-thunk withExtraArgument](https://github.com/reduxjs/redux-thunk). Mocking this redux thunk fetchClient would look something like below: | ||
```js | ||
import parrotFetch from 'parrot-fetch'; | ||
import scenarios from './scenarios'; | ||
const callParrotFetch = () => { | ||
return (dispatch, getState, fetchWrapper) => { | ||
// fetchWrapper = { fetchClient }; | ||
return parrotFetch(scenarios, fetchWrapper); | ||
}; | ||
}; | ||
const parrot = dispatch(callParrotFetch()); | ||
parrot.setActiveScenario('has a ship log'); | ||
``` |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
19914
2
53
4
1
+ Added@babel/runtime@7.26.0(transitive)
+ Addedparrot-core@4.1.1(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)
- Removedbabel-runtime@^6.0.0
- Removedbabel-runtime@6.26.0(transitive)
- Removedcore-js@2.6.12(transitive)
- Removedparrot-core@3.1.0(transitive)
- Removedregenerator-runtime@0.11.1(transitive)
Updatedparrot-core@^4.0.1