-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin.js
43 lines (34 loc) · 1.23 KB
/
plugin.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
'use strict'
const fp = require('fastify-plugin')
const { createClient } = require('@supabase/supabase-js')
function fastifySupabase (fastify, options, next) {
const { namespace, supabaseKey, supabaseUrl, ...supabaseOptions } = options
if (!supabaseKey) {
return next(new Error('You must provide a Supabase API key'))
}
if (!supabaseUrl) {
return next(new Error('You must provide a Supabase Project URL'))
}
const supabase = createClient(supabaseUrl, supabaseKey, supabaseOptions)
if (namespace) {
if (supabase[namespace]) {
return next(new Error(`fastify-supabase '${namespace}' is a reserved keyword`))
} else if (!fastify.supabase) {
fastify.decorate('supabase', Object.create(null))
} else if (Object.prototype.hasOwnProperty.call(fastify.supabase, namespace)) {
return next(new Error(`Supabase client '${namespace}' instance name has already been registered`))
}
fastify.supabase[namespace] = supabase
} else {
if (fastify.supabase) {
return next(new Error('fastify-supabase has already been registered'))
} else {
fastify.decorate('supabase', supabase)
}
}
next()
}
module.exports = fp(fastifySupabase, {
fastify: '>=3.0.0',
name: 'fastify-supabase'
})