import {z} from 'zod' export type GetApiApiOrganizationsArgs = {created?: string | undefined, id?: string | undefined, is_active?: boolean | undefined, limit?: number | undefined, modified?: string | undefined, name?: string | undefined, offset?: number | undefined, slug?: string | undefined, users?: Array | undefined}; export type GetApiApiOrganizationsOptions = {baseUrl: string, cookieAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const organization = z.object({id: z.string(), url: z.string(), owner: z.string(), organization_users: z.array(z.string()), name: z.string().max(200), is_active: z.boolean().optional(), created: z.string(), modified: z.string(), slug: z.string()}); export const paginatedOrganizationList = z.array(organization); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * List organizations * * GET /api/organizations/ * * Also accepts auth: basicAuth, jwtAuth */ export const getApiApiOrganizations = async (args: GetApiApiOrganizationsArgs, options: GetApiApiOrganizationsOptions) => { const {baseUrl, fetch: fetchFn = fetch, cookieAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.created != null) query.append("created", String(args.created)) if (args.id != null) query.append("id", String(args.id)) if (args.is_active != null) query.append("is_active", String(args.is_active)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.modified != null) query.append("modified", String(args.modified)) if (args.name != null) query.append("name", String(args.name)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.slug != null) query.append("slug", String(args.slug)) for (const item of args.users ?? []) query.append("users", String(item)) if (cookieAuth !== undefined) cookies.push("sessionid=" + encodeURIComponent(cookieAuth)) const url = `${baseUrl.replace(/\/+$/, '')}/api/organizations/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(paginatedOrganizationList, await res.json()) };