@workopilot/webapp-sdk 0.1.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 ADDED
@@ -0,0 +1,66 @@
1
+ # WiseAiFrontSDK
2
+
3
+ 这个 SDK 面向挂在宿主项目里的 iframe 三方应用。
4
+
5
+ ## 初始化
6
+
7
+ ```ts
8
+ import { createWiseAiFrontSdk } from 'wiseai-front-sdk';
9
+
10
+ const sdk = createWiseAiFrontSdk({
11
+ env: 'dev',
12
+ appCode: 'third-party-mysql-demo',
13
+ javaBasePath: '',
14
+ netBasePath: '/net-api',
15
+ });
16
+
17
+ await sdk.init();
18
+ ```
19
+
20
+ ## token 规则
21
+
22
+ - 优先从本地存储读取 `accessToken`
23
+ - 读取不到时:
24
+ - `prod`:通过 `postMessage` 通知父窗口重新登录,并抛错
25
+ - `dev`:弹出登录窗,输入用户名和密码后登录换取 token
26
+
27
+ ## iframe 通讯
28
+
29
+ 当生产环境下没有 token 时,SDK 会向父窗口发送消息:
30
+
31
+ ```ts
32
+ {
33
+ source: 'wiseai-front-sdk',
34
+ type: 'WISEAI_FRONT_SDK:RELOGIN_REQUIRED',
35
+ payload: {
36
+ appCode,
37
+ env,
38
+ reason: 'missing-token'
39
+ }
40
+ }
41
+ ```
42
+
43
+ 父窗口可按自己的方式处理,比如:
44
+
45
+ - 打开宿主登录页
46
+ - 关闭当前 iframe
47
+ - 提示用户重新认证
48
+
49
+ 如需父窗口主动告诉 SDK 重新登录,也可以回传:
50
+
51
+ ```ts
52
+ {
53
+ source: 'wiseai-front-sdk-host',
54
+ type: 'WISEAI_FRONT_SDK:RELOGIN',
55
+ payload: { appCode }
56
+ }
57
+ ```
58
+
59
+ ## 导出能力
60
+
61
+ - `request`
62
+ - `get`
63
+ - `postJson`
64
+ - `uploadFile`
65
+ - `getRoutePermissions`
66
+
@@ -0,0 +1,8 @@
1
+ import type { WiseAiEnvironment } from './types';
2
+ export interface BridgeMessage {
3
+ payload?: Record<string, unknown>;
4
+ source: 'wiseai-front-sdk';
5
+ type: string;
6
+ }
7
+ export declare function sendParentMessage(parentWindow: Window | undefined, message: BridgeMessage): void;
8
+ export declare function notifyReloginRequired(parentWindow: Window | undefined, appCode: string, env: WiseAiEnvironment, reason: string): void;
package/dist/bridge.js ADDED
@@ -0,0 +1,13 @@
1
+ export function sendParentMessage(parentWindow, message) {
2
+ if (!parentWindow || typeof window === 'undefined') {
3
+ return;
4
+ }
5
+ parentWindow.postMessage(message, '*');
6
+ }
7
+ export function notifyReloginRequired(parentWindow, appCode, env, reason) {
8
+ sendParentMessage(parentWindow, {
9
+ payload: { appCode, env, reason },
10
+ source: 'wiseai-front-sdk',
11
+ type: 'WISEAI_FRONT_SDK:RELOGIN_REQUIRED',
12
+ });
13
+ }
@@ -0,0 +1,6 @@
1
+ import type { LoginCredentials, LoginModalOptions } from './types';
2
+ interface DialogConfig extends LoginModalOptions {
3
+ onSubmit: (credentials: LoginCredentials) => Promise<void>;
4
+ }
5
+ export declare function openDevLoginDialog(config: DialogConfig): Promise<LoginCredentials>;
6
+ export {};
package/dist/dialog.js ADDED
@@ -0,0 +1,100 @@
1
+ function createInput(label, type = 'text') {
2
+ const wrapper = document.createElement('label');
3
+ wrapper.style.display = 'grid';
4
+ wrapper.style.gap = '6px';
5
+ wrapper.style.fontSize = '13px';
6
+ const text = document.createElement('span');
7
+ text.textContent = label;
8
+ const input = document.createElement('input');
9
+ input.type = type;
10
+ input.style.border = '1px solid #d0d7de';
11
+ input.style.borderRadius = '8px';
12
+ input.style.padding = '10px 12px';
13
+ input.style.fontSize = '14px';
14
+ wrapper.append(text, input);
15
+ return { input, wrapper };
16
+ }
17
+ export function openDevLoginDialog(config) {
18
+ return new Promise((resolve, reject) => {
19
+ const mask = document.createElement('div');
20
+ mask.style.position = 'fixed';
21
+ mask.style.inset = '0';
22
+ mask.style.background = 'rgba(15, 23, 42, 0.48)';
23
+ mask.style.display = 'grid';
24
+ mask.style.placeItems = 'center';
25
+ mask.style.zIndex = '2147483647';
26
+ const panel = document.createElement('div');
27
+ panel.style.width = 'min(440px, calc(100vw - 32px))';
28
+ panel.style.background = '#fff';
29
+ panel.style.borderRadius = '12px';
30
+ panel.style.padding = '20px';
31
+ panel.style.boxShadow = '0 24px 80px rgba(15, 23, 42, 0.24)';
32
+ panel.style.display = 'grid';
33
+ panel.style.gap = '14px';
34
+ const title = document.createElement('div');
35
+ title.textContent = config.title ?? '登录';
36
+ title.style.fontSize = '18px';
37
+ title.style.fontWeight = '700';
38
+ const username = createInput('用户名');
39
+ const password = createInput('密码', 'password');
40
+ const captcha = config.captchaEnabled === false
41
+ ? null
42
+ : createInput(config.captchaLabel ?? '验证码');
43
+ const actions = document.createElement('div');
44
+ actions.style.display = 'flex';
45
+ actions.style.justifyContent = 'flex-end';
46
+ actions.style.gap = '8px';
47
+ const cancel = document.createElement('button');
48
+ cancel.textContent = '取消';
49
+ cancel.type = 'button';
50
+ cancel.style.border = '1px solid #d0d7de';
51
+ cancel.style.background = '#fff';
52
+ cancel.style.borderRadius = '8px';
53
+ cancel.style.padding = '8px 14px';
54
+ const submit = document.createElement('button');
55
+ submit.textContent = '登录';
56
+ submit.type = 'button';
57
+ submit.style.border = '0';
58
+ submit.style.background = '#1677ff';
59
+ submit.style.color = '#fff';
60
+ submit.style.borderRadius = '8px';
61
+ submit.style.padding = '8px 14px';
62
+ const handleClose = (error) => {
63
+ mask.remove();
64
+ if (error) {
65
+ reject(error);
66
+ }
67
+ };
68
+ cancel.addEventListener('click', () => {
69
+ handleClose(new Error('用户取消登录'));
70
+ });
71
+ submit.addEventListener('click', async () => {
72
+ submit.disabled = true;
73
+ try {
74
+ const credentials = {
75
+ username: username.input.value.trim(),
76
+ password: password.input.value,
77
+ };
78
+ if (captcha) {
79
+ credentials.captcha = captcha.input.value.trim();
80
+ }
81
+ await config.onSubmit(credentials);
82
+ handleClose();
83
+ resolve(credentials);
84
+ }
85
+ catch (error) {
86
+ submit.disabled = false;
87
+ console.error(error);
88
+ }
89
+ });
90
+ actions.append(cancel, submit);
91
+ panel.append(title, username.wrapper, password.wrapper);
92
+ if (captcha) {
93
+ panel.append(captcha.wrapper);
94
+ }
95
+ panel.append(actions);
96
+ mask.append(panel);
97
+ document.body.append(mask);
98
+ username.input.focus();
99
+ });
100
+ }
package/dist/http.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ import type { MenuPermissionNode, RequestOptions, UploadOptions, WiseAiFrontSdkOptions, WiseAiFrontSdkState } from './types';
2
+ export declare class WiseAiFrontSdk {
3
+ private readonly appCode;
4
+ private readonly env;
5
+ private readonly javaBasePath;
6
+ private readonly loginModal?;
7
+ private readonly loginPath;
8
+ private readonly netBasePath;
9
+ private readonly onReloginRequired?;
10
+ private readonly parentWindow?;
11
+ private readonly requestTimeoutMs;
12
+ private readonly storage?;
13
+ private readonly tokenKey;
14
+ private readonly tokenStorageKey;
15
+ private readonly uploadPath;
16
+ private readonly routePermissionPath;
17
+ private readonly loginRequest?;
18
+ private accessToken;
19
+ constructor(options: WiseAiFrontSdkOptions);
20
+ get state(): WiseAiFrontSdkState;
21
+ private getCurrentToken;
22
+ private ensureToken;
23
+ private loginInDev;
24
+ private openLoginModal;
25
+ init(): Promise<WiseAiFrontSdkState>;
26
+ private buildHeaders;
27
+ private resolveBase;
28
+ request<T = unknown>(path: string, options?: RequestOptions): Promise<T>;
29
+ private resolveRequestBody;
30
+ get<T = unknown>(path: string, params?: Record<string, unknown>, options?: Omit<RequestOptions, 'body' | 'method' | 'params'>): Promise<T>;
31
+ postJson<T = unknown>(path: string, body?: unknown, options?: Omit<RequestOptions, 'body' | 'method'>): Promise<T>;
32
+ uploadFile<T = unknown>(file: File | Blob, options?: UploadOptions): Promise<T>;
33
+ getRoutePermissions(): Promise<MenuPermissionNode[]>;
34
+ clearToken(): void;
35
+ }
package/dist/http.js ADDED
@@ -0,0 +1,262 @@
1
+ import { notifyReloginRequired, } from './bridge';
2
+ import { isJwtExpired, } from './jwt';
3
+ import { clearToken, persistToken, resolveTokenFromStorage, } from './storage';
4
+ import { openDevLoginDialog } from './dialog';
5
+ function normalizeBasePath(basePath = '') {
6
+ const trimmed = basePath.trim();
7
+ if (!trimmed) {
8
+ return '';
9
+ }
10
+ return trimmed.replace(/\/+$/, '');
11
+ }
12
+ function joinPath(basePath, path) {
13
+ if (/^https?:\/\//i.test(path)) {
14
+ return path;
15
+ }
16
+ const normalizedPath = path.startsWith('/') ? path : `/${path}`;
17
+ return `${normalizeBasePath(basePath)}${normalizedPath}`;
18
+ }
19
+ function toQueryString(params) {
20
+ if (!params) {
21
+ return '';
22
+ }
23
+ const search = new URLSearchParams();
24
+ Object.entries(params).forEach(([key, value]) => {
25
+ if (value === undefined || value === null) {
26
+ return;
27
+ }
28
+ if (Array.isArray(value)) {
29
+ value.forEach((item) => search.append(key, String(item)));
30
+ return;
31
+ }
32
+ search.set(key, String(value));
33
+ });
34
+ const query = search.toString();
35
+ return query ? `?${query}` : '';
36
+ }
37
+ async function parseResponse(response) {
38
+ const contentType = response.headers.get('content-type') || '';
39
+ if (contentType.includes('application/json')) {
40
+ return (await response.json());
41
+ }
42
+ if (contentType.includes('text/')) {
43
+ return (await response.text());
44
+ }
45
+ return (await response.blob());
46
+ }
47
+ export class WiseAiFrontSdk {
48
+ constructor(options) {
49
+ this.accessToken = '';
50
+ this.appCode = options.appCode;
51
+ this.env = options.env;
52
+ this.javaBasePath = normalizeBasePath(options.javaBasePath);
53
+ this.loginModal = options.loginModal;
54
+ this.loginPath = options.loginPath || '/auth/login';
55
+ this.netBasePath = normalizeBasePath(options.netBasePath || '/net-api');
56
+ this.onReloginRequired = options.onReloginRequired;
57
+ this.parentWindow = options.parentWindow ?? (typeof window !== 'undefined' ? window.parent : undefined);
58
+ this.requestTimeoutMs = options.requestTimeoutMs ?? 30000;
59
+ this.storage = options.storage;
60
+ this.tokenKey = options.tokenKey || 'accessToken';
61
+ this.tokenStorageKey = options.tokenStorageKey || 'accessToken';
62
+ this.uploadPath = options.uploadPath || '/api/file/upload';
63
+ this.routePermissionPath = options.routePermissionPath || '/app/menu/getRouters';
64
+ this.loginRequest = options.loginRequest;
65
+ }
66
+ get state() {
67
+ return {
68
+ accessToken: this.accessToken,
69
+ appCode: this.appCode,
70
+ env: this.env,
71
+ javaBasePath: this.javaBasePath,
72
+ netBasePath: this.netBasePath,
73
+ };
74
+ }
75
+ getCurrentToken() {
76
+ const token = resolveTokenFromStorage(this.storage, this.tokenKey, this.tokenStorageKey);
77
+ if (!token) {
78
+ return '';
79
+ }
80
+ if (isJwtExpired(token)) {
81
+ clearToken(this.storage, this.tokenKey, this.tokenStorageKey);
82
+ return '';
83
+ }
84
+ return token;
85
+ }
86
+ async ensureToken() {
87
+ const current = this.getCurrentToken();
88
+ if (current) {
89
+ this.accessToken = current;
90
+ return current;
91
+ }
92
+ if (this.env === 'prod') {
93
+ notifyReloginRequired(this.parentWindow, this.appCode, this.env, 'missing-token');
94
+ this.onReloginRequired?.({
95
+ appCode: this.appCode,
96
+ env: this.env,
97
+ reason: 'missing-token',
98
+ });
99
+ throw new Error('WiseAiFrontSDK: token missing in prod environment');
100
+ }
101
+ const token = await this.loginInDev();
102
+ this.accessToken = token;
103
+ return token;
104
+ }
105
+ async loginInDev() {
106
+ if (this.loginRequest) {
107
+ const result = await this.loginRequest(await this.openLoginModal());
108
+ const token = result.access_token || result.accessToken || '';
109
+ if (!token) {
110
+ throw new Error('WiseAiFrontSDK: login succeeded but no token returned');
111
+ }
112
+ persistToken(this.storage, this.tokenKey, this.tokenStorageKey, token);
113
+ return token;
114
+ }
115
+ const credentials = await this.openLoginModal();
116
+ const response = await fetch(joinPath(this.javaBasePath, this.loginPath), {
117
+ body: JSON.stringify({
118
+ password: credentials.password,
119
+ username: credentials.username,
120
+ captcha: credentials.captcha,
121
+ captchaId: credentials.captchaId,
122
+ grantType: 'password',
123
+ appCode: this.appCode,
124
+ }),
125
+ headers: {
126
+ 'Content-Type': 'application/json;charset=utf-8',
127
+ },
128
+ method: 'POST',
129
+ });
130
+ if (!response.ok) {
131
+ throw new Error(`WiseAiFrontSDK: login failed with ${response.status}`);
132
+ }
133
+ const data = (await response.json());
134
+ const token = data.access_token || data.accessToken || '';
135
+ if (!token) {
136
+ throw new Error('WiseAiFrontSDK: login response missing token');
137
+ }
138
+ persistToken(this.storage, this.tokenKey, this.tokenStorageKey, token);
139
+ return token;
140
+ }
141
+ async openLoginModal() {
142
+ return openDevLoginDialog({
143
+ captchaEnabled: this.loginModal?.captchaEnabled ?? false,
144
+ captchaLabel: this.loginModal?.captchaLabel,
145
+ title: this.loginModal?.title,
146
+ onSubmit: async () => undefined,
147
+ }).catch(() => {
148
+ throw new Error('WiseAiFrontSDK: login cancelled');
149
+ });
150
+ }
151
+ async init() {
152
+ await this.ensureToken();
153
+ return this.state;
154
+ }
155
+ buildHeaders(headers = {}, token) {
156
+ return token ? { Authorization: `Bearer ${token}`, ...headers } : headers;
157
+ }
158
+ resolveBase(base) {
159
+ if (base === 'net') {
160
+ return this.netBasePath;
161
+ }
162
+ if (base === 'absolute') {
163
+ return '';
164
+ }
165
+ return this.javaBasePath;
166
+ }
167
+ async request(path, options = {}) {
168
+ const token = await this.ensureToken();
169
+ const base = this.resolveBase(options.base);
170
+ const url = options.base === 'absolute' || /^https?:\/\//i.test(path)
171
+ ? path
172
+ : `${joinPath(base, path)}${toQueryString(options.params)}`;
173
+ const controller = new AbortController();
174
+ const timeout = window.setTimeout(() => controller.abort(), this.requestTimeoutMs);
175
+ try {
176
+ const response = await fetch(url, {
177
+ body: this.resolveRequestBody(options.body, options.bodyType),
178
+ headers: this.buildHeaders(options.headers, token),
179
+ method: options.method || 'GET',
180
+ signal: controller.signal,
181
+ });
182
+ if (!response.ok) {
183
+ throw new Error(`WiseAiFrontSDK: request failed with ${response.status}`);
184
+ }
185
+ return await parseResponse(response);
186
+ }
187
+ finally {
188
+ window.clearTimeout(timeout);
189
+ }
190
+ }
191
+ resolveRequestBody(body, bodyType) {
192
+ if (body === undefined || body === null || bodyType === undefined) {
193
+ return undefined;
194
+ }
195
+ if (bodyType === 'raw') {
196
+ return body;
197
+ }
198
+ if (bodyType === 'text') {
199
+ return String(body);
200
+ }
201
+ if (bodyType === 'form') {
202
+ const formData = new FormData();
203
+ if (body instanceof FormData) {
204
+ return body;
205
+ }
206
+ if (body && typeof body === 'object') {
207
+ Object.entries(body).forEach(([key, value]) => {
208
+ if (value === undefined || value === null) {
209
+ return;
210
+ }
211
+ formData.append(key, value instanceof Blob ? value : String(value));
212
+ });
213
+ }
214
+ return formData;
215
+ }
216
+ if (bodyType === 'json') {
217
+ return JSON.stringify(body);
218
+ }
219
+ return body;
220
+ }
221
+ async get(path, params, options = {}) {
222
+ return this.request(path, {
223
+ ...options,
224
+ method: 'GET',
225
+ params,
226
+ });
227
+ }
228
+ async postJson(path, body, options = {}) {
229
+ return this.request(path, {
230
+ ...options,
231
+ body,
232
+ bodyType: 'json',
233
+ method: 'POST',
234
+ });
235
+ }
236
+ async uploadFile(file, options = {}) {
237
+ const formData = new FormData();
238
+ formData.append(options.fileFieldName || 'file', file);
239
+ Object.entries(options.extraData || {}).forEach(([key, value]) => {
240
+ formData.append(key, value);
241
+ });
242
+ return this.request(options.path || this.uploadPath, {
243
+ body: formData,
244
+ bodyType: 'raw',
245
+ headers: options.headers,
246
+ method: 'POST',
247
+ });
248
+ }
249
+ async getRoutePermissions() {
250
+ const response = await this.get(this.routePermissionPath, undefined, {
251
+ base: 'net',
252
+ });
253
+ if (Array.isArray(response)) {
254
+ return response;
255
+ }
256
+ return response.data || response.rows || [];
257
+ }
258
+ clearToken() {
259
+ this.accessToken = '';
260
+ clearToken(this.storage, this.tokenKey, this.tokenStorageKey);
261
+ }
262
+ }
@@ -0,0 +1,7 @@
1
+ export { decodeJwtPayload, isJwtExpired } from './jwt';
2
+ export { notifyReloginRequired } from './bridge';
3
+ import { WiseAiFrontSdk } from './http';
4
+ export { persistToken, resolveTokenFromStorage } from './storage';
5
+ export type { ApiBase, HttpMethod, JwtPayload, LoginCredentials, LoginModalOptions, LoginResult, MenuPermissionNode, RequestBodyType, RequestOptions, UploadOptions, WiseAiEnvironment, WiseAiFrontSdkOptions, WiseAiFrontSdkState, } from './types';
6
+ export { WiseAiFrontSdk } from './http';
7
+ export declare function createWiseAiFrontSdk(options: import('./types').WiseAiFrontSdkOptions): WiseAiFrontSdk;
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export { decodeJwtPayload, isJwtExpired } from './jwt';
2
+ export { notifyReloginRequired } from './bridge';
3
+ import { WiseAiFrontSdk } from './http';
4
+ export { persistToken, resolveTokenFromStorage } from './storage';
5
+ export { WiseAiFrontSdk } from './http';
6
+ export function createWiseAiFrontSdk(options) {
7
+ return new WiseAiFrontSdk(options);
8
+ }
package/dist/jwt.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import type { JwtPayload } from './types';
2
+ export declare function decodeJwtPayload(token: string): JwtPayload | null;
3
+ export declare function isJwtExpired(token: string): boolean;
package/dist/jwt.js ADDED
@@ -0,0 +1,29 @@
1
+ function base64UrlDecode(value) {
2
+ const normalized = value.replace(/-/g, '+').replace(/_/g, '/');
3
+ const padded = normalized.padEnd(normalized.length + ((4 - (normalized.length % 4)) % 4), '=');
4
+ return decodeURIComponent(atob(padded)
5
+ .split('')
6
+ .map((char) => {
7
+ return `%${char.charCodeAt(0).toString(16).padStart(2, '0')}`;
8
+ })
9
+ .join(''));
10
+ }
11
+ export function decodeJwtPayload(token) {
12
+ const parts = token.split('.');
13
+ if (parts.length < 2) {
14
+ return null;
15
+ }
16
+ try {
17
+ return JSON.parse(base64UrlDecode(parts[1]));
18
+ }
19
+ catch {
20
+ return null;
21
+ }
22
+ }
23
+ export function isJwtExpired(token) {
24
+ const payload = decodeJwtPayload(token);
25
+ if (!payload?.exp) {
26
+ return false;
27
+ }
28
+ return payload.exp * 1000 <= Date.now();
29
+ }
@@ -0,0 +1,3 @@
1
+ export declare function resolveTokenFromStorage(storage: Storage | undefined, tokenKey: string, tokenStorageKey: string): string;
2
+ export declare function persistToken(storage: Storage | undefined, tokenKey: string, tokenStorageKey: string, token: string): void;
3
+ export declare function clearToken(storage: Storage | undefined, tokenKey: string, tokenStorageKey: string): void;
@@ -0,0 +1,50 @@
1
+ const ACCESS_TOKEN_KEY = 'accessToken';
2
+ const REFRESH_TOKEN_KEY = 'refreshToken';
3
+ function getSafeStorage(storage) {
4
+ if (storage) {
5
+ return storage;
6
+ }
7
+ if (typeof window === 'undefined') {
8
+ return null;
9
+ }
10
+ return window.localStorage;
11
+ }
12
+ export function resolveTokenFromStorage(storage, tokenKey, tokenStorageKey) {
13
+ const safeStorage = getSafeStorage(storage);
14
+ if (!safeStorage) {
15
+ return '';
16
+ }
17
+ return (safeStorage.getItem(tokenStorageKey) ||
18
+ safeStorage.getItem(tokenKey) ||
19
+ safeStorage.getItem(`${tokenStorageKey}:${ACCESS_TOKEN_KEY}`) ||
20
+ safeStorage.getItem(`${tokenKey}:${ACCESS_TOKEN_KEY}`) ||
21
+ safeStorage.getItem(`${tokenStorageKey}:${REFRESH_TOKEN_KEY}`) ||
22
+ safeStorage.getItem(`${tokenKey}:${REFRESH_TOKEN_KEY}`) ||
23
+ '');
24
+ }
25
+ export function persistToken(storage, tokenKey, tokenStorageKey, token) {
26
+ const safeStorage = getSafeStorage(storage);
27
+ if (!safeStorage) {
28
+ return;
29
+ }
30
+ safeStorage.setItem(tokenStorageKey, token);
31
+ safeStorage.setItem(tokenKey, token);
32
+ safeStorage.setItem(`${tokenStorageKey}:${ACCESS_TOKEN_KEY}`, token);
33
+ safeStorage.setItem(`${tokenKey}:${ACCESS_TOKEN_KEY}`, token);
34
+ safeStorage.setItem(`${tokenStorageKey}:${REFRESH_TOKEN_KEY}`, token);
35
+ safeStorage.setItem(`${tokenKey}:${REFRESH_TOKEN_KEY}`, token);
36
+ }
37
+ export function clearToken(storage, tokenKey, tokenStorageKey) {
38
+ const safeStorage = getSafeStorage(storage);
39
+ if (!safeStorage) {
40
+ return;
41
+ }
42
+ [
43
+ tokenStorageKey,
44
+ tokenKey,
45
+ `${tokenStorageKey}:accessToken`,
46
+ `${tokenKey}:accessToken`,
47
+ `${tokenStorageKey}:refreshToken`,
48
+ `${tokenKey}:refreshToken`,
49
+ ].forEach((item) => safeStorage.removeItem(item));
50
+ }
@@ -0,0 +1,77 @@
1
+ export type WiseAiEnvironment = 'dev' | 'prod';
2
+ export type HttpMethod = 'DELETE' | 'GET' | 'PATCH' | 'POST' | 'PUT';
3
+ export type RequestBodyType = 'form' | 'json' | 'raw' | 'text';
4
+ export type ApiBase = 'java' | 'net' | 'absolute';
5
+ export interface LoginCredentials {
6
+ captcha?: string;
7
+ captchaId?: string;
8
+ password: string;
9
+ username: string;
10
+ }
11
+ export interface LoginResult {
12
+ access_token?: string;
13
+ accessToken?: string;
14
+ refresh_token?: string;
15
+ refreshToken?: string;
16
+ }
17
+ export interface JwtPayload {
18
+ exp?: number;
19
+ iat?: number;
20
+ [key: string]: unknown;
21
+ }
22
+ export interface MenuPermissionNode {
23
+ children?: MenuPermissionNode[];
24
+ hidden?: boolean;
25
+ icon?: string;
26
+ name?: string;
27
+ path: string;
28
+ title?: string;
29
+ }
30
+ export interface RequestOptions {
31
+ base?: ApiBase;
32
+ body?: unknown;
33
+ bodyType?: RequestBodyType;
34
+ headers?: Record<string, string>;
35
+ method?: HttpMethod;
36
+ params?: Record<string, unknown>;
37
+ responseType?: 'arrayBuffer' | 'blob' | 'json' | 'text';
38
+ }
39
+ export interface UploadOptions {
40
+ extraData?: Record<string, string | Blob>;
41
+ fileFieldName?: string;
42
+ headers?: Record<string, string>;
43
+ path?: string;
44
+ }
45
+ export interface LoginModalOptions {
46
+ captchaEnabled?: boolean;
47
+ captchaLabel?: string;
48
+ title?: string;
49
+ }
50
+ export interface WiseAiFrontSdkOptions {
51
+ appCode: string;
52
+ env: WiseAiEnvironment;
53
+ javaBasePath?: string;
54
+ loginModal?: LoginModalOptions;
55
+ loginPath?: string;
56
+ netBasePath?: string;
57
+ onReloginRequired?: (payload: {
58
+ appCode: string;
59
+ env: WiseAiEnvironment;
60
+ reason: string;
61
+ }) => void;
62
+ parentWindow?: Window;
63
+ requestTimeoutMs?: number;
64
+ storage?: Storage;
65
+ tokenKey?: string;
66
+ tokenStorageKey?: string;
67
+ uploadPath?: string;
68
+ routePermissionPath?: string;
69
+ loginRequest?: (credentials: LoginCredentials) => Promise<LoginResult>;
70
+ }
71
+ export interface WiseAiFrontSdkState {
72
+ accessToken: string;
73
+ appCode: string;
74
+ env: WiseAiEnvironment;
75
+ javaBasePath: string;
76
+ netBasePath: string;
77
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@workopilot/webapp-sdk",
3
+ "version": "0.1.0",
4
+ "description": "WiseAI front-end SDK for iframe-hosted third-party apps",
5
+ "keywords":"wiseai,workopilot",
6
+ "author":"workopilot",
7
+ "license": "MIT",
8
+ "type": "module",
9
+ "main": "dist/index.js",
10
+ "module": "dist/index.js",
11
+ "types": "dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "README.md"
21
+ ],
22
+ "scripts": {
23
+ "build": "tsc -p tsconfig.json"
24
+ },
25
+ "devDependencies": {
26
+ "typescript": "^5.7.2"
27
+ }
28
+ }
29
+