import {z} from 'zod' export type GetApiV1InvitesArgs = {company_riseid?: string | undefined, company_id?: (string | number) | undefined}; export type GetApiV1InvitesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1InvitesResponse = z.object({data: z.array(z.object({invite_id: z.string(), email: z.string(), alias: z.string().nullable(), invite_alternate_value: z.string(), converted: z.boolean(), role: z.string(), anonymous: z.boolean(), uuid: z.string(), avatar: z.string().nullable(), status: z.enum(["pending", "awaiting_invite_acceptance", "onboard_compliance_pending", "onboard_started"])}))}); 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 invites (V1 Legacy) * * GET /v1/invites */ export const getApiV1Invites = async (args: GetApiV1InvitesArgs, options?: GetApiV1InvitesOptions) => { const {baseUrl = "https://b2b-api.dev-riseworks.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.company_riseid != null) query.append("company_riseid", String(args.company_riseid)) if (args.company_id != null) query.append("company_id", String(args.company_id)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/invites${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(getApiV1InvitesResponse, await res.json()) };