{"version":3,"file":"apollo-orbit.angular.fetch.mjs","sources":["../../fetch/src/index.ts","../../fetch/src/apollo-orbit.angular.fetch.ts"],"sourcesContent":["import { HttpClient, HttpErrorResponse, HttpHeaders, HttpResponse } from '@angular/common/http';\r\nimport { lastValueFrom, of } from 'rxjs';\r\nimport { catchError, map } from 'rxjs/operators';\r\nimport { HttpConfig } from './httpConfig';\r\n\r\nexport { HttpConfig } from './httpConfig';\r\n\r\nconst normalizedHeaders: { [from: string]: string | undefined } = {\r\n    'accept': 'Accept',\r\n    'content-type': 'Content-Type'\r\n};\r\n\r\nexport const makeFetch = (httpClient: HttpClient) => (url: string, { method, ...config }: HttpConfig): Promise<Response> => {\r\n    return lastValueFrom(\r\n        httpClient.request(method, url, {\r\n            ...config,\r\n            headers: normalizeHeaders(config.headers),\r\n            observe: 'response',\r\n            responseType: 'text',\r\n            reportProgress: false,\r\n            withCredentials: config.credentials === 'include'\r\n        }).pipe(\r\n            map(response => getResponse(response)),\r\n            catchError(error => of(getErrorResponse(error)))\r\n        )\r\n    );\r\n};\r\n\r\nfunction getErrorResponse(errorResponse: HttpErrorResponse): Response {\r\n    const { error, name, message, ...rest } = errorResponse;\r\n    if (errorResponse.status <= 0) throw new TypeError('Failed to fetch');\r\n    return getResponse({ body: error, ...rest } as HttpResponse<string>);\r\n}\r\n\r\nfunction getResponse(response: HttpResponse<string>): Response {\r\n    if (typeof Response !== 'function') {\r\n        // node environment\r\n        const { ok, status, statusText, url, body } = response;\r\n        return { ok, status, statusText, url, text: () => Promise.resolve(body) } as Response;\r\n    } else {\r\n        // browser environment\r\n        return new Response(response.body, { ...response, headers: mapHeaders(response.headers) });\r\n    }\r\n}\r\n\r\nfunction mapHeaders(headers: HttpHeaders | undefined): HeadersInit | undefined {\r\n    if (!headers) return headers;\r\n    return headers.keys().reduce((acc, key) => ({ ...acc, [key]: headers.get(key) }), {});\r\n}\r\n\r\n// @apollo/client passes headers in a format that fails angular checks\r\nfunction normalizeHeaders(headers: Record<string, string> | undefined): Record<string, string> | undefined {\r\n    if (!headers) return headers;\r\n\r\n    return Object\r\n        .keys(headers)\r\n        .reduce(\r\n            (acc, header) => {\r\n                const normalizedHeader = normalizedHeaders[header];\r\n                return normalizedHeader !== undefined\r\n                    ? { ...acc, [normalizedHeader]: headers[header] }\r\n                    : { ...acc, [header]: headers[header] };\r\n            },\r\n            {});\r\n}\r\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;AAOA,MAAM,iBAAiB,GAA2C;AAC9D,IAAA,QAAQ,EAAE,QAAQ;AAClB,IAAA,cAAc,EAAE,cAAc;CACjC,CAAC;AAEW,MAAA,SAAS,GAAG,CAAC,UAAsB,KAAK,CAAC,GAAW,EAAE,EAAE,MAAM,EAAE,GAAG,MAAM,EAAc,KAAuB;IACvH,OAAO,aAAa,CAChB,UAAU,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,EAAE;AAC5B,QAAA,GAAG,MAAM;AACT,QAAA,OAAO,EAAE,gBAAgB,CAAC,MAAM,CAAC,OAAO,CAAC;AACzC,QAAA,OAAO,EAAE,UAAU;AACnB,QAAA,YAAY,EAAE,MAAM;AACpB,QAAA,cAAc,EAAE,KAAK;AACrB,QAAA,eAAe,EAAE,MAAM,CAAC,WAAW,KAAK,SAAS;AACpD,KAAA,CAAC,CAAC,IAAI,CACH,GAAG,CAAC,QAAQ,IAAI,WAAW,CAAC,QAAQ,CAAC,CAAC,EACtC,UAAU,CAAC,KAAK,IAAI,EAAE,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC,CAAC,CACnD,CACJ,CAAC;AACN,EAAE;AAEF,SAAS,gBAAgB,CAAC,aAAgC,EAAA;AACtD,IAAA,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,GAAG,aAAa,CAAC;AACxD,IAAA,IAAI,aAAa,CAAC,MAAM,IAAI,CAAC;AAAE,QAAA,MAAM,IAAI,SAAS,CAAC,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