ai-app-client 0.1.0 → 0.1.2
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 +1 -0
- package/dist/index.cjs +148 -0
- package/dist/index.d.cts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +146 -0
- package/package.json +46 -6
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
AI App Client
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// src/auth/index.ts
|
|
4
|
+
var import_supabase_js = require("@supabase/supabase-js");
|
|
5
|
+
|
|
6
|
+
// src/auth/injectRequest.ts
|
|
7
|
+
function injectFetch() {
|
|
8
|
+
const originalFetch = window.fetch;
|
|
9
|
+
window.fetch = async (...args) => {
|
|
10
|
+
const response = await originalFetch(...args);
|
|
11
|
+
if (response.status === 401) {
|
|
12
|
+
const session = await window.supabase?.auth?.getSession?.();
|
|
13
|
+
if (!session?.data?.session) {
|
|
14
|
+
window.AIApp.Auth.login();
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return response;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function injectXMLHttpRequest() {
|
|
21
|
+
const OriginalXMLHttpRequest = window.XMLHttpRequest;
|
|
22
|
+
class InterceptedXMLHttpRequest extends OriginalXMLHttpRequest {
|
|
23
|
+
constructor() {
|
|
24
|
+
super();
|
|
25
|
+
this._initInterceptor();
|
|
26
|
+
}
|
|
27
|
+
_initInterceptor() {
|
|
28
|
+
this.addEventListener("readystatechange", () => {
|
|
29
|
+
if (this.readyState === 4 && this.status === 401) {
|
|
30
|
+
this._handleUnauthorized();
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
async _handleUnauthorized() {
|
|
35
|
+
try {
|
|
36
|
+
const session = await window.supabase?.auth?.getSession?.();
|
|
37
|
+
if (!session?.data?.session) {
|
|
38
|
+
window.AIApp.Auth.login();
|
|
39
|
+
}
|
|
40
|
+
} catch (error) {
|
|
41
|
+
console.error("Failed to check session during 401 handling:", error);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
window.XMLHttpRequest = InterceptedXMLHttpRequest;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/utils/cookies.ts
|
|
49
|
+
function setCookie(name, value, days) {
|
|
50
|
+
const date = /* @__PURE__ */ new Date();
|
|
51
|
+
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1e3);
|
|
52
|
+
const expires = "expires=" + date.toUTCString();
|
|
53
|
+
document.cookie = name + "=" + value + ";" + expires + ";path=/";
|
|
54
|
+
}
|
|
55
|
+
function deleteCookie(name) {
|
|
56
|
+
document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// src/utils/url.ts
|
|
60
|
+
function setUrlSearchParamsState(key, value) {
|
|
61
|
+
const url = new URL(window.location.href);
|
|
62
|
+
let changed = false;
|
|
63
|
+
if (value === void 0 || value === null) {
|
|
64
|
+
url.searchParams.delete(key);
|
|
65
|
+
changed = true;
|
|
66
|
+
} else {
|
|
67
|
+
url.searchParams.set(key, value);
|
|
68
|
+
changed = true;
|
|
69
|
+
}
|
|
70
|
+
if (changed) {
|
|
71
|
+
window.history.replaceState({}, "", url.toString());
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/auth/tryLoginFromUrl.ts
|
|
78
|
+
function tryLoginFromUrl() {
|
|
79
|
+
const access_token = window.__SUPABASE_ACCESS_TOKEN__ || "";
|
|
80
|
+
const refresh_token = window.__SUPABASE_REFRESH_TOKEN__ || "";
|
|
81
|
+
if (access_token) {
|
|
82
|
+
setUrlSearchParamsState("access_token", void 0);
|
|
83
|
+
}
|
|
84
|
+
if (refresh_token) {
|
|
85
|
+
setUrlSearchParamsState("refresh_token", void 0);
|
|
86
|
+
}
|
|
87
|
+
if (!window.supabase) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (access_token && refresh_token) {
|
|
91
|
+
window.supabase.auth.setSession({ access_token, refresh_token });
|
|
92
|
+
}
|
|
93
|
+
window.supabase.auth.onAuthStateChange((event, session) => {
|
|
94
|
+
const { access_token: access_token2, user } = session || {};
|
|
95
|
+
const { user_metadata } = user || {};
|
|
96
|
+
const { corp_id, emp_id } = user_metadata || {};
|
|
97
|
+
switch (event) {
|
|
98
|
+
case "INITIAL_SESSION":
|
|
99
|
+
break;
|
|
100
|
+
case "SIGNED_IN":
|
|
101
|
+
case "TOKEN_REFRESHED":
|
|
102
|
+
case "USER_UPDATED":
|
|
103
|
+
setCookie("access_token", access_token2, 30);
|
|
104
|
+
setCookie("user_id", emp_id, 30);
|
|
105
|
+
setCookie("corp_id", corp_id, 30);
|
|
106
|
+
break;
|
|
107
|
+
case "PASSWORD_RECOVERY":
|
|
108
|
+
break;
|
|
109
|
+
case "SIGNED_OUT":
|
|
110
|
+
deleteCookie("access_token");
|
|
111
|
+
deleteCookie("user_id");
|
|
112
|
+
deleteCookie("corp_id");
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// src/auth/index.ts
|
|
119
|
+
var AIAppUrl = window.__AI_APP_URL__ || "https://ai-app.dingtalk.com";
|
|
120
|
+
function createAuthTools() {
|
|
121
|
+
const supabaseAnonKey = window.__SUPABASE_ANON_KEY__ || "";
|
|
122
|
+
let supabase = null;
|
|
123
|
+
if (supabaseAnonKey) {
|
|
124
|
+
supabase = (0, import_supabase_js.createClient)(window.location.origin, supabaseAnonKey);
|
|
125
|
+
window.supabase = supabase;
|
|
126
|
+
}
|
|
127
|
+
window.AIApp.Auth = {
|
|
128
|
+
...supabase?.auth || {},
|
|
129
|
+
/**
|
|
130
|
+
* 登录
|
|
131
|
+
*/
|
|
132
|
+
login() {
|
|
133
|
+
const url = new URL(`${AIAppUrl}${AIAppUrl.endsWith("/") ? "" : "/"}oauth/dingtalk-login`);
|
|
134
|
+
url.searchParams.set("continue_url", window.location.href);
|
|
135
|
+
window.location.href = url.toString();
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
if (window.supabase) {
|
|
139
|
+
injectFetch();
|
|
140
|
+
injectXMLHttpRequest();
|
|
141
|
+
}
|
|
142
|
+
tryLoginFromUrl();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// src/index.ts
|
|
146
|
+
if (typeof window !== "undefined") {
|
|
147
|
+
createAuthTools();
|
|
148
|
+
}
|
package/dist/index.d.cts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// src/auth/index.ts
|
|
2
|
+
import { createClient } from "@supabase/supabase-js";
|
|
3
|
+
|
|
4
|
+
// src/auth/injectRequest.ts
|
|
5
|
+
function injectFetch() {
|
|
6
|
+
const originalFetch = window.fetch;
|
|
7
|
+
window.fetch = async (...args) => {
|
|
8
|
+
const response = await originalFetch(...args);
|
|
9
|
+
if (response.status === 401) {
|
|
10
|
+
const session = await window.supabase?.auth?.getSession?.();
|
|
11
|
+
if (!session?.data?.session) {
|
|
12
|
+
window.AIApp.Auth.login();
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return response;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function injectXMLHttpRequest() {
|
|
19
|
+
const OriginalXMLHttpRequest = window.XMLHttpRequest;
|
|
20
|
+
class InterceptedXMLHttpRequest extends OriginalXMLHttpRequest {
|
|
21
|
+
constructor() {
|
|
22
|
+
super();
|
|
23
|
+
this._initInterceptor();
|
|
24
|
+
}
|
|
25
|
+
_initInterceptor() {
|
|
26
|
+
this.addEventListener("readystatechange", () => {
|
|
27
|
+
if (this.readyState === 4 && this.status === 401) {
|
|
28
|
+
this._handleUnauthorized();
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
async _handleUnauthorized() {
|
|
33
|
+
try {
|
|
34
|
+
const session = await window.supabase?.auth?.getSession?.();
|
|
35
|
+
if (!session?.data?.session) {
|
|
36
|
+
window.AIApp.Auth.login();
|
|
37
|
+
}
|
|
38
|
+
} catch (error) {
|
|
39
|
+
console.error("Failed to check session during 401 handling:", error);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
window.XMLHttpRequest = InterceptedXMLHttpRequest;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/utils/cookies.ts
|
|
47
|
+
function setCookie(name, value, days) {
|
|
48
|
+
const date = /* @__PURE__ */ new Date();
|
|
49
|
+
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1e3);
|
|
50
|
+
const expires = "expires=" + date.toUTCString();
|
|
51
|
+
document.cookie = name + "=" + value + ";" + expires + ";path=/";
|
|
52
|
+
}
|
|
53
|
+
function deleteCookie(name) {
|
|
54
|
+
document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/utils/url.ts
|
|
58
|
+
function setUrlSearchParamsState(key, value) {
|
|
59
|
+
const url = new URL(window.location.href);
|
|
60
|
+
let changed = false;
|
|
61
|
+
if (value === void 0 || value === null) {
|
|
62
|
+
url.searchParams.delete(key);
|
|
63
|
+
changed = true;
|
|
64
|
+
} else {
|
|
65
|
+
url.searchParams.set(key, value);
|
|
66
|
+
changed = true;
|
|
67
|
+
}
|
|
68
|
+
if (changed) {
|
|
69
|
+
window.history.replaceState({}, "", url.toString());
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/auth/tryLoginFromUrl.ts
|
|
76
|
+
function tryLoginFromUrl() {
|
|
77
|
+
const access_token = window.__SUPABASE_ACCESS_TOKEN__ || "";
|
|
78
|
+
const refresh_token = window.__SUPABASE_REFRESH_TOKEN__ || "";
|
|
79
|
+
if (access_token) {
|
|
80
|
+
setUrlSearchParamsState("access_token", void 0);
|
|
81
|
+
}
|
|
82
|
+
if (refresh_token) {
|
|
83
|
+
setUrlSearchParamsState("refresh_token", void 0);
|
|
84
|
+
}
|
|
85
|
+
if (!window.supabase) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (access_token && refresh_token) {
|
|
89
|
+
window.supabase.auth.setSession({ access_token, refresh_token });
|
|
90
|
+
}
|
|
91
|
+
window.supabase.auth.onAuthStateChange((event, session) => {
|
|
92
|
+
const { access_token: access_token2, user } = session || {};
|
|
93
|
+
const { user_metadata } = user || {};
|
|
94
|
+
const { corp_id, emp_id } = user_metadata || {};
|
|
95
|
+
switch (event) {
|
|
96
|
+
case "INITIAL_SESSION":
|
|
97
|
+
break;
|
|
98
|
+
case "SIGNED_IN":
|
|
99
|
+
case "TOKEN_REFRESHED":
|
|
100
|
+
case "USER_UPDATED":
|
|
101
|
+
setCookie("access_token", access_token2, 30);
|
|
102
|
+
setCookie("user_id", emp_id, 30);
|
|
103
|
+
setCookie("corp_id", corp_id, 30);
|
|
104
|
+
break;
|
|
105
|
+
case "PASSWORD_RECOVERY":
|
|
106
|
+
break;
|
|
107
|
+
case "SIGNED_OUT":
|
|
108
|
+
deleteCookie("access_token");
|
|
109
|
+
deleteCookie("user_id");
|
|
110
|
+
deleteCookie("corp_id");
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// src/auth/index.ts
|
|
117
|
+
var AIAppUrl = window.__AI_APP_URL__ || "https://ai-app.dingtalk.com";
|
|
118
|
+
function createAuthTools() {
|
|
119
|
+
const supabaseAnonKey = window.__SUPABASE_ANON_KEY__ || "";
|
|
120
|
+
let supabase = null;
|
|
121
|
+
if (supabaseAnonKey) {
|
|
122
|
+
supabase = createClient(window.location.origin, supabaseAnonKey);
|
|
123
|
+
window.supabase = supabase;
|
|
124
|
+
}
|
|
125
|
+
window.AIApp.Auth = {
|
|
126
|
+
...supabase?.auth || {},
|
|
127
|
+
/**
|
|
128
|
+
* 登录
|
|
129
|
+
*/
|
|
130
|
+
login() {
|
|
131
|
+
const url = new URL(`${AIAppUrl}${AIAppUrl.endsWith("/") ? "" : "/"}oauth/dingtalk-login`);
|
|
132
|
+
url.searchParams.set("continue_url", window.location.href);
|
|
133
|
+
window.location.href = url.toString();
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
if (window.supabase) {
|
|
137
|
+
injectFetch();
|
|
138
|
+
injectXMLHttpRequest();
|
|
139
|
+
}
|
|
140
|
+
tryLoginFromUrl();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// src/index.ts
|
|
144
|
+
if (typeof window !== "undefined") {
|
|
145
|
+
createAuthTools();
|
|
146
|
+
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-app-client",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "AI App Client",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
5
24
|
"scripts": {
|
|
25
|
+
"build": "tsup",
|
|
26
|
+
"dev": "tsup --watch",
|
|
27
|
+
"prepublishOnly": "pnpm build",
|
|
28
|
+
"typecheck": "tsc --noEmit",
|
|
6
29
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
7
30
|
},
|
|
8
|
-
"author": "",
|
|
31
|
+
"author": "AI App",
|
|
9
32
|
"license": "ISC",
|
|
10
|
-
"description": "",
|
|
11
33
|
"publishConfig": {
|
|
12
|
-
"registry": "https://registry.npmjs.org"
|
|
13
|
-
|
|
34
|
+
"registry": "https://registry.npmjs.org",
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@supabase/supabase-js": "^2.99.2"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"tsup": "^8.5.1",
|
|
42
|
+
"typescript": "^5.9.3"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=18"
|
|
46
|
+
},
|
|
47
|
+
"keywords": [
|
|
48
|
+
"ai",
|
|
49
|
+
"app",
|
|
50
|
+
"client",
|
|
51
|
+
"supabase",
|
|
52
|
+
"auth"
|
|
53
|
+
]
|
|
14
54
|
}
|