@pothos/plugin-scope-auth
Version:
A Pothos plugin for adding scope based authorization checks to your GraphQL Schema
40 lines (39 loc) • 2.22 kB
JavaScript
import { isThenable } from '@pothos/core';
import { ForbiddenError } from './errors.js';
import RequestCache from './request-cache.js';
export 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.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 (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 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 ForbiddenError(error, stepResult) : error;
}
}
return isTypeOf ? isTypeOf(parent, context, info) : true;
}
return runSteps(0);
};
}
//# sourceMappingURL=is-type-of-helper.js.map