module.mjs 130 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106
  1. /**
  2. * @license Angular v20.1.0
  3. * (c) 2010-2025 Google LLC. https://angular.io/
  4. * License: MIT
  5. */
  6. import * as i0 from '@angular/core';
  7. import { ɵRuntimeError as _RuntimeError, Injectable, InjectionToken, inject, NgZone, DestroyRef, ɵformatRuntimeError as _formatRuntimeError, PendingTasks, ɵConsole as _Console, runInInjectionContext, DOCUMENT, Inject, makeEnvironmentProviders, NgModule } from '@angular/core';
  8. import { concatMap, filter, map, finalize, switchMap } from 'rxjs/operators';
  9. import { of, Observable, from } from 'rxjs';
  10. import { XhrFactory, parseCookieValue } from './xhr.mjs';
  11. /**
  12. * Transforms an `HttpRequest` into a stream of `HttpEvent`s, one of which will likely be a
  13. * `HttpResponse`.
  14. *
  15. * `HttpHandler` is injectable. When injected, the handler instance dispatches requests to the
  16. * first interceptor in the chain, which dispatches to the second, etc, eventually reaching the
  17. * `HttpBackend`.
  18. *
  19. * In an `HttpInterceptor`, the `HttpHandler` parameter is the next interceptor in the chain.
  20. *
  21. * @publicApi
  22. */
  23. class HttpHandler {
  24. }
  25. /**
  26. * A final `HttpHandler` which will dispatch the request via browser HTTP APIs to a backend.
  27. *
  28. * Interceptors sit between the `HttpClient` interface and the `HttpBackend`.
  29. *
  30. * When injected, `HttpBackend` dispatches requests directly to the backend, without going
  31. * through the interceptor chain.
  32. *
  33. * @publicApi
  34. */
  35. class HttpBackend {
  36. }
  37. /**
  38. * Represents the header configuration options for an HTTP request.
  39. * Instances are immutable. Modifying methods return a cloned
  40. * instance with the change. The original object is never changed.
  41. *
  42. * @publicApi
  43. */
  44. class HttpHeaders {
  45. /**
  46. * Internal map of lowercase header names to values.
  47. */
  48. headers;
  49. /**
  50. * Internal map of lowercased header names to the normalized
  51. * form of the name (the form seen first).
  52. */
  53. normalizedNames = new Map();
  54. /**
  55. * Complete the lazy initialization of this object (needed before reading).
  56. */
  57. lazyInit;
  58. /**
  59. * Queued updates to be materialized the next initialization.
  60. */
  61. lazyUpdate = null;
  62. /** Constructs a new HTTP header object with the given values.*/
  63. constructor(headers) {
  64. if (!headers) {
  65. this.headers = new Map();
  66. }
  67. else if (typeof headers === 'string') {
  68. this.lazyInit = () => {
  69. this.headers = new Map();
  70. headers.split('\n').forEach((line) => {
  71. const index = line.indexOf(':');
  72. if (index > 0) {
  73. const name = line.slice(0, index);
  74. const value = line.slice(index + 1).trim();
  75. this.addHeaderEntry(name, value);
  76. }
  77. });
  78. };
  79. }
  80. else if (typeof Headers !== 'undefined' && headers instanceof Headers) {
  81. this.headers = new Map();
  82. headers.forEach((value, name) => {
  83. this.addHeaderEntry(name, value);
  84. });
  85. }
  86. else {
  87. this.lazyInit = () => {
  88. if (typeof ngDevMode === 'undefined' || ngDevMode) {
  89. assertValidHeaders(headers);
  90. }
  91. this.headers = new Map();
  92. Object.entries(headers).forEach(([name, values]) => {
  93. this.setHeaderEntries(name, values);
  94. });
  95. };
  96. }
  97. }
  98. /**
  99. * Checks for existence of a given header.
  100. *
  101. * @param name The header name to check for existence.
  102. *
  103. * @returns True if the header exists, false otherwise.
  104. */
  105. has(name) {
  106. this.init();
  107. return this.headers.has(name.toLowerCase());
  108. }
  109. /**
  110. * Retrieves the first value of a given header.
  111. *
  112. * @param name The header name.
  113. *
  114. * @returns The value string if the header exists, null otherwise
  115. */
  116. get(name) {
  117. this.init();
  118. const values = this.headers.get(name.toLowerCase());
  119. return values && values.length > 0 ? values[0] : null;
  120. }
  121. /**
  122. * Retrieves the names of the headers.
  123. *
  124. * @returns A list of header names.
  125. */
  126. keys() {
  127. this.init();
  128. return Array.from(this.normalizedNames.values());
  129. }
  130. /**
  131. * Retrieves a list of values for a given header.
  132. *
  133. * @param name The header name from which to retrieve values.
  134. *
  135. * @returns A string of values if the header exists, null otherwise.
  136. */
  137. getAll(name) {
  138. this.init();
  139. return this.headers.get(name.toLowerCase()) || null;
  140. }
  141. /**
  142. * Appends a new value to the existing set of values for a header
  143. * and returns them in a clone of the original instance.
  144. *
  145. * @param name The header name for which to append the values.
  146. * @param value The value to append.
  147. *
  148. * @returns A clone of the HTTP headers object with the value appended to the given header.
  149. */
  150. append(name, value) {
  151. return this.clone({ name, value, op: 'a' });
  152. }
  153. /**
  154. * Sets or modifies a value for a given header in a clone of the original instance.
  155. * If the header already exists, its value is replaced with the given value
  156. * in the returned object.
  157. *
  158. * @param name The header name.
  159. * @param value The value or values to set or override for the given header.
  160. *
  161. * @returns A clone of the HTTP headers object with the newly set header value.
  162. */
  163. set(name, value) {
  164. return this.clone({ name, value, op: 's' });
  165. }
  166. /**
  167. * Deletes values for a given header in a clone of the original instance.
  168. *
  169. * @param name The header name.
  170. * @param value The value or values to delete for the given header.
  171. *
  172. * @returns A clone of the HTTP headers object with the given value deleted.
  173. */
  174. delete(name, value) {
  175. return this.clone({ name, value, op: 'd' });
  176. }
  177. maybeSetNormalizedName(name, lcName) {
  178. if (!this.normalizedNames.has(lcName)) {
  179. this.normalizedNames.set(lcName, name);
  180. }
  181. }
  182. init() {
  183. if (!!this.lazyInit) {
  184. if (this.lazyInit instanceof HttpHeaders) {
  185. this.copyFrom(this.lazyInit);
  186. }
  187. else {
  188. this.lazyInit();
  189. }
  190. this.lazyInit = null;
  191. if (!!this.lazyUpdate) {
  192. this.lazyUpdate.forEach((update) => this.applyUpdate(update));
  193. this.lazyUpdate = null;
  194. }
  195. }
  196. }
  197. copyFrom(other) {
  198. other.init();
  199. Array.from(other.headers.keys()).forEach((key) => {
  200. this.headers.set(key, other.headers.get(key));
  201. this.normalizedNames.set(key, other.normalizedNames.get(key));
  202. });
  203. }
  204. clone(update) {
  205. const clone = new HttpHeaders();
  206. clone.lazyInit = !!this.lazyInit && this.lazyInit instanceof HttpHeaders ? this.lazyInit : this;
  207. clone.lazyUpdate = (this.lazyUpdate || []).concat([update]);
  208. return clone;
  209. }
  210. applyUpdate(update) {
  211. const key = update.name.toLowerCase();
  212. switch (update.op) {
  213. case 'a':
  214. case 's':
  215. let value = update.value;
  216. if (typeof value === 'string') {
  217. value = [value];
  218. }
  219. if (value.length === 0) {
  220. return;
  221. }
  222. this.maybeSetNormalizedName(update.name, key);
  223. const base = (update.op === 'a' ? this.headers.get(key) : undefined) || [];
  224. base.push(...value);
  225. this.headers.set(key, base);
  226. break;
  227. case 'd':
  228. const toDelete = update.value;
  229. if (!toDelete) {
  230. this.headers.delete(key);
  231. this.normalizedNames.delete(key);
  232. }
  233. else {
  234. let existing = this.headers.get(key);
  235. if (!existing) {
  236. return;
  237. }
  238. existing = existing.filter((value) => toDelete.indexOf(value) === -1);
  239. if (existing.length === 0) {
  240. this.headers.delete(key);
  241. this.normalizedNames.delete(key);
  242. }
  243. else {
  244. this.headers.set(key, existing);
  245. }
  246. }
  247. break;
  248. }
  249. }
  250. addHeaderEntry(name, value) {
  251. const key = name.toLowerCase();
  252. this.maybeSetNormalizedName(name, key);
  253. if (this.headers.has(key)) {
  254. this.headers.get(key).push(value);
  255. }
  256. else {
  257. this.headers.set(key, [value]);
  258. }
  259. }
  260. setHeaderEntries(name, values) {
  261. const headerValues = (Array.isArray(values) ? values : [values]).map((value) => value.toString());
  262. const key = name.toLowerCase();
  263. this.headers.set(key, headerValues);
  264. this.maybeSetNormalizedName(name, key);
  265. }
  266. /**
  267. * @internal
  268. */
  269. forEach(fn) {
  270. this.init();
  271. Array.from(this.normalizedNames.keys()).forEach((key) => fn(this.normalizedNames.get(key), this.headers.get(key)));
  272. }
  273. }
  274. /**
  275. * Verifies that the headers object has the right shape: the values
  276. * must be either strings, numbers or arrays. Throws an error if an invalid
  277. * header value is present.
  278. */
  279. function assertValidHeaders(headers) {
  280. for (const [key, value] of Object.entries(headers)) {
  281. if (!(typeof value === 'string' || typeof value === 'number') && !Array.isArray(value)) {
  282. throw new Error(`Unexpected value of the \`${key}\` header provided. ` +
  283. `Expecting either a string, a number or an array, but got: \`${value}\`.`);
  284. }
  285. }
  286. }
  287. /**
  288. * Provides encoding and decoding of URL parameter and query-string values.
  289. *
  290. * Serializes and parses URL parameter keys and values to encode and decode them.
  291. * If you pass URL query parameters without encoding,
  292. * the query parameters can be misinterpreted at the receiving end.
  293. *
  294. *
  295. * @publicApi
  296. */
  297. class HttpUrlEncodingCodec {
  298. /**
  299. * Encodes a key name for a URL parameter or query-string.
  300. * @param key The key name.
  301. * @returns The encoded key name.
  302. */
  303. encodeKey(key) {
  304. return standardEncoding(key);
  305. }
  306. /**
  307. * Encodes the value of a URL parameter or query-string.
  308. * @param value The value.
  309. * @returns The encoded value.
  310. */
  311. encodeValue(value) {
  312. return standardEncoding(value);
  313. }
  314. /**
  315. * Decodes an encoded URL parameter or query-string key.
  316. * @param key The encoded key name.
  317. * @returns The decoded key name.
  318. */
  319. decodeKey(key) {
  320. return decodeURIComponent(key);
  321. }
  322. /**
  323. * Decodes an encoded URL parameter or query-string value.
  324. * @param value The encoded value.
  325. * @returns The decoded value.
  326. */
  327. decodeValue(value) {
  328. return decodeURIComponent(value);
  329. }
  330. }
  331. function paramParser(rawParams, codec) {
  332. const map = new Map();
  333. if (rawParams.length > 0) {
  334. // The `window.location.search` can be used while creating an instance of the `HttpParams` class
  335. // (e.g. `new HttpParams({ fromString: window.location.search })`). The `window.location.search`
  336. // may start with the `?` char, so we strip it if it's present.
  337. const params = rawParams.replace(/^\?/, '').split('&');
  338. params.forEach((param) => {
  339. const eqIdx = param.indexOf('=');
  340. const [key, val] = eqIdx == -1
  341. ? [codec.decodeKey(param), '']
  342. : [codec.decodeKey(param.slice(0, eqIdx)), codec.decodeValue(param.slice(eqIdx + 1))];
  343. const list = map.get(key) || [];
  344. list.push(val);
  345. map.set(key, list);
  346. });
  347. }
  348. return map;
  349. }
  350. /**
  351. * Encode input string with standard encodeURIComponent and then un-encode specific characters.
  352. */
  353. const STANDARD_ENCODING_REGEX = /%(\d[a-f0-9])/gi;
  354. const STANDARD_ENCODING_REPLACEMENTS = {
  355. '40': '@',
  356. '3A': ':',
  357. '24': '$',
  358. '2C': ',',
  359. '3B': ';',
  360. '3D': '=',
  361. '3F': '?',
  362. '2F': '/',
  363. };
  364. function standardEncoding(v) {
  365. return encodeURIComponent(v).replace(STANDARD_ENCODING_REGEX, (s, t) => STANDARD_ENCODING_REPLACEMENTS[t] ?? s);
  366. }
  367. function valueToString(value) {
  368. return `${value}`;
  369. }
  370. /**
  371. * An HTTP request/response body that represents serialized parameters,
  372. * per the MIME type `application/x-www-form-urlencoded`.
  373. *
  374. * This class is immutable; all mutation operations return a new instance.
  375. *
  376. * @publicApi
  377. */
  378. class HttpParams {
  379. map;
  380. encoder;
  381. updates = null;
  382. cloneFrom = null;
  383. constructor(options = {}) {
  384. this.encoder = options.encoder || new HttpUrlEncodingCodec();
  385. if (options.fromString) {
  386. if (options.fromObject) {
  387. throw new _RuntimeError(2805 /* RuntimeErrorCode.CANNOT_SPECIFY_BOTH_FROM_STRING_AND_FROM_OBJECT */, ngDevMode && 'Cannot specify both fromString and fromObject.');
  388. }
  389. this.map = paramParser(options.fromString, this.encoder);
  390. }
  391. else if (!!options.fromObject) {
  392. this.map = new Map();
  393. Object.keys(options.fromObject).forEach((key) => {
  394. const value = options.fromObject[key];
  395. // convert the values to strings
  396. const values = Array.isArray(value) ? value.map(valueToString) : [valueToString(value)];
  397. this.map.set(key, values);
  398. });
  399. }
  400. else {
  401. this.map = null;
  402. }
  403. }
  404. /**
  405. * Reports whether the body includes one or more values for a given parameter.
  406. * @param param The parameter name.
  407. * @returns True if the parameter has one or more values,
  408. * false if it has no value or is not present.
  409. */
  410. has(param) {
  411. this.init();
  412. return this.map.has(param);
  413. }
  414. /**
  415. * Retrieves the first value for a parameter.
  416. * @param param The parameter name.
  417. * @returns The first value of the given parameter,
  418. * or `null` if the parameter is not present.
  419. */
  420. get(param) {
  421. this.init();
  422. const res = this.map.get(param);
  423. return !!res ? res[0] : null;
  424. }
  425. /**
  426. * Retrieves all values for a parameter.
  427. * @param param The parameter name.
  428. * @returns All values in a string array,
  429. * or `null` if the parameter not present.
  430. */
  431. getAll(param) {
  432. this.init();
  433. return this.map.get(param) || null;
  434. }
  435. /**
  436. * Retrieves all the parameters for this body.
  437. * @returns The parameter names in a string array.
  438. */
  439. keys() {
  440. this.init();
  441. return Array.from(this.map.keys());
  442. }
  443. /**
  444. * Appends a new value to existing values for a parameter.
  445. * @param param The parameter name.
  446. * @param value The new value to add.
  447. * @return A new body with the appended value.
  448. */
  449. append(param, value) {
  450. return this.clone({ param, value, op: 'a' });
  451. }
  452. /**
  453. * Constructs a new body with appended values for the given parameter name.
  454. * @param params parameters and values
  455. * @return A new body with the new value.
  456. */
  457. appendAll(params) {
  458. const updates = [];
  459. Object.keys(params).forEach((param) => {
  460. const value = params[param];
  461. if (Array.isArray(value)) {
  462. value.forEach((_value) => {
  463. updates.push({ param, value: _value, op: 'a' });
  464. });
  465. }
  466. else {
  467. updates.push({ param, value: value, op: 'a' });
  468. }
  469. });
  470. return this.clone(updates);
  471. }
  472. /**
  473. * Replaces the value for a parameter.
  474. * @param param The parameter name.
  475. * @param value The new value.
  476. * @return A new body with the new value.
  477. */
  478. set(param, value) {
  479. return this.clone({ param, value, op: 's' });
  480. }
  481. /**
  482. * Removes a given value or all values from a parameter.
  483. * @param param The parameter name.
  484. * @param value The value to remove, if provided.
  485. * @return A new body with the given value removed, or with all values
  486. * removed if no value is specified.
  487. */
  488. delete(param, value) {
  489. return this.clone({ param, value, op: 'd' });
  490. }
  491. /**
  492. * Serializes the body to an encoded string, where key-value pairs (separated by `=`) are
  493. * separated by `&`s.
  494. */
  495. toString() {
  496. this.init();
  497. return (this.keys()
  498. .map((key) => {
  499. const eKey = this.encoder.encodeKey(key);
  500. // `a: ['1']` produces `'a=1'`
  501. // `b: []` produces `''`
  502. // `c: ['1', '2']` produces `'c=1&c=2'`
  503. return this.map.get(key)
  504. .map((value) => eKey + '=' + this.encoder.encodeValue(value))
  505. .join('&');
  506. })
  507. // filter out empty values because `b: []` produces `''`
  508. // which results in `a=1&&c=1&c=2` instead of `a=1&c=1&c=2` if we don't
  509. .filter((param) => param !== '')
  510. .join('&'));
  511. }
  512. clone(update) {
  513. const clone = new HttpParams({ encoder: this.encoder });
  514. clone.cloneFrom = this.cloneFrom || this;
  515. clone.updates = (this.updates || []).concat(update);
  516. return clone;
  517. }
  518. init() {
  519. if (this.map === null) {
  520. this.map = new Map();
  521. }
  522. if (this.cloneFrom !== null) {
  523. this.cloneFrom.init();
  524. this.cloneFrom.keys().forEach((key) => this.map.set(key, this.cloneFrom.map.get(key)));
  525. this.updates.forEach((update) => {
  526. switch (update.op) {
  527. case 'a':
  528. case 's':
  529. const base = (update.op === 'a' ? this.map.get(update.param) : undefined) || [];
  530. base.push(valueToString(update.value));
  531. this.map.set(update.param, base);
  532. break;
  533. case 'd':
  534. if (update.value !== undefined) {
  535. let base = this.map.get(update.param) || [];
  536. const idx = base.indexOf(valueToString(update.value));
  537. if (idx !== -1) {
  538. base.splice(idx, 1);
  539. }
  540. if (base.length > 0) {
  541. this.map.set(update.param, base);
  542. }
  543. else {
  544. this.map.delete(update.param);
  545. }
  546. }
  547. else {
  548. this.map.delete(update.param);
  549. break;
  550. }
  551. }
  552. });
  553. this.cloneFrom = this.updates = null;
  554. }
  555. }
  556. }
  557. /**
  558. * A token used to manipulate and access values stored in `HttpContext`.
  559. *
  560. * @publicApi
  561. */
  562. class HttpContextToken {
  563. defaultValue;
  564. constructor(defaultValue) {
  565. this.defaultValue = defaultValue;
  566. }
  567. }
  568. /**
  569. * Http context stores arbitrary user defined values and ensures type safety without
  570. * actually knowing the types. It is backed by a `Map` and guarantees that keys do not clash.
  571. *
  572. * This context is mutable and is shared between cloned requests unless explicitly specified.
  573. *
  574. * @usageNotes
  575. *
  576. * ### Usage Example
  577. *
  578. * ```ts
  579. * // inside cache.interceptors.ts
  580. * export const IS_CACHE_ENABLED = new HttpContextToken<boolean>(() => false);
  581. *
  582. * export class CacheInterceptor implements HttpInterceptor {
  583. *
  584. * intercept(req: HttpRequest<any>, delegate: HttpHandler): Observable<HttpEvent<any>> {
  585. * if (req.context.get(IS_CACHE_ENABLED) === true) {
  586. * return ...;
  587. * }
  588. * return delegate.handle(req);
  589. * }
  590. * }
  591. *
  592. * // inside a service
  593. *
  594. * this.httpClient.get('/api/weather', {
  595. * context: new HttpContext().set(IS_CACHE_ENABLED, true)
  596. * }).subscribe(...);
  597. * ```
  598. *
  599. * @publicApi
  600. */
  601. class HttpContext {
  602. map = new Map();
  603. /**
  604. * Store a value in the context. If a value is already present it will be overwritten.
  605. *
  606. * @param token The reference to an instance of `HttpContextToken`.
  607. * @param value The value to store.
  608. *
  609. * @returns A reference to itself for easy chaining.
  610. */
  611. set(token, value) {
  612. this.map.set(token, value);
  613. return this;
  614. }
  615. /**
  616. * Retrieve the value associated with the given token.
  617. *
  618. * @param token The reference to an instance of `HttpContextToken`.
  619. *
  620. * @returns The stored value or default if one is defined.
  621. */
  622. get(token) {
  623. if (!this.map.has(token)) {
  624. this.map.set(token, token.defaultValue());
  625. }
  626. return this.map.get(token);
  627. }
  628. /**
  629. * Delete the value associated with the given token.
  630. *
  631. * @param token The reference to an instance of `HttpContextToken`.
  632. *
  633. * @returns A reference to itself for easy chaining.
  634. */
  635. delete(token) {
  636. this.map.delete(token);
  637. return this;
  638. }
  639. /**
  640. * Checks for existence of a given token.
  641. *
  642. * @param token The reference to an instance of `HttpContextToken`.
  643. *
  644. * @returns True if the token exists, false otherwise.
  645. */
  646. has(token) {
  647. return this.map.has(token);
  648. }
  649. /**
  650. * @returns a list of tokens currently stored in the context.
  651. */
  652. keys() {
  653. return this.map.keys();
  654. }
  655. }
  656. /**
  657. * Determine whether the given HTTP method may include a body.
  658. */
  659. function mightHaveBody(method) {
  660. switch (method) {
  661. case 'DELETE':
  662. case 'GET':
  663. case 'HEAD':
  664. case 'OPTIONS':
  665. case 'JSONP':
  666. return false;
  667. default:
  668. return true;
  669. }
  670. }
  671. /**
  672. * Safely assert whether the given value is an ArrayBuffer.
  673. *
  674. * In some execution environments ArrayBuffer is not defined.
  675. */
  676. function isArrayBuffer(value) {
  677. return typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer;
  678. }
  679. /**
  680. * Safely assert whether the given value is a Blob.
  681. *
  682. * In some execution environments Blob is not defined.
  683. */
  684. function isBlob(value) {
  685. return typeof Blob !== 'undefined' && value instanceof Blob;
  686. }
  687. /**
  688. * Safely assert whether the given value is a FormData instance.
  689. *
  690. * In some execution environments FormData is not defined.
  691. */
  692. function isFormData(value) {
  693. return typeof FormData !== 'undefined' && value instanceof FormData;
  694. }
  695. /**
  696. * Safely assert whether the given value is a URLSearchParams instance.
  697. *
  698. * In some execution environments URLSearchParams is not defined.
  699. */
  700. function isUrlSearchParams(value) {
  701. return typeof URLSearchParams !== 'undefined' && value instanceof URLSearchParams;
  702. }
  703. /**
  704. * `Content-Type` is an HTTP header used to indicate the media type
  705. * (also known as MIME type) of the resource being sent to the client
  706. * or received from the server.
  707. */
  708. const CONTENT_TYPE_HEADER = 'Content-Type';
  709. /**
  710. * The `Accept` header is an HTTP request header that indicates the media types
  711. * (or content types) the client is willing to receive from the server.
  712. */
  713. const ACCEPT_HEADER = 'Accept';
  714. /**
  715. * `X-Request-URL` is a custom HTTP header used in older browser versions,
  716. * including Firefox (< 32), Chrome (< 37), Safari (< 8), and Internet Explorer,
  717. * to include the full URL of the request in cross-origin requests.
  718. */
  719. const X_REQUEST_URL_HEADER = 'X-Request-URL';
  720. /**
  721. * `text/plain` is a content type used to indicate that the content being
  722. * sent is plain text with no special formatting or structured data
  723. * like HTML, XML, or JSON.
  724. */
  725. const TEXT_CONTENT_TYPE = 'text/plain';
  726. /**
  727. * `application/json` is a content type used to indicate that the content
  728. * being sent is in the JSON format.
  729. */
  730. const JSON_CONTENT_TYPE = 'application/json';
  731. /**
  732. * `application/json, text/plain, *\/*` is a content negotiation string often seen in the
  733. * Accept header of HTTP requests. It indicates the types of content the client is willing
  734. * to accept from the server, with a preference for `application/json` and `text/plain`,
  735. * but also accepting any other type (*\/*).
  736. */
  737. const ACCEPT_HEADER_VALUE = `${JSON_CONTENT_TYPE}, ${TEXT_CONTENT_TYPE}, */*`;
  738. /**
  739. * An outgoing HTTP request with an optional typed body.
  740. *
  741. * `HttpRequest` represents an outgoing request, including URL, method,
  742. * headers, body, and other request configuration options. Instances should be
  743. * assumed to be immutable. To modify a `HttpRequest`, the `clone`
  744. * method should be used.
  745. *
  746. * @publicApi
  747. */
  748. class HttpRequest {
  749. url;
  750. /**
  751. * The request body, or `null` if one isn't set.
  752. *
  753. * Bodies are not enforced to be immutable, as they can include a reference to any
  754. * user-defined data type. However, interceptors should take care to preserve
  755. * idempotence by treating them as such.
  756. */
  757. body = null;
  758. /**
  759. * Outgoing headers for this request.
  760. */
  761. headers;
  762. /**
  763. * Shared and mutable context that can be used by interceptors
  764. */
  765. context;
  766. /**
  767. * Whether this request should be made in a way that exposes progress events.
  768. *
  769. * Progress events are expensive (change detection runs on each event) and so
  770. * they should only be requested if the consumer intends to monitor them.
  771. *
  772. * Note: The `FetchBackend` doesn't support progress report on uploads.
  773. */
  774. reportProgress = false;
  775. /**
  776. * Whether this request should be sent with outgoing credentials (cookies).
  777. */
  778. withCredentials = false;
  779. /**
  780. * The credentials mode of the request, which determines how cookies and HTTP authentication are handled.
  781. * This can affect whether cookies are sent with the request, and how authentication is handled.
  782. */
  783. credentials;
  784. /**
  785. * When using the fetch implementation and set to `true`, the browser will not abort the associated request if the page that initiated it is unloaded before the request is complete.
  786. */
  787. keepalive = false;
  788. /**
  789. * Controls how the request will interact with the browser's HTTP cache.
  790. * This affects whether a response is retrieved from the cache, how it is stored, or if it bypasses the cache altogether.
  791. */
  792. cache;
  793. /**
  794. * Indicates the relative priority of the request. This may be used by the browser to decide the order in which requests are dispatched and resources fetched.
  795. */
  796. priority;
  797. /**
  798. * The mode of the request, which determines how the request will interact with the browser's security model.
  799. * This can affect things like CORS (Cross-Origin Resource Sharing) and same-origin policies.
  800. */
  801. mode;
  802. /**
  803. * The redirect mode of the request, which determines how redirects are handled.
  804. * This can affect whether the request follows redirects automatically, or if it fails when a redirect occurs.
  805. */
  806. redirect;
  807. /**
  808. * The expected response type of the server.
  809. *
  810. * This is used to parse the response appropriately before returning it to
  811. * the requestee.
  812. */
  813. responseType = 'json';
  814. /**
  815. * The outgoing HTTP request method.
  816. */
  817. method;
  818. /**
  819. * Outgoing URL parameters.
  820. *
  821. * To pass a string representation of HTTP parameters in the URL-query-string format,
  822. * the `HttpParamsOptions`' `fromString` may be used. For example:
  823. *
  824. * ```ts
  825. * new HttpParams({fromString: 'angular=awesome'})
  826. * ```
  827. */
  828. params;
  829. /**
  830. * The outgoing URL with all URL parameters set.
  831. */
  832. urlWithParams;
  833. /**
  834. * The HttpTransferCache option for the request
  835. */
  836. transferCache;
  837. /**
  838. * The timeout for the backend HTTP request in ms.
  839. */
  840. timeout;
  841. constructor(method, url, third, fourth) {
  842. this.url = url;
  843. this.method = method.toUpperCase();
  844. // Next, need to figure out which argument holds the HttpRequestInit
  845. // options, if any.
  846. let options;
  847. // Check whether a body argument is expected. The only valid way to omit
  848. // the body argument is to use a known no-body method like GET.
  849. if (mightHaveBody(this.method) || !!fourth) {
  850. // Body is the third argument, options are the fourth.
  851. this.body = third !== undefined ? third : null;
  852. options = fourth;
  853. }
  854. else {
  855. // No body required, options are the third argument. The body stays null.
  856. options = third;
  857. }
  858. // If options have been passed, interpret them.
  859. if (options) {
  860. // Normalize reportProgress and withCredentials.
  861. this.reportProgress = !!options.reportProgress;
  862. this.withCredentials = !!options.withCredentials;
  863. this.keepalive = !!options.keepalive;
  864. // Override default response type of 'json' if one is provided.
  865. if (!!options.responseType) {
  866. this.responseType = options.responseType;
  867. }
  868. // Override headers if they're provided.
  869. if (options.headers) {
  870. this.headers = options.headers;
  871. }
  872. if (options.context) {
  873. this.context = options.context;
  874. }
  875. if (options.params) {
  876. this.params = options.params;
  877. }
  878. if (options.priority) {
  879. this.priority = options.priority;
  880. }
  881. if (options.cache) {
  882. this.cache = options.cache;
  883. }
  884. if (options.credentials) {
  885. this.credentials = options.credentials;
  886. }
  887. if (typeof options.timeout === 'number') {
  888. // XHR will ignore any value below 1. AbortSignals only accept unsigned integers.
  889. if (options.timeout < 1 || !Number.isInteger(options.timeout)) {
  890. // TODO: create a runtime error
  891. throw new Error(ngDevMode ? '`timeout` must be a positive integer value' : '');
  892. }
  893. this.timeout = options.timeout;
  894. }
  895. if (options.mode) {
  896. this.mode = options.mode;
  897. }
  898. if (options.redirect) {
  899. this.redirect = options.redirect;
  900. }
  901. // We do want to assign transferCache even if it's falsy (false is valid value)
  902. this.transferCache = options.transferCache;
  903. }
  904. // If no headers have been passed in, construct a new HttpHeaders instance.
  905. this.headers ??= new HttpHeaders();
  906. // If no context have been passed in, construct a new HttpContext instance.
  907. this.context ??= new HttpContext();
  908. // If no parameters have been passed in, construct a new HttpUrlEncodedParams instance.
  909. if (!this.params) {
  910. this.params = new HttpParams();
  911. this.urlWithParams = url;
  912. }
  913. else {
  914. // Encode the parameters to a string in preparation for inclusion in the URL.
  915. const params = this.params.toString();
  916. if (params.length === 0) {
  917. // No parameters, the visible URL is just the URL given at creation time.
  918. this.urlWithParams = url;
  919. }
  920. else {
  921. // Does the URL already have query parameters? Look for '?'.
  922. const qIdx = url.indexOf('?');
  923. // There are 3 cases to handle:
  924. // 1) No existing parameters -> append '?' followed by params.
  925. // 2) '?' exists and is followed by existing query string ->
  926. // append '&' followed by params.
  927. // 3) '?' exists at the end of the url -> append params directly.
  928. // This basically amounts to determining the character, if any, with
  929. // which to join the URL and parameters.
  930. const sep = qIdx === -1 ? '?' : qIdx < url.length - 1 ? '&' : '';
  931. this.urlWithParams = url + sep + params;
  932. }
  933. }
  934. }
  935. /**
  936. * Transform the free-form body into a serialized format suitable for
  937. * transmission to the server.
  938. */
  939. serializeBody() {
  940. // If no body is present, no need to serialize it.
  941. if (this.body === null) {
  942. return null;
  943. }
  944. // Check whether the body is already in a serialized form. If so,
  945. // it can just be returned directly.
  946. if (typeof this.body === 'string' ||
  947. isArrayBuffer(this.body) ||
  948. isBlob(this.body) ||
  949. isFormData(this.body) ||
  950. isUrlSearchParams(this.body)) {
  951. return this.body;
  952. }
  953. // Check whether the body is an instance of HttpUrlEncodedParams.
  954. if (this.body instanceof HttpParams) {
  955. return this.body.toString();
  956. }
  957. // Check whether the body is an object or array, and serialize with JSON if so.
  958. if (typeof this.body === 'object' ||
  959. typeof this.body === 'boolean' ||
  960. Array.isArray(this.body)) {
  961. return JSON.stringify(this.body);
  962. }
  963. // Fall back on toString() for everything else.
  964. return this.body.toString();
  965. }
  966. /**
  967. * Examine the body and attempt to infer an appropriate MIME type
  968. * for it.
  969. *
  970. * If no such type can be inferred, this method will return `null`.
  971. */
  972. detectContentTypeHeader() {
  973. // An empty body has no content type.
  974. if (this.body === null) {
  975. return null;
  976. }
  977. // FormData bodies rely on the browser's content type assignment.
  978. if (isFormData(this.body)) {
  979. return null;
  980. }
  981. // Blobs usually have their own content type. If it doesn't, then
  982. // no type can be inferred.
  983. if (isBlob(this.body)) {
  984. return this.body.type || null;
  985. }
  986. // Array buffers have unknown contents and thus no type can be inferred.
  987. if (isArrayBuffer(this.body)) {
  988. return null;
  989. }
  990. // Technically, strings could be a form of JSON data, but it's safe enough
  991. // to assume they're plain strings.
  992. if (typeof this.body === 'string') {
  993. return TEXT_CONTENT_TYPE;
  994. }
  995. // `HttpUrlEncodedParams` has its own content-type.
  996. if (this.body instanceof HttpParams) {
  997. return 'application/x-www-form-urlencoded;charset=UTF-8';
  998. }
  999. // Arrays, objects, boolean and numbers will be encoded as JSON.
  1000. if (typeof this.body === 'object' ||
  1001. typeof this.body === 'number' ||
  1002. typeof this.body === 'boolean') {
  1003. return JSON_CONTENT_TYPE;
  1004. }
  1005. // No type could be inferred.
  1006. return null;
  1007. }
  1008. clone(update = {}) {
  1009. // For method, url, and responseType, take the current value unless
  1010. // it is overridden in the update hash.
  1011. const method = update.method || this.method;
  1012. const url = update.url || this.url;
  1013. const responseType = update.responseType || this.responseType;
  1014. const keepalive = update.keepalive ?? this.keepalive;
  1015. const priority = update.priority || this.priority;
  1016. const cache = update.cache || this.cache;
  1017. const mode = update.mode || this.mode;
  1018. const redirect = update.redirect || this.redirect;
  1019. const credentials = update.credentials || this.credentials;
  1020. // Carefully handle the transferCache to differentiate between
  1021. // `false` and `undefined` in the update args.
  1022. const transferCache = update.transferCache ?? this.transferCache;
  1023. const timeout = update.timeout ?? this.timeout;
  1024. // The body is somewhat special - a `null` value in update.body means
  1025. // whatever current body is present is being overridden with an empty
  1026. // body, whereas an `undefined` value in update.body implies no
  1027. // override.
  1028. const body = update.body !== undefined ? update.body : this.body;
  1029. // Carefully handle the boolean options to differentiate between
  1030. // `false` and `undefined` in the update args.
  1031. const withCredentials = update.withCredentials ?? this.withCredentials;
  1032. const reportProgress = update.reportProgress ?? this.reportProgress;
  1033. // Headers and params may be appended to if `setHeaders` or
  1034. // `setParams` are used.
  1035. let headers = update.headers || this.headers;
  1036. let params = update.params || this.params;
  1037. // Pass on context if needed
  1038. const context = update.context ?? this.context;
  1039. // Check whether the caller has asked to add headers.
  1040. if (update.setHeaders !== undefined) {
  1041. // Set every requested header.
  1042. headers = Object.keys(update.setHeaders).reduce((headers, name) => headers.set(name, update.setHeaders[name]), headers);
  1043. }
  1044. // Check whether the caller has asked to set params.
  1045. if (update.setParams) {
  1046. // Set every requested param.
  1047. params = Object.keys(update.setParams).reduce((params, param) => params.set(param, update.setParams[param]), params);
  1048. }
  1049. // Finally, construct the new HttpRequest using the pieces from above.
  1050. return new HttpRequest(method, url, body, {
  1051. params,
  1052. headers,
  1053. context,
  1054. reportProgress,
  1055. responseType,
  1056. withCredentials,
  1057. transferCache,
  1058. keepalive,
  1059. cache,
  1060. priority,
  1061. timeout,
  1062. mode,
  1063. redirect,
  1064. credentials,
  1065. });
  1066. }
  1067. }
  1068. /**
  1069. * Type enumeration for the different kinds of `HttpEvent`.
  1070. *
  1071. * @publicApi
  1072. */
  1073. var HttpEventType;
  1074. (function (HttpEventType) {
  1075. /**
  1076. * The request was sent out over the wire.
  1077. */
  1078. HttpEventType[HttpEventType["Sent"] = 0] = "Sent";
  1079. /**
  1080. * An upload progress event was received.
  1081. *
  1082. * Note: The `FetchBackend` doesn't support progress report on uploads.
  1083. */
  1084. HttpEventType[HttpEventType["UploadProgress"] = 1] = "UploadProgress";
  1085. /**
  1086. * The response status code and headers were received.
  1087. */
  1088. HttpEventType[HttpEventType["ResponseHeader"] = 2] = "ResponseHeader";
  1089. /**
  1090. * A download progress event was received.
  1091. */
  1092. HttpEventType[HttpEventType["DownloadProgress"] = 3] = "DownloadProgress";
  1093. /**
  1094. * The full response including the body was received.
  1095. */
  1096. HttpEventType[HttpEventType["Response"] = 4] = "Response";
  1097. /**
  1098. * A custom event from an interceptor or a backend.
  1099. */
  1100. HttpEventType[HttpEventType["User"] = 5] = "User";
  1101. })(HttpEventType || (HttpEventType = {}));
  1102. /**
  1103. * Base class for both `HttpResponse` and `HttpHeaderResponse`.
  1104. *
  1105. * @publicApi
  1106. */
  1107. class HttpResponseBase {
  1108. /**
  1109. * All response headers.
  1110. */
  1111. headers;
  1112. /**
  1113. * Response status code.
  1114. */
  1115. status;
  1116. /**
  1117. * Textual description of response status code, defaults to OK.
  1118. *
  1119. * Do not depend on this.
  1120. */
  1121. statusText;
  1122. /**
  1123. * URL of the resource retrieved, or null if not available.
  1124. */
  1125. url;
  1126. /**
  1127. * Whether the status code falls in the 2xx range.
  1128. */
  1129. ok;
  1130. /**
  1131. * Type of the response, narrowed to either the full response or the header.
  1132. */
  1133. type;
  1134. /**
  1135. * Super-constructor for all responses.
  1136. *
  1137. * The single parameter accepted is an initialization hash. Any properties
  1138. * of the response passed there will override the default values.
  1139. */
  1140. constructor(init, defaultStatus = 200, defaultStatusText = 'OK') {
  1141. // If the hash has values passed, use them to initialize the response.
  1142. // Otherwise use the default values.
  1143. this.headers = init.headers || new HttpHeaders();
  1144. this.status = init.status !== undefined ? init.status : defaultStatus;
  1145. this.statusText = init.statusText || defaultStatusText;
  1146. this.url = init.url || null;
  1147. // Cache the ok value to avoid defining a getter.
  1148. this.ok = this.status >= 200 && this.status < 300;
  1149. }
  1150. }
  1151. /**
  1152. * A partial HTTP response which only includes the status and header data,
  1153. * but no response body.
  1154. *
  1155. * `HttpHeaderResponse` is a `HttpEvent` available on the response
  1156. * event stream, only when progress events are requested.
  1157. *
  1158. * @publicApi
  1159. */
  1160. class HttpHeaderResponse extends HttpResponseBase {
  1161. /**
  1162. * Create a new `HttpHeaderResponse` with the given parameters.
  1163. */
  1164. constructor(init = {}) {
  1165. super(init);
  1166. }
  1167. type = HttpEventType.ResponseHeader;
  1168. /**
  1169. * Copy this `HttpHeaderResponse`, overriding its contents with the
  1170. * given parameter hash.
  1171. */
  1172. clone(update = {}) {
  1173. // Perform a straightforward initialization of the new HttpHeaderResponse,
  1174. // overriding the current parameters with new ones if given.
  1175. return new HttpHeaderResponse({
  1176. headers: update.headers || this.headers,
  1177. status: update.status !== undefined ? update.status : this.status,
  1178. statusText: update.statusText || this.statusText,
  1179. url: update.url || this.url || undefined,
  1180. });
  1181. }
  1182. }
  1183. /**
  1184. * A full HTTP response, including a typed response body (which may be `null`
  1185. * if one was not returned).
  1186. *
  1187. * `HttpResponse` is a `HttpEvent` available on the response event
  1188. * stream.
  1189. *
  1190. * @publicApi
  1191. */
  1192. class HttpResponse extends HttpResponseBase {
  1193. /**
  1194. * The response body, or `null` if one was not returned.
  1195. */
  1196. body;
  1197. /**
  1198. * Construct a new `HttpResponse`.
  1199. */
  1200. constructor(init = {}) {
  1201. super(init);
  1202. this.body = init.body !== undefined ? init.body : null;
  1203. }
  1204. type = HttpEventType.Response;
  1205. clone(update = {}) {
  1206. return new HttpResponse({
  1207. body: update.body !== undefined ? update.body : this.body,
  1208. headers: update.headers || this.headers,
  1209. status: update.status !== undefined ? update.status : this.status,
  1210. statusText: update.statusText || this.statusText,
  1211. url: update.url || this.url || undefined,
  1212. });
  1213. }
  1214. }
  1215. /**
  1216. * A response that represents an error or failure, either from a
  1217. * non-successful HTTP status, an error while executing the request,
  1218. * or some other failure which occurred during the parsing of the response.
  1219. *
  1220. * Any error returned on the `Observable` response stream will be
  1221. * wrapped in an `HttpErrorResponse` to provide additional context about
  1222. * the state of the HTTP layer when the error occurred. The error property
  1223. * will contain either a wrapped Error object or the error response returned
  1224. * from the server.
  1225. *
  1226. * @publicApi
  1227. */
  1228. class HttpErrorResponse extends HttpResponseBase {
  1229. name = 'HttpErrorResponse';
  1230. message;
  1231. error;
  1232. /**
  1233. * Errors are never okay, even when the status code is in the 2xx success range.
  1234. */
  1235. ok = false;
  1236. constructor(init) {
  1237. // Initialize with a default status of 0 / Unknown Error.
  1238. super(init, 0, 'Unknown Error');
  1239. // If the response was successful, then this was a parse error. Otherwise, it was
  1240. // a protocol-level failure of some sort. Either the request failed in transit
  1241. // or the server returned an unsuccessful status code.
  1242. if (this.status >= 200 && this.status < 300) {
  1243. this.message = `Http failure during parsing for ${init.url || '(unknown url)'}`;
  1244. }
  1245. else {
  1246. this.message = `Http failure response for ${init.url || '(unknown url)'}: ${init.status} ${init.statusText}`;
  1247. }
  1248. this.error = init.error || null;
  1249. }
  1250. }
  1251. /**
  1252. * We use these constant to prevent pulling the whole HttpStatusCode enum
  1253. * Those are the only ones referenced directly by the framework
  1254. */
  1255. const HTTP_STATUS_CODE_OK = 200;
  1256. const HTTP_STATUS_CODE_NO_CONTENT = 204;
  1257. /**
  1258. * Http status codes.
  1259. * As per https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
  1260. * @publicApi
  1261. */
  1262. var HttpStatusCode;
  1263. (function (HttpStatusCode) {
  1264. HttpStatusCode[HttpStatusCode["Continue"] = 100] = "Continue";
  1265. HttpStatusCode[HttpStatusCode["SwitchingProtocols"] = 101] = "SwitchingProtocols";
  1266. HttpStatusCode[HttpStatusCode["Processing"] = 102] = "Processing";
  1267. HttpStatusCode[HttpStatusCode["EarlyHints"] = 103] = "EarlyHints";
  1268. HttpStatusCode[HttpStatusCode["Ok"] = 200] = "Ok";
  1269. HttpStatusCode[HttpStatusCode["Created"] = 201] = "Created";
  1270. HttpStatusCode[HttpStatusCode["Accepted"] = 202] = "Accepted";
  1271. HttpStatusCode[HttpStatusCode["NonAuthoritativeInformation"] = 203] = "NonAuthoritativeInformation";
  1272. HttpStatusCode[HttpStatusCode["NoContent"] = 204] = "NoContent";
  1273. HttpStatusCode[HttpStatusCode["ResetContent"] = 205] = "ResetContent";
  1274. HttpStatusCode[HttpStatusCode["PartialContent"] = 206] = "PartialContent";
  1275. HttpStatusCode[HttpStatusCode["MultiStatus"] = 207] = "MultiStatus";
  1276. HttpStatusCode[HttpStatusCode["AlreadyReported"] = 208] = "AlreadyReported";
  1277. HttpStatusCode[HttpStatusCode["ImUsed"] = 226] = "ImUsed";
  1278. HttpStatusCode[HttpStatusCode["MultipleChoices"] = 300] = "MultipleChoices";
  1279. HttpStatusCode[HttpStatusCode["MovedPermanently"] = 301] = "MovedPermanently";
  1280. HttpStatusCode[HttpStatusCode["Found"] = 302] = "Found";
  1281. HttpStatusCode[HttpStatusCode["SeeOther"] = 303] = "SeeOther";
  1282. HttpStatusCode[HttpStatusCode["NotModified"] = 304] = "NotModified";
  1283. HttpStatusCode[HttpStatusCode["UseProxy"] = 305] = "UseProxy";
  1284. HttpStatusCode[HttpStatusCode["Unused"] = 306] = "Unused";
  1285. HttpStatusCode[HttpStatusCode["TemporaryRedirect"] = 307] = "TemporaryRedirect";
  1286. HttpStatusCode[HttpStatusCode["PermanentRedirect"] = 308] = "PermanentRedirect";
  1287. HttpStatusCode[HttpStatusCode["BadRequest"] = 400] = "BadRequest";
  1288. HttpStatusCode[HttpStatusCode["Unauthorized"] = 401] = "Unauthorized";
  1289. HttpStatusCode[HttpStatusCode["PaymentRequired"] = 402] = "PaymentRequired";
  1290. HttpStatusCode[HttpStatusCode["Forbidden"] = 403] = "Forbidden";
  1291. HttpStatusCode[HttpStatusCode["NotFound"] = 404] = "NotFound";
  1292. HttpStatusCode[HttpStatusCode["MethodNotAllowed"] = 405] = "MethodNotAllowed";
  1293. HttpStatusCode[HttpStatusCode["NotAcceptable"] = 406] = "NotAcceptable";
  1294. HttpStatusCode[HttpStatusCode["ProxyAuthenticationRequired"] = 407] = "ProxyAuthenticationRequired";
  1295. HttpStatusCode[HttpStatusCode["RequestTimeout"] = 408] = "RequestTimeout";
  1296. HttpStatusCode[HttpStatusCode["Conflict"] = 409] = "Conflict";
  1297. HttpStatusCode[HttpStatusCode["Gone"] = 410] = "Gone";
  1298. HttpStatusCode[HttpStatusCode["LengthRequired"] = 411] = "LengthRequired";
  1299. HttpStatusCode[HttpStatusCode["PreconditionFailed"] = 412] = "PreconditionFailed";
  1300. HttpStatusCode[HttpStatusCode["PayloadTooLarge"] = 413] = "PayloadTooLarge";
  1301. HttpStatusCode[HttpStatusCode["UriTooLong"] = 414] = "UriTooLong";
  1302. HttpStatusCode[HttpStatusCode["UnsupportedMediaType"] = 415] = "UnsupportedMediaType";
  1303. HttpStatusCode[HttpStatusCode["RangeNotSatisfiable"] = 416] = "RangeNotSatisfiable";
  1304. HttpStatusCode[HttpStatusCode["ExpectationFailed"] = 417] = "ExpectationFailed";
  1305. HttpStatusCode[HttpStatusCode["ImATeapot"] = 418] = "ImATeapot";
  1306. HttpStatusCode[HttpStatusCode["MisdirectedRequest"] = 421] = "MisdirectedRequest";
  1307. HttpStatusCode[HttpStatusCode["UnprocessableEntity"] = 422] = "UnprocessableEntity";
  1308. HttpStatusCode[HttpStatusCode["Locked"] = 423] = "Locked";
  1309. HttpStatusCode[HttpStatusCode["FailedDependency"] = 424] = "FailedDependency";
  1310. HttpStatusCode[HttpStatusCode["TooEarly"] = 425] = "TooEarly";
  1311. HttpStatusCode[HttpStatusCode["UpgradeRequired"] = 426] = "UpgradeRequired";
  1312. HttpStatusCode[HttpStatusCode["PreconditionRequired"] = 428] = "PreconditionRequired";
  1313. HttpStatusCode[HttpStatusCode["TooManyRequests"] = 429] = "TooManyRequests";
  1314. HttpStatusCode[HttpStatusCode["RequestHeaderFieldsTooLarge"] = 431] = "RequestHeaderFieldsTooLarge";
  1315. HttpStatusCode[HttpStatusCode["UnavailableForLegalReasons"] = 451] = "UnavailableForLegalReasons";
  1316. HttpStatusCode[HttpStatusCode["InternalServerError"] = 500] = "InternalServerError";
  1317. HttpStatusCode[HttpStatusCode["NotImplemented"] = 501] = "NotImplemented";
  1318. HttpStatusCode[HttpStatusCode["BadGateway"] = 502] = "BadGateway";
  1319. HttpStatusCode[HttpStatusCode["ServiceUnavailable"] = 503] = "ServiceUnavailable";
  1320. HttpStatusCode[HttpStatusCode["GatewayTimeout"] = 504] = "GatewayTimeout";
  1321. HttpStatusCode[HttpStatusCode["HttpVersionNotSupported"] = 505] = "HttpVersionNotSupported";
  1322. HttpStatusCode[HttpStatusCode["VariantAlsoNegotiates"] = 506] = "VariantAlsoNegotiates";
  1323. HttpStatusCode[HttpStatusCode["InsufficientStorage"] = 507] = "InsufficientStorage";
  1324. HttpStatusCode[HttpStatusCode["LoopDetected"] = 508] = "LoopDetected";
  1325. HttpStatusCode[HttpStatusCode["NotExtended"] = 510] = "NotExtended";
  1326. HttpStatusCode[HttpStatusCode["NetworkAuthenticationRequired"] = 511] = "NetworkAuthenticationRequired";
  1327. })(HttpStatusCode || (HttpStatusCode = {}));
  1328. /**
  1329. * Constructs an instance of `HttpRequestOptions<T>` from a source `HttpMethodOptions` and
  1330. * the given `body`. This function clones the object and adds the body.
  1331. *
  1332. * Note that the `responseType` *options* value is a String that identifies the
  1333. * single data type of the response.
  1334. * A single overload version of the method handles each response type.
  1335. * The value of `responseType` cannot be a union, as the combined signature could imply.
  1336. *
  1337. */
  1338. function addBody(options, body) {
  1339. return {
  1340. body,
  1341. headers: options.headers,
  1342. context: options.context,
  1343. observe: options.observe,
  1344. params: options.params,
  1345. reportProgress: options.reportProgress,
  1346. responseType: options.responseType,
  1347. withCredentials: options.withCredentials,
  1348. transferCache: options.transferCache,
  1349. keepalive: options.keepalive,
  1350. priority: options.priority,
  1351. cache: options.cache,
  1352. mode: options.mode,
  1353. redirect: options.redirect,
  1354. };
  1355. }
  1356. /**
  1357. * Performs HTTP requests.
  1358. * This service is available as an injectable class, with methods to perform HTTP requests.
  1359. * Each request method has multiple signatures, and the return type varies based on
  1360. * the signature that is called (mainly the values of `observe` and `responseType`).
  1361. *
  1362. * Note that the `responseType` *options* value is a String that identifies the
  1363. * single data type of the response.
  1364. * A single overload version of the method handles each response type.
  1365. * The value of `responseType` cannot be a union, as the combined signature could imply.
  1366. *
  1367. * @usageNotes
  1368. *
  1369. * ### HTTP Request Example
  1370. *
  1371. * ```ts
  1372. * // GET heroes whose name contains search term
  1373. * searchHeroes(term: string): observable<Hero[]>{
  1374. *
  1375. * const params = new HttpParams({fromString: 'name=term'});
  1376. * return this.httpClient.request('GET', this.heroesUrl, {responseType:'json', params});
  1377. * }
  1378. * ```
  1379. *
  1380. * Alternatively, the parameter string can be used without invoking HttpParams
  1381. * by directly joining to the URL.
  1382. * ```ts
  1383. * this.httpClient.request('GET', this.heroesUrl + '?' + 'name=term', {responseType:'json'});
  1384. * ```
  1385. *
  1386. *
  1387. * ### JSONP Example
  1388. * ```ts
  1389. * requestJsonp(url, callback = 'callback') {
  1390. * return this.httpClient.jsonp(this.heroesURL, callback);
  1391. * }
  1392. * ```
  1393. *
  1394. * ### PATCH Example
  1395. * ```ts
  1396. * // PATCH one of the heroes' name
  1397. * patchHero (id: number, heroName: string): Observable<{}> {
  1398. * const url = `${this.heroesUrl}/${id}`; // PATCH api/heroes/42
  1399. * return this.httpClient.patch(url, {name: heroName}, httpOptions)
  1400. * .pipe(catchError(this.handleError('patchHero')));
  1401. * }
  1402. * ```
  1403. *
  1404. * @see [HTTP Guide](guide/http)
  1405. * @see [HTTP Request](api/common/http/HttpRequest)
  1406. *
  1407. * @publicApi
  1408. */
  1409. class HttpClient {
  1410. handler;
  1411. constructor(handler) {
  1412. this.handler = handler;
  1413. }
  1414. /**
  1415. * Constructs an observable for a generic HTTP request that, when subscribed,
  1416. * fires the request through the chain of registered interceptors and on to the
  1417. * server.
  1418. *
  1419. * You can pass an `HttpRequest` directly as the only parameter. In this case,
  1420. * the call returns an observable of the raw `HttpEvent` stream.
  1421. *
  1422. * Alternatively you can pass an HTTP method as the first parameter,
  1423. * a URL string as the second, and an options hash containing the request body as the third.
  1424. * See `addBody()`. In this case, the specified `responseType` and `observe` options determine the
  1425. * type of returned observable.
  1426. * * The `responseType` value determines how a successful response body is parsed.
  1427. * * If `responseType` is the default `json`, you can pass a type interface for the resulting
  1428. * object as a type parameter to the call.
  1429. *
  1430. * The `observe` value determines the return type, according to what you are interested in
  1431. * observing.
  1432. * * An `observe` value of events returns an observable of the raw `HttpEvent` stream, including
  1433. * progress events by default.
  1434. * * An `observe` value of response returns an observable of `HttpResponse<T>`,
  1435. * where the `T` parameter depends on the `responseType` and any optionally provided type
  1436. * parameter.
  1437. * * An `observe` value of body returns an observable of `<T>` with the same `T` body type.
  1438. *
  1439. */
  1440. request(first, url, options = {}) {
  1441. let req;
  1442. // First, check whether the primary argument is an instance of `HttpRequest`.
  1443. if (first instanceof HttpRequest) {
  1444. // It is. The other arguments must be undefined (per the signatures) and can be
  1445. // ignored.
  1446. req = first;
  1447. }
  1448. else {
  1449. // It's a string, so it represents a URL. Construct a request based on it,
  1450. // and incorporate the remaining arguments (assuming `GET` unless a method is
  1451. // provided.
  1452. // Figure out the headers.
  1453. let headers = undefined;
  1454. if (options.headers instanceof HttpHeaders) {
  1455. headers = options.headers;
  1456. }
  1457. else {
  1458. headers = new HttpHeaders(options.headers);
  1459. }
  1460. // Sort out parameters.
  1461. let params = undefined;
  1462. if (!!options.params) {
  1463. if (options.params instanceof HttpParams) {
  1464. params = options.params;
  1465. }
  1466. else {
  1467. params = new HttpParams({ fromObject: options.params });
  1468. }
  1469. }
  1470. // Construct the request.
  1471. req = new HttpRequest(first, url, options.body !== undefined ? options.body : null, {
  1472. headers,
  1473. context: options.context,
  1474. params,
  1475. reportProgress: options.reportProgress,
  1476. // By default, JSON is assumed to be returned for all calls.
  1477. responseType: options.responseType || 'json',
  1478. withCredentials: options.withCredentials,
  1479. transferCache: options.transferCache,
  1480. keepalive: options.keepalive,
  1481. priority: options.priority,
  1482. cache: options.cache,
  1483. mode: options.mode,
  1484. redirect: options.redirect,
  1485. credentials: options.credentials,
  1486. });
  1487. }
  1488. // Start with an Observable.of() the initial request, and run the handler (which
  1489. // includes all interceptors) inside a concatMap(). This way, the handler runs
  1490. // inside an Observable chain, which causes interceptors to be re-run on every
  1491. // subscription (this also makes retries re-run the handler, including interceptors).
  1492. const events$ = of(req).pipe(concatMap((req) => this.handler.handle(req)));
  1493. // If coming via the API signature which accepts a previously constructed HttpRequest,
  1494. // the only option is to get the event stream. Otherwise, return the event stream if
  1495. // that is what was requested.
  1496. if (first instanceof HttpRequest || options.observe === 'events') {
  1497. return events$;
  1498. }
  1499. // The requested stream contains either the full response or the body. In either
  1500. // case, the first step is to filter the event stream to extract a stream of
  1501. // responses(s).
  1502. const res$ = (events$.pipe(filter((event) => event instanceof HttpResponse)));
  1503. // Decide which stream to return.
  1504. switch (options.observe || 'body') {
  1505. case 'body':
  1506. // The requested stream is the body. Map the response stream to the response
  1507. // body. This could be done more simply, but a misbehaving interceptor might
  1508. // transform the response body into a different format and ignore the requested
  1509. // responseType. Guard against this by validating that the response is of the
  1510. // requested type.
  1511. switch (req.responseType) {
  1512. case 'arraybuffer':
  1513. return res$.pipe(map((res) => {
  1514. // Validate that the body is an ArrayBuffer.
  1515. if (res.body !== null && !(res.body instanceof ArrayBuffer)) {
  1516. throw new _RuntimeError(2806 /* RuntimeErrorCode.RESPONSE_IS_NOT_AN_ARRAY_BUFFER */, ngDevMode && 'Response is not an ArrayBuffer.');
  1517. }
  1518. return res.body;
  1519. }));
  1520. case 'blob':
  1521. return res$.pipe(map((res) => {
  1522. // Validate that the body is a Blob.
  1523. if (res.body !== null && !(res.body instanceof Blob)) {
  1524. throw new _RuntimeError(2807 /* RuntimeErrorCode.RESPONSE_IS_NOT_A_BLOB */, ngDevMode && 'Response is not a Blob.');
  1525. }
  1526. return res.body;
  1527. }));
  1528. case 'text':
  1529. return res$.pipe(map((res) => {
  1530. // Validate that the body is a string.
  1531. if (res.body !== null && typeof res.body !== 'string') {
  1532. throw new _RuntimeError(2808 /* RuntimeErrorCode.RESPONSE_IS_NOT_A_STRING */, ngDevMode && 'Response is not a string.');
  1533. }
  1534. return res.body;
  1535. }));
  1536. case 'json':
  1537. default:
  1538. // No validation needed for JSON responses, as they can be of any type.
  1539. return res$.pipe(map((res) => res.body));
  1540. }
  1541. case 'response':
  1542. // The response stream was requested directly, so return it.
  1543. return res$;
  1544. default:
  1545. // Guard against new future observe types being added.
  1546. throw new _RuntimeError(2809 /* RuntimeErrorCode.UNHANDLED_OBSERVE_TYPE */, ngDevMode && `Unreachable: unhandled observe type ${options.observe}}`);
  1547. }
  1548. }
  1549. /**
  1550. * Constructs an observable that, when subscribed, causes the configured
  1551. * `DELETE` request to execute on the server. See the individual overloads for
  1552. * details on the return type.
  1553. *
  1554. * @param url The endpoint URL.
  1555. * @param options The HTTP options to send with the request.
  1556. *
  1557. */
  1558. delete(url, options = {}) {
  1559. return this.request('DELETE', url, options);
  1560. }
  1561. /**
  1562. * Constructs an observable that, when subscribed, causes the configured
  1563. * `GET` request to execute on the server. See the individual overloads for
  1564. * details on the return type.
  1565. */
  1566. get(url, options = {}) {
  1567. return this.request('GET', url, options);
  1568. }
  1569. /**
  1570. * Constructs an observable that, when subscribed, causes the configured
  1571. * `HEAD` request to execute on the server. The `HEAD` method returns
  1572. * meta information about the resource without transferring the
  1573. * resource itself. See the individual overloads for
  1574. * details on the return type.
  1575. */
  1576. head(url, options = {}) {
  1577. return this.request('HEAD', url, options);
  1578. }
  1579. /**
  1580. * Constructs an `Observable` that, when subscribed, causes a request with the special method
  1581. * `JSONP` to be dispatched via the interceptor pipeline.
  1582. * The [JSONP pattern](https://en.wikipedia.org/wiki/JSONP) works around limitations of certain
  1583. * API endpoints that don't support newer,
  1584. * and preferable [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) protocol.
  1585. * JSONP treats the endpoint API as a JavaScript file and tricks the browser to process the
  1586. * requests even if the API endpoint is not located on the same domain (origin) as the client-side
  1587. * application making the request.
  1588. * The endpoint API must support JSONP callback for JSONP requests to work.
  1589. * The resource API returns the JSON response wrapped in a callback function.
  1590. * You can pass the callback function name as one of the query parameters.
  1591. * Note that JSONP requests can only be used with `GET` requests.
  1592. *
  1593. * @param url The resource URL.
  1594. * @param callbackParam The callback function name.
  1595. *
  1596. */
  1597. jsonp(url, callbackParam) {
  1598. return this.request('JSONP', url, {
  1599. params: new HttpParams().append(callbackParam, 'JSONP_CALLBACK'),
  1600. observe: 'body',
  1601. responseType: 'json',
  1602. });
  1603. }
  1604. /**
  1605. * Constructs an `Observable` that, when subscribed, causes the configured
  1606. * `OPTIONS` request to execute on the server. This method allows the client
  1607. * to determine the supported HTTP methods and other capabilities of an endpoint,
  1608. * without implying a resource action. See the individual overloads for
  1609. * details on the return type.
  1610. */
  1611. options(url, options = {}) {
  1612. return this.request('OPTIONS', url, options);
  1613. }
  1614. /**
  1615. * Constructs an observable that, when subscribed, causes the configured
  1616. * `PATCH` request to execute on the server. See the individual overloads for
  1617. * details on the return type.
  1618. */
  1619. patch(url, body, options = {}) {
  1620. return this.request('PATCH', url, addBody(options, body));
  1621. }
  1622. /**
  1623. * Constructs an observable that, when subscribed, causes the configured
  1624. * `POST` request to execute on the server. The server responds with the location of
  1625. * the replaced resource. See the individual overloads for
  1626. * details on the return type.
  1627. */
  1628. post(url, body, options = {}) {
  1629. return this.request('POST', url, addBody(options, body));
  1630. }
  1631. /**
  1632. * Constructs an observable that, when subscribed, causes the configured
  1633. * `PUT` request to execute on the server. The `PUT` method replaces an existing resource
  1634. * with a new set of values.
  1635. * See the individual overloads for details on the return type.
  1636. */
  1637. put(url, body, options = {}) {
  1638. return this.request('PUT', url, addBody(options, body));
  1639. }
  1640. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpClient, deps: [{ token: HttpHandler }], target: i0.ɵɵFactoryTarget.Injectable });
  1641. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpClient });
  1642. }
  1643. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpClient, decorators: [{
  1644. type: Injectable
  1645. }], ctorParameters: () => [{ type: HttpHandler }] });
  1646. const XSSI_PREFIX$1 = /^\)\]\}',?\n/;
  1647. /**
  1648. * Determine an appropriate URL for the response, by checking either
  1649. * response url or the X-Request-URL header.
  1650. */
  1651. function getResponseUrl$1(response) {
  1652. if (response.url) {
  1653. return response.url;
  1654. }
  1655. // stored as lowercase in the map
  1656. const xRequestUrl = X_REQUEST_URL_HEADER.toLocaleLowerCase();
  1657. return response.headers.get(xRequestUrl);
  1658. }
  1659. /**
  1660. * An internal injection token to reference `FetchBackend` implementation
  1661. * in a tree-shakable way.
  1662. */
  1663. const FETCH_BACKEND = new InjectionToken(typeof ngDevMode === 'undefined' || ngDevMode ? 'FETCH_BACKEND' : '');
  1664. /**
  1665. * Uses `fetch` to send requests to a backend server.
  1666. *
  1667. * This `FetchBackend` requires the support of the
  1668. * [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) which is available on all
  1669. * supported browsers and on Node.js v18 or later.
  1670. *
  1671. * @see {@link HttpHandler}
  1672. *
  1673. * @publicApi
  1674. */
  1675. class FetchBackend {
  1676. // We use an arrow function to always reference the current global implementation of `fetch`.
  1677. // This is helpful for cases when the global `fetch` implementation is modified by external code,
  1678. // see https://github.com/angular/angular/issues/57527.
  1679. fetchImpl = inject(FetchFactory, { optional: true })?.fetch ?? ((...args) => globalThis.fetch(...args));
  1680. ngZone = inject(NgZone);
  1681. destroyRef = inject(DestroyRef);
  1682. destroyed = false;
  1683. constructor() {
  1684. this.destroyRef.onDestroy(() => {
  1685. this.destroyed = true;
  1686. });
  1687. }
  1688. handle(request) {
  1689. return new Observable((observer) => {
  1690. const aborter = new AbortController();
  1691. this.doRequest(request, aborter.signal, observer).then(noop, (error) => observer.error(new HttpErrorResponse({ error })));
  1692. let timeoutId;
  1693. if (request.timeout) {
  1694. // TODO: Replace with AbortSignal.any([aborter.signal, AbortSignal.timeout(request.timeout)])
  1695. // when AbortSignal.any support is Baseline widely available (NET nov. 2026)
  1696. timeoutId = this.ngZone.runOutsideAngular(() => setTimeout(() => {
  1697. if (!aborter.signal.aborted) {
  1698. aborter.abort(new DOMException('signal timed out', 'TimeoutError'));
  1699. }
  1700. }, request.timeout));
  1701. }
  1702. return () => {
  1703. if (timeoutId !== undefined) {
  1704. clearTimeout(timeoutId);
  1705. }
  1706. aborter.abort();
  1707. };
  1708. });
  1709. }
  1710. async doRequest(request, signal, observer) {
  1711. const init = this.createRequestInit(request);
  1712. let response;
  1713. try {
  1714. // Run fetch outside of Angular zone.
  1715. // This is due to Node.js fetch implementation (Undici) which uses a number of setTimeouts to check if
  1716. // the response should eventually timeout which causes extra CD cycles every 500ms
  1717. const fetchPromise = this.ngZone.runOutsideAngular(() => this.fetchImpl(request.urlWithParams, { signal, ...init }));
  1718. // Make sure Zone.js doesn't trigger false-positive unhandled promise
  1719. // error in case the Promise is rejected synchronously. See function
  1720. // description for additional information.
  1721. silenceSuperfluousUnhandledPromiseRejection(fetchPromise);
  1722. // Send the `Sent` event before awaiting the response.
  1723. observer.next({ type: HttpEventType.Sent });
  1724. response = await fetchPromise;
  1725. }
  1726. catch (error) {
  1727. observer.error(new HttpErrorResponse({
  1728. error,
  1729. status: error.status ?? 0,
  1730. statusText: error.statusText,
  1731. url: request.urlWithParams,
  1732. headers: error.headers,
  1733. }));
  1734. return;
  1735. }
  1736. const headers = new HttpHeaders(response.headers);
  1737. const statusText = response.statusText;
  1738. const url = getResponseUrl$1(response) ?? request.urlWithParams;
  1739. let status = response.status;
  1740. let body = null;
  1741. if (request.reportProgress) {
  1742. observer.next(new HttpHeaderResponse({ headers, status, statusText, url }));
  1743. }
  1744. if (response.body) {
  1745. // Read Progress
  1746. const contentLength = response.headers.get('content-length');
  1747. const chunks = [];
  1748. const reader = response.body.getReader();
  1749. let receivedLength = 0;
  1750. let decoder;
  1751. let partialText;
  1752. // We have to check whether the Zone is defined in the global scope because this may be called
  1753. // when the zone is nooped.
  1754. const reqZone = typeof Zone !== 'undefined' && Zone.current;
  1755. let canceled = false;
  1756. // Perform response processing outside of Angular zone to
  1757. // ensure no excessive change detection runs are executed
  1758. // Here calling the async ReadableStreamDefaultReader.read() is responsible for triggering CD
  1759. await this.ngZone.runOutsideAngular(async () => {
  1760. while (true) {
  1761. // Prevent reading chunks if the app is destroyed. Otherwise, we risk doing
  1762. // unnecessary work or triggering side effects after teardown.
  1763. // This may happen if the app was explicitly destroyed before
  1764. // the response returned entirely.
  1765. if (this.destroyed) {
  1766. // Streams left in a pending state (due to `break` without cancel) may
  1767. // continue consuming or holding onto data behind the scenes.
  1768. // Calling `reader.cancel()` allows the browser or the underlying
  1769. // system to release any network or memory resources associated with the stream.
  1770. await reader.cancel();
  1771. canceled = true;
  1772. break;
  1773. }
  1774. const { done, value } = await reader.read();
  1775. if (done) {
  1776. break;
  1777. }
  1778. chunks.push(value);
  1779. receivedLength += value.length;
  1780. if (request.reportProgress) {
  1781. partialText =
  1782. request.responseType === 'text'
  1783. ? (partialText ?? '') +
  1784. (decoder ??= new TextDecoder()).decode(value, { stream: true })
  1785. : undefined;
  1786. const reportProgress = () => observer.next({
  1787. type: HttpEventType.DownloadProgress,
  1788. total: contentLength ? +contentLength : undefined,
  1789. loaded: receivedLength,
  1790. partialText,
  1791. });
  1792. reqZone ? reqZone.run(reportProgress) : reportProgress();
  1793. }
  1794. }
  1795. });
  1796. // We need to manage the canceled state — because the Streams API does not
  1797. // expose a direct `.state` property on the reader.
  1798. // We need to `return` because `parseBody` may not be able to parse chunks
  1799. // that were only partially read (due to cancellation caused by app destruction).
  1800. if (canceled) {
  1801. observer.complete();
  1802. return;
  1803. }
  1804. // Combine all chunks.
  1805. const chunksAll = this.concatChunks(chunks, receivedLength);
  1806. try {
  1807. const contentType = response.headers.get(CONTENT_TYPE_HEADER) ?? '';
  1808. body = this.parseBody(request, chunksAll, contentType);
  1809. }
  1810. catch (error) {
  1811. // Body loading or parsing failed
  1812. observer.error(new HttpErrorResponse({
  1813. error,
  1814. headers: new HttpHeaders(response.headers),
  1815. status: response.status,
  1816. statusText: response.statusText,
  1817. url: getResponseUrl$1(response) ?? request.urlWithParams,
  1818. }));
  1819. return;
  1820. }
  1821. }
  1822. // Same behavior as the XhrBackend
  1823. if (status === 0) {
  1824. status = body ? HTTP_STATUS_CODE_OK : 0;
  1825. }
  1826. // ok determines whether the response will be transmitted on the event or
  1827. // error channel. Unsuccessful status codes (not 2xx) will always be errors,
  1828. // but a successful status code can still result in an error if the user
  1829. // asked for JSON data and the body cannot be parsed as such.
  1830. const ok = status >= 200 && status < 300;
  1831. if (ok) {
  1832. observer.next(new HttpResponse({
  1833. body,
  1834. headers,
  1835. status,
  1836. statusText,
  1837. url,
  1838. }));
  1839. // The full body has been received and delivered, no further events
  1840. // are possible. This request is complete.
  1841. observer.complete();
  1842. }
  1843. else {
  1844. observer.error(new HttpErrorResponse({
  1845. error: body,
  1846. headers,
  1847. status,
  1848. statusText,
  1849. url,
  1850. }));
  1851. }
  1852. }
  1853. parseBody(request, binContent, contentType) {
  1854. switch (request.responseType) {
  1855. case 'json':
  1856. // stripping the XSSI when present
  1857. const text = new TextDecoder().decode(binContent).replace(XSSI_PREFIX$1, '');
  1858. return text === '' ? null : JSON.parse(text);
  1859. case 'text':
  1860. return new TextDecoder().decode(binContent);
  1861. case 'blob':
  1862. return new Blob([binContent], { type: contentType });
  1863. case 'arraybuffer':
  1864. return binContent.buffer;
  1865. }
  1866. }
  1867. createRequestInit(req) {
  1868. // We could share some of this logic with the XhrBackend
  1869. const headers = {};
  1870. let credentials;
  1871. // If the request has a credentials property, use it.
  1872. // Otherwise, if the request has withCredentials set to true, use 'include'.
  1873. credentials = req.credentials;
  1874. // If withCredentials is true should be set to 'include', for compatibility
  1875. if (req.withCredentials) {
  1876. // A warning is logged in development mode if the request has both
  1877. (typeof ngDevMode === 'undefined' || ngDevMode) && warningOptionsMessage(req);
  1878. credentials = 'include';
  1879. }
  1880. // Setting all the requested headers.
  1881. req.headers.forEach((name, values) => (headers[name] = values.join(',')));
  1882. // Add an Accept header if one isn't present already.
  1883. if (!req.headers.has(ACCEPT_HEADER)) {
  1884. headers[ACCEPT_HEADER] = ACCEPT_HEADER_VALUE;
  1885. }
  1886. // Auto-detect the Content-Type header if one isn't present already.
  1887. if (!req.headers.has(CONTENT_TYPE_HEADER)) {
  1888. const detectedType = req.detectContentTypeHeader();
  1889. // Sometimes Content-Type detection fails.
  1890. if (detectedType !== null) {
  1891. headers[CONTENT_TYPE_HEADER] = detectedType;
  1892. }
  1893. }
  1894. return {
  1895. body: req.serializeBody(),
  1896. method: req.method,
  1897. headers,
  1898. credentials,
  1899. keepalive: req.keepalive,
  1900. cache: req.cache,
  1901. priority: req.priority,
  1902. mode: req.mode,
  1903. redirect: req.redirect,
  1904. };
  1905. }
  1906. concatChunks(chunks, totalLength) {
  1907. const chunksAll = new Uint8Array(totalLength);
  1908. let position = 0;
  1909. for (const chunk of chunks) {
  1910. chunksAll.set(chunk, position);
  1911. position += chunk.length;
  1912. }
  1913. return chunksAll;
  1914. }
  1915. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: FetchBackend, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  1916. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: FetchBackend });
  1917. }
  1918. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: FetchBackend, decorators: [{
  1919. type: Injectable
  1920. }], ctorParameters: () => [] });
  1921. /**
  1922. * Abstract class to provide a mocked implementation of `fetch()`
  1923. */
  1924. class FetchFactory {
  1925. }
  1926. function noop() { }
  1927. function warningOptionsMessage(req) {
  1928. if (req.credentials && req.withCredentials) {
  1929. console.warn(_formatRuntimeError(2819 /* RuntimeErrorCode.WITH_CREDENTIALS_OVERRIDES_EXPLICIT_CREDENTIALS */, `Angular detected that a \`HttpClient\` request has both \`withCredentials: true\` and \`credentials: '${req.credentials}'\` options. The \`withCredentials\` option is overriding the explicit \`credentials\` setting to 'include'. Consider removing \`withCredentials\` and using \`credentials: '${req.credentials}'\` directly for clarity.`));
  1930. }
  1931. }
  1932. /**
  1933. * Zone.js treats a rejected promise that has not yet been awaited
  1934. * as an unhandled error. This function adds a noop `.then` to make
  1935. * sure that Zone.js doesn't throw an error if the Promise is rejected
  1936. * synchronously.
  1937. */
  1938. function silenceSuperfluousUnhandledPromiseRejection(promise) {
  1939. promise.then(noop, noop);
  1940. }
  1941. function interceptorChainEndFn(req, finalHandlerFn) {
  1942. return finalHandlerFn(req);
  1943. }
  1944. /**
  1945. * Constructs a `ChainedInterceptorFn` which adapts a legacy `HttpInterceptor` to the
  1946. * `ChainedInterceptorFn` interface.
  1947. */
  1948. function adaptLegacyInterceptorToChain(chainTailFn, interceptor) {
  1949. return (initialRequest, finalHandlerFn) => interceptor.intercept(initialRequest, {
  1950. handle: (downstreamRequest) => chainTailFn(downstreamRequest, finalHandlerFn),
  1951. });
  1952. }
  1953. /**
  1954. * Constructs a `ChainedInterceptorFn` which wraps and invokes a functional interceptor in the given
  1955. * injector.
  1956. */
  1957. function chainedInterceptorFn(chainTailFn, interceptorFn, injector) {
  1958. return (initialRequest, finalHandlerFn) => runInInjectionContext(injector, () => interceptorFn(initialRequest, (downstreamRequest) => chainTailFn(downstreamRequest, finalHandlerFn)));
  1959. }
  1960. /**
  1961. * A multi-provider token that represents the array of registered
  1962. * `HttpInterceptor` objects.
  1963. *
  1964. * @publicApi
  1965. */
  1966. const HTTP_INTERCEPTORS = new InjectionToken(ngDevMode ? 'HTTP_INTERCEPTORS' : '');
  1967. /**
  1968. * A multi-provided token of `HttpInterceptorFn`s.
  1969. */
  1970. const HTTP_INTERCEPTOR_FNS = new InjectionToken(ngDevMode ? 'HTTP_INTERCEPTOR_FNS' : '');
  1971. /**
  1972. * A multi-provided token of `HttpInterceptorFn`s that are only set in root.
  1973. */
  1974. const HTTP_ROOT_INTERCEPTOR_FNS = new InjectionToken(ngDevMode ? 'HTTP_ROOT_INTERCEPTOR_FNS' : '');
  1975. // TODO(atscott): We need a larger discussion about stability and what should contribute to stability.
  1976. // Should the whole interceptor chain contribute to stability or just the backend request #55075?
  1977. // Should HttpClient contribute to stability automatically at all?
  1978. const REQUESTS_CONTRIBUTE_TO_STABILITY = new InjectionToken(ngDevMode ? 'REQUESTS_CONTRIBUTE_TO_STABILITY' : '', { providedIn: 'root', factory: () => true });
  1979. /**
  1980. * Creates an `HttpInterceptorFn` which lazily initializes an interceptor chain from the legacy
  1981. * class-based interceptors and runs the request through it.
  1982. */
  1983. function legacyInterceptorFnFactory() {
  1984. let chain = null;
  1985. return (req, handler) => {
  1986. if (chain === null) {
  1987. const interceptors = inject(HTTP_INTERCEPTORS, { optional: true }) ?? [];
  1988. // Note: interceptors are wrapped right-to-left so that final execution order is
  1989. // left-to-right. That is, if `interceptors` is the array `[a, b, c]`, we want to
  1990. // produce a chain that is conceptually `c(b(a(end)))`, which we build from the inside
  1991. // out.
  1992. chain = interceptors.reduceRight(adaptLegacyInterceptorToChain, interceptorChainEndFn);
  1993. }
  1994. const pendingTasks = inject(PendingTasks);
  1995. const contributeToStability = inject(REQUESTS_CONTRIBUTE_TO_STABILITY);
  1996. if (contributeToStability) {
  1997. const removeTask = pendingTasks.add();
  1998. return chain(req, handler).pipe(finalize(removeTask));
  1999. }
  2000. else {
  2001. return chain(req, handler);
  2002. }
  2003. };
  2004. }
  2005. let fetchBackendWarningDisplayed = false;
  2006. class HttpInterceptorHandler extends HttpHandler {
  2007. backend;
  2008. injector;
  2009. chain = null;
  2010. pendingTasks = inject(PendingTasks);
  2011. contributeToStability = inject(REQUESTS_CONTRIBUTE_TO_STABILITY);
  2012. constructor(backend, injector) {
  2013. super();
  2014. this.backend = backend;
  2015. this.injector = injector;
  2016. // We strongly recommend using fetch backend for HTTP calls when SSR is used
  2017. // for an application. The logic below checks if that's the case and produces
  2018. // a warning otherwise.
  2019. if ((typeof ngDevMode === 'undefined' || ngDevMode) && !fetchBackendWarningDisplayed) {
  2020. // This flag is necessary because provideHttpClientTesting() overrides the backend
  2021. // even if `withFetch()` is used within the test. When the testing HTTP backend is provided,
  2022. // no HTTP calls are actually performed during the test, so producing a warning would be
  2023. // misleading.
  2024. const isTestingBackend = this.backend.isTestingBackend;
  2025. if (typeof ngServerMode !== 'undefined' &&
  2026. ngServerMode &&
  2027. !(this.backend instanceof FetchBackend) &&
  2028. !isTestingBackend) {
  2029. fetchBackendWarningDisplayed = true;
  2030. injector
  2031. .get(_Console)
  2032. .warn(_formatRuntimeError(2801 /* RuntimeErrorCode.NOT_USING_FETCH_BACKEND_IN_SSR */, 'Angular detected that `HttpClient` is not configured ' +
  2033. "to use `fetch` APIs. It's strongly recommended to " +
  2034. 'enable `fetch` for applications that use Server-Side Rendering ' +
  2035. 'for better performance and compatibility. ' +
  2036. 'To enable `fetch`, add the `withFetch()` to the `provideHttpClient()` ' +
  2037. 'call at the root of the application.'));
  2038. }
  2039. }
  2040. }
  2041. handle(initialRequest) {
  2042. if (this.chain === null) {
  2043. const dedupedInterceptorFns = Array.from(new Set([
  2044. ...this.injector.get(HTTP_INTERCEPTOR_FNS),
  2045. ...this.injector.get(HTTP_ROOT_INTERCEPTOR_FNS, []),
  2046. ]));
  2047. // Note: interceptors are wrapped right-to-left so that final execution order is
  2048. // left-to-right. That is, if `dedupedInterceptorFns` is the array `[a, b, c]`, we want to
  2049. // produce a chain that is conceptually `c(b(a(end)))`, which we build from the inside
  2050. // out.
  2051. this.chain = dedupedInterceptorFns.reduceRight((nextSequencedFn, interceptorFn) => chainedInterceptorFn(nextSequencedFn, interceptorFn, this.injector), interceptorChainEndFn);
  2052. }
  2053. if (this.contributeToStability) {
  2054. const removeTask = this.pendingTasks.add();
  2055. return this.chain(initialRequest, (downstreamRequest) => this.backend.handle(downstreamRequest)).pipe(finalize(removeTask));
  2056. }
  2057. else {
  2058. return this.chain(initialRequest, (downstreamRequest) => this.backend.handle(downstreamRequest));
  2059. }
  2060. }
  2061. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpInterceptorHandler, deps: [{ token: HttpBackend }, { token: i0.EnvironmentInjector }], target: i0.ɵɵFactoryTarget.Injectable });
  2062. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpInterceptorHandler });
  2063. }
  2064. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpInterceptorHandler, decorators: [{
  2065. type: Injectable
  2066. }], ctorParameters: () => [{ type: HttpBackend }, { type: i0.EnvironmentInjector }] });
  2067. // Every request made through JSONP needs a callback name that's unique across the
  2068. // whole page. Each request is assigned an id and the callback name is constructed
  2069. // from that. The next id to be assigned is tracked in a global variable here that
  2070. // is shared among all applications on the page.
  2071. let nextRequestId = 0;
  2072. /**
  2073. * When a pending <script> is unsubscribed we'll move it to this document, so it won't be
  2074. * executed.
  2075. */
  2076. let foreignDocument;
  2077. // Error text given when a JSONP script is injected, but doesn't invoke the callback
  2078. // passed in its URL.
  2079. const JSONP_ERR_NO_CALLBACK = 'JSONP injected script did not invoke callback.';
  2080. // Error text given when a request is passed to the JsonpClientBackend that doesn't
  2081. // have a request method JSONP.
  2082. const JSONP_ERR_WRONG_METHOD = 'JSONP requests must use JSONP request method.';
  2083. const JSONP_ERR_WRONG_RESPONSE_TYPE = 'JSONP requests must use Json response type.';
  2084. // Error text given when a request is passed to the JsonpClientBackend that has
  2085. // headers set
  2086. const JSONP_ERR_HEADERS_NOT_SUPPORTED = 'JSONP requests do not support headers.';
  2087. /**
  2088. * DI token/abstract type representing a map of JSONP callbacks.
  2089. *
  2090. * In the browser, this should always be the `window` object.
  2091. *
  2092. *
  2093. */
  2094. class JsonpCallbackContext {
  2095. }
  2096. /**
  2097. * Factory function that determines where to store JSONP callbacks.
  2098. *
  2099. * Ordinarily JSONP callbacks are stored on the `window` object, but this may not exist
  2100. * in test environments. In that case, callbacks are stored on an anonymous object instead.
  2101. *
  2102. *
  2103. */
  2104. function jsonpCallbackContext() {
  2105. if (typeof window === 'object') {
  2106. return window;
  2107. }
  2108. return {};
  2109. }
  2110. /**
  2111. * Processes an `HttpRequest` with the JSONP method,
  2112. * by performing JSONP style requests.
  2113. * @see {@link HttpHandler}
  2114. * @see {@link HttpXhrBackend}
  2115. *
  2116. * @publicApi
  2117. */
  2118. class JsonpClientBackend {
  2119. callbackMap;
  2120. document;
  2121. /**
  2122. * A resolved promise that can be used to schedule microtasks in the event handlers.
  2123. */
  2124. resolvedPromise = Promise.resolve();
  2125. constructor(callbackMap, document) {
  2126. this.callbackMap = callbackMap;
  2127. this.document = document;
  2128. }
  2129. /**
  2130. * Get the name of the next callback method, by incrementing the global `nextRequestId`.
  2131. */
  2132. nextCallback() {
  2133. return `ng_jsonp_callback_${nextRequestId++}`;
  2134. }
  2135. /**
  2136. * Processes a JSONP request and returns an event stream of the results.
  2137. * @param req The request object.
  2138. * @returns An observable of the response events.
  2139. *
  2140. */
  2141. handle(req) {
  2142. // Firstly, check both the method and response type. If either doesn't match
  2143. // then the request was improperly routed here and cannot be handled.
  2144. if (req.method !== 'JSONP') {
  2145. throw new _RuntimeError(2810 /* RuntimeErrorCode.JSONP_WRONG_METHOD */, ngDevMode && JSONP_ERR_WRONG_METHOD);
  2146. }
  2147. else if (req.responseType !== 'json') {
  2148. throw new _RuntimeError(2811 /* RuntimeErrorCode.JSONP_WRONG_RESPONSE_TYPE */, ngDevMode && JSONP_ERR_WRONG_RESPONSE_TYPE);
  2149. }
  2150. // Check the request headers. JSONP doesn't support headers and
  2151. // cannot set any that were supplied.
  2152. if (req.headers.keys().length > 0) {
  2153. throw new _RuntimeError(2812 /* RuntimeErrorCode.JSONP_HEADERS_NOT_SUPPORTED */, ngDevMode && JSONP_ERR_HEADERS_NOT_SUPPORTED);
  2154. }
  2155. // Everything else happens inside the Observable boundary.
  2156. return new Observable((observer) => {
  2157. // The first step to make a request is to generate the callback name, and replace the
  2158. // callback placeholder in the URL with the name. Care has to be taken here to ensure
  2159. // a trailing &, if matched, gets inserted back into the URL in the correct place.
  2160. const callback = this.nextCallback();
  2161. const url = req.urlWithParams.replace(/=JSONP_CALLBACK(&|$)/, `=${callback}$1`);
  2162. // Construct the <script> tag and point it at the URL.
  2163. const node = this.document.createElement('script');
  2164. node.src = url;
  2165. // A JSONP request requires waiting for multiple callbacks. These variables
  2166. // are closed over and track state across those callbacks.
  2167. // The response object, if one has been received, or null otherwise.
  2168. let body = null;
  2169. // Whether the response callback has been called.
  2170. let finished = false;
  2171. // Set the response callback in this.callbackMap (which will be the window
  2172. // object in the browser. The script being loaded via the <script> tag will
  2173. // eventually call this callback.
  2174. this.callbackMap[callback] = (data) => {
  2175. // Data has been received from the JSONP script. Firstly, delete this callback.
  2176. delete this.callbackMap[callback];
  2177. // Set state to indicate data was received.
  2178. body = data;
  2179. finished = true;
  2180. };
  2181. // cleanup() is a utility closure that removes the <script> from the page and
  2182. // the response callback from the window. This logic is used in both the
  2183. // success, error, and cancellation paths, so it's extracted out for convenience.
  2184. const cleanup = () => {
  2185. node.removeEventListener('load', onLoad);
  2186. node.removeEventListener('error', onError);
  2187. // Remove the <script> tag if it's still on the page.
  2188. node.remove();
  2189. // Remove the response callback from the callbackMap (window object in the
  2190. // browser).
  2191. delete this.callbackMap[callback];
  2192. };
  2193. // onLoad() is the success callback which runs after the response callback
  2194. // if the JSONP script loads successfully. The event itself is unimportant.
  2195. // If something went wrong, onLoad() may run without the response callback
  2196. // having been invoked.
  2197. const onLoad = () => {
  2198. // We wrap it in an extra Promise, to ensure the microtask
  2199. // is scheduled after the loaded endpoint has executed any potential microtask itself,
  2200. // which is not guaranteed in Internet Explorer and EdgeHTML. See issue #39496
  2201. this.resolvedPromise.then(() => {
  2202. // Cleanup the page.
  2203. cleanup();
  2204. // Check whether the response callback has run.
  2205. if (!finished) {
  2206. // It hasn't, something went wrong with the request. Return an error via
  2207. // the Observable error path. All JSONP errors have status 0.
  2208. observer.error(new HttpErrorResponse({
  2209. url,
  2210. status: 0,
  2211. statusText: 'JSONP Error',
  2212. error: new Error(JSONP_ERR_NO_CALLBACK),
  2213. }));
  2214. return;
  2215. }
  2216. // Success. body either contains the response body or null if none was
  2217. // returned.
  2218. observer.next(new HttpResponse({
  2219. body,
  2220. status: HTTP_STATUS_CODE_OK,
  2221. statusText: 'OK',
  2222. url,
  2223. }));
  2224. // Complete the stream, the response is over.
  2225. observer.complete();
  2226. });
  2227. };
  2228. // onError() is the error callback, which runs if the script returned generates
  2229. // a Javascript error. It emits the error via the Observable error channel as
  2230. // a HttpErrorResponse.
  2231. const onError = (error) => {
  2232. cleanup();
  2233. // Wrap the error in a HttpErrorResponse.
  2234. observer.error(new HttpErrorResponse({
  2235. error,
  2236. status: 0,
  2237. statusText: 'JSONP Error',
  2238. url,
  2239. }));
  2240. };
  2241. // Subscribe to both the success (load) and error events on the <script> tag,
  2242. // and add it to the page.
  2243. node.addEventListener('load', onLoad);
  2244. node.addEventListener('error', onError);
  2245. this.document.body.appendChild(node);
  2246. // The request has now been successfully sent.
  2247. observer.next({ type: HttpEventType.Sent });
  2248. // Cancellation handler.
  2249. return () => {
  2250. if (!finished) {
  2251. this.removeListeners(node);
  2252. }
  2253. // And finally, clean up the page.
  2254. cleanup();
  2255. };
  2256. });
  2257. }
  2258. removeListeners(script) {
  2259. // Issue #34818
  2260. // Changing <script>'s ownerDocument will prevent it from execution.
  2261. // https://html.spec.whatwg.org/multipage/scripting.html#execute-the-script-block
  2262. foreignDocument ??= this.document.implementation.createHTMLDocument();
  2263. foreignDocument.adoptNode(script);
  2264. }
  2265. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: JsonpClientBackend, deps: [{ token: JsonpCallbackContext }, { token: DOCUMENT }], target: i0.ɵɵFactoryTarget.Injectable });
  2266. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: JsonpClientBackend });
  2267. }
  2268. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: JsonpClientBackend, decorators: [{
  2269. type: Injectable
  2270. }], ctorParameters: () => [{ type: JsonpCallbackContext }, { type: undefined, decorators: [{
  2271. type: Inject,
  2272. args: [DOCUMENT]
  2273. }] }] });
  2274. /**
  2275. * Identifies requests with the method JSONP and shifts them to the `JsonpClientBackend`.
  2276. */
  2277. function jsonpInterceptorFn(req, next) {
  2278. if (req.method === 'JSONP') {
  2279. return inject(JsonpClientBackend).handle(req);
  2280. }
  2281. // Fall through for normal HTTP requests.
  2282. return next(req);
  2283. }
  2284. /**
  2285. * Identifies requests with the method JSONP and
  2286. * shifts them to the `JsonpClientBackend`.
  2287. *
  2288. * @see {@link HttpInterceptor}
  2289. *
  2290. * @publicApi
  2291. */
  2292. class JsonpInterceptor {
  2293. injector;
  2294. constructor(injector) {
  2295. this.injector = injector;
  2296. }
  2297. /**
  2298. * Identifies and handles a given JSONP request.
  2299. * @param initialRequest The outgoing request object to handle.
  2300. * @param next The next interceptor in the chain, or the backend
  2301. * if no interceptors remain in the chain.
  2302. * @returns An observable of the event stream.
  2303. */
  2304. intercept(initialRequest, next) {
  2305. return runInInjectionContext(this.injector, () => jsonpInterceptorFn(initialRequest, (downstreamRequest) => next.handle(downstreamRequest)));
  2306. }
  2307. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: JsonpInterceptor, deps: [{ token: i0.EnvironmentInjector }], target: i0.ɵɵFactoryTarget.Injectable });
  2308. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: JsonpInterceptor });
  2309. }
  2310. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: JsonpInterceptor, decorators: [{
  2311. type: Injectable
  2312. }], ctorParameters: () => [{ type: i0.EnvironmentInjector }] });
  2313. const XSSI_PREFIX = /^\)\]\}',?\n/;
  2314. const X_REQUEST_URL_REGEXP = RegExp(`^${X_REQUEST_URL_HEADER}:`, 'm');
  2315. /**
  2316. * Determine an appropriate URL for the response, by checking either
  2317. * XMLHttpRequest.responseURL or the X-Request-URL header.
  2318. */
  2319. function getResponseUrl(xhr) {
  2320. if ('responseURL' in xhr && xhr.responseURL) {
  2321. return xhr.responseURL;
  2322. }
  2323. if (X_REQUEST_URL_REGEXP.test(xhr.getAllResponseHeaders())) {
  2324. return xhr.getResponseHeader(X_REQUEST_URL_HEADER);
  2325. }
  2326. return null;
  2327. }
  2328. /**
  2329. * Validates whether the request is compatible with the XHR backend.
  2330. * Show a warning if the request contains options that are not supported by XHR.
  2331. */
  2332. function validateXhrCompatibility(req) {
  2333. const unsupportedOptions = [
  2334. {
  2335. property: 'keepalive',
  2336. errorCode: 2813 /* RuntimeErrorCode.KEEPALIVE_NOT_SUPPORTED_WITH_XHR */,
  2337. },
  2338. {
  2339. property: 'cache',
  2340. errorCode: 2814 /* RuntimeErrorCode.CACHE_NOT_SUPPORTED_WITH_XHR */,
  2341. },
  2342. {
  2343. property: 'priority',
  2344. errorCode: 2815 /* RuntimeErrorCode.PRIORITY_NOT_SUPPORTED_WITH_XHR */,
  2345. },
  2346. {
  2347. property: 'mode',
  2348. errorCode: 2816 /* RuntimeErrorCode.MODE_NOT_SUPPORTED_WITH_XHR */,
  2349. },
  2350. {
  2351. property: 'redirect',
  2352. errorCode: 2817 /* RuntimeErrorCode.REDIRECT_NOT_SUPPORTED_WITH_XHR */,
  2353. },
  2354. {
  2355. property: 'credentials',
  2356. errorCode: 2818 /* RuntimeErrorCode.CREDENTIALS_NOT_SUPPORTED_WITH_XHR */,
  2357. },
  2358. ];
  2359. // Check each unsupported option and warn if present
  2360. for (const { property, errorCode } of unsupportedOptions) {
  2361. if (property in req) {
  2362. console.warn(_formatRuntimeError(errorCode, `Angular detected that a \`HttpClient\` request with the \`${property}\` option was sent using XHR, which does not support it. To use the \`${property}\` option, enable Fetch API support by passing \`withFetch()\` as an argument to \`provideHttpClient()\`.`));
  2363. }
  2364. }
  2365. }
  2366. /**
  2367. * Uses `XMLHttpRequest` to send requests to a backend server.
  2368. * @see {@link HttpHandler}
  2369. * @see {@link JsonpClientBackend}
  2370. *
  2371. * @publicApi
  2372. */
  2373. class HttpXhrBackend {
  2374. xhrFactory;
  2375. constructor(xhrFactory) {
  2376. this.xhrFactory = xhrFactory;
  2377. }
  2378. /**
  2379. * Processes a request and returns a stream of response events.
  2380. * @param req The request object.
  2381. * @returns An observable of the response events.
  2382. */
  2383. handle(req) {
  2384. // Quick check to give a better error message when a user attempts to use
  2385. // HttpClient.jsonp() without installing the HttpClientJsonpModule
  2386. if (req.method === 'JSONP') {
  2387. throw new _RuntimeError(-2800 /* RuntimeErrorCode.MISSING_JSONP_MODULE */, (typeof ngDevMode === 'undefined' || ngDevMode) &&
  2388. `Cannot make a JSONP request without JSONP support. To fix the problem, either add the \`withJsonpSupport()\` call (if \`provideHttpClient()\` is used) or import the \`HttpClientJsonpModule\` in the root NgModule.`);
  2389. }
  2390. // Validate that the request is compatible with the XHR backend.
  2391. ngDevMode && validateXhrCompatibility(req);
  2392. // Check whether this factory has a special function to load an XHR implementation
  2393. // for various non-browser environments. We currently limit it to only `ServerXhr`
  2394. // class, which needs to load an XHR implementation.
  2395. const xhrFactory = this.xhrFactory;
  2396. const source =
  2397. // Note that `ɵloadImpl` is never defined in client bundles and can be
  2398. // safely dropped whenever we're running in the browser.
  2399. // This branching is redundant.
  2400. // The `ngServerMode` guard also enables tree-shaking of the `from()`
  2401. // function from the common bundle, as it's only used in server code.
  2402. typeof ngServerMode !== 'undefined' && ngServerMode && xhrFactory.ɵloadImpl
  2403. ? from(xhrFactory.ɵloadImpl())
  2404. : of(null);
  2405. return source.pipe(switchMap(() => {
  2406. // Everything happens on Observable subscription.
  2407. return new Observable((observer) => {
  2408. // Start by setting up the XHR object with request method, URL, and withCredentials
  2409. // flag.
  2410. const xhr = xhrFactory.build();
  2411. xhr.open(req.method, req.urlWithParams);
  2412. if (req.withCredentials) {
  2413. xhr.withCredentials = true;
  2414. }
  2415. // Add all the requested headers.
  2416. req.headers.forEach((name, values) => xhr.setRequestHeader(name, values.join(',')));
  2417. // Add an Accept header if one isn't present already.
  2418. if (!req.headers.has(ACCEPT_HEADER)) {
  2419. xhr.setRequestHeader(ACCEPT_HEADER, ACCEPT_HEADER_VALUE);
  2420. }
  2421. // Auto-detect the Content-Type header if one isn't present already.
  2422. if (!req.headers.has(CONTENT_TYPE_HEADER)) {
  2423. const detectedType = req.detectContentTypeHeader();
  2424. // Sometimes Content-Type detection fails.
  2425. if (detectedType !== null) {
  2426. xhr.setRequestHeader(CONTENT_TYPE_HEADER, detectedType);
  2427. }
  2428. }
  2429. if (req.timeout) {
  2430. xhr.timeout = req.timeout;
  2431. }
  2432. // Set the responseType if one was requested.
  2433. if (req.responseType) {
  2434. const responseType = req.responseType.toLowerCase();
  2435. // JSON responses need to be processed as text. This is because if the server
  2436. // returns an XSSI-prefixed JSON response, the browser will fail to parse it,
  2437. // xhr.response will be null, and xhr.responseText cannot be accessed to
  2438. // retrieve the prefixed JSON data in order to strip the prefix. Thus, all JSON
  2439. // is parsed by first requesting text and then applying JSON.parse.
  2440. xhr.responseType = (responseType !== 'json' ? responseType : 'text');
  2441. }
  2442. // Serialize the request body if one is present. If not, this will be set to null.
  2443. const reqBody = req.serializeBody();
  2444. // If progress events are enabled, response headers will be delivered
  2445. // in two events - the HttpHeaderResponse event and the full HttpResponse
  2446. // event. However, since response headers don't change in between these
  2447. // two events, it doesn't make sense to parse them twice. So headerResponse
  2448. // caches the data extracted from the response whenever it's first parsed,
  2449. // to ensure parsing isn't duplicated.
  2450. let headerResponse = null;
  2451. // partialFromXhr extracts the HttpHeaderResponse from the current XMLHttpRequest
  2452. // state, and memoizes it into headerResponse.
  2453. const partialFromXhr = () => {
  2454. if (headerResponse !== null) {
  2455. return headerResponse;
  2456. }
  2457. const statusText = xhr.statusText || 'OK';
  2458. // Parse headers from XMLHttpRequest - this step is lazy.
  2459. const headers = new HttpHeaders(xhr.getAllResponseHeaders());
  2460. // Read the response URL from the XMLHttpResponse instance and fall back on the
  2461. // request URL.
  2462. const url = getResponseUrl(xhr) || req.url;
  2463. // Construct the HttpHeaderResponse and memoize it.
  2464. headerResponse = new HttpHeaderResponse({ headers, status: xhr.status, statusText, url });
  2465. return headerResponse;
  2466. };
  2467. // Next, a few closures are defined for the various events which XMLHttpRequest can
  2468. // emit. This allows them to be unregistered as event listeners later.
  2469. // First up is the load event, which represents a response being fully available.
  2470. const onLoad = () => {
  2471. // Read response state from the memoized partial data.
  2472. let { headers, status, statusText, url } = partialFromXhr();
  2473. // The body will be read out if present.
  2474. let body = null;
  2475. if (status !== HTTP_STATUS_CODE_NO_CONTENT) {
  2476. // Use XMLHttpRequest.response if set, responseText otherwise.
  2477. body = typeof xhr.response === 'undefined' ? xhr.responseText : xhr.response;
  2478. }
  2479. // Normalize another potential bug (this one comes from CORS).
  2480. if (status === 0) {
  2481. status = !!body ? HTTP_STATUS_CODE_OK : 0;
  2482. }
  2483. // ok determines whether the response will be transmitted on the event or
  2484. // error channel. Unsuccessful status codes (not 2xx) will always be errors,
  2485. // but a successful status code can still result in an error if the user
  2486. // asked for JSON data and the body cannot be parsed as such.
  2487. let ok = status >= 200 && status < 300;
  2488. // Check whether the body needs to be parsed as JSON (in many cases the browser
  2489. // will have done that already).
  2490. if (req.responseType === 'json' && typeof body === 'string') {
  2491. // Save the original body, before attempting XSSI prefix stripping.
  2492. const originalBody = body;
  2493. body = body.replace(XSSI_PREFIX, '');
  2494. try {
  2495. // Attempt the parse. If it fails, a parse error should be delivered to the
  2496. // user.
  2497. body = body !== '' ? JSON.parse(body) : null;
  2498. }
  2499. catch (error) {
  2500. // Since the JSON.parse failed, it's reasonable to assume this might not have
  2501. // been a JSON response. Restore the original body (including any XSSI prefix)
  2502. // to deliver a better error response.
  2503. body = originalBody;
  2504. // If this was an error request to begin with, leave it as a string, it
  2505. // probably just isn't JSON. Otherwise, deliver the parsing error to the user.
  2506. if (ok) {
  2507. // Even though the response status was 2xx, this is still an error.
  2508. ok = false;
  2509. // The parse error contains the text of the body that failed to parse.
  2510. body = { error, text: body };
  2511. }
  2512. }
  2513. }
  2514. if (ok) {
  2515. // A successful response is delivered on the event stream.
  2516. observer.next(new HttpResponse({
  2517. body,
  2518. headers,
  2519. status,
  2520. statusText,
  2521. url: url || undefined,
  2522. }));
  2523. // The full body has been received and delivered, no further events
  2524. // are possible. This request is complete.
  2525. observer.complete();
  2526. }
  2527. else {
  2528. // An unsuccessful request is delivered on the error channel.
  2529. observer.error(new HttpErrorResponse({
  2530. // The error in this case is the response body (error from the server).
  2531. error: body,
  2532. headers,
  2533. status,
  2534. statusText,
  2535. url: url || undefined,
  2536. }));
  2537. }
  2538. };
  2539. // The onError callback is called when something goes wrong at the network level.
  2540. // Connection timeout, DNS error, offline, etc. These are actual errors, and are
  2541. // transmitted on the error channel.
  2542. const onError = (error) => {
  2543. const { url } = partialFromXhr();
  2544. const res = new HttpErrorResponse({
  2545. error,
  2546. status: xhr.status || 0,
  2547. statusText: xhr.statusText || 'Unknown Error',
  2548. url: url || undefined,
  2549. });
  2550. observer.error(res);
  2551. };
  2552. let onTimeout = onError;
  2553. if (req.timeout) {
  2554. onTimeout = (_) => {
  2555. const { url } = partialFromXhr();
  2556. const res = new HttpErrorResponse({
  2557. error: new DOMException('Request timed out', 'TimeoutError'),
  2558. status: xhr.status || 0,
  2559. statusText: xhr.statusText || 'Request timeout',
  2560. url: url || undefined,
  2561. });
  2562. observer.error(res);
  2563. };
  2564. }
  2565. // The sentHeaders flag tracks whether the HttpResponseHeaders event
  2566. // has been sent on the stream. This is necessary to track if progress
  2567. // is enabled since the event will be sent on only the first download
  2568. // progress event.
  2569. let sentHeaders = false;
  2570. // The download progress event handler, which is only registered if
  2571. // progress events are enabled.
  2572. const onDownProgress = (event) => {
  2573. // Send the HttpResponseHeaders event if it hasn't been sent already.
  2574. if (!sentHeaders) {
  2575. observer.next(partialFromXhr());
  2576. sentHeaders = true;
  2577. }
  2578. // Start building the download progress event to deliver on the response
  2579. // event stream.
  2580. let progressEvent = {
  2581. type: HttpEventType.DownloadProgress,
  2582. loaded: event.loaded,
  2583. };
  2584. // Set the total number of bytes in the event if it's available.
  2585. if (event.lengthComputable) {
  2586. progressEvent.total = event.total;
  2587. }
  2588. // If the request was for text content and a partial response is
  2589. // available on XMLHttpRequest, include it in the progress event
  2590. // to allow for streaming reads.
  2591. if (req.responseType === 'text' && !!xhr.responseText) {
  2592. progressEvent.partialText = xhr.responseText;
  2593. }
  2594. // Finally, fire the event.
  2595. observer.next(progressEvent);
  2596. };
  2597. // The upload progress event handler, which is only registered if
  2598. // progress events are enabled.
  2599. const onUpProgress = (event) => {
  2600. // Upload progress events are simpler. Begin building the progress
  2601. // event.
  2602. let progress = {
  2603. type: HttpEventType.UploadProgress,
  2604. loaded: event.loaded,
  2605. };
  2606. // If the total number of bytes being uploaded is available, include
  2607. // it.
  2608. if (event.lengthComputable) {
  2609. progress.total = event.total;
  2610. }
  2611. // Send the event.
  2612. observer.next(progress);
  2613. };
  2614. // By default, register for load and error events.
  2615. xhr.addEventListener('load', onLoad);
  2616. xhr.addEventListener('error', onError);
  2617. xhr.addEventListener('timeout', onTimeout);
  2618. xhr.addEventListener('abort', onError);
  2619. // Progress events are only enabled if requested.
  2620. if (req.reportProgress) {
  2621. // Download progress is always enabled if requested.
  2622. xhr.addEventListener('progress', onDownProgress);
  2623. // Upload progress depends on whether there is a body to upload.
  2624. if (reqBody !== null && xhr.upload) {
  2625. xhr.upload.addEventListener('progress', onUpProgress);
  2626. }
  2627. }
  2628. // Fire the request, and notify the event stream that it was fired.
  2629. xhr.send(reqBody);
  2630. observer.next({ type: HttpEventType.Sent });
  2631. // This is the return from the Observable function, which is the
  2632. // request cancellation handler.
  2633. return () => {
  2634. // On a cancellation, remove all registered event listeners.
  2635. xhr.removeEventListener('error', onError);
  2636. xhr.removeEventListener('abort', onError);
  2637. xhr.removeEventListener('load', onLoad);
  2638. xhr.removeEventListener('timeout', onTimeout);
  2639. if (req.reportProgress) {
  2640. xhr.removeEventListener('progress', onDownProgress);
  2641. if (reqBody !== null && xhr.upload) {
  2642. xhr.upload.removeEventListener('progress', onUpProgress);
  2643. }
  2644. }
  2645. // Finally, abort the in-flight request.
  2646. if (xhr.readyState !== xhr.DONE) {
  2647. xhr.abort();
  2648. }
  2649. };
  2650. });
  2651. }));
  2652. }
  2653. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpXhrBackend, deps: [{ token: XhrFactory }], target: i0.ɵɵFactoryTarget.Injectable });
  2654. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpXhrBackend });
  2655. }
  2656. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpXhrBackend, decorators: [{
  2657. type: Injectable
  2658. }], ctorParameters: () => [{ type: XhrFactory }] });
  2659. const XSRF_ENABLED = new InjectionToken(ngDevMode ? 'XSRF_ENABLED' : '');
  2660. const XSRF_DEFAULT_COOKIE_NAME = 'XSRF-TOKEN';
  2661. const XSRF_COOKIE_NAME = new InjectionToken(ngDevMode ? 'XSRF_COOKIE_NAME' : '', {
  2662. providedIn: 'root',
  2663. factory: () => XSRF_DEFAULT_COOKIE_NAME,
  2664. });
  2665. const XSRF_DEFAULT_HEADER_NAME = 'X-XSRF-TOKEN';
  2666. const XSRF_HEADER_NAME = new InjectionToken(ngDevMode ? 'XSRF_HEADER_NAME' : '', {
  2667. providedIn: 'root',
  2668. factory: () => XSRF_DEFAULT_HEADER_NAME,
  2669. });
  2670. /**
  2671. * Retrieves the current XSRF token to use with the next outgoing request.
  2672. *
  2673. * @publicApi
  2674. */
  2675. class HttpXsrfTokenExtractor {
  2676. }
  2677. /**
  2678. * `HttpXsrfTokenExtractor` which retrieves the token from a cookie.
  2679. */
  2680. class HttpXsrfCookieExtractor {
  2681. doc;
  2682. cookieName;
  2683. lastCookieString = '';
  2684. lastToken = null;
  2685. /**
  2686. * @internal for testing
  2687. */
  2688. parseCount = 0;
  2689. constructor(doc, cookieName) {
  2690. this.doc = doc;
  2691. this.cookieName = cookieName;
  2692. }
  2693. getToken() {
  2694. if (typeof ngServerMode !== 'undefined' && ngServerMode) {
  2695. return null;
  2696. }
  2697. const cookieString = this.doc.cookie || '';
  2698. if (cookieString !== this.lastCookieString) {
  2699. this.parseCount++;
  2700. this.lastToken = parseCookieValue(cookieString, this.cookieName);
  2701. this.lastCookieString = cookieString;
  2702. }
  2703. return this.lastToken;
  2704. }
  2705. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpXsrfCookieExtractor, deps: [{ token: DOCUMENT }, { token: XSRF_COOKIE_NAME }], target: i0.ɵɵFactoryTarget.Injectable });
  2706. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpXsrfCookieExtractor });
  2707. }
  2708. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpXsrfCookieExtractor, decorators: [{
  2709. type: Injectable
  2710. }], ctorParameters: () => [{ type: undefined, decorators: [{
  2711. type: Inject,
  2712. args: [DOCUMENT]
  2713. }] }, { type: undefined, decorators: [{
  2714. type: Inject,
  2715. args: [XSRF_COOKIE_NAME]
  2716. }] }] });
  2717. function xsrfInterceptorFn(req, next) {
  2718. const lcUrl = req.url.toLowerCase();
  2719. // Skip both non-mutating requests and absolute URLs.
  2720. // Non-mutating requests don't require a token, and absolute URLs require special handling
  2721. // anyway as the cookie set
  2722. // on our origin is not the same as the token expected by another origin.
  2723. if (!inject(XSRF_ENABLED) ||
  2724. req.method === 'GET' ||
  2725. req.method === 'HEAD' ||
  2726. lcUrl.startsWith('http://') ||
  2727. lcUrl.startsWith('https://')) {
  2728. return next(req);
  2729. }
  2730. const token = inject(HttpXsrfTokenExtractor).getToken();
  2731. const headerName = inject(XSRF_HEADER_NAME);
  2732. // Be careful not to overwrite an existing header of the same name.
  2733. if (token != null && !req.headers.has(headerName)) {
  2734. req = req.clone({ headers: req.headers.set(headerName, token) });
  2735. }
  2736. return next(req);
  2737. }
  2738. /**
  2739. * `HttpInterceptor` which adds an XSRF token to eligible outgoing requests.
  2740. */
  2741. class HttpXsrfInterceptor {
  2742. injector;
  2743. constructor(injector) {
  2744. this.injector = injector;
  2745. }
  2746. intercept(initialRequest, next) {
  2747. return runInInjectionContext(this.injector, () => xsrfInterceptorFn(initialRequest, (downstreamRequest) => next.handle(downstreamRequest)));
  2748. }
  2749. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpXsrfInterceptor, deps: [{ token: i0.EnvironmentInjector }], target: i0.ɵɵFactoryTarget.Injectable });
  2750. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpXsrfInterceptor });
  2751. }
  2752. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpXsrfInterceptor, decorators: [{
  2753. type: Injectable
  2754. }], ctorParameters: () => [{ type: i0.EnvironmentInjector }] });
  2755. /**
  2756. * Identifies a particular kind of `HttpFeature`.
  2757. *
  2758. * @publicApi
  2759. */
  2760. var HttpFeatureKind;
  2761. (function (HttpFeatureKind) {
  2762. HttpFeatureKind[HttpFeatureKind["Interceptors"] = 0] = "Interceptors";
  2763. HttpFeatureKind[HttpFeatureKind["LegacyInterceptors"] = 1] = "LegacyInterceptors";
  2764. HttpFeatureKind[HttpFeatureKind["CustomXsrfConfiguration"] = 2] = "CustomXsrfConfiguration";
  2765. HttpFeatureKind[HttpFeatureKind["NoXsrfProtection"] = 3] = "NoXsrfProtection";
  2766. HttpFeatureKind[HttpFeatureKind["JsonpSupport"] = 4] = "JsonpSupport";
  2767. HttpFeatureKind[HttpFeatureKind["RequestsMadeViaParent"] = 5] = "RequestsMadeViaParent";
  2768. HttpFeatureKind[HttpFeatureKind["Fetch"] = 6] = "Fetch";
  2769. })(HttpFeatureKind || (HttpFeatureKind = {}));
  2770. function makeHttpFeature(kind, providers) {
  2771. return {
  2772. ɵkind: kind,
  2773. ɵproviders: providers,
  2774. };
  2775. }
  2776. /**
  2777. * Configures Angular's `HttpClient` service to be available for injection.
  2778. *
  2779. * By default, `HttpClient` will be configured for injection with its default options for XSRF
  2780. * protection of outgoing requests. Additional configuration options can be provided by passing
  2781. * feature functions to `provideHttpClient`. For example, HTTP interceptors can be added using the
  2782. * `withInterceptors(...)` feature.
  2783. *
  2784. * <div class="docs-alert docs-alert-helpful">
  2785. *
  2786. * It's strongly recommended to enable
  2787. * [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) for applications that use
  2788. * Server-Side Rendering for better performance and compatibility. To enable `fetch`, add
  2789. * `withFetch()` feature to the `provideHttpClient()` call at the root of the application:
  2790. *
  2791. * ```ts
  2792. * provideHttpClient(withFetch());
  2793. * ```
  2794. *
  2795. * </div>
  2796. *
  2797. * @see {@link withInterceptors}
  2798. * @see {@link withInterceptorsFromDi}
  2799. * @see {@link withXsrfConfiguration}
  2800. * @see {@link withNoXsrfProtection}
  2801. * @see {@link withJsonpSupport}
  2802. * @see {@link withRequestsMadeViaParent}
  2803. * @see {@link withFetch}
  2804. */
  2805. function provideHttpClient(...features) {
  2806. if (ngDevMode) {
  2807. const featureKinds = new Set(features.map((f) => f.ɵkind));
  2808. if (featureKinds.has(HttpFeatureKind.NoXsrfProtection) &&
  2809. featureKinds.has(HttpFeatureKind.CustomXsrfConfiguration)) {
  2810. throw new Error(ngDevMode
  2811. ? `Configuration error: found both withXsrfConfiguration() and withNoXsrfProtection() in the same call to provideHttpClient(), which is a contradiction.`
  2812. : '');
  2813. }
  2814. }
  2815. const providers = [
  2816. HttpClient,
  2817. HttpXhrBackend,
  2818. HttpInterceptorHandler,
  2819. { provide: HttpHandler, useExisting: HttpInterceptorHandler },
  2820. {
  2821. provide: HttpBackend,
  2822. useFactory: () => {
  2823. return inject(FETCH_BACKEND, { optional: true }) ?? inject(HttpXhrBackend);
  2824. },
  2825. },
  2826. {
  2827. provide: HTTP_INTERCEPTOR_FNS,
  2828. useValue: xsrfInterceptorFn,
  2829. multi: true,
  2830. },
  2831. { provide: XSRF_ENABLED, useValue: true },
  2832. { provide: HttpXsrfTokenExtractor, useClass: HttpXsrfCookieExtractor },
  2833. ];
  2834. for (const feature of features) {
  2835. providers.push(...feature.ɵproviders);
  2836. }
  2837. return makeEnvironmentProviders(providers);
  2838. }
  2839. /**
  2840. * Adds one or more functional-style HTTP interceptors to the configuration of the `HttpClient`
  2841. * instance.
  2842. *
  2843. * @see {@link HttpInterceptorFn}
  2844. * @see {@link provideHttpClient}
  2845. * @publicApi
  2846. */
  2847. function withInterceptors(interceptorFns) {
  2848. return makeHttpFeature(HttpFeatureKind.Interceptors, interceptorFns.map((interceptorFn) => {
  2849. return {
  2850. provide: HTTP_INTERCEPTOR_FNS,
  2851. useValue: interceptorFn,
  2852. multi: true,
  2853. };
  2854. }));
  2855. }
  2856. const LEGACY_INTERCEPTOR_FN = new InjectionToken(ngDevMode ? 'LEGACY_INTERCEPTOR_FN' : '');
  2857. /**
  2858. * Includes class-based interceptors configured using a multi-provider in the current injector into
  2859. * the configured `HttpClient` instance.
  2860. *
  2861. * Prefer `withInterceptors` and functional interceptors instead, as support for DI-provided
  2862. * interceptors may be phased out in a later release.
  2863. *
  2864. * @see {@link HttpInterceptor}
  2865. * @see {@link HTTP_INTERCEPTORS}
  2866. * @see {@link provideHttpClient}
  2867. */
  2868. function withInterceptorsFromDi() {
  2869. // Note: the legacy interceptor function is provided here via an intermediate token
  2870. // (`LEGACY_INTERCEPTOR_FN`), using a pattern which guarantees that if these providers are
  2871. // included multiple times, all of the multi-provider entries will have the same instance of the
  2872. // interceptor function. That way, the `HttpINterceptorHandler` will dedup them and legacy
  2873. // interceptors will not run multiple times.
  2874. return makeHttpFeature(HttpFeatureKind.LegacyInterceptors, [
  2875. {
  2876. provide: LEGACY_INTERCEPTOR_FN,
  2877. useFactory: legacyInterceptorFnFactory,
  2878. },
  2879. {
  2880. provide: HTTP_INTERCEPTOR_FNS,
  2881. useExisting: LEGACY_INTERCEPTOR_FN,
  2882. multi: true,
  2883. },
  2884. ]);
  2885. }
  2886. /**
  2887. * Customizes the XSRF protection for the configuration of the current `HttpClient` instance.
  2888. *
  2889. * This feature is incompatible with the `withNoXsrfProtection` feature.
  2890. *
  2891. * @see {@link provideHttpClient}
  2892. */
  2893. function withXsrfConfiguration({ cookieName, headerName, }) {
  2894. const providers = [];
  2895. if (cookieName !== undefined) {
  2896. providers.push({ provide: XSRF_COOKIE_NAME, useValue: cookieName });
  2897. }
  2898. if (headerName !== undefined) {
  2899. providers.push({ provide: XSRF_HEADER_NAME, useValue: headerName });
  2900. }
  2901. return makeHttpFeature(HttpFeatureKind.CustomXsrfConfiguration, providers);
  2902. }
  2903. /**
  2904. * Disables XSRF protection in the configuration of the current `HttpClient` instance.
  2905. *
  2906. * This feature is incompatible with the `withXsrfConfiguration` feature.
  2907. *
  2908. * @see {@link provideHttpClient}
  2909. */
  2910. function withNoXsrfProtection() {
  2911. return makeHttpFeature(HttpFeatureKind.NoXsrfProtection, [
  2912. {
  2913. provide: XSRF_ENABLED,
  2914. useValue: false,
  2915. },
  2916. ]);
  2917. }
  2918. /**
  2919. * Add JSONP support to the configuration of the current `HttpClient` instance.
  2920. *
  2921. * @see {@link provideHttpClient}
  2922. */
  2923. function withJsonpSupport() {
  2924. return makeHttpFeature(HttpFeatureKind.JsonpSupport, [
  2925. JsonpClientBackend,
  2926. { provide: JsonpCallbackContext, useFactory: jsonpCallbackContext },
  2927. { provide: HTTP_INTERCEPTOR_FNS, useValue: jsonpInterceptorFn, multi: true },
  2928. ]);
  2929. }
  2930. /**
  2931. * Configures the current `HttpClient` instance to make requests via the parent injector's
  2932. * `HttpClient` instead of directly.
  2933. *
  2934. * By default, `provideHttpClient` configures `HttpClient` in its injector to be an independent
  2935. * instance. For example, even if `HttpClient` is configured in the parent injector with
  2936. * one or more interceptors, they will not intercept requests made via this instance.
  2937. *
  2938. * With this option enabled, once the request has passed through the current injector's
  2939. * interceptors, it will be delegated to the parent injector's `HttpClient` chain instead of
  2940. * dispatched directly, and interceptors in the parent configuration will be applied to the request.
  2941. *
  2942. * If there are several `HttpClient` instances in the injector hierarchy, it's possible for
  2943. * `withRequestsMadeViaParent` to be used at multiple levels, which will cause the request to
  2944. * "bubble up" until either reaching the root level or an `HttpClient` which was not configured with
  2945. * this option.
  2946. *
  2947. * @see {@link provideHttpClient}
  2948. * @publicApi 19.0
  2949. */
  2950. function withRequestsMadeViaParent() {
  2951. return makeHttpFeature(HttpFeatureKind.RequestsMadeViaParent, [
  2952. {
  2953. provide: HttpBackend,
  2954. useFactory: () => {
  2955. const handlerFromParent = inject(HttpHandler, { skipSelf: true, optional: true });
  2956. if (ngDevMode && handlerFromParent === null) {
  2957. throw new Error('withRequestsMadeViaParent() can only be used when the parent injector also configures HttpClient');
  2958. }
  2959. return handlerFromParent;
  2960. },
  2961. },
  2962. ]);
  2963. }
  2964. /**
  2965. * Configures the current `HttpClient` instance to make requests using the fetch API.
  2966. *
  2967. * Note: The Fetch API doesn't support progress report on uploads.
  2968. *
  2969. * @publicApi
  2970. */
  2971. function withFetch() {
  2972. return makeHttpFeature(HttpFeatureKind.Fetch, [
  2973. FetchBackend,
  2974. { provide: FETCH_BACKEND, useExisting: FetchBackend },
  2975. { provide: HttpBackend, useExisting: FetchBackend },
  2976. ]);
  2977. }
  2978. /**
  2979. * Configures XSRF protection support for outgoing requests.
  2980. *
  2981. * For a server that supports a cookie-based XSRF protection system,
  2982. * use directly to configure XSRF protection with the correct
  2983. * cookie and header names.
  2984. *
  2985. * If no names are supplied, the default cookie name is `XSRF-TOKEN`
  2986. * and the default header name is `X-XSRF-TOKEN`.
  2987. *
  2988. * @publicApi
  2989. * @deprecated Use withXsrfConfiguration({cookieName: 'XSRF-TOKEN', headerName: 'X-XSRF-TOKEN'}) as
  2990. * providers instead or `withNoXsrfProtection` if you want to disabled XSRF protection.
  2991. */
  2992. class HttpClientXsrfModule {
  2993. /**
  2994. * Disable the default XSRF protection.
  2995. */
  2996. static disable() {
  2997. return {
  2998. ngModule: HttpClientXsrfModule,
  2999. providers: [withNoXsrfProtection().ɵproviders],
  3000. };
  3001. }
  3002. /**
  3003. * Configure XSRF protection.
  3004. * @param options An object that can specify either or both
  3005. * cookie name or header name.
  3006. * - Cookie name default is `XSRF-TOKEN`.
  3007. * - Header name default is `X-XSRF-TOKEN`.
  3008. *
  3009. */
  3010. static withOptions(options = {}) {
  3011. return {
  3012. ngModule: HttpClientXsrfModule,
  3013. providers: withXsrfConfiguration(options).ɵproviders,
  3014. };
  3015. }
  3016. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpClientXsrfModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  3017. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.1.0", ngImport: i0, type: HttpClientXsrfModule });
  3018. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpClientXsrfModule, providers: [
  3019. HttpXsrfInterceptor,
  3020. { provide: HTTP_INTERCEPTORS, useExisting: HttpXsrfInterceptor, multi: true },
  3021. { provide: HttpXsrfTokenExtractor, useClass: HttpXsrfCookieExtractor },
  3022. withXsrfConfiguration({
  3023. cookieName: XSRF_DEFAULT_COOKIE_NAME,
  3024. headerName: XSRF_DEFAULT_HEADER_NAME,
  3025. }).ɵproviders,
  3026. { provide: XSRF_ENABLED, useValue: true },
  3027. ] });
  3028. }
  3029. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpClientXsrfModule, decorators: [{
  3030. type: NgModule,
  3031. args: [{
  3032. providers: [
  3033. HttpXsrfInterceptor,
  3034. { provide: HTTP_INTERCEPTORS, useExisting: HttpXsrfInterceptor, multi: true },
  3035. { provide: HttpXsrfTokenExtractor, useClass: HttpXsrfCookieExtractor },
  3036. withXsrfConfiguration({
  3037. cookieName: XSRF_DEFAULT_COOKIE_NAME,
  3038. headerName: XSRF_DEFAULT_HEADER_NAME,
  3039. }).ɵproviders,
  3040. { provide: XSRF_ENABLED, useValue: true },
  3041. ],
  3042. }]
  3043. }] });
  3044. /**
  3045. * Configures the dependency injector for `HttpClient`
  3046. * with supporting services for XSRF. Automatically imported by `HttpClientModule`.
  3047. *
  3048. * You can add interceptors to the chain behind `HttpClient` by binding them to the
  3049. * multiprovider for built-in DI token `HTTP_INTERCEPTORS`.
  3050. *
  3051. * @publicApi
  3052. * @deprecated use `provideHttpClient(withInterceptorsFromDi())` as providers instead
  3053. */
  3054. class HttpClientModule {
  3055. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpClientModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  3056. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.1.0", ngImport: i0, type: HttpClientModule });
  3057. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpClientModule, providers: [provideHttpClient(withInterceptorsFromDi())] });
  3058. }
  3059. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpClientModule, decorators: [{
  3060. type: NgModule,
  3061. args: [{
  3062. /**
  3063. * Configures the dependency injector where it is imported
  3064. * with supporting services for HTTP communications.
  3065. */
  3066. providers: [provideHttpClient(withInterceptorsFromDi())],
  3067. }]
  3068. }] });
  3069. /**
  3070. * Configures the dependency injector for `HttpClient`
  3071. * with supporting services for JSONP.
  3072. * Without this module, Jsonp requests reach the backend
  3073. * with method JSONP, where they are rejected.
  3074. *
  3075. * @publicApi
  3076. * @deprecated `withJsonpSupport()` as providers instead
  3077. */
  3078. class HttpClientJsonpModule {
  3079. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpClientJsonpModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  3080. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.1.0", ngImport: i0, type: HttpClientJsonpModule });
  3081. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpClientJsonpModule, providers: [withJsonpSupport().ɵproviders] });
  3082. }
  3083. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.0", ngImport: i0, type: HttpClientJsonpModule, decorators: [{
  3084. type: NgModule,
  3085. args: [{
  3086. providers: [withJsonpSupport().ɵproviders],
  3087. }]
  3088. }] });
  3089. export { FetchBackend, HTTP_INTERCEPTORS, HTTP_ROOT_INTERCEPTOR_FNS, HttpBackend, HttpClient, HttpClientJsonpModule, HttpClientModule, HttpClientXsrfModule, HttpContext, HttpContextToken, HttpErrorResponse, HttpEventType, HttpFeatureKind, HttpHandler, HttpHeaderResponse, HttpHeaders, HttpInterceptorHandler, HttpParams, HttpRequest, HttpResponse, HttpResponseBase, HttpStatusCode, HttpUrlEncodingCodec, HttpXhrBackend, HttpXsrfTokenExtractor, JsonpClientBackend, JsonpInterceptor, REQUESTS_CONTRIBUTE_TO_STABILITY, provideHttpClient, withFetch, withInterceptors, withInterceptorsFromDi, withJsonpSupport, withNoXsrfProtection, withRequestsMadeViaParent, withXsrfConfiguration };
  3090. //# sourceMappingURL=module.mjs.map