-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
180 lines (162 loc) · 4.93 KB
/
server.js
File metadata and controls
180 lines (162 loc) · 4.93 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
require("isomorphic-fetch");
const dotenv = require("dotenv");
const Koa = require("koa");
const next = require("next");
const { default: createShopifyAuth } = require("@shopify/koa-shopify-auth");
const { verifyRequest } = require("@shopify/koa-shopify-auth");
const session = require("koa-session");
const Router = require("koa-router");
const router = new Router();
const fetch = require("node-fetch");
dotenv.config();
const { default: graphQLProxy } = require("@shopify/koa-shopify-graphql-proxy");
const { ApiVersion } = require("@shopify/koa-shopify-graphql-proxy");
const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
const { SHOPIFY_API_SECRET_KEY, SHOPIFY_API_KEY } = process.env;
app.prepare().then(() => {
const server = new Koa();
server.use(session({ secure: true, sameSite: "none" }, server));
server.keys = [SHOPIFY_API_SECRET_KEY];
server.use(
createShopifyAuth({
apiKey: SHOPIFY_API_KEY,
secret: SHOPIFY_API_SECRET_KEY,
scopes: ["read_products", "write_products", "write_script_tags"],
afterAuth(ctx) {
const { shop, accessToken } = ctx.session;
ctx.cookies.set("shopOrigin", shop, {
httpOnly: false,
secure: true,
sameSite: "none",
});
ctx.cookies.set("accessToken", accessToken, {
httpOnly: false,
secure: true,
sameSite: "none",
});
ctx.redirect("/");
},
})
);
function makeResponse(ctx) {
return {
status: ctx.status,
receivedAt: new Date(),
result: "Webhook successful",
body: ctx.request.body,
};
}
// GDPR endpoints
router.post("/customers/redact", (ctx) => {
ctx.status = 200;
ctx.body = makeResponse(ctx);
});
router.post("/shop/redact", (ctx) => {
ctx.status = 200;
ctx.body = makeResponse(ctx);
});
router.post("/customers/data_request", (ctx) => {
ctx.status = 200;
ctx.body = makeResponse(ctx);
});
// End GDPR endpoints
// Create threekit shop-wide metafield
router.get("/api/makeMeta/:value", async (ctx) => {
try {
const results = await fetch(
"https://" +
ctx.cookies.get("shopOrigin") +
"/admin/api/2023-01/metafields.json",
{
headers: {
"X-Shopify-Access-Token": ctx.cookies.get("accessToken"),
Accept: "application/json",
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
metafield: {
namespace: "threekit",
key: "token",
value: `${ctx.params.value}`,
value_type: "string",
},
}),
}
)
.then((response) => response.json())
.then((json) => {
// console.log(JSON.parse(json))
return json;
});
ctx.body = {
status: "success",
data: results,
};
} catch (err) {
console.log(err);
}
});
// Insert threekit-products into the metafield.
// You will need to get all of the values and then submit them together in order to retain data
router.get("/api/insertMeta/:id/:value", async (ctx) => {
try {
const results = await fetch(
"https://" +
ctx.cookies.get("shopOrigin") +
"/admin/api/2023-01/metafields/" +
ctx.params.id +
".json",
{
headers: {
"X-Shopify-Access-Token": ctx.cookies.get("accessToken"),
Accept: "application/json",
"Content-Type": "application/json",
},
method: "PUT",
body: JSON.stringify({
metafield: {
id: ctx.params.id,
value: `${ctx.params.value}`,
value_type: "string",
},
}),
}
)
.then((response) => response.json())
.then((json) => {
// console.log(JSON.parse(json))
return json;
});
ctx.body = {
status: "success",
data: results,
};
} catch (err) {
console.log(err);
}
});
// Create threekit shop-wide metafield
// Routes for API
server.use(router.routes());
server.use(graphQLProxy({ version: ApiVersion.October19 }));
server.use(verifyRequest({ authRoute: "/auth", fallbackRoute: "/auth" }));
server.use(async (ctx) => {
await handle(ctx.req, ctx.res);
ctx.respond = false;
ctx.res.statusCode = 200;
return;
});
server.use(async (ctx) => {
await handle(ctx.req, ctx.res);
ctx.respond = false;
ctx.res.statusCode = 200;
return;
});
server.listen(port, () => {
console.log(`> Ready on http://localhost:${port}`);
});
});