@lark-apaas/dataloom 0.1.1-alpha.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.
Files changed (98) hide show
  1. package/README.md +3 -0
  2. package/lib/DataloomClient.d.ts +31 -0
  3. package/lib/DataloomClient.d.ts.map +1 -0
  4. package/lib/DataloomClient.js +39 -0
  5. package/lib/RateLimiter.d.ts +15 -0
  6. package/lib/RateLimiter.d.ts.map +1 -0
  7. package/lib/RateLimiter.js +31 -0
  8. package/lib/index.d.ts +9 -0
  9. package/lib/index.d.ts.map +1 -0
  10. package/lib/index.js +3 -0
  11. package/lib/service/index.d.ts +9 -0
  12. package/lib/service/index.d.ts.map +1 -0
  13. package/lib/service/index.js +11 -0
  14. package/lib/service/services/DataloomServiceBase.d.ts +17 -0
  15. package/lib/service/services/DataloomServiceBase.d.ts.map +1 -0
  16. package/lib/service/services/DataloomServiceBase.js +126 -0
  17. package/lib/service/services/session-service/index.d.ts +45 -0
  18. package/lib/service/services/session-service/index.d.ts.map +1 -0
  19. package/lib/service/services/session-service/index.js +111 -0
  20. package/lib/service/services/session-service/types.d.ts +34 -0
  21. package/lib/service/services/session-service/types.d.ts.map +1 -0
  22. package/lib/service/services/session-service/types.js +0 -0
  23. package/lib/service/services/types.d.ts +12 -0
  24. package/lib/service/services/types.d.ts.map +1 -0
  25. package/lib/service/services/types.js +14 -0
  26. package/lib/service/services/user-service/index.d.ts +9 -0
  27. package/lib/service/services/user-service/index.d.ts.map +1 -0
  28. package/lib/service/services/user-service/index.js +60 -0
  29. package/lib/service/services/user-service/types.d.ts +51 -0
  30. package/lib/service/services/user-service/types.d.ts.map +1 -0
  31. package/lib/service/services/user-service/types.js +0 -0
  32. package/lib/service/utils/helpers.d.ts +8 -0
  33. package/lib/service/utils/helpers.d.ts.map +1 -0
  34. package/lib/service/utils/helpers.js +18 -0
  35. package/lib/service/utils/logger.d.ts +15 -0
  36. package/lib/service/utils/logger.d.ts.map +1 -0
  37. package/lib/service/utils/logger.js +18 -0
  38. package/lib/service/utils/trace-id.d.ts +45 -0
  39. package/lib/service/utils/trace-id.d.ts.map +1 -0
  40. package/lib/service/utils/trace-id.js +20 -0
  41. package/lib/service/utils/types.d.ts +49 -0
  42. package/lib/service/utils/types.d.ts.map +1 -0
  43. package/lib/service/utils/types.js +7 -0
  44. package/lib/storage/StorageClient.d.ts +22 -0
  45. package/lib/storage/StorageClient.d.ts.map +1 -0
  46. package/lib/storage/StorageClient.js +11 -0
  47. package/lib/storage/index.d.ts +4 -0
  48. package/lib/storage/index.d.ts.map +1 -0
  49. package/lib/storage/index.js +4 -0
  50. package/lib/storage/libs/constants.d.ts +4 -0
  51. package/lib/storage/libs/constants.d.ts.map +1 -0
  52. package/lib/storage/libs/constants.js +5 -0
  53. package/lib/storage/libs/errors.d.ts +24 -0
  54. package/lib/storage/libs/errors.d.ts.map +1 -0
  55. package/lib/storage/libs/errors.js +39 -0
  56. package/lib/storage/libs/fetch.d.ts +24 -0
  57. package/lib/storage/libs/fetch.d.ts.map +1 -0
  58. package/lib/storage/libs/fetch.js +139 -0
  59. package/lib/storage/libs/helpers.d.ts +18 -0
  60. package/lib/storage/libs/helpers.d.ts.map +1 -0
  61. package/lib/storage/libs/helpers.js +57 -0
  62. package/lib/storage/libs/index.d.ts +5 -0
  63. package/lib/storage/libs/index.d.ts.map +1 -0
  64. package/lib/storage/libs/index.js +4 -0
  65. package/lib/storage/libs/types.d.ts +205 -0
  66. package/lib/storage/libs/types.d.ts.map +1 -0
  67. package/lib/storage/libs/types.js +0 -0
  68. package/lib/storage/libs/version.d.ts +2 -0
  69. package/lib/storage/libs/version.d.ts.map +1 -0
  70. package/lib/storage/libs/version.js +2 -0
  71. package/lib/storage/packages/StorageBucketApi.d.ts +121 -0
  72. package/lib/storage/packages/StorageBucketApi.d.ts.map +1 -0
  73. package/lib/storage/packages/StorageBucketApi.js +135 -0
  74. package/lib/storage/packages/StorageFileApi.d.ts +190 -0
  75. package/lib/storage/packages/StorageFileApi.d.ts.map +1 -0
  76. package/lib/storage/packages/StorageFileApi.js +378 -0
  77. package/lib/storage/utils/logger.d.ts +15 -0
  78. package/lib/storage/utils/logger.d.ts.map +1 -0
  79. package/lib/storage/utils/logger.js +18 -0
  80. package/lib/storage/utils/trace-id.d.ts +45 -0
  81. package/lib/storage/utils/trace-id.d.ts.map +1 -0
  82. package/lib/storage/utils/trace-id.js +20 -0
  83. package/lib/utils/constants.d.ts +7 -0
  84. package/lib/utils/constants.d.ts.map +1 -0
  85. package/lib/utils/constants.js +33 -0
  86. package/lib/utils/fetch.d.ts +6 -0
  87. package/lib/utils/fetch.d.ts.map +1 -0
  88. package/lib/utils/fetch.js +22 -0
  89. package/lib/utils/helpers.d.ts +5 -0
  90. package/lib/utils/helpers.d.ts.map +1 -0
  91. package/lib/utils/helpers.js +17 -0
  92. package/lib/utils/types.d.ts +34 -0
  93. package/lib/utils/types.d.ts.map +1 -0
  94. package/lib/utils/types.js +0 -0
  95. package/lib/utils/version.d.ts +2 -0
  96. package/lib/utils/version.d.ts.map +1 -0
  97. package/lib/utils/version.js +2 -0
  98. package/package.json +34 -0
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @lark-apaas/dataloom
2
+
3
+ Dataloom client SDK for @lark-apaas (storage + service). Migrated from @data-loom/js.
@@ -0,0 +1,31 @@
1
+ import RateLimiter from './RateLimiter';
2
+ import DataloomService from './service';
3
+ import { StorageClient } from './storage';
4
+ import type { Fetch, SupabaseClientOptions } from './utils/types';
5
+ /**
6
+ * Dataloom Client.
7
+ *
8
+ * An isomorphic Javascript client for interacting with Dataloom service and storage.
9
+ */
10
+ export default class DataloomClient {
11
+ protected supabaseUrl: string;
12
+ protected supabaseKey: string;
13
+ protected workspaceId: string;
14
+ service: DataloomService;
15
+ storage: StorageClient;
16
+ protected storageUrl: string;
17
+ protected fetch?: Fetch;
18
+ protected rateLimiter?: RateLimiter;
19
+ protected onRequestError: (error: any) => void;
20
+ protected headers: Record<string, string>;
21
+ /**
22
+ * Create a new client for use in the browser.
23
+ * @param supabaseUrl The unique Dataloom URL which is supplied when you create a new project.
24
+ * @param supabaseKey The unique Dataloom Key which is supplied when you create a new project.
25
+ * @param workspaceId The unique workspace id which is supplied when you create a new workspace.
26
+ * @param options.global.fetch A custom fetch implementation.
27
+ * @param options.global.headers Any additional headers to send with each network request.
28
+ */
29
+ constructor(supabaseUrl: string, supabaseKey: string, workspaceId: string, options?: SupabaseClientOptions);
30
+ }
31
+ //# sourceMappingURL=DataloomClient.d.ts.map
@@ -0,0 +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;AAI1C,OAAO,KAAK,EAAE,KAAK,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAElE;;;;GAIG;AACH,MAAM,CAAC,OAAO,OAAO,cAAc;IAoB/B,SAAS,CAAC,WAAW,EAAE,MAAM;IAC7B,SAAS,CAAC,WAAW,EAAE,MAAM;IAC7B,SAAS,CAAC,WAAW,EAAE,MAAM;IArB/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,GAAG,KAAK,IAAI,CAAC;IAE/C,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE1C;;;;;;;OAOG;gBAES,WAAW,EAAE,MAAM,EACnB,WAAW,EAAE,MAAM,EACnB,WAAW,EAAE,MAAM,EAC7B,OAAO,CAAC,EAAE,qBAAqB;CAoDlC"}
@@ -0,0 +1,39 @@
1
+ import RateLimiter from "./RateLimiter.js";
2
+ import service from "./service/index.js";
3
+ import { StorageClient } from "./storage/index.js";
4
+ import { DEFAULT_GLOBAL_OPTIONS } from "./utils/constants.js";
5
+ import { fetchWithAuth } from "./utils/fetch.js";
6
+ import { applySettingDefaults, stripTrailingSlash } from "./utils/helpers.js";
7
+ class DataloomClient {
8
+ constructor(supabaseUrl, supabaseKey, workspaceId, options){
9
+ this.supabaseUrl = supabaseUrl;
10
+ this.supabaseKey = supabaseKey;
11
+ this.workspaceId = workspaceId;
12
+ const _supabaseUrl = stripTrailingSlash(supabaseUrl);
13
+ this.storageUrl = `${_supabaseUrl}/storage`;
14
+ const DEFAULTS = {
15
+ global: DEFAULT_GLOBAL_OPTIONS
16
+ };
17
+ const settings = applySettingDefaults(options ?? {}, DEFAULTS);
18
+ this.headers = settings.global.headers ?? {};
19
+ this.onRequestError = (error)=>{
20
+ if (settings.global.onError) settings.global.onError(error, this);
21
+ };
22
+ if (settings.global.requestRateLimit && settings.global.requestRateLimit > 0) this.rateLimiter = new RateLimiter(settings.global.requestRateLimit);
23
+ this.fetch = fetchWithAuth(supabaseKey, this.rateLimiter, settings.global.fetch);
24
+ this.service = new service({
25
+ headers: this.headers,
26
+ fetch: this.fetch,
27
+ logger: settings.global.enableDataloomLog,
28
+ brandName: settings.global.brandName,
29
+ appId: settings.global.appId,
30
+ onRequestError: this.onRequestError
31
+ });
32
+ this.storage = new StorageClient(this.storageUrl, this.headers, this.fetch, {
33
+ enableLogger: settings.global.enableDataloomLog,
34
+ onRequestError: this.onRequestError,
35
+ appId: settings.global.appId
36
+ });
37
+ }
38
+ }
39
+ export { DataloomClient as default };
@@ -0,0 +1,15 @@
1
+ /**
2
+ * rate limiter that allows a given number of requests within a predefined time window.
3
+ */
4
+ declare class RateLimiter {
5
+ static readonly WINDOW_SIZE = 5000;
6
+ maxRequests: number;
7
+ private windowSizeMs;
8
+ private requestCount;
9
+ private windowStart;
10
+ constructor(maxRequests: number);
11
+ checkAndResetWindow(): boolean;
12
+ canRequest(): boolean;
13
+ }
14
+ export default RateLimiter;
15
+ //# sourceMappingURL=RateLimiter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RateLimiter.d.ts","sourceRoot":"","sources":["../src/RateLimiter.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,cAAM,WAAW;IACf,MAAM,CAAC,QAAQ,CAAC,WAAW,QAAQ;IACnC,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,WAAW,CAAS;gBAEhB,WAAW,EAAE,MAAM;IAO/B,mBAAmB;IAYnB,UAAU;CASX;AAED,eAAe,WAAW,CAAC"}
@@ -0,0 +1,31 @@
1
+ class RateLimiter {
2
+ static{
3
+ this.WINDOW_SIZE = 5000;
4
+ }
5
+ constructor(maxRequests){
6
+ this.maxRequests = maxRequests;
7
+ this.windowSizeMs = RateLimiter.WINDOW_SIZE;
8
+ this.requestCount = 0;
9
+ this.windowStart = Date.now();
10
+ }
11
+ checkAndResetWindow() {
12
+ const now = Date.now();
13
+ const windowElapsed = now - this.windowStart;
14
+ if (windowElapsed >= this.windowSizeMs) {
15
+ this.requestCount = 0;
16
+ this.windowStart = now;
17
+ return true;
18
+ }
19
+ return false;
20
+ }
21
+ canRequest() {
22
+ this.checkAndResetWindow();
23
+ if (this.requestCount < this.maxRequests) {
24
+ this.requestCount++;
25
+ return true;
26
+ }
27
+ return false;
28
+ }
29
+ }
30
+ const src_RateLimiter = RateLimiter;
31
+ export { src_RateLimiter as default };
package/lib/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import DataloomClient from './DataloomClient';
2
+ import type { SupabaseClientOptions } from './utils/types';
3
+ export { default as DataloomClient } from './DataloomClient';
4
+ export type { SupabaseClientOptions } from './utils/types';
5
+ /**
6
+ * Creates a new Dataloom Client.
7
+ */
8
+ export declare const createClient: (dataloomUrl: string, dataloomKey: string, workspaceId: string, options?: SupabaseClientOptions) => DataloomClient;
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +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;;GAEG;AACH,eAAO,MAAM,YAAY,GACvB,aAAa,MAAM,EACnB,aAAa,MAAM,EACnB,aAAa,MAAM,EACnB,UAAU,qBAAqB,KAC9B,cAEF,CAAC"}
package/lib/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import DataloomClient from "./DataloomClient.js";
2
+ const createClient = (dataloomUrl, dataloomKey, workspaceId, options)=>new DataloomClient(dataloomUrl, dataloomKey, workspaceId, options);
3
+ export { DataloomClient, createClient };
@@ -0,0 +1,9 @@
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';
4
+ export default class DataloomService {
5
+ user: UserService;
6
+ session: SessionService;
7
+ constructor(options: DataloomServiceOptions);
8
+ }
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
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"}
@@ -0,0 +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";
4
+ class DataloomService {
5
+ constructor(options){
6
+ const baseUrl = isBrowser() ? window.location.origin : '';
7
+ this.user = new user_service(baseUrl, options);
8
+ this.session = new session_service(baseUrl, options);
9
+ }
10
+ }
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;CAuIvC"}
@@ -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?.message,
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;AAChD,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;IAiCrC;;OAEG;IACG,OAAO,IAAI,OAAO,CAAC,uBAAuB,CAAC,IAAI,CAAC,CAAC;IAoBvD;;OAEG;IACH,mBAAmB,IAAI,MAAM;IAa7B;;OAEG;IACG,WAAW,IAAI,OAAO,CAAC,uBAAuB,CAAC,gBAAgB,CAAC,CAAC;IAkDvE;;OAEG;IACH,qBAAqB,CAAC,OAAO,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,OAAO,CAAA;KAAE;;;;;;;;;;;;;;;;CA0BrD"}
@@ -0,0 +1,111 @@
1
+ import { isBrowser } from "../../utils/helpers.js";
2
+ import { ServiceName } from "../../utils/types.js";
3
+ import DataloomServiceBase from "../DataloomServiceBase.js";
4
+ const browserEnvError = {
5
+ data: null,
6
+ error: {
7
+ code: 400,
8
+ message: 'Incompatible runtime environment',
9
+ hint: 'Please check if the current environment is browser.',
10
+ details: 'This method can only be invoked in browser environment.'
11
+ },
12
+ status: 400,
13
+ statusText: 'Bad Request'
14
+ };
15
+ class SessionService extends DataloomServiceBase {
16
+ redirectToLogin(options) {
17
+ if (!isBrowser()) {
18
+ console.log('[Dataloom][SessionService.RedirectToLogin] dataloom.service.session.redirectToLogin can only be invoked in browser environment.');
19
+ return browserEnvError;
20
+ }
21
+ const { returnUrl, newTab = false } = options ?? {};
22
+ const encodedRedirectUrl = encodeURIComponent(decodeURIComponent(returnUrl || window.location.href));
23
+ const isSparkFaasPath = window.location.pathname.startsWith('/spark');
24
+ const baseLoginUrl = isSparkFaasPath ? `/spark/faas/${this.appId}/runtime/page/login` : `/app/${this.appId}/runtime/page/login`;
25
+ const loginPageUrl = `${baseLoginUrl}?biz_source=${this.bizSource}&redirect_uri=${encodedRedirectUrl}`;
26
+ if (newTab) window.open(loginPageUrl, '_blank');
27
+ else window.location.href = loginPageUrl;
28
+ return {
29
+ data: 'success',
30
+ error: null,
31
+ status: 200,
32
+ statusText: 'OK'
33
+ };
34
+ }
35
+ async signOut() {
36
+ if (!isBrowser()) {
37
+ console.log('[Dataloom][SessionService.SignOut] dataloom.service.session.signOut can only be invoked in browser environment.');
38
+ return browserEnvError;
39
+ }
40
+ const location = this.getUrlWithBizSource();
41
+ return this.fetchData('/af/runtime/api/v1/account/logout', {
42
+ method: 'POST',
43
+ body: {
44
+ appID: this.appId,
45
+ redirectURI: location
46
+ }
47
+ });
48
+ }
49
+ getUrlWithBizSource() {
50
+ if (!isBrowser()) {
51
+ console.log('[Dataloom][SessionService.GetUrlWithBizSource] dataloom.service.session.getUrlWithBizSource can only be invoked in browser environment.');
52
+ return '';
53
+ }
54
+ const currentUrl = new URL(window.location.href);
55
+ currentUrl.searchParams.set('biz_source', this.bizSource);
56
+ return currentUrl.toString();
57
+ }
58
+ async getUserInfo() {
59
+ if (!isBrowser()) {
60
+ console.log('[Dataloom][SessionService.GetUserInfo] dataloom.service.session.getUserInfo can only be invoked in browser environment.');
61
+ return Promise.resolve(browserEnvError);
62
+ }
63
+ const url = `/app/${this.appId}/__runtime__/api/v1/account/login/user`;
64
+ const urlObj = new URL(globalThis.location.href);
65
+ const pathSegments = urlObj.hostname.split('.');
66
+ const tenantDomainName = pathSegments[0] ?? '';
67
+ const response = await this.fetchData(url, {
68
+ method: 'POST',
69
+ body: {
70
+ appID: this.appId,
71
+ tenantDomainName
72
+ }
73
+ });
74
+ const userInfoResponse = {
75
+ data: {
76
+ user_info: {
77
+ user_id: response.data?.userInfo?.userID,
78
+ name: response.data?.userInfo?.name,
79
+ avatar: response.data?.userInfo?.avatar,
80
+ email: response.data?.userInfo?.email,
81
+ phone_number: response.data?.userInfo?.desensitizedPhoneNumber
82
+ }
83
+ },
84
+ error: null,
85
+ status: response.status,
86
+ statusText: response.statusText
87
+ };
88
+ return userInfoResponse;
89
+ }
90
+ navigateToUserProfile(options) {
91
+ if (!isBrowser()) {
92
+ console.log('[Dataloom][SessionService.NavigateToUserProfile] dataloom.service.session.navigateToUserProfile can only be invoked in browser environment.');
93
+ return browserEnvError;
94
+ }
95
+ const { newTab = false } = options ?? {};
96
+ const encodedReturnUrl = encodeURIComponent(window.location.href);
97
+ const profilePageUrl = `/suda/user?brand=${this.brandId}&returnUrl=${encodedReturnUrl}&appId=${this.appId}`;
98
+ if (newTab) window.open(profilePageUrl, '_blank');
99
+ else window.location.href = profilePageUrl;
100
+ return {
101
+ data: 'success',
102
+ error: null,
103
+ status: 200,
104
+ statusText: 'OK'
105
+ };
106
+ }
107
+ constructor(...args){
108
+ super(...args), this.serviceName = ServiceName.SessionService;
109
+ }
110
+ }
111
+ 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"}
File without changes
@@ -0,0 +1,12 @@
1
+ export declare enum AccountStatus {
2
+ Unspecified = 0,
3
+ Inactive = 1,// 未激活
4
+ Active = 2,// 已激活
5
+ Disabled = 3,// 停用
6
+ Terminated = 4
7
+ }
8
+ export declare enum UserStatus {
9
+ active = 1,// 正常
10
+ inactive = 2
11
+ }
12
+ //# sourceMappingURL=types.d.ts.map
@@ -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"}
@@ -0,0 +1,14 @@
1
+ var types_AccountStatus = /*#__PURE__*/ function(AccountStatus) {
2
+ AccountStatus[AccountStatus["Unspecified"] = 0] = "Unspecified";
3
+ AccountStatus[AccountStatus["Inactive"] = 1] = "Inactive";
4
+ AccountStatus[AccountStatus["Active"] = 2] = "Active";
5
+ AccountStatus[AccountStatus["Disabled"] = 3] = "Disabled";
6
+ AccountStatus[AccountStatus["Terminated"] = 4] = "Terminated";
7
+ return AccountStatus;
8
+ }({});
9
+ var types_UserStatus = /*#__PURE__*/ function(UserStatus) {
10
+ UserStatus[UserStatus["active"] = 1] = "active";
11
+ UserStatus[UserStatus["inactive"] = 2] = "inactive";
12
+ return UserStatus;
13
+ }({});
14
+ export { types_AccountStatus as AccountStatus, types_UserStatus as UserStatus };
@@ -0,0 +1,9 @@
1
+ import { type DataloomServiceResponse, ServiceName } from '../../utils/types';
2
+ import DataloomServiceBase from '../DataloomServiceBase';
3
+ import type { SearchParams, UserResponse } from './types';
4
+ export default class UserService extends DataloomServiceBase {
5
+ serviceName: ServiceName;
6
+ search(params: SearchParams): Promise<DataloomServiceResponse<UserResponse>>;
7
+ getByIds(ids: string[] | number[]): Promise<DataloomServiceResponse<UserResponse>>;
8
+ }
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/service/services/user-service/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,uBAAuB,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC9E,OAAO,mBAAmB,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,EAEV,YAAY,EAEZ,YAAY,EACb,MAAM,SAAS,CAAC;AAEjB,MAAM,CAAC,OAAO,OAAO,WAAY,SAAQ,mBAAmB;IACjD,WAAW,cAA2B;IAEzC,MAAM,CACV,MAAM,EAAE,YAAY,GACnB,OAAO,CAAC,uBAAuB,CAAC,YAAY,CAAC,CAAC;IAqC3C,QAAQ,CACZ,GAAG,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,GACvB,OAAO,CAAC,uBAAuB,CAAC,YAAY,CAAC,CAAC;CAkClD"}
@@ -0,0 +1,60 @@
1
+ import { convertUserStatus, retrieveNameFromI18n } from "../../utils/helpers.js";
2
+ import { ServiceName } from "../../utils/types.js";
3
+ import DataloomServiceBase from "../DataloomServiceBase.js";
4
+ class UserService extends DataloomServiceBase {
5
+ async search(params) {
6
+ const { name, pageSize } = params;
7
+ const res = await this.fetchData(`/app/${this.appId}/__runtime__/api/v1/account/search_user`, {
8
+ method: 'POST',
9
+ body: {
10
+ query: name,
11
+ page_size: pageSize
12
+ }
13
+ }, {
14
+ psmName: 'UserService.Search'
15
+ });
16
+ if (res.error) return res;
17
+ return {
18
+ ...res,
19
+ data: {
20
+ user_list: res.data?.userList?.map((item)=>({
21
+ avatar: item.avatar ?? '',
22
+ email: item.email ?? '',
23
+ name: retrieveNameFromI18n(item.name) ?? '',
24
+ status: convertUserStatus(item.userStatus),
25
+ user_id: item.userID ?? ''
26
+ })) ?? []
27
+ }
28
+ };
29
+ }
30
+ async getByIds(ids) {
31
+ const res = await this.fetchData(`/app/${this.appId}/__runtime__/api/v1/account/list_users`, {
32
+ method: 'POST',
33
+ body: {
34
+ userIDList: ids
35
+ }
36
+ }, {
37
+ psmName: 'UserService.GetByIds'
38
+ });
39
+ if (res.error) return res;
40
+ return {
41
+ ...res,
42
+ data: {
43
+ user_list: ids.map((id)=>{
44
+ const item = res.data?.userInfoMap?.[id];
45
+ return {
46
+ avatar: item?.avatar?.image?.large ?? '',
47
+ email: item?.email ?? '',
48
+ name: retrieveNameFromI18n(item?.name) ?? '',
49
+ status: convertUserStatus(item?.userStatus),
50
+ user_id: item?.userID ? String(item.userID) : ''
51
+ };
52
+ })
53
+ }
54
+ };
55
+ }
56
+ constructor(...args){
57
+ super(...args), this.serviceName = ServiceName.UserService;
58
+ }
59
+ }
60
+ export { UserService as default };