import {z} from 'zod' export type GetApiBrandsBrandIdArgs = {brand_id: string}; export type GetApiBrandsBrandIdOptions = {baseUrl?: string | undefined, yourApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const brand = z.object({id: z.string().optional(), type: z.literal("brand").optional(), attributes: z.object({name: z.string().optional(), primary_colour: z.string().optional(), secondary_colour: z.string().optional(), tertiary_colour: z.string().optional(), about_us: z.string().optional(), why_instruct_us: z.string().optional(), valuation_url: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}).optional(), relationships: z.object({}).optional()}); export const meta = z.object({}); export const link = z.union([z.string(), z.object({href: z.string(), meta: meta.optional()})]); export const includedLinks = z.object({self: link.optional(), related: link.optional()}); export const jsonApi = z.object({version: z.string().optional(), meta: meta.optional()}); export const getApiBrandsBrandIdResponse = z.object({data: brand.optional(), links: includedLinks.optional(), meta: z.object({}).optional(), jsonapi: jsonApi.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 }; /** * Get a single Brand * * GET /brands/{brand_id} */ export const getApiBrandsBrandId = async (args: GetApiBrandsBrandIdArgs, options?: GetApiBrandsBrandIdOptions) => { const {baseUrl = "https://street.co.uk/open-api/v1", fetch: fetchFn = fetch, yourApiToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (yourApiToken !== undefined) headers.set('authorization', `Bearer ${yourApiToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/brands/${encodeURIComponent(String(args.brand_id))}${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(getApiBrandsBrandIdResponse, await res.json()) };