import {z} from 'zod' export type GetApiProjectProjectIdResourcebundlesResourceBundlePathArgs = {project_id: string, resource_bundle_path: string}; export type GetApiProjectProjectIdResourcebundlesResourceBundlePathOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const resouceBundleKey = z.object({name: z.string(), description: z.string().optional()}); export const resouceBundleMessage = z.object({key: z.string(), value: z.string().optional()}); export const resourceBundleValueSet = z.object({messages: z.array(resouceBundleMessage).optional(), name: z.string()}); export const system = z.object({createdBy: z.string().optional(), createdAt: z.string().optional(), updatedBy: z.string().optional(), updatedAt: z.string().optional()}); export const resourceBundle = z.object({name: z.string(), displayName: z.string(), id: z.string(), path: z.string(), keys: z.array(resouceBundleKey), valueSets: z.array(resourceBundleValueSet), system: system.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 resource bundle in a specific project * * GET /project/{project_id}/resourcebundles/{resource_bundle_path} */ export const getApiProjectProjectIdResourcebundlesResourceBundlePath = async (args: GetApiProjectProjectIdResourcebundlesResourceBundlePathArgs, options?: GetApiProjectProjectIdResourcebundlesResourceBundlePathOptions) => { const {baseUrl = "https://developers.bloomreach.io/delivery/site/v2/documents", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/project/${encodeURIComponent(String(args.project_id))}/resourcebundles/${encodeURIComponent(String(args.resource_bundle_path))}${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(resourceBundle, await res.json()) };