agentleFS
L-X-TL-X-T/ng-agentic-skills/.agents/skills/ng-data-access/SKILL.md

ng-data-access

Skill13 starsChanged 43 days agoMITView on GitHub

Build the typed server-communication layer in Angular v22+ – fetch data through httpResource/resource/rxResource backed by HttpClient, with functional interceptors, loading/error/reload state, and SSR-safe transfer caching. Use when fetching data, writing an API service, wiring HttpClient, choosing httpResource vs resource vs rxResource, adding an interceptor, or surfacing loading/error/reload state. The project's TypeScript style guide prefers resource()/rxResource()/httpResource() over "RxJS + async pipe" for reads.

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Check app.config.ts first: if it lacks provideHttpClient, add it before any fetch works.

    line 25
  2. Distinctive:

    Read the state as signals, never subscribe manually:

    line 120
  3. Very specific:

    Test: a vitest spec with provideHttpClientTesting() + HttpTestingController to expect/flush requests offline – see the ng-testing skill for …

    line 133

≈ “Common” is an estimate: at least 1 in 5 of the 364,980 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Angular Data Access

The server-communication layer is signals-first: a reactive request drives an async loader, and status/value/error come back as synchronous signals you read straight in an OnPush template. Reads go through resource-family primitives (preferred by style-guide/style-guide.ts.md over "RxJS + async pipe"); mutations stay imperative HttpClient calls. Everything routes through the HttpClient stack so interceptors, testing, and SSR transfer cache apply uniformly.

For exhaustive resource API depth, defer to the canonical ../angular-developer/references/resource.md (synced from Angular's official skill). This skill owns the project-shaped layer: wiring, service shape, interceptors, error UX, and SSR.

1. Wire HttpClient first

Check app.config.ts first: if it lacks provideHttpClient, add it before any fetch works. Use withFetch() so requests run on the Fetch API (SSR-friendly, streaming-capable):

// src/app/app.config.ts
import { provideHttpClient, withFetch, withInterceptors } from '@angular/common/http';

export const appConfig: ApplicationConfig = {
  providers: [
    // …existing providers, incl. provideClientHydration(withEventReplay())
    provideHttpClient(withFetch(), withInterceptors([/* fns */])),
  ],
};

provideClientHydration() is already present, so the HTTP transfer cache is on by default – any HttpClient-backed fetch during SSR is replayed on the client without a second request. See references/interceptors-and-ssr.md.

Done when provideHttpClient(withFetch()) is in app.config.ts and the app builds.

2. Pick the primitive

NeedUse
Reactive read through HttpClient (interceptors, transfer cache)httpResource – default for reads
Async read from a non-HttpClient source (fetch, SDK, promise)resource
Read whose loader is naturally an Observable (debounce, retry, switchMap)rxResource
One-shot call or a mutation (POST/PUT/PATCH/DELETE on user action)plain HttpClient (returns Observable)

resource/httpResource/rxResource are read-only – they cancel in-flight loads via AbortSignal on destroy or param change, which would abort a mutation. Never fetch a mutation through them. Details and verified examples: references/resource-apis.md.

Worked example – signal params → httpResource → template states

import { Component, signal } from '@angular/core';
import { httpResource } from '@angular/common/http';
import { User } from './user-api';

@Component({
  selector: 'app-user-card',
  template: `
    @if (user.isLoading()) {
      <p>Loading…</p>
    } @else if (user.error()) {
      <p class="error" role="alert">Couldn't load user. <button (click)="user.reload()">Retry</button></p>
    } @else {
      <p>{{ user.value()?.name }}</p>
    }
  `,
})
export class UserCard {
  protected readonly userId = signal('123');
  // Re-fetches whenever userId() changes; undefined URL ⇒ resource stays idle.
  protected readonly user = httpResource<User>(() => `/api/users/${this.userId()}`);
}

3. Typed service shape

Group HTTP concerns behind a root-provided service with typed models. Expose mutations as methods returning Observable; let components/stores create the read httpResource at the call site.

import { HttpClient } from '@angular/common/http';
import { inject, Injectable } from '@angular/core';
import { Observable } from 'rxjs';

export interface User {
  id: string;
  name: string;
  email: string;
}

@Injectable({ providedIn: 'root' })
export class UserApi {
  private readonly http = inject(HttpClient);
  private readonly baseUrl = '/api/users';

  create(user: Omit<User, 'id'>): Observable<User> {
    return this.http.post<User>(this.baseUrl, user);
  }
  update(id: string, patch: Partial<User>): Observable<User> {
    return this.http.patch<User>(`${this.baseUrl}/${id}`, patch);
  }
}

Signal stores consume this layer – they hold state, not fetching logic (see ng-signal-store). Form submits call the mutation methods (see ng-forms).

4. Loading / error / reload UX

Read the state as signals, never subscribe manually:

  • isLoading() – gate a spinner (covers both loading and reloading).
  • error() – Signal<Error | undefined>; show a recoverable message with a retry wired to reload().
  • hasValue() – reactive type-guard; prefer over value() !== undefined.
  • value() – keeps the previous value during reloading, so the UI need not flash empty.

Give error() branches role="alert" and always offer reload() – a dead-end error is a bug.

5. Verify loop

  • Types: pnpm exec tsc --noEmit (or pnpm build).
  • Lint: pnpm lint.
  • Test: a vitest spec with provideHttpClientTesting() + HttpTestingController to expect/flush requests offline – see the ng-testing skill for the harness.

Done when tsc + lint pass and the fetch behaviour is covered by a test.

Cross-links

  • ng-security – XSRF (withXsrfConfiguration), SSR allowedHosts. Don't re-teach here.
  • ng-testing – HttpTestingController harness for the specs above.
  • ng-signal-store – stores consume this layer; don't duplicate fetching in them.
  • ng-forms – form submission calls the service's mutation methods.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.