@proveanything/smartlinks 1.0.12 → 1.0.13
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/dist/api/auth.d.ts +11 -0
- package/dist/api/auth.js +8 -0
- package/dist/http.d.ts +1 -0
- package/dist/http.js +62 -0
- package/package.json +1 -1
- package/src/api/auth.ts +15 -0
- package/src/http.ts +75 -0
package/dist/api/auth.d.ts
CHANGED
|
@@ -12,6 +12,12 @@ type VerifyTokenResponse = {
|
|
|
12
12
|
email?: string;
|
|
13
13
|
account?: Record<string, any>;
|
|
14
14
|
};
|
|
15
|
+
type AccountInfoResponse = {
|
|
16
|
+
user: Record<string, any>;
|
|
17
|
+
owner: Record<string, any>;
|
|
18
|
+
account: Record<string, any>;
|
|
19
|
+
location: Record<string, any>;
|
|
20
|
+
};
|
|
15
21
|
export declare namespace auth {
|
|
16
22
|
/**
|
|
17
23
|
* Login with email and password.
|
|
@@ -27,5 +33,10 @@ export declare namespace auth {
|
|
|
27
33
|
* Returns user/account info if valid.
|
|
28
34
|
*/
|
|
29
35
|
function verifyToken(token?: string): Promise<VerifyTokenResponse>;
|
|
36
|
+
/**
|
|
37
|
+
* Gets current account information for the logged in user.
|
|
38
|
+
* Returns user, owner, account, and location objects.
|
|
39
|
+
*/
|
|
40
|
+
function getAccount(): Promise<AccountInfoResponse>;
|
|
30
41
|
}
|
|
31
42
|
export {};
|
package/dist/api/auth.js
CHANGED
|
@@ -35,4 +35,12 @@ export var auth;
|
|
|
35
35
|
return result;
|
|
36
36
|
}
|
|
37
37
|
auth.verifyToken = verifyToken;
|
|
38
|
+
/**
|
|
39
|
+
* Gets current account information for the logged in user.
|
|
40
|
+
* Returns user, owner, account, and location objects.
|
|
41
|
+
*/
|
|
42
|
+
async function getAccount() {
|
|
43
|
+
return post("/public/auth/account", {});
|
|
44
|
+
}
|
|
45
|
+
auth.getAccount = getAccount;
|
|
38
46
|
})(auth || (auth = {}));
|
package/dist/http.d.ts
CHANGED
package/dist/http.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
let baseURL = null;
|
|
6
6
|
let apiKey = undefined;
|
|
7
7
|
let bearerToken = undefined;
|
|
8
|
+
let proxyMode = false;
|
|
8
9
|
/**
|
|
9
10
|
* Call this once (e.g. at app startup) to configure baseURL/auth.
|
|
10
11
|
*
|
|
@@ -16,6 +17,7 @@ export function initializeApi(options) {
|
|
|
16
17
|
baseURL = options.baseURL.replace(/\/+\$/, ""); // trim trailing slash
|
|
17
18
|
apiKey = options.apiKey;
|
|
18
19
|
bearerToken = options.bearerToken;
|
|
20
|
+
proxyMode = !!options.proxyMode;
|
|
19
21
|
}
|
|
20
22
|
/**
|
|
21
23
|
* Allows setting the bearerToken at runtime (e.g. after login/logout).
|
|
@@ -23,12 +25,60 @@ export function initializeApi(options) {
|
|
|
23
25
|
export function setBearerToken(token) {
|
|
24
26
|
bearerToken = token;
|
|
25
27
|
}
|
|
28
|
+
// Map of pending proxy requests: id -> {resolve, reject}
|
|
29
|
+
const proxyPending = {};
|
|
30
|
+
function generateProxyId() {
|
|
31
|
+
return "proxy_" + Math.random().toString(36).slice(2) + Date.now();
|
|
32
|
+
}
|
|
33
|
+
// Shared listener for proxy responses
|
|
34
|
+
function ensureProxyListener() {
|
|
35
|
+
if (window._smartlinksProxyListener)
|
|
36
|
+
return;
|
|
37
|
+
window.addEventListener("message", (event) => {
|
|
38
|
+
const msg = event.data;
|
|
39
|
+
if (!msg || !msg._smartlinksProxyResponse || !msg.id)
|
|
40
|
+
return;
|
|
41
|
+
const pending = proxyPending[msg.id];
|
|
42
|
+
if (pending) {
|
|
43
|
+
if (msg.error) {
|
|
44
|
+
pending.reject(new Error(msg.error));
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
pending.resolve(msg.data);
|
|
48
|
+
}
|
|
49
|
+
delete proxyPending[msg.id];
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
window._smartlinksProxyListener = true;
|
|
53
|
+
}
|
|
54
|
+
// Proxy request implementation
|
|
55
|
+
async function proxyRequest(method, path, body, headers, options) {
|
|
56
|
+
ensureProxyListener();
|
|
57
|
+
const id = generateProxyId();
|
|
58
|
+
const msg = {
|
|
59
|
+
_smartlinksProxyRequest: true,
|
|
60
|
+
id,
|
|
61
|
+
method,
|
|
62
|
+
path,
|
|
63
|
+
body,
|
|
64
|
+
headers,
|
|
65
|
+
options,
|
|
66
|
+
};
|
|
67
|
+
return new Promise((resolve, reject) => {
|
|
68
|
+
proxyPending[id] = { resolve, reject };
|
|
69
|
+
window.parent.postMessage(msg, "*");
|
|
70
|
+
// Optionally: add a timeout here to reject if no response
|
|
71
|
+
});
|
|
72
|
+
}
|
|
26
73
|
/**
|
|
27
74
|
* Internal helper that performs a GET request to \`\${baseURL}\${path}\`,
|
|
28
75
|
* injecting headers for apiKey or bearerToken if present.
|
|
29
76
|
* Returns the parsed JSON as T, or throws an Error.
|
|
30
77
|
*/
|
|
31
78
|
export async function request(path) {
|
|
79
|
+
if (proxyMode) {
|
|
80
|
+
return proxyRequest("GET", path);
|
|
81
|
+
}
|
|
32
82
|
if (!baseURL) {
|
|
33
83
|
throw new Error("HTTP client is not initialized. Call initializeApi(...) first.");
|
|
34
84
|
}
|
|
@@ -65,6 +115,9 @@ export async function request(path) {
|
|
|
65
115
|
* Returns the parsed JSON as T, or throws an Error.
|
|
66
116
|
*/
|
|
67
117
|
export async function post(path, body, extraHeaders) {
|
|
118
|
+
if (proxyMode) {
|
|
119
|
+
return proxyRequest("POST", path, body, extraHeaders);
|
|
120
|
+
}
|
|
68
121
|
if (!baseURL) {
|
|
69
122
|
throw new Error("HTTP client is not initialized. Call initializeApi(...) first.");
|
|
70
123
|
}
|
|
@@ -101,6 +154,9 @@ export async function post(path, body, extraHeaders) {
|
|
|
101
154
|
* Returns the parsed JSON as T, or throws an Error.
|
|
102
155
|
*/
|
|
103
156
|
export async function put(path, body, extraHeaders) {
|
|
157
|
+
if (proxyMode) {
|
|
158
|
+
return proxyRequest("PUT", path, body, extraHeaders);
|
|
159
|
+
}
|
|
104
160
|
if (!baseURL) {
|
|
105
161
|
throw new Error("HTTP client is not initialized. Call initializeApi(...) first.");
|
|
106
162
|
}
|
|
@@ -136,6 +192,9 @@ export async function put(path, body, extraHeaders) {
|
|
|
136
192
|
* Returns the parsed JSON as T, or throws an Error.
|
|
137
193
|
*/
|
|
138
194
|
export async function requestWithOptions(path, options) {
|
|
195
|
+
if (proxyMode) {
|
|
196
|
+
return proxyRequest(options.method || "GET", path, options.body, options.headers, options);
|
|
197
|
+
}
|
|
139
198
|
if (!baseURL) {
|
|
140
199
|
throw new Error("HTTP client is not initialized. Call initializeApi(...) first.");
|
|
141
200
|
}
|
|
@@ -176,6 +235,9 @@ export async function requestWithOptions(path, options) {
|
|
|
176
235
|
* Returns the parsed JSON as T, or throws an Error.
|
|
177
236
|
*/
|
|
178
237
|
export async function del(path, extraHeaders) {
|
|
238
|
+
if (proxyMode) {
|
|
239
|
+
return proxyRequest("DELETE", path, undefined, extraHeaders);
|
|
240
|
+
}
|
|
179
241
|
if (!baseURL) {
|
|
180
242
|
throw new Error("HTTP client is not initialized. Call initializeApi(...) first.");
|
|
181
243
|
}
|
package/package.json
CHANGED
package/src/api/auth.ts
CHANGED
|
@@ -16,6 +16,13 @@ type VerifyTokenResponse = {
|
|
|
16
16
|
account?: Record<string, any>
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
type AccountInfoResponse = {
|
|
20
|
+
user: Record<string, any>
|
|
21
|
+
owner: Record<string, any>
|
|
22
|
+
account: Record<string, any>
|
|
23
|
+
location: Record<string, any>
|
|
24
|
+
}
|
|
25
|
+
|
|
19
26
|
export namespace auth {
|
|
20
27
|
/**
|
|
21
28
|
* Login with email and password.
|
|
@@ -50,4 +57,12 @@ export namespace auth {
|
|
|
50
57
|
}
|
|
51
58
|
return result
|
|
52
59
|
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Gets current account information for the logged in user.
|
|
63
|
+
* Returns user, owner, account, and location objects.
|
|
64
|
+
*/
|
|
65
|
+
export async function getAccount(): Promise<AccountInfoResponse> {
|
|
66
|
+
return post<AccountInfoResponse>("/public/auth/account", {})
|
|
67
|
+
}
|
|
53
68
|
}
|
package/src/http.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
let baseURL: string | null = null
|
|
7
7
|
let apiKey: string | undefined = undefined
|
|
8
8
|
let bearerToken: string | undefined = undefined
|
|
9
|
+
let proxyMode: boolean = false
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Call this once (e.g. at app startup) to configure baseURL/auth.
|
|
@@ -18,10 +19,12 @@ export function initializeApi(options: {
|
|
|
18
19
|
baseURL: string
|
|
19
20
|
apiKey?: string
|
|
20
21
|
bearerToken?: string
|
|
22
|
+
proxyMode?: boolean
|
|
21
23
|
}): void {
|
|
22
24
|
baseURL = options.baseURL.replace(/\/+\$/, "") // trim trailing slash
|
|
23
25
|
apiKey = options.apiKey
|
|
24
26
|
bearerToken = options.bearerToken
|
|
27
|
+
proxyMode = !!options.proxyMode
|
|
25
28
|
}
|
|
26
29
|
|
|
27
30
|
/**
|
|
@@ -31,12 +34,68 @@ export function setBearerToken(token: string | undefined) {
|
|
|
31
34
|
bearerToken = token
|
|
32
35
|
}
|
|
33
36
|
|
|
37
|
+
// Map of pending proxy requests: id -> {resolve, reject}
|
|
38
|
+
const proxyPending: Record<string, { resolve: (data: any) => void, reject: (err: any) => void }> = {}
|
|
39
|
+
|
|
40
|
+
function generateProxyId(): string {
|
|
41
|
+
return "proxy_" + Math.random().toString(36).slice(2) + Date.now()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Shared listener for proxy responses
|
|
45
|
+
function ensureProxyListener() {
|
|
46
|
+
if ((window as any)._smartlinksProxyListener) return
|
|
47
|
+
window.addEventListener("message", (event) => {
|
|
48
|
+
const msg = event.data
|
|
49
|
+
if (!msg || !msg._smartlinksProxyResponse || !msg.id) return
|
|
50
|
+
const pending = proxyPending[msg.id]
|
|
51
|
+
if (pending) {
|
|
52
|
+
if (msg.error) {
|
|
53
|
+
pending.reject(new Error(msg.error))
|
|
54
|
+
} else {
|
|
55
|
+
pending.resolve(msg.data)
|
|
56
|
+
}
|
|
57
|
+
delete proxyPending[msg.id]
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
;(window as any)._smartlinksProxyListener = true
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Proxy request implementation
|
|
64
|
+
async function proxyRequest<T>(
|
|
65
|
+
method: string,
|
|
66
|
+
path: string,
|
|
67
|
+
body?: any,
|
|
68
|
+
headers?: Record<string, string>,
|
|
69
|
+
options?: RequestInit
|
|
70
|
+
): Promise<T> {
|
|
71
|
+
ensureProxyListener()
|
|
72
|
+
const id = generateProxyId()
|
|
73
|
+
const msg = {
|
|
74
|
+
_smartlinksProxyRequest: true,
|
|
75
|
+
id,
|
|
76
|
+
method,
|
|
77
|
+
path,
|
|
78
|
+
body,
|
|
79
|
+
headers,
|
|
80
|
+
options,
|
|
81
|
+
}
|
|
82
|
+
return new Promise<T>((resolve, reject) => {
|
|
83
|
+
proxyPending[id] = { resolve, reject }
|
|
84
|
+
window.parent.postMessage(msg, "*")
|
|
85
|
+
// Optionally: add a timeout here to reject if no response
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
34
89
|
/**
|
|
35
90
|
* Internal helper that performs a GET request to \`\${baseURL}\${path}\`,
|
|
36
91
|
* injecting headers for apiKey or bearerToken if present.
|
|
37
92
|
* Returns the parsed JSON as T, or throws an Error.
|
|
38
93
|
*/
|
|
39
94
|
export async function request<T>(path: string): Promise<T> {
|
|
95
|
+
if (proxyMode) {
|
|
96
|
+
return proxyRequest<T>("GET", path)
|
|
97
|
+
}
|
|
98
|
+
|
|
40
99
|
if (!baseURL) {
|
|
41
100
|
throw new Error("HTTP client is not initialized. Call initializeApi(...) first.")
|
|
42
101
|
}
|
|
@@ -81,6 +140,10 @@ export async function post<T>(
|
|
|
81
140
|
body: any,
|
|
82
141
|
extraHeaders?: Record<string, string>
|
|
83
142
|
): Promise<T> {
|
|
143
|
+
if (proxyMode) {
|
|
144
|
+
return proxyRequest<T>("POST", path, body, extraHeaders)
|
|
145
|
+
}
|
|
146
|
+
|
|
84
147
|
if (!baseURL) {
|
|
85
148
|
throw new Error("HTTP client is not initialized. Call initializeApi(...) first.")
|
|
86
149
|
}
|
|
@@ -125,6 +188,10 @@ export async function put<T>(
|
|
|
125
188
|
body: any,
|
|
126
189
|
extraHeaders?: Record<string, string>
|
|
127
190
|
): Promise<T> {
|
|
191
|
+
if (proxyMode) {
|
|
192
|
+
return proxyRequest<T>("PUT", path, body, extraHeaders)
|
|
193
|
+
}
|
|
194
|
+
|
|
128
195
|
if (!baseURL) {
|
|
129
196
|
throw new Error("HTTP client is not initialized. Call initializeApi(...) first.")
|
|
130
197
|
}
|
|
@@ -167,6 +234,10 @@ export async function requestWithOptions<T>(
|
|
|
167
234
|
path: string,
|
|
168
235
|
options: RequestInit
|
|
169
236
|
): Promise<T> {
|
|
237
|
+
if (proxyMode) {
|
|
238
|
+
return proxyRequest<T>(options.method || "GET", path, options.body, options.headers as Record<string, string>, options)
|
|
239
|
+
}
|
|
240
|
+
|
|
170
241
|
if (!baseURL) {
|
|
171
242
|
throw new Error("HTTP client is not initialized. Call initializeApi(...) first.")
|
|
172
243
|
}
|
|
@@ -221,6 +292,10 @@ export async function del<T>(
|
|
|
221
292
|
path: string,
|
|
222
293
|
extraHeaders?: Record<string, string>
|
|
223
294
|
): Promise<T> {
|
|
295
|
+
if (proxyMode) {
|
|
296
|
+
return proxyRequest<T>("DELETE", path, undefined, extraHeaders)
|
|
297
|
+
}
|
|
298
|
+
|
|
224
299
|
if (!baseURL) {
|
|
225
300
|
throw new Error("HTTP client is not initialized. Call initializeApi(...) first.")
|
|
226
301
|
}
|