import { computed, untracked } from '@angular/core' import type { Resource, ResourceStatus, Signal } from '@angular/core' import type { QueryObserverResult } from '@tanstack/query-core' import type { ResourceSnapshot } from './utils/resource-types' /** * A query-backed Angular Resource that also exposes an atomic state snapshot. */ export interface QueryResource extends Resource { /** * The current query state as a single snapshot. */ readonly snapshot: Signal> /** * Requests a new query fetch only when the current query data is stale. * * Used for compatibility with Angular APIs that might want to reload a resource, * like Signal Forms' validateAsync resource interface. * @returns `true` if a reload was initiated, `false` if a reload was unnecessary or unsupported. */ reload: () => boolean } type QueryResourceSource = { readonly data: Signal readonly status: Signal readonly fetchStatus: Signal readonly error: Signal readonly isStale: Signal readonly refetch: () => unknown } /** * Converts an Angular Query result into Angular's Resource interface. * * Warning: experimental feature. The API may change in minor and patch releases. * Pin your package version to an exact version when using this feature. * * Call this function once and reuse the returned resource. * @param query - An Angular Query or infinite-query result. * @returns A Resource-compatible view of the query result. */ export function toResource( query: QueryResourceSource, ): QueryResource { // Intentionally does not use resource from snapshot to have // support for the reload method for compatible libraries const status = computed(() => { const fetchStatus = query.fetchStatus() const data = query.data() if (fetchStatus === 'fetching' || fetchStatus === 'paused') { return data === undefined ? 'loading' : 'reloading' } switch (query.status()) { case 'success': return 'resolved' case 'error': return 'error' case 'pending': return 'idle' } }) const error = computed(() => { return query.status() === 'error' ? normalizeError(query.error()) : undefined }) const value = computed(() => { if (status() === 'error') { throw error() } return query.data() }) const snapshot = computed>(() => { const currentStatus = status() if (currentStatus === 'error') { return { status: 'error', error: error()! } } return { status: currentStatus, value: query.data() } }) const hasValue = (() => status() !== 'error' && query.data() !== undefined) as QueryResource['hasValue'] return { value, status, error, snapshot, isLoading: computed(() => { const currentStatus = status() return currentStatus === 'loading' || currentStatus === 'reloading' }), hasValue, reload(): boolean { const currentStatus = untracked(status) if ( currentStatus === 'idle' || currentStatus === 'loading' || currentStatus === 'reloading' ) { return false } if (!untracked(query.isStale)) { return false } void query.refetch() return true }, } } /** * Converts an unknown rejection reason to an Error-compatible value. * @param error - The rejected value. * @returns The existing error-like value, or an Error wrapping the rejection. */ function normalizeError(error: unknown): Error { if (isErrorLike(error)) { return error } return new Error(String(error), { cause: error }) } /** * Checks whether a value provides the name and message of an Error. * @param error - The value to inspect. * @returns Whether the value can be used as an Error. */ function isErrorLike(error: unknown): error is Error { return ( error instanceof Error || (typeof error === 'object' && error !== null && typeof (error as Error).name === 'string' && typeof (error as Error).message === 'string') ) }