UNPKG

@randajan/oauth2-client

Version:

Lightweight Node.js helper that streamlines OAuth 2.0 and service-account authentication for all Google APIs, giving downstream packages hassle-free token acquisition and refresh

110 lines (107 loc) 3.28 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.js var index_exports = {}; __export(index_exports, { extendURL: () => extendURL, formatCredentials: () => formatCredentials, isValidURL: () => isValidURL, objFromBase64: () => objFromBase64, objToBase64: () => objToBase64, sliceMap: () => sliceMap, strFromBase64: () => strFromBase64, strToBase64: () => strToBase64, validateFn: () => validateFn, validateStr: () => validateStr, validateURL: () => validateURL }); module.exports = __toCommonJS(index_exports); // src/tools.js var sliceMap = (arr, size, callback) => { size = Math.max(1, size) || 1; const r = []; if (!Array.isArray(arr)) { return r; } for (let k = 0; k < arr.length; k += size) { r.push(callback(arr.slice(k, k + size), r.length, size, arr.length)); } return r; }; var extendURL = (url, query = {}) => { const u = new URL(url); for (let i in query) { if (query[i] != null) { u.searchParams.append(i, query[i]); } } return u.toString(); }; var isValidURL = (str) => { try { new URL(str); } catch (e) { return false; } return true; }; var validateURL = (required, url, errProp) => { if (!url && !required) { return; } if (isValidURL(url)) { return url; } throw new Error(`${errProp} is not a valid URL`); }; var validateFn = (required, fn, errProp) => { if (!fn && !required) { return; } if (typeof fn === "function") { return fn; } throw new Error(`${errProp} is not a valid function`); }; var validateStr = (required, str, errProp) => { if (!str && !required) { return; } if (typeof str === "string") { return str; } throw new Error(`${errProp} is not a valid string`); }; var formatCredentials = (credentials = {}) => { const c = { ...credentials }; if (c.expires_in && !c.expiry_date) { c.expiry_date = Date.now() + c.expires_in * 1e3; } if (c.refresh_token && !c.expiry_date) { throw new Error(`OAuth2 credentials 'refresh_token' must be provided with 'expiry_date'`); } if (!c.access_token && !c.refresh_token) { throw new Error(`OAuth2 credentials 'access_token' of 'refresh_token' must be provided`); } return c; }; var strToBase64 = (str) => Buffer.from(str, "utf8").toString("base64"); var strFromBase64 = (strEncoded) => Buffer.from(strEncoded, "base64").toString("utf8"); var objToBase64 = (obj) => strToBase64(JSON.stringify(obj)); var objFromBase64 = (objEncoded) => JSON.parse(strFromBase64(objEncoded)); //# sourceMappingURL=index.js.map