@lovrabet/sdk 1.1.5 → 1.1.7-beta.0
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 +7 -7
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -1
- package/dist/src/auth/auth-manager.d.ts +1 -2
- package/dist/src/auth/index.d.ts +3 -0
- package/dist/src/auth/openapi-auth.d.ts +3 -2
- package/dist/src/client/client.d.ts +10 -1
- package/dist/src/config/constants.d.ts +1 -1
- package/dist/src/http/http-client.d.ts +5 -5
- package/dist/src/models/abstract-base-model.d.ts +26 -0
- package/dist/src/models/index.d.ts +6 -0
- package/dist/src/models/model-factory.d.ts +20 -0
- package/dist/src/models/model-manager.d.ts +3 -4
- package/dist/src/models/openapi-model.d.ts +15 -0
- package/dist/src/models/webapi-model.d.ts +15 -0
- package/dist/src/types/index.d.ts +38 -19
- package/package.json +16 -2
- package/dist/src/auth/user-auth.d.ts +0 -8
- package/dist/src/models/base-model.d.ts +0 -18
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import type { ClientConfig, AuthHeaders, AuthManager as IAuthManager } from '../types';
|
|
2
2
|
export declare class AuthManager implements IAuthManager {
|
|
3
3
|
private config;
|
|
4
|
-
private userAuth?;
|
|
5
4
|
private openApiAuth?;
|
|
6
5
|
private cookieAuth?;
|
|
7
6
|
constructor(config: ClientConfig);
|
|
8
7
|
private initAuthMethods;
|
|
9
|
-
getAuthHeaders(): Promise<AuthHeaders>;
|
|
8
|
+
getAuthHeaders(appCode?: string, datasetCode?: string): Promise<AuthHeaders>;
|
|
10
9
|
validateAuth(): Promise<boolean>;
|
|
11
10
|
/**
|
|
12
11
|
* Check if we're using cookie-based authentication
|
|
@@ -2,6 +2,7 @@ import type { OpenApiAuthConfig, AuthHeaders } from '../types';
|
|
|
2
2
|
export declare class OpenApiAuth {
|
|
3
3
|
private config;
|
|
4
4
|
constructor(config: OpenApiAuthConfig);
|
|
5
|
-
getAuthHeaders(): Promise<AuthHeaders>;
|
|
6
|
-
private
|
|
5
|
+
getAuthHeaders(appCode: string, datasetCode: string): Promise<AuthHeaders>;
|
|
6
|
+
private generateToken;
|
|
7
|
+
private hmacSha256;
|
|
7
8
|
}
|
|
@@ -73,5 +73,14 @@ export declare class LovrabetClient implements ILovrabetClient {
|
|
|
73
73
|
* 2. 支持按索引快速获取第一个模型,便于演示和快速开发
|
|
74
74
|
* 3. 作为 models.xxx 语法糖的底层实现
|
|
75
75
|
*/
|
|
76
|
-
getModel(indexOrName: number | string): import("../
|
|
76
|
+
getModel(indexOrName: number | string): import("../types").BaseModelMethods;
|
|
77
|
+
/**
|
|
78
|
+
* 检查当前是否为 OpenAPI 模式
|
|
79
|
+
* 为什么提供这个方法:
|
|
80
|
+
* 1. 让用户能够了解当前使用的认证和API模式
|
|
81
|
+
* 2. 支持根据模式执行不同的业务逻辑
|
|
82
|
+
* 3. 便于调试和排查问题
|
|
83
|
+
* 4. 与 BaseModel 的逻辑保持一致
|
|
84
|
+
*/
|
|
85
|
+
isOpenApiMode(): boolean;
|
|
77
86
|
}
|
|
@@ -18,9 +18,9 @@ export declare class HttpClient {
|
|
|
18
18
|
* 通用的fetch请求方法(用于PUT/DELETE等没有专门优化函数的方法)
|
|
19
19
|
*/
|
|
20
20
|
private makeRequest;
|
|
21
|
-
get<T = any>(url: string, params?: Record<string, any>, options?: RequestInit): Promise<T>;
|
|
22
|
-
post<T = any>(url: string, data?: any, options?: RequestInit): Promise<T>;
|
|
23
|
-
put<T = any>(url: string, data?: any, options?: RequestInit): Promise<T>;
|
|
24
|
-
delete<T = any>(url: string, options?: RequestInit): Promise<T>;
|
|
25
|
-
request<T = any>(method: 'GET' | 'POST' | 'PUT' | 'DELETE', url: string, data?: any, options?: RequestInit): Promise<T>;
|
|
21
|
+
get<T = any>(url: string, params?: Record<string, any>, options?: RequestInit, datasetCode?: string): Promise<T>;
|
|
22
|
+
post<T = any>(url: string, data?: any, options?: RequestInit, datasetCode?: string): Promise<T>;
|
|
23
|
+
put<T = any>(url: string, data?: any, options?: RequestInit, datasetCode?: string): Promise<T>;
|
|
24
|
+
delete<T = any>(url: string, options?: RequestInit, datasetCode?: string): Promise<T>;
|
|
25
|
+
request<T = any>(method: 'GET' | 'POST' | 'PUT' | 'DELETE', url: string, data?: any, options?: RequestInit, datasetCode?: string): Promise<T>;
|
|
26
26
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { BaseModelMethods, ListParams, ListResponse, ModelConfig, ClientConfig } from '../types';
|
|
2
|
+
import type { HttpClient } from '../http/http-client';
|
|
3
|
+
/**
|
|
4
|
+
* 抽象基础模型类
|
|
5
|
+
* 定义所有模型的通用接口和基础实现
|
|
6
|
+
*/
|
|
7
|
+
export declare abstract class AbstractBaseModel implements BaseModelMethods {
|
|
8
|
+
protected modelName: string;
|
|
9
|
+
protected httpClient: HttpClient;
|
|
10
|
+
protected config: ModelConfig;
|
|
11
|
+
protected appCode: string;
|
|
12
|
+
protected globalConfig: ClientConfig;
|
|
13
|
+
constructor(modelName: string, httpClient: HttpClient, globalConfig: ClientConfig);
|
|
14
|
+
private resolveModelConfig;
|
|
15
|
+
protected abstract getApiPath(method: string): string;
|
|
16
|
+
protected abstract buildRequestBody(method: string, data: any): any;
|
|
17
|
+
getList<T = any>(params?: ListParams): Promise<ListResponse<T>>;
|
|
18
|
+
getOne<T = any>(id: string | number): Promise<T>;
|
|
19
|
+
create<T = any>(data: Record<string, any>): Promise<T>;
|
|
20
|
+
update<T = any>(id: string | number, data: Record<string, any>): Promise<T>;
|
|
21
|
+
delete(id: string | number): Promise<void>;
|
|
22
|
+
getDatasetList<T = any>(): Promise<T[]>;
|
|
23
|
+
private isOpenApiMode;
|
|
24
|
+
getConfig(): ModelConfig;
|
|
25
|
+
getModelName(): string;
|
|
26
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { AbstractBaseModel } from './abstract-base-model';
|
|
2
|
+
export { OpenApiModel } from './openapi-model';
|
|
3
|
+
export { WebApiModel } from './webapi-model';
|
|
4
|
+
export { ModelFactory } from './model-factory';
|
|
5
|
+
export { ModelManager } from './model-manager';
|
|
6
|
+
export type { BaseModelMethods, ModelManager as IModelManager } from '../types';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { BaseModelMethods, ClientConfig } from '../types';
|
|
2
|
+
import type { HttpClient } from '../http/http-client';
|
|
3
|
+
/**
|
|
4
|
+
* 模型工厂类
|
|
5
|
+
* 根据配置决定创建 OpenAPI 或 WebAPI 模型实例
|
|
6
|
+
*/
|
|
7
|
+
export declare class ModelFactory {
|
|
8
|
+
/**
|
|
9
|
+
* 创建模型实例
|
|
10
|
+
*/
|
|
11
|
+
static createModel(modelName: string, httpClient: HttpClient, globalConfig: ClientConfig): BaseModelMethods;
|
|
12
|
+
/**
|
|
13
|
+
* 判断是否为 OpenAPI 模式
|
|
14
|
+
*/
|
|
15
|
+
private static isOpenApiMode;
|
|
16
|
+
/**
|
|
17
|
+
* 获取当前模式名称(用于调试)
|
|
18
|
+
*/
|
|
19
|
+
static getModeName(config: ClientConfig): 'OpenAPI' | 'WebAPI';
|
|
20
|
+
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { ModelManager as IModelManager, ClientConfig } from '../types';
|
|
1
|
+
import type { ModelManager as IModelManager, ClientConfig, BaseModelMethods } from '../types';
|
|
2
2
|
import type { HttpClient } from '../http/http-client';
|
|
3
|
-
import { BaseModel } from './base-model';
|
|
4
3
|
export declare class ModelManager implements IModelManager {
|
|
5
4
|
private httpClient;
|
|
6
5
|
private config;
|
|
@@ -11,8 +10,8 @@ export declare class ModelManager implements IModelManager {
|
|
|
11
10
|
clearCache(): void;
|
|
12
11
|
addModel(modelName: string, config: {
|
|
13
12
|
tableName: string;
|
|
14
|
-
|
|
13
|
+
datasetCode: string;
|
|
15
14
|
}): void;
|
|
16
15
|
list(): string[];
|
|
17
|
-
get(indexOrName: number | string):
|
|
16
|
+
get(indexOrName: number | string): BaseModelMethods;
|
|
18
17
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AbstractBaseModel } from './abstract-base-model';
|
|
2
|
+
/**
|
|
3
|
+
* OpenAPI 模型实现
|
|
4
|
+
* 处理 OpenAPI 模式下的 URL 生成和请求体构建
|
|
5
|
+
*/
|
|
6
|
+
export declare class OpenApiModel extends AbstractBaseModel {
|
|
7
|
+
/**
|
|
8
|
+
* OpenAPI 模式的 URL 生成策略
|
|
9
|
+
*/
|
|
10
|
+
protected getApiPath(sdkMethod: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* OpenAPI 模式的请求体构建策略
|
|
13
|
+
*/
|
|
14
|
+
protected buildRequestBody(method: string, data: any): any;
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AbstractBaseModel } from './abstract-base-model';
|
|
2
|
+
/**
|
|
3
|
+
* WebAPI 模型实现
|
|
4
|
+
* 处理 WebAPI 模式下的 URL 生成和请求体构建
|
|
5
|
+
*/
|
|
6
|
+
export declare class WebApiModel extends AbstractBaseModel {
|
|
7
|
+
/**
|
|
8
|
+
* WebAPI 模式的 URL 生成策略
|
|
9
|
+
*/
|
|
10
|
+
protected getApiPath(sdkMethod: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* WebAPI 模式的请求体构建策略
|
|
13
|
+
*/
|
|
14
|
+
protected buildRequestBody(method: string, data: any): any;
|
|
15
|
+
}
|
|
@@ -47,8 +47,8 @@ export interface ListResponse<T> {
|
|
|
47
47
|
export interface ModelConfig {
|
|
48
48
|
/** 数据表名称 */
|
|
49
49
|
tableName: string;
|
|
50
|
-
/**
|
|
51
|
-
|
|
50
|
+
/** 数据集代码 */
|
|
51
|
+
datasetCode: string;
|
|
52
52
|
}
|
|
53
53
|
/**
|
|
54
54
|
* 模型配置集合接口
|
|
@@ -58,8 +58,8 @@ export interface ModelConfig {
|
|
|
58
58
|
* const config: ModelsConfig = {
|
|
59
59
|
* appCode: 'my-app',
|
|
60
60
|
* models: {
|
|
61
|
-
* Users: { tableName: 'users',
|
|
62
|
-
* Posts: { tableName: 'posts',
|
|
61
|
+
* Users: { tableName: 'users', datasetCode: 'user-dataset-code' },
|
|
62
|
+
* Posts: { tableName: 'posts', datasetCode: 'post-dataset-code' }
|
|
63
63
|
* }
|
|
64
64
|
* };
|
|
65
65
|
* ```
|
|
@@ -173,6 +173,13 @@ export interface BaseModelMethods {
|
|
|
173
173
|
* @returns Promise<void>
|
|
174
174
|
*/
|
|
175
175
|
delete(id: string | number): Promise<void>;
|
|
176
|
+
/**
|
|
177
|
+
* 获取数据集列表
|
|
178
|
+
* @template T 返回数据的类型
|
|
179
|
+
* @returns 返回数据集列表
|
|
180
|
+
* @description 获取当前应用下的所有数据集列表,主要用于 OpenAPI 模式
|
|
181
|
+
*/
|
|
182
|
+
getDatasetList<T = any>(): Promise<T[]>;
|
|
176
183
|
}
|
|
177
184
|
/**
|
|
178
185
|
* 模型管理器接口
|
|
@@ -206,7 +213,7 @@ export interface ModelManager {
|
|
|
206
213
|
*/
|
|
207
214
|
addModel(modelName: string, config: {
|
|
208
215
|
tableName: string;
|
|
209
|
-
|
|
216
|
+
datasetCode: string;
|
|
210
217
|
}): void;
|
|
211
218
|
/**
|
|
212
219
|
* 获取所有可用模型列表
|
|
@@ -248,23 +255,19 @@ export interface AuthHeaders {
|
|
|
248
255
|
/** 请求头键值对 */
|
|
249
256
|
[key: string]: string;
|
|
250
257
|
}
|
|
251
|
-
/**
|
|
252
|
-
* 用户 Token 认证配置
|
|
253
|
-
* @description 用于 Bearer Token 认证方式
|
|
254
|
-
*/
|
|
255
|
-
export interface UserAuthConfig {
|
|
256
|
-
/** 用户认证 Token */
|
|
257
|
-
token: string;
|
|
258
|
-
}
|
|
259
258
|
/**
|
|
260
259
|
* OpenAPI 密钥认证配置
|
|
261
|
-
* @description
|
|
260
|
+
* @description 用于签名认证方式,支持两种模式:
|
|
261
|
+
* 1. Server 端:传入 accessKey,内部生成 token
|
|
262
|
+
* 2. Client 端:直接传入预计算的 token
|
|
262
263
|
*/
|
|
263
264
|
export interface OpenApiAuthConfig {
|
|
264
|
-
/** API
|
|
265
|
-
accessKey
|
|
266
|
-
/** API
|
|
267
|
-
secretKey
|
|
265
|
+
/** API 访问密钥(Server 端使用) */
|
|
266
|
+
accessKey?: string;
|
|
267
|
+
/** API 密钥,用于计算签名(可选,有默认值) */
|
|
268
|
+
secretKey?: string;
|
|
269
|
+
/** 预计算的 token(Client 端使用,避免在浏览器暴露 accessKey) */
|
|
270
|
+
token?: string;
|
|
268
271
|
}
|
|
269
272
|
/**
|
|
270
273
|
* 认证管理器接口
|
|
@@ -284,9 +287,11 @@ export interface AuthManager {
|
|
|
284
287
|
/**
|
|
285
288
|
* 获取认证请求头
|
|
286
289
|
* @description 根据当前配置的认证方式生成对应的请求头
|
|
290
|
+
* @param appCode 应用代码(OpenAPI 认证时必需)
|
|
291
|
+
* @param datasetCode 数据集代码(OpenAPI 认证时必需)
|
|
287
292
|
* @returns 包含认证信息的请求头对象
|
|
288
293
|
*/
|
|
289
|
-
getAuthHeaders(): Promise<AuthHeaders>;
|
|
294
|
+
getAuthHeaders(appCode?: string, datasetCode?: string): Promise<AuthHeaders>;
|
|
290
295
|
/**
|
|
291
296
|
* 验证当前认证是否有效
|
|
292
297
|
* @returns 认证是否有效
|
|
@@ -390,4 +395,18 @@ export interface LovrabetClient {
|
|
|
390
395
|
* ```
|
|
391
396
|
*/
|
|
392
397
|
getModel(indexOrName: number | string): BaseModelMethods;
|
|
398
|
+
/**
|
|
399
|
+
* 检查当前是否为 OpenAPI 模式
|
|
400
|
+
* @returns 如果配置了 accessKey 或 token 则返回 true,否则返回 false
|
|
401
|
+
* @description OpenAPI 模式会使用不同的 URL 路径和认证方式
|
|
402
|
+
* @example
|
|
403
|
+
* ```typescript
|
|
404
|
+
* const client = createClient({ accessKey: 'ak-xxx' });
|
|
405
|
+
* console.log(client.isOpenApiMode()); // true
|
|
406
|
+
*
|
|
407
|
+
* const cookieClient = createClient({});
|
|
408
|
+
* console.log(cookieClient.isOpenApiMode()); // false
|
|
409
|
+
* ```
|
|
410
|
+
*/
|
|
411
|
+
isOpenApiMode(): boolean;
|
|
393
412
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lovrabet/sdk",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.7-beta.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -14,6 +14,16 @@
|
|
|
14
14
|
"type": "module",
|
|
15
15
|
"scripts": {
|
|
16
16
|
"build": "rm -rf dist && bun build index.ts --outdir=dist --target=browser --format=esm && javascript-obfuscator dist --output dist && npm run types",
|
|
17
|
+
"build:dev": "rm -rf dist && bun build index.ts --outdir=dist --target=browser --format=esm && npm run types",
|
|
18
|
+
"dev": "npm run build:dev",
|
|
19
|
+
"start": "npm run watch",
|
|
20
|
+
"watch": "npm run build:dev && npm run watch:files",
|
|
21
|
+
"watch:files": "chokidar \"src/**/*.ts\" \"index.ts\" -c \"npm run build:dev\"",
|
|
22
|
+
"test": "vitest",
|
|
23
|
+
"test:run": "vitest run",
|
|
24
|
+
"test:ui": "vitest --ui",
|
|
25
|
+
"test:coverage": "vitest run --coverage",
|
|
26
|
+
"test:watch": "vitest watch",
|
|
17
27
|
"types": "npx tsc --emitDeclarationOnly --declaration --outDir dist index.ts",
|
|
18
28
|
"beta-release": "bun run build && sh scripts/update-beta-version.sh && git push && git push origin --tag && bun publish",
|
|
19
29
|
"release": "bun run build && bun pm version patch && git push && git push origin --tag && bun publish"
|
|
@@ -23,7 +33,11 @@
|
|
|
23
33
|
],
|
|
24
34
|
"devDependencies": {
|
|
25
35
|
"@types/bun": "latest",
|
|
26
|
-
"javascript-obfuscator": "^4.1.1"
|
|
36
|
+
"javascript-obfuscator": "^4.1.1",
|
|
37
|
+
"chokidar-cli": "^3.0.0",
|
|
38
|
+
"vitest": "^2.1.8",
|
|
39
|
+
"@vitest/ui": "^2.1.8",
|
|
40
|
+
"c8": "^10.1.2"
|
|
27
41
|
},
|
|
28
42
|
"peerDependencies": {
|
|
29
43
|
"typescript": "^5"
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { BaseModelMethods, ListParams, ListResponse, ClientConfig, ModelConfig } from '../types';
|
|
2
|
-
import type { HttpClient } from '../http/http-client';
|
|
3
|
-
export declare class BaseModel implements BaseModelMethods {
|
|
4
|
-
private modelName;
|
|
5
|
-
private httpClient;
|
|
6
|
-
private config;
|
|
7
|
-
private appCode;
|
|
8
|
-
constructor(modelName: string, httpClient: HttpClient, globalConfig: ClientConfig);
|
|
9
|
-
private resolveModelConfig;
|
|
10
|
-
private getApiPath;
|
|
11
|
-
getList<T = any>(params?: ListParams): Promise<ListResponse<T>>;
|
|
12
|
-
getOne<T = any>(id: string | number): Promise<T>;
|
|
13
|
-
create<T = any>(data: Record<string, any>): Promise<T>;
|
|
14
|
-
update<T = any>(id: string | number, data: Record<string, any>): Promise<T>;
|
|
15
|
-
delete(id: string | number): Promise<void>;
|
|
16
|
-
getConfig(): ModelConfig;
|
|
17
|
-
getModelName(): string;
|
|
18
|
-
}
|