import {z} from 'zod' export type DataSourceColumnMetadata = {name: string, type: {name?: string | undefined}}; export type UpdateApiV1DataSourcesDataSourceIdArgs = {body: {dataSource: {name: string, type?: 'CSV' | undefined, columnsMetadata: Array, rows: Array>} | {name: string, type: 'DATABASE', databaseConnection: string, tableName: string}}}; export type UpdateApiV1DataSourcesDataSourceIdOptions = {baseUrl?: string | undefined, tokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const dataSourceColumnMetadata = z.object({name: z.string(), type: z.object({name: z.string().optional()})}); export const dataSourceResponse = z.union([z.object({id: z.string(), name: z.string(), aclObject: z.string().optional(), lastUpdatedTime: z.string(), version: z.number(), type: z.literal("CSV"), state: z.enum(["ENABLED", "DISABLED"]), links: z.object({self: z.string(), acl: z.string()}), columnsMetadata: z.array(dataSourceColumnMetadata)}), z.object({id: z.string(), name: z.string(), aclObject: z.string().optional(), lastUpdatedTime: z.string(), version: z.number(), type: z.literal("DATABASE"), state: z.enum(["ENABLED", "DISABLED"]), links: z.object({self: z.string(), acl: z.string()}), databaseConnection: z.string(), tableName: z.string()})]); export const updateApiV1DataSourcesDataSourceIdResponse = z.object({dataSource: dataSourceResponse}); 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 }; /** * Update data source * * PUT /v1/data-sources/{dataSourceId} */ export const updateApiV1DataSourcesDataSourceId = async (args: UpdateApiV1DataSourcesDataSourceIdArgs, options?: UpdateApiV1DataSourcesDataSourceIdOptions) => { const {baseUrl = "https://wmc.wiremockapi.cloud", fetch: fetchFn = fetch, tokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (tokenAuth !== undefined) headers.set("Authorization", tokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/data-sources/${encodeURIComponent(String(args.dataSourceId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiV1DataSourcesDataSourceIdResponse, await res.json()) };