UNPKG

prisma-trpc-shield-generator

Version:

Prisma +2 generator to emit a tRPC shield from your Prisma schema

86 lines 3.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.constructShield = exports.wrapWithExport = exports.wrapWithTrpcShieldCall = exports.wrapWithObject = exports.getImports = void 0; const internals_1 = require("@prisma/internals"); const getRelativePath_1 = __importDefault(require("./utils/getRelativePath")); const getImports = (type, newPath) => { let statement = ''; if (type === 'trpc') { statement = "import * as trpc from '@trpc/server';\n"; } else if (type === 'trpc-shield') { statement = "import { shield, allow } from 'trpc-shield';\n"; } else if (type === 'context') { statement = `import { Context } from '${newPath}';\n`; } return statement; }; exports.getImports = getImports; const wrapWithObject = ({ shieldItemLines, }) => { let wrapped = '{'; wrapped += '\n'; wrapped += Array.isArray(shieldItemLines) ? ' ' + shieldItemLines.join(',\r\n') : ' ' + shieldItemLines; wrapped += '\n'; wrapped += '}'; return wrapped; }; exports.wrapWithObject = wrapWithObject; const wrapWithTrpcShieldCall = ({ shieldObjectTextWrapped, }) => { let wrapped = 'shield<Context>('; wrapped += '\n'; wrapped += ' ' + shieldObjectTextWrapped; wrapped += '\n'; wrapped += ')'; return wrapped; }; exports.wrapWithTrpcShieldCall = wrapWithTrpcShieldCall; const wrapWithExport = ({ shieldObjectText, }) => { return `export const permissions = ${shieldObjectText};`; }; exports.wrapWithExport = wrapWithExport; const constructShield = ({ queries, mutations, subscriptions, }, config, options) => { if (queries.length === 0 && mutations.length === 0 && subscriptions.length === 0) { return ''; } let rootItems = ''; if (queries.length > 0) { const queryLinesWrapped = `query: ${(0, exports.wrapWithObject)({ shieldItemLines: queries.map((query) => `${query}: allow`), })},`; rootItems += queryLinesWrapped; } if (mutations.length > 0) { const mutationLinesWrapped = `mutation: ${(0, exports.wrapWithObject)({ shieldItemLines: mutations.map((mutation) => `${mutation}: allow`), })},`; rootItems += mutationLinesWrapped; } if (subscriptions.length > 0) { const subscriptionLinesWrapped = `subscription: ${(0, exports.wrapWithObject)({ shieldItemLines: subscriptions.map((subscription) => `${subscription}: allow`), })},`; rootItems += subscriptionLinesWrapped; } if (rootItems.length === 0) return ''; let shieldText = (0, exports.getImports)('trpc-shield'); const outputDir = (0, internals_1.parseEnvValue)(options.generator.output); shieldText += (0, exports.getImports)('context', (0, getRelativePath_1.default)(outputDir, config.contextPath, options.schemaPath)); shieldText += '\n\n'; shieldText += (0, exports.wrapWithExport)({ shieldObjectText: (0, exports.wrapWithTrpcShieldCall)({ shieldObjectTextWrapped: (0, exports.wrapWithObject)({ shieldItemLines: rootItems }), }), }); return shieldText; }; exports.constructShield = constructShield; //# sourceMappingURL=helpers.js.map