UNPKG

@pothos/plugin-scope-auth

Version:

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

55 lines (54 loc) 2.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "isTypeOfHelper", { enumerable: true, get: function() { return isTypeOfHelper; } }); const _core = require("@pothos/core"); const _errors = require("./errors.js"); const _requestcache = /*#__PURE__*/ _interop_require_default(require("./request-cache.js")); function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function isTypeOfHelper(steps, plugin, isTypeOf) { var _plugin_builder_options_scopeAuth; const globalUnauthorizedError = (_plugin_builder_options_scopeAuth = plugin.builder.options.scopeAuth) === null || _plugin_builder_options_scopeAuth === void 0 ? void 0 : _plugin_builder_options_scopeAuth.unauthorizedError; const createError = (parent, context, info, result)=>globalUnauthorizedError ? globalUnauthorizedError(parent, context, info, result) : result.message; return (parent, context, info)=>{ const cache = _requestcache.default.fromContext(context, plugin.builder); function runSteps(index) { for(let i = index; i < steps.length; i += 1){ const { run, errorMessage } = steps[i]; const stepResult = run(cache, parent, {}, context, info, ()=>{}); if ((0, _core.isThenable)(stepResult)) { return stepResult.then((result)=>{ if (result) { const error = createError(parent, context, info, { message: typeof errorMessage === 'function' ? errorMessage(parent, {}, context, info) : errorMessage, failure: result }); throw typeof error === 'string' ? new _errors.ForbiddenError(error, result) : error; } return runSteps(i + 1); }); } if (stepResult) { const error = createError(parent, context, info, { message: typeof errorMessage === 'function' ? errorMessage(parent, {}, context, info) : errorMessage, failure: stepResult }); throw typeof error === 'string' ? new _errors.ForbiddenError(error, stepResult) : error; } } return isTypeOf ? isTypeOf(parent, context, info) : true; } return runSteps(0); }; } //# sourceMappingURL=is-type-of-helper.js.map