export type Interface = {name?: string | undefined, ip?: string | undefined, mac?: string | undefined}; export type Key = {key?: string | undefined, keyfile?: string | undefined, until?: string | undefined}; export type Instance = {hash?: string | undefined, interface?: Interface | undefined, port?: string | undefined, key?: Key | undefined}; export type CreateApiInstanceArgs = {body: Instance}; export type CreateApiInstanceOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; /** * Create new P2P instance * * POST /instance */ export const createApiInstance = async (args: CreateApiInstanceArgs, options?: CreateApiInstanceOptions) => { const {baseUrl = "http://localhost/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/instance${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return undefined };