Skip to content

Commit 09aa722

Browse files
author
Adam Siekierski
committed
Redirecting function
1 parent a46d351 commit 09aa722

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

api/[slug].ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
import { NowRequest, NowResponse } from "@vercel/node";
2+
import faunadb from "faunadb";
3+
4+
const secret = process.env.FAUNADB_SECRET;
5+
const q = faunadb.query;
6+
const client = new faunadb.Client({ secret });
7+
8+
interface LinkQueryResult {
9+
data: {
10+
slug: string;
11+
destination: string;
12+
};
13+
}
214

315
export default (req: NowRequest, res: NowResponse) => {
4-
return res.json({
5-
message: "hello world!",
6-
});
16+
return client
17+
.query(q.Get(q.Match(q.Index("links"), req.query.slug)))
18+
.then((dbs: LinkQueryResult) => {
19+
res.redirect(dbs.data.destination);
20+
})
21+
.catch(() => res.status(404).send("Link not found"));
722
};

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
},
99
"dependencies": {
1010
"@vercel/node": "^1.8.4",
11+
"faunadb": "^3.0.1",
1112
"vercel": "^20.1.2"
1213
}
1314
}

yarn.lock

+74
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ arg@^4.1.0:
7777
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
7878
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
7979

80+
base64-js@^1.2.0:
81+
version "1.3.1"
82+
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.3.1.tgz#58ece8cb75dd07e71ed08c736abc5fac4dbf8df1"
83+
integrity sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g==
84+
8085
boxen@^4.2.0:
8186
version "4.2.0"
8287
resolved "https://registry.yarnpkg.com/boxen/-/boxen-4.2.0.tgz#e411b62357d6d6d36587c8ac3d5d974daa070e64"
@@ -91,6 +96,11 @@ boxen@^4.2.0:
9196
type-fest "^0.8.1"
9297
widest-line "^3.1.0"
9398

99+
btoa-lite@^1.0.0:
100+
version "1.0.0"
101+
resolved "https://registry.yarnpkg.com/btoa-lite/-/btoa-lite-1.0.0.tgz#337766da15801210fdd956c22e9c6891ab9d0337"
102+
integrity sha1-M3dm2hWAEhD92VbCLpxokaudAzc=
103+
94104
buffer-from@^1.0.0:
95105
version "1.1.1"
96106
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
@@ -163,6 +173,13 @@ configstore@^5.0.1:
163173
write-file-atomic "^3.0.0"
164174
xdg-basedir "^4.0.0"
165175

176+
cross-fetch@^3.0.4:
177+
version "3.0.6"
178+
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.0.6.tgz#3a4040bc8941e653e0e9cf17f29ebcd177d3365c"
179+
integrity sha512-KBPUbqgFjzWlVcURG+Svp9TlhA5uliYtiNx/0r8nv0pdypeQCRJ9IaSIc3q/x3q8t3F75cHuwxVql1HFGHCNJQ==
180+
dependencies:
181+
node-fetch "2.6.1"
182+
166183
crypto-random-string@^2.0.0:
167184
version "2.0.0"
168185
resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-2.0.0.tgz#ef2a7a966ec11083388369baa02ebead229b30d5"
@@ -197,6 +214,11 @@ dot-prop@^5.2.0:
197214
dependencies:
198215
is-obj "^2.0.0"
199216

217+
dotenv@^8.2.0:
218+
version "8.2.0"
219+
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
220+
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==
221+
200222
duplexer3@^0.1.4:
201223
version "0.1.4"
202224
resolved "https://registry.yarnpkg.com/duplexer3/-/duplexer3-0.1.4.tgz#ee01dd1cac0ed3cbc7fdbea37dc0a8f1ce002ce2"
@@ -224,6 +246,25 @@ escape-goat@^2.0.0:
224246
resolved "https://registry.yarnpkg.com/escape-goat/-/escape-goat-2.1.1.tgz#1b2dc77003676c457ec760b2dc68edb648188675"
225247
integrity sha512-8/uIhbG12Csjy2JEW7D9pHbreaVaS/OpN3ycnyvElTdwM5n6GY6W6e2IPemfvGZeUMqZ9A/3GqIZMgKnBhAw/Q==
226248

