import {z} from 'zod' export type MatchConfig = {strategy?: 'code_then_name' | undefined, name_match?: 'normalized_exact' | undefined}; export type MatchKey = {type: 'code' | 'name', value: string}; export type FixedCellPlacement = {kind?: 'fixed_cell' | undefined, sheet: string, cell: string, match_key: MatchKey, uom: 'EA' | 'LF' | 'IN' | 'FT' | 'SF' | 'SY' | 'CF' | 'CY', result_type?: ('count' | 'total_height' | 'perimeter' | 'surface_area_top_or_bottom' | 'surface_area_top_and_bottom' | 'surface_area_all_sides' | 'surface_area_full' | 'volume' | 'length' | 'segment_count' | 'surface_area_single_side' | 'surface_area_both_sides' | 'surface_area_single_end' | 'surface_area_both_ends' | 'surface_area_all_side_duct' | 'area' | 'grid_length_visible' | 'grid_length_x_visible' | 'grid_length_y_visible' | 'grid_intersection_count' | 'tile_count_average' | 'tile_count_visible' | 'area_counts' | null) | undefined}; export type ListRegionPlacement = {kind?: 'list_region' | undefined, sheet: string, start_row: number, key_column: string, key_type: 'code' | 'name', columns: {[key: string]: string}, on_unmatched_template_row?: ('leave' | 'clear') | undefined, on_extra_quantity?: ('append' | 'ignore') | undefined}; export type SectionInsertPlacement = {kind?: 'section_insert' | undefined, sheet: string, header_label: string, key_column: string, columns: {[key: string]: string}, codes: Array, stop_labels?: Array | undefined}; export type GridColumn = {column: string, source: 'derivation' | 'dimension' | 'name' | 'size_string' | 'const', uom?: ('EA' | 'LF' | 'IN' | 'FT' | 'SF' | 'SY' | 'CF' | 'CY' | null) | undefined, result_type?: ('count' | 'total_height' | 'perimeter' | 'surface_area_top_or_bottom' | 'surface_area_top_and_bottom' | 'surface_area_all_sides' | 'surface_area_full' | 'volume' | 'length' | 'segment_count' | 'surface_area_single_side' | 'surface_area_both_sides' | 'surface_area_single_end' | 'surface_area_both_ends' | 'surface_area_all_side_duct' | 'area' | 'grid_length_visible' | 'grid_length_x_visible' | 'grid_length_y_visible' | 'grid_intersection_count' | 'tile_count_average' | 'tile_count_visible' | 'area_counts' | null) | undefined, dimension?: ('width_in' | 'thickness_in' | 'height_in' | null) | undefined, unit?: ('in' | 'ft') | undefined, const_value?: (number | null) | undefined}; export type SectionGridPlacement = {kind?: 'section_grid' | undefined, sheet: string, match_key: MatchKey, family: 'area' | 'area_multiline' | 'area_shifted' | 'linear_curb' | 'count_footing' | 'linear_footing' | 'grade_beam' | 'pier' | 'slab_shifted' | 'wall', start_row: number, max_rows: number, columns?: Array | undefined}; export type ConnectorMapping = {match?: MatchConfig | undefined, placements?: (Array) | undefined}; export type SaveConnectorRequest = {mapping: ConnectorMapping, notes?: (string | null) | undefined}; export type CreateApiApiV1ExportTemplatesTemplateIdConnectorsArgs = {template_id: string, body: SaveConnectorRequest}; export type CreateApiApiV1ExportTemplatesTemplateIdConnectorsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const savedConnector = z.object({id: z.string(), template_id: z.string(), version: z.number().int(), is_active: z.boolean()}); 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 }; /** * Save Connector * * POST /api/v1/export-templates/{template_id}/connectors */ export const createApiApiV1ExportTemplatesTemplateIdConnectors = async (args: CreateApiApiV1ExportTemplatesTemplateIdConnectorsArgs, options: CreateApiApiV1ExportTemplatesTemplateIdConnectorsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/export-templates/${encodeURIComponent(String(args.template_id))}/connectors${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(savedConnector, await res.json()) };