MCPcopy Index your code
hub / github.com/angular/angular / makeHttpResourceFn

Function makeHttpResourceFn

packages/common/http/src/resource.ts:237–283  ·  view source on GitHub ↗
(responseType: ResponseType)

Source from the content-addressed store, hash-verified

235 | ((ctx: ResourceParamsContext) => HttpResourceRequest | undefined);
236
237function 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
285function normalizeRequest(
286 ctx: ResourceParamsContext,

Callers 1

resource.tsFile · 0.85

Calls 4

assertInInjectionContextFunction · 0.90
injectFunction · 0.90
normalizeRequestFunction · 0.85
getMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…