249+
faunadb@^3.0.1:
250+
version "3.0.1"
251+
resolved "https://registry.yarnpkg.com/faunadb/-/faunadb-3.0.1.tgz#b31f9e2db865c6120bd2c085c9087fc2be42a006"
252+
integrity sha512-WlfPjC0V9xHs4NTunOWmYZtJfbJ45Z1VAIKKka6+mRrmijWOFQzJVDY9CqS6X9kvepM36EjmtNkIvV0OJ1wTEA==
253+
dependencies:
254+
base64-js "^1.2.0"
255+
btoa-lite "^1.0.0"
256+
cross-fetch "^3.0.4"
257+
dotenv "^8.2.0"
258+
fn-annotate "^1.1.3"
259+
object-assign "^4.1.0"
260+
url-parse "^1.4.7"
261+
util-deprecate "^1.0.2"
262+
263+
fn-annotate@^1.1.3:
264+
version "1.2.0"
265+
resolved "https://registry.yarnpkg.com/fn-annotate/-/fn-annotate-1.2.0.tgz#28da000117dea61842fe61f353f41cf4c93a7a7e"
266+
integrity sha1-KNoAARfephhC/mHzU/Qc9Mk6en4=
267+
227268
get-stream@^4.1.0:
228269
version "4.1.0"
229270
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-4.1.0.tgz#c1b255575f3dc21d59bfc79cd3d2b46b1c3a54b5"
@@ -398,11 +439,21 @@ minimist@^1.2.0:
398439
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602"
399440
integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==
400441

442+
443+
version "2.6.1"
444+
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
445+
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
446+
401447
normalize-url@^4.1.0:
402448
version "4.5.0"
403449
resolved "https://registry.yarnpkg.com/normalize-url/-/normalize-url-4.5.0.tgz#453354087e6ca96957bd8f5baf753f5982142129"
404450
integrity sha512-2s47yzUxdexf1OhyRi4Em83iQk0aPvwTddtFz4hnSSw9dCEsLEGf6SwIO8ss/19S9iBb5sJaOuTvTGDeZI00BQ==
405451

452+
object-assign@^4.1.0:
453+
version "4.1.1"
454+
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
455+
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=
456+
406457
once@^1.3.1, once@^1.4.0:
407458
version "1.4.0"
408459
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
@@ -445,6 +496,11 @@ pupa@^2.0.1:
445496
dependencies:
446497
escape-goat "^2.0.0"
447498

499+
querystringify@^2.1.1:
500+
version "2.2.0"
501+
resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-2.2.0.tgz#3345941b4153cb9d082d8eee4cda2016a9aef7f6"
502+
integrity sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==
503+
448504
rc@^1.2.8:
449505
version "1.2.8"
450506
resolved "https://registry.yarnpkg.com/rc/-/rc-1.2.8.tgz#cd924bf5200a075b83c188cd6b9e211b7fc0d3ed"
@@ -469,6 +525,11 @@ registry-url@^5.0.0:
469525
dependencies:
470526
rc "^1.2.8"
471527

528+
requires-port@^1.0.0:
529+
version "1.0.0"
530+
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"
531+
integrity sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8=
532+
472533
responselike@^1.0.2:
473534
version "1.0.2"
474535
resolved "https://registry.yarnpkg.com/responselike/-/responselike-1.0.2.tgz#918720ef3b631c5642be068f15ade5a46f4ba1e7"
@@ -621,6 +682,19 @@ url-parse-lax@^3.0.0:
621682
dependencies:
622683
prepend-http "^2.0.0"
623684

685+
url-parse@^1.4.7:
686+
version "1.4.7"
687+
resolved "https://registry.yarnpkg.com/url-parse/-/url-parse-1.4.7.tgz#a8a83535e8c00a316e403a5db4ac1b9b853ae278"
688+
integrity sha512-d3uaVyzDB9tQoSXFvuSUNFibTd9zxd2bkVrDRvF5TmvWWQwqE4lgYJ5m+x1DbecWkw+LK4RNl2CU1hHuOKPVlg==
689+
dependencies:
690+
querystringify "^2.1.1"
691+
requires-port "^1.0.0"
692+
693+
util-deprecate@^1.0.2:
694+
version "1.0.2"
695+
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
696+
integrity sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=
697+
624698
vercel@^20.1.2:
625699
version "20.1.2"
626700
resolved "https://registry.yarnpkg.com/vercel/-/vercel-20.1.2.tgz#5338b38b7e53d1218144414ea462707d3fae31b6"

0 commit comments

Comments
 (0)