import {z} from 'zod' export type GetApiApiInvitationsArgs = {search?: string | undefined, status?: ('pending' | 'accepted') | undefined, page?: number | undefined, perPage?: number | undefined}; export type GetApiApiInvitationsOptions = {baseUrl: string, personalAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiInvitationsResponse = z.object({results: z.array(z.object({id: z.string(), email: z.string(), expiresAt: z.string().nullable(), createdAt: z.string(), acceptedAt: z.string().nullable(), status: z.enum(["pending", "accepted"]), createdBy: z.object({id: z.string(), email: z.string(), firstName: z.string().nullable(), lastName: z.string().nullable(), type: z.enum(["invited", "member", "author", "admin", "owner"]), status: z.enum(["active", "disabled", "locked"])}).nullable().optional(), user: z.object({id: z.string(), email: z.string(), firstName: z.string().nullable(), lastName: z.string().nullable(), type: z.enum(["invited", "member", "author", "admin", "owner"]), status: z.enum(["active", "disabled", "locked"])}).nullable().optional(), organisation: z.object({id: z.string(), name: z.string()}).nullable().optional()})), paginate: z.object({current: z.number(), from: z.number(), to: z.number(), total: z.number(), pageSize: z.number()})}); 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 invitations * * GET /api/invitations * * Also accepts auth: internalAuth */ export const getApiApiInvitations = async (args: GetApiApiInvitationsArgs, options: GetApiApiInvitationsOptions) => { const {baseUrl, fetch: fetchFn = fetch, personalAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.status != null) query.append("status", String(args.status)) if (args.page != null) query.append("page", String(args.page)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (personalAccessToken !== undefined) headers.set('authorization', `Bearer ${personalAccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/invitations${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(getApiApiInvitationsResponse, await res.json()) };