import {z} from 'zod' export type GetApiV1TimeZonesTimeZoneArgs = {time_zone: string, instant?: string | undefined}; export type GetApiV1TimeZonesTimeZoneOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const singleTimeZoneResponse = z.object({abbreviation: z.string(), aliases: z.array(z.string()), canonical: z.boolean(), dst: z.boolean(), formatted_offset: z.string(), generic_long_name: z.string(), golden: z.boolean(), id: z.string(), legacy: z.boolean(), long_name: z.string(), metazone: z.object({exemplar_city: z.string().nullable().optional(), long: z.object({current: z.string().nullable().optional(), daylight: z.string().nullable().optional(), generic: z.string().nullable().optional(), standard: z.string().nullable().optional()}).nullable().optional(), name: z.string().optional(), short: z.object({current: z.string().nullable().optional(), daylight: z.string().nullable().optional(), generic: z.string().nullable().optional(), standard: z.string().nullable().optional()}).nullable().optional(), territories: z.array(z.string()).optional()}).nullable().optional(), offset: z.number().int(), offset_std: z.number().int(), offset_utc: z.number().int(), period: z.object({from: z.string().nullable().optional(), until: z.string().nullable().optional()}), windows_zone: z.object({name: z.string().optional()}).nullable()}); 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 }; /** * Get time zone * * GET /v1/time_zones/{time_zone} * * Also accepts auth: PersonalAccessToken, PublicKey */ export const getApiV1TimeZonesTimeZone = async (args: GetApiV1TimeZonesTimeZoneArgs, options?: GetApiV1TimeZonesTimeZoneOptions) => { const {baseUrl = "https://api.savvycal.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.instant != null) query.append("instant", String(args.instant)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/time_zones/${encodeURIComponent(String(args.time_zone))}${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(singleTimeZoneResponse, await res.json()) };