import {z} from 'zod' export type GetApiV1ExternalUsersArgs = {limit?: number | undefined, page?: number | undefined, search?: string | undefined, startDate?: string | undefined, endDate?: string | undefined, timeZone?: string | undefined, sortField?: string | undefined, sortDirection?: ('asc' | 'desc') | undefined, checks?: string | undefined}; export type GetApiV1ExternalUsersOptions = {baseUrl?: string | undefined, ProjectPrivateApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const user = z.object({id: z.string().optional(), createdAt: z.string().optional(), externalId: z.string().optional(), lastSeen: z.string().optional(), props: z.object({}).optional(), cost: z.number().optional()}); export const getApiV1ExternalUsersResponse = z.object({page: z.number().int().optional(), limit: z.number().int().optional(), data: z.array(user).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 }; /** * List project users * * GET /v1/external-users */ export const getApiV1ExternalUsers = async (args: GetApiV1ExternalUsersArgs, options?: GetApiV1ExternalUsersOptions) => { const {baseUrl = "https://api.lunary.ai", fetch: fetchFn = fetch, ProjectPrivateApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.search != null) query.append("search", String(args.search)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.timeZone != null) query.append("timeZone", String(args.timeZone)) if (args.sortField != null) query.append("sortField", String(args.sortField)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) if (args.checks != null) query.append("checks", String(args.checks)) if (ProjectPrivateApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ProjectPrivateApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/external-users${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(getApiV1ExternalUsersResponse, await res.json()) };