@proveanything/smartlinks 1.0.11 → 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 +42 -0
- package/dist/api/auth.js +46 -0
- package/dist/api/index.d.ts +1 -0
- package/dist/api/index.js +1 -0
- package/dist/http.d.ts +5 -0
- package/dist/http.js +68 -0
- package/dist/types/attestation.d.ts +6 -5
- package/package.json +1 -1
- package/src/api/auth.ts +41 -1
- package/src/http.ts +75 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
type LoginResponse = {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
email: string;
|
|
5
|
+
bearerToken: string;
|
|
6
|
+
account: Record<string, any>;
|
|
7
|
+
};
|
|
8
|
+
type VerifyTokenResponse = {
|
|
9
|
+
valid: boolean;
|
|
10
|
+
id?: string;
|
|
11
|
+
name?: string;
|
|
12
|
+
email?: string;
|
|
13
|
+
account?: Record<string, any>;
|
|
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
|
+
};
|
|
21
|
+
export declare namespace auth {
|
|
22
|
+
/**
|
|
23
|
+
* Login with email and password.
|
|
24
|
+
* Sets the bearerToken for subsequent API calls.
|
|
25
|
+
*/
|
|
26
|
+
function login(email: string, password: string): Promise<LoginResponse>;
|
|
27
|
+
/**
|
|
28
|
+
* Logout (clears bearerToken for future API calls).
|
|
29
|
+
*/
|
|
30
|
+
function logout(): void;
|
|
31
|
+
/**
|
|
32
|
+
* Verifies the current bearerToken (or a provided token).
|
|
33
|
+
* Returns user/account info if valid.
|
|
34
|
+
*/
|
|
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>;
|
|
41
|
+
}
|
|
42
|
+
export {};
|
package/dist/api/auth.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { post, setBearerToken, getApiHeaders } from "../http";
|
|
2
|
+
export var auth;
|
|
3
|
+
(function (auth) {
|
|
4
|
+
/**
|
|
5
|
+
* Login with email and password.
|
|
6
|
+
* Sets the bearerToken for subsequent API calls.
|
|
7
|
+
*/
|
|
8
|
+
async function login(email, password) {
|
|
9
|
+
const res = await post("/public/auth/login", { email, password });
|
|
10
|
+
setBearerToken(res.bearerToken);
|
|
11
|
+
return res;
|
|
12
|
+
}
|
|
13
|
+
auth.login = login;
|
|
14
|
+
/**
|
|
15
|
+
* Logout (clears bearerToken for future API calls).
|
|
16
|
+
*/
|
|
17
|
+
function logout() {
|
|
18
|
+
setBearerToken(undefined);
|
|
19
|
+
}
|
|
20
|
+
auth.logout = logout;
|
|
21
|
+
/**
|
|
22
|
+
* Verifies the current bearerToken (or a provided token).
|
|
23
|
+
* Returns user/account info if valid.
|
|
24
|
+
*/
|
|
25
|
+
async function verifyToken(token) {
|
|
26
|
+
// Use the provided token, or the one from getApiHeaders
|
|
27
|
+
const headers = Object.assign({}, getApiHeaders());
|
|
28
|
+
if (token) {
|
|
29
|
+
headers["AUTHORIZATION"] = `Bearer ${token}`;
|
|
30
|
+
}
|
|
31
|
+
const result = await post("/public/auth/verify", {}, headers);
|
|
32
|
+
if (token && result.valid) {
|
|
33
|
+
setBearerToken(token);
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
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;
|
|
46
|
+
})(auth || (auth = {}));
|
package/dist/api/index.d.ts
CHANGED
package/dist/api/index.js
CHANGED
package/dist/http.d.ts
CHANGED
|
@@ -9,7 +9,12 @@ export declare function initializeApi(options: {
|
|
|
9
9
|
baseURL: string;
|
|
10
10
|
apiKey?: string;
|
|
11
11
|
bearerToken?: string;
|
|
12
|
+
proxyMode?: boolean;
|
|
12
13
|
}): void;
|
|
14
|
+
/**
|
|
15
|
+
* Allows setting the bearerToken at runtime (e.g. after login/logout).
|
|
16
|
+
*/
|
|
17
|
+
export declare function setBearerToken(token: string | undefined): void;
|
|
13
18
|
/**
|
|
14
19
|
* Internal helper that performs a GET request to \`\${baseURL}\${path}\`,
|
|
15
20
|
* injecting headers for apiKey or bearerToken if present.
|
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,58 @@ 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;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Allows setting the bearerToken at runtime (e.g. after login/logout).
|
|
24
|
+
*/
|
|
25
|
+
export function setBearerToken(token) {
|
|
26
|
+
bearerToken = token;
|
|
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
|
+
});
|
|
19
72
|
}
|
|
20
73
|
/**
|
|
21
74
|
* Internal helper that performs a GET request to \`\${baseURL}\${path}\`,
|
|
@@ -23,6 +76,9 @@ export function initializeApi(options) {
|
|
|
23
76
|
* Returns the parsed JSON as T, or throws an Error.
|
|
24
77
|
*/
|
|
25
78
|
export async function request(path) {
|
|
79
|
+
if (proxyMode) {
|
|
80
|
+
return proxyRequest("GET", path);
|
|
81
|
+
}
|
|
26
82
|
if (!baseURL) {
|
|
27
83
|
throw new Error("HTTP client is not initialized. Call initializeApi(...) first.");
|
|
28
84
|
}
|
|
@@ -59,6 +115,9 @@ export async function request(path) {
|
|
|
59
115
|
* Returns the parsed JSON as T, or throws an Error.
|
|
60
116
|
*/
|
|
61
117
|
export async function post(path, body, extraHeaders) {
|
|
118
|
+
if (proxyMode) {
|
|
119
|
+
return proxyRequest("POST", path, body, extraHeaders);
|
|
120
|
+
}
|
|
62
121
|
if (!baseURL) {
|
|
63
122
|
throw new Error("HTTP client is not initialized. Call initializeApi(...) first.");
|
|
64
123
|
}
|
|
@@ -95,6 +154,9 @@ export async function post(path, body, extraHeaders) {
|
|
|
95
154
|
* Returns the parsed JSON as T, or throws an Error.
|
|
96
155
|
*/
|
|
97
156
|
export async function put(path, body, extraHeaders) {
|
|
157
|
+
if (proxyMode) {
|
|
158
|
+
return proxyRequest("PUT", path, body, extraHeaders);
|
|
159
|
+
}
|
|
98
160
|
if (!baseURL) {
|
|
99
161
|
throw new Error("HTTP client is not initialized. Call initializeApi(...) first.");
|
|
100
162
|
}
|
|
@@ -130,6 +192,9 @@ export async function put(path, body, extraHeaders) {
|
|
|
130
192
|
* Returns the parsed JSON as T, or throws an Error.
|
|
131
193
|
*/
|
|
132
194
|
export async function requestWithOptions(path, options) {
|
|
195
|
+
if (proxyMode) {
|
|
196
|
+
return proxyRequest(options.method || "GET", path, options.body, options.headers, options);
|
|
197
|
+
}
|
|
133
198
|
if (!baseURL) {
|
|
134
199
|
throw new Error("HTTP client is not initialized. Call initializeApi(...) first.");
|
|
135
200
|
}
|
|
@@ -170,6 +235,9 @@ export async function requestWithOptions(path, options) {
|
|
|
170
235
|
* Returns the parsed JSON as T, or throws an Error.
|
|
171
236
|
*/
|
|
172
237
|
export async function del(path, extraHeaders) {
|
|
238
|
+
if (proxyMode) {
|
|
239
|
+
return proxyRequest("DELETE", path, undefined, extraHeaders);
|
|
240
|
+
}
|
|
173
241
|
if (!baseURL) {
|
|
174
242
|
throw new Error("HTTP client is not initialized. Call initializeApi(...) first.");
|
|
175
243
|
}
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
export interface AttestationResponse {
|
|
2
2
|
id: string;
|
|
3
|
-
proofId: string;
|
|
4
3
|
createdAt: string;
|
|
5
4
|
updatedAt: string;
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
public: Record<string, any>;
|
|
6
|
+
private: Record<string, any>;
|
|
7
|
+
proof: Record<string, any>;
|
|
8
8
|
}
|
|
9
9
|
export interface AttestationCreateRequest {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
public: Record<string, any>;
|
|
11
|
+
private: Record<string, any>;
|
|
12
|
+
proof: Record<string, any>;
|
|
12
13
|
}
|
|
13
14
|
export interface AttestationUpdateRequest {
|
|
14
15
|
type?: string;
|
package/package.json
CHANGED
package/src/api/auth.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { post, setBearerToken } from "../http"
|
|
1
|
+
import { post, setBearerToken, getApiHeaders } from "../http"
|
|
2
2
|
|
|
3
3
|
type LoginResponse = {
|
|
4
4
|
id: string
|
|
@@ -8,6 +8,21 @@ type LoginResponse = {
|
|
|
8
8
|
account: Record<string, any>
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
type VerifyTokenResponse = {
|
|
12
|
+
valid: boolean
|
|
13
|
+
id?: string
|
|
14
|
+
name?: string
|
|
15
|
+
email?: string
|
|
16
|
+
account?: Record<string, any>
|
|
17
|
+
}
|
|
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
|
+
|
|
11
26
|
export namespace auth {
|
|
12
27
|
/**
|
|
13
28
|
* Login with email and password.
|
|
@@ -25,4 +40,29 @@ export namespace auth {
|
|
|
25
40
|
export function logout(): void {
|
|
26
41
|
setBearerToken(undefined)
|
|
27
42
|
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Verifies the current bearerToken (or a provided token).
|
|
46
|
+
* Returns user/account info if valid.
|
|
47
|
+
*/
|
|
48
|
+
export async function verifyToken(token?: string): Promise<VerifyTokenResponse> {
|
|
49
|
+
// Use the provided token, or the one from getApiHeaders
|
|
50
|
+
const headers = { ...getApiHeaders() }
|
|
51
|
+
if (token) {
|
|
52
|
+
headers["AUTHORIZATION"] = `Bearer ${token}`
|
|
53
|
+
}
|
|
54
|
+
const result = await post<VerifyTokenResponse>("/public/auth/verify", {}, headers)
|
|
55
|
+
if (token && result.valid) {
|
|
56
|
+
setBearerToken(token)
|
|
57
|
+
}
|
|
58
|
+
return result
|
|
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
|
+
}
|
|
28
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
|
}
|