import {z} from 'zod' export type GetApiV1AccountsArgs = {maxPageSize?: number | undefined, pageToken?: (string | null) | undefined, filter?: (string | null) | undefined}; export type GetApiV1AccountsOptions = {baseUrl?: string | undefined, OAuth2ClientCredentialsBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const oEMAccount = z.object({id: z.string(), username: z.string()}); export const pageOEMAccount = z.object({results: z.array(oEMAccount), nextPageToken: z.string().nullable(), totalResults: 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 }; /** * List Accounts * * GET /v1/accounts */ export const getApiV1Accounts = async (args: GetApiV1AccountsArgs, options?: GetApiV1AccountsOptions) => { const {baseUrl = "https://api.volteras.com", fetch: fetchFn = fetch, OAuth2ClientCredentialsBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.maxPageSize != null) query.append("maxPageSize", String(args.maxPageSize)) if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) if (args.filter != null) query.append("filter", String(args.filter)) if (OAuth2ClientCredentialsBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentialsBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts${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(pageOEMAccount, await res.json()) };