import {z} from 'zod' export type GetApiV1MembersArgs = {type?: ('member' | 'guest' | 'libraryMember') | undefined, email?: string | undefined, search?: string | undefined, limit?: string | undefined, cursor?: string | undefined}; export type GetApiV1MembersOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, WorkspaceIdAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const member = z.object({id: z.string(), type: z.enum(["member", "guest", "libraryMember"]), displayName: z.string().nullable(), firstName: z.string().nullable(), lastName: z.string().nullable(), email: z.string(), firstJoinedAt: z.string(), avatar: z.string().optional()}); export const cursorPage = z.object({hasMore: z.boolean(), cursor: z.string().nullable()}); export const memberList = z.object({data: z.array(member), pagination: cursorPage, total: z.number().int()}); 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 }; /** * /members * * GET /v1/members * * Also accepts auth: BearerAuth + WorkspaceIdAuth */ export const getApiV1Members = async (args: GetApiV1MembersArgs, options: GetApiV1MembersOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth, WorkspaceIdAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (args.email != null) query.append("email", String(args.email)) if (args.search != null) query.append("search", String(args.search)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) if (WorkspaceIdAuth !== undefined) headers.set("x-air-workspace-id", WorkspaceIdAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/members${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(memberList, await res.json()) };