import {z} from 'zod' export type Metafield = {id?: string | undefined, title: string, key: string, type: string, value: unknown, helptext?: string | undefined, required?: boolean | undefined, media_validation_type?: string | undefined, repeater_fields?: Array | undefined, options?: Array<{[key: string]: unknown}> | undefined} | {[key: string]: unknown}; export type ObjectTypeCreate = {title: string, singular?: string | undefined, slug?: string | undefined, singleton?: boolean | undefined, emoji?: string | undefined, metafields?: Array | undefined, options?: {[key: string]: unknown} | undefined, localization?: boolean | undefined, locales?: Array | undefined, priority_locale?: string | undefined}; export type CreateApiBucketsSlugObjectTypesArgs = {body: ObjectTypeCreate}; export type CreateApiBucketsSlugObjectTypesOptions = {baseUrl?: string | undefined, writeKey?: string | undefined, fetch?: typeof fetch | undefined}; export const metafield: z.ZodType = z.object({id: z.string().optional(), title: z.string(), key: z.string(), type: z.string(), value: z.unknown(), helptext: z.string().optional(), required: z.boolean().optional(), media_validation_type: z.string().optional(), repeater_fields: z.array(z.lazy(() => metafield)).optional(), options: z.array(z.record(z.string(), z.unknown())).optional()}).and(z.record(z.string(), z.unknown())); export const objectType = z.object({id: z.string().optional(), title: z.string().optional(), singular: z.string().optional(), slug: z.string().optional(), singleton: z.boolean().optional(), emoji: z.string().optional(), metafields: z.array(metafield).optional(), options: z.object({slug_field: z.boolean().optional()}).and(z.record(z.string(), z.unknown())).optional(), localization: z.boolean().optional(), locales: z.array(z.string()).optional(), priority_locale: z.string().nullable().optional(), created_at: z.string().optional(), modified_at: z.string().optional()}).and(z.record(z.string(), z.unknown())); export const objectTypeResponse = z.object({object_type: objectType}); 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 }; /** * Create an Object type * * POST /buckets/{slug}/object-types */ export const createApiBucketsSlugObjectTypes = async (args: CreateApiBucketsSlugObjectTypesArgs, options?: CreateApiBucketsSlugObjectTypesOptions) => { const {baseUrl = "https://api.cosmicjs.com/v3", fetch: fetchFn = fetch, writeKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (writeKey !== undefined) headers.set('authorization', `Bearer ${writeKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/buckets/${encodeURIComponent(String(args.slug))}/object-types${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(objectTypeResponse, await res.json()) };