{"version":3,"file":"fetch.cjs","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":";;;AAIA,qDAAiD;AAKjD;;;;;;;;;;;GAWG;AACH,SAAgB,qBAAqB,CAAC,EACpC,UAAU,EACV,OAAO,GAAG,EAAE,GAMb;IAKC,OAAO,KAAK,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE;QACpC,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACrC,MAAM,OAAO,GACX,OAAO,CAAC,mBAAmB,KAAK,SAAS,IAAI,MAAM,KAAK,SAAS;YAC/D,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,mBAAmB,CAAC,EAAE,MAAM,EAAE;YAC3C,CAAC,CAAC,EAAE,CAAC;QAET,MAAM,eAAe,GAAG,MAAM,UAAU,CAAC,OAAO,CAAC,OAAO,EAAE;YACxD,OAAO;SACR,CAAC,CAAC;QAEH,0EAA0E;QAC1E,4CAA4C;QAC5C,sEAAsE;QACtE,uEAAuE;QACvE,IAAI,OAAO,IAAI,eAAe,EAAE,CAAC;YAC/B,MAAM,sBAAS,CAAC,QAAQ,CAAC;gBACvB,IAAI,EAAE,eAAe,CAAC,KAAK;aAC5B,CAAC,CAAC;QACL,CAAC;QACD,OAAO,eAAe,CAAC,MAAM,CAAC;IAChC,CAAC,CAAC;AACJ,CAAC;AAnCD,sDAmCC","sourcesContent":["import type {\n  JsonRpcMiddleware,\n  MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport type { AbstractRpcServiceLike } from './types';\n\n/**\n * Creates middleware for sending a JSON-RPC request through the given RPC\n * service.\n *\n * @param args - The arguments to this function.\n * @param args.rpcService - The RPC service to use.\n * @param args.options - Options.\n * @param args.options.originHttpHeaderKey - If provided, the origin field for\n * each JSON-RPC request will be attached to each outgoing fetch request under\n * this header.\n * @returns The fetch middleware.\n */\nexport function createFetchMiddleware({\n  rpcService,\n  options = {},\n}: {\n  rpcService: AbstractRpcServiceLike;\n  options?: {\n    originHttpHeaderKey?: string;\n  };\n}): JsonRpcMiddleware<\n  JsonRpcRequest,\n  Json,\n  MiddlewareContext<{ origin: string }>\n> {\n  return async ({ request, context }) => {\n    const origin = context.get('origin');\n    const headers =\n      options.originHttpHeaderKey !== undefined && origin !== undefined\n        ? { [options.originHttpHeaderKey]: origin }\n        : {};\n\n    const jsonRpcResponse = await rpcService.request(request, {\n      headers,\n    });\n\n    // NOTE: We intentionally do not test to see if `jsonRpcResponse.error` is\n    // strictly a JSON-RPC error response as per\n    // <https://www.jsonrpc.org/specification#error_object> to account for\n    // Ganache returning error objects with extra properties such as `name`\n    if ('error' in jsonRpcResponse) {\n      throw rpcErrors.internal({\n        data: jsonRpcResponse.error,\n      });\n    }\n    return jsonRpcResponse.result;\n  };\n}\n"]}