import {z} from 'zod' export type PatchApiMembersMemberIdArgs = {memberId: string, body: {name?: (string | null) | undefined, displayName?: (string | null) | undefined, role?: ('admin' | 'depot-manager' | 'dispatcher' | 'read-only' | null) | undefined, depots?: (Array | null) | undefined}}; export type PatchApiMembersMemberIdOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const memberIdSchema = z.string(); export const driverIdSchema = z.string(); export const roleSchema = z.array(z.enum(["owner", "admin", "depot-manager", "dispatcher", "read-only", "driver"])); export const memberSchema = z.object({id: memberIdSchema, name: z.string().nullable(), email: z.string().nullable(), phone: z.string().nullable(), linkedDriverId: driverIdSchema.nullable(), displayName: z.string().nullable(), active: z.boolean(), depots: z.array(z.string()), roles: roleSchema}); 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 }; /** * Update a member * * PATCH /members/{memberId} */ export const patchApiMembersMemberId = async (args: PatchApiMembersMemberIdArgs, options?: PatchApiMembersMemberIdOptions) => { const {baseUrl = "https://api.spoke.com/public/v1", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/members/${encodeURIComponent(String(args.memberId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(memberSchema, await res.json()) };