@atlaskit/util-service-support
Advanced tools
Comparing version 6.0.2 to 6.0.3
# @atlaskit/util-service-support | ||
## 6.0.3 | ||
### Patch Changes | ||
- [`d3265f19be`](https://bitbucket.org/atlassian/atlassian-frontend/commits/d3265f19be) - Transpile packages using babel rather than tsc | ||
## 6.0.2 | ||
@@ -4,0 +10,0 @@ |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _exportNames = { | ||
utils: true | ||
}; | ||
exports.utils = void 0; | ||
var tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./types"), exports); | ||
tslib_1.__exportStar(require("./serviceResources"), exports); | ||
var serviceUtils = tslib_1.__importStar(require("./serviceUtils")); | ||
exports.utils = serviceUtils; | ||
//# sourceMappingURL=index.js.map | ||
var _types = require("./types"); | ||
Object.keys(_types).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; | ||
if (key in exports && exports[key] === _types[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function get() { | ||
return _types[key]; | ||
} | ||
}); | ||
}); | ||
var _serviceResources = require("./serviceResources"); | ||
Object.keys(_serviceResources).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; | ||
if (key in exports && exports[key] === _serviceResources[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function get() { | ||
return _serviceResources[key]; | ||
} | ||
}); | ||
}); | ||
var serviceUtils = _interopRequireWildcard(require("./serviceUtils")); | ||
var utils = serviceUtils; | ||
exports.utils = utils; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.AbstractResource = void 0; | ||
var AbstractResource = /** @class */ (function () { | ||
function AbstractResource() { | ||
this.listeners = new Set(); | ||
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); | ||
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
var AbstractResource = /*#__PURE__*/function () { | ||
function AbstractResource() { | ||
(0, _classCallCheck2.default)(this, AbstractResource); | ||
(0, _defineProperty2.default)(this, "listeners", new Set()); | ||
} | ||
(0, _createClass2.default)(AbstractResource, [{ | ||
key: "subscribe", | ||
value: function subscribe(onChange) { | ||
this.listeners.add(onChange); | ||
if (this.lastResult) { | ||
// Notify subscribe of last result (i.e. initial state) | ||
onChange.result(this.lastResult); | ||
} | ||
} | ||
AbstractResource.prototype.subscribe = function (onChange) { | ||
this.listeners.add(onChange); | ||
if (this.lastResult) { | ||
// Notify subscribe of last result (i.e. initial state) | ||
onChange.result(this.lastResult); | ||
}, { | ||
key: "unsubscribe", | ||
value: function unsubscribe(onChange) { | ||
this.listeners.delete(onChange); | ||
} | ||
}, { | ||
key: "notifyResult", | ||
value: function notifyResult(result) { | ||
this.listeners.forEach(function (onChange) { | ||
onChange.result(result); | ||
}); | ||
} | ||
}, { | ||
key: "notifyError", | ||
value: function notifyError(error) { | ||
this.listeners.forEach(function (onChange) { | ||
if (onChange.error) { | ||
onChange.error(error); | ||
} | ||
}; | ||
AbstractResource.prototype.unsubscribe = function (onChange) { | ||
this.listeners.delete(onChange); | ||
}; | ||
AbstractResource.prototype.notifyResult = function (result) { | ||
this.listeners.forEach(function (onChange) { | ||
onChange.result(result); | ||
}); | ||
}; | ||
AbstractResource.prototype.notifyError = function (error) { | ||
this.listeners.forEach(function (onChange) { | ||
if (onChange.error) { | ||
onChange.error(error); | ||
} | ||
}); | ||
}; | ||
AbstractResource.prototype.notifyInfo = function (info) { | ||
this.listeners.forEach(function (onChange) { | ||
if (onChange.info) { | ||
onChange.info(info); | ||
} | ||
}); | ||
}; | ||
AbstractResource.prototype.notifyNotReady = function () { | ||
this.listeners.forEach(function (onChange) { | ||
if (onChange.notReady) { | ||
onChange.notReady(); | ||
} | ||
}); | ||
}; | ||
return AbstractResource; | ||
}()); | ||
exports.AbstractResource = AbstractResource; | ||
//# sourceMappingURL=serviceResources.js.map | ||
}); | ||
} | ||
}, { | ||
key: "notifyInfo", | ||
value: function notifyInfo(info) { | ||
this.listeners.forEach(function (onChange) { | ||
if (onChange.info) { | ||
onChange.info(info); | ||
} | ||
}); | ||
} | ||
}, { | ||
key: "notifyNotReady", | ||
value: function notifyNotReady() { | ||
this.listeners.forEach(function (onChange) { | ||
if (onChange.notReady) { | ||
onChange.notReady(); | ||
} | ||
}); | ||
} | ||
}]); | ||
return AbstractResource; | ||
}(); | ||
exports.AbstractResource = AbstractResource; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.requestService = void 0; | ||
var tslib_1 = require("tslib"); | ||
var URL = tslib_1.__importStar(require("url")); | ||
var url_search_params_1 = tslib_1.__importDefault(require("url-search-params")); // IE, Safari, Mobile Chrome, Mobile Safari | ||
var types_1 = require("./types"); | ||
var URLSearchParams = url_search_params_1.default.default || url_search_params_1.default; | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
var URL = _interopRequireWildcard(require("url")); | ||
var _urlSearchParams = _interopRequireDefault(require("url-search-params")); | ||
var _types = require("./types"); | ||
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; } | ||
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) { (0, _defineProperty2.default)(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 URLSearchParams = _urlSearchParams.default.default || _urlSearchParams.default; | ||
var defaultRequestServiceOptions = {}; | ||
var buildUrl = function (baseUrl, path, queryParams, secOptions) { | ||
if (path === void 0) { path = ''; } | ||
var searchParam = new URLSearchParams(URL.parse(baseUrl).search || undefined); | ||
baseUrl = baseUrl.split('?')[0]; | ||
if (queryParams) { | ||
for (var key in queryParams) { | ||
if ({}.hasOwnProperty.call(queryParams, key)) { | ||
searchParam.append(key, queryParams[key]); | ||
} | ||
} | ||
var buildUrl = function buildUrl(baseUrl) { | ||
var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
var queryParams = arguments.length > 2 ? arguments[2] : undefined; | ||
var secOptions = arguments.length > 3 ? arguments[3] : undefined; | ||
var searchParam = new URLSearchParams(URL.parse(baseUrl).search || undefined); | ||
baseUrl = baseUrl.split('?')[0]; | ||
if (queryParams) { | ||
for (var key in queryParams) { | ||
if ({}.hasOwnProperty.call(queryParams, key)) { | ||
searchParam.append(key, queryParams[key]); | ||
} | ||
} | ||
if (secOptions && secOptions.params) { | ||
for (var key in secOptions.params) { | ||
if ({}.hasOwnProperty.call(secOptions.params, key)) { | ||
var values = secOptions.params[key]; | ||
if (Array.isArray(values)) { | ||
for (var i = 0; i < values.length; i++) { | ||
searchParam.append(key, values[i]); | ||
} | ||
} | ||
else { | ||
searchParam.append(key, values); | ||
} | ||
} | ||
} | ||
if (secOptions && secOptions.params) { | ||
for (var _key in secOptions.params) { | ||
if ({}.hasOwnProperty.call(secOptions.params, _key)) { | ||
var values = secOptions.params[_key]; | ||
if (Array.isArray(values)) { | ||
for (var i = 0; i < values.length; i++) { | ||
searchParam.append(_key, values[i]); | ||
} | ||
} else { | ||
searchParam.append(_key, values); | ||
} | ||
} | ||
} | ||
var seperator = ''; | ||
if (path && baseUrl.substr(-1) !== '/') { | ||
seperator = '/'; | ||
} | ||
var params = searchParam.toString(); | ||
if (params) { | ||
params = '?' + params; | ||
} | ||
return "" + baseUrl + seperator + path + params; | ||
} | ||
var seperator = ''; | ||
if (path && baseUrl.substr(-1) !== '/') { | ||
seperator = '/'; | ||
} | ||
var params = searchParam.toString(); | ||
if (params) { | ||
params = '?' + params; | ||
} | ||
return "".concat(baseUrl).concat(seperator).concat(path).concat(params); | ||
}; | ||
var addToHeaders = function (headers, keyValues) { | ||
if (keyValues) { | ||
for (var key in keyValues) { | ||
if ({}.hasOwnProperty.call(keyValues, key)) { | ||
var values = keyValues[key]; | ||
if (Array.isArray(values)) { | ||
for (var i = 0; i < values.length; i++) { | ||
headers[key] = values[i]; | ||
} | ||
} | ||
else { | ||
headers[key] = values; | ||
} | ||
} | ||
var addToHeaders = function addToHeaders(headers, keyValues) { | ||
if (keyValues) { | ||
for (var key in keyValues) { | ||
if ({}.hasOwnProperty.call(keyValues, key)) { | ||
var values = keyValues[key]; | ||
if (Array.isArray(values)) { | ||
for (var i = 0; i < values.length; i++) { | ||
headers[key] = values[i]; | ||
} | ||
} else { | ||
headers[key] = values; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
var buildHeaders = function (secOptions, extraHeaders) { | ||
var headers = {}; | ||
addToHeaders(headers, extraHeaders); | ||
if (secOptions) { | ||
addToHeaders(headers, secOptions.headers); | ||
} | ||
return headers; | ||
var buildHeaders = function buildHeaders(secOptions, extraHeaders) { | ||
var headers = {}; | ||
addToHeaders(headers, extraHeaders); | ||
if (secOptions) { | ||
addToHeaders(headers, secOptions.headers); | ||
} | ||
return headers; | ||
}; | ||
@@ -74,34 +104,49 @@ /** | ||
*/ | ||
exports.requestService = function (serviceConfig, options) { | ||
var url = serviceConfig.url, securityProvider = serviceConfig.securityProvider, refreshedSecurityProvider = serviceConfig.refreshedSecurityProvider; | ||
var _a = options || defaultRequestServiceOptions, path = _a.path, queryParams = _a.queryParams, requestInit = _a.requestInit; | ||
var secOptions = securityProvider && securityProvider(); | ||
var requestUrl = buildUrl(url, path, queryParams, secOptions); | ||
var headers = buildHeaders(secOptions, requestInit && requestInit.headers); | ||
var credentials = types_1.buildCredentials(secOptions); | ||
var requestOptions = tslib_1.__assign(tslib_1.__assign({}, requestInit), { headers: headers, | ||
credentials: credentials }); | ||
return fetch(requestUrl, requestOptions).then(function (response) { | ||
if (response.status === 204) { | ||
return Promise.resolve(); | ||
} | ||
else if (response.ok) { | ||
return response.json(); | ||
} | ||
else if (response.status === 401 && refreshedSecurityProvider) { | ||
// auth issue - try once | ||
return refreshedSecurityProvider().then(function (newSecOptions) { | ||
var retryServiceConfig = { | ||
url: url, | ||
securityProvider: function () { return newSecOptions; }, | ||
}; | ||
return exports.requestService(retryServiceConfig, options); | ||
}); | ||
} | ||
return Promise.reject({ | ||
code: response.status, | ||
reason: response.statusText, | ||
}); | ||
var requestService = function requestService(serviceConfig, options) { | ||
var url = serviceConfig.url, | ||
securityProvider = serviceConfig.securityProvider, | ||
refreshedSecurityProvider = serviceConfig.refreshedSecurityProvider; | ||
var _ref = options || defaultRequestServiceOptions, | ||
path = _ref.path, | ||
queryParams = _ref.queryParams, | ||
requestInit = _ref.requestInit; | ||
var secOptions = securityProvider && securityProvider(); | ||
var requestUrl = buildUrl(url, path, queryParams, secOptions); | ||
var headers = buildHeaders(secOptions, requestInit && requestInit.headers); | ||
var credentials = (0, _types.buildCredentials)(secOptions); | ||
var requestOptions = _objectSpread(_objectSpread({}, requestInit), {}, { | ||
headers: headers, | ||
credentials: credentials | ||
}); | ||
return fetch(requestUrl, requestOptions).then(function (response) { | ||
if (response.status === 204) { | ||
return Promise.resolve(); | ||
} else if (response.ok) { | ||
return response.json(); | ||
} else if (response.status === 401 && refreshedSecurityProvider) { | ||
// auth issue - try once | ||
return refreshedSecurityProvider().then(function (newSecOptions) { | ||
var retryServiceConfig = { | ||
url: url, | ||
securityProvider: function securityProvider() { | ||
return newSecOptions; | ||
} | ||
}; | ||
return requestService(retryServiceConfig, options); | ||
}); | ||
} | ||
return Promise.reject({ | ||
code: response.status, | ||
reason: response.statusText | ||
}); | ||
}); | ||
}; | ||
//# sourceMappingURL=serviceUtils.js.map | ||
exports.requestService = requestService; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.buildCredentials = void 0; | ||
exports.buildCredentials = function (secOptions) { | ||
return secOptions && secOptions.omitCredentials ? 'omit' : 'include'; | ||
/** | ||
* Defines a typical Resource. | ||
* | ||
* Q = query type | ||
* R = result type | ||
* E = error type | ||
* I = info type | ||
* O = options that filter can accept | ||
*/ | ||
var buildCredentials = function buildCredentials(secOptions) { | ||
return secOptions && secOptions.omitCredentials ? 'omit' : 'include'; | ||
}; | ||
//# sourceMappingURL=types.js.map | ||
/** | ||
* Returns a promise to a SecurityOptions that has just been forcibly refreshed with a | ||
* new token. Will be used for single retry per request if a 401 is returned. | ||
*/ | ||
exports.buildCredentials = buildCredentials; |
{ | ||
"name": "@atlaskit/util-service-support", | ||
"version": "6.0.2" | ||
"version": "6.0.3" | ||
} |
export * from './types'; | ||
export * from './serviceResources'; | ||
import * as serviceUtils from './serviceUtils'; | ||
export const utils = serviceUtils; | ||
//# sourceMappingURL=index.js.map | ||
export const utils = serviceUtils; |
@@ -0,42 +1,50 @@ | ||
import _defineProperty from "@babel/runtime/helpers/defineProperty"; | ||
export class AbstractResource { | ||
constructor() { | ||
this.listeners = new Set(); | ||
constructor() { | ||
_defineProperty(this, "listeners", new Set()); | ||
} | ||
subscribe(onChange) { | ||
this.listeners.add(onChange); | ||
if (this.lastResult) { | ||
// Notify subscribe of last result (i.e. initial state) | ||
onChange.result(this.lastResult); | ||
} | ||
subscribe(onChange) { | ||
this.listeners.add(onChange); | ||
if (this.lastResult) { | ||
// Notify subscribe of last result (i.e. initial state) | ||
onChange.result(this.lastResult); | ||
} | ||
} | ||
unsubscribe(onChange) { | ||
this.listeners.delete(onChange); | ||
} | ||
notifyResult(result) { | ||
this.listeners.forEach(onChange => { | ||
onChange.result(result); | ||
}); | ||
} | ||
notifyError(error) { | ||
this.listeners.forEach(onChange => { | ||
if (onChange.error) { | ||
onChange.error(error); | ||
} | ||
}); | ||
} | ||
notifyInfo(info) { | ||
this.listeners.forEach(onChange => { | ||
if (onChange.info) { | ||
onChange.info(info); | ||
} | ||
}); | ||
} | ||
notifyNotReady() { | ||
this.listeners.forEach(onChange => { | ||
if (onChange.notReady) { | ||
onChange.notReady(); | ||
} | ||
}); | ||
} | ||
} | ||
//# sourceMappingURL=serviceResources.js.map | ||
} | ||
unsubscribe(onChange) { | ||
this.listeners.delete(onChange); | ||
} | ||
notifyResult(result) { | ||
this.listeners.forEach(onChange => { | ||
onChange.result(result); | ||
}); | ||
} | ||
notifyError(error) { | ||
this.listeners.forEach(onChange => { | ||
if (onChange.error) { | ||
onChange.error(error); | ||
} | ||
}); | ||
} | ||
notifyInfo(info) { | ||
this.listeners.forEach(onChange => { | ||
if (onChange.info) { | ||
onChange.info(info); | ||
} | ||
}); | ||
} | ||
notifyNotReady() { | ||
this.listeners.forEach(onChange => { | ||
if (onChange.notReady) { | ||
onChange.notReady(); | ||
} | ||
}); | ||
} | ||
} |
import * as URL from 'url'; | ||
import USP from 'url-search-params'; // IE, Safari, Mobile Chrome, Mobile Safari | ||
import { buildCredentials, } from './types'; | ||
import { buildCredentials } from './types'; | ||
const URLSearchParams = USP.default || USP; | ||
const defaultRequestServiceOptions = {}; | ||
const buildUrl = (baseUrl, path = '', queryParams, secOptions) => { | ||
const searchParam = new URLSearchParams(URL.parse(baseUrl).search || undefined); | ||
baseUrl = baseUrl.split('?')[0]; | ||
if (queryParams) { | ||
for (const key in queryParams) { | ||
if ({}.hasOwnProperty.call(queryParams, key)) { | ||
searchParam.append(key, queryParams[key]); | ||
} | ||
} | ||
const searchParam = new URLSearchParams(URL.parse(baseUrl).search || undefined); | ||
baseUrl = baseUrl.split('?')[0]; | ||
if (queryParams) { | ||
for (const key in queryParams) { | ||
if ({}.hasOwnProperty.call(queryParams, key)) { | ||
searchParam.append(key, queryParams[key]); | ||
} | ||
} | ||
if (secOptions && secOptions.params) { | ||
for (const key in secOptions.params) { | ||
if ({}.hasOwnProperty.call(secOptions.params, key)) { | ||
const values = secOptions.params[key]; | ||
if (Array.isArray(values)) { | ||
for (let i = 0; i < values.length; i++) { | ||
searchParam.append(key, values[i]); | ||
} | ||
} | ||
else { | ||
searchParam.append(key, values); | ||
} | ||
} | ||
} | ||
if (secOptions && secOptions.params) { | ||
for (const key in secOptions.params) { | ||
if ({}.hasOwnProperty.call(secOptions.params, key)) { | ||
const values = secOptions.params[key]; | ||
if (Array.isArray(values)) { | ||
for (let i = 0; i < values.length; i++) { | ||
searchParam.append(key, values[i]); | ||
} | ||
} else { | ||
searchParam.append(key, values); | ||
} | ||
} | ||
} | ||
let seperator = ''; | ||
if (path && baseUrl.substr(-1) !== '/') { | ||
seperator = '/'; | ||
} | ||
let params = searchParam.toString(); | ||
if (params) { | ||
params = '?' + params; | ||
} | ||
return `${baseUrl}${seperator}${path}${params}`; | ||
} | ||
let seperator = ''; | ||
if (path && baseUrl.substr(-1) !== '/') { | ||
seperator = '/'; | ||
} | ||
let params = searchParam.toString(); | ||
if (params) { | ||
params = '?' + params; | ||
} | ||
return `${baseUrl}${seperator}${path}${params}`; | ||
}; | ||
const addToHeaders = (headers, keyValues) => { | ||
if (keyValues) { | ||
for (const key in keyValues) { | ||
if ({}.hasOwnProperty.call(keyValues, key)) { | ||
const values = keyValues[key]; | ||
if (Array.isArray(values)) { | ||
for (let i = 0; i < values.length; i++) { | ||
headers[key] = values[i]; | ||
} | ||
} | ||
else { | ||
headers[key] = values; | ||
} | ||
} | ||
if (keyValues) { | ||
for (const key in keyValues) { | ||
if ({}.hasOwnProperty.call(keyValues, key)) { | ||
const values = keyValues[key]; | ||
if (Array.isArray(values)) { | ||
for (let i = 0; i < values.length; i++) { | ||
headers[key] = values[i]; | ||
} | ||
} else { | ||
headers[key] = values; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
const buildHeaders = (secOptions, extraHeaders) => { | ||
const headers = {}; | ||
addToHeaders(headers, extraHeaders); | ||
if (secOptions) { | ||
addToHeaders(headers, secOptions.headers); | ||
} | ||
return headers; | ||
const headers = {}; | ||
addToHeaders(headers, extraHeaders); | ||
if (secOptions) { | ||
addToHeaders(headers, secOptions.headers); | ||
} | ||
return headers; | ||
}; | ||
@@ -69,37 +82,44 @@ /** | ||
*/ | ||
export const requestService = (serviceConfig, options) => { | ||
const { url, securityProvider, refreshedSecurityProvider } = serviceConfig; | ||
const { path, queryParams, requestInit } = options || defaultRequestServiceOptions; | ||
const secOptions = securityProvider && securityProvider(); | ||
const requestUrl = buildUrl(url, path, queryParams, secOptions); | ||
const headers = buildHeaders(secOptions, requestInit && requestInit.headers); | ||
const credentials = buildCredentials(secOptions); | ||
const requestOptions = { | ||
...requestInit, | ||
headers, | ||
credentials, | ||
}; | ||
return fetch(requestUrl, requestOptions).then((response) => { | ||
if (response.status === 204) { | ||
return Promise.resolve(); | ||
} | ||
else if (response.ok) { | ||
return response.json(); | ||
} | ||
else if (response.status === 401 && refreshedSecurityProvider) { | ||
// auth issue - try once | ||
return refreshedSecurityProvider().then(newSecOptions => { | ||
const retryServiceConfig = { | ||
url, | ||
securityProvider: () => newSecOptions, | ||
}; | ||
return requestService(retryServiceConfig, options); | ||
}); | ||
} | ||
return Promise.reject({ | ||
code: response.status, | ||
reason: response.statusText, | ||
}); | ||
const { | ||
url, | ||
securityProvider, | ||
refreshedSecurityProvider | ||
} = serviceConfig; | ||
const { | ||
path, | ||
queryParams, | ||
requestInit | ||
} = options || defaultRequestServiceOptions; | ||
const secOptions = securityProvider && securityProvider(); | ||
const requestUrl = buildUrl(url, path, queryParams, secOptions); | ||
const headers = buildHeaders(secOptions, requestInit && requestInit.headers); | ||
const credentials = buildCredentials(secOptions); | ||
const requestOptions = { ...requestInit, | ||
headers, | ||
credentials | ||
}; | ||
return fetch(requestUrl, requestOptions).then(response => { | ||
if (response.status === 204) { | ||
return Promise.resolve(); | ||
} else if (response.ok) { | ||
return response.json(); | ||
} else if (response.status === 401 && refreshedSecurityProvider) { | ||
// auth issue - try once | ||
return refreshedSecurityProvider().then(newSecOptions => { | ||
const retryServiceConfig = { | ||
url, | ||
securityProvider: () => newSecOptions | ||
}; | ||
return requestService(retryServiceConfig, options); | ||
}); | ||
} | ||
return Promise.reject({ | ||
code: response.status, | ||
reason: response.statusText | ||
}); | ||
}; | ||
//# sourceMappingURL=serviceUtils.js.map | ||
}); | ||
}; |
@@ -1,4 +0,16 @@ | ||
export const buildCredentials = (secOptions) => { | ||
return secOptions && secOptions.omitCredentials ? 'omit' : 'include'; | ||
/** | ||
* Defines a typical Resource. | ||
* | ||
* Q = query type | ||
* R = result type | ||
* E = error type | ||
* I = info type | ||
* O = options that filter can accept | ||
*/ | ||
export const buildCredentials = secOptions => { | ||
return secOptions && secOptions.omitCredentials ? 'omit' : 'include'; | ||
}; | ||
//# sourceMappingURL=types.js.map | ||
/** | ||
* Returns a promise to a SecurityOptions that has just been forcibly refreshed with a | ||
* new token. Will be used for single retry per request if a 401 is returned. | ||
*/ |
{ | ||
"name": "@atlaskit/util-service-support", | ||
"version": "6.0.2" | ||
"version": "6.0.3" | ||
} |
export * from './types'; | ||
export * from './serviceResources'; | ||
import * as serviceUtils from './serviceUtils'; | ||
export var utils = serviceUtils; | ||
//# sourceMappingURL=index.js.map | ||
export var utils = serviceUtils; |
@@ -1,44 +0,63 @@ | ||
var AbstractResource = /** @class */ (function () { | ||
function AbstractResource() { | ||
this.listeners = new Set(); | ||
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; | ||
import _createClass from "@babel/runtime/helpers/createClass"; | ||
import _defineProperty from "@babel/runtime/helpers/defineProperty"; | ||
export var AbstractResource = /*#__PURE__*/function () { | ||
function AbstractResource() { | ||
_classCallCheck(this, AbstractResource); | ||
_defineProperty(this, "listeners", new Set()); | ||
} | ||
_createClass(AbstractResource, [{ | ||
key: "subscribe", | ||
value: function subscribe(onChange) { | ||
this.listeners.add(onChange); | ||
if (this.lastResult) { | ||
// Notify subscribe of last result (i.e. initial state) | ||
onChange.result(this.lastResult); | ||
} | ||
} | ||
AbstractResource.prototype.subscribe = function (onChange) { | ||
this.listeners.add(onChange); | ||
if (this.lastResult) { | ||
// Notify subscribe of last result (i.e. initial state) | ||
onChange.result(this.lastResult); | ||
}, { | ||
key: "unsubscribe", | ||
value: function unsubscribe(onChange) { | ||
this.listeners.delete(onChange); | ||
} | ||
}, { | ||
key: "notifyResult", | ||
value: function notifyResult(result) { | ||
this.listeners.forEach(function (onChange) { | ||
onChange.result(result); | ||
}); | ||
} | ||
}, { | ||
key: "notifyError", | ||
value: function notifyError(error) { | ||
this.listeners.forEach(function (onChange) { | ||
if (onChange.error) { | ||
onChange.error(error); | ||
} | ||
}; | ||
AbstractResource.prototype.unsubscribe = function (onChange) { | ||
this.listeners.delete(onChange); | ||
}; | ||
AbstractResource.prototype.notifyResult = function (result) { | ||
this.listeners.forEach(function (onChange) { | ||
onChange.result(result); | ||
}); | ||
}; | ||
AbstractResource.prototype.notifyError = function (error) { | ||
this.listeners.forEach(function (onChange) { | ||
if (onChange.error) { | ||
onChange.error(error); | ||
} | ||
}); | ||
}; | ||
AbstractResource.prototype.notifyInfo = function (info) { | ||
this.listeners.forEach(function (onChange) { | ||
if (onChange.info) { | ||
onChange.info(info); | ||
} | ||
}); | ||
}; | ||
AbstractResource.prototype.notifyNotReady = function () { | ||
this.listeners.forEach(function (onChange) { | ||
if (onChange.notReady) { | ||
onChange.notReady(); | ||
} | ||
}); | ||
}; | ||
return AbstractResource; | ||
}()); | ||
export { AbstractResource }; | ||
//# sourceMappingURL=serviceResources.js.map | ||
}); | ||
} | ||
}, { | ||
key: "notifyInfo", | ||
value: function notifyInfo(info) { | ||
this.listeners.forEach(function (onChange) { | ||
if (onChange.info) { | ||
onChange.info(info); | ||
} | ||
}); | ||
} | ||
}, { | ||
key: "notifyNotReady", | ||
value: function notifyNotReady() { | ||
this.listeners.forEach(function (onChange) { | ||
if (onChange.notReady) { | ||
onChange.notReady(); | ||
} | ||
}); | ||
} | ||
}]); | ||
return AbstractResource; | ||
}(); |
@@ -1,67 +0,87 @@ | ||
import { __assign } from "tslib"; | ||
import _defineProperty from "@babel/runtime/helpers/defineProperty"; | ||
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; } | ||
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; } | ||
import * as URL from 'url'; | ||
import USP from 'url-search-params'; // IE, Safari, Mobile Chrome, Mobile Safari | ||
import { buildCredentials, } from './types'; | ||
import { buildCredentials } from './types'; | ||
var URLSearchParams = USP.default || USP; | ||
var defaultRequestServiceOptions = {}; | ||
var buildUrl = function (baseUrl, path, queryParams, secOptions) { | ||
if (path === void 0) { path = ''; } | ||
var searchParam = new URLSearchParams(URL.parse(baseUrl).search || undefined); | ||
baseUrl = baseUrl.split('?')[0]; | ||
if (queryParams) { | ||
for (var key in queryParams) { | ||
if ({}.hasOwnProperty.call(queryParams, key)) { | ||
searchParam.append(key, queryParams[key]); | ||
} | ||
} | ||
var buildUrl = function buildUrl(baseUrl) { | ||
var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
var queryParams = arguments.length > 2 ? arguments[2] : undefined; | ||
var secOptions = arguments.length > 3 ? arguments[3] : undefined; | ||
var searchParam = new URLSearchParams(URL.parse(baseUrl).search || undefined); | ||
baseUrl = baseUrl.split('?')[0]; | ||
if (queryParams) { | ||
for (var key in queryParams) { | ||
if ({}.hasOwnProperty.call(queryParams, key)) { | ||
searchParam.append(key, queryParams[key]); | ||
} | ||
} | ||
if (secOptions && secOptions.params) { | ||
for (var key in secOptions.params) { | ||
if ({}.hasOwnProperty.call(secOptions.params, key)) { | ||
var values = secOptions.params[key]; | ||
if (Array.isArray(values)) { | ||
for (var i = 0; i < values.length; i++) { | ||
searchParam.append(key, values[i]); | ||
} | ||
} | ||
else { | ||
searchParam.append(key, values); | ||
} | ||
} | ||
} | ||
if (secOptions && secOptions.params) { | ||
for (var _key in secOptions.params) { | ||
if ({}.hasOwnProperty.call(secOptions.params, _key)) { | ||
var values = secOptions.params[_key]; | ||
if (Array.isArray(values)) { | ||
for (var i = 0; i < values.length; i++) { | ||
searchParam.append(_key, values[i]); | ||
} | ||
} else { | ||
searchParam.append(_key, values); | ||
} | ||
} | ||
} | ||
var seperator = ''; | ||
if (path && baseUrl.substr(-1) !== '/') { | ||
seperator = '/'; | ||
} | ||
var params = searchParam.toString(); | ||
if (params) { | ||
params = '?' + params; | ||
} | ||
return "" + baseUrl + seperator + path + params; | ||
} | ||
var seperator = ''; | ||
if (path && baseUrl.substr(-1) !== '/') { | ||
seperator = '/'; | ||
} | ||
var params = searchParam.toString(); | ||
if (params) { | ||
params = '?' + params; | ||
} | ||
return "".concat(baseUrl).concat(seperator).concat(path).concat(params); | ||
}; | ||
var addToHeaders = function (headers, keyValues) { | ||
if (keyValues) { | ||
for (var key in keyValues) { | ||
if ({}.hasOwnProperty.call(keyValues, key)) { | ||
var values = keyValues[key]; | ||
if (Array.isArray(values)) { | ||
for (var i = 0; i < values.length; i++) { | ||
headers[key] = values[i]; | ||
} | ||
} | ||
else { | ||
headers[key] = values; | ||
} | ||
} | ||
var addToHeaders = function addToHeaders(headers, keyValues) { | ||
if (keyValues) { | ||
for (var key in keyValues) { | ||
if ({}.hasOwnProperty.call(keyValues, key)) { | ||
var values = keyValues[key]; | ||
if (Array.isArray(values)) { | ||
for (var i = 0; i < values.length; i++) { | ||
headers[key] = values[i]; | ||
} | ||
} else { | ||
headers[key] = values; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
var buildHeaders = function (secOptions, extraHeaders) { | ||
var headers = {}; | ||
addToHeaders(headers, extraHeaders); | ||
if (secOptions) { | ||
addToHeaders(headers, secOptions.headers); | ||
} | ||
return headers; | ||
var buildHeaders = function buildHeaders(secOptions, extraHeaders) { | ||
var headers = {}; | ||
addToHeaders(headers, extraHeaders); | ||
if (secOptions) { | ||
addToHeaders(headers, secOptions.headers); | ||
} | ||
return headers; | ||
}; | ||
@@ -71,34 +91,47 @@ /** | ||
*/ | ||
export var requestService = function (serviceConfig, options) { | ||
var url = serviceConfig.url, securityProvider = serviceConfig.securityProvider, refreshedSecurityProvider = serviceConfig.refreshedSecurityProvider; | ||
var _a = options || defaultRequestServiceOptions, path = _a.path, queryParams = _a.queryParams, requestInit = _a.requestInit; | ||
var secOptions = securityProvider && securityProvider(); | ||
var requestUrl = buildUrl(url, path, queryParams, secOptions); | ||
var headers = buildHeaders(secOptions, requestInit && requestInit.headers); | ||
var credentials = buildCredentials(secOptions); | ||
var requestOptions = __assign(__assign({}, requestInit), { headers: headers, | ||
credentials: credentials }); | ||
return fetch(requestUrl, requestOptions).then(function (response) { | ||
if (response.status === 204) { | ||
return Promise.resolve(); | ||
} | ||
else if (response.ok) { | ||
return response.json(); | ||
} | ||
else if (response.status === 401 && refreshedSecurityProvider) { | ||
// auth issue - try once | ||
return refreshedSecurityProvider().then(function (newSecOptions) { | ||
var retryServiceConfig = { | ||
url: url, | ||
securityProvider: function () { return newSecOptions; }, | ||
}; | ||
return requestService(retryServiceConfig, options); | ||
}); | ||
} | ||
return Promise.reject({ | ||
code: response.status, | ||
reason: response.statusText, | ||
}); | ||
export var requestService = function requestService(serviceConfig, options) { | ||
var url = serviceConfig.url, | ||
securityProvider = serviceConfig.securityProvider, | ||
refreshedSecurityProvider = serviceConfig.refreshedSecurityProvider; | ||
var _ref = options || defaultRequestServiceOptions, | ||
path = _ref.path, | ||
queryParams = _ref.queryParams, | ||
requestInit = _ref.requestInit; | ||
var secOptions = securityProvider && securityProvider(); | ||
var requestUrl = buildUrl(url, path, queryParams, secOptions); | ||
var headers = buildHeaders(secOptions, requestInit && requestInit.headers); | ||
var credentials = buildCredentials(secOptions); | ||
var requestOptions = _objectSpread(_objectSpread({}, requestInit), {}, { | ||
headers: headers, | ||
credentials: credentials | ||
}); | ||
return fetch(requestUrl, requestOptions).then(function (response) { | ||
if (response.status === 204) { | ||
return Promise.resolve(); | ||
} else if (response.ok) { | ||
return response.json(); | ||
} else if (response.status === 401 && refreshedSecurityProvider) { | ||
// auth issue - try once | ||
return refreshedSecurityProvider().then(function (newSecOptions) { | ||
var retryServiceConfig = { | ||
url: url, | ||
securityProvider: function securityProvider() { | ||
return newSecOptions; | ||
} | ||
}; | ||
return requestService(retryServiceConfig, options); | ||
}); | ||
} | ||
return Promise.reject({ | ||
code: response.status, | ||
reason: response.statusText | ||
}); | ||
}; | ||
//# sourceMappingURL=serviceUtils.js.map | ||
}); | ||
}; |
@@ -1,4 +0,16 @@ | ||
export var buildCredentials = function (secOptions) { | ||
return secOptions && secOptions.omitCredentials ? 'omit' : 'include'; | ||
/** | ||
* Defines a typical Resource. | ||
* | ||
* Q = query type | ||
* R = result type | ||
* E = error type | ||
* I = info type | ||
* O = options that filter can accept | ||
*/ | ||
export var buildCredentials = function buildCredentials(secOptions) { | ||
return secOptions && secOptions.omitCredentials ? 'omit' : 'include'; | ||
}; | ||
//# sourceMappingURL=types.js.map | ||
/** | ||
* Returns a promise to a SecurityOptions that has just been forcibly refreshed with a | ||
* new token. Will be used for single retry per request if a 401 is returned. | ||
*/ |
{ | ||
"name": "@atlaskit/util-service-support", | ||
"version": "6.0.2" | ||
"version": "6.0.3" | ||
} |
{ | ||
"name": "@atlaskit/util-service-support", | ||
"version": "6.0.2", | ||
"version": "6.0.3", | ||
"description": "A library of support classes for integrating React components with REST HTTP services", | ||
@@ -24,3 +24,3 @@ "publishConfig": { | ||
"dependencies": { | ||
"tslib": "^2.0.0", | ||
"@babel/runtime": "^7.0.0", | ||
"url": "^0.11.0", | ||
@@ -27,0 +27,0 @@ "url-search-params": "^0.10.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
39273
28
744
+ Added@babel/runtime@^7.0.0
+ Added@babel/runtime@7.26.0(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)
- Removedtslib@^2.0.0
- Removedtslib@2.8.1(transitive)