@haddowg/json-api-angular

中文文档
1.2.23发布于 2026/11/17MITTypeScriptESM + CJS构建来源已验证近 30 天下载 4940

Signal-based Angular bindings for the JSON:API client: an HttpClient transport, DI providers, and resource/mutation helpers.

原始使用文档
文档目录5 节
  1. Install
  2. Provide the client
  3. Read with a resource
  4. Write with a mutation
  5. License

@haddowg/json-api-angular

Part of the jsonapi.rest suite — a complete, spec-compliant JSON:API 1.1 stack: a framework-agnostic PHP core, a Symfony bundle, a Laravel package, and this typed TypeScript client, bound together by one conformance-tested OpenAPI 3.1 contract. Full documentation: haddowg.github.io/json-api-ts.

Signal-based Angular bindings for a generated @haddowg/json-api-client. The client goes into dependency injection and sends through Angular's HttpClient, so your interceptors run on every request. Reads become signals through Angular's resource(), and writes get a small signal-based mutation handle.

Install

npm install @haddowg/json-api-angular @haddowg/json-api-client

@angular/core and @angular/common (20, 21 or 22) are peer dependencies.

Provide the client

import { provideHttpClient, withInterceptors } from '@angular/common/http'
import { provideJsonApiClient } from '@haddowg/json-api-angular'
import { createClient } from './api/music.gen'

export const appConfig: ApplicationConfig = {
  providers: [
    provideHttpClient(withInterceptors([authInterceptor])),
    provideJsonApiClient(createClient, { baseUrl: '/api/v5' }),
  ],
}

Register the client's type once so it is typed everywhere without a token:

import type { JsonApiClientOf } from '@haddowg/json-api-angular'

declare module '@haddowg/json-api-angular' {
  interface Register {
    client: JsonApiClientOf<typeof createClient>
  }
}

For a second client (another server), create a token with createJsonApiClientToken() and pass it as the third argument to provideJsonApiClient, then to injectJsonApiClient(token) or a resource's client option.

Read with a resource

@Component({
  template: `
    @if (albums.hasValue()) {
      @for (album of albums.value(); track album.id) {
        <li>{{ album.title }} by {{ album.artist?.name }}</li>
      }
    }
    @if (albums.jsonApiError(); as error) {
      <p>{{ error.status }}</p>
    }
  `,
})
export class AlbumList {
  readonly search = signal('')

  readonly albums = jsonApiResource({
    params: () => ({ filter: { title: this.search() } }),
    loader: ({ client, params, abortSignal }) =>
      client.albums.list({ ...params, include: ['artist'], signal: abortSignal }),
  })
}

Changing search starts a new read and cancels the one in flight. Return undefined from params to hold the read until its inputs are ready. As with any Angular resource, value() throws in the error state, so guard it with hasValue().

nextPage(list) and prevPage(list) return the page params from a collection's links, ready to set on the signal your params read. loadAll(firstPage, { signal }) follows next links and returns every member in one array.

Write with a mutation

readonly rename = jsonApiMutation((client, title: string) =>
  client.albums.id(this.id()).update({ title }),
)

save(title: string) {
  this.rename.mutate(title).then(() => this.albums.reload(), () => {})
}

isPending(), error(), jsonApiError(), value() and reset() track the latest call. Nothing is cached between resources, so reload the reads a write affects.

See the Angular guide for the details.

License

MIT

版本与发布记录

@haddowg/json-api-angular 共发布 29 个版本,首次发布于 2026/10/22;过去 12 个月发布了 29 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.2.23latest2026/11/17今天
1.2.222026/11/17今天
1.2.212026/11/16今天
1.2.202026/11/133 天前
1.2.192026/11/134 天前
1.2.182026/11/124 天前

关于 @haddowg/json-api-angular 的常见问题

@haddowg/json-api-angular 是什么?

Signal-based Angular bindings for the JSON:API client: an HttpClient transport, DI providers, and resource/mutation helpers.

如何安装 @haddowg/json-api-angular?

在项目目录运行 npm install @haddowg/json-api-angular,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@angular/core(^20.0.0 || ^21.0.0 || ^22.0.0)、@angular/common(^20.0.0 || ^21.0.0 || ^22.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@haddowg/json-api-angular 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@haddowg/json-api-angular 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@haddowg/json-api-angular 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@haddowg/json-api-angular 可以商用吗?

@haddowg/json-api-angular 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@haddowg/json-api-angular 的下载量有多少?

根据 npm 官方下载统计,@haddowg/json-api-angular 在 2026/10/17 – 2026/11/15 这 30 天内被下载 4,940 次,日均约 165 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@haddowg/json-api-angular 还在维护吗?

最新版本 1.2.23 发布于 2026/11/17(今天);至今共发布 29 个版本,其中过去 12 个月发布了 29 个;首次发布于 2026/10/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。