@drawbridge/drawbridge-utils 0.0.17 → 0.0.18
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/chunk-CUUQCM5T.js +34 -0
- package/dist/encrypt.js +4 -29
- package/dist/shopify.cjs +166 -0
- package/dist/shopify.d.cts +127 -0
- package/dist/shopify.d.ts +127 -0
- package/dist/shopify.js +104 -0
- package/package.json +2 -2
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// encrypt.js
|
|
2
|
+
import crypto from "crypto";
|
|
3
|
+
var ALGORITHM = "aes-256-gcm";
|
|
4
|
+
var getKey = () => Buffer.from(process.env.ENCRYPT_CONNECTION_SECRET, "hex");
|
|
5
|
+
var encrypt = (value) => {
|
|
6
|
+
const iv = crypto.randomBytes(12);
|
|
7
|
+
const cipher = crypto.createCipheriv(ALGORITHM, getKey(), iv);
|
|
8
|
+
const data = Buffer.concat([
|
|
9
|
+
cipher.update(JSON.stringify(value), "utf8"),
|
|
10
|
+
cipher.final()
|
|
11
|
+
]);
|
|
12
|
+
const tag = cipher.getAuthTag();
|
|
13
|
+
return [iv, tag, data].map((b) => b.toString("hex")).join(":");
|
|
14
|
+
};
|
|
15
|
+
var decrypt = (value) => {
|
|
16
|
+
if (typeof value !== "string") return value;
|
|
17
|
+
const [ivHex, tagHex, dataHex] = value.split(":");
|
|
18
|
+
const decipher = crypto.createDecipheriv(
|
|
19
|
+
ALGORITHM,
|
|
20
|
+
getKey(),
|
|
21
|
+
Buffer.from(ivHex, "hex")
|
|
22
|
+
);
|
|
23
|
+
decipher.setAuthTag(Buffer.from(tagHex, "hex"));
|
|
24
|
+
const result = Buffer.concat([
|
|
25
|
+
decipher.update(Buffer.from(dataHex, "hex")),
|
|
26
|
+
decipher.final()
|
|
27
|
+
]);
|
|
28
|
+
return JSON.parse(result.toString("utf8"));
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
encrypt,
|
|
33
|
+
decrypt
|
|
34
|
+
};
|
package/dist/encrypt.js
CHANGED
|
@@ -1,32 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var encrypt = (value) => {
|
|
6
|
-
const iv = crypto.randomBytes(12);
|
|
7
|
-
const cipher = crypto.createCipheriv(ALGORITHM, getKey(), iv);
|
|
8
|
-
const data = Buffer.concat([
|
|
9
|
-
cipher.update(JSON.stringify(value), "utf8"),
|
|
10
|
-
cipher.final()
|
|
11
|
-
]);
|
|
12
|
-
const tag = cipher.getAuthTag();
|
|
13
|
-
return [iv, tag, data].map((b) => b.toString("hex")).join(":");
|
|
14
|
-
};
|
|
15
|
-
var decrypt = (value) => {
|
|
16
|
-
if (typeof value !== "string") return value;
|
|
17
|
-
const [ivHex, tagHex, dataHex] = value.split(":");
|
|
18
|
-
const decipher = crypto.createDecipheriv(
|
|
19
|
-
ALGORITHM,
|
|
20
|
-
getKey(),
|
|
21
|
-
Buffer.from(ivHex, "hex")
|
|
22
|
-
);
|
|
23
|
-
decipher.setAuthTag(Buffer.from(tagHex, "hex"));
|
|
24
|
-
const result = Buffer.concat([
|
|
25
|
-
decipher.update(Buffer.from(dataHex, "hex")),
|
|
26
|
-
decipher.final()
|
|
27
|
-
]);
|
|
28
|
-
return JSON.parse(result.toString("utf8"));
|
|
29
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
decrypt,
|
|
3
|
+
encrypt
|
|
4
|
+
} from "./chunk-CUUQCM5T.js";
|
|
30
5
|
export {
|
|
31
6
|
decrypt,
|
|
32
7
|
encrypt
|
package/dist/shopify.cjs
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// shopify.js
|
|
30
|
+
var shopify_exports = {};
|
|
31
|
+
__export(shopify_exports, {
|
|
32
|
+
getAdminToken: () => getAdminToken,
|
|
33
|
+
refreshAdminToken: () => refreshAdminToken
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(shopify_exports);
|
|
36
|
+
|
|
37
|
+
// encrypt.js
|
|
38
|
+
var import_crypto = __toESM(require("crypto"), 1);
|
|
39
|
+
var ALGORITHM = "aes-256-gcm";
|
|
40
|
+
var getKey = () => Buffer.from(process.env.ENCRYPT_CONNECTION_SECRET, "hex");
|
|
41
|
+
var encrypt = (value) => {
|
|
42
|
+
const iv = import_crypto.default.randomBytes(12);
|
|
43
|
+
const cipher = import_crypto.default.createCipheriv(ALGORITHM, getKey(), iv);
|
|
44
|
+
const data = Buffer.concat([
|
|
45
|
+
cipher.update(JSON.stringify(value), "utf8"),
|
|
46
|
+
cipher.final()
|
|
47
|
+
]);
|
|
48
|
+
const tag = cipher.getAuthTag();
|
|
49
|
+
return [iv, tag, data].map((b) => b.toString("hex")).join(":");
|
|
50
|
+
};
|
|
51
|
+
var decrypt = (value) => {
|
|
52
|
+
if (typeof value !== "string") return value;
|
|
53
|
+
const [ivHex, tagHex, dataHex] = value.split(":");
|
|
54
|
+
const decipher = import_crypto.default.createDecipheriv(
|
|
55
|
+
ALGORITHM,
|
|
56
|
+
getKey(),
|
|
57
|
+
Buffer.from(ivHex, "hex")
|
|
58
|
+
);
|
|
59
|
+
decipher.setAuthTag(Buffer.from(tagHex, "hex"));
|
|
60
|
+
const result = Buffer.concat([
|
|
61
|
+
decipher.update(Buffer.from(dataHex, "hex")),
|
|
62
|
+
decipher.final()
|
|
63
|
+
]);
|
|
64
|
+
return JSON.parse(result.toString("utf8"));
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// shopify.js
|
|
68
|
+
var shopifyOAuthFetch = async (url, body) => {
|
|
69
|
+
const response = await fetch(url, {
|
|
70
|
+
method: "POST",
|
|
71
|
+
headers: {
|
|
72
|
+
"Content-Type": "application/json"
|
|
73
|
+
},
|
|
74
|
+
body: JSON.stringify(body)
|
|
75
|
+
});
|
|
76
|
+
if (!response.ok) {
|
|
77
|
+
const text = await response.text().catch(() => "");
|
|
78
|
+
throw new Error(response.status + ": " + text);
|
|
79
|
+
}
|
|
80
|
+
return response.json();
|
|
81
|
+
};
|
|
82
|
+
var refreshAdminToken = async ({ connection, controller }) => {
|
|
83
|
+
const settings = decrypt(connection.settings);
|
|
84
|
+
const { domain, refreshToken } = settings;
|
|
85
|
+
const data = await shopifyOAuthFetch(
|
|
86
|
+
`https://${domain}/admin/oauth/access_token`,
|
|
87
|
+
{
|
|
88
|
+
grant_type: "refresh_token",
|
|
89
|
+
client_id: process.env.SHOPIFY_API_KEY,
|
|
90
|
+
client_secret: process.env.SHOPIFY_API_SECRET,
|
|
91
|
+
refresh_token: refreshToken
|
|
92
|
+
}
|
|
93
|
+
);
|
|
94
|
+
const adminAccessToken = data.access_token;
|
|
95
|
+
const newRefreshToken = data.refresh_token;
|
|
96
|
+
const expiresIn = data.expires_in;
|
|
97
|
+
const tokenExpiresAt = expiresIn ? new Date(Date.now() + expiresIn * 1e3) : null;
|
|
98
|
+
const tokenWorkflow = await controller.get({
|
|
99
|
+
collection: "workflow",
|
|
100
|
+
query: {
|
|
101
|
+
connection: connection.id,
|
|
102
|
+
organization: connection.organization,
|
|
103
|
+
system: true,
|
|
104
|
+
title: "Shopify Token Activity"
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
await controller.transaction(async (session) => {
|
|
108
|
+
const options = { session };
|
|
109
|
+
await controller.update({
|
|
110
|
+
collection: "connection",
|
|
111
|
+
data: {
|
|
112
|
+
$set: {
|
|
113
|
+
settings: encrypt({
|
|
114
|
+
...settings,
|
|
115
|
+
adminAccessToken,
|
|
116
|
+
refreshToken: newRefreshToken,
|
|
117
|
+
tokenExpiresAt
|
|
118
|
+
})
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
options,
|
|
122
|
+
query: {
|
|
123
|
+
id: connection.id
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
if (tokenWorkflow) {
|
|
127
|
+
await controller.create({
|
|
128
|
+
collection: "action",
|
|
129
|
+
data: {
|
|
130
|
+
completedAt: /* @__PURE__ */ new Date(),
|
|
131
|
+
error: null,
|
|
132
|
+
errorStatus: null,
|
|
133
|
+
organization: connection.organization,
|
|
134
|
+
request: null,
|
|
135
|
+
response: null,
|
|
136
|
+
slug: "action.shopify.token.refresh",
|
|
137
|
+
status: "succeeded",
|
|
138
|
+
trigger: {
|
|
139
|
+
data: {
|
|
140
|
+
connection: connection.id
|
|
141
|
+
},
|
|
142
|
+
event: "shopify.token"
|
|
143
|
+
},
|
|
144
|
+
usage: null,
|
|
145
|
+
workflow: tokenWorkflow.id
|
|
146
|
+
},
|
|
147
|
+
options
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
;
|
|
151
|
+
});
|
|
152
|
+
return adminAccessToken;
|
|
153
|
+
};
|
|
154
|
+
var getAdminToken = async ({ connection, controller }) => {
|
|
155
|
+
const { adminAccessToken, tokenExpiresAt } = decrypt(connection.settings);
|
|
156
|
+
const needsRefresh = tokenExpiresAt && new Date(tokenExpiresAt) < new Date(Date.now() + 5 * 60 * 1e3);
|
|
157
|
+
if (needsRefresh) {
|
|
158
|
+
return refreshAdminToken({ connection, controller });
|
|
159
|
+
}
|
|
160
|
+
return adminAccessToken;
|
|
161
|
+
};
|
|
162
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
163
|
+
0 && (module.exports = {
|
|
164
|
+
getAdminToken,
|
|
165
|
+
refreshAdminToken
|
|
166
|
+
});
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { decrypt, encrypt } from './encrypt.cjs';
|
|
2
|
+
import 'crypto';
|
|
3
|
+
|
|
4
|
+
const shopifyOAuthFetch = async ( url, body ) => {
|
|
5
|
+
|
|
6
|
+
const response = await fetch( url, {
|
|
7
|
+
method : 'POST',
|
|
8
|
+
headers : {
|
|
9
|
+
'Content-Type' : 'application/json'
|
|
10
|
+
},
|
|
11
|
+
body : JSON.stringify( body )
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
if( ! response.ok ){
|
|
15
|
+
|
|
16
|
+
const text = await response.text().catch( () => '' );
|
|
17
|
+
|
|
18
|
+
throw new Error( response.status + ': ' + text );
|
|
19
|
+
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return response.json();
|
|
23
|
+
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const refreshAdminToken = async ({ connection, controller }) => {
|
|
27
|
+
|
|
28
|
+
const settings = decrypt( connection.settings );
|
|
29
|
+
const { domain, refreshToken } = settings;
|
|
30
|
+
|
|
31
|
+
const data = await shopifyOAuthFetch(
|
|
32
|
+
`https://${ domain }/admin/oauth/access_token`,
|
|
33
|
+
{
|
|
34
|
+
grant_type : 'refresh_token',
|
|
35
|
+
client_id : process.env.SHOPIFY_API_KEY,
|
|
36
|
+
client_secret : process.env.SHOPIFY_API_SECRET,
|
|
37
|
+
refresh_token : refreshToken
|
|
38
|
+
}
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const adminAccessToken = data.access_token;
|
|
42
|
+
const newRefreshToken = data.refresh_token;
|
|
43
|
+
const expiresIn = data.expires_in;
|
|
44
|
+
const tokenExpiresAt = expiresIn ? new Date( Date.now() + ( expiresIn * 1000 ) ) : null;
|
|
45
|
+
|
|
46
|
+
const tokenWorkflow = await controller.get({
|
|
47
|
+
collection : 'workflow',
|
|
48
|
+
query : {
|
|
49
|
+
connection : connection.id,
|
|
50
|
+
organization : connection.organization,
|
|
51
|
+
system : true,
|
|
52
|
+
title : 'Shopify Token Activity'
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
await controller.transaction( async ( session ) => {
|
|
57
|
+
|
|
58
|
+
const options = { session };
|
|
59
|
+
|
|
60
|
+
await controller.update({
|
|
61
|
+
collection : 'connection',
|
|
62
|
+
data : {
|
|
63
|
+
$set : {
|
|
64
|
+
settings : encrypt({
|
|
65
|
+
...settings,
|
|
66
|
+
adminAccessToken,
|
|
67
|
+
refreshToken : newRefreshToken,
|
|
68
|
+
tokenExpiresAt
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
options,
|
|
73
|
+
query : {
|
|
74
|
+
id : connection.id
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
if( tokenWorkflow ){
|
|
79
|
+
|
|
80
|
+
await controller.create({
|
|
81
|
+
collection : 'action',
|
|
82
|
+
data : {
|
|
83
|
+
completedAt : new Date(),
|
|
84
|
+
error : null,
|
|
85
|
+
errorStatus : null,
|
|
86
|
+
organization : connection.organization,
|
|
87
|
+
request : null,
|
|
88
|
+
response : null,
|
|
89
|
+
slug : 'action.shopify.token.refresh',
|
|
90
|
+
status : 'succeeded',
|
|
91
|
+
trigger : {
|
|
92
|
+
data : {
|
|
93
|
+
connection : connection.id
|
|
94
|
+
},
|
|
95
|
+
event : 'shopify.token'
|
|
96
|
+
},
|
|
97
|
+
usage : null,
|
|
98
|
+
workflow : tokenWorkflow.id
|
|
99
|
+
},
|
|
100
|
+
options
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
}
|
|
104
|
+
} );
|
|
105
|
+
|
|
106
|
+
return adminAccessToken;
|
|
107
|
+
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const getAdminToken = async ({ connection, controller }) => {
|
|
111
|
+
|
|
112
|
+
const { adminAccessToken, tokenExpiresAt } = decrypt( connection.settings );
|
|
113
|
+
|
|
114
|
+
const needsRefresh = tokenExpiresAt &&
|
|
115
|
+
new Date( tokenExpiresAt ) < new Date( Date.now() + ( 5 * 60 * 1000 ) );
|
|
116
|
+
|
|
117
|
+
if( needsRefresh ){
|
|
118
|
+
|
|
119
|
+
return refreshAdminToken({ connection, controller });
|
|
120
|
+
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return adminAccessToken;
|
|
124
|
+
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
export { getAdminToken, refreshAdminToken };
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { decrypt, encrypt } from './encrypt.js';
|
|
2
|
+
import 'crypto';
|
|
3
|
+
|
|
4
|
+
const shopifyOAuthFetch = async ( url, body ) => {
|
|
5
|
+
|
|
6
|
+
const response = await fetch( url, {
|
|
7
|
+
method : 'POST',
|
|
8
|
+
headers : {
|
|
9
|
+
'Content-Type' : 'application/json'
|
|
10
|
+
},
|
|
11
|
+
body : JSON.stringify( body )
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
if( ! response.ok ){
|
|
15
|
+
|
|
16
|
+
const text = await response.text().catch( () => '' );
|
|
17
|
+
|
|
18
|
+
throw new Error( response.status + ': ' + text );
|
|
19
|
+
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return response.json();
|
|
23
|
+
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const refreshAdminToken = async ({ connection, controller }) => {
|
|
27
|
+
|
|
28
|
+
const settings = decrypt( connection.settings );
|
|
29
|
+
const { domain, refreshToken } = settings;
|
|
30
|
+
|
|
31
|
+
const data = await shopifyOAuthFetch(
|
|
32
|
+
`https://${ domain }/admin/oauth/access_token`,
|
|
33
|
+
{
|
|
34
|
+
grant_type : 'refresh_token',
|
|
35
|
+
client_id : process.env.SHOPIFY_API_KEY,
|
|
36
|
+
client_secret : process.env.SHOPIFY_API_SECRET,
|
|
37
|
+
refresh_token : refreshToken
|
|
38
|
+
}
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const adminAccessToken = data.access_token;
|
|
42
|
+
const newRefreshToken = data.refresh_token;
|
|
43
|
+
const expiresIn = data.expires_in;
|
|
44
|
+
const tokenExpiresAt = expiresIn ? new Date( Date.now() + ( expiresIn * 1000 ) ) : null;
|
|
45
|
+
|
|
46
|
+
const tokenWorkflow = await controller.get({
|
|
47
|
+
collection : 'workflow',
|
|
48
|
+
query : {
|
|
49
|
+
connection : connection.id,
|
|
50
|
+
organization : connection.organization,
|
|
51
|
+
system : true,
|
|
52
|
+
title : 'Shopify Token Activity'
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
await controller.transaction( async ( session ) => {
|
|
57
|
+
|
|
58
|
+
const options = { session };
|
|
59
|
+
|
|
60
|
+
await controller.update({
|
|
61
|
+
collection : 'connection',
|
|
62
|
+
data : {
|
|
63
|
+
$set : {
|
|
64
|
+
settings : encrypt({
|
|
65
|
+
...settings,
|
|
66
|
+
adminAccessToken,
|
|
67
|
+
refreshToken : newRefreshToken,
|
|
68
|
+
tokenExpiresAt
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
options,
|
|
73
|
+
query : {
|
|
74
|
+
id : connection.id
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
if( tokenWorkflow ){
|
|
79
|
+
|
|
80
|
+
await controller.create({
|
|
81
|
+
collection : 'action',
|
|
82
|
+
data : {
|
|
83
|
+
completedAt : new Date(),
|
|
84
|
+
error : null,
|
|
85
|
+
errorStatus : null,
|
|
86
|
+
organization : connection.organization,
|
|
87
|
+
request : null,
|
|
88
|
+
response : null,
|
|
89
|
+
slug : 'action.shopify.token.refresh',
|
|
90
|
+
status : 'succeeded',
|
|
91
|
+
trigger : {
|
|
92
|
+
data : {
|
|
93
|
+
connection : connection.id
|
|
94
|
+
},
|
|
95
|
+
event : 'shopify.token'
|
|
96
|
+
},
|
|
97
|
+
usage : null,
|
|
98
|
+
workflow : tokenWorkflow.id
|
|
99
|
+
},
|
|
100
|
+
options
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
}
|
|
104
|
+
} );
|
|
105
|
+
|
|
106
|
+
return adminAccessToken;
|
|
107
|
+
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const getAdminToken = async ({ connection, controller }) => {
|
|
111
|
+
|
|
112
|
+
const { adminAccessToken, tokenExpiresAt } = decrypt( connection.settings );
|
|
113
|
+
|
|
114
|
+
const needsRefresh = tokenExpiresAt &&
|
|
115
|
+
new Date( tokenExpiresAt ) < new Date( Date.now() + ( 5 * 60 * 1000 ) );
|
|
116
|
+
|
|
117
|
+
if( needsRefresh ){
|
|
118
|
+
|
|
119
|
+
return refreshAdminToken({ connection, controller });
|
|
120
|
+
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return adminAccessToken;
|
|
124
|
+
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
export { getAdminToken, refreshAdminToken };
|
package/dist/shopify.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import {
|
|
2
|
+
decrypt,
|
|
3
|
+
encrypt
|
|
4
|
+
} from "./chunk-CUUQCM5T.js";
|
|
5
|
+
|
|
6
|
+
// shopify.js
|
|
7
|
+
var shopifyOAuthFetch = async (url, body) => {
|
|
8
|
+
const response = await fetch(url, {
|
|
9
|
+
method: "POST",
|
|
10
|
+
headers: {
|
|
11
|
+
"Content-Type": "application/json"
|
|
12
|
+
},
|
|
13
|
+
body: JSON.stringify(body)
|
|
14
|
+
});
|
|
15
|
+
if (!response.ok) {
|
|
16
|
+
const text = await response.text().catch(() => "");
|
|
17
|
+
throw new Error(response.status + ": " + text);
|
|
18
|
+
}
|
|
19
|
+
return response.json();
|
|
20
|
+
};
|
|
21
|
+
var refreshAdminToken = async ({ connection, controller }) => {
|
|
22
|
+
const settings = decrypt(connection.settings);
|
|
23
|
+
const { domain, refreshToken } = settings;
|
|
24
|
+
const data = await shopifyOAuthFetch(
|
|
25
|
+
`https://${domain}/admin/oauth/access_token`,
|
|
26
|
+
{
|
|
27
|
+
grant_type: "refresh_token",
|
|
28
|
+
client_id: process.env.SHOPIFY_API_KEY,
|
|
29
|
+
client_secret: process.env.SHOPIFY_API_SECRET,
|
|
30
|
+
refresh_token: refreshToken
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
const adminAccessToken = data.access_token;
|
|
34
|
+
const newRefreshToken = data.refresh_token;
|
|
35
|
+
const expiresIn = data.expires_in;
|
|
36
|
+
const tokenExpiresAt = expiresIn ? new Date(Date.now() + expiresIn * 1e3) : null;
|
|
37
|
+
const tokenWorkflow = await controller.get({
|
|
38
|
+
collection: "workflow",
|
|
39
|
+
query: {
|
|
40
|
+
connection: connection.id,
|
|
41
|
+
organization: connection.organization,
|
|
42
|
+
system: true,
|
|
43
|
+
title: "Shopify Token Activity"
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
await controller.transaction(async (session) => {
|
|
47
|
+
const options = { session };
|
|
48
|
+
await controller.update({
|
|
49
|
+
collection: "connection",
|
|
50
|
+
data: {
|
|
51
|
+
$set: {
|
|
52
|
+
settings: encrypt({
|
|
53
|
+
...settings,
|
|
54
|
+
adminAccessToken,
|
|
55
|
+
refreshToken: newRefreshToken,
|
|
56
|
+
tokenExpiresAt
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
options,
|
|
61
|
+
query: {
|
|
62
|
+
id: connection.id
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
if (tokenWorkflow) {
|
|
66
|
+
await controller.create({
|
|
67
|
+
collection: "action",
|
|
68
|
+
data: {
|
|
69
|
+
completedAt: /* @__PURE__ */ new Date(),
|
|
70
|
+
error: null,
|
|
71
|
+
errorStatus: null,
|
|
72
|
+
organization: connection.organization,
|
|
73
|
+
request: null,
|
|
74
|
+
response: null,
|
|
75
|
+
slug: "action.shopify.token.refresh",
|
|
76
|
+
status: "succeeded",
|
|
77
|
+
trigger: {
|
|
78
|
+
data: {
|
|
79
|
+
connection: connection.id
|
|
80
|
+
},
|
|
81
|
+
event: "shopify.token"
|
|
82
|
+
},
|
|
83
|
+
usage: null,
|
|
84
|
+
workflow: tokenWorkflow.id
|
|
85
|
+
},
|
|
86
|
+
options
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
;
|
|
90
|
+
});
|
|
91
|
+
return adminAccessToken;
|
|
92
|
+
};
|
|
93
|
+
var getAdminToken = async ({ connection, controller }) => {
|
|
94
|
+
const { adminAccessToken, tokenExpiresAt } = decrypt(connection.settings);
|
|
95
|
+
const needsRefresh = tokenExpiresAt && new Date(tokenExpiresAt) < new Date(Date.now() + 5 * 60 * 1e3);
|
|
96
|
+
if (needsRefresh) {
|
|
97
|
+
return refreshAdminToken({ connection, controller });
|
|
98
|
+
}
|
|
99
|
+
return adminAccessToken;
|
|
100
|
+
};
|
|
101
|
+
export {
|
|
102
|
+
getAdminToken,
|
|
103
|
+
refreshAdminToken
|
|
104
|
+
};
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"axios": "1.16.0",
|
|
5
5
|
"currency-codes": "2.2.0",
|
|
6
6
|
"nanoid": "3.3.8",
|
|
7
|
-
"qs": "6.15.
|
|
7
|
+
"qs": "6.15.2",
|
|
8
8
|
"slugify": "1.6.6",
|
|
9
9
|
"tsup": "8.5.1",
|
|
10
10
|
"typescript": "5.9.3"
|
|
@@ -81,5 +81,5 @@
|
|
|
81
81
|
"build": "tsup && npm publish"
|
|
82
82
|
},
|
|
83
83
|
"types": "dist/index.d.ts",
|
|
84
|
-
"version": "0.0.
|
|
84
|
+
"version": "0.0.18"
|
|
85
85
|
}
|