@metamask/json-rpc-engine
Version: 
A tool for processing JSON-RPC messages
1 lines • 1.92 kB
Source Map (JSON)
{"version":3,"file":"createScaffoldMiddleware.cjs","sourceRoot":"","sources":["../src/createScaffoldMiddleware.ts"],"names":[],"mappings":";;;AASA;;;;;;;;GAQG;AACH,SAAgB,wBAAwB,CAAC,QAExC;IACC,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,EAAE;QAC7B,MAAM,OAAO,GAAG,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACrC,wBAAwB;QACxB,IAAI,OAAO,KAAK,SAAS,EAAE;YACzB,OAAO,IAAI,EAAE,CAAC;SACf;QAED,uCAAuC;QACvC,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE;YACjC,OAAO,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC;SACrC;QACD,gDAAgD;QAC/C,GAAsB,CAAC,MAAM,GAAG,OAAO,CAAC;QACzC,OAAO,GAAG,EAAE,CAAC;IACf,CAAC,CAAC;AACJ,CAAC;AAlBD,4DAkBC","sourcesContent":["import type { Json, JsonRpcParams, JsonRpcSuccess } from '@metamask/utils';\n\nimport type { JsonRpcMiddleware } from './JsonRpcEngine';\n\ntype ScaffoldMiddlewareHandler<\n  Params extends JsonRpcParams,\n  Result extends Json,\n> = JsonRpcMiddleware<Params, Result> | Json;\n\n/**\n * Creates a middleware function from an object of RPC method handler functions,\n * keyed to particular method names. If a method corresponding to a key of this\n * object is requested, this middleware will pass it to the corresponding\n * handler and return the result.\n *\n * @param handlers - The RPC method handler functions.\n * @returns The scaffold middleware function.\n */\nexport function createScaffoldMiddleware(handlers: {\n  [methodName: string]: ScaffoldMiddlewareHandler<JsonRpcParams, Json>;\n}): JsonRpcMiddleware<JsonRpcParams, Json> {\n  return (req, res, next, end) => {\n    const handler = handlers[req.method];\n    // if no handler, return\n    if (handler === undefined) {\n      return next();\n    }\n\n    // if handler is fn, call as middleware\n    if (typeof handler === 'function') {\n      return handler(req, res, next, end);\n    }\n    // if handler is some other value, use as result\n    (res as JsonRpcSuccess).result = handler;\n    return end();\n  };\n}\n"]}