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,QAAkB;QACxB,OAAA,QAAQ;aACL,IAAI,EAAE;aACN,IAAI,CAAC,UAAC,QAAQ,IAAK,OAAA,aAAa,CAAC,QAAQ,EAAE,QAAQ,CAAC,EAAjC,CAAiC,CAAC;aACrD,IAAI,CAAC,UAAC,MAAW;YAChB,IAAI,QAAQ,CAAC,MAAM,IAAI,GAAG,EAAE;gBAE1B,gBAAgB,CACd,QAAQ,EACR,MAAM,EACN,wDAAiD,QAAQ,CAAC,MAAM,CAAE,CACnE,CAAC;aACH;YACD,IACE,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;gBACtB,CAAC,cAAc,CAAC,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC;gBACpC,CAAC,cAAc,CAAC,IAAI,CAAC,MAAM,EAAE,QAAQ,CAAC,EACtC;gBAEA,gBAAgB,CACd,QAAQ,EACR,MAAM,EACN,iDACE,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC;oBACvB,CAAC,CAAC,UAAU,CAAC,GAAG,CAAC,UAAC,EAAE,IAAK,OAAA,EAAE,CAAC,aAAa,EAAhB,CAAgB,CAAC;oBAC1C,CAAC,CAAC,UAAU,CAAC,aAAa,OAC1B,CACL,CAAC;aACH;YACD,OAAO,MAAM,CAAC;QAChB,CAAC,CAAC;IA7BJ,CA6BI,CAAC;AACT,CAAC","sourcesContent":["import { responseIterator } from \"./responseIterator\";\nimport { Operation } from \"../core\";\nimport { throwServerError } from \"../utils\";\nimport { Observer } from \"../../utilities\";\n\nconst { hasOwnProperty } = Object.prototype;\n\nexport type ServerParseError = Error & {\n response: Response;\n statusCode: number;\n bodyText: string;\n};\n\nexport async function readMultipartBody<\n T extends object = Record<string, unknown>\n>(response: Response, observer: Observer<T>) {\n if (TextDecoder === undefined) {\n throw new Error(\n \"TextDecoder must be defined in the environment: please import a polyfill.\"\n );\n }\n const decoder = new TextDecoder(\"utf-8\");\n const contentType = response.headers?.get('content-type');\n const delimiter = \"boundary=\";\n\n // parse boundary value and ignore any subsequent name/value pairs after ;\n // https://www.rfc-editor.org/rfc/rfc9110.html#name-parameters\n // e.g. multipart/mixed;boundary=\"graphql\";deferSpec=20220824\n // if no boundary is specified, default to -\n const boundaryVal = contentType?.includes(delimiter)\n ? contentType\n ?.substring(contentType?.indexOf(delimiter) + delimiter.length)\n .replace(/['\"]/g, \"\")\n .replace(/\\;(.*)/gm, \"\")\n .trim()\n : \"-\";\n\n let boundary = `--${boundaryVal}`;\n let buffer = \"\";\n const iterator = responseIterator(response);\n let running = true;\n\n while (running) {\n const { value, done } = await iterator.next();\n const chunk = typeof value === \"string\" ? value : decoder.decode(value);\n running = !done;\n buffer += chunk;\n let bi = buffer.indexOf(boundary);\n\n while (bi > -1) {\n let message: string;\n [message, buffer] = [\n buffer.slice(0, bi),\n buffer.slice(bi + boundary.length),\n ];\n if (message.trim()) {\n const i = message.indexOf(\"\\r\\n\\r\\n\");\n const headers = parseHeaders(message.slice(0, i));\n const contentType = headers[\"content-type\"];\n if (\n contentType &&\n contentType.toLowerCase().indexOf(\"application/json\") === -1\n ) {\n throw new Error(\"Unsupported patch content type: application/json is required.\");\n }\n const body = message.slice(i);\n\n try {\n const result = parseJsonBody<T>(response, body.replace(\"\\r\\n\", \"\"));\n if (\n Object.keys(result).length > 1 ||\n \"data\" in result ||\n \"incremental\" in result ||\n \"errors\" in result\n ) {\n // for the last chunk with only `hasNext: false`,\n // we don't need to call observer.next as there is no data/errors\n observer.next?.(result);\n }\n } catch (err) {\n handleError(err, observer);\n }\n }\n bi = buffer.indexOf(boundary);\n }\n }\n observer.complete?.();\n}\n\nexport function parseHeaders(headerText: string): Record<string, string> {\n const headersInit: Record<string, string> = {};\n headerText.split(\"\\n\").forEach((line) => {\n const i = line.indexOf(\":\");\n if (i > -1) {\n // normalize headers to lowercase\n const name = line.slice(0, i).trim().toLowerCase();\n const value = line.slice(i + 1).trim();\n headersInit[name] = value;\n }\n });\n return headersInit;\n}\n\nexport function parseJsonBody<T>(response: Response, bodyText: string): T {\n if (response.status >= 300) {\n // Network error\n const getResult = () => {\n try {\n return JSON.parse(bodyText);\n } catch (err) {\n return bodyText\n }\n }\n throwServerError(\n response,\n getResult(),\n `Response not successful: Received status code ${response.status}`,\n );\n }\n\n try {\n return JSON.parse(bodyText) as T;\n } catch (err) {\n const parseError = err as ServerParseError;\n parseError.name = \"ServerParseError\";\n parseError.response = response;\n parseError.statusCode = response.status;\n parseError.bodyText = bodyText;\n throw parseError;\n }\n}\n\nexport function handleError(err: any, observer: Observer<any>) {\n if (err.name === \"AbortError\") return;\n // if it is a network error, BUT there is graphql result info fire\n // the next observer before calling error this gives apollo-client\n // (and react-apollo) the `graphqlErrors` and `networkErrors` to\n // pass to UI this should only happen if we *also* have data as\n // part of the response key per the spec\n if (err.result && err.result.errors && err.result.data) {\n // if we don't call next, the UI can only show networkError\n // because AC didn't get any graphqlErrors this is graphql\n // execution result info (i.e errors and possibly data) this is\n // because there is no formal spec how errors should translate to\n // http status codes. So an auth error (401) could have both data\n // from a public field, errors from a private field, and a status\n // of 401\n // {\n // user { // this will have errors\n // firstName\n // }\n // products { // this is public so will have data\n // cost\n // }\n // }\n //\n // the result of above *could* look like this:\n // {\n // data: { products: [{ cost: \"$10\" }] },\n // errors: [{\n // message: 'your session has timed out',\n // path: []\n // }]\n // }\n // status code of above would be a 401\n // in the UI you want to show data where you can, errors as data where you can\n // and use correct http status codes\n observer.next?.(err.result);\n }\n\n observer.error?.(err);\n}\n\nexport function readJsonBody<T = Record<string, unknown>>(\n response: Response,\n operation: Operation,\n observer: Observer<T>\n) {\n parseAndCheckHttpResponse(operation)(response)\n .then((result) => {\n observer.next?.(result);\n observer.complete?.();\n })\n .catch((err) => handleError(err, observer));\n}\n\nexport function parseAndCheckHttpResponse(operations: Operation | Operation[]) {\n return (response: Response) =>\n response\n .text()\n .then((bodyText) => parseJsonBody(response, bodyText))\n .then((result: any) => {\n if (response.status >= 300) {\n // Network error\n throwServerError(\n response,\n result,\n `Response not successful: Received status code ${response.status}`\n );\n }\n if (\n !Array.isArray(result) &&\n !hasOwnProperty.call(result, \"data\") &&\n !hasOwnProperty.call(result, \"errors\")\n ) {\n // Data error\n throwServerError(\n response,\n result,\n `Server response was missing for query '${\n Array.isArray(operations)\n ? operations.map((op) => op.operationName)\n : operations.operationName\n }'.`\n );\n }\n return result;\n });\n}\n"]}
\No newline at end of file