UNPKG

kinde-cli

Version:

Kinde cli for managing your business integration, users, roles and permissions etc

125 lines (124 loc) 5.14 kB
"use strict"; 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; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.errorHandler = exports.extractUrlWithoutApi = exports.generateMessage = exports.omit = exports.onCancelCallback = exports.filterUndefined = void 0; const context_1 = __importDefault(require("../lib/context")); const prompts_1 = require("@clack/prompts"); const picocolors_1 = __importDefault(require("picocolors")); const zod_1 = require("zod"); const storage_1 = require("./storage"); /** Remove keys whose values are undefined */ function filterUndefined(obj) { let temp = {}; for (let key in obj) { if (obj[key] && obj[key] !== null) { temp[key] = obj[key]; } } return temp; } exports.filterUndefined = filterUndefined; exports.onCancelCallback = { onCancel: ({ results }) => { (0, prompts_1.cancel)("Done."); process.exit(0); }, }; const omit = (key, obj) => { const _a = obj, _b = key, omitted = _a[_b], rest = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]); return rest; }; exports.omit = omit; const generateMessage = ({ identifier, desc, attr }) => `${picocolors_1.default.green(`${identifier}:`)} ${picocolors_1.default.blue(`(${desc})`)} ${picocolors_1.default.green(`[${attr}]`)}`; exports.generateMessage = generateMessage; /** * Strips the api ending portion of the endpoint * `'https://<domain>.kinde.com/api'` => `'https://<domain>.kinde.com/'` */ function extractUrlWithoutApi(url) { // Define the regex pattern to match URLs excluding "/api" let pattern = /(https:\/\/(?:www\.)?[^\/]+)(?:\/api)?(?:\/|$)/; // Find the match in the given URL let match = url.match(pattern); // If a match is found, return the extracted URL if (match) { return match[1]; } else { return null; } } exports.extractUrlWithoutApi = extractUrlWithoutApi; const handleMiddlewareError = (err) => { console.error(err.message); process.exit(1); }; const isAuthenticated = () => __awaiter(void 0, void 0, void 0, function* () { let config = yield (0, storage_1.readGlobalConfig)(); if (!config) { throw new Error("You are not authenticated, please login: 'kinde-cli login'"); } let toJson = JSON.parse(config); let schema = zod_1.z.object({ clientId: zod_1.z.string().min(1), clientSecret: zod_1.z.string().min(1), normalDomain: zod_1.z.string().url(), personalDomainNoApiEndingPath: zod_1.z.string().url(), token: zod_1.z.object({ access_token: zod_1.z.string().min(1), expires_in: zod_1.z.number(), scope: zod_1.z.string(), // probably not an empty string token_type: zod_1.z.string().min(1), }), }); let validateContext = schema.safeParse(toJson); if (!validateContext.success) { console.warn(validateContext.error.errors.map((e) => `Path: [${e.path}] - '${e.message}'`)); throw new Error("Invalid config structure, please login: 'kinde-cli login'"); } context_1.default.setData(toJson); }); function errorHandler(handler, fn) { return (...args) => __awaiter(this, void 0, void 0, function* () { /** * Some command action handlers do not require auth, * But this auth check should run all at all times if handler is Auth */ if (handler === "Auth") { try { yield isAuthenticated(); } catch (e) { handleMiddlewareError(e); } } /** * This function below will never be executed if auth check fails, * if auth fails handleMiddlwareError exits the process with a status of 1 */ return yield fn(...args).catch(handleMiddlewareError); }); } exports.errorHandler = errorHandler;