{"version":3,"file":"index.cjs","names":["isBrowser","err: unknown","retryErr: unknown","params: QueryParams"],"sources":["../../src/http-helpers/index.ts"],"sourcesContent":["/* eslint-disable max-depth */\nimport axios, { type Method, type RawAxiosRequestHeaders } from \"axios\";\nimport { isBrowser } from \"browser-or-node\";\n\nimport type { DropNotificationParams, OrdersScoringParams } from \"../types/index.js\";\n\nexport const GET = \"GET\";\nexport const POST = \"POST\";\nexport const DELETE = \"DELETE\";\nexport const PUT = \"PUT\";\n\nconst overloadHeaders = (\n\tmethod: Method,\n\theaders: Record<string, string | number | boolean> = {},\n) => {\n\tif (isBrowser) {\n\t\treturn;\n\t}\n\n\theaders[\"User-Agent\"] = `@polymarket/clob-client`;\n\theaders.Accept = \"*/*\";\n\theaders.Connection = \"keep-alive\";\n\theaders[\"Content-Type\"] = \"application/json\";\n\n\tif (method === GET) {\n\t\theaders[\"Accept-Encoding\"] = \"gzip\";\n\t}\n};\n\nexport const request = async (\n\tendpoint: string,\n\tmethod: Method,\n\theaders?: any,\n\tdata?: any,\n\tparams?: any,\n): Promise<any> => {\n\toverloadHeaders(method, headers);\n\treturn await axios({ method, url: endpoint, headers, data, params });\n};\n\nexport type QueryParams = Record<string, any>;\n\nexport interface RequestOptions {\n\theaders?: RawAxiosRequestHeaders;\n\tdata?: any;\n\tparams?: QueryParams;\n}\n\nconst sleep = (ms: number) => new Promise(res => setTimeout(res, ms));\n\nexport const post = async (\n\tendpoint: string,\n\toptions?: RequestOptions,\n\tretryOnError: boolean = false,\n): Promise<any> => {\n\ttry {\n\t\tconst resp = await request(\n\t\t\tendpoint,\n\t\t\tPOST,\n\t\t\toptions?.headers,\n\t\t\toptions?.data,\n\t\t\toptions?.params,\n\t\t);\n\t\treturn resp.data;\n\t} catch (err: unknown) {\n\t\tif (retryOnError && isTransientAxiosError(err)) {\n\t\t\tawait sleep(30);\n\t\t\ttry {\n\t\t\t\tconst resp = await request(\n\t\t\t\t\tendpoint,\n\t\t\t\t\tPOST,\n\t\t\t\t\toptions?.headers,\n\t\t\t\t\toptions?.data,\n\t\t\t\t\toptions?.params,\n\t\t\t\t);\n\t\t\t\treturn resp.data;\n\t\t\t} catch (retryErr: unknown) {\n\t\t\t\treturn errorHandling(retryErr);\n\t\t\t}\n\t\t}\n\t\treturn errorHandling(err);\n\t}\n};\n\nexport const get = async (endpoint: string, options?: RequestOptions): Promise<any> => {\n\ttry {\n\t\tconst resp = await request(endpoint, GET, options?.headers, options?.data, options?.params);\n\t\treturn resp.data;\n\t} catch (err: unknown) {\n\t\treturn errorHandling(err);\n\t}\n};\n\nexport const del = async (endpoint: string, options?: RequestOptions): Promise<any> => {\n\ttry {\n\t\tconst resp = await request(\n\t\t\tendpoint,\n\t\t\tDELETE,\n\t\t\toptions?.headers,\n\t\t\toptions?.data,\n\t\t\toptions?.params,\n\t\t);\n\t\treturn resp.data;\n\t} catch (err: unknown) {\n\t\treturn errorHandling(err);\n\t}\n};\n\nconst errorHandling = (err: unknown) => {\n\tif (axios.isAxiosError(err)) {\n\t\tif (err.response) {\n\t\t\tif (err.response?.data) {\n\t\t\t\tif (\n\t\t\t\t\ttypeof err.response?.data === \"string\" ||\n\t\t\t\t\terr.response?.data instanceof String\n\t\t\t\t) {\n\t\t\t\t\treturn { error: err.response?.data, status: err.response?.status };\n\t\t\t\t}\n\t\t\t\t// biome-ignore lint/suspicious/noPrototypeBuiltins: ES2022 Object.hasOwn not available with current target\n\t\t\t\tif (!Object.prototype.hasOwnProperty.call(err.response?.data, \"error\")) {\n\t\t\t\t\treturn { error: err.response?.data, status: err.response?.status };\n\t\t\t\t}\n\t\t\t\t// in this case the field 'error' is included\n\t\t\t\treturn { ...err.response?.data, status: err.response?.status };\n\t\t\t}\n\t\t}\n\n\t\tif (err.message) {\n\t\t\treturn { error: err.message };\n\t\t}\n\t}\n\n\treturn { error: err };\n};\n\nconst isTransientAxiosError = (err: unknown): boolean => {\n\tif (!axios.isAxiosError(err)) return false;\n\tif (!err.response) return true; // network error\n\tconst status = err.response.status ?? 0;\n\tif (status >= 500 && status < 600) return true; // 5xx\n\tconst code = (err.code ?? \"\").toString();\n\treturn [\"ECONNABORTED\", \"ENETUNREACH\", \"EAI_AGAIN\", \"ETIMEDOUT\"].includes(code);\n};\n\nexport const parseOrdersScoringParams = (orderScoringParams?: OrdersScoringParams): QueryParams => {\n\tconst params: QueryParams = {};\n\tif (orderScoringParams !== undefined) {\n\t\tif (orderScoringParams.orderIds !== undefined) {\n\t\t\tparams.order_ids = orderScoringParams?.orderIds.join(\",\");\n\t\t}\n\t}\n\treturn params;\n};\n\nexport const parseDropNotificationParams = (\n\tdropNotificationParams?: DropNotificationParams,\n): QueryParams => {\n\tconst params: QueryParams = {};\n\tif (dropNotificationParams !== undefined) {\n\t\tif (dropNotificationParams.ids !== undefined) {\n\t\t\tparams.ids = dropNotificationParams?.ids.join(\",\");\n\t\t}\n\t}\n\treturn params;\n};\n"],"mappings":";;;;;;AAMA,MAAa,MAAM;AACnB,MAAa,OAAO;AACpB,MAAa,SAAS;AAGtB,MAAM,mBACL,QACA,UAAqD,EAAE,KACnD;AACJ,KAAIA,0BACH;AAGD,SAAQ,gBAAgB;AACxB,SAAQ,SAAS;AACjB,SAAQ,aAAa;AACrB,SAAQ,kBAAkB;AAE1B,KAAI,WAAW,IACd,SAAQ,qBAAqB;;AAI/B,MAAa,UAAU,OACtB,UACA,QACA,SACA,MACA,WACkB;AAClB,iBAAgB,QAAQ,QAAQ;AAChC,QAAO,yBAAY;EAAE;EAAQ,KAAK;EAAU;EAAS;EAAM;EAAQ,CAAC;;AAWrE,MAAM,SAAS,OAAe,IAAI,SAAQ,QAAO,WAAW,KAAK,GAAG,CAAC;AAErE,MAAa,OAAO,OACnB,UACA,SACA,eAAwB,UACN;AAClB,KAAI;AAQH,UAPa,MAAM,QAClB,UACA,MACA,SAAS,SACT,SAAS,MACT,SAAS,OACT,EACW;UACJC,KAAc;AACtB,MAAI,gBAAgB,sBAAsB,IAAI,EAAE;AAC/C,SAAM,MAAM,GAA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