import {z} from 'zod' export type SecurityMode = 'Best' | 'Sign' | 'SignAndEncrypt' | 'None' | 'NotNone'; export type EndpointModel = {url: string, alternativeUrls?: Array | undefined, securityMode?: SecurityMode | undefined, securityPolicy?: string | undefined, certificate?: string | undefined}; export type CredentialType = 'None' | 'UserName' | 'X509Certificate' | 'JwtToken'; export type UserIdentityModel = {user?: string | undefined, password?: string | undefined, thumbprint?: string | undefined}; export type CredentialModel = {type?: CredentialType | undefined, value?: UserIdentityModel | undefined}; export type DiagnosticsLevel = 'None' | 'Status' | 'Information' | 'Debug' | 'Verbose'; export type DiagnosticsModel = {level?: DiagnosticsLevel | undefined, auditId?: string | undefined, timeStamp?: string | undefined}; export type ConnectionOptions = 'None' | 'UseReverseConnect' | 'NoComplexTypeSystem' | 'NoSubscriptionTransfer' | 'DumpDiagnostics'; export type ConnectionModel = {endpoint: EndpointModel, user?: CredentialModel | undefined, diagnostics?: DiagnosticsModel | undefined, group?: string | undefined, locales?: Array | undefined, options?: ConnectionOptions | undefined}; export type ServiceResultModel = {statusCode?: number | undefined, errorMessage?: string | undefined, symbolicId?: string | undefined, locale?: string | undefined, additionalInfo?: string | undefined, namespaceUri?: string | undefined, inner?: ServiceResultModel | undefined}; export type DataLocation = 'Raw' | 'Calculated' | 'Interpolated'; export type HistoryUpdateOperation = 'Insert' | 'Replace' | 'Update' | 'Delete'; export type ModificationInfoModel = {modificationTime?: string | undefined, updateType?: HistoryUpdateOperation | undefined, userName?: string | undefined}; export type AdditionalData = 'None' | 'Partial' | 'ExtraData' | 'MultipleValues'; export type HistoricValueModel = {value?: Record | undefined, dataType?: string | undefined, status?: ServiceResultModel | undefined, sourceTimestamp?: string | undefined, sourcePicoseconds?: number | undefined, serverTimestamp?: string | undefined, serverPicoseconds?: number | undefined, dataLocation?: DataLocation | undefined, modificationInfo?: ModificationInfoModel | undefined, additionalData?: AdditionalData | undefined}; export type UpdateValuesDetailsModel = {values: Array}; export type NamespaceFormat = 'Uri' | 'Index' | 'Expanded' | 'ExpandedWithNamespace0'; export type RequestHeaderModel = {elevation?: CredentialModel | undefined, locales?: Array | undefined, diagnostics?: DiagnosticsModel | undefined, namespaceFormat?: NamespaceFormat | undefined, operationTimeout?: number | undefined, serviceCallTimeout?: number | undefined, connectTimeout?: number | undefined}; export type UpdateValuesDetailsModelHistoryUpdateRequestModel = {nodeId?: string | undefined, browsePath?: Array | undefined, details: UpdateValuesDetailsModel, header?: RequestHeaderModel | undefined}; export type UpdateValuesDetailsModelHistoryUpdateRequestModelRequestEnvelope = {connection: ConnectionModel, request?: UpdateValuesDetailsModelHistoryUpdateRequestModel | undefined}; export type CreateApiV2HistoryValuesUpsertArgs = {body: UpdateValuesDetailsModelHistoryUpdateRequestModelRequestEnvelope}; export type CreateApiV2HistoryValuesUpsertOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceResultModel: z.ZodType = z.object({statusCode: z.number().int().optional(), errorMessage: z.string().optional(), symbolicId: z.string().optional(), locale: z.string().optional(), additionalInfo: z.string().optional(), namespaceUri: z.string().optional(), inner: z.lazy(() => serviceResultModel.optional())}); export const historyUpdateResponseModel = z.object({results: z.array(serviceResultModel).optional(), errorInfo: serviceResultModel.optional()}); 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 }; /** * HistoryUpsertValues * * POST /v2/history/values/upsert */ export const createApiV2HistoryValuesUpsert = async (args: CreateApiV2HistoryValuesUpsertArgs, options?: CreateApiV2HistoryValuesUpsertOptions) => { const {baseUrl = "https://localhost:9072", 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(/\/+$/, '')}/v2/history/values/upsert${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(historyUpdateResponseModel, await res.json()) };