-
Notifications
You must be signed in to change notification settings - Fork 2
/
snow.ts
95 lines (82 loc) · 2.71 KB
/
snow.ts
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
/* Deploy this on a nodejs runtime or simply run in local and connect to cloudflare tunnels */
// import { env } from "hono/adapter";
// import { createRoute } from "honox/factory";
// import snowflake from "snowflake-sdk";
// export const POST = createRoute(async (c) => {
// const token = c.req.header("Authorization");
// if (!token || !token.startsWith("Bearer ")) {
// return new Response("Unauthorized", {
// status: 401,
// headers: { "Content-Type": "application/json" },
// });
// }
// const bearerToken = token.substring(7); // Remove "Bearer " prefix
// if (bearerToken !== env<{ TOKEN: string }>(c).TOKEN) {
// return new Response("Unauthorized", {
// status: 401,
// headers: { "Content-Type": "application/json" },
// });
// }
// const requestBody = await c.req.json();
// console.log(requestBody);
// const config = env<{
// ACCOUNT: string;
// USER_NAME: string;
// PASSWORD: string;
// ROLE: string;
// WAREHOUSE: string;
// DATABASE: string;
// SCHEMA: string;
// }>(c);
// const snowConnect = snowflake.createConnection({
// account: config.ACCOUNT,
// username: config.USER_NAME,
// password: config.PASSWORD,
// role: config.ROLE,
// warehouse: config.WAREHOUSE,
// database: config.DATABASE,
// schema: config.SCHEMA,
// });
// snowflake.configure({ ocspFailOpen: false });
// const query = requestBody.query;
// try {
// const result = await new Promise<any[]>((resolve, reject) => {
// snowConnect.connect((err, conn) => {
// if (err) {
// console.error("Unable to connect: " + err.message);
// reject(err);
// } else {
// snowConnect.execute({
// sqlText: query,
// complete: (err, stmt, rows) => {
// if (err) {
// console.error(
// "Failed to execute statement due to the following error: " +
// err.message
// );
// reject(err);
// } else {
// resolve(rows || []);
// }
// },
// });
// }
// });
// });
// // const columns = result.length > 0 ? Object.keys(result[0]) : [];
// // const formattedResult = {
// // columns,
// // data: result,
// // };
// return new Response(JSON.stringify(result), {
// status: 200,
// headers: { "Content-Type": "application/json" },
// });
// } catch (error) {
// console.error(error);
// return new Response(JSON.stringify({ error: error }), {
// status: 500,
// headers: { "Content-Type": "application/json" },
// });
// }
// });