@kmlckj/licos-platform-sdk 0.8.1 → 0.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -5
- package/dist/index.d.ts +15 -13
- package/dist/index.js +1 -1
- package/dist/storage.js +1 -1
- package/dist/uns.d.ts +135 -0
- package/dist/uns.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -94,7 +94,7 @@ const file = await storage.uploadFile('/tmp/report.pdf');
|
|
|
94
94
|
const link = await storage.shareUrl(file.id);
|
|
95
95
|
```
|
|
96
96
|
|
|
97
|
-
`uploadFile` limits each file to
|
|
97
|
+
`uploadFile` limits each file to 100 MiB. Payloads larger than 8 MiB automatically use the platform chunk-upload protocol.
|
|
98
98
|
|
|
99
99
|
The browser entry does not export object storage helpers because runtime tokens
|
|
100
100
|
must not be bundled into public frontend code.
|
|
@@ -119,10 +119,44 @@ const results = await knowledge.search('How do I reset my password?', {
|
|
|
119
119
|
});
|
|
120
120
|
```
|
|
121
121
|
|
|
122
|
-
The browser entry does not export knowledge helpers because runtime tokens must
|
|
123
|
-
not be bundled into public frontend code.
|
|
124
|
-
|
|
125
|
-
##
|
|
122
|
+
The browser entry does not export knowledge helpers because runtime tokens must
|
|
123
|
+
not be bundled into public frontend code.
|
|
124
|
+
|
|
125
|
+
## Unified Namespace (UNS)
|
|
126
|
+
|
|
127
|
+
UNS helpers expose the same Schema, resource discovery, validation, plan,
|
|
128
|
+
apply, binding, data, point, video, and enterprise path APIs as the Python
|
|
129
|
+
platform SDK. They are server-only and derive the trusted project scope from
|
|
130
|
+
the injected runtime identity.
|
|
131
|
+
|
|
132
|
+
```js
|
|
133
|
+
import { uns } from '@kmlckj/licos-platform-sdk';
|
|
134
|
+
|
|
135
|
+
const schema = await uns.schemaCurrent();
|
|
136
|
+
const checked = await uns.validate(document);
|
|
137
|
+
const reviewedPlan = await uns.plan(document);
|
|
138
|
+
|
|
139
|
+
await uns.apply(document, {
|
|
140
|
+
expectedDocumentHash: reviewedPlan.documentHash,
|
|
141
|
+
expectedPlanHash: reviewedPlan.planHash,
|
|
142
|
+
confirmed: true,
|
|
143
|
+
});
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Use `uns.openPath(path)` when business code targets an existing enterprise UNS
|
|
147
|
+
path without creating or changing `UNS.json`:
|
|
148
|
+
|
|
149
|
+
```js
|
|
150
|
+
const temperature = uns.openPath('project:/production-line/temperature');
|
|
151
|
+
const current = await temperature.pointCurrent();
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Resource apply, data writes, point writes, and video controls require explicit
|
|
155
|
+
confirmation. Data deletion accepts the exact rows returned by a prior query
|
|
156
|
+
and also requires explicit confirmation. Point writes require a reason and ticket. The browser entry
|
|
157
|
+
does not export UNS helpers.
|
|
158
|
+
|
|
159
|
+
## OAuth2 Application Management
|
|
126
160
|
|
|
127
161
|
OAuth2 management is available only from trusted Node runtime code. It uses the
|
|
128
162
|
current project owner's platform identity and is not exported by the browser
|
package/dist/index.d.ts
CHANGED
|
@@ -26,7 +26,9 @@ export interface RuntimeConfig {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
export function runtimeConfig(options?: { environmentOverrideEnv?: string }): Promise<RuntimeConfig>;
|
|
29
|
-
export function authHeaders(config: RuntimeConfig, contentType?: string): Record<string, string>;
|
|
29
|
+
export function authHeaders(config: RuntimeConfig, contentType?: string): Record<string, string>;
|
|
30
|
+
|
|
31
|
+
export * from './uns.js';
|
|
30
32
|
|
|
31
33
|
export type OAuthLoginAudience = 'ALL' | 'OWNER_TENANT';
|
|
32
34
|
|
|
@@ -115,19 +117,19 @@ export class OAuthProtocolError extends ApiError {
|
|
|
115
117
|
error?: string;
|
|
116
118
|
}
|
|
117
119
|
|
|
118
|
-
export interface OAuthRuntimeConfig {
|
|
119
|
-
clientId: string;
|
|
120
|
-
publicBaseUrl: string;
|
|
121
|
-
scopes: readonly string[];
|
|
122
|
-
redirectUriConfigs: readonly OAuthRedirectUriConfig[];
|
|
123
|
-
redirectUris: readonly string[];
|
|
120
|
+
export interface OAuthRuntimeConfig {
|
|
121
|
+
clientId: string;
|
|
122
|
+
publicBaseUrl: string;
|
|
123
|
+
scopes: readonly string[];
|
|
124
|
+
redirectUriConfigs: readonly OAuthRedirectUriConfig[];
|
|
125
|
+
redirectUris: readonly string[];
|
|
124
126
|
endpoints: Readonly<Record<string, string>>;
|
|
125
127
|
appId?: string;
|
|
126
128
|
appCode?: string;
|
|
127
129
|
loginAudience?: OAuthLoginAudience;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export function oauthRedirectUriForEnvironment(config: OAuthRuntimeConfig, environment: string): string;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function oauthRedirectUriForEnvironment(config: OAuthRuntimeConfig, environment: string): string;
|
|
131
133
|
|
|
132
134
|
export interface OAuthAuthorizationRequest {
|
|
133
135
|
authorizationUrl: string;
|
|
@@ -180,9 +182,9 @@ export function completeOAuthAuthorization(
|
|
|
180
182
|
options: { code: string; redirectUri: string; codeVerifier: string },
|
|
181
183
|
): Promise<{ tokens: OAuthTokenResponse; user: Record<string, unknown> }>;
|
|
182
184
|
|
|
183
|
-
export const oauthRuntime: {
|
|
184
|
-
loadConfig: typeof loadOAuthRuntimeConfig;
|
|
185
|
-
redirectUriForEnvironment: typeof oauthRedirectUriForEnvironment;
|
|
185
|
+
export const oauthRuntime: {
|
|
186
|
+
loadConfig: typeof loadOAuthRuntimeConfig;
|
|
187
|
+
redirectUriForEnvironment: typeof oauthRedirectUriForEnvironment;
|
|
186
188
|
createAuthorizationRequest: typeof createOAuthAuthorizationRequest;
|
|
187
189
|
exchangeAuthorizationCode: typeof exchangeOAuthAuthorizationCode;
|
|
188
190
|
refreshAccessToken: typeof refreshOAuthAccessToken;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export{PlatformSdkError,ConfigurationError,ApiError}from"./shared.js";export{runtimeConfig,authHeaders}from"./runtime.js";export*from"./database.js";export*from"./knowledge.js";export*from"./oauth.js";export*from"./oauth-runtime.js";export*from"./storage.js";export{studioDatabase}from"./studio-database.js";
|
|
1
|
+
export{PlatformSdkError,ConfigurationError,ApiError}from"./shared.js";export{runtimeConfig,authHeaders}from"./runtime.js";export*from"./database.js";export*from"./knowledge.js";export*from"./oauth.js";export*from"./oauth-runtime.js";export*from"./storage.js";export*from"./uns.js";export{studioDatabase}from"./studio-database.js";
|
package/dist/storage.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{readFile as
|
|
1
|
+
import{createReadStream as e}from"node:fs";import{readFile as t,stat as o}from"node:fs/promises";import n from"node:path";import{ApiError as r}from"./shared.js";import{authHeaders as a,refreshRuntimeConfig as i,runtimeConfig as s,shouldRefreshUserToken as l}from"./runtime.js";export const MAX_UPLOAD_FILE_BYTES=104857600;export const DIRECT_UPLOAD_THRESHOLD_BYTES=8388608;async function c(){return s({environmentOverrideEnv:"LICOS_STORAGE_SCOPE"})}function d(e,t,o){return`${e.baseUrl}/api/v1/studio/storage${t}?${function(e,t={}){const o=new URLSearchParams;o.set("projectId",e.projectId),o.set("scope",e.environment);for(const[e,n]of Object.entries(t))null!=n&&o.set(e,String(n));return o.toString()}(e,o)}`}async function f(e){const t=await e.text(),o=t?JSON.parse(t):null;if(!e.ok)throw new r(t||`platform storage API returned ${e.status}`,{status:e.status,details:o});if(!o||"object"!=typeof o)return o;if(void 0!==o.code&&0!==o.code||!1===o.success)throw new r(o.message||"platform storage API failed",{status:e.status,code:"number"==typeof o.code?o.code:void 0,details:o});return o.data}async function u(e,t,{params:o,body:n}={}){let s=await c();for(let r=0;r<2;r+=1)try{const r=await fetch(d(s,t,o),{method:e,headers:a(s),body:void 0===n?void 0:JSON.stringify(n)});return await f(r)}catch(e){if(0===r&&l(e)){s=await i(s);continue}throw e}throw new r("platform storage API failed")}function p(e){if(e>104857600)throw new RangeError("uploadFile input exceeds max size of 104857600 bytes")}export async function summary(){return u("GET","/summary")}export async function listFolders(){return await u("GET","/folders")||[]}export async function listEntries({folderId:e}={}){return u("GET","/entries",{params:{folderId:e}})}export async function listFiles(){return await u("GET","/files")||[]}export async function createFolder(e,{parentId:t}={}){return u("POST","/folders",{body:{folderName:e,parentId:t}})}export async function renameFolder(e,t){return u("PUT",`/folders/${encodeURIComponent(e)}`,{body:{folderName:t}})}export async function deleteFolder(e){await u("DELETE",`/folders/${encodeURIComponent(e)}`)}export async function uploadFile(s,d={}){const y=await async function(r,a={}){if("string"==typeof r){const i=await o(r);return p(i.size),{size:i.size,fileName:a.fileName||n.basename(r),contentType:a.contentType||"application/octet-stream",readAll:async()=>t(r),chunks:t=>e(r,{highWaterMark:t})}}if(r instanceof Blob)return p(r.size),{size:r.size,fileName:a.fileName||"file",contentType:a.contentType||r.type||"application/octet-stream",readAll:async()=>new Uint8Array(await r.arrayBuffer()),chunks:async function*(e){for(let t=0;t<r.size;t+=e)yield new Uint8Array(await r.slice(t,t+e).arrayBuffer())}};if(r instanceof Uint8Array||Buffer.isBuffer(r)){p(r.byteLength);const e=Buffer.from(r);return{size:e.byteLength,fileName:a.fileName||"file",contentType:a.contentType||"application/octet-stream",readAll:async()=>e,chunks:async function*(t){for(let o=0;o<e.byteLength;o+=t)yield e.subarray(o,Math.min(o+t,e.byteLength))}}}throw new TypeError("uploadFile input must be a file path, Blob, Buffer, or Uint8Array")}(s,d);if(y.size>8388608)return async function(e,t){const o=await u("POST","/files/upload/init",{body:{fileName:e.fileName,contentType:e.contentType,sizeBytes:e.size,folderId:t.folderId}}),n=o?.uploadId;if(!n)throw new r("platform storage upload init returned no uploadId");const a=Number(o.partSize||8388608);let i=0;try{for await(const t of e.chunks(a))i+=1,await m("PUT",`/files/upload/${encodeURIComponent(n)}/parts/${i}`,t);return await u("POST",`/files/upload/${encodeURIComponent(n)}/complete`,{body:{partCount:i}})}catch(e){try{await u("DELETE",`/files/upload/${encodeURIComponent(n)}`)}catch{}throw e}}(y,d);const w=await y.readAll(),h=new Blob([w],{type:y.contentType});let b=await c();for(let e=0;e<2;e+=1){const t=new FormData;t.set("projectId",b.projectId),t.set("scope",b.environment),d.folderId&&t.set("folderId",d.folderId),t.set("file",h,y.fileName);try{const e=await fetch(`${b.baseUrl}/api/v1/studio/storage/files/upload`,{method:"POST",headers:a(b,null),body:t});return await f(e)}catch(t){if(0===e&&l(t)){b=await i(b);continue}throw t}}throw new r("platform storage upload failed")}async function m(e,t,o){let n=await c();for(let r=0;r<2;r+=1)try{const r=await fetch(d(n,t),{method:e,headers:a(n,"application/octet-stream"),body:o});return await f(r)}catch(e){if(0===r&&l(e)){n=await i(n);continue}throw e}throw new r("platform storage upload failed")}export async function renameFile(e,t){return u("PUT",`/files/${encodeURIComponent(e)}/rename`,{body:{fileName:t}})}export async function moveFile(e,{folderId:t}={}){return u("PUT",`/files/${encodeURIComponent(e)}/move`,{body:{folderId:t}})}export async function shareUrl(e,{expirySeconds:t}={}){return u("POST",`/files/${encodeURIComponent(e)}/share-url`,{body:{expirySeconds:t}})}export async function objectExists({fileId:e,objectKey:t}={}){return u("POST","/objects/exists",{body:{fileId:e,objectKey:t}})}export async function deleteFile(e){await u("DELETE",`/files/${encodeURIComponent(e)}`)}export const storage={summary:summary,listFolders:listFolders,listEntries:listEntries,listFiles:listFiles,createFolder:createFolder,renameFolder:renameFolder,deleteFolder:deleteFolder,uploadFile:uploadFile,renameFile:renameFile,moveFile:moveFile,shareUrl:shareUrl,objectExists:objectExists,deleteFile:deleteFile};
|
package/dist/uns.d.ts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
export type UnsObject = Record<string, unknown>;
|
|
2
|
+
|
|
3
|
+
export interface UnsDataQueryOptions {
|
|
4
|
+
pageNum?: number;
|
|
5
|
+
page_num?: number;
|
|
6
|
+
pageSize?: number;
|
|
7
|
+
page_size?: number;
|
|
8
|
+
paginationMode?: string;
|
|
9
|
+
pagination_mode?: string;
|
|
10
|
+
pageToken?: string;
|
|
11
|
+
page_token?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface UnsPointHistoryOptions {
|
|
15
|
+
startTime?: string;
|
|
16
|
+
start_time?: string;
|
|
17
|
+
endTime?: string;
|
|
18
|
+
end_time?: string;
|
|
19
|
+
page?: number;
|
|
20
|
+
pageSize?: number;
|
|
21
|
+
page_size?: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface UnsPointWriteOptions {
|
|
25
|
+
reason: string;
|
|
26
|
+
ticket: string;
|
|
27
|
+
requestId?: string;
|
|
28
|
+
request_id?: string;
|
|
29
|
+
dryRun?: boolean;
|
|
30
|
+
dry_run?: boolean;
|
|
31
|
+
confirmed?: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface UnsVideoControlOptions {
|
|
35
|
+
parameters?: UnsObject;
|
|
36
|
+
confirmed: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function getCurrentUnsSchema(): Promise<UnsObject>;
|
|
40
|
+
export function getUnsSchema(version?: string): Promise<UnsObject>;
|
|
41
|
+
export function listUnsSchemaBlocks(version?: string): Promise<UnsObject[]>;
|
|
42
|
+
export function getUnsSchemaBlock(blockCode: string, options?: { version?: string; depth?: number }): Promise<UnsObject>;
|
|
43
|
+
export function getUnsProtocolSchema(protocol: string, options?: { version?: string }): Promise<UnsObject>;
|
|
44
|
+
export function discoverUnsResources(options?: { resourceTypes?: string[]; protocols?: string[] }): Promise<UnsObject>;
|
|
45
|
+
export function searchUnsResources(
|
|
46
|
+
resourceType: string,
|
|
47
|
+
options?: { protocol?: string; keyword?: string; selector?: UnsObject; page?: number; pageSize?: number; page_size?: number },
|
|
48
|
+
): Promise<UnsObject>;
|
|
49
|
+
export function listUnsBindings(): Promise<UnsObject[]>;
|
|
50
|
+
export function resolveUnsPath(path: string): Promise<UnsObject>;
|
|
51
|
+
export function validateUnsDocument(document: UnsObject): Promise<UnsObject>;
|
|
52
|
+
export function planUnsDocument(document: UnsObject): Promise<UnsObject>;
|
|
53
|
+
export function applyUnsDocument(
|
|
54
|
+
document: UnsObject,
|
|
55
|
+
options: {
|
|
56
|
+
expectedDocumentHash?: string;
|
|
57
|
+
expected_document_hash?: string;
|
|
58
|
+
expectedPlanHash?: string;
|
|
59
|
+
expected_plan_hash?: string;
|
|
60
|
+
confirmed: boolean;
|
|
61
|
+
},
|
|
62
|
+
): Promise<UnsObject>;
|
|
63
|
+
export function queryUnsNodeData(nodeCode: string, options?: UnsDataQueryOptions): Promise<UnsObject>;
|
|
64
|
+
export function writeUnsNodeData(
|
|
65
|
+
nodeCode: string,
|
|
66
|
+
fieldValues: UnsObject,
|
|
67
|
+
options: { confirmed: boolean },
|
|
68
|
+
): Promise<UnsObject>;
|
|
69
|
+
export function deleteUnsNodeData(nodeCode: string, rows: UnsObject[], options: { confirmed: boolean }): Promise<UnsObject>;
|
|
70
|
+
export function queryUnsPathData(path: string, options?: UnsDataQueryOptions): Promise<UnsObject>;
|
|
71
|
+
export function writeUnsPathData(path: string, fieldValues: UnsObject, options: { confirmed: boolean }): Promise<UnsObject>;
|
|
72
|
+
export function deleteUnsPathData(path: string, rows: UnsObject[], options: { confirmed: boolean }): Promise<UnsObject>;
|
|
73
|
+
export function getUnsPointCurrent(nodeCode: string): Promise<UnsObject>;
|
|
74
|
+
export function getUnsPathPointCurrent(path: string): Promise<UnsObject>;
|
|
75
|
+
export function getUnsPointHistory(nodeCode: string, options: UnsPointHistoryOptions): Promise<UnsObject>;
|
|
76
|
+
export function getUnsPathPointHistory(path: string, options: UnsPointHistoryOptions): Promise<UnsObject>;
|
|
77
|
+
export function writeUnsPoint(nodeCode: string, value: unknown, options: UnsPointWriteOptions): Promise<UnsObject>;
|
|
78
|
+
export function writeUnsPathPoint(path: string, value: unknown, options: UnsPointWriteOptions): Promise<UnsObject>;
|
|
79
|
+
export function getUnsVideoStreamOptions(nodeCode: string): Promise<UnsObject>;
|
|
80
|
+
export function getUnsPathVideoStreamOptions(path: string): Promise<UnsObject>;
|
|
81
|
+
export function createUnsVideoSnapshot(nodeCode: string): Promise<UnsObject>;
|
|
82
|
+
export function createUnsPathVideoSnapshot(path: string): Promise<UnsObject>;
|
|
83
|
+
export function controlUnsVideo(nodeCode: string, action: string, options: UnsVideoControlOptions): Promise<UnsObject>;
|
|
84
|
+
export function controlUnsPathVideo(path: string, action: string, options: UnsVideoControlOptions): Promise<UnsObject>;
|
|
85
|
+
|
|
86
|
+
export class UnsPathNode {
|
|
87
|
+
readonly path: string;
|
|
88
|
+
constructor(path: string);
|
|
89
|
+
resolve(): Promise<UnsObject>;
|
|
90
|
+
queryData(options?: UnsDataQueryOptions): Promise<UnsObject>;
|
|
91
|
+
writeData(fieldValues: UnsObject, options: { confirmed: boolean }): Promise<UnsObject>;
|
|
92
|
+
deleteData(rows: UnsObject[], options: { confirmed: boolean }): Promise<UnsObject>;
|
|
93
|
+
pointCurrent(): Promise<UnsObject>;
|
|
94
|
+
pointHistory(options: UnsPointHistoryOptions): Promise<UnsObject>;
|
|
95
|
+
writePoint(value: unknown, options: UnsPointWriteOptions): Promise<UnsObject>;
|
|
96
|
+
videoStreamOptions(): Promise<UnsObject>;
|
|
97
|
+
videoSnapshot(): Promise<UnsObject>;
|
|
98
|
+
controlVideo(action: string, options: UnsVideoControlOptions): Promise<UnsObject>;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function openUnsPath(path: string): UnsPathNode;
|
|
102
|
+
|
|
103
|
+
export const uns: {
|
|
104
|
+
schemaCurrent: typeof getCurrentUnsSchema;
|
|
105
|
+
schema: typeof getUnsSchema;
|
|
106
|
+
schemaBlocks: typeof listUnsSchemaBlocks;
|
|
107
|
+
schemaBlock: typeof getUnsSchemaBlock;
|
|
108
|
+
protocolSchema: typeof getUnsProtocolSchema;
|
|
109
|
+
discover: typeof discoverUnsResources;
|
|
110
|
+
searchResources: typeof searchUnsResources;
|
|
111
|
+
bindings: typeof listUnsBindings;
|
|
112
|
+
resolvePath: typeof resolveUnsPath;
|
|
113
|
+
validate: typeof validateUnsDocument;
|
|
114
|
+
plan: typeof planUnsDocument;
|
|
115
|
+
apply: typeof applyUnsDocument;
|
|
116
|
+
queryNodeData: typeof queryUnsNodeData;
|
|
117
|
+
writeNodeData: typeof writeUnsNodeData;
|
|
118
|
+
deleteNodeData: typeof deleteUnsNodeData;
|
|
119
|
+
queryPathData: typeof queryUnsPathData;
|
|
120
|
+
writePathData: typeof writeUnsPathData;
|
|
121
|
+
deletePathData: typeof deleteUnsPathData;
|
|
122
|
+
pointCurrent: typeof getUnsPointCurrent;
|
|
123
|
+
pathPointCurrent: typeof getUnsPathPointCurrent;
|
|
124
|
+
pointHistory: typeof getUnsPointHistory;
|
|
125
|
+
pathPointHistory: typeof getUnsPathPointHistory;
|
|
126
|
+
writePoint: typeof writeUnsPoint;
|
|
127
|
+
writePathPoint: typeof writeUnsPathPoint;
|
|
128
|
+
videoStreamOptions: typeof getUnsVideoStreamOptions;
|
|
129
|
+
pathVideoStreamOptions: typeof getUnsPathVideoStreamOptions;
|
|
130
|
+
videoSnapshot: typeof createUnsVideoSnapshot;
|
|
131
|
+
pathVideoSnapshot: typeof createUnsPathVideoSnapshot;
|
|
132
|
+
controlVideo: typeof controlUnsVideo;
|
|
133
|
+
controlPathVideo: typeof controlUnsPathVideo;
|
|
134
|
+
openPath: typeof openUnsPath;
|
|
135
|
+
};
|
package/dist/uns.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ApiError as t}from"./shared.js";import{authHeaders as e,refreshRuntimeConfig as n,runtimeConfig as o,shouldRefreshUserToken as r}from"./runtime.js";function a(t){return Object.fromEntries(Object.entries(t).filter(([,t])=>null!=t))}function i(t,e){return`/projects/${encodeURIComponent(t.projectId)}${e}`}function s(t,e,n,o){return i(t,`/uns/runtime/${e}/${encodeURIComponent(n)}/${o}`)}function c(t,e,n){const o=String(n||"").trim();if(!o)throw new TypeError("UNS path cannot be empty");return`${i(t,`/uns/runtime/path/${e}`)}?${new URLSearchParams({path:o})}`}function u(t,e){return new URL(`${t.baseUrl}/api/v1${e}`)}async function p(e){const n=await e.text();let o=null;try{o=n?JSON.parse(n):null}catch{throw new t(n||`platform UNS API returned ${e.status}`,{status:e.status,details:n})}if(!e.ok)throw new t(o?.message||n||`platform UNS API returned ${e.status}`,{status:e.status,code:"number"==typeof o?.code?o.code:void 0,details:o});if(!o||"object"!=typeof o)return o;if(void 0!==o.code&&0!==o.code||!1===o.success)throw new t(o.message||"platform UNS API failed",{status:e.status,code:"number"==typeof o.code?o.code:void 0,details:o});return o.data}async function d(i,s,{body:c,config:d}={}){let h=d||await o();for(let t=0;t<2;t+=1)try{const t="function"==typeof s?s(h):s,n=await fetch(u(h,t),{method:i,headers:e(h,void 0===c?void 0:"application/json"),body:void 0===c?void 0:JSON.stringify(a(c))});return await p(n)}catch(e){if(0===t&&r(e)){h=await n(h);continue}throw e}throw new t("platform UNS API failed")}export function getCurrentUnsSchema(){return d("GET","/uns/schemas/current")}export function getUnsSchema(t="1.0"){return d("GET",`/uns/schemas/${encodeURIComponent(t)}`)}export function listUnsSchemaBlocks(t="1.0"){return d("GET",`/uns/schemas/${encodeURIComponent(t)}/blocks`)}export function getUnsSchemaBlock(t,{version:e="1.0",depth:n=1}={}){return d("GET",`/uns/schemas/${encodeURIComponent(e)}/blocks/${encodeURIComponent(t)}?depth=${encodeURIComponent(n)}`)}export function getUnsProtocolSchema(t,{version:e="1.0"}={}){return d("GET",`/uns/schemas/${encodeURIComponent(e)}/blocks/collection-point/protocols/${encodeURIComponent(t)}`)}export async function discoverUnsResources({resourceTypes:t=[],protocols:e=[]}={}){const n=await o();return d("POST",i(n,"/uns/discover"),{body:{resourceTypes:t,protocols:e},config:n})}export async function searchUnsResources(t,e={}){const n=await o();return d("POST",i(n,"/uns/resources/search"),{body:{resourceType:t,protocol:e.protocol,keyword:e.keyword,selector:e.selector||{},page:e.page??1,pageSize:e.pageSize??e.page_size??20},config:n})}export async function listUnsBindings(){const t=await o();return d("GET",i(t,"/uns/bindings"),{config:t})}export async function resolveUnsPath(t){const e=await o();return d("GET",c(e,"resolve",t),{config:e})}export async function validateUnsDocument(t){const e=await o();return d("POST",i(e,"/uns/validate"),{body:{document:t},config:e})}export async function planUnsDocument(t){const e=await o();return d("POST",i(e,"/uns/plan"),{body:{document:t},config:e})}export async function applyUnsDocument(t,e={}){const n=String(e.expectedDocumentHash??e.expected_document_hash??"").trim(),r=String(e.expectedPlanHash??e.expected_plan_hash??"").trim();if(!0!==e.confirmed)throw new TypeError("UNS apply requires explicit confirmation");if(!n||!r)throw new TypeError("UNS apply requires document and plan hashes");const a=await o();return d("POST",i(a,"/uns/apply"),{body:{document:t,expectedDocumentHash:n,expectedPlanHash:r,confirmed:!0},config:a})}function h(t={}){return a({pageNum:t.pageNum??t.page_num??1,pageSize:t.pageSize??t.page_size??50,paginationMode:t.paginationMode??t.pagination_mode??"OFFSET",pageToken:t.pageToken??t.page_token})}export async function queryUnsNodeData(t,e={}){const n=await o();return d("POST",s(n,"nodes",t,"query"),{body:h(e),config:n})}function f(t,e){if(!0!==e)throw new TypeError("UNS data write requires explicit confirmation");if(!t||"object"!=typeof t||0===Object.keys(t).length)throw new TypeError("UNS data write field values cannot be empty")}export async function writeUnsNodeData(t,e,{confirmed:n=!1}={}){f(e,n);const r=await o();return d("POST",s(r,"nodes",t,"write"),{body:{fieldValues:e,environment:r.environment,confirmed:!0},config:r})}function m(t,e){if(!0!==e)throw new TypeError("UNS data deletion requires explicit confirmation");if(!Array.isArray(t)||0===t.length||t.some(t=>!t||"object"!=typeof t||Array.isArray(t)||0===Object.keys(t).length))throw new TypeError("UNS data deletion rows cannot be empty")}export async function deleteUnsNodeData(t,e,{confirmed:n=!1}={}){m(e,n);const r=await o();return d("POST",s(r,"nodes",t,"delete"),{body:{rows:e,confirmed:!0},config:r})}export async function queryUnsPathData(t,e={}){const n=await o();return d("POST",c(n,"data/query",t),{body:h(e),config:n})}export async function writeUnsPathData(t,e,{confirmed:n=!1}={}){f(e,n);const r=await o();return d("POST",c(r,"data/write",t),{body:{fieldValues:e,environment:r.environment,confirmed:!0},config:r})}export async function deleteUnsPathData(t,e,{confirmed:n=!1}={}){m(e,n);const r=await o();return d("POST",c(r,"data/delete",t),{body:{rows:e,confirmed:!0},config:r})}export async function getUnsPointCurrent(t){const e=await o();return d("GET",s(e,"points",t,"current"),{config:e})}export async function getUnsPathPointCurrent(t){const e=await o();return d("GET",c(e,"point/current",t),{config:e})}function y(t={}){return{startTime:t.startTime??t.start_time,endTime:t.endTime??t.end_time,page:t.page??1,pageSize:t.pageSize??t.page_size??100}}export async function getUnsPointHistory(t,e){const n=await o();return d("POST",s(n,"points",t,"history"),{body:y(e),config:n})}export async function getUnsPathPointHistory(t,e){const n=await o();return d("POST",c(n,"point/history",t),{body:y(e),config:n})}function l(t,e={}){const n=String(e.reason||"").trim(),o=String(e.ticket||"").trim(),r=e.dryRun??e.dry_run??!1,i=e.confirmed??!1;if(!r&&!0!==i)throw new TypeError("UNS point write requires explicit confirmation");if(!n||!o)throw new TypeError("UNS point write requires reason and ticket");return a({value:t,reason:n,ticket:o,requestId:e.requestId??e.request_id,dryRun:r,confirmed:i})}export async function writeUnsPoint(t,e,n={}){const r=l(e,n),a=await o();return d("POST",s(a,"points",t,"write"),{body:r,config:a})}export async function writeUnsPathPoint(t,e,n={}){const r=l(e,n),a=await o();return d("POST",c(a,"point/write",t),{body:r,config:a})}export async function getUnsVideoStreamOptions(t){const e=await o();return d("GET",s(e,"videos",t,"stream-options"),{config:e})}export async function getUnsPathVideoStreamOptions(t){const e=await o();return d("GET",c(e,"video/stream-options",t),{config:e})}export async function createUnsVideoSnapshot(t){const e=await o();return d("POST",s(e,"videos",t,"snapshot"),{config:e})}export async function createUnsPathVideoSnapshot(t){const e=await o();return d("POST",c(e,"video/snapshot",t),{config:e})}function U(t,e={}){if(!0!==e.confirmed)throw new TypeError("UNS video control requires explicit confirmation");return{action:t,parameters:e.parameters||{},confirmed:!0}}export async function controlUnsVideo(t,e,n={}){const r=U(e,n),a=await o();return d("POST",s(a,"videos",t,"control"),{body:r,config:a})}export async function controlUnsPathVideo(t,e,n={}){const r=U(e,n),a=await o();return d("POST",c(a,"video/control",t),{body:r,config:a})}export class UnsPathNode{constructor(t){if(this.path=String(t||"").trim(),!this.path)throw new TypeError("UNS path cannot be empty")}resolve(){return resolveUnsPath(this.path)}queryData(t){return queryUnsPathData(this.path,t)}writeData(t,e){return writeUnsPathData(this.path,t,e)}deleteData(t,e){return deleteUnsPathData(this.path,t,e)}pointCurrent(){return getUnsPathPointCurrent(this.path)}pointHistory(t){return getUnsPathPointHistory(this.path,t)}writePoint(t,e){return writeUnsPathPoint(this.path,t,e)}videoStreamOptions(){return getUnsPathVideoStreamOptions(this.path)}videoSnapshot(){return createUnsPathVideoSnapshot(this.path)}controlVideo(t,e){return controlUnsPathVideo(this.path,t,e)}}export function openUnsPath(t){return new UnsPathNode(t)}export const uns={schemaCurrent:getCurrentUnsSchema,schema:getUnsSchema,schemaBlocks:listUnsSchemaBlocks,schemaBlock:getUnsSchemaBlock,protocolSchema:getUnsProtocolSchema,discover:discoverUnsResources,searchResources:searchUnsResources,bindings:listUnsBindings,resolvePath:resolveUnsPath,validate:validateUnsDocument,plan:planUnsDocument,apply:applyUnsDocument,queryNodeData:queryUnsNodeData,writeNodeData:writeUnsNodeData,deleteNodeData:deleteUnsNodeData,queryPathData:queryUnsPathData,writePathData:writeUnsPathData,deletePathData:deleteUnsPathData,pointCurrent:getUnsPointCurrent,pathPointCurrent:getUnsPathPointCurrent,pointHistory:getUnsPointHistory,pathPointHistory:getUnsPathPointHistory,writePoint:writeUnsPoint,writePathPoint:writeUnsPathPoint,videoStreamOptions:getUnsVideoStreamOptions,pathVideoStreamOptions:getUnsPathVideoStreamOptions,videoSnapshot:createUnsVideoSnapshot,pathVideoSnapshot:createUnsPathVideoSnapshot,controlVideo:controlUnsVideo,controlPathVideo:controlUnsPathVideo,openPath:openUnsPath};
|