import {z} from 'zod' export type CreateApiApiSiteCopyArgs = {targetOrganizationID: string, siteID: string, siteName?: (string | null) | undefined, siteDirectory?: (string | null) | undefined, startDate?: (string | null) | undefined, endDate?: (string | null) | undefined}; export type CreateApiApiSiteCopyOptions = {baseUrl: string, AuthenticationToken?: string | undefined, fetch?: typeof fetch | undefined}; export const quantumObjectType = z.enum(["Adjacency", "AllowedQuanta", "Analysis", "Animation", "ApiKey", "Artifact", "Asset", "AuthGroup", "Behavior", "Binding", "Building", "Campus", "CampusDirectory", "CompatibleQuanta", "ConnectionNet", "ConnectionNode", "ConstructionCategory", "ControlKnot", "Device", "DisplayRule", "EmailVerification", "Equation", "Equipment", "EquipmentComponent", "EquipmentComponentPropertyRouter", "EscalationGroup", "Event", "EventClass", "EventHistory", "EventRelation", "EventType", "Floor", "GraphVersion", "History", "Image", "ImportedModel", "InterfaceAlternate", "Invite", "Layer", "LayerSet", "LayerSetJoin", "Library", "Location", "LocationProperty", "Manufacturer", "Media", "MediaComponent", "Model", "MultifactorAuthentication", "NodeSet", "NodeSetConnectionNodeJoin", "Organization", "OrganizationAuthGroupPermission", "Prediction", "PredictionGroup", "Preference", "PreferenceGroup", "PrivilegeJoin", "Procedure", "Property", "PropertyTagRouter", "Quanta", "RefreshToken", "Relationship", "SelectedModel", "SessionBinding", "SessionEvent", "Shape", "Site", "Subsystem", "SubsystemCapability", "SubsystemSet", "Surface", "System", "Tag", "TagFamily", "TypicalConstruction", "UnitLookup", "UnitPreference", "User", "UserSession", "UserSessionHistory", "Vertex", "View", "WeatherSource", "Zone", "ZoneGroup", "ZoneSubsystem", "ZoneSubsystemSet"]); export const objectPair = z.object({type: quantumObjectType, newID: z.string(), oldID: z.string()}); export const objectIdentifiers = z.object({objectIdentifierMap: z.array(objectPair)}); 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 }; /** * Copy Project from one organization to another * * POST /api/site/copy * * Also accepts auth: DEPRECATED - API Key, API Key, Basic Auth */ export const createApiApiSiteCopy = async (args: CreateApiApiSiteCopyArgs, options: CreateApiApiSiteCopyOptions) => { const {baseUrl, fetch: fetchFn = fetch, AuthenticationToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.targetOrganizationID != null) query.append("targetOrganizationID", String(args.targetOrganizationID)) if (args.siteID != null) query.append("siteID", String(args.siteID)) if (args.siteName != null) query.append("siteName", String(args.siteName)) if (args.siteDirectory != null) query.append("siteDirectory", String(args.siteDirectory)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (AuthenticationToken !== undefined) headers.set("X-PL-AUTH", AuthenticationToken) const url = `${baseUrl.replace(/\/+$/, '')}/api/site/copy${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(objectIdentifiers, await res.json()) };