(responseType: ResponseType)
| 235 | | ((ctx: ResourceParamsContext) => HttpResourceRequest | undefined); |
| 236 | |
| 237 | function makeHttpResourceFn<TRaw>(responseType: ResponseType) { |
| 238 | return function httpResource<TResult = TRaw>( |
| 239 | request: RawRequestType, |
| 240 | options?: HttpResourceOptions<TResult, TRaw>, |
| 241 | ): HttpResourceRef<TResult> { |
| 242 | if (ngDevMode && !options?.injector) { |
| 243 | assertInInjectionContext(httpResource); |
| 244 | } |
| 245 | const injector = options?.injector ?? inject(Injector); |
| 246 | |
| 247 | const cacheOptions = injector.get(CACHE_OPTIONS, null, {optional: true}); |
| 248 | const transferState = injector.get(TransferState, null, {optional: true}); |
| 249 | const originMap = injector.get(HTTP_TRANSFER_CACHE_ORIGIN_MAP, null, {optional: true}); |
| 250 | |
| 251 | const getInitialStream = (req: HttpRequest<unknown> | undefined) => { |
| 252 | if (cacheOptions && transferState && req) { |
| 253 | const cachedResponse = retrieveStateFromCache(req, cacheOptions, transferState, originMap); |
| 254 | if (cachedResponse) { |
| 255 | try { |
| 256 | const body = cachedResponse.body as TRaw; |
| 257 | const parsed = options?.parse ? options.parse(body) : (body as unknown as TResult); |
| 258 | return signal({value: parsed}); |
| 259 | } catch (e) { |
| 260 | if (typeof ngDevMode === 'undefined' || ngDevMode) { |
| 261 | console.warn( |
| 262 | `Angular detected an error while parsing the cached response for the httpResource at \`${req.url}\`. ` + |
| 263 | `The resource will fall back to its default value and try again asynchronously.`, |
| 264 | e, |
| 265 | ); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | return undefined; |
| 271 | }; |
| 272 | |
| 273 | return new HttpResourceImpl( |
| 274 | injector, |
| 275 | (ctx: ResourceParamsContext) => normalizeRequest(ctx, request, responseType), |
| 276 | options?.defaultValue, |
| 277 | options?.debugName, |
| 278 | options?.parse as (value: unknown) => TResult, |
| 279 | options?.equal as ValueEqualityFn<unknown>, |
| 280 | getInitialStream, |
| 281 | ) as HttpResourceRef<TResult>; |
| 282 | }; |
| 283 | } |
| 284 | |
| 285 | function normalizeRequest( |
| 286 | ctx: ResourceParamsContext, |
no test coverage detected
searching dependent graphs…