-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
54 lines (44 loc) · 1.37 KB
/
index.js
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
// Dotenv to eviroment variables
import 'https://deno.land/x/[email protected]/load.ts'
// oak middleware server
import { Application, Router } from 'https://deno.land/x/[email protected]/mod.ts'
// // supabase is a database as a service?
import { createClient } from 'https://cdn.jsdelivr.net/npm/@supabase/supabase-js/+esm'
// Export Eviroment variables from .env file :D
const { SUPABASE_URL, CLIENT_KEY } = Deno.env.toObject()
// create supabase client
const supabase = createClient(SUPABASE_URL, CLIENT_KEY)
// declare get function
async function getData(currency) {
const { data, error } = await supabase
.rpc('get_last_bolivar_exchange', {
curr: currency || 'any'
})
.select('*')
if (error) throw error
else return data
}
// declare handler function
async function handler(ctx) {
const { curr } = ctx?.params
try {
ctx.response.body = await getData(curr)
} catch (error) {
ctx.response.body = { error }
}
}
// create server
const app = new Application()
// declare router object
const router = new Router()
// declare route to get bolivar exchanges from supabase
router.get('/v1/exchange', async ctx => {
await handler(ctx)
})
router.get('/v1/exchange/:curr', async ctx => {
await handler(ctx)
})
// use all routes
app.use(router.routes())
app.use(router.allowedMethods())
app.listen({ port: 3000 }).then(() => console.log('Listening on port 3000'))