@ haddowg/ json- api- angula
部分 jsonapi.rest (英语) 套间 - 完整、符合规格 JSON: API 1. 1 堆栈: 框架不可知 PHP 核心项目页:1 符号捆绑页:1 Laravel 软件包这个 键入类型脚本 客户端,由经过检测的OpenAPI 3.1合同捆绑在一起. 完整文档: (原始内容存档于2018-10-21) (韩语). haddog.github.io/json-api-ts。 。 。 .
生成的基于信号的角绑定 @haddowg/json-api-client客户端
进入依赖性注射 并通过Angular发送 HttpClient,所以你的拦截器运行
每一个请求。 读取通过Angular成为信号 resource(),写一个小
以信号为基础的变异手柄.
安装
npm install @haddowg/json-api-angular @haddowg/json-api-client
@angular/core 财务报告和审定财务报表 @angular/common (20, 21或22)是同级依赖关系.
提供客户端
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' }),
],
}
注册客户端类型一次, 这样它会无符号地输入 :
import type { JsonApiClientOf } from '@haddowg/json-api-angular'
declare module '@haddowg/json-api-angular' {
interface Register {
client: JsonApiClientOf<typeof createClient>
}
}
对于第二个客户端(另一个服务器),用 createJsonApiClientToken() 传过来
作为第三个参数 provideJsonApiClient,然后改为 injectJsonApiClient(token) 或一个
资源 client 选项.
用资源读取
@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 }),
})
}
变化 search 开始新的读取并取消飞行中的读取。 返回时 undefined 从
params 将读取直到其投入准备好。 与任何角资源一样, value()
丢入出错状态, 所以用 hasValue()。 。 。 .
nextPage(list) 财务报告和审定财务报表 prevPage(list) 返回 page 收藏链接中的参数,准备
设置在读取的参数上的信号上. loadAll(firstPage, { signal }) 如下 next 链接和
返回一个数组中的每个成员.
写作突变
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() 财务报告和审定财务报表 reset() 跟踪最新通话。 没什么
在资源之间被缓存,所以重新装入读取的写入影响 .
见 角向导 关于细节.
许可证
麻省理工学院