import {z} from 'zod' export type GetApi10AuthorizationWebsiteArgs = {Authorization: string, 'User-Agent': string}; export type GetApi10AuthorizationWebsiteOptions = {baseUrl?: string | undefined, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const location = z.object({country: z.string().optional(), region: z.string().optional()}); export const measurementStandard = z.enum(["IMPERIAL", "METRIC"]); export const websiteProfile = z.object({currency: z.string().optional(), id: z.string().optional(), language: z.string().optional(), location: location.optional(), measurementStandard: measurementStandard.optional(), siteId: z.string().optional(), timeZone: z.string().optional(), title: z.string().optional(), url: z.string().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 }; /** * Retrieves basic details about the website that owns the provided API key or OAuth token * * GET /1.0/authorization/website */ export const getApi10AuthorizationWebsite = async (args: GetApi10AuthorizationWebsiteArgs, options?: GetApi10AuthorizationWebsiteOptions) => { const {baseUrl = "https://api.squarespace.com", fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['User-Agent'] != null) headers.set("User-Agent", String(args['User-Agent'])) if (Authorization !== undefined) headers.set('authorization', `Bearer ${Authorization}`) const url = `${baseUrl.replace(/\/+$/, '')}/1.0/authorization/website${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(websiteProfile, await res.json()) };