UNPKG

firebase-admin-ql

Version:

A powerful library that bridges Firebase Admin SDK with PostgreSQL, simplifies interaction with stored procedures, facilitates seamless third-party API calls using fetch, and provides utility functions to streamline backend operations.

85 lines 3.58 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.callableFunctionWrapper = exports.chainMiddlewares = exports.isAuthorizedUser = exports.isConfirmedApp = void 0; const v2_1 = require("firebase-functions/v2"); const https_1 = require("firebase-functions/v2/https"); const isConfirmedApp = (app, next) => { if (app === undefined) { throw new Error("The function must be called from an App Check verified app."); } next(); }; exports.isConfirmedApp = isConfirmedApp; const isAuthorizedUser = (auth, next) => { if (auth === undefined) { throw new Error("The function must be called by an authorized user"); } next(); }; exports.isAuthorizedUser = isAuthorizedUser; const chainMiddlewares = (middlewares, request, handler) => __awaiter(void 0, void 0, void 0, function* () { const execute = (index) => __awaiter(void 0, void 0, void 0, function* () { if (index < middlewares.length) { return new Promise((resolve, reject) => { try { middlewares[index](request, () => __awaiter(void 0, void 0, void 0, function* () { resolve(yield execute(index + 1)); })); } catch (error) { reject(error); } }); } else { return yield handler(request); } }); return yield execute(0); }); exports.chainMiddlewares = chainMiddlewares; const callableFunctionWrapper = (callback, withAuth = false, maxInstances) => { return (0, https_1.onCall)({ timeoutSeconds: 120, enforceAppCheck: true, maxInstances: maxInstances }, (request) => __awaiter(void 0, void 0, void 0, function* () { try { const middleWares = [ (req, next) => (0, exports.isConfirmedApp)(req.app, next), ]; if (withAuth) middleWares.push((req, next) => (0, exports.isAuthorizedUser)(req.auth, next)); return yield (0, exports.chainMiddlewares)(middleWares, request, () => __awaiter(void 0, void 0, void 0, function* () { try { return yield callback(request); } catch (error) { v2_1.logger.log("callableFunctionWrapper: ", error); return { status: "error", message: "Unable to conclude process", }; } })); } catch (error) { v2_1.logger.log("init error: ", error); return { status: "error", message: "Poor network connections!", }; } })); }; exports.callableFunctionWrapper = callableFunctionWrapper; //# sourceMappingURL=firebase.js.map