UNPKG

@pothos/plugin-scope-auth

Version:

A Pothos plugin for adding scope based authorization checks to your GraphQL Schema

76 lines (75 loc) 2.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const _core = /*#__PURE__*/ _interop_require_wildcard(require("@pothos/core")); const _errors = require("./errors"); const _requestcache = /*#__PURE__*/ _interop_require_default(require("./request-cache")); function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interop_require_wildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = { __proto__: null }; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for(var key in obj){ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } const schemaBuilderProto = _core.default.prototype; schemaBuilderProto.runAuthScopes = function runAuthScopes(context, scopes, unauthorizedError = (result)=>new _errors.ForbiddenError(result.message, result.failure)) { const cache = _requestcache.default.fromContext(context, this); const resultOrPromise = cache.evaluateScopeMap(scopes); if ((0, _core.isThenable)(resultOrPromise)) { return resultOrPromise.then(handleScopeResult); } handleScopeResult(resultOrPromise); function handleScopeResult(result) { if (result) { const error = unauthorizedError({ message: 'Unauthorized', failure: result }); if (typeof error === 'string') { throw new _errors.ForbiddenError(error, result); } throw error; } } }; //# sourceMappingURL=schema-builder.js.map