import {z} from 'zod' export type GetApiV1FleetDriverGroupsArgs = {activeOnly?: boolean | undefined, limit?: number | undefined, cursor?: (string | null) | undefined}; export type GetApiV1FleetDriverGroupsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const driverGroupRead = z.object({firstVersionContractId: z.string(), externalIdentifier: z.string().nullable().optional(), displayName: z.string(), isActive: z.boolean(), unassignedDriverTimeoutMinutes: z.number().int().nullable().optional(), providerId: z.string()}); export const listDriverGroupsResponse = z.object({driverGroups: z.array(driverGroupRead), nextCursor: z.string().nullable().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 Driver Groups * * GET /v1/fleet/driver-groups */ export const getApiV1FleetDriverGroups = async (args: GetApiV1FleetDriverGroupsArgs, options?: GetApiV1FleetDriverGroupsOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.activeOnly != null) query.append("activeOnly", String(args.activeOnly)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/fleet/driver-groups${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(listDriverGroupsResponse, await res.json()) };