import {z} from 'zod' export type GetApiManagementV1ProjectsProjectIdMetadataSchemasMetadataSchemaIdOptions = {baseUrl?: string | undefined, XManagementId?: string | undefined, XManagementToken?: string | undefined, fetch?: typeof fetch | undefined}; export const managementProjectsMetadataSchemaDefinition = z.object({type: z.enum(["string", "number", "object", "date", "datetime", "geopoint", "boolean", "image_url"]), optional: z.boolean(), array: z.boolean(), deleted: z.boolean().optional(), object_type: z.string().nullable().optional(), min_length: z.number().int().optional(), max_length: z.number().int().optional(), exact_length: z.number().int().optional(), eq: z.array(z.union([z.number(), z.string()])).optional(), ne: z.array(z.number()).optional(), lt: z.number().optional(), lte: z.number().optional(), gt: z.number().optional(), gte: z.number().optional()}); export const managementProjectsMetadataSchemasGetResponseBody = z.object({id: z.string(), related_object: z.string(), properties: z.record(z.string(), managementProjectsMetadataSchemaDefinition), allow_defined_only: z.boolean().nullable(), created_at: z.string(), updated_at: z.string().nullable(), object: z.string()}); 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 Metadata Schema * * GET /management/v1/projects/{projectId}/metadata-schemas/{metadataSchemaId} */ export const getApiManagementV1ProjectsProjectIdMetadataSchemasMetadataSchemaId = async (options?: GetApiManagementV1ProjectsProjectIdMetadataSchemasMetadataSchemaIdOptions) => { const {baseUrl = "https://api.voucherify.io", fetch: fetchFn = fetch, XManagementId, XManagementToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (XManagementId !== undefined) headers.set("X-Management-Id", XManagementId) if (XManagementToken !== undefined) headers.set("X-Management-Token", XManagementToken) const url = `${baseUrl.replace(/\/+$/, '')}/management/v1/projects/${encodeURIComponent(String(args.projectId))}/metadata-schemas/${encodeURIComponent(String(args.metadataSchemaId))}${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(managementProjectsMetadataSchemasGetResponseBody, await res.json()) };