api2key-base-sdk 0.1.2 → 0.1.4

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.md CHANGED
@@ -1,44 +1,42 @@
1
1
  # api2key-base-sdk
2
2
 
3
- `api2key-base-sdk` 现在只承载稳定的基础平台能力,不再包含项目业务 API 或后台管理 API。
3
+ `api2key-base-sdk` 是当前唯一保留的 SDK 入口,统一承载基础平台能力、项目运行时能力和后台管理客户端。
4
4
 
5
5
  适合放进所有新项目作为默认基础依赖。
6
6
 
7
7
  ## 当前边界
8
8
 
9
- 基础能力仅包括:
9
+ 当前统一暴露:
10
10
 
11
11
  1. `auth`
12
12
  2. `credits`
13
13
  3. `apiKeys`
14
14
  4. `settings`
15
-
16
- 不再包含:
17
-
18
- 1. 项目商品与项目会员接口
19
- 2. 订单与支付接口
20
- 3. AI 业务接口
21
- 4. 管理后台接口
22
-
23
- 这些能力已拆分为独立包:
24
-
25
- 1. `api2key-project-sdk`
26
- 2. `api2key-admin-sdk`
15
+ 5. `projects`
16
+ 6. `orders`
17
+ 7. `payment`
18
+ 8. `ai`
19
+ 9. `admin`
27
20
 
28
21
  安装原则:
29
22
 
30
23
  1. 所有新项目默认只安装 `api2key-base-sdk`
31
- 2. 只有需要业务扩展能力时才安装 `api2key-project-sdk`
32
- 3. 只有需要开发管理后台时才安装 `api2key-admin-sdk`
24
+ 2. 后台项目也只安装 `api2key-base-sdk`
25
+ 3. 单项目私有逻辑优先留在本地 facade,而不是再拆新的 SDK 包
33
26
 
34
27
  ## 暴露的客户端
35
28
 
36
- 当前 `api2key-base-sdk` 只暴露以下客户端:
29
+ 当前 `api2key-base-sdk` 暴露以下客户端:
37
30
 
38
31
  1. `auth`
39
32
  2. `credits`
40
33
  3. `apiKeys`
41
34
  4. `settings`
35
+ 5. `projects`
36
+ 6. `orders`
37
+ 7. `payment`
38
+ 8. `ai`
39
+ 9. `admin`
42
40
 
43
41
  ### 1. `auth`
44
42
 
@@ -53,12 +51,12 @@
53
51
 
54
52
  ### 2. `credits`
55
53
 
56
- 负责稳定的积分账户与流水能力。
54
+ 负责稳定的积分账户与服务侧扣减能力。
57
55
 
58
56
  当前主要方法包括:
59
57
 
60
58
  1. `getBalance(accessToken?)`
61
- 2. `getLedger(input)`
59
+ 2. `getLedger(input)`(仅管理员账号可查询当前账户积分消耗流水,普通用户前台不应依赖)
62
60
  3. `spend(input)`
63
61
  4. `reserve(input)`
64
62
  5. `confirm(id)`
@@ -72,6 +70,14 @@
72
70
 
73
71
  负责用户级设置的读取与更新。
74
72
 
73
+ ### 5. `projects / orders / payment / ai`
74
+
75
+ 负责项目商品、会员、订单、支付、AI 等运行时能力。
76
+
77
+ ### 6. `admin`
78
+
79
+ 负责项目管理、产品管理、用户管理、会员管理、模型管理、后台积分操作等控制面能力。
80
+
75
81
  ## 适合哪些项目
76
82
 
77
83
  适合:
@@ -80,13 +86,7 @@
80
86
  2. 只需要登录、积分、用户设置、API Key 的项目
81
87
  3. 需要一个长期稳定基础依赖的项目
82
88
 
83
- 不适合单独承载:
84
-
85
- 1. 商品目录
86
- 2. 会员业务
87
- 3. 订单与支付业务
88
- 4. AI 业务策略
89
- 5. 管理后台接口
89
+ 适合同一个 SDK 内统一暴露所有共享客户端,但不适合把某个单项目专属兼容逻辑强行塞进共享包。
90
90
 
91
91
  ## 安装
92
92
 
@@ -107,14 +107,27 @@ const baseClient = createBasePlatformClient({
107
107
 
108
108
  const me = await baseClient.auth.me();
109
109
  const credits = await baseClient.credits.getBalance();
110
- const settings = await baseClient.settings.get();
110
+
111
+ const projectClient = createProjectPlatformClient({
112
+ baseUrl: process.env.API2KEY_BASE_URL!,
113
+ getAccessToken: () => process.env.ACCESS_TOKEN,
114
+ getProjectId: () => process.env.PROJECT_ID,
115
+ });
116
+
117
+ const adminClient = createAdminPlatformClient({
118
+ baseUrl: process.env.API2KEY_BASE_URL!,
119
+ getAccessToken: () => process.env.ADMIN_ACCESS_TOKEN,
120
+ });
121
+
122
+ const products = await projectClient.projects.getCatalogProducts(process.env.PROJECT_ID!);
123
+ const projects = await adminClient.admin.listProjects();
111
124
  ```
112
125
 
113
126
  ## 设计原则
114
127
 
115
128
  1. 新项目默认只依赖这个包
116
- 2. 业务扩展能力按需安装独立 SDK
117
- 3. `api2key-base-api` 可以稳定演进而不被业务模块拖住
129
+ 2. 共享客户端统一收敛在一个包里
130
+ 3. `api2key-base-api` 可以继续保持清晰的路由分层,而不需要在消费侧安装多个 SDK
118
131
 
119
132
  ## Agent 约定
120
133
 
@@ -122,15 +135,16 @@ const settings = await baseClient.settings.get();
122
135
 
123
136
  规则:
124
137
 
125
- 1. 如果需求属于认证、积分、用户设置、API Key,优先在这里找
126
- 2. 不要把商品、会员、订单、支付、AI、后台管理方法加回这个包
127
- 3. 如果新需求明显带有项目业务语义,应优先落到 `api2key-project-sdk`
128
- 4. 如果新需求明显带有后台控制面语义,应优先落到 `api2key-admin-sdk`
138
+ 1. 如果需求属于共享客户端能力,优先在这里找
139
+ 2. 后端路由边界仍按 base/project/admin 区分,不要因为 SDK 合并而改乱 API 归属
140
+ 3. 如果新需求只服务单个项目,优先留在项目本地 facade
129
141
 
130
142
  ## 兼容说明
131
143
 
132
- 当前仍保留 `createPlatformAClient` 作为过渡别名,但它现在只返回基础能力客户端。
144
+ 当前仍保留 `createPlatformAClient` 作为过渡别名,它返回基础能力客户端。
133
145
 
134
146
  建议新代码统一使用:
135
147
 
136
- - `createBasePlatformClient`
148
+ - `createBasePlatformClient`
149
+ - `createProjectPlatformClient`
150
+ - `createAdminPlatformClient`
@@ -1,5 +1,5 @@
1
1
  import type { PlatformHttpClient } from '../core/client';
2
- import type { AdminConfigItem, AdminMembershipFilters, AdminMembershipRecord, AdminOrderFilters, AdminOrderRecord, AdminProduct, AdminProject, AdminProjectExportFormat, AdminUserRecord } from '../core/types';
2
+ import type { AdminConfigItem, AdminMembershipFilters, AdminMembershipRecord, AdminModelInput, AdminModelRecord, AdminOrderFilters, AdminOrderRecord, AdminProduct, AdminProductBackfillResult, AdminProductListResult, AdminProject, AdminProjectExportFormat, AdminUserCreditsSummary, AdminUserRecord, UpdateAdminUserInput } from '../core/types';
3
3
  export declare class AdminClient {
4
4
  private readonly http;
5
5
  constructor(http: PlatformHttpClient);
@@ -25,10 +25,14 @@ export declare class AdminClient {
25
25
  }, accessToken?: string): Promise<null>;
26
26
  deleteProject(id: string, accessToken?: string): Promise<null>;
27
27
  exportProject(id: string, format?: AdminProjectExportFormat, accessToken?: string): Promise<unknown>;
28
- listProducts(projectId?: string, accessToken?: string): Promise<AdminProduct[]>;
28
+ listProducts(projectId?: string, accessToken?: string): Promise<AdminProductListResult>;
29
29
  createProduct(input: Omit<AdminProduct, 'id'>, accessToken?: string): Promise<AdminProduct>;
30
30
  updateProduct(input: AdminProduct, accessToken?: string): Promise<AdminProduct>;
31
31
  deleteProduct(id: string, accessToken?: string): Promise<null>;
32
+ backfillProductsProject(input: {
33
+ projectId: string;
34
+ productIds?: string[];
35
+ }, accessToken?: string): Promise<AdminProductBackfillResult>;
32
36
  listMemberships(filters?: AdminMembershipFilters, accessToken?: string): Promise<{
33
37
  memberships: AdminMembershipRecord[];
34
38
  unboundUsers: Array<{
@@ -84,6 +88,16 @@ export declare class AdminClient {
84
88
  }, accessToken?: string): Promise<{
85
89
  users: AdminUserRecord[];
86
90
  }>;
91
+ updateUser(input: UpdateAdminUserInput, accessToken?: string): Promise<{
92
+ userId: string;
93
+ name?: string | null;
94
+ status?: string;
95
+ projectId: string | null;
96
+ membershipTier?: string | null;
97
+ membershipStatus?: string | null;
98
+ membershipCredits?: number;
99
+ creditsBalance?: number;
100
+ }>;
87
101
  updateUserProject(input: {
88
102
  userId: string;
89
103
  projectId?: string | null;
@@ -112,4 +126,34 @@ export declare class AdminClient {
112
126
  key: string;
113
127
  value: string;
114
128
  }, accessToken?: string): Promise<AdminConfigItem>;
129
+ grantCredits(input: {
130
+ userId: string;
131
+ amount: number;
132
+ description?: string;
133
+ taskId?: string;
134
+ }, accessToken?: string): Promise<{
135
+ balanceAfter: number;
136
+ idempotent?: boolean;
137
+ }>;
138
+ getUserCredits(userId: string, accessToken?: string): Promise<AdminUserCreditsSummary>;
139
+ listManagedModels(accessToken?: string): Promise<{
140
+ models: AdminModelRecord[];
141
+ warning?: string | null;
142
+ schemaReady?: boolean;
143
+ }>;
144
+ createManagedModel(input: AdminModelInput, accessToken?: string): Promise<{
145
+ model: AdminModelRecord;
146
+ }>;
147
+ updateManagedModel(id: string, input: Partial<AdminModelInput>, accessToken?: string): Promise<{
148
+ model: AdminModelRecord;
149
+ }>;
150
+ deleteManagedModel(id: string, accessToken?: string): Promise<void>;
151
+ bulkDeleteManagedModels(ids: string[], accessToken?: string): Promise<{
152
+ deletedCount: number;
153
+ }>;
154
+ importManagedModels(input: AdminModelInput[], accessToken?: string): Promise<{
155
+ models: AdminModelRecord[];
156
+ count: number;
157
+ }>;
158
+ flushManagedModelCache(accessToken?: string): Promise<void>;
115
159
  }
@@ -68,6 +68,13 @@ export class AdminClient {
68
68
  accessToken,
69
69
  });
70
70
  }
71
+ backfillProductsProject(input, accessToken) {
72
+ return this.http.request('/api/v1/admin/products/backfill-project', {
73
+ method: 'POST',
74
+ body: input,
75
+ accessToken,
76
+ });
77
+ }
71
78
  listMemberships(filters = {}, accessToken) {
72
79
  return this.http.request('/api/v1/admin/memberships', {
73
80
  query: {
@@ -107,13 +114,16 @@ export class AdminClient {
107
114
  accessToken,
108
115
  });
109
116
  }
110
- updateUserProject(input, accessToken) {
117
+ updateUser(input, accessToken) {
111
118
  return this.http.request('/api/v1/admin/users', {
112
119
  method: 'PUT',
113
120
  body: input,
114
121
  accessToken,
115
122
  });
116
123
  }
124
+ updateUserProject(input, accessToken) {
125
+ return this.updateUser(input, accessToken);
126
+ }
117
127
  deleteUsers(userIds, accessToken) {
118
128
  return this.http.request('/api/v1/admin/users', {
119
129
  method: 'DELETE',
@@ -143,4 +153,59 @@ export class AdminClient {
143
153
  accessToken,
144
154
  });
145
155
  }
156
+ grantCredits(input, accessToken) {
157
+ return this.http.request('/api/v1/credits/admin/grant', {
158
+ method: 'POST',
159
+ body: input,
160
+ accessToken,
161
+ });
162
+ }
163
+ getUserCredits(userId, accessToken) {
164
+ return this.http.request(`/api/v1/credits/admin/users/${encodeURIComponent(userId)}`, {
165
+ accessToken,
166
+ });
167
+ }
168
+ listManagedModels(accessToken) {
169
+ return this.http.request('/api/v1/admin/models', { accessToken });
170
+ }
171
+ createManagedModel(input, accessToken) {
172
+ return this.http.request('/api/v1/admin/models', {
173
+ method: 'POST',
174
+ body: input,
175
+ accessToken,
176
+ });
177
+ }
178
+ updateManagedModel(id, input, accessToken) {
179
+ return this.http.request(`/api/v1/admin/models/${encodeURIComponent(id)}`, {
180
+ method: 'PUT',
181
+ body: input,
182
+ accessToken,
183
+ });
184
+ }
185
+ deleteManagedModel(id, accessToken) {
186
+ return this.http.request(`/api/v1/admin/models/${encodeURIComponent(id)}`, {
187
+ method: 'DELETE',
188
+ accessToken,
189
+ });
190
+ }
191
+ bulkDeleteManagedModels(ids, accessToken) {
192
+ return this.http.request('/api/v1/admin/models/delete', {
193
+ method: 'POST',
194
+ body: { ids },
195
+ accessToken,
196
+ });
197
+ }
198
+ importManagedModels(input, accessToken) {
199
+ return this.http.request('/api/v1/admin/models/import', {
200
+ method: 'POST',
201
+ body: input,
202
+ accessToken,
203
+ });
204
+ }
205
+ flushManagedModelCache(accessToken) {
206
+ return this.http.request('/api/v1/admin/models/cache/flush', {
207
+ method: 'POST',
208
+ accessToken,
209
+ });
210
+ }
146
211
  }
@@ -1,4 +1,4 @@
1
- import { PlatformApiError, PlatformTransportError } from './errors';
1
+ import { PlatformApiError, PlatformTransportError } from './errors.js';
2
2
  function buildQueryString(query) {
3
3
  if (!query)
4
4
  return '';
@@ -336,3 +336,63 @@ export interface AdminConfigItem {
336
336
  description?: string;
337
337
  updated_at?: number;
338
338
  }
339
+ export interface AdminUserCreditsSummary {
340
+ account: {
341
+ balance: number;
342
+ reserved: number;
343
+ total_earned: number;
344
+ total_spent: number;
345
+ };
346
+ recentLedger: CreditsLedgerItem[];
347
+ }
348
+ export interface AdminModelRecord {
349
+ id: string;
350
+ model_key: string;
351
+ model_id: string;
352
+ name: string;
353
+ provider: string;
354
+ model_type?: string | null;
355
+ min_tier?: string | null;
356
+ input_pricing: number;
357
+ output_pricing: number;
358
+ cache_ratio?: number | null;
359
+ enabled: number;
360
+ sort_order: number;
361
+ created_at?: number;
362
+ updated_at?: number;
363
+ }
364
+ export interface AdminModelInput {
365
+ model_key: string;
366
+ model_id: string;
367
+ name: string;
368
+ provider: string;
369
+ model_type?: string;
370
+ min_tier?: string;
371
+ input_pricing: number;
372
+ output_pricing: number;
373
+ cache_ratio?: number;
374
+ enabled?: number;
375
+ sort_order?: number;
376
+ }
377
+ export interface AdminProductListResult {
378
+ project: {
379
+ id: string;
380
+ name: string;
381
+ slug: string;
382
+ status: string;
383
+ } | null;
384
+ products: AdminProduct[];
385
+ }
386
+ export interface AdminProductBackfillResult {
387
+ updated: number;
388
+ }
389
+ export interface UpdateAdminUserInput {
390
+ userId: string;
391
+ name?: string | null;
392
+ status?: 'active' | 'disabled' | 'banned';
393
+ projectId?: string | null;
394
+ membershipTier?: string | null;
395
+ membershipStatus?: 'active' | 'expired' | 'cancelled';
396
+ membershipCredits?: number;
397
+ creditsBalance?: number;
398
+ }
package/dist/index.d.ts CHANGED
@@ -1,11 +1,16 @@
1
+ import { AdminClient } from './admin/client';
2
+ import { AiClient } from './ai/client';
3
+ import { AuthClient } from './auth/client';
1
4
  import { PlatformHttpClient } from './core/client';
2
5
  import type { PlatformClientOptions } from './core/types';
3
- import { AuthClient } from './auth/client';
4
6
  import { CreditsClient } from './credits/client';
7
+ import { OrdersClient } from './orders/client';
8
+ import { PaymentClient } from './payment/client';
9
+ import { ProjectsClient } from './projects/client';
5
10
  import { UserApiKeysClient } from './user/api-keys-client';
6
11
  import { UserSettingsClient } from './user/settings-client';
7
12
  export * from './core/errors';
8
- export type { AuthSession, AuthUser, CreditsBalance, CreditsLedgerItem, PlatformClientOptions, PlatformEnvelope, PlatformRequestOptions, UserApiKeyRecord, UserApiKeySecret, UserSettingDefinition, UserSettingsPayload, } from './core/types';
13
+ export type { AdminConfigItem, AdminMembershipFilters, AdminMembershipRecord, AdminModelInput, AdminModelRecord, AdminOrderFilters, AdminOrderRecord, AdminProduct, AdminProductBackfillResult, AdminProductListResult, AdminProject, AdminProjectExportFormat, AdminUserCreditsSummary, AdminUserRecord, AiModelSummary, AuthSession, AuthUser, CreditsBalance, CreditsLedgerItem, MembershipView, OrderDetail, OrderSummary, PaymentCreateResponse, PaymentQueryResponse, PlatformClientOptions, PlatformEnvelope, PlatformRequestOptions, ProductSummary, ProjectSummary, UpdateAdminUserInput, UserApiKeyRecord, UserApiKeySecret, UserSettingDefinition, UserSettingsPayload, } from './core/types';
9
14
  export declare class BasePlatformClient {
10
15
  readonly http: PlatformHttpClient;
11
16
  readonly auth: AuthClient;
@@ -15,5 +20,20 @@ export declare class BasePlatformClient {
15
20
  constructor(options: PlatformClientOptions);
16
21
  }
17
22
  export declare function createBasePlatformClient(options: PlatformClientOptions): BasePlatformClient;
23
+ export declare class ProjectPlatformClient {
24
+ readonly http: PlatformHttpClient;
25
+ readonly projects: ProjectsClient;
26
+ readonly orders: OrdersClient;
27
+ readonly payment: PaymentClient;
28
+ readonly ai: AiClient;
29
+ constructor(options: PlatformClientOptions);
30
+ }
31
+ export declare function createProjectPlatformClient(options: PlatformClientOptions): ProjectPlatformClient;
32
+ export declare class AdminPlatformClient {
33
+ readonly http: PlatformHttpClient;
34
+ readonly admin: AdminClient;
35
+ constructor(options: PlatformClientOptions);
36
+ }
37
+ export declare function createAdminPlatformClient(options: PlatformClientOptions): AdminPlatformClient;
18
38
  export { BasePlatformClient as PlatformAClient };
19
39
  export declare const createPlatformAClient: typeof createBasePlatformClient;
package/dist/index.js CHANGED
@@ -1,9 +1,14 @@
1
- import { PlatformHttpClient } from './core/client';
2
- import { AuthClient } from './auth/client';
3
- import { CreditsClient } from './credits/client';
4
- import { UserApiKeysClient } from './user/api-keys-client';
5
- import { UserSettingsClient } from './user/settings-client';
6
- export * from './core/errors';
1
+ import { AdminClient } from './admin/client.js';
2
+ import { AiClient } from './ai/client.js';
3
+ import { AuthClient } from './auth/client.js';
4
+ import { PlatformHttpClient } from './core/client.js';
5
+ import { CreditsClient } from './credits/client.js';
6
+ import { OrdersClient } from './orders/client.js';
7
+ import { PaymentClient } from './payment/client.js';
8
+ import { ProjectsClient } from './projects/client.js';
9
+ import { UserApiKeysClient } from './user/api-keys-client.js';
10
+ import { UserSettingsClient } from './user/settings-client.js';
11
+ export * from './core/errors.js';
7
12
  export class BasePlatformClient {
8
13
  constructor(options) {
9
14
  this.http = new PlatformHttpClient(options);
@@ -16,5 +21,26 @@ export class BasePlatformClient {
16
21
  export function createBasePlatformClient(options) {
17
22
  return new BasePlatformClient(options);
18
23
  }
24
+ export class ProjectPlatformClient {
25
+ constructor(options) {
26
+ this.http = new PlatformHttpClient(options);
27
+ this.projects = new ProjectsClient(this.http);
28
+ this.orders = new OrdersClient(this.http);
29
+ this.payment = new PaymentClient(this.http);
30
+ this.ai = new AiClient(this.http);
31
+ }
32
+ }
33
+ export function createProjectPlatformClient(options) {
34
+ return new ProjectPlatformClient(options);
35
+ }
36
+ export class AdminPlatformClient {
37
+ constructor(options) {
38
+ this.http = new PlatformHttpClient(options);
39
+ this.admin = new AdminClient(this.http);
40
+ }
41
+ }
42
+ export function createAdminPlatformClient(options) {
43
+ return new AdminPlatformClient(options);
44
+ }
19
45
  export { BasePlatformClient as PlatformAClient };
20
46
  export const createPlatformAClient = createBasePlatformClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "api2key-base-sdk",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "TypeScript SDK for consuming the Api2Key API stable APIs",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",
@@ -35,7 +35,7 @@
35
35
  "dist"
36
36
  ],
37
37
  "scripts": {
38
- "build": "tsc -p tsconfig.build.json",
38
+ "build": "tsc -p tsconfig.build.json && node ./scripts/fix-esm-imports.mjs",
39
39
  "clean": "rm -rf dist",
40
40
  "pack:check": "npm pack --dry-run",
41
41
  "release:check": "npm run clean && npm run type-check && npm run build && npm run pack:check",