UNPKG

@metamask/snaps-rpc-methods

Version:
66 lines 2.35 kB
import { providerErrors, rpcErrors } from "@metamask/rpc-errors"; import { StructError, create, object, string } from "@metamask/superstruct"; import { SnapEndowments } from "../endowments/index.mjs"; const methodName = 'snap_cancelBackgroundEvent'; const hookNames = { cancelBackgroundEvent: true, hasPermission: true, }; export const cancelBackgroundEventHandler = { methodNames: [methodName], implementation: getCancelBackgroundEventImplementation, hookNames, }; const CancelBackgroundEventsParametersStruct = object({ id: string(), }); /** * The `snap_cancelBackgroundEvent` method implementation. * * @param req - The JSON-RPC request object. * @param res - The JSON-RPC response object. * @param _next - The `json-rpc-engine` "next" callback. Not used by this * function. * @param end - The `json-rpc-engine` "end" callback. * @param hooks - The RPC method hooks. * @param hooks.cancelBackgroundEvent - The function to cancel a background event. * @param hooks.hasPermission - The function to check if a snap has the `endowment:cronjob` permission. * @returns Nothing. */ async function getCancelBackgroundEventImplementation(req, res, _next, end, { cancelBackgroundEvent, hasPermission }) { const { params } = req; if (!hasPermission(SnapEndowments.Cronjob)) { return end(providerErrors.unauthorized()); } try { const validatedParams = getValidatedParams(params); const { id } = validatedParams; cancelBackgroundEvent(id); res.result = null; } catch (error) { return end(error); } return end(); } /** * Validate the cancelBackgroundEvent method `params` and returns them cast to the correct type. Throws if validation fails. * * @param params - The unvalidated params object from the method request. * @returns The validated resolveInterface method parameter object. */ function getValidatedParams(params) { try { return create(params, CancelBackgroundEventsParametersStruct); } catch (error) { if (error instanceof StructError) { throw rpcErrors.invalidParams({ message: `Invalid params: ${error.message}.`, }); } /* istanbul ignore next */ throw rpcErrors.internal(); } } //# sourceMappingURL=cancelBackgroundEvent.mjs.map