export type Key = {key?: string | undefined, keyfile?: string | undefined, until?: string | undefined}; export type CreateApiSwarmArgs = {hash: string, body: Key}; export type CreateApiSwarmOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; /** * Update swarm keys * * POST /swarm */ export const createApiSwarm = async (args: CreateApiSwarmArgs, options?: CreateApiSwarmOptions) => { const {baseUrl = "http://localhost/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") if (args.hash != null) query.append("hash", String(args.hash)) const url = `${baseUrl.replace(/\/+$/, '')}/swarm${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 };