export type CreateApiJwtArgs = {expire?: number | undefined, only_elements?: string | undefined, only_lists?: string | undefined, scope: string}; export type CreateApiJwtOptions = {baseUrl: string, jwt?: string | undefined, fetch?: typeof fetch | undefined}; /** * JWT key generator * * POST /jwt */ export const createApiJwt = async (args: CreateApiJwtArgs, options: CreateApiJwtOptions) => { const {baseUrl, fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() if (args.expire != null) query.append("expire", String(args.expire)) if (args.only_elements != null) query.append("only_elements", String(args.only_elements)) if (args.only_lists != null) query.append("only_lists", String(args.only_lists)) if (args.scope != null) query.append("scope", String(args.scope)) if (jwt !== undefined) headers.set("Authorization", jwt) const url = `${baseUrl.replace(/\/+$/, '')}/jwt${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return undefined };