@vercel/connect 0.0.4-alpha.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 +351 -0
- package/dist/ash/connection-authorization.d.ts +131 -0
- package/dist/ash/connection-authorization.js +191 -0
- package/dist/ash/index.d.ts +10 -0
- package/dist/ash/index.js +10 -0
- package/dist/ash/slack-credentials.d.ts +25 -0
- package/dist/ash/slack-credentials.js +31 -0
- package/dist/authjs/connect-provider.d.ts +43 -0
- package/dist/authjs/connect-provider.js +95 -0
- package/dist/authjs/index.d.ts +10 -0
- package/dist/authjs/index.js +10 -0
- package/dist/authorization.d.ts +12 -0
- package/dist/authorization.js +68 -0
- package/dist/betterauth/connect-provider.d.ts +32 -0
- package/dist/betterauth/connect-provider.js +107 -0
- package/dist/betterauth/index.d.ts +10 -0
- package/dist/betterauth/index.js +10 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/internal/team-id.d.ts +3 -0
- package/dist/internal/team-id.js +47 -0
- package/dist/token.d.ts +47 -0
- package/dist/token.js +102 -0
- package/package.json +59 -0
package/dist/token.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { getVercelOidcToken } from "@vercel/oidc";
|
|
2
|
+
export class NoValidTokenError extends Error {
|
|
3
|
+
constructor(message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "NoValidTokenError";
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export class UserAuthorizationRequiredError extends Error {
|
|
9
|
+
constructor(message) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = "UserAuthorizationRequiredError";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export class ConnectorInstallationRequiredError extends Error {
|
|
15
|
+
constructor(message) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = "ConnectorInstallationRequiredError";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export async function getToken(connector, params, options) {
|
|
21
|
+
const { token } = await getTokenResponse(connector, params, options);
|
|
22
|
+
return token;
|
|
23
|
+
}
|
|
24
|
+
export async function getTokenResponse(connector, params, options) {
|
|
25
|
+
const bufferMs = params.validityBufferMs ?? DEFAULT_VALIDITY_BUFFER_MS;
|
|
26
|
+
const cacheKey = JSON.stringify({ connector, ...params });
|
|
27
|
+
const cached = cache.get(cacheKey);
|
|
28
|
+
if (cached) {
|
|
29
|
+
const now = Date.now();
|
|
30
|
+
if (cached.response.expiresAt - now > bufferMs) {
|
|
31
|
+
cached.lastUsed = now;
|
|
32
|
+
return cached.response;
|
|
33
|
+
}
|
|
34
|
+
cache.delete(cacheKey);
|
|
35
|
+
}
|
|
36
|
+
const vercelToken = options?.vercelToken ?? (await getVercelOidcToken());
|
|
37
|
+
const endpoint = `https://api.vercel.com/v1/connex/token/${encodeURIComponent(connector)}`;
|
|
38
|
+
const response = await fetch(endpoint, {
|
|
39
|
+
method: "POST",
|
|
40
|
+
headers: {
|
|
41
|
+
Accept: "application/json",
|
|
42
|
+
"Content-Type": "application/json",
|
|
43
|
+
Authorization: `Bearer ${vercelToken}`,
|
|
44
|
+
},
|
|
45
|
+
body: JSON.stringify(params),
|
|
46
|
+
});
|
|
47
|
+
if (!response.ok) {
|
|
48
|
+
let errorText;
|
|
49
|
+
let errorObject;
|
|
50
|
+
try {
|
|
51
|
+
errorText = await response.text();
|
|
52
|
+
const errorJson = JSON.parse(errorText);
|
|
53
|
+
if (typeof errorJson === "object" && errorJson !== null) {
|
|
54
|
+
errorObject = errorJson;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch { }
|
|
58
|
+
const code = errorObject?.error?.code;
|
|
59
|
+
const message = errorObject?.error?.message;
|
|
60
|
+
if (code === "no_token") {
|
|
61
|
+
throw new NoValidTokenError(message || errorText || "No valid token available");
|
|
62
|
+
}
|
|
63
|
+
if (code === "user_authorization_required") {
|
|
64
|
+
throw new UserAuthorizationRequiredError(message || errorText || "User authorization is required");
|
|
65
|
+
}
|
|
66
|
+
if (code === "client_installation_required" ||
|
|
67
|
+
code === "connector_installation_required") {
|
|
68
|
+
throw new ConnectorInstallationRequiredError(message || errorText || "Connector installation is required");
|
|
69
|
+
}
|
|
70
|
+
throw new Error([
|
|
71
|
+
"Failed to get token:",
|
|
72
|
+
`${response.status}`,
|
|
73
|
+
response.statusText,
|
|
74
|
+
code ? ` - ${code}` : "",
|
|
75
|
+
errorText ? ` - ${errorText}` : "",
|
|
76
|
+
]
|
|
77
|
+
.filter(Boolean)
|
|
78
|
+
.join(" "));
|
|
79
|
+
}
|
|
80
|
+
const data = await response.json();
|
|
81
|
+
if (cache.size >= MAX_CACHE_SIZE) {
|
|
82
|
+
evictLru();
|
|
83
|
+
}
|
|
84
|
+
cache.set(cacheKey, { response: data, lastUsed: Date.now() });
|
|
85
|
+
return data;
|
|
86
|
+
}
|
|
87
|
+
const DEFAULT_VALIDITY_BUFFER_MS = 30_000;
|
|
88
|
+
const MAX_CACHE_SIZE = 100;
|
|
89
|
+
const cache = new Map();
|
|
90
|
+
function evictLru() {
|
|
91
|
+
let oldestKey;
|
|
92
|
+
let oldestTime = Infinity;
|
|
93
|
+
for (const [key, entry] of cache) {
|
|
94
|
+
if (entry.lastUsed < oldestTime) {
|
|
95
|
+
oldestTime = entry.lastUsed;
|
|
96
|
+
oldestKey = key;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (oldestKey !== undefined) {
|
|
100
|
+
cache.delete(oldestKey);
|
|
101
|
+
}
|
|
102
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vercel/connect",
|
|
3
|
+
"version": "0.0.4-alpha.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"default": "./dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"./ash": {
|
|
11
|
+
"types": "./dist/ash/index.d.ts",
|
|
12
|
+
"default": "./dist/ash/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./betterauth": {
|
|
15
|
+
"types": "./dist/betterauth/index.d.ts",
|
|
16
|
+
"default": "./dist/betterauth/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./authjs": {
|
|
19
|
+
"types": "./dist/authjs/index.d.ts",
|
|
20
|
+
"default": "./dist/authjs/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@vercel/oidc": "*"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@auth/core": ">=0.37.0",
|
|
31
|
+
"better-auth": ">=1.5.0",
|
|
32
|
+
"experimental-ash": ">=0.3.0-alpha.29"
|
|
33
|
+
},
|
|
34
|
+
"peerDependenciesMeta": {
|
|
35
|
+
"@auth/core": {
|
|
36
|
+
"optional": true
|
|
37
|
+
},
|
|
38
|
+
"better-auth": {
|
|
39
|
+
"optional": true
|
|
40
|
+
},
|
|
41
|
+
"experimental-ash": {
|
|
42
|
+
"optional": true
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@auth/core": "0.37.4",
|
|
47
|
+
"better-auth": "1.5.5",
|
|
48
|
+
"experimental-ash": "0.3.0-alpha.29",
|
|
49
|
+
"typescript": "^5"
|
|
50
|
+
},
|
|
51
|
+
"private": false,
|
|
52
|
+
"publishConfig": {
|
|
53
|
+
"access": "public"
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "tsc",
|
|
57
|
+
"typecheck": "tsc --noEmit"
|
|
58
|
+
}
|
|
59
|
+
}
|