@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user