@drawbridge/drawbridge-utils 0.0.41 → 0.0.43
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/ai.cjs +4 -4
- package/dist/ai.js +127 -8
- package/dist/axios.cjs +1 -1
- package/dist/axios.js +1 -1
- package/dist/cdn.cjs +1 -1
- package/dist/cdn.js +1 -1
- package/dist/circuit.cjs +1 -1
- package/dist/circuit.js +50 -3
- package/dist/encrypt.cjs +3 -3
- package/dist/encrypt.js +38 -5
- package/dist/fetch.cjs +1 -1
- package/dist/fetch.js +1 -1
- package/dist/http.cjs +1 -1
- package/dist/http.js +1 -1
- package/dist/nanoid.cjs +1 -1
- package/dist/nanoid.js +1 -1
- package/dist/oauth/index.cjs +196 -0
- package/dist/oauth/index.d.cts +231 -0
- package/dist/oauth/index.d.ts +231 -0
- package/dist/oauth/index.js +151 -0
- package/dist/oauth/server.cjs +377 -0
- package/dist/oauth/server.d.cts +387 -0
- package/dist/oauth/server.d.ts +387 -0
- package/dist/oauth/server.js +341 -0
- package/dist/orders.cjs +1 -1
- package/dist/orders.js +1 -1
- package/dist/redirect.cjs +1 -1
- package/dist/redirect.js +1 -1
- package/dist/shopify.cjs +5 -5
- package/dist/shopify.js +46 -11
- package/dist/slugify.cjs +1 -1
- package/dist/slugify.js +1 -1
- package/dist/token.cjs +1 -1
- package/dist/token.js +21 -5
- package/dist/transactions.cjs +1 -1
- package/dist/transactions.js +108 -4
- package/dist/upload.cjs +1 -1
- package/dist/upload.js +1 -1
- package/package.json +20 -4
- package/dist/chunk-2DRAIKGZ.js +0 -38
- package/dist/chunk-6HH36OK6.js +0 -54
- package/dist/chunk-JT2PAZAZ.js +0 -113
- package/dist/chunk-OS2AHUWX.js +0 -27
- package/dist/oauth.cjs +0 -94
- package/dist/oauth.d.cts +0 -129
- package/dist/oauth.d.ts +0 -129
- package/dist/oauth.js +0 -70
package/dist/oauth.cjs
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
|
|
19
|
-
// oauth.js
|
|
20
|
-
var oauth_exports = {};
|
|
21
|
-
__export(oauth_exports, {
|
|
22
|
-
createOAuthClient: () => createOAuthClient
|
|
23
|
-
});
|
|
24
|
-
module.exports = __toCommonJS(oauth_exports);
|
|
25
|
-
var REFRESH_LEAD_SECONDS = 60;
|
|
26
|
-
var createOAuthClient = ({
|
|
27
|
-
clientId,
|
|
28
|
-
clientSecret,
|
|
29
|
-
scopes = [],
|
|
30
|
-
tokenUri
|
|
31
|
-
}) => {
|
|
32
|
-
if (!clientId) throw new Error("createOAuthClient: clientId required");
|
|
33
|
-
if (!clientSecret) throw new Error("createOAuthClient: clientSecret required");
|
|
34
|
-
if (!tokenUri) throw new Error("createOAuthClient: tokenUri required");
|
|
35
|
-
let cached = null;
|
|
36
|
-
let pending = null;
|
|
37
|
-
const isFresh = () => {
|
|
38
|
-
if (!cached) return false;
|
|
39
|
-
const now = Math.floor(Date.now() / 1e3);
|
|
40
|
-
return cached.expiresAt - REFRESH_LEAD_SECONDS > now;
|
|
41
|
-
};
|
|
42
|
-
const fetchToken = async () => {
|
|
43
|
-
const params = new URLSearchParams();
|
|
44
|
-
params.set("grant_type", "client_credentials");
|
|
45
|
-
if (scopes.length > 0) {
|
|
46
|
-
params.set("scope", scopes.join(" "));
|
|
47
|
-
}
|
|
48
|
-
const basic = Buffer.from(clientId + ":" + clientSecret).toString("base64");
|
|
49
|
-
const response = await fetch(
|
|
50
|
-
tokenUri,
|
|
51
|
-
{
|
|
52
|
-
body: params.toString(),
|
|
53
|
-
headers: {
|
|
54
|
-
"authorization": "Basic " + basic,
|
|
55
|
-
"content-type": "application/x-www-form-urlencoded"
|
|
56
|
-
},
|
|
57
|
-
method: "POST"
|
|
58
|
-
}
|
|
59
|
-
);
|
|
60
|
-
if (!response.ok) {
|
|
61
|
-
const text = await response.text();
|
|
62
|
-
throw new Error("createOAuthClient: token fetch failed (" + response.status + "): " + text);
|
|
63
|
-
}
|
|
64
|
-
const body = await response.json();
|
|
65
|
-
cached = {
|
|
66
|
-
accessToken: body.access_token,
|
|
67
|
-
expiresAt: Math.floor(Date.now() / 1e3) + Number(body.expires_in || 3600),
|
|
68
|
-
scope: body.scope
|
|
69
|
-
};
|
|
70
|
-
return cached.accessToken;
|
|
71
|
-
};
|
|
72
|
-
const refresh = () => {
|
|
73
|
-
if (pending) return pending;
|
|
74
|
-
pending = fetchToken().finally(() => {
|
|
75
|
-
pending = null;
|
|
76
|
-
});
|
|
77
|
-
return pending;
|
|
78
|
-
};
|
|
79
|
-
return {
|
|
80
|
-
token: async () => {
|
|
81
|
-
if (isFresh()) return cached.accessToken;
|
|
82
|
-
return refresh();
|
|
83
|
-
},
|
|
84
|
-
// Force a refresh on the next call. Use this when a request returns 401
|
|
85
|
-
// before the cached token's expiry — the server may have revoked it.
|
|
86
|
-
invalidate: () => {
|
|
87
|
-
cached = null;
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
};
|
|
91
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
92
|
-
0 && (module.exports = {
|
|
93
|
-
createOAuthClient
|
|
94
|
-
});
|
package/dist/oauth.d.cts
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
// Client_credentials token caching helper for first-party Drawbridge apps
|
|
2
|
-
// that call drawbridge-api as themselves (drawbridge-share, drawbridge-emails,
|
|
3
|
-
// drawbridge-webhooks, etc.).
|
|
4
|
-
//
|
|
5
|
-
// Caches the access token in process memory; refreshes when nearing expiry
|
|
6
|
-
// or after a 401. Single-flight refresh prevents thundering herd when many
|
|
7
|
-
// concurrent requests arrive while the token is being rotated.
|
|
8
|
-
//
|
|
9
|
-
// Usage:
|
|
10
|
-
// import { createOAuthClient } from '@drawbridge/drawbridge-utils/oauth';
|
|
11
|
-
//
|
|
12
|
-
// const client = createOAuthClient({
|
|
13
|
-
// clientId : process.env.OAUTH_CLIENT_ID,
|
|
14
|
-
// clientSecret : process.env.OAUTH_CLIENT_SECRET,
|
|
15
|
-
// tokenUri : process.env.NEXT_PUBLIC_API_URI + '/oauth/token',
|
|
16
|
-
// scopes : [ 'share:read' ]
|
|
17
|
-
// });
|
|
18
|
-
//
|
|
19
|
-
// await request({ endpoint : '/page/abc', token : await client.token() });
|
|
20
|
-
|
|
21
|
-
const REFRESH_LEAD_SECONDS = 60;
|
|
22
|
-
|
|
23
|
-
const createOAuthClient = ({
|
|
24
|
-
clientId,
|
|
25
|
-
clientSecret,
|
|
26
|
-
scopes = [],
|
|
27
|
-
tokenUri
|
|
28
|
-
}) => {
|
|
29
|
-
|
|
30
|
-
if( ! clientId ) throw new Error( 'createOAuthClient: clientId required' );
|
|
31
|
-
if( ! clientSecret ) throw new Error( 'createOAuthClient: clientSecret required' );
|
|
32
|
-
if( ! tokenUri ) throw new Error( 'createOAuthClient: tokenUri required' );
|
|
33
|
-
|
|
34
|
-
let cached = null;
|
|
35
|
-
let pending = null;
|
|
36
|
-
|
|
37
|
-
const isFresh = () => {
|
|
38
|
-
|
|
39
|
-
if( ! cached ) return false;
|
|
40
|
-
|
|
41
|
-
const now = Math.floor( Date.now() / 1000 );
|
|
42
|
-
|
|
43
|
-
return cached.expiresAt - REFRESH_LEAD_SECONDS > now;
|
|
44
|
-
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const fetchToken = async () => {
|
|
48
|
-
|
|
49
|
-
const params = new URLSearchParams();
|
|
50
|
-
|
|
51
|
-
params.set( 'grant_type', 'client_credentials' );
|
|
52
|
-
|
|
53
|
-
if( scopes.length > 0 ){
|
|
54
|
-
|
|
55
|
-
params.set( 'scope', scopes.join( ' ' ) );
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const basic = Buffer
|
|
60
|
-
.from( clientId + ':' + clientSecret )
|
|
61
|
-
.toString( 'base64' );
|
|
62
|
-
|
|
63
|
-
const response = await fetch(
|
|
64
|
-
tokenUri,
|
|
65
|
-
{
|
|
66
|
-
body : params.toString(),
|
|
67
|
-
headers : {
|
|
68
|
-
'authorization' : 'Basic ' + basic,
|
|
69
|
-
'content-type' : 'application/x-www-form-urlencoded'
|
|
70
|
-
},
|
|
71
|
-
method : 'POST'
|
|
72
|
-
}
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
if( ! response.ok ){
|
|
76
|
-
|
|
77
|
-
const text = await response.text();
|
|
78
|
-
|
|
79
|
-
throw new Error( 'createOAuthClient: token fetch failed (' + response.status + '): ' + text );
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const body = await response.json();
|
|
84
|
-
|
|
85
|
-
cached = {
|
|
86
|
-
accessToken : body.access_token,
|
|
87
|
-
expiresAt : Math.floor( Date.now() / 1000 ) + Number( body.expires_in || 3600 ),
|
|
88
|
-
scope : body.scope
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
return cached.accessToken;
|
|
92
|
-
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
// Single-flight: collapse concurrent token fetches into one network call.
|
|
96
|
-
const refresh = () => {
|
|
97
|
-
|
|
98
|
-
if( pending ) return pending;
|
|
99
|
-
|
|
100
|
-
pending = fetchToken().finally( () => {
|
|
101
|
-
|
|
102
|
-
pending = null;
|
|
103
|
-
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
return pending;
|
|
107
|
-
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
return {
|
|
111
|
-
token : async () => {
|
|
112
|
-
|
|
113
|
-
if( isFresh() ) return cached.accessToken;
|
|
114
|
-
|
|
115
|
-
return refresh();
|
|
116
|
-
|
|
117
|
-
},
|
|
118
|
-
// Force a refresh on the next call. Use this when a request returns 401
|
|
119
|
-
// before the cached token's expiry — the server may have revoked it.
|
|
120
|
-
invalidate : () => {
|
|
121
|
-
|
|
122
|
-
cached = null;
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
export { createOAuthClient };
|
package/dist/oauth.d.ts
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
// Client_credentials token caching helper for first-party Drawbridge apps
|
|
2
|
-
// that call drawbridge-api as themselves (drawbridge-share, drawbridge-emails,
|
|
3
|
-
// drawbridge-webhooks, etc.).
|
|
4
|
-
//
|
|
5
|
-
// Caches the access token in process memory; refreshes when nearing expiry
|
|
6
|
-
// or after a 401. Single-flight refresh prevents thundering herd when many
|
|
7
|
-
// concurrent requests arrive while the token is being rotated.
|
|
8
|
-
//
|
|
9
|
-
// Usage:
|
|
10
|
-
// import { createOAuthClient } from '@drawbridge/drawbridge-utils/oauth';
|
|
11
|
-
//
|
|
12
|
-
// const client = createOAuthClient({
|
|
13
|
-
// clientId : process.env.OAUTH_CLIENT_ID,
|
|
14
|
-
// clientSecret : process.env.OAUTH_CLIENT_SECRET,
|
|
15
|
-
// tokenUri : process.env.NEXT_PUBLIC_API_URI + '/oauth/token',
|
|
16
|
-
// scopes : [ 'share:read' ]
|
|
17
|
-
// });
|
|
18
|
-
//
|
|
19
|
-
// await request({ endpoint : '/page/abc', token : await client.token() });
|
|
20
|
-
|
|
21
|
-
const REFRESH_LEAD_SECONDS = 60;
|
|
22
|
-
|
|
23
|
-
const createOAuthClient = ({
|
|
24
|
-
clientId,
|
|
25
|
-
clientSecret,
|
|
26
|
-
scopes = [],
|
|
27
|
-
tokenUri
|
|
28
|
-
}) => {
|
|
29
|
-
|
|
30
|
-
if( ! clientId ) throw new Error( 'createOAuthClient: clientId required' );
|
|
31
|
-
if( ! clientSecret ) throw new Error( 'createOAuthClient: clientSecret required' );
|
|
32
|
-
if( ! tokenUri ) throw new Error( 'createOAuthClient: tokenUri required' );
|
|
33
|
-
|
|
34
|
-
let cached = null;
|
|
35
|
-
let pending = null;
|
|
36
|
-
|
|
37
|
-
const isFresh = () => {
|
|
38
|
-
|
|
39
|
-
if( ! cached ) return false;
|
|
40
|
-
|
|
41
|
-
const now = Math.floor( Date.now() / 1000 );
|
|
42
|
-
|
|
43
|
-
return cached.expiresAt - REFRESH_LEAD_SECONDS > now;
|
|
44
|
-
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const fetchToken = async () => {
|
|
48
|
-
|
|
49
|
-
const params = new URLSearchParams();
|
|
50
|
-
|
|
51
|
-
params.set( 'grant_type', 'client_credentials' );
|
|
52
|
-
|
|
53
|
-
if( scopes.length > 0 ){
|
|
54
|
-
|
|
55
|
-
params.set( 'scope', scopes.join( ' ' ) );
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const basic = Buffer
|
|
60
|
-
.from( clientId + ':' + clientSecret )
|
|
61
|
-
.toString( 'base64' );
|
|
62
|
-
|
|
63
|
-
const response = await fetch(
|
|
64
|
-
tokenUri,
|
|
65
|
-
{
|
|
66
|
-
body : params.toString(),
|
|
67
|
-
headers : {
|
|
68
|
-
'authorization' : 'Basic ' + basic,
|
|
69
|
-
'content-type' : 'application/x-www-form-urlencoded'
|
|
70
|
-
},
|
|
71
|
-
method : 'POST'
|
|
72
|
-
}
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
if( ! response.ok ){
|
|
76
|
-
|
|
77
|
-
const text = await response.text();
|
|
78
|
-
|
|
79
|
-
throw new Error( 'createOAuthClient: token fetch failed (' + response.status + '): ' + text );
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const body = await response.json();
|
|
84
|
-
|
|
85
|
-
cached = {
|
|
86
|
-
accessToken : body.access_token,
|
|
87
|
-
expiresAt : Math.floor( Date.now() / 1000 ) + Number( body.expires_in || 3600 ),
|
|
88
|
-
scope : body.scope
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
return cached.accessToken;
|
|
92
|
-
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
// Single-flight: collapse concurrent token fetches into one network call.
|
|
96
|
-
const refresh = () => {
|
|
97
|
-
|
|
98
|
-
if( pending ) return pending;
|
|
99
|
-
|
|
100
|
-
pending = fetchToken().finally( () => {
|
|
101
|
-
|
|
102
|
-
pending = null;
|
|
103
|
-
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
return pending;
|
|
107
|
-
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
return {
|
|
111
|
-
token : async () => {
|
|
112
|
-
|
|
113
|
-
if( isFresh() ) return cached.accessToken;
|
|
114
|
-
|
|
115
|
-
return refresh();
|
|
116
|
-
|
|
117
|
-
},
|
|
118
|
-
// Force a refresh on the next call. Use this when a request returns 401
|
|
119
|
-
// before the cached token's expiry — the server may have revoked it.
|
|
120
|
-
invalidate : () => {
|
|
121
|
-
|
|
122
|
-
cached = null;
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
export { createOAuthClient };
|
package/dist/oauth.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
// oauth.js
|
|
2
|
-
var REFRESH_LEAD_SECONDS = 60;
|
|
3
|
-
var createOAuthClient = ({
|
|
4
|
-
clientId,
|
|
5
|
-
clientSecret,
|
|
6
|
-
scopes = [],
|
|
7
|
-
tokenUri
|
|
8
|
-
}) => {
|
|
9
|
-
if (!clientId) throw new Error("createOAuthClient: clientId required");
|
|
10
|
-
if (!clientSecret) throw new Error("createOAuthClient: clientSecret required");
|
|
11
|
-
if (!tokenUri) throw new Error("createOAuthClient: tokenUri required");
|
|
12
|
-
let cached = null;
|
|
13
|
-
let pending = null;
|
|
14
|
-
const isFresh = () => {
|
|
15
|
-
if (!cached) return false;
|
|
16
|
-
const now = Math.floor(Date.now() / 1e3);
|
|
17
|
-
return cached.expiresAt - REFRESH_LEAD_SECONDS > now;
|
|
18
|
-
};
|
|
19
|
-
const fetchToken = async () => {
|
|
20
|
-
const params = new URLSearchParams();
|
|
21
|
-
params.set("grant_type", "client_credentials");
|
|
22
|
-
if (scopes.length > 0) {
|
|
23
|
-
params.set("scope", scopes.join(" "));
|
|
24
|
-
}
|
|
25
|
-
const basic = Buffer.from(clientId + ":" + clientSecret).toString("base64");
|
|
26
|
-
const response = await fetch(
|
|
27
|
-
tokenUri,
|
|
28
|
-
{
|
|
29
|
-
body: params.toString(),
|
|
30
|
-
headers: {
|
|
31
|
-
"authorization": "Basic " + basic,
|
|
32
|
-
"content-type": "application/x-www-form-urlencoded"
|
|
33
|
-
},
|
|
34
|
-
method: "POST"
|
|
35
|
-
}
|
|
36
|
-
);
|
|
37
|
-
if (!response.ok) {
|
|
38
|
-
const text = await response.text();
|
|
39
|
-
throw new Error("createOAuthClient: token fetch failed (" + response.status + "): " + text);
|
|
40
|
-
}
|
|
41
|
-
const body = await response.json();
|
|
42
|
-
cached = {
|
|
43
|
-
accessToken: body.access_token,
|
|
44
|
-
expiresAt: Math.floor(Date.now() / 1e3) + Number(body.expires_in || 3600),
|
|
45
|
-
scope: body.scope
|
|
46
|
-
};
|
|
47
|
-
return cached.accessToken;
|
|
48
|
-
};
|
|
49
|
-
const refresh = () => {
|
|
50
|
-
if (pending) return pending;
|
|
51
|
-
pending = fetchToken().finally(() => {
|
|
52
|
-
pending = null;
|
|
53
|
-
});
|
|
54
|
-
return pending;
|
|
55
|
-
};
|
|
56
|
-
return {
|
|
57
|
-
token: async () => {
|
|
58
|
-
if (isFresh()) return cached.accessToken;
|
|
59
|
-
return refresh();
|
|
60
|
-
},
|
|
61
|
-
// Force a refresh on the next call. Use this when a request returns 401
|
|
62
|
-
// before the cached token's expiry — the server may have revoked it.
|
|
63
|
-
invalidate: () => {
|
|
64
|
-
cached = null;
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
};
|
|
68
|
-
export {
|
|
69
|
-
createOAuthClient
|
|
70
|
-
};
|