@lark-apaas/dataloom 0.1.4-alpha.5 → 0.1.4-alpha.7
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 +32 -24
- package/lib/DataloomClient.d.ts +7 -7
- package/lib/DataloomClient.d.ts.map +1 -1
- package/lib/DataloomClient.js +15 -6
- package/lib/index.d.ts +1 -5
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/service/index.d.ts +5 -26
- package/lib/service/index.d.ts.map +1 -1
- package/lib/service/index.js +6 -3
- package/lib/service/services/DataloomServiceBase.d.ts +17 -0
- package/lib/service/services/DataloomServiceBase.d.ts.map +1 -0
- package/lib/service/services/DataloomServiceBase.js +126 -0
- package/lib/service/services/session-service/index.d.ts +45 -0
- package/lib/service/services/session-service/index.d.ts.map +1 -0
- package/lib/service/services/session-service/index.js +137 -0
- package/lib/service/services/session-service/types.d.ts +34 -0
- package/lib/service/services/session-service/types.d.ts.map +1 -0
- package/lib/service/services/types.d.ts +12 -0
- package/lib/service/services/types.d.ts.map +1 -0
- package/lib/service/services/types.js +14 -0
- package/lib/service/services/user-service/index.d.ts +9 -0
- package/lib/service/services/user-service/index.d.ts.map +1 -0
- package/lib/service/services/user-service/index.js +62 -0
- package/lib/service/services/user-service/types.d.ts +57 -0
- package/lib/service/services/user-service/types.d.ts.map +1 -0
- package/lib/service/services/user-service/types.js +0 -0
- package/lib/service/utils/helpers.d.ts +8 -0
- package/lib/service/utils/helpers.d.ts.map +1 -0
- package/lib/service/utils/helpers.js +18 -0
- package/lib/service/utils/logger.d.ts +15 -0
- package/lib/service/utils/logger.d.ts.map +1 -0
- package/lib/service/utils/logger.js +18 -0
- package/lib/service/utils/trace-id.d.ts +45 -0
- package/lib/service/utils/trace-id.d.ts.map +1 -0
- package/lib/service/utils/trace-id.js +20 -0
- package/lib/service/utils/types.d.ts +49 -0
- package/lib/service/utils/types.d.ts.map +1 -0
- package/lib/service/utils/types.js +7 -0
- package/lib/storage/StorageClient.d.ts +3 -1
- package/lib/storage/StorageClient.d.ts.map +1 -1
- package/lib/storage/StorageClient.js +4 -6
- package/lib/storage/libs/fetch.d.ts +0 -6
- package/lib/storage/libs/fetch.d.ts.map +1 -1
- package/lib/storage/libs/fetch.js +1 -11
- package/lib/storage/libs/helpers.d.ts +0 -14
- package/lib/storage/libs/helpers.d.ts.map +1 -1
- package/lib/storage/libs/helpers.js +1 -6
- package/lib/storage/libs/types.d.ts +6 -5
- package/lib/storage/libs/types.d.ts.map +1 -1
- package/lib/storage/packages/StorageFileApi.d.ts.map +1 -1
- package/lib/storage/packages/StorageFileApi.js +14 -14
- package/lib/utils/fetch.d.ts +1 -8
- package/lib/utils/fetch.d.ts.map +1 -1
- package/lib/utils/fetch.js +8 -3
- package/lib/utils/helpers.d.ts +1 -0
- package/lib/utils/helpers.d.ts.map +1 -1
- package/lib/utils/helpers.js +4 -1
- package/lib/utils/types.d.ts +0 -10
- package/lib/utils/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/lib/service/types.d.ts +0 -113
- package/lib/service/types.d.ts.map +0 -1
- package/lib/storage/utils/csrf.d.ts +0 -11
- package/lib/storage/utils/csrf.d.ts.map +0 -1
- package/lib/storage/utils/csrf.js +0 -10
- /package/lib/service/{types.js → services/session-service/types.js} +0 -0
package/README.md
CHANGED
|
@@ -47,17 +47,18 @@ import { createClient } from '@lark-apaas/dataloom';
|
|
|
47
47
|
```ts
|
|
48
48
|
import { createClient } from '@lark-apaas/dataloom';
|
|
49
49
|
|
|
50
|
-
const client = createClient(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
const client = createClient(
|
|
51
|
+
'https://example.com',
|
|
52
|
+
'dataloom-key',
|
|
53
|
+
{
|
|
54
|
+
global: {
|
|
55
|
+
brandName: 'miaoda',
|
|
56
|
+
appId: 'app_xxx',
|
|
57
|
+
},
|
|
54
58
|
},
|
|
55
|
-
|
|
59
|
+
);
|
|
56
60
|
```
|
|
57
61
|
|
|
58
|
-
> storage 走 cookie session 鉴权(runtime endpoint 自动携带 cookie),不再需要传入
|
|
59
|
-
> `dataloomUrl` / `dataloomKey`;appId 通过 `options.global.appId` 传入即可。
|
|
60
|
-
|
|
61
62
|
创建完成后,可以通过以下入口访问能力:
|
|
62
63
|
|
|
63
64
|
- `client.service.user`
|
|
@@ -69,18 +70,21 @@ const client = createClient({
|
|
|
69
70
|
### `createClient(...)`
|
|
70
71
|
|
|
71
72
|
```ts
|
|
72
|
-
createClient(
|
|
73
|
+
createClient(
|
|
74
|
+
dataloomUrl: string,
|
|
75
|
+
dataloomKey: string,
|
|
76
|
+
options?: DataloomClientOptions,
|
|
77
|
+
)
|
|
73
78
|
```
|
|
74
79
|
|
|
75
80
|
#### 入参
|
|
76
81
|
|
|
77
82
|
| 参数 | 类型 | 必填 | 说明 |
|
|
78
83
|
| --- | --- | --- | --- |
|
|
84
|
+
| `dataloomUrl` | `string` | 是 | Dataloom 服务地址 |
|
|
85
|
+
| `dataloomKey` | `string` | 是 | 访问 Dataloom 的 key |
|
|
79
86
|
| `options` | `DataloomClientOptions` | 否 | 初始化配置 |
|
|
80
87
|
|
|
81
|
-
> storage 走 cookie session 鉴权,不再需要 `dataloomUrl` / `dataloomKey`;
|
|
82
|
-
> appId 通过 `options.global.appId` 传入。
|
|
83
|
-
|
|
84
88
|
#### 返回
|
|
85
89
|
|
|
86
90
|
| 返回值 | 类型 | 说明 |
|
|
@@ -120,20 +124,24 @@ type DataloomClientOptions = {
|
|
|
120
124
|
完整示例:
|
|
121
125
|
|
|
122
126
|
```ts
|
|
123
|
-
const client = createClient(
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
127
|
+
const client = createClient(
|
|
128
|
+
'https://example.com',
|
|
129
|
+
'dataloom-key',
|
|
130
|
+
{
|
|
131
|
+
global: {
|
|
132
|
+
brandName: 'miaoda',
|
|
133
|
+
appId: 'app_xxx',
|
|
134
|
+
headers: {
|
|
135
|
+
'x-custom-header': 'demo',
|
|
136
|
+
},
|
|
137
|
+
enableDataloomLog: false,
|
|
138
|
+
requestRateLimit: 5,
|
|
139
|
+
onError(error, instance) {
|
|
140
|
+
console.error('[dataloom error]', error, instance);
|
|
141
|
+
},
|
|
134
142
|
},
|
|
135
143
|
},
|
|
136
|
-
|
|
144
|
+
);
|
|
137
145
|
```
|
|
138
146
|
|
|
139
147
|
## Service API
|
package/lib/DataloomClient.d.ts
CHANGED
|
@@ -2,29 +2,29 @@ import RateLimiter from './RateLimiter';
|
|
|
2
2
|
import DataloomService from './service';
|
|
3
3
|
import { StorageClient } from './storage';
|
|
4
4
|
import type { DataloomClientOptions, Fetch } from './utils/types';
|
|
5
|
-
import type { OnRequestErrorType } from './
|
|
5
|
+
import type { OnRequestErrorType } from './service/utils/types';
|
|
6
6
|
/**
|
|
7
7
|
* Dataloom Client.
|
|
8
8
|
*
|
|
9
9
|
* An isomorphic Javascript client for interacting with Dataloom service and storage.
|
|
10
10
|
*/
|
|
11
11
|
export default class DataloomClient {
|
|
12
|
+
protected dataloomUrl: string;
|
|
13
|
+
protected dataloomKey: string;
|
|
12
14
|
service: DataloomService;
|
|
13
15
|
storage: StorageClient;
|
|
16
|
+
protected storageUrl: string;
|
|
14
17
|
protected fetch?: Fetch;
|
|
15
18
|
protected rateLimiter?: RateLimiter;
|
|
16
19
|
protected onRequestError: (error: OnRequestErrorType) => void;
|
|
17
20
|
protected headers: Record<string, string>;
|
|
18
21
|
/**
|
|
19
22
|
* Create a new client for use in the browser.
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* runtime endpoints and rely on cookie session authentication.
|
|
23
|
-
*
|
|
24
|
-
* @param options.global.appId The appId used to build the storage runtime path.
|
|
23
|
+
* @param dataloomUrl The unique Dataloom URL which is supplied when you create a new project.
|
|
24
|
+
* @param dataloomKey The unique Dataloom Key which is supplied when you create a new project.
|
|
25
25
|
* @param options.global.fetch A custom fetch implementation.
|
|
26
26
|
* @param options.global.headers Any additional headers to send with each network request.
|
|
27
27
|
*/
|
|
28
|
-
constructor(options?: DataloomClientOptions);
|
|
28
|
+
constructor(dataloomUrl: string, dataloomKey: string, options?: DataloomClientOptions);
|
|
29
29
|
}
|
|
30
30
|
//# sourceMappingURL=DataloomClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DataloomClient.d.ts","sourceRoot":"","sources":["../src/DataloomClient.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,MAAM,eAAe,CAAC;AACxC,OAAO,eAAe,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAK1C,OAAO,KAAK,EAAE,qBAAqB,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAClE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"DataloomClient.d.ts","sourceRoot":"","sources":["../src/DataloomClient.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,MAAM,eAAe,CAAC;AACxC,OAAO,eAAe,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAK1C,OAAO,KAAK,EAAE,qBAAqB,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAClE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAEhE;;;;GAIG;AACH,MAAM,CAAC,OAAO,OAAO,cAAc;IAmB/B,SAAS,CAAC,WAAW,EAAE,MAAM;IAC7B,SAAS,CAAC,WAAW,EAAE,MAAM;IAnB/B,OAAO,EAAE,eAAe,CAAC;IACzB,OAAO,EAAE,aAAa,CAAC;IAEvB,SAAS,CAAC,UAAU,EAAE,MAAM,CAAC;IAC7B,SAAS,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC;IACxB,SAAS,CAAC,WAAW,CAAC,EAAE,WAAW,CAAC;IACpC,SAAS,CAAC,cAAc,EAAE,CAAC,KAAK,EAAE,kBAAkB,KAAK,IAAI,CAAC;IAE9D,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE1C;;;;;;OAMG;gBAES,WAAW,EAAE,MAAM,EACnB,WAAW,EAAE,MAAM,EAC7B,OAAO,CAAC,EAAE,qBAAqB;CA0DlC"}
|
package/lib/DataloomClient.js
CHANGED
|
@@ -2,11 +2,15 @@ import RateLimiter from "./RateLimiter.js";
|
|
|
2
2
|
import service from "./service/index.js";
|
|
3
3
|
import { StorageClient } from "./storage/index.js";
|
|
4
4
|
import { DEFAULT_GLOBAL_OPTIONS } from "./utils/constants.js";
|
|
5
|
-
import {
|
|
6
|
-
import { applySettingDefaults } from "./utils/helpers.js";
|
|
5
|
+
import { fetchWithAuth } from "./utils/fetch.js";
|
|
6
|
+
import { applySettingDefaults, stripTrailingSlash } from "./utils/helpers.js";
|
|
7
7
|
import { reportDataloomException } from "./utils/slardar.js";
|
|
8
8
|
class DataloomClient {
|
|
9
|
-
constructor(options){
|
|
9
|
+
constructor(dataloomUrl, dataloomKey, options){
|
|
10
|
+
this.dataloomUrl = dataloomUrl;
|
|
11
|
+
this.dataloomKey = dataloomKey;
|
|
12
|
+
const normalizedDataloomUrl = stripTrailingSlash(dataloomUrl);
|
|
13
|
+
this.storageUrl = `${normalizedDataloomUrl}/storage`;
|
|
10
14
|
const DEFAULTS = {
|
|
11
15
|
global: DEFAULT_GLOBAL_OPTIONS
|
|
12
16
|
};
|
|
@@ -21,11 +25,16 @@ class DataloomClient {
|
|
|
21
25
|
if (settings.global.onError) settings.global.onError(error, this);
|
|
22
26
|
};
|
|
23
27
|
if (settings.global.requestRateLimit && settings.global.requestRateLimit > 0) this.rateLimiter = new RateLimiter(settings.global.requestRateLimit);
|
|
24
|
-
this.fetch =
|
|
28
|
+
this.fetch = fetchWithAuth(dataloomKey, this.rateLimiter, settings.global.fetch);
|
|
25
29
|
this.service = new service({
|
|
26
|
-
|
|
30
|
+
headers: this.headers,
|
|
31
|
+
fetch: this.fetch,
|
|
32
|
+
logger: settings.global.enableDataloomLog,
|
|
33
|
+
brandName: settings.global.brandName,
|
|
34
|
+
appId: settings.global.appId,
|
|
35
|
+
onRequestError: this.onRequestError
|
|
27
36
|
});
|
|
28
|
-
this.storage = new StorageClient(this.headers, this.fetch, {
|
|
37
|
+
this.storage = new StorageClient(this.storageUrl, this.headers, this.fetch, {
|
|
29
38
|
enableLogger: settings.global.enableDataloomLog,
|
|
30
39
|
onRequestError: this.onRequestError,
|
|
31
40
|
appId: settings.global.appId
|
package/lib/index.d.ts
CHANGED
|
@@ -4,10 +4,6 @@ export { default as DataloomClient } from './DataloomClient';
|
|
|
4
4
|
export type { DataloomClientOptions } from './utils/types';
|
|
5
5
|
/**
|
|
6
6
|
* Creates a new Dataloom Client.
|
|
7
|
-
*
|
|
8
|
-
* Storage requests are authenticated via cookie session against the
|
|
9
|
-
* `/app/:appId/__runtime__/api/v1/storage` runtime endpoints, so no
|
|
10
|
-
* dataloom url / key needs to be supplied any more.
|
|
11
7
|
*/
|
|
12
|
-
export declare const createClient: (options?: DataloomClientOptions) => DataloomClient;
|
|
8
|
+
export declare const createClient: (dataloomUrl: string, dataloomKey: string, options?: DataloomClientOptions) => DataloomClient;
|
|
13
9
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAC9C,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAE3D,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAC7D,YAAY,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAE3D
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAC9C,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAE3D,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAC7D,YAAY,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAE3D;;GAEG;AACH,eAAO,MAAM,YAAY,GACvB,aAAa,MAAM,EACnB,aAAa,MAAM,EACnB,UAAU,qBAAqB,KAC9B,cAEF,CAAC"}
|
package/lib/index.js
CHANGED
package/lib/service/index.d.ts
CHANGED
|
@@ -1,30 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
/**
|
|
6
|
-
* DataloomService —— 账号相关服务(user / session)的承载壳。
|
|
7
|
-
*
|
|
8
|
-
* 历史背景:
|
|
9
|
-
* - 早期 user/session 的实现内置在 dataloom 内部;后续抽离到 `@lark-apaas/auth-sdk`,
|
|
10
|
-
* dataloom 不再持有具体实现,只通过依赖注入(DI)拿到 authClient.user / authClient.session。
|
|
11
|
-
*
|
|
12
|
-
* 注入路径:
|
|
13
|
-
* client-toolkit / client-toolkit-lite
|
|
14
|
-
* → createClient({ global: { accountServices: { user, session } } })
|
|
15
|
-
* → new DataloomClient(...)
|
|
16
|
-
* → new DataloomService({ accountServices })
|
|
17
|
-
*
|
|
18
|
-
* 设计要点:
|
|
19
|
-
* - 依赖方向单向:dataloom 不 import auth-sdk,只声明 `*ServiceShape` 形状契约。
|
|
20
|
-
* - 未注入时不抛错:把字段降级为空对象占位,让存在性检查(如 `if (service.user)`)
|
|
21
|
-
* 与对象探测保持中性;具体方法是否可用由调用方在使用点感知(拿到 undefined / TypeError)。
|
|
22
|
-
* - 仅作"中转字段",不做任何运行时逻辑封装。
|
|
23
|
-
*/
|
|
1
|
+
import { default as SessionService } from './services/session-service';
|
|
2
|
+
import { default as UserService } from './services/user-service';
|
|
3
|
+
import type { DataloomServiceOptions } from './utils/types';
|
|
24
4
|
export default class DataloomService {
|
|
25
|
-
|
|
26
|
-
|
|
5
|
+
user: UserService;
|
|
6
|
+
session: SessionService;
|
|
27
7
|
constructor(options: DataloomServiceOptions);
|
|
28
8
|
}
|
|
29
|
-
export type { AccountServicesInjection, SessionServiceShape, UserServiceShape };
|
|
30
9
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/service/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/service/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,4BAA4B,CAAC;AACvE,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAEjE,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAE5D,MAAM,CAAC,OAAO,OAAO,eAAe;IAClC,IAAI,EAAE,WAAW,CAAC;IAClB,OAAO,EAAE,cAAc,CAAC;gBAEZ,OAAO,EAAE,sBAAsB;CAK5C"}
|
package/lib/service/index.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import session_service from "./services/session-service/index.js";
|
|
2
|
+
import user_service from "./services/user-service/index.js";
|
|
3
|
+
import { isBrowser } from "./utils/helpers.js";
|
|
1
4
|
class DataloomService {
|
|
2
5
|
constructor(options){
|
|
3
|
-
const
|
|
4
|
-
this.user =
|
|
5
|
-
this.session =
|
|
6
|
+
const baseUrl = isBrowser() ? window.location.origin : '';
|
|
7
|
+
this.user = new user_service(baseUrl, options);
|
|
8
|
+
this.session = new session_service(baseUrl, options);
|
|
6
9
|
}
|
|
7
10
|
}
|
|
8
11
|
export { DataloomService as default };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type DataloomServiceOptions, type DataloomServiceResponse, type ExtraInfo, type Fetch, type FetchOptions, type OnRequestErrorType, ServiceName } from '../utils/types';
|
|
2
|
+
export default abstract class DataloomServiceBase {
|
|
3
|
+
protected baseUrl: string;
|
|
4
|
+
protected headers: {
|
|
5
|
+
[key: string]: string;
|
|
6
|
+
};
|
|
7
|
+
protected fetch: Fetch;
|
|
8
|
+
protected logger: boolean;
|
|
9
|
+
protected serviceName: ServiceName;
|
|
10
|
+
protected brandId: string;
|
|
11
|
+
protected bizSource: string;
|
|
12
|
+
protected appId: string;
|
|
13
|
+
protected onRequestError?: (error: OnRequestErrorType) => void;
|
|
14
|
+
constructor(baseUrl: string, options: DataloomServiceOptions);
|
|
15
|
+
fetchData<T>(url: string, options: FetchOptions, extras?: ExtraInfo): Promise<DataloomServiceResponse<T>>;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=DataloomServiceBase.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DataloomServiceBase.d.ts","sourceRoot":"","sources":["../../../src/service/services/DataloomServiceBase.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,KAAK,sBAAsB,EAC3B,KAAK,uBAAuB,EAC5B,KAAK,SAAS,EACd,KAAK,KAAK,EACV,KAAK,YAAY,EACjB,KAAK,kBAAkB,EACvB,WAAW,EACZ,MAAM,gBAAgB,CAAC;AAExB,MAAM,CAAC,OAAO,CAAC,QAAQ,OAAO,mBAAmB;IAC/C,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC;IAC1B,SAAS,CAAC,OAAO,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;IAC7C,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC;IACvB,SAAS,CAAC,MAAM,EAAE,OAAO,CAAC;IAC1B,SAAS,CAAC,WAAW,EAAE,WAAW,CAAC;IACnC,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC;IAC1B,SAAS,CAAC,SAAS,EAAE,MAAM,CAAC;IAC5B,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC;IACxB,SAAS,CAAC,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,kBAAkB,KAAK,IAAI,CAAC;gBAEnD,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,sBAAsB;IAoBtD,SAAS,CAAC,CAAC,EACf,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,YAAY,EACrB,MAAM,CAAC,EAAE,SAAS,GACjB,OAAO,CAAC,uBAAuB,CAAC,CAAC,CAAC,CAAC;CAsIvC"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { getCookieByName, serializeSessionBrandId } from "../utils/helpers.js";
|
|
2
|
+
import { logRequest, logResponse } from "../utils/logger.js";
|
|
3
|
+
import { traceIdGenerator } from "../utils/trace-id.js";
|
|
4
|
+
import { ServiceName } from "../utils/types.js";
|
|
5
|
+
class DataloomServiceBase {
|
|
6
|
+
constructor(baseUrl, options){
|
|
7
|
+
this.baseUrl = baseUrl;
|
|
8
|
+
this.headers = {
|
|
9
|
+
...options.headers,
|
|
10
|
+
'X-Suda-Csrf-Token': getCookieByName('suda-csrf-token')
|
|
11
|
+
};
|
|
12
|
+
this.logger = options.logger ?? false;
|
|
13
|
+
this.serviceName = ServiceName.DEFAULT;
|
|
14
|
+
this.brandId = serializeSessionBrandId(options.brandName);
|
|
15
|
+
this.bizSource = options.brandName;
|
|
16
|
+
this.appId = options.appId;
|
|
17
|
+
this.onRequestError = options.onRequestError;
|
|
18
|
+
if (options.fetch) this.fetch = options.fetch;
|
|
19
|
+
else this.fetch = fetch;
|
|
20
|
+
}
|
|
21
|
+
async fetchData(url, options, extras) {
|
|
22
|
+
const fetchUrl = `${this.baseUrl.toString()}${url}`;
|
|
23
|
+
let traceId = -1;
|
|
24
|
+
if (this.logger) {
|
|
25
|
+
traceId = traceIdGenerator.next();
|
|
26
|
+
logRequest({
|
|
27
|
+
traceId,
|
|
28
|
+
url: fetchUrl,
|
|
29
|
+
method: options.method,
|
|
30
|
+
body: options.body
|
|
31
|
+
}, extras?.psmName);
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
const res = await this.fetch(fetchUrl, {
|
|
35
|
+
method: options.method,
|
|
36
|
+
mode: 'cors',
|
|
37
|
+
redirect: 'manual',
|
|
38
|
+
credentials: 'include',
|
|
39
|
+
headers: this.headers,
|
|
40
|
+
body: JSON.stringify(options.body)
|
|
41
|
+
});
|
|
42
|
+
let error = null;
|
|
43
|
+
let data = null;
|
|
44
|
+
let status = res.status;
|
|
45
|
+
let statusText = res.statusText;
|
|
46
|
+
if (res.ok) {
|
|
47
|
+
const body = await res.text();
|
|
48
|
+
const parsedBody = JSON.parse(body);
|
|
49
|
+
if (parsedBody.status_code && parsedBody.error_msg) {
|
|
50
|
+
data = null;
|
|
51
|
+
error = {
|
|
52
|
+
code: parsedBody.status_code || res.status,
|
|
53
|
+
details: '',
|
|
54
|
+
hint: '',
|
|
55
|
+
message: parsedBody.error_msg || body
|
|
56
|
+
};
|
|
57
|
+
} else {
|
|
58
|
+
data = parsedBody.data;
|
|
59
|
+
error = null;
|
|
60
|
+
}
|
|
61
|
+
} else {
|
|
62
|
+
const body = await res.text();
|
|
63
|
+
const parsedBody = JSON.parse(body);
|
|
64
|
+
try {
|
|
65
|
+
error = 429 === res.status ? {
|
|
66
|
+
code: 429,
|
|
67
|
+
message: 'Too many requests received within 5 seconds.',
|
|
68
|
+
details: '',
|
|
69
|
+
hint: 'Retry after 5 seconds.'
|
|
70
|
+
} : {
|
|
71
|
+
code: parsedBody.status_code || res.status,
|
|
72
|
+
message: parsedBody.error_msg || body,
|
|
73
|
+
details: '',
|
|
74
|
+
hint: ''
|
|
75
|
+
};
|
|
76
|
+
} catch (_e) {
|
|
77
|
+
error = {
|
|
78
|
+
code: parsedBody.status_code || res.status,
|
|
79
|
+
message: parsedBody.error_msg || body,
|
|
80
|
+
details: '',
|
|
81
|
+
hint: ''
|
|
82
|
+
};
|
|
83
|
+
data = null;
|
|
84
|
+
status = 500;
|
|
85
|
+
statusText = 'Dataloom Internal Error';
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const serviceResponse = {
|
|
89
|
+
data,
|
|
90
|
+
error,
|
|
91
|
+
status,
|
|
92
|
+
statusText
|
|
93
|
+
};
|
|
94
|
+
if (error) this.onRequestError?.({
|
|
95
|
+
...error,
|
|
96
|
+
status,
|
|
97
|
+
statusText
|
|
98
|
+
});
|
|
99
|
+
if (this.logger) logResponse(traceId, serviceResponse, extras?.psmName);
|
|
100
|
+
return serviceResponse;
|
|
101
|
+
} catch (e) {
|
|
102
|
+
const internalError = {
|
|
103
|
+
error: {
|
|
104
|
+
code: 500,
|
|
105
|
+
message: e instanceof Error ? e.message : String(e),
|
|
106
|
+
details: '',
|
|
107
|
+
hint: ''
|
|
108
|
+
},
|
|
109
|
+
status: 500,
|
|
110
|
+
statusText: 'Dataloom Internal Error'
|
|
111
|
+
};
|
|
112
|
+
this.onRequestError?.({
|
|
113
|
+
...internalError.error,
|
|
114
|
+
status: internalError.status,
|
|
115
|
+
statusText: internalError.statusText
|
|
116
|
+
});
|
|
117
|
+
const serviceResponse = {
|
|
118
|
+
data: null,
|
|
119
|
+
...internalError
|
|
120
|
+
};
|
|
121
|
+
if (this.logger) logResponse(traceId, serviceResponse, extras?.psmName);
|
|
122
|
+
return serviceResponse;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
export { DataloomServiceBase as default };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { DataloomServiceResponse } from '../../utils/types';
|
|
2
|
+
import { ServiceName } from '../../utils/types';
|
|
3
|
+
import DataloomServiceBase from '../DataloomServiceBase';
|
|
4
|
+
import type { SignInRedirectionOptions, UserInfoResponse } from './types';
|
|
5
|
+
export default class SessionService extends DataloomServiceBase {
|
|
6
|
+
serviceName: ServiceName;
|
|
7
|
+
/**
|
|
8
|
+
* Redirect to login page within app runtime environment.
|
|
9
|
+
*/
|
|
10
|
+
redirectToLogin(options?: SignInRedirectionOptions): DataloomServiceResponse<'success'>;
|
|
11
|
+
/**
|
|
12
|
+
* Sign out current user session.
|
|
13
|
+
*/
|
|
14
|
+
signOut(): Promise<DataloomServiceResponse<null>>;
|
|
15
|
+
/**
|
|
16
|
+
* Get current page URL with biz_source parameter
|
|
17
|
+
*/
|
|
18
|
+
getUrlWithBizSource(): string;
|
|
19
|
+
/**
|
|
20
|
+
* Gets the current user information if there is an existing session.
|
|
21
|
+
*/
|
|
22
|
+
getUserInfo(): Promise<DataloomServiceResponse<UserInfoResponse>>;
|
|
23
|
+
/**
|
|
24
|
+
* Navigate to user profile page within app runtime environment.
|
|
25
|
+
*/
|
|
26
|
+
navigateToUserProfile(options?: {
|
|
27
|
+
newTab?: boolean;
|
|
28
|
+
}): {
|
|
29
|
+
data: null;
|
|
30
|
+
error: {
|
|
31
|
+
code: number;
|
|
32
|
+
message: string;
|
|
33
|
+
hint: string;
|
|
34
|
+
details: string;
|
|
35
|
+
};
|
|
36
|
+
status: number;
|
|
37
|
+
statusText: string;
|
|
38
|
+
} | {
|
|
39
|
+
data: string;
|
|
40
|
+
error: null;
|
|
41
|
+
status: number;
|
|
42
|
+
statusText: string;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/service/services/session-service/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AACjE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,mBAAmB,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,EAGV,wBAAwB,EACxB,gBAAgB,EACjB,MAAM,SAAS,CAAC;AAcjB,MAAM,CAAC,OAAO,OAAO,cAAe,SAAQ,mBAAmB;IACpD,WAAW,cAA8B;IAElD;;OAEG;IACH,eAAe,CACb,OAAO,CAAC,EAAE,wBAAwB,GACjC,uBAAuB,CAAC,SAAS,CAAC;IAsCrC;;OAEG;IACG,OAAO,IAAI,OAAO,CAAC,uBAAuB,CAAC,IAAI,CAAC,CAAC;IAyBvD;;OAEG;IACH,mBAAmB,IAAI,MAAM;IAkB7B;;OAEG;IACG,WAAW,IAAI,OAAO,CAAC,uBAAuB,CAAC,gBAAgB,CAAC,CAAC;IAuDvE;;OAEG;IACH,qBAAqB,CAAC,OAAO,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,OAAO,CAAA;KAAE;;;;;;;;;;;;;;;;CA+BrD"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { isBrowser } from "../../utils/helpers.js";
|
|
2
|
+
import { ServiceName } from "../../utils/types.js";
|
|
3
|
+
import { reportDataloomException } from "../../../utils/slardar.js";
|
|
4
|
+
import DataloomServiceBase from "../DataloomServiceBase.js";
|
|
5
|
+
const browserEnvError = {
|
|
6
|
+
data: null,
|
|
7
|
+
error: {
|
|
8
|
+
code: 400,
|
|
9
|
+
message: 'Incompatible runtime environment',
|
|
10
|
+
hint: 'Please check if the current environment is browser.',
|
|
11
|
+
details: 'This method can only be invoked in browser environment.'
|
|
12
|
+
},
|
|
13
|
+
status: 400,
|
|
14
|
+
statusText: 'Bad Request'
|
|
15
|
+
};
|
|
16
|
+
class SessionService extends DataloomServiceBase {
|
|
17
|
+
redirectToLogin(options) {
|
|
18
|
+
if (!isBrowser()) {
|
|
19
|
+
reportDataloomException(browserEnvError.error, {
|
|
20
|
+
module: 'session-service',
|
|
21
|
+
method: 'redirectToLogin',
|
|
22
|
+
type: 'runtime'
|
|
23
|
+
});
|
|
24
|
+
console.log('[Dataloom][SessionService.RedirectToLogin] dataloom.service.session.redirectToLogin can only be invoked in browser environment.');
|
|
25
|
+
return browserEnvError;
|
|
26
|
+
}
|
|
27
|
+
const { returnUrl, newTab = false } = options ?? {};
|
|
28
|
+
const encodedRedirectUrl = encodeURIComponent(decodeURIComponent(returnUrl || window.location.href));
|
|
29
|
+
const isSparkFaasPath = window.location.pathname.startsWith('/spark');
|
|
30
|
+
const baseLoginUrl = isSparkFaasPath ? `/spark/faas/${this.appId}/runtime/page/login` : `/app/${this.appId}/runtime/page/login`;
|
|
31
|
+
const loginPageUrl = `${baseLoginUrl}?biz_source=${this.bizSource}&redirect_uri=${encodedRedirectUrl}`;
|
|
32
|
+
if (newTab) window.open(loginPageUrl, '_blank');
|
|
33
|
+
else window.location.href = loginPageUrl;
|
|
34
|
+
return {
|
|
35
|
+
data: 'success',
|
|
36
|
+
error: null,
|
|
37
|
+
status: 200,
|
|
38
|
+
statusText: 'OK'
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
async signOut() {
|
|
42
|
+
if (!isBrowser()) {
|
|
43
|
+
reportDataloomException(browserEnvError.error, {
|
|
44
|
+
module: 'session-service',
|
|
45
|
+
method: 'signOut',
|
|
46
|
+
type: 'runtime'
|
|
47
|
+
});
|
|
48
|
+
console.log('[Dataloom][SessionService.SignOut] dataloom.service.session.signOut can only be invoked in browser environment.');
|
|
49
|
+
return browserEnvError;
|
|
50
|
+
}
|
|
51
|
+
const location = this.getUrlWithBizSource();
|
|
52
|
+
return this.fetchData('/af/runtime/api/v1/account/logout', {
|
|
53
|
+
method: 'POST',
|
|
54
|
+
body: {
|
|
55
|
+
appID: this.appId,
|
|
56
|
+
redirectURI: location
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
getUrlWithBizSource() {
|
|
61
|
+
if (!isBrowser()) {
|
|
62
|
+
reportDataloomException(browserEnvError.error, {
|
|
63
|
+
module: 'session-service',
|
|
64
|
+
method: 'getUrlWithBizSource',
|
|
65
|
+
type: 'runtime'
|
|
66
|
+
});
|
|
67
|
+
console.log('[Dataloom][SessionService.GetUrlWithBizSource] dataloom.service.session.getUrlWithBizSource can only be invoked in browser environment.');
|
|
68
|
+
return '';
|
|
69
|
+
}
|
|
70
|
+
const currentUrl = new URL(window.location.href);
|
|
71
|
+
currentUrl.searchParams.set('biz_source', this.bizSource);
|
|
72
|
+
return currentUrl.toString();
|
|
73
|
+
}
|
|
74
|
+
async getUserInfo() {
|
|
75
|
+
if (!isBrowser()) {
|
|
76
|
+
reportDataloomException(browserEnvError.error, {
|
|
77
|
+
module: 'session-service',
|
|
78
|
+
method: 'getUserInfo',
|
|
79
|
+
type: 'runtime'
|
|
80
|
+
});
|
|
81
|
+
console.log('[Dataloom][SessionService.GetUserInfo] dataloom.service.session.getUserInfo can only be invoked in browser environment.');
|
|
82
|
+
return Promise.resolve(browserEnvError);
|
|
83
|
+
}
|
|
84
|
+
const url = `/app/${this.appId}/__runtime__/api/v1/account/login/user`;
|
|
85
|
+
const urlObj = new URL(globalThis.location.href);
|
|
86
|
+
const pathSegments = urlObj.hostname.split('.');
|
|
87
|
+
const tenantDomainName = pathSegments[0] ?? '';
|
|
88
|
+
const response = await this.fetchData(url, {
|
|
89
|
+
method: 'POST',
|
|
90
|
+
body: {
|
|
91
|
+
appID: this.appId,
|
|
92
|
+
tenantDomainName
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
const userInfoResponse = {
|
|
96
|
+
data: {
|
|
97
|
+
user_info: {
|
|
98
|
+
user_id: response.data?.userInfo?.userID,
|
|
99
|
+
name: response.data?.userInfo?.name,
|
|
100
|
+
avatar: response.data?.userInfo?.avatar,
|
|
101
|
+
email: response.data?.userInfo?.email,
|
|
102
|
+
phone_number: response.data?.userInfo?.desensitizedPhoneNumber
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
error: null,
|
|
106
|
+
status: response.status,
|
|
107
|
+
statusText: response.statusText
|
|
108
|
+
};
|
|
109
|
+
return userInfoResponse;
|
|
110
|
+
}
|
|
111
|
+
navigateToUserProfile(options) {
|
|
112
|
+
if (!isBrowser()) {
|
|
113
|
+
reportDataloomException(browserEnvError.error, {
|
|
114
|
+
module: 'session-service',
|
|
115
|
+
method: 'navigateToUserProfile',
|
|
116
|
+
type: 'runtime'
|
|
117
|
+
});
|
|
118
|
+
console.log('[Dataloom][SessionService.NavigateToUserProfile] dataloom.service.session.navigateToUserProfile can only be invoked in browser environment.');
|
|
119
|
+
return browserEnvError;
|
|
120
|
+
}
|
|
121
|
+
const { newTab = false } = options ?? {};
|
|
122
|
+
const encodedReturnUrl = encodeURIComponent(window.location.href);
|
|
123
|
+
const profilePageUrl = `/suda/user?brand=${this.brandId}&returnUrl=${encodedReturnUrl}&appId=${this.appId}`;
|
|
124
|
+
if (newTab) window.open(profilePageUrl, '_blank');
|
|
125
|
+
else window.location.href = profilePageUrl;
|
|
126
|
+
return {
|
|
127
|
+
data: 'success',
|
|
128
|
+
error: null,
|
|
129
|
+
status: 200,
|
|
130
|
+
statusText: 'OK'
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
constructor(...args){
|
|
134
|
+
super(...args), this.serviceName = ServiceName.SessionService;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
export { SessionService as default };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export interface I18n {
|
|
2
|
+
language_code: number;
|
|
3
|
+
text: string;
|
|
4
|
+
}
|
|
5
|
+
export type I18ns = I18n[];
|
|
6
|
+
export interface Avatar {
|
|
7
|
+
source?: string;
|
|
8
|
+
image?: {
|
|
9
|
+
large?: string;
|
|
10
|
+
};
|
|
11
|
+
color?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface SignInRedirectionOptions {
|
|
14
|
+
/**
|
|
15
|
+
* The URL to redirect to after a successful login. Defaults to the current URL.
|
|
16
|
+
*/
|
|
17
|
+
returnUrl?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Whether to open the login page in a new tab. Defaults to `false`.
|
|
20
|
+
*/
|
|
21
|
+
newTab?: boolean;
|
|
22
|
+
}
|
|
23
|
+
export interface UserBaseInfo {
|
|
24
|
+
user_id?: number;
|
|
25
|
+
name?: I18ns;
|
|
26
|
+
avatar?: Avatar;
|
|
27
|
+
email?: string;
|
|
28
|
+
phone_number?: string;
|
|
29
|
+
tenant_name?: string;
|
|
30
|
+
}
|
|
31
|
+
export interface UserInfoResponse {
|
|
32
|
+
user_info?: UserBaseInfo;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/service/services/session-service/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,IAAI;IACnB,aAAa,EAAE,MAAM,CAAC;IACtB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,KAAK,GAAG,IAAI,EAAE,CAAC;AAE3B,MAAM,WAAW,MAAM;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE;QACN,KAAK,CAAC,EAAE,MAAM,CAAC;KAChB,CAAC;IACF,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,wBAAwB;IACvC;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,YAAY;IAC3B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,KAAK,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,gBAAgB;IAC/B,SAAS,CAAC,EAAE,YAAY,CAAC;CAC1B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/service/services/types.ts"],"names":[],"mappings":"AACA,oBAAY,aAAa;IACvB,WAAW,IAAI;IACf,QAAQ,IAAI,CAAE,MAAM;IACpB,MAAM,IAAI,CAAE,MAAM;IAClB,QAAQ,IAAI,CAAE,KAAK;IACnB,UAAU,IAAI;CACf;AAGD,oBAAY,UAAU;IACpB,MAAM,IAAI,CAAE,KAAK;IACjB,QAAQ,IAAI;CACb"}
|