import {z} from 'zod' export type GetApiPublicV2ContactsArgs = {page?: number | undefined}; export type GetApiPublicV2ContactsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const validationResult = z.object({memberNames: z.array(z.string()).nullable().optional(), errorMessage: z.string().nullable().optional()}); export const userResponse = z.object({subjectId: z.string().min(1), email: z.string().min(1), roles: z.array(z.string())}); export const userResponsePaginated = z.object({pageIndex: z.number().int().optional(), limit: z.number().int().optional(), isLastPage: z.boolean().optional(), results: z.array(userResponse).nullable().optional(), totalCount: z.number().int().optional()}); export const userResponsePaginatedServiceResponse = z.object({errors: z.array(validationResult).nullable().optional(), warnings: z.array(validationResult).nullable().optional(), information: z.array(z.string()).nullable().optional(), isValid: z.boolean().optional(), content: userResponsePaginated.optional()}); 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 }; /** * Get contacts * * GET /public/v2/contacts */ export const getApiPublicV2Contacts = async (args: GetApiPublicV2ContactsArgs, options?: GetApiPublicV2ContactsOptions) => { const {baseUrl = "https://starlink.com/api", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/public/v2/contacts${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(userResponsePaginatedServiceResponse, await res.json()) };