| 7 | providedIn: 'root', |
| 8 | }) |
| 9 | export class PostsQueries { |
| 10 | readonly #api = inject(PostsAPI) |
| 11 | |
| 12 | all = () => ['posts'] as const |
| 13 | |
| 14 | list = () => |
| 15 | queryOptions({ |
| 16 | queryKey: [...this.all(), 'list'] as const, |
| 17 | queryFn: () => lastValueFrom(this.#api.getPosts()), |
| 18 | }) |
| 19 | |
| 20 | detail = (id: number) => |
| 21 | queryOptions({ |
| 22 | queryKey: [...this.all(), 'detail', id] as const, |
| 23 | queryFn: (context) => { |
| 24 | // Cancels the request when component is destroyed before the request finishes |
| 25 | const abort$ = fromEvent(context.signal, 'abort') |
| 26 | return lastValueFrom(this.#api.getPost(id).pipe(takeUntil(abort$))) |
| 27 | }, |
| 28 | }) |
| 29 | } |
nothing calls this directly
no test coverage detected