UNPKG

@metamask/snaps-rpc-methods

Version:
65 lines 1.89 kB
import { PermissionType, SubjectType } from "@metamask/permission-controller"; const methodName = 'snap_getPreferences'; /** * The specification builder for the `snap_getPreferences` permission. * `snap_getPreferences` allows snaps to access user preferences. * * @param options - The specification builder options. * @param options.allowedCaveats - The optional allowed caveats for the permission. * @param options.methodHooks - The RPC method hooks needed by the method implementation. * @returns The specification for the `snap_getPreferences` permission. */ export const specificationBuilder = ({ allowedCaveats = null, methodHooks }) => { return { permissionType: PermissionType.RestrictedMethod, targetName: methodName, allowedCaveats, methodImplementation: getImplementation(methodHooks), subjectTypes: [SubjectType.Snap], }; }; const methodHooks = { getPreferences: true, }; /** * Gets the user's preferences. * * @example * ```json name="Manifest" * { * "initialPermissions": { * "snap_getPreferences": {} * } * } * ``` * ```ts name="Usage" * const preferences = await snap.request({ * method: 'snap_getPreferences', * }); * * console.log(preferences); * // { * // locale: 'en', * // currency: 'usd', * // ... * // } * ``` */ export const getPreferencesBuilder = Object.freeze({ targetName: methodName, specificationBuilder, methodHooks, }); /** * Builds the method implementation for `snap_getPreferences`. * * @param hooks - The RPC method hooks. * @param hooks.getPreferences - A function that returns the user selected preferences. * @returns The user preferences. */ export function getImplementation({ getPreferences, }) { return async function implementation(_args) { return getPreferences(); }; } //# sourceMappingURL=getPreferences.mjs.map