@grantjs/client 1.0.0 → 1.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 +6 -5
- package/dist/grant-client-BuvFCpqW.js +249 -0
- package/dist/grant-client-BuvFCpqW.js.map +1 -0
- package/dist/grant-client-COFg3pj8.cjs +254 -0
- package/dist/grant-client-COFg3pj8.cjs.map +1 -0
- package/dist/grant-client.d.ts +2 -1
- package/dist/grant-client.d.ts.map +1 -1
- package/dist/index.cjs +2 -227
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +2 -228
- package/dist/react/context.d.ts.map +1 -1
- package/dist/react/hooks/useGrant.d.ts.map +1 -1
- package/dist/react/index.d.ts +4 -4
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react.cjs +232 -122
- package/dist/react.cjs.map +1 -1
- package/dist/react.mjs +231 -127
- package/dist/react.mjs.map +1 -1
- package/dist/types.d.ts +6 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +17 -17
- package/dist/index.cjs.map +0 -1
- package/dist/index.mjs.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,228 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
constructor(config) {
|
|
9
|
-
__publicField(this, "config");
|
|
10
|
-
__publicField(this, "cache", /* @__PURE__ */ new Map());
|
|
11
|
-
__publicField(this, "defaultTtl");
|
|
12
|
-
this.config = config;
|
|
13
|
-
this.defaultTtl = config.cache?.ttl ?? 5 * 60 * 1e3;
|
|
14
|
-
}
|
|
15
|
-
// ============================================================================
|
|
16
|
-
// Public API - Permission Checks
|
|
17
|
-
// ============================================================================
|
|
18
|
-
/**
|
|
19
|
-
* Check if the current user has a specific permission
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* ```ts
|
|
23
|
-
* const canEdit = await grant.can('document', 'update');
|
|
24
|
-
* if (canEdit) {
|
|
25
|
-
* // Show edit button
|
|
26
|
-
* }
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
async can(resource, action, options) {
|
|
30
|
-
const result = await this.isAuthorized(resource, action, options);
|
|
31
|
-
return result.authorized;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Alias for `can` - check if user has permission
|
|
35
|
-
*/
|
|
36
|
-
async hasPermission(resource, action, options) {
|
|
37
|
-
return this.can(resource, action, options);
|
|
38
|
-
}
|
|
39
|
-
// ============================================================================
|
|
40
|
-
// Public API - Project OAuth (sign-in with project app)
|
|
41
|
-
// ============================================================================
|
|
42
|
-
/**
|
|
43
|
-
* Start project-app OAuth flow (redirect only).
|
|
44
|
-
* Navigates the current window to the Grant OAuth entry page; after sign-in and consent,
|
|
45
|
-
* the user is redirected to the app's `redirect_uri` with token in the URL fragment.
|
|
46
|
-
*
|
|
47
|
-
* Requires `config.frontendUrl` and `redirectUri`.
|
|
48
|
-
*/
|
|
49
|
-
async signInWithProjectApp(options) {
|
|
50
|
-
const frontendUrl = this.config.frontendUrl;
|
|
51
|
-
if (!frontendUrl) {
|
|
52
|
-
throw new Error("GrantClient: frontendUrl is required for signInWithProjectApp");
|
|
53
|
-
}
|
|
54
|
-
const locale = options.locale ?? "en";
|
|
55
|
-
const redirectUri = options.redirectUri;
|
|
56
|
-
if (!redirectUri) {
|
|
57
|
-
throw new Error("redirectUri is required for signInWithProjectApp");
|
|
58
|
-
}
|
|
59
|
-
const entryPath = `/${locale}/auth/project`;
|
|
60
|
-
const params = new URLSearchParams({
|
|
61
|
-
client_id: options.clientId,
|
|
62
|
-
redirect_uri: redirectUri,
|
|
63
|
-
state: options.state ?? ""
|
|
64
|
-
});
|
|
65
|
-
if (options.scope) params.set("scope", options.scope);
|
|
66
|
-
const entryUrl = `${frontendUrl.replace(/\/$/, "")}${entryPath}?${params.toString()}`;
|
|
67
|
-
if (typeof window !== "undefined") {
|
|
68
|
-
window.location.href = entryUrl;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Check authorization with full result details
|
|
73
|
-
*
|
|
74
|
-
* @example
|
|
75
|
-
* ```ts
|
|
76
|
-
* const result = await grant.isAuthorized('document', 'update');
|
|
77
|
-
* if (!result.authorized) {
|
|
78
|
-
* console.log('Denied:', result.reason);
|
|
79
|
-
* }
|
|
80
|
-
* ```
|
|
81
|
-
*/
|
|
82
|
-
async isAuthorized(resource, action, options) {
|
|
83
|
-
const contextResourceKey = options?.context?.resource != null ? JSON.stringify(options.context.resource) : void 0;
|
|
84
|
-
const cacheKey = this.getCacheKey("auth", resource, action, options?.scope, contextResourceKey);
|
|
85
|
-
if (options?.useCache !== false) {
|
|
86
|
-
const cached = this.getFromCache(cacheKey);
|
|
87
|
-
if (cached) return cached;
|
|
88
|
-
}
|
|
89
|
-
try {
|
|
90
|
-
const scope = options?.scope;
|
|
91
|
-
const hasValidScope = scope && typeof scope === "object" && "tenant" in scope && "id" in scope;
|
|
92
|
-
const contextResource = options?.context?.resource ?? (hasValidScope && scope && "id" in scope && scope.id != null ? { id: scope.id } : null);
|
|
93
|
-
const response = await this.fetchWithAuth("/api/auth/is-authorized", {
|
|
94
|
-
method: "POST",
|
|
95
|
-
body: JSON.stringify({
|
|
96
|
-
permission: {
|
|
97
|
-
resource,
|
|
98
|
-
action
|
|
99
|
-
},
|
|
100
|
-
context: {
|
|
101
|
-
resource: contextResource
|
|
102
|
-
},
|
|
103
|
-
// Pass scope for dynamic scope override (only works with session tokens)
|
|
104
|
-
...hasValidScope && { scope }
|
|
105
|
-
})
|
|
106
|
-
});
|
|
107
|
-
if (!response.ok) {
|
|
108
|
-
const error = await response.json().catch(() => ({}));
|
|
109
|
-
return {
|
|
110
|
-
authorized: false,
|
|
111
|
-
reason: error.message || `API error: ${response.status}`
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
const json = await response.json();
|
|
115
|
-
const result = json.data ?? json;
|
|
116
|
-
this.setCache(cacheKey, result);
|
|
117
|
-
return result;
|
|
118
|
-
} catch (error) {
|
|
119
|
-
return {
|
|
120
|
-
authorized: false,
|
|
121
|
-
reason: error instanceof Error ? error.message : "Unknown error"
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
// ============================================================================
|
|
126
|
-
// Public API - Cache Management
|
|
127
|
-
// ============================================================================
|
|
128
|
-
/**
|
|
129
|
-
* Clear all cached data
|
|
130
|
-
*/
|
|
131
|
-
clearCache() {
|
|
132
|
-
this.cache.clear();
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Clear cached data for a specific scope
|
|
136
|
-
*/
|
|
137
|
-
clearScopeCache(scope) {
|
|
138
|
-
const scopeKey = scope ? JSON.stringify(scope) : "default";
|
|
139
|
-
for (const key of this.cache.keys()) {
|
|
140
|
-
if (key.includes(scopeKey)) {
|
|
141
|
-
this.cache.delete(key);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
// ============================================================================
|
|
146
|
-
// Private - HTTP & Authentication
|
|
147
|
-
// ============================================================================
|
|
148
|
-
/**
|
|
149
|
-
* Make an authenticated fetch request with automatic token refresh on 401
|
|
150
|
-
*/
|
|
151
|
-
async fetchWithAuth(url, init) {
|
|
152
|
-
const response = await this.doFetch(url, init);
|
|
153
|
-
if (response.status !== 401) return response;
|
|
154
|
-
if (this.config.onRefreshWithCredentials) {
|
|
155
|
-
if (!sharedCredentialsRefreshPromise) {
|
|
156
|
-
sharedCredentialsRefreshPromise = this.config.onRefreshWithCredentials().finally(() => {
|
|
157
|
-
sharedCredentialsRefreshPromise = null;
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
const refreshed = await sharedCredentialsRefreshPromise;
|
|
161
|
-
if (refreshed) return this.doFetch(url, init);
|
|
162
|
-
this.config.onUnauthorized?.();
|
|
163
|
-
}
|
|
164
|
-
return response;
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* Perform the actual fetch request
|
|
168
|
-
*/
|
|
169
|
-
async doFetch(url, init) {
|
|
170
|
-
const fetchFn = this.config.fetch ?? globalThis.fetch;
|
|
171
|
-
const fullUrl = url.startsWith("http") ? url : `${this.config.apiUrl}${url}`;
|
|
172
|
-
const headers = {
|
|
173
|
-
"Content-Type": "application/json",
|
|
174
|
-
...init?.headers
|
|
175
|
-
};
|
|
176
|
-
const token = await this.getToken();
|
|
177
|
-
if (token) {
|
|
178
|
-
headers["Authorization"] = `Bearer ${token}`;
|
|
179
|
-
}
|
|
180
|
-
return fetchFn(fullUrl, {
|
|
181
|
-
...init,
|
|
182
|
-
headers,
|
|
183
|
-
// Include cookies for same-origin requests (supports cookie-based auth)
|
|
184
|
-
credentials: this.config.credentials ?? "include"
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Get the current access token
|
|
189
|
-
*/
|
|
190
|
-
async getToken() {
|
|
191
|
-
if (this.config.getAccessToken) {
|
|
192
|
-
const token = this.config.getAccessToken();
|
|
193
|
-
return token instanceof Promise ? token : token;
|
|
194
|
-
}
|
|
195
|
-
return null;
|
|
196
|
-
}
|
|
197
|
-
// ============================================================================
|
|
198
|
-
// Private - Cache & URL Helpers
|
|
199
|
-
// ============================================================================
|
|
200
|
-
buildUrl(path, scope) {
|
|
201
|
-
const url = new URL(path, this.config.apiUrl);
|
|
202
|
-
if (scope) {
|
|
203
|
-
url.searchParams.set("scope", JSON.stringify(scope));
|
|
204
|
-
}
|
|
205
|
-
return url.toString();
|
|
206
|
-
}
|
|
207
|
-
getCacheKey(...parts) {
|
|
208
|
-
const prefix = this.config.cache?.prefix ?? "grant";
|
|
209
|
-
return `${prefix}:${parts.map((p) => p ? JSON.stringify(p) : "default").join(":")}`;
|
|
210
|
-
}
|
|
211
|
-
getFromCache(key) {
|
|
212
|
-
const entry = this.cache.get(key);
|
|
213
|
-
if (!entry) return null;
|
|
214
|
-
if (Date.now() > entry.expires) {
|
|
215
|
-
this.cache.delete(key);
|
|
216
|
-
return null;
|
|
217
|
-
}
|
|
218
|
-
return entry.data;
|
|
219
|
-
}
|
|
220
|
-
setCache(key, data) {
|
|
221
|
-
this.cache.set(key, {
|
|
222
|
-
data,
|
|
223
|
-
expires: Date.now() + this.defaultTtl
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
exports.GrantClient = GrantClient;
|
|
228
|
-
//# sourceMappingURL=index.cjs.map
|
|
2
|
+
const require_grant_client = require("./grant-client-COFg3pj8.cjs");
|
|
3
|
+
exports.GrantClient = require_grant_client.GrantClient;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { GrantClient } from './grant-client';
|
|
2
|
-
export type {
|
|
2
|
+
export type { ApiError, AuthorizationResult, AuthTokens, CacheOptions, GrantClientConfig, Permission, PermissionQueryOptions, Resource, Scope, SignInWithProjectAppOptions, Tenant, } from './types';
|
|
3
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAG7C,YAAY,EACV,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAG7C,YAAY,EACV,QAAQ,EACR,mBAAmB,EACnB,UAAU,EACV,YAAY,EACZ,iBAAiB,EACjB,UAAU,EACV,sBAAsB,EACtB,QAAQ,EAER,KAAK,EACL,2BAA2B,EAC3B,MAAM,GACP,MAAM,SAAS,CAAC"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,228 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
-
let sharedCredentialsRefreshPromise = null;
|
|
5
|
-
class GrantClient {
|
|
6
|
-
constructor(config) {
|
|
7
|
-
__publicField(this, "config");
|
|
8
|
-
__publicField(this, "cache", /* @__PURE__ */ new Map());
|
|
9
|
-
__publicField(this, "defaultTtl");
|
|
10
|
-
this.config = config;
|
|
11
|
-
this.defaultTtl = config.cache?.ttl ?? 5 * 60 * 1e3;
|
|
12
|
-
}
|
|
13
|
-
// ============================================================================
|
|
14
|
-
// Public API - Permission Checks
|
|
15
|
-
// ============================================================================
|
|
16
|
-
/**
|
|
17
|
-
* Check if the current user has a specific permission
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* ```ts
|
|
21
|
-
* const canEdit = await grant.can('document', 'update');
|
|
22
|
-
* if (canEdit) {
|
|
23
|
-
* // Show edit button
|
|
24
|
-
* }
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
async can(resource, action, options) {
|
|
28
|
-
const result = await this.isAuthorized(resource, action, options);
|
|
29
|
-
return result.authorized;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Alias for `can` - check if user has permission
|
|
33
|
-
*/
|
|
34
|
-
async hasPermission(resource, action, options) {
|
|
35
|
-
return this.can(resource, action, options);
|
|
36
|
-
}
|
|
37
|
-
// ============================================================================
|
|
38
|
-
// Public API - Project OAuth (sign-in with project app)
|
|
39
|
-
// ============================================================================
|
|
40
|
-
/**
|
|
41
|
-
* Start project-app OAuth flow (redirect only).
|
|
42
|
-
* Navigates the current window to the Grant OAuth entry page; after sign-in and consent,
|
|
43
|
-
* the user is redirected to the app's `redirect_uri` with token in the URL fragment.
|
|
44
|
-
*
|
|
45
|
-
* Requires `config.frontendUrl` and `redirectUri`.
|
|
46
|
-
*/
|
|
47
|
-
async signInWithProjectApp(options) {
|
|
48
|
-
const frontendUrl = this.config.frontendUrl;
|
|
49
|
-
if (!frontendUrl) {
|
|
50
|
-
throw new Error("GrantClient: frontendUrl is required for signInWithProjectApp");
|
|
51
|
-
}
|
|
52
|
-
const locale = options.locale ?? "en";
|
|
53
|
-
const redirectUri = options.redirectUri;
|
|
54
|
-
if (!redirectUri) {
|
|
55
|
-
throw new Error("redirectUri is required for signInWithProjectApp");
|
|
56
|
-
}
|
|
57
|
-
const entryPath = `/${locale}/auth/project`;
|
|
58
|
-
const params = new URLSearchParams({
|
|
59
|
-
client_id: options.clientId,
|
|
60
|
-
redirect_uri: redirectUri,
|
|
61
|
-
state: options.state ?? ""
|
|
62
|
-
});
|
|
63
|
-
if (options.scope) params.set("scope", options.scope);
|
|
64
|
-
const entryUrl = `${frontendUrl.replace(/\/$/, "")}${entryPath}?${params.toString()}`;
|
|
65
|
-
if (typeof window !== "undefined") {
|
|
66
|
-
window.location.href = entryUrl;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Check authorization with full result details
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* ```ts
|
|
74
|
-
* const result = await grant.isAuthorized('document', 'update');
|
|
75
|
-
* if (!result.authorized) {
|
|
76
|
-
* console.log('Denied:', result.reason);
|
|
77
|
-
* }
|
|
78
|
-
* ```
|
|
79
|
-
*/
|
|
80
|
-
async isAuthorized(resource, action, options) {
|
|
81
|
-
const contextResourceKey = options?.context?.resource != null ? JSON.stringify(options.context.resource) : void 0;
|
|
82
|
-
const cacheKey = this.getCacheKey("auth", resource, action, options?.scope, contextResourceKey);
|
|
83
|
-
if (options?.useCache !== false) {
|
|
84
|
-
const cached = this.getFromCache(cacheKey);
|
|
85
|
-
if (cached) return cached;
|
|
86
|
-
}
|
|
87
|
-
try {
|
|
88
|
-
const scope = options?.scope;
|
|
89
|
-
const hasValidScope = scope && typeof scope === "object" && "tenant" in scope && "id" in scope;
|
|
90
|
-
const contextResource = options?.context?.resource ?? (hasValidScope && scope && "id" in scope && scope.id != null ? { id: scope.id } : null);
|
|
91
|
-
const response = await this.fetchWithAuth("/api/auth/is-authorized", {
|
|
92
|
-
method: "POST",
|
|
93
|
-
body: JSON.stringify({
|
|
94
|
-
permission: {
|
|
95
|
-
resource,
|
|
96
|
-
action
|
|
97
|
-
},
|
|
98
|
-
context: {
|
|
99
|
-
resource: contextResource
|
|
100
|
-
},
|
|
101
|
-
// Pass scope for dynamic scope override (only works with session tokens)
|
|
102
|
-
...hasValidScope && { scope }
|
|
103
|
-
})
|
|
104
|
-
});
|
|
105
|
-
if (!response.ok) {
|
|
106
|
-
const error = await response.json().catch(() => ({}));
|
|
107
|
-
return {
|
|
108
|
-
authorized: false,
|
|
109
|
-
reason: error.message || `API error: ${response.status}`
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
const json = await response.json();
|
|
113
|
-
const result = json.data ?? json;
|
|
114
|
-
this.setCache(cacheKey, result);
|
|
115
|
-
return result;
|
|
116
|
-
} catch (error) {
|
|
117
|
-
return {
|
|
118
|
-
authorized: false,
|
|
119
|
-
reason: error instanceof Error ? error.message : "Unknown error"
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
// ============================================================================
|
|
124
|
-
// Public API - Cache Management
|
|
125
|
-
// ============================================================================
|
|
126
|
-
/**
|
|
127
|
-
* Clear all cached data
|
|
128
|
-
*/
|
|
129
|
-
clearCache() {
|
|
130
|
-
this.cache.clear();
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Clear cached data for a specific scope
|
|
134
|
-
*/
|
|
135
|
-
clearScopeCache(scope) {
|
|
136
|
-
const scopeKey = scope ? JSON.stringify(scope) : "default";
|
|
137
|
-
for (const key of this.cache.keys()) {
|
|
138
|
-
if (key.includes(scopeKey)) {
|
|
139
|
-
this.cache.delete(key);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
// ============================================================================
|
|
144
|
-
// Private - HTTP & Authentication
|
|
145
|
-
// ============================================================================
|
|
146
|
-
/**
|
|
147
|
-
* Make an authenticated fetch request with automatic token refresh on 401
|
|
148
|
-
*/
|
|
149
|
-
async fetchWithAuth(url, init) {
|
|
150
|
-
const response = await this.doFetch(url, init);
|
|
151
|
-
if (response.status !== 401) return response;
|
|
152
|
-
if (this.config.onRefreshWithCredentials) {
|
|
153
|
-
if (!sharedCredentialsRefreshPromise) {
|
|
154
|
-
sharedCredentialsRefreshPromise = this.config.onRefreshWithCredentials().finally(() => {
|
|
155
|
-
sharedCredentialsRefreshPromise = null;
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
const refreshed = await sharedCredentialsRefreshPromise;
|
|
159
|
-
if (refreshed) return this.doFetch(url, init);
|
|
160
|
-
this.config.onUnauthorized?.();
|
|
161
|
-
}
|
|
162
|
-
return response;
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Perform the actual fetch request
|
|
166
|
-
*/
|
|
167
|
-
async doFetch(url, init) {
|
|
168
|
-
const fetchFn = this.config.fetch ?? globalThis.fetch;
|
|
169
|
-
const fullUrl = url.startsWith("http") ? url : `${this.config.apiUrl}${url}`;
|
|
170
|
-
const headers = {
|
|
171
|
-
"Content-Type": "application/json",
|
|
172
|
-
...init?.headers
|
|
173
|
-
};
|
|
174
|
-
const token = await this.getToken();
|
|
175
|
-
if (token) {
|
|
176
|
-
headers["Authorization"] = `Bearer ${token}`;
|
|
177
|
-
}
|
|
178
|
-
return fetchFn(fullUrl, {
|
|
179
|
-
...init,
|
|
180
|
-
headers,
|
|
181
|
-
// Include cookies for same-origin requests (supports cookie-based auth)
|
|
182
|
-
credentials: this.config.credentials ?? "include"
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* Get the current access token
|
|
187
|
-
*/
|
|
188
|
-
async getToken() {
|
|
189
|
-
if (this.config.getAccessToken) {
|
|
190
|
-
const token = this.config.getAccessToken();
|
|
191
|
-
return token instanceof Promise ? token : token;
|
|
192
|
-
}
|
|
193
|
-
return null;
|
|
194
|
-
}
|
|
195
|
-
// ============================================================================
|
|
196
|
-
// Private - Cache & URL Helpers
|
|
197
|
-
// ============================================================================
|
|
198
|
-
buildUrl(path, scope) {
|
|
199
|
-
const url = new URL(path, this.config.apiUrl);
|
|
200
|
-
if (scope) {
|
|
201
|
-
url.searchParams.set("scope", JSON.stringify(scope));
|
|
202
|
-
}
|
|
203
|
-
return url.toString();
|
|
204
|
-
}
|
|
205
|
-
getCacheKey(...parts) {
|
|
206
|
-
const prefix = this.config.cache?.prefix ?? "grant";
|
|
207
|
-
return `${prefix}:${parts.map((p) => p ? JSON.stringify(p) : "default").join(":")}`;
|
|
208
|
-
}
|
|
209
|
-
getFromCache(key) {
|
|
210
|
-
const entry = this.cache.get(key);
|
|
211
|
-
if (!entry) return null;
|
|
212
|
-
if (Date.now() > entry.expires) {
|
|
213
|
-
this.cache.delete(key);
|
|
214
|
-
return null;
|
|
215
|
-
}
|
|
216
|
-
return entry.data;
|
|
217
|
-
}
|
|
218
|
-
setCache(key, data) {
|
|
219
|
-
this.cache.set(key, {
|
|
220
|
-
data,
|
|
221
|
-
expires: Date.now() + this.defaultTtl
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
export {
|
|
226
|
-
GrantClient
|
|
227
|
-
};
|
|
228
|
-
//# sourceMappingURL=index.mjs.map
|
|
1
|
+
import { t as GrantClient } from "./grant-client-BuvFCpqW.js";
|
|
2
|
+
export { GrantClient };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/react/context.tsx"],"names":[],"mappings":"AAEA,OAAO,
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/react/context.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAiB,KAAK,SAAS,EAAuB,MAAM,OAAO,CAAC;AAE3E,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAOlD;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,MAAM,EAAE,iBAAiB,CAAC;IAE1B;;;OAGG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;IAErB;;OAEG;IACH,QAAQ,EAAE,SAAS,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,aAAa,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,kBAAkB,2CAO7E;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,IAAI,WAAW,CAW5C;AAED;;;;;GAKG;AACH,wBAAgB,sBAAsB,IAAI,WAAW,GAAG,IAAI,CAE3D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useGrant.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useGrant.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useGrant.d.ts","sourceRoot":"","sources":["../../../src/react/hooks/useGrant.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAuB,KAAK,EAAE,MAAM,aAAa,CAAC;AAG9D;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,2GAA2G;IAC3G,KAAK,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC;IACrB,2CAA2C;IAC3C,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,oDAAoD;IACpD,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,uDAAuD;IACvD,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,uCAAuC;IACvC,OAAO,CAAC,EAAE;QACR,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;KAC3C,CAAC;CACH;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,6CAA6C;IAC7C,SAAS,EAAE,OAAO,CAAC;IACnB,8CAA8C;IAC9C,SAAS,EAAE,OAAO,CAAC;CACpB;AAWD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,QAAQ,CACtB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,eAAoB,GAC5B,OAAO,GAAG,cAAc,CAqH1B"}
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export { GrantProvider, useGrantClient, useGrantClientOptional } from './context';
|
|
2
1
|
export type { GrantProviderProps } from './context';
|
|
3
|
-
export {
|
|
2
|
+
export { GrantProvider, useGrantClient, useGrantClientOptional } from './context';
|
|
4
3
|
export type { UseGrantOptions, UseGrantResult } from './hooks/useGrant';
|
|
5
|
-
export {
|
|
4
|
+
export { useGrant } from './hooks/useGrant';
|
|
6
5
|
export type { GrantGateProps } from './components/GrantGate';
|
|
7
|
-
export
|
|
6
|
+
export { GrantGate } from './components/GrantGate';
|
|
7
|
+
export type { AuthorizationResult, AuthTokens, GrantClientConfig, Permission, Scope, } from '../types';
|
|
8
8
|
export { GrantClient } from '../grant-client';
|
|
9
9
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AACA,YAAY,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC;AAGlF,YAAY,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACxE,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAG5C,YAAY,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC7D,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAGnD,YAAY,EACV,mBAAmB,EACnB,UAAU,EACV,iBAAiB,EACjB,UAAU,EACV,KAAK,GACN,MAAM,UAAU,CAAC;AAGlB,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC"}
|