From 4210436ca2b392d1ed26d451540c916cf77a0898 Mon Sep 17 00:00:00 2001 From: Vasilii Mikhailovskii Date: Tue, 3 Dec 2024 17:32:50 +0200 Subject: [PATCH] fix url --- .../migrations/1732483677017-dbs.js | 45 +++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/services/db-migrations/migrations/1732483677017-dbs.js b/services/db-migrations/migrations/1732483677017-dbs.js index 7b2d92f..04a6f28 100644 --- a/services/db-migrations/migrations/1732483677017-dbs.js +++ b/services/db-migrations/migrations/1732483677017-dbs.js @@ -40,32 +40,49 @@ const dbsSecurity = { }, }; +if (!process.env.API_URL) { + throw new Error("Environment variable API_URL is not set"); +} +const urlObject = new URL(process.env.API_URL); +const headers = { + "Content-Type": "application/json", + Authorization: "Basic " + btoa(`${urlObject.username}:${urlObject.password}`), +}; +const getUrl = (dbUrlPath) => { + return `${dbObject.protocol}//${urlObject.host}${urlObject.pathname}${dbUrlPath}${urlObject.search}${urlObject.hash}`; +}; + export const up = async () => { for (const [dbName, dbSecurity] of Object.entries(dbsSecurity)) { - const responseCreateDb = await fetch(`${process.env.API_URL}/${dbName}`, { + const responseCreateDb = await fetch(getUrl(dbName), { method: "PUT", + headers, }).then((response) => response.json()); - console.assert( - responseCreateDb.ok, - `${dbName} create db error ${JSON.stringify(responseCreateDb)}`, - ); + if (!responseCreateDb.ok) { + throw new Error( + `${dbName} security error ${JSON.stringify(responseCreateDb)}`, + ); + } - const responseSetSecurity = await fetch(`${process.env.API_URL}/${dbName}/_security`, { - method: "PUT", - body: JSON.stringify(dbSecurity), - }).then((response) => response.json()); + const responseSetSecurity = await fetch(getUrl(`${dbName}/_security`), { + method: "PUT", + headers, + body: JSON.stringify(dbSecurity), + }).then((response) => response.json()); - console.assert( - responseSetSecurity.ok, - `${dbName} security error ${JSON.stringify(responseSetSecurity)}`, - ); + if (!responseSetSecurity.ok) { + throw new Error( + `${dbName} security error ${JSON.stringify(responseSetSecurity)}`, + ); + } } }; export const down = async () => { for (const dbName of Object.keys(dbsSecurity)) { - const responseDeleteDb = await fetch(`${process.env.API_URL}/${dbName}`, { + const responseDeleteDb = await fetch(getUrl(dbName), { + headers, method: "DELETE", }); console.assert(