import {z} from 'zod' export type GetApiHubManagementTimeScopesArgs = {stations?: Array | undefined}; export type GetApiHubManagementTimeScopesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiHubManagementTimeScopesResponse = z.object({results: z.array(z.object({station: z.object({uuid: z.string().optional(), address: z.string().max(400).nullable().optional(), location: z.string().optional(), nickname: z.string().max(255).nullable().optional(), postal_code: z.string().max(10).nullable().optional(), images: z.array(z.string()).optional(), districts: z.object({}).nullable().optional()}), time_scopes: z.array(z.object({expected_check_in: z.string(), expected_duration: z.string()}))}))}); 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 }; /** * Lists time scopes per station. * * GET /hub-management/time-scopes */ export const getApiHubManagementTimeScopes = async (args: GetApiHubManagementTimeScopesArgs, options?: GetApiHubManagementTimeScopesOptions) => { const {baseUrl = "https://api.ganje.net/v1.1", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.stations ?? []) query.append("stations", String(item)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/hub-management/time-scopes${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(getApiHubManagementTimeScopesResponse, await res.json()) };