import {z} from 'zod' export type GetApiOfficesOfficeIdWeatherstoriesOptions = {baseUrl?: string | undefined, userAgent?: string | undefined, fetch?: typeof fetch | undefined}; export const officeWeatherStory = z.object({id: z.string().optional(), startTime: z.string(), endTime: z.string(), updateTime: z.string(), title: z.string().max(50), description: z.string(), priority: z.boolean(), download: z.string(), altText: z.string(), order: z.number().int().gte(1).lte(7)}); export const officeWeatherStoryCollection = z.array(officeWeatherStory); 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 }; /** * Returns active weather stories for an NWS office * * GET /offices/{officeId}/weatherstories * * Also accepts auth: apiKeyAuth */ export const getApiOfficesOfficeIdWeatherstories = async (options?: GetApiOfficesOfficeIdWeatherstoriesOptions) => { const {baseUrl = "https://api.weather.gov", fetch: fetchFn = fetch, userAgent} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/ld+json") if (userAgent !== undefined) headers.set("User-Agent", userAgent) const url = `${baseUrl.replace(/\/+$/, '')}/offices/${encodeURIComponent(String(args.officeId))}/weatherstories${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(officeWeatherStoryCollection, await res.json()) };