Skip to content

Commit 8aec2df

Browse files
committed
feat(common): introduce experimental httpResource
`httpResource` is a new frontend to the `HttpClient` infrastructure. It declares a dependency on an HTTP endpoint. The request to be made can be reactive, updating in response to signals for the URL, method, or otherwise. The response is returned as an instance of `HttpResource`, a `WritableResource` with some additional signals which represent parts of the HTTP response metadata (status, headers, etc).
1 parent 5e52249 commit 8aec2df

File tree

7 files changed

+850
-2
lines changed

7 files changed

+850
-2
lines changed

goldens/public-api/common/http/index.api.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ import { EnvironmentInjector } from '@angular/core';
88
import { EnvironmentProviders } from '@angular/core';
99
import * as i0 from '@angular/core';
1010
import { InjectionToken } from '@angular/core';
11+
import type { Injector } from '@angular/core';
1112
import { ModuleWithProviders } from '@angular/core';
1213
import { Observable } from 'rxjs';
1314
import { Provider } from '@angular/core';
15+
import type { ResourceRef } from '@angular/core';
16+
import type { Signal } from '@angular/core';
17+
import type { ValueEqualityFn } from '@angular/core';
18+
import type { WritableResource } from '@angular/core';
1419
import { XhrFactory } from '@angular/common';
1520

1621
// @public
@@ -2153,6 +2158,84 @@ export class HttpRequest<T> {
21532158
readonly withCredentials: boolean;
21542159
}
21552160

2161+
// @public
2162+
export const httpResource: HttpResourceFn;
2163+
2164+
// @public
2165+
export interface HttpResourceFn {
2166+
<TResult = unknown>(url: string | (() => string | undefined), options: HttpResourceOptions<TResult, unknown> & {
2167+
defaultValue: NoInfer<TResult>;
2168+
}): HttpResourceRef<TResult>;
2169+
<TResult = unknown>(url: string | (() => string | undefined), options?: HttpResourceOptions<TResult, unknown>): HttpResourceRef<TResult | undefined>;
2170+
<TResult = unknown>(request: HttpResourceRequest | (() => HttpResourceRequest | undefined), options: HttpResourceOptions<TResult, unknown> & {
2171+
defaultValue: NoInfer<TResult>;
2172+
}): HttpResourceRef<TResult>;
2173+
<TResult = unknown>(request: HttpResourceRequest | (() => HttpResourceRequest | undefined), options?: HttpResourceOptions<TResult, unknown>): HttpResourceRef<TResult | undefined>;
2174+
arrayBuffer: {
2175+
<TResult = ArrayBuffer>(url: string | (() => string | undefined), options: HttpResourceOptions<TResult, ArrayBuffer> & {
2176+
defaultValue: NoInfer<TResult>;
2177+
}): HttpResourceRef<TResult>;
2178+
<TResult = ArrayBuffer>(url: string | (() => string | undefined), options?: HttpResourceOptions<TResult, ArrayBuffer>): HttpResourceRef<TResult | undefined>;
2179+
<TResult = ArrayBuffer>(request: HttpResourceRequest | (() => HttpResourceRequest | undefined), options: HttpResourceOptions<TResult, ArrayBuffer> & {
2180+
defaultValue: NoInfer<TResult>;
2181+
}): HttpResourceRef<TResult>;
2182+
<TResult = ArrayBuffer>(request: HttpResourceRequest | (() => HttpResourceRequest | undefined), options?: HttpResourceOptions<TResult, ArrayBuffer>): HttpResourceRef<TResult | undefined>;
2183+
};
2184+
blob: {
2185+
<TResult = Blob>(url: string | (() => string | undefined), options: HttpResourceOptions<TResult, Blob> & {
2186+
defaultValue: NoInfer<TResult>;
2187+
}): HttpResourceRef<TResult>;
2188+
<TResult = Blob>(url: string | (() => string | undefined), options?: HttpResourceOptions<TResult, Blob>): HttpResourceRef<TResult | undefined>;
2189+
<TResult = Blob>(request: HttpResourceRequest | (() => HttpResourceRequest | undefined), options: HttpResourceOptions<TResult, Blob> & {
2190+
defaultValue: NoInfer<TResult>;
2191+
}): HttpResourceRef<TResult>;
2192+
<TResult = Blob>(request: HttpResourceRequest | (() => HttpResourceRequest | undefined), options?: HttpResourceOptions<TResult, Blob>): HttpResourceRef<TResult | undefined>;
2193+
};
2194+
text: {
2195+
<TResult = string>(url: string | (() => string | undefined), options: HttpResourceOptions<TResult, string> & {
2196+
defaultValue: NoInfer<TResult>;
2197+
}): HttpResourceRef<TResult>;
2198+
<TResult = string>(url: string | (() => string | undefined), options?: HttpResourceOptions<TResult, string>): HttpResourceRef<TResult | undefined>;
2199+
<TResult = string>(request: HttpResourceRequest | (() => HttpResourceRequest | undefined), options: HttpResourceOptions<TResult, string> & {
2200+
defaultValue: NoInfer<TResult>;
2201+
}): HttpResourceRef<TResult>;
2202+
<TResult = string>(request: HttpResourceRequest | (() => HttpResourceRequest | undefined), options?: HttpResourceOptions<TResult, string>): HttpResourceRef<TResult | undefined>;
2203+
};
2204+
}
2205+
2206+
// @public
2207+
export interface HttpResourceOptions<TResult, TRaw> {
2208+
defaultValue?: NoInfer<TResult>;
2209+
equal?: ValueEqualityFn<NoInfer<TResult>>;
2210+
injector?: Injector;
2211+
map?: (value: TRaw) => TResult;
2212+
}
2213+
2214+
// @public
2215+
export interface HttpResourceRef<T> extends WritableResource<T>, ResourceRef<T> {
2216+
// (undocumented)
2217+
destroy(): void;
2218+
// (undocumented)
2219+
hasValue(): this is HttpResourceRef<Exclude<T, undefined>>;
2220+
readonly headers: Signal<HttpHeaders | undefined>;
2221+
readonly progress: Signal<HttpProgressEvent | undefined>;
2222+
readonly statusCode: Signal<number | undefined>;
2223+
}
2224+
2225+
// @public
2226+
export interface HttpResourceRequest {
2227+
body?: unknown;
2228+
headers?: HttpHeaders | Record<string, string | ReadonlyArray<string>>;
2229+
method?: string;
2230+
params?: HttpParams | Record<string, string | number | boolean | ReadonlyArray<string | number | boolean>>;
2231+
reportProgress?: boolean;
2232+
transferCache?: {
2233+
includeHeaders?: string[];
2234+
} | boolean;
2235+
url: string;
2236+
withCredentials?: boolean;
2237+
}
2238+
21562239
// @public
21572240
export class HttpResponse<T> extends HttpResponseBase {
21582241
constructor(init?: {

packages/common/http/public_api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export {
5454
HttpUploadProgressEvent,
5555
HttpUserEvent,
5656
} from './src/response';
57+
export {HttpResourceRef, HttpResourceOptions, HttpResourceRequest} from './src/resource_api';
58+
export {httpResource, HttpResourceFn} from './src/resource';
5759
export {
5860
HttpTransferCacheOptions,
5961
withHttpTransferCache as ɵwithHttpTransferCache,

0 commit comments

Comments
 (0)