MCPcopy Create free account
hub / github.com/angular/angular / makeHttpResourceFn

Function makeHttpResourceFn

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

Source from the content-addressed store, hash-verified

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