43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import {APIResult} from "$lib/auth";
|
|
import type {APIError} from "$lib/auth";
|
|
import {Logger, logSetup} from "$lib/logger";
|
|
import {PUBLIC_BASE_URL} from "$env/static/public";
|
|
|
|
logSetup();
|
|
const logger = new Logger("netcreate.ts");
|
|
|
|
export interface OrgCreateInfo {
|
|
organization: string,
|
|
ca: string,
|
|
network: string
|
|
}
|
|
|
|
export async function createNetwork(token: string, cidr: string): Promise<[APIResult, OrgCreateInfo | APIError]> {
|
|
logger.info('creating network');
|
|
|
|
try {
|
|
logger.debug(`api call: baseurl ${PUBLIC_BASE_URL}`);
|
|
const resp = await fetch(`${PUBLIC_BASE_URL}/v1/organization`, {
|
|
'method': 'POST',
|
|
'body': JSON.stringify({
|
|
cidr: `${cidr}`
|
|
}),
|
|
'headers': {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
});
|
|
if (!resp.ok) {
|
|
logger.error('call returned error code');
|
|
const rawerror = JSON.parse(await resp.text()).errors[0];
|
|
logger.error(`error sending org create: ${rawerror.message}`);
|
|
return [APIResult.Failed, {code: rawerror.code, message: rawerror.message}];
|
|
}
|
|
|
|
return [APIResult.Successful, await resp.json()]
|
|
} catch (e) {
|
|
logger.error(`error making API request: ${e}`);
|
|
return [APIResult.Failed, {code: "api_call_failed", message: `${e}`}]
|
|
}
|
|
}
|