@h-ai/api-client 0.1.0-alpha.65 → 0.1.0-alpha.67
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +14 -10
- package/README.ja.md +15 -11
- package/README.md +15 -11
- package/package.json +4 -4
package/README.en.md
CHANGED
|
@@ -4,9 +4,11 @@
|
|
|
4
4
|
|
|
5
5
|
[Framework home](../../README.en.md) · Related modules: [@h-ai/api-contract](../api-contract/README.en.md) · [@h-ai/core](../core/README.en.md) · [@h-ai/crypto](../crypto/README.en.md)
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## Introduction
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
A cross-platform oRPC/OpenAPI typed client for hai-framework, with consistent API calls and token management across Web, apps, and mini programs.
|
|
10
|
+
|
|
11
|
+
## Core features
|
|
10
12
|
|
|
11
13
|
- `apiClient`: the default typed client for the iam/storage/ai domains.
|
|
12
14
|
- `apiClient.create(contract)`: create a typed client for a custom contract.
|
|
@@ -17,7 +19,9 @@
|
|
|
17
19
|
- Optional transport encryption: `apiClient.init({ transport: { crypto } })` automatically uses `crypto.transport.createClient()`.
|
|
18
20
|
- Transport encryption and automatic refresh can run together: `/auth/refresh` after a 401 still uses encrypted fetch without falling back to plaintext.
|
|
19
21
|
|
|
20
|
-
##
|
|
22
|
+
## Getting started
|
|
23
|
+
|
|
24
|
+
### Quick start
|
|
21
25
|
|
|
22
26
|
```ts
|
|
23
27
|
import { apiClient } from '@h-ai/api-client'
|
|
@@ -47,7 +51,7 @@ if (me.success) {
|
|
|
47
51
|
await apiClient.close()
|
|
48
52
|
```
|
|
49
53
|
|
|
50
|
-
|
|
54
|
+
#### Enable transport encryption
|
|
51
55
|
|
|
52
56
|
First enable `serv.createApp({ transport: { crypto } })` on the server. The client only needs the same `@h-ai/crypto` service instance; no manual key exchange code is required.
|
|
53
57
|
|
|
@@ -67,7 +71,7 @@ await apiClient.close()
|
|
|
67
71
|
await crypto.close()
|
|
68
72
|
```
|
|
69
73
|
|
|
70
|
-
|
|
74
|
+
### API contracts
|
|
71
75
|
|
|
72
76
|
Custom applications can bind their own oRPC contract:
|
|
73
77
|
|
|
@@ -82,7 +86,7 @@ await client.init({ baseUrl: 'https://api.example.com/api/v1' })
|
|
|
82
86
|
const result = await client.iam.auth.login({ identifier: 'alice', password: 'secret' })
|
|
83
87
|
```
|
|
84
88
|
|
|
85
|
-
|
|
89
|
+
### API overview
|
|
86
90
|
|
|
87
91
|
- `apiClient.init(config)`: initialize the default client.
|
|
88
92
|
- `apiClient.close()`: clear client state.
|
|
@@ -92,7 +96,7 @@ const result = await client.iam.auth.login({ identifier: 'alice', password: 'sec
|
|
|
92
96
|
|
|
93
97
|
Calling `init()` on an initialized client returns `CONFIG_ERROR` and leaves the existing client intact. To change `baseUrl` / `fetch` / `transport`, call `await apiClient.close()` before initializing again.
|
|
94
98
|
|
|
95
|
-
|
|
99
|
+
### Configuration
|
|
96
100
|
|
|
97
101
|
- `baseUrl`: API base URL, usually including `/api/v1`.
|
|
98
102
|
- `auth.storage`: token storage adapter; defaults to `apiClient.tokenStorage.httpOnlyCookie()` (recommended for browsers). Explicitly use `apiClient.tokenStorage.memory()` for SSR / tests.
|
|
@@ -104,7 +108,7 @@ Calling `init()` on an initialized client returns `CONFIG_ERROR` and leaves the
|
|
|
104
108
|
- `transport.keyExchangePath`: key exchange subpath; defaults to `/_hai/key-exchange` and is appended to `baseUrl` automatically.
|
|
105
109
|
- `apiClient.config`: a **redacted snapshot of runtime configuration**; credentials embedded in `baseUrl`, `headers.authorization`, and other sensitive values are hidden automatically.
|
|
106
110
|
|
|
107
|
-
|
|
111
|
+
### Configuration driven authentication
|
|
108
112
|
|
|
109
113
|
Agreements support configuration-loaded userAgreementMarkdown/privacyPolicyMarkdown and URLs. Markdown takes priority for the same agreement and is safely rendered in a modal.
|
|
110
114
|
|
|
@@ -112,7 +116,7 @@ apiClient.createAuthActions(client) supplies publicConfig, login, register, send
|
|
|
112
116
|
|
|
113
117
|
[完整 IAM 配置 / IAM configuration](../iam/README.en.md)
|
|
114
118
|
|
|
115
|
-
|
|
119
|
+
### Error handling
|
|
116
120
|
|
|
117
121
|
Business APIs return `HaiResult<T>`:
|
|
118
122
|
|
|
@@ -130,7 +134,7 @@ Client-side failures such as missing initialization, network errors, timeouts, a
|
|
|
130
134
|
|
|
131
135
|
Automatic refresh handles an actual HTTP 401 (`Response.status`) or a valid failed HaiResult over HTTP 200 (`success=false`, string `error.code/message` and `error.httpStatus=401`). After a successful refresh, the original request is retried once. Built-in login and registration routes and the configured `auth.refreshPath` never trigger automatic refresh; other failures such as 403/500 do not trigger it either. Ordinary serv procedures still return HTTP 200. Callers must check the final `success` and `error.code` and handle expired authentication when refresh fails.
|
|
132
136
|
|
|
133
|
-
##
|
|
137
|
+
## Development and testing
|
|
134
138
|
|
|
135
139
|
```bash
|
|
136
140
|
pnpm --filter @h-ai/api-client test
|
package/README.ja.md
CHANGED
|
@@ -4,9 +4,11 @@
|
|
|
4
4
|
|
|
5
5
|
[フレームワークのホーム](../../README.ja.md) · 関連モジュール:[@h-ai/api-contract](../api-contract/README.ja.md) · [@h-ai/core](../core/README.ja.md) · [@h-ai/crypto](../crypto/README.ja.md)
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## 概要
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
hai-framework のクロスプラットフォーム oRPC/OpenAPI 型付きクライアント。Web、アプリ、ミニプログラムに共通の API 呼び出しと Token 管理を提供します。
|
|
10
|
+
|
|
11
|
+
## 主な機能
|
|
10
12
|
|
|
11
13
|
- `apiClient`:iam/storage/ai ドメインに対応する既定の型付きクライアント。
|
|
12
14
|
- `apiClient.create(contract)`:独自の contract に対応する型付きクライアントを作成。
|
|
@@ -17,7 +19,9 @@
|
|
|
17
19
|
- 任意の通信暗号化:`apiClient.init({ transport: { crypto } })` が `crypto.transport.createClient()` を自動使用。
|
|
18
20
|
- 通信暗号化と自動更新を併用可能。401 後の `/auth/refresh` も暗号化された fetch を使用し、平文には切り替わりません。
|
|
19
21
|
|
|
20
|
-
##
|
|
22
|
+
## 使い始める
|
|
23
|
+
|
|
24
|
+
### クイックスタート
|
|
21
25
|
|
|
22
26
|
```ts
|
|
23
27
|
import { apiClient } from '@h-ai/api-client'
|
|
@@ -47,7 +51,7 @@ if (me.success) {
|
|
|
47
51
|
await apiClient.close()
|
|
48
52
|
```
|
|
49
53
|
|
|
50
|
-
|
|
54
|
+
#### 通信暗号化の有効化
|
|
51
55
|
|
|
52
56
|
サーバーで先に `serv.createApp({ transport: { crypto } })` を有効にします。クライアントには同じ `@h-ai/crypto` サービスのインスタンスを渡すだけで、鍵交換コードの実装は不要です。
|
|
53
57
|
|
|
@@ -67,7 +71,7 @@ await apiClient.close()
|
|
|
67
71
|
await crypto.close()
|
|
68
72
|
```
|
|
69
73
|
|
|
70
|
-
|
|
74
|
+
### API 契約
|
|
71
75
|
|
|
72
76
|
独自のアプリケーションでは、専用の oRPC contract を指定できます:
|
|
73
77
|
|
|
@@ -82,7 +86,7 @@ await client.init({ baseUrl: 'https://api.example.com/api/v1' })
|
|
|
82
86
|
const result = await client.iam.auth.login({ identifier: 'alice', password: 'secret' })
|
|
83
87
|
```
|
|
84
88
|
|
|
85
|
-
|
|
89
|
+
### API 一覧
|
|
86
90
|
|
|
87
91
|
- `apiClient.init(config)`:既定のクライアントを初期化。
|
|
88
92
|
- `apiClient.close()`:クライアントの状態を解放。
|
|
@@ -92,7 +96,7 @@ const result = await client.iam.auth.login({ identifier: 'alice', password: 'sec
|
|
|
92
96
|
|
|
93
97
|
初期化済みのクライアントで `init()` を再度呼ぶと `CONFIG_ERROR` が返り、既存のクライアントは維持されます。`baseUrl` / `fetch` / `transport` を変更する場合は、先に `await apiClient.close()` を呼んでから再初期化してください。
|
|
94
98
|
|
|
95
|
-
|
|
99
|
+
### 設定
|
|
96
100
|
|
|
97
101
|
- `baseUrl`:API のベース URL。通常は `/api/v1` を含みます。
|
|
98
102
|
- `auth.storage`:Token ストレージアダプター。既定は `apiClient.tokenStorage.httpOnlyCookie()`(ブラウザー推奨)。SSR / テストでは `apiClient.tokenStorage.memory()` を明示してください。
|
|
@@ -104,7 +108,7 @@ const result = await client.iam.auth.login({ identifier: 'alice', password: 'sec
|
|
|
104
108
|
- `transport.keyExchangePath`:鍵交換のサブパス。既定は `/_hai/key-exchange` で、`baseUrl` に自動的に連結されます。
|
|
105
109
|
- `apiClient.config`:**機密情報をマスクした実行時設定のスナップショット**。`baseUrl` 内の認証情報や `headers.authorization` などの機密値は自動的に非表示になります。
|
|
106
110
|
|
|
107
|
-
|
|
111
|
+
### 設定による認証画面
|
|
108
112
|
|
|
109
113
|
規約は設定の userAgreementMarkdown/privacyPolicyMarkdown または URL を使用できます。同じ規約は Markdown 本文を優先して安全な Modal で描画します。
|
|
110
114
|
|
|
@@ -112,7 +116,7 @@ const result = await client.iam.auth.login({ identifier: 'alice', password: 'sec
|
|
|
112
116
|
|
|
113
117
|
[完整 IAM 配置 / IAM configuration](../iam/README.ja.md)
|
|
114
118
|
|
|
115
|
-
|
|
119
|
+
### エラー処理
|
|
116
120
|
|
|
117
121
|
業務 API は `HaiResult<T>` を返します:
|
|
118
122
|
|
|
@@ -130,13 +134,13 @@ else {
|
|
|
130
134
|
|
|
131
135
|
自動更新は実際の HTTP 401(`Response.status`)、または HTTP 200 の有効な失敗 HaiResult(`success=false`、文字列の `error.code/message`、`error.httpStatus=401`)で動作します。更新が成功すると元のリクエストを一度だけ再試行します。組み込みのログイン・登録と設定された `auth.refreshPath` は自動更新の対象外で、403/500 など他の失敗でも更新しません。serv の通常の procedure は引き続き HTTP 200 を返します。呼び出し側は最終結果の `success` と `error.code` を確認し、更新失敗による認証失効を処理してください。
|
|
132
136
|
|
|
133
|
-
##
|
|
137
|
+
## 開発とテスト
|
|
134
138
|
|
|
135
139
|
```bash
|
|
136
140
|
pnpm --filter @h-ai/api-client test
|
|
137
141
|
pnpm --filter @h-ai/api-client typecheck
|
|
138
142
|
```
|
|
139
143
|
|
|
140
|
-
##
|
|
144
|
+
## ライセンス
|
|
141
145
|
|
|
142
146
|
Apache-2.0
|
package/README.md
CHANGED
|
@@ -4,9 +4,11 @@
|
|
|
4
4
|
|
|
5
5
|
[框架首页](../../README.md) · 相关模块:[@h-ai/api-contract](../api-contract/README.md) · [@h-ai/core](../core/README.md) · [@h-ai/crypto](../crypto/README.md)
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## 简介
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
hai-framework 的跨端 oRPC/OpenAPI typed client,面向 Web、App、小程序等运行时提供统一 API 调用与 Token 管理。
|
|
10
|
+
|
|
11
|
+
## 核心功能清单
|
|
10
12
|
|
|
11
13
|
- `apiClient`:默认绑定 iam/storage/ai 领域的统一 typed client 入口。
|
|
12
14
|
- `apiClient.create(contract)`:为自定义 contract 创建 typed client。
|
|
@@ -17,7 +19,9 @@
|
|
|
17
19
|
- 可选传输加密:`apiClient.init({ transport: { crypto } })` 自动使用 `crypto.transport.createClient()`。
|
|
18
20
|
- 传输加密与自动刷新可同时启用:401 后的 `/auth/refresh` 会继续走 encrypted fetch,不降级明文。
|
|
19
21
|
|
|
20
|
-
##
|
|
22
|
+
## 使用入门
|
|
23
|
+
|
|
24
|
+
### 快速开始
|
|
21
25
|
|
|
22
26
|
```ts
|
|
23
27
|
import { apiClient } from '@h-ai/api-client'
|
|
@@ -47,7 +51,7 @@ if (me.success) {
|
|
|
47
51
|
await apiClient.close()
|
|
48
52
|
```
|
|
49
53
|
|
|
50
|
-
|
|
54
|
+
#### 启用传输加密
|
|
51
55
|
|
|
52
56
|
服务端需先启用 `serv.createApp({ transport: { crypto } })`;客户端只注入同一个 `@h-ai/crypto` 服务实例,无需手写密钥协商代码。
|
|
53
57
|
|
|
@@ -67,7 +71,7 @@ await apiClient.close()
|
|
|
67
71
|
await crypto.close()
|
|
68
72
|
```
|
|
69
73
|
|
|
70
|
-
|
|
74
|
+
### API 契约
|
|
71
75
|
|
|
72
76
|
自定义应用可以绑定自己的 oRPC contract:
|
|
73
77
|
|
|
@@ -82,7 +86,7 @@ await client.init({ baseUrl: 'https://api.example.com/api/v1' })
|
|
|
82
86
|
const result = await client.iam.auth.login({ identifier: 'alice', password: 'secret' })
|
|
83
87
|
```
|
|
84
88
|
|
|
85
|
-
|
|
89
|
+
### API 概览
|
|
86
90
|
|
|
87
91
|
- `apiClient.init(config)`:初始化默认 client。
|
|
88
92
|
- `apiClient.close()`:清理 client 状态。
|
|
@@ -92,7 +96,7 @@ const result = await client.iam.auth.login({ identifier: 'alice', password: 'sec
|
|
|
92
96
|
|
|
93
97
|
已初始化后再次调用 `init()` 会返回 `CONFIG_ERROR`,不会覆盖现有 client;如需切换 `baseUrl` / `fetch` / `transport`,请先 `await apiClient.close()` 再重新初始化。
|
|
94
98
|
|
|
95
|
-
|
|
99
|
+
### 配置
|
|
96
100
|
|
|
97
101
|
- `baseUrl`:API 基础地址,通常包含 `/api/v1`。
|
|
98
102
|
- `auth.storage`:Token 存储适配器;默认 `apiClient.tokenStorage.httpOnlyCookie()`(浏览器推荐);SSR / 测试场景请显式传入 `apiClient.tokenStorage.memory()`。
|
|
@@ -104,7 +108,7 @@ const result = await client.iam.auth.login({ identifier: 'alice', password: 'sec
|
|
|
104
108
|
- `transport.keyExchangePath`:密钥协商子路径,默认 `/_hai/key-exchange`;会自动拼接到 `baseUrl` 后。
|
|
105
109
|
- `apiClient.config`:返回**脱敏后的运行配置快照**;`baseUrl` 的内嵌凭证、`headers.authorization` 等敏感值会自动隐藏。
|
|
106
110
|
|
|
107
|
-
|
|
111
|
+
### 配置驱动认证页面
|
|
108
112
|
|
|
109
113
|
协议支持配置中的 userAgreementMarkdown/privacyPolicyMarkdown 正文及 URL;同主题优先 Markdown,由安全渲染器在弹窗显示。
|
|
110
114
|
|
|
@@ -112,7 +116,7 @@ const result = await client.iam.auth.login({ identifier: 'alice', password: 'sec
|
|
|
112
116
|
|
|
113
117
|
[完整 IAM 配置 / IAM configuration](../iam/README.md)
|
|
114
118
|
|
|
115
|
-
|
|
119
|
+
### 错误处理
|
|
116
120
|
|
|
117
121
|
业务 API 返回 `HaiResult<T>`:
|
|
118
122
|
|
|
@@ -130,13 +134,13 @@ else {
|
|
|
130
134
|
|
|
131
135
|
自动刷新在真实 HTTP 401(`Response.status`)或 HTTP 200 的有效失败 HaiResult(`success=false`、`error.code/message` 为字符串且 `error.httpStatus=401`)后触发,刷新成功后只重试一次原请求。内置登录、注册与配置的 `auth.refreshPath` 不触发自动刷新,403/500 等其它失败也不会触发。serv 普通 procedure 的 HTTP 状态仍为 200;调用方须检查最终结果的 `success` 与 `error.code`,并处理刷新失败后的登录失效。
|
|
132
136
|
|
|
133
|
-
##
|
|
137
|
+
## 开发测试
|
|
134
138
|
|
|
135
139
|
```bash
|
|
136
140
|
pnpm --filter @h-ai/api-client test
|
|
137
141
|
pnpm --filter @h-ai/api-client typecheck
|
|
138
142
|
```
|
|
139
143
|
|
|
140
|
-
##
|
|
144
|
+
## 许可证
|
|
141
145
|
|
|
142
146
|
Apache-2.0
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@h-ai/api-client",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.0-alpha.
|
|
4
|
+
"version": "0.1.0-alpha.67",
|
|
5
5
|
"description": "Hai Framework universal HTTP client with Bearer token and API contract support.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"exports": {
|
|
@@ -23,9 +23,9 @@
|
|
|
23
23
|
"node": "^22.22.2 || ^24.15.0 || >=26.0.0"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@h-ai/api-contract": "0.1.0-alpha.
|
|
27
|
-
"@h-ai/core": "0.1.0-alpha.
|
|
28
|
-
"@h-ai/crypto": "0.1.0-alpha.
|
|
26
|
+
"@h-ai/api-contract": "0.1.0-alpha.67",
|
|
27
|
+
"@h-ai/core": "0.1.0-alpha.67",
|
|
28
|
+
"@h-ai/crypto": "0.1.0-alpha.67",
|
|
29
29
|
"@orpc/client": "^1.15.5",
|
|
30
30
|
"@orpc/contract": "^1.15.5",
|
|
31
31
|
"@orpc/openapi-client": "^1.15.5",
|