{"version":3,"sources":["../../src/client/core.ts"],"sourcesContent":["// Copyright © Aptos Foundation\n// SPDX-License-Identifier: Apache-2.0\n\nimport { AptosConfig } from \"../api/aptosConfig\";\nimport { AptosApiError, AptosResponse } from \"./types\";\nimport { VERSION } from \"../version\";\nimport { AnyNumber, AptosRequest, Client, ClientRequest, ClientResponse, MimeType } from \"../types\";\nimport { AptosApiType } from \"../utils\";\n\n/**\n * Meaningful errors map\n */\nconst errors: Record<number, string> = {\n  400: \"Bad Request\",\n  401: \"Unauthorized\",\n  403: \"Forbidden\",\n  404: \"Not Found\",\n  429: \"Too Many Requests\",\n  500: \"Internal Server Error\",\n  502: \"Bad Gateway\",\n  503: \"Service Unavailable\",\n};\n\n/**\n * Given a url and method, sends the request with axios and\n * returns the response.\n */\nexport async function request<Req, Res>(options: ClientRequest<Req>, client: Client): Promise<ClientResponse<Res>> {\n  const { url, method, body, contentType, params, overrides, originMethod } = options;\n  const headers: Record<string, string | AnyNumber | boolean | undefined> = {\n    ...overrides?.HEADERS,\n    \"x-aptos-client\": `aptos-typescript-sdk/${VERSION}`,\n    \"content-type\": contentType ?? MimeType.JSON,\n    \"x-aptos-typescript-sdk-origin-method\": originMethod,\n  };\n\n  if (overrides?.AUTH_TOKEN) {\n    headers.Authorization = `Bearer ${overrides?.AUTH_TOKEN}`;\n  }\n  if (overrides?.API_KEY) {\n    headers.Authorization = `Bearer ${overrides?.API_KEY}`;\n  }\n\n  /*\n   * make a call using the @aptos-labs/aptos-client package\n   * {@link https://www.npmjs.com/package/@aptos-labs/aptos-client}\n   */\n  return client.provider<Req, Res>({\n    url,\n    method,\n    body,\n    params,\n    headers,\n    overrides,\n  });\n}\n\n/**\n * The main function to use when doing an API request.\n *\n * @param options AptosRequest\n * @param aptosConfig The config information for the SDK client instance\n * @returns the response or AptosApiError\n */\nexport async function aptosRequest<Req extends {}, Res extends {}>(\n  options: AptosRequest,\n  aptosConfig: AptosConfig,\n  apiType: AptosApiType,\n): Promise<AptosResponse<Req, Res>> {\n  const { url, path } = options;\n  const fullUrl = path ? `${url}/${path}` : url;\n  const response = await request<Req, Res>({ ...options, url: fullUrl }, aptosConfig.client);\n\n  const result: AptosResponse<Req, Res> = {\n    status: response.status,\n    statusText: response.statusText!,\n    data: response.data,\n    headers: response.headers,\n    config: response.config,\n    request: response.request,\n    url: fullUrl,\n  };\n\n  // Handle case for `Unauthorized` error (i.e API_KEY error)\n  if (result.status === 401) {\n    throw new AptosApiError(options, result, `Error: ${result.data}`);\n  }\n\n  // to support both fullnode and indexer responses,\n  // check if it is an indexer query, and adjust response.data\n  if (apiType === AptosApiType.INDEXER) {\n    const indexerResponse = result.data as any;\n    // Handle Indexer general errors\n    if (indexerResponse.errors) {\n      throw new AptosApiError(\n        options,\n        result,\n        `Indexer error: ${indexerResponse.errors[0].message}` ??\n          `Indexer unhandled Error ${response.status} : ${response.statusText}`,\n      );\n    }\n    result.data = indexerResponse.data as Res;\n  }\n\n  if (result.status >= 200 && result.status < 300) {\n    return result;\n  }\n\n  let errorMessage: string;\n\n  if (result && result.data && \"message\" in result.data && \"error_code\" in result.data) {\n    errorMessage = JSON.stringify(result.data);\n  } else if (result.status in errors) {\n    // If it's not an API type, it must come form infra, these are prehandled\n    errorMessage = errors[result.status];\n  } else {\n    // Everything else is unhandled\n    errorMessage = `Unhandled Error ${result.status} : ${result.statusText}`;\n  }\n\n  // We have to explicitly check for all request types, because if the error is a non-indexer error, but\n  // comes from an indexer request (e.g. 404), we'll need to mention it appropriately\n  throw new AptosApiError(options, result, `${apiType} error: ${errorMessage}`);\n}\n"],"mappings":"kFAYA,IAAMA,EAAiC,CACrC,IAAK,cACL,IAAK,eACL,IAAK,YACL,IAAK,YACL,IAAK,oBACL,IAAK,wBACL,IAAK,cACL,IAAK,qBACP,EAMA,eAAsBC,EAAkBC,EAA6BC,EAA8C,CACjH,GAAM,CAAE,IAAAC,EAAK,OAAAC,EAAQ,KAAAC,EAAM,YAAAC,EAAa,OAAAC,EAAQ,UAAAC,EAA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CA,EAAO,UAAUnB,EAE1BsB,EAAetB,EAAOmB,EAAO,MAAM,EAGnCG,EAAe,mBAAmBH,EAAO,MAAM,MAAMA,EAAO,UAAU,GAKlE,IAAIC,EAAclB,EAASiB,EAAQ,GAAGJ,CAAO,WAAWO,CAAY,EAAE,CAC9E","names":["errors","request","options","client","url","method","body","contentType","params","overrides","originMethod","headers","VERSION","aptosRequest","aptosConfig","apiType","path","fullUrl","response","result","AptosApiError","indexerResponse","errorMessage"]}