import {z} from 'zod' export type CreateApiResourceTenantLocatorTablesArgs = {tenantLocator: string, name: string, staticName: string, jurisdiction?: Array | undefined, body: BodyInit}; export type CreateApiResourceTenantLocatorTablesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const basicResourceResponse = z.object({name: z.string(), staticName: z.string().optional(), resourceType: z.enum(["constraintTable", "customFont", "documentTemplate", "documentTemplateSnippet", "rangeTable", "secret", "staticDocument", "table"]), lookupTableLocator: z.string().optional(), rangeTableLocator: z.string().optional(), constraintTableLocator: z.string().optional(), template: z.string().optional(), staticDocumentLocator: z.string().optional(), fontLocator: z.string().optional(), riskAssessmentCriteriaLocator: z.string().optional(), uiConfigLocator: z.string().optional(), templateFormat: z.enum(["liquid", "velocity"]).optional(), createdBy: z.string(), createdAt: z.string(), jurisdictions: z.array(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 }; /** * POST /resource/{tenantLocator}/tables */ export const createApiResourceTenantLocatorTables = async (args: CreateApiResourceTenantLocatorTablesArgs, options: CreateApiResourceTenantLocatorTablesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.name != null) query.append("name", String(args.name)) if (args.staticName != null) query.append("staticName", String(args.staticName)) for (const item of args.jurisdiction ?? []) query.append("jurisdiction", String(item)) const url = `${baseUrl.replace(/\/+$/, '')}/resource/${encodeURIComponent(String(args.tenantLocator))}/tables${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(basicResourceResponse, await res.json()) };