Created
June 23, 2023 07:48
-
-
Save Razikus/4078f7ae6f9413d02c00af29b250bf41 to your computer and use it in GitHub Desktop.
Supabase immudb vault hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Follow this setup guide to integrate the Deno language server with your editor: | |
// https://deno.land/manual/getting_started/setup_your_environment | |
// This enables autocomplete, go to definition, etc. | |
import { serve } from "https://deno.land/[email protected]/http/server.ts" | |
import * as postgres from 'https://deno.land/x/[email protected]/mod.ts' | |
console.log("FUNCTION VAULTSIGN LOADED") | |
const databaseUrl = Deno.env.get('SUPABASE_DB_URL')! | |
const secret = Deno.env.get('APIKEY_SECRET_INTEREXCHANGE')! | |
const pool = new postgres.Pool(databaseUrl, 3, true) | |
serve(async (req) => { | |
let apikey = req.headers.get("X-API-KEY") | |
if (!apikey) { | |
return new Response( | |
JSON.stringify({ error: "Missing X-API-KEY header" }), | |
{ status: 401 }, | |
) | |
} | |
if (apikey !== secret) { | |
return new Response( | |
JSON.stringify({ error: "Invalid X-API-KEY header" }), | |
{ status: 401 }, | |
) | |
} | |
const bodyOf = await req.json() | |
let stringified = JSON.stringify(bodyOf) | |
try { | |
const connection = await pool.connect() | |
const isSignEnabled = await connection.queryObject(`SELECT vault_sign, vault_sign_apikey, vault_collection FROM "yourtable_withcredentials" WHERE id = ${bodyOf.record.project_id} LIMIT 1`) | |
if(isSignEnabled.rows.length == 0) { | |
let body = JSON.stringify({ message: 'Project not found' }) | |
return new Response(body, { | |
status: 400, | |
headers: { | |
'Content-Type': 'application/json; charset=utf-8', | |
}, | |
}) | |
} | |
let isSignEnabledValue = isSignEnabled.rows[0].vault_sign | |
let apiKey = isSignEnabled.rows[0].vault_sign_apikey | |
let vaultCollection = isSignEnabled.rows[0].vault_collection | |
let vaultLedger = isSignEnabled.rows[0].vault_ledger | |
if(!isSignEnabledValue) { | |
let body = JSON.stringify({ message: 'Vault sign is not enabled for this project' }) | |
return new Response(body, { | |
status: 400, | |
headers: { | |
'Content-Type': 'application/json; charset=utf-8', | |
}, | |
}) | |
} | |
let resp = await fetch("https://vault.immudb.io/ics/api/v1/ledger/" + vaultLedger + "/collection/" + vaultCollection + "/document", { | |
method: "PUT", | |
headers: { | |
"Content-Type": "application/json", | |
"X-API-Key": apiKey, | |
}, | |
body: stringified, | |
}); | |
let response = await resp.json() | |
let documentId = response.documentId | |
const result = await connection.queryArray(`UPDATE "yourtable" SET vault_uuid = '${documentId}' WHERE id = '${bodyOf.record.id}' RETURNING id`) | |
const resultRows = result.rows | |
const bodyToReturn = JSON.stringify( | |
resultRows, | |
(key, value) => (typeof value === 'bigint' ? value.toString() : value), | |
2 | |
) | |
return new Response( | |
bodyToReturn, | |
{ headers: { "Content-Type": "application/json" } }, | |
) | |
} finally { | |
connection.release() | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment