Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
152 lines (134 loc) · 4.16 KB
/
Copy pathindex.js
File metadata and controls
152 lines (134 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import fs from "node:fs";
import path from "node:path";
import crypto from "node:crypto";
import { fileURLToPath, pathToFileURL } from "node:url";
const API_URL = "https://open-api.affiliate.shopee.vn/graphql";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export function loadEnv(filePath) {
if (!fs.existsSync(filePath)) {
throw new Error(".env not found. Copy .env.example to .env first.");
}
const content = fs.readFileSync(filePath, "utf8");
const env = {};
for (const rawLine of content.split("\n")) {
const line = rawLine.trim();
if (!line || line.startsWith("#")) continue;
const [key, ...rest] = line.split("=");
if (!key || rest.length === 0) continue;
env[key.trim()] = rest
.join("=")
.trim()
.replace(/^['"]|['"]$/g, "");
}
return env;
}
export function buildPayload(apiName, inputUrl) {
const queries = {
shopeeOfferV2: `
{
shopeeOfferV2(keyword: "phone", sortType: 1, page: 1, limit: 5) {
nodes { offerName offerLink commissionRate }
pageInfo { page limit hasNextPage }
}
}
`,
brandOfferV2: `
{
brandOffer(keyword: "phone", sortType: 1, page: 1, limit: 5) {
nodes { offerName offerLink commissionRate }
pageInfo { page limit hasNextPage }
}
}
`,
productOfferV2: `
{
productOfferV2(keyword: "phone", sortType: 1, page: 1, limit: 5) {
nodes { productName offerLink commissionRate sales }
pageInfo { page limit hasNextPage }
}
}
`,
generateShortLink: `
mutation {
generateShortLink(input: { originUrl: "${inputUrl}", subIds: ["s1"] }) {
shortLink
}
}
`,
conversionReportV2: `
{
conversionReport(limit: 5) {
nodes { conversionId purchaseTime totalCommission }
pageInfo { scrollId }
}
}
`,
validationReportV2: `
{
validatedReport(validationId: 1, limit: 5) {
nodes { conversionId purchaseTime totalCommission }
pageInfo { scrollId }
}
}
`,
};
if (!queries[apiName]) {
const supported = Object.keys(queries).join(", ");
throw new Error(`Unsupported api name: ${apiName}. Supported: ${supported}`);
}
return JSON.stringify({ query: queries[apiName] });
}
export function buildAuthorization(appId, secret, payload, timestamp) {
const signatureBase = `${appId}${timestamp}${payload}${secret}`;
const signature = crypto.createHash("sha256").update(signatureBase).digest("hex");
return `SHA256 Credential=${appId}, Timestamp=${timestamp}, Signature=${signature}`;
}
export async function callShopeeApi() {
const env = loadEnv(path.join(__dirname, ".env"));
const appId = env.SHOPEE_API_APP_ID || "";
const secret = env.SHOPEE_API_SECRET || "";
if (!appId || !secret) {
throw new Error("Missing SHOPEE_API_APP_ID or SHOPEE_API_SECRET in .env");
}
const apiName = process.argv[2] || "shopeeOfferV2";
const inputUrl = process.argv[3] || "https://shopee.vn";
const payload = buildPayload(apiName, inputUrl);
const timestamp = Math.floor(Date.now() / 1000);
const authorization = buildAuthorization(appId, secret, payload, timestamp);
const response = await fetch(API_URL, {
method: "POST",
headers: {
Authorization: authorization,
"Content-Type": "application/json",
},
body: payload,
});
const json = await response.json();
return {
api: apiName,
httpCode: response.status,
response: json,
};
}
const isDirectRun = process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href;
if (isDirectRun) {
callShopeeApi()
.then((data) => {
console.log(JSON.stringify(data, null, 2));
})
.catch((error) => {
console.error(
JSON.stringify(
{
success: false,
error: error.message,
usage: "node index.js [apiName] [originUrl-for-generateShortLink]",
},
null,
2,
),
);
process.exitCode = 1;
});
}