forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): introduce experimental
httpResource
WIP: this commit is a work-in-progress of `httpResource`, a new entrypoint to `HttpClient` for declaratively configuring resources backed by HTTP requests.
- Loading branch information
Showing
5 changed files
with
216 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import { | ||
Injector, | ||
Signal, | ||
WritableResource, | ||
computed, | ||
ɵResourceImpl as ResourceImpl, | ||
inject, | ||
linkedSignal, | ||
WritableSignal, | ||
assertInInjectionContext, | ||
ValueEqualityFn, | ||
} from '@angular/core'; | ||
import {HttpRequest} from './request'; | ||
import {HttpClient} from './client'; | ||
import {HttpEventType, HttpProgressEvent} from './response'; | ||
import {HttpHeaders} from './headers'; | ||
import {HttpParams} from './params'; | ||
|
||
export interface HttpResourceRequest { | ||
url: string; | ||
method?: string; | ||
body?: unknown; | ||
params?: | ||
| HttpParams | ||
| Record<string, string | number | boolean | ReadonlyArray<string | number | boolean>>; | ||
headers?: HttpHeaders | Record<string, string | ReadonlyArray<string>>; | ||
reportProgress?: boolean; | ||
withCredentials?: boolean; | ||
} | ||
|
||
export interface HttpResourceOptions<T> { | ||
map?: (value: unknown) => T; | ||
/** | ||
* Value that the resource will take when in Idle, Loading, or Error states. | ||
*/ | ||
defaultValue?: NoInfer<T>; | ||
|
||
// TODO: equal? | ||
injector?: Injector; | ||
} | ||
|
||
export function httpResource<T = unknown>( | ||
url: string | (() => string), | ||
options: HttpResourceOptions<T> & {defaultValue: NoInfer<T>}, | ||
): HttpResource<T>; | ||
export function httpResource<T = unknown>( | ||
url: string | (() => string), | ||
options?: HttpResourceOptions<T>, | ||
): HttpResource<T | undefined>; | ||
export function httpResource<T = unknown>( | ||
request: HttpResourceRequest | (() => HttpResourceRequest), | ||
options?: HttpResourceOptions<T> & {defaultValue: NoInfer<T>}, | ||
): HttpResource<T>; | ||
export function httpResource<T = unknown>( | ||
request: HttpResourceRequest | (() => HttpResourceRequest), | ||
options?: HttpResourceOptions<T>, | ||
): HttpResource<T | undefined>; | ||
export function httpResource( | ||
request: string | HttpResourceRequest | (() => string | HttpResourceRequest), | ||
options?: HttpResourceOptions<unknown>, | ||
): HttpResource<unknown> { | ||
options?.injector || assertInInjectionContext(httpResource); | ||
const injector = options?.injector ?? inject(Injector); | ||
|
||
const toHttpRequest = () => { | ||
let unwrappedRequest = typeof request === 'function' ? request() : request; | ||
if (typeof unwrappedRequest === 'string') { | ||
unwrappedRequest = {url: unwrappedRequest}; | ||
} | ||
|
||
return new HttpRequest( | ||
unwrappedRequest.method ?? 'GET', | ||
unwrappedRequest.url, | ||
unwrappedRequest.body ?? null, | ||
{ | ||
headers: | ||
unwrappedRequest.headers instanceof HttpHeaders | ||
? unwrappedRequest.headers | ||
: new HttpHeaders( | ||
unwrappedRequest.headers as | ||
| Record<string, string | number | Array<string | number>> | ||
| undefined, | ||
), | ||
params: | ||
unwrappedRequest.params instanceof HttpParams | ||
? unwrappedRequest.params | ||
: new HttpParams(unwrappedRequest.params), | ||
reportProgress: unwrappedRequest.reportProgress, | ||
withCredentials: unwrappedRequest.withCredentials, | ||
responseType: 'json', | ||
}, | ||
); | ||
}; | ||
|
||
return new HttpResourceImpl(injector, toHttpRequest, options?.defaultValue, options?.map); | ||
} | ||
|
||
export interface HttpResource<T> extends WritableResource<T> { | ||
headers: Signal<HttpHeaders | undefined>; | ||
} | ||
|
||
class HttpResourceImpl<T> extends ResourceImpl<T, HttpRequest<unknown>> implements HttpResource<T> { | ||
private client!: HttpClient; | ||
private _headers = linkedSignal({ | ||
source: this.extendedRequest, | ||
computation: () => undefined as HttpHeaders | undefined, | ||
}); | ||
private _progress = linkedSignal({ | ||
source: this.extendedRequest, | ||
computation: () => undefined as HttpProgressEvent | undefined, | ||
}); | ||
|
||
readonly headers = this._headers.asReadonly(); | ||
readonly progress = this._progress.asReadonly(); | ||
|
||
constructor( | ||
injector: Injector, | ||
request: () => HttpRequest<T>, | ||
defaultValue: T, | ||
map?: (value: unknown) => T, | ||
) { | ||
super( | ||
request, | ||
({request, set, error, abortSignal}) => { | ||
const sub = this.client.request(request).subscribe({ | ||
next: (event) => { | ||
switch (event.type) { | ||
case HttpEventType.Response: | ||
this._headers.set(event.headers); | ||
try { | ||
set(map ? map(event.body) : (event.body as T)); | ||
} catch (err) { | ||
error(err); | ||
} | ||
break; | ||
case HttpEventType.DownloadProgress: | ||
this._progress.set(event); | ||
break; | ||
} | ||
}, | ||
error, | ||
}); | ||
abortSignal.addEventListener('abort', () => sub.unsubscribe()); | ||
}, | ||
defaultValue, | ||
undefined, | ||
injector, | ||
); | ||
this.client = injector.get(HttpClient); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters