UNPKG

@omneedia/auth-js

Version:
134 lines 6.23 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { expiresAt, looksLikeFetchResponse } from './helpers'; import { AuthApiError, AuthRetryableFetchError, AuthUnknownError } from './errors'; const _getErrorMessage = (err) => err.msg || err.message || err.error_description || err.error || JSON.stringify(err); const handleError = (error, reject) => __awaiter(void 0, void 0, void 0, function* () { const NETWORK_ERROR_CODES = [502, 503, 504]; if (!looksLikeFetchResponse(error)) { reject(new AuthRetryableFetchError(_getErrorMessage(error), 0)); } else if (NETWORK_ERROR_CODES.includes(error.status)) { // status in 500...599 range - server had an error, request might be retryed. reject(new AuthRetryableFetchError(_getErrorMessage(error), error.status)); } else { // got a response from server that is not in the 500...599 range - should not retry error .json() .then((err) => { reject(new AuthApiError(_getErrorMessage(err), error.status || 500)); }) .catch((e) => { // not a valid json response reject(new AuthUnknownError(_getErrorMessage(e), e)); }); } }); const _getRequestParams = (method, options, parameters, body) => { const params = { method, headers: (options === null || options === void 0 ? void 0 : options.headers) || {} }; if (method === 'GET') { return params; } params.headers = Object.assign({ 'Content-Type': 'application/json;charset=UTF-8' }, options === null || options === void 0 ? void 0 : options.headers); params.body = JSON.stringify(body); return Object.assign(Object.assign({}, params), parameters); }; export function _request(fetcher, method, url, options) { var _a; return __awaiter(this, void 0, void 0, function* () { const headers = Object.assign({}, options === null || options === void 0 ? void 0 : options.headers); if (options === null || options === void 0 ? void 0 : options.jwt) { headers['Authorization'] = `Bearer ${options.jwt}`; } const qs = (_a = options === null || options === void 0 ? void 0 : options.query) !== null && _a !== void 0 ? _a : {}; if (options === null || options === void 0 ? void 0 : options.redirectTo) { qs['redirect_to'] = options.redirectTo; } const queryString = Object.keys(qs).length ? '?' + new URLSearchParams(qs).toString() : ''; const data = yield _handleRequest(fetcher, method, url + queryString, { headers, noResolveJson: options === null || options === void 0 ? void 0 : options.noResolveJson }, {}, options === null || options === void 0 ? void 0 : options.body); return (options === null || options === void 0 ? void 0 : options.xform) ? options === null || options === void 0 ? void 0 : options.xform(data) : { data: Object.assign({}, data), error: null }; }); } function _handleRequest(fetcher, method, url, options, parameters, body) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { fetcher(url, _getRequestParams(method, options, parameters, body)) .then((result) => { if (!result.ok) throw result; if (options === null || options === void 0 ? void 0 : options.noResolveJson) return result; return result.json(); }) .then((data) => resolve(data)) .catch((error) => handleError(error, reject)); }); }); } export function _sessionResponse(data) { var _a; let session = null; if (hasSession(data)) { session = Object.assign({}, data); session.expires_at = expiresAt(data.expires_in); } const user = (_a = data.user) !== null && _a !== void 0 ? _a : data; return { data: { session, user }, error: null }; } export function _userResponse(data) { var _a; const user = (_a = data.user) !== null && _a !== void 0 ? _a : data; return { data: { user }, error: null }; } export function _ssoResponse(data) { return { data, error: null }; } export function _generateLinkResponse(data) { const { action_link, email_otp, hashed_token, redirect_to, verification_type } = data, rest = __rest(data, ["action_link", "email_otp", "hashed_token", "redirect_to", "verification_type"]); const properties = { action_link, email_otp, hashed_token, redirect_to, verification_type, }; const user = Object.assign({}, rest); return { data: { properties, user, }, error: null, }; } export function _noResolveJsonResponse(data) { return data; } /** * hasSession checks if the response object contains a valid session * @param data A response object * @returns true if a session is in the response */ function hasSession(data) { return data.access_token && data.refresh_token && data.expires_in; } //# sourceMappingURL=fetch.js.map