@dronedeploy/rocos-js-sdk 3.0.1-alpha.5 → 3.0.1-alpha.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. package/cjs/RocosSDK.d.ts +2 -1
  2. package/cjs/RocosSDK.js +38 -26
  3. package/cjs/api/streams/caller/CallerStream.js +2 -1
  4. package/cjs/api/streams/command/CommandStream.js +2 -1
  5. package/cjs/api/streams/control/ControlStream.js +2 -1
  6. package/cjs/api/streams/fileAccessor/FileAccessorStream.js +2 -1
  7. package/cjs/api/streams/search/SearchStream.js +2 -1
  8. package/cjs/api/streams/telemetry/TelemetryStream.js +2 -1
  9. package/cjs/api/streams/webRTCSignalling/WebRTCSignallingStream.js +2 -1
  10. package/cjs/constants/api.d.ts +1 -0
  11. package/cjs/constants/api.js +2 -1
  12. package/cjs/helpers/getSha256Hex.d.ts +1 -0
  13. package/cjs/helpers/getSha256Hex.js +9 -0
  14. package/cjs/helpers/getSha256Hex.spec.d.ts +1 -0
  15. package/cjs/helpers/getSha256Hex.spec.js +19 -0
  16. package/cjs/helpers/getSha256HexNode.d.ts +1 -0
  17. package/cjs/helpers/getSha256HexNode.js +13 -0
  18. package/cjs/helpers/getSha256HexNode.spec.d.ts +1 -0
  19. package/cjs/helpers/getSha256HexNode.spec.js +10 -0
  20. package/cjs/models/IRocosSDKConfig.d.ts +8 -4
  21. package/cjs/models/workflow/Workflow.d.ts +69 -11
  22. package/cjs/node/RocosSDKNode.d.ts +1 -2
  23. package/cjs/node/RocosSDKNode.js +54 -65
  24. package/cjs/services/AssetStorageService.js +1 -1
  25. package/cjs/services/BaseStreamService.js +2 -0
  26. package/cjs/services/WorkflowService.d.ts +11 -16
  27. package/cjs/services/WorkflowService.js +32 -15
  28. package/cjs/services/WorkflowServiceNode.d.ts +4 -0
  29. package/cjs/services/WorkflowServiceNode.js +12 -0
  30. package/esm/RocosSDK.d.ts +2 -1
  31. package/esm/RocosSDK.js +38 -26
  32. package/esm/api/streams/caller/CallerStream.js +2 -1
  33. package/esm/api/streams/command/CommandStream.js +2 -1
  34. package/esm/api/streams/control/ControlStream.js +2 -1
  35. package/esm/api/streams/fileAccessor/FileAccessorStream.js +2 -1
  36. package/esm/api/streams/search/SearchStream.js +2 -1
  37. package/esm/api/streams/telemetry/TelemetryStream.js +2 -1
  38. package/esm/api/streams/webRTCSignalling/WebRTCSignallingStream.js +2 -1
  39. package/esm/constants/api.d.ts +1 -0
  40. package/esm/constants/api.js +1 -0
  41. package/esm/helpers/getSha256Hex.d.ts +1 -0
  42. package/esm/helpers/getSha256Hex.js +5 -0
  43. package/esm/helpers/getSha256Hex.spec.d.ts +1 -0
  44. package/esm/helpers/getSha256Hex.spec.js +14 -0
  45. package/esm/helpers/getSha256HexNode.d.ts +1 -0
  46. package/esm/helpers/getSha256HexNode.js +6 -0
  47. package/esm/helpers/getSha256HexNode.spec.d.ts +1 -0
  48. package/esm/helpers/getSha256HexNode.spec.js +8 -0
  49. package/esm/models/IRocosSDKConfig.d.ts +8 -4
  50. package/esm/models/workflow/Workflow.d.ts +69 -11
  51. package/esm/node/RocosSDKNode.d.ts +1 -2
  52. package/esm/node/RocosSDKNode.js +26 -37
  53. package/esm/services/AssetStorageService.js +3 -3
  54. package/esm/services/BaseStreamService.js +2 -0
  55. package/esm/services/WorkflowService.d.ts +11 -16
  56. package/esm/services/WorkflowService.js +33 -16
  57. package/esm/services/WorkflowServiceNode.d.ts +4 -0
  58. package/esm/services/WorkflowServiceNode.js +8 -0
  59. package/package.json +1 -1
@@ -0,0 +1,6 @@
1
+ import crypto from 'crypto';
2
+ export const getSha256HexNode = async (arrayBuffer) => {
3
+ const hashBuffer = await crypto.subtle.digest('SHA-256', arrayBuffer);
4
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
5
+ return hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
6
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import { getSha256HexNode } from './getSha256HexNode';
2
+ describe('getSha256HexNode', () => {
3
+ it('should return the correct SHA-256 hash', async () => {
4
+ const buffer = new TextEncoder().encode('Hello, World!');
5
+ const hash = await getSha256HexNode(buffer);
6
+ expect(hash).toBe('dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f');
7
+ });
8
+ });
@@ -1,6 +1,9 @@
1
1
  import { IStreamOptions } from './IStreamOptions';
2
- export type IRocosSDKConfig = IRocosSDKConfigApp | IRocosSDKConfigToken;
3
- interface IRocosSDKConfigBase {
2
+ import { ServiceEnum } from './ServiceEnum';
3
+ export type IRocosSDKConfig = AuthorisedConfig & {
4
+ overrides?: Partial<Record<ServiceEnum, Partial<AuthorisedConfig>>>;
5
+ };
6
+ interface ConfigBase {
4
7
  url: string;
5
8
  token?: string;
6
9
  refreshToken?: string;
@@ -8,12 +11,13 @@ interface IRocosSDKConfigBase {
8
11
  port?: number;
9
12
  insecure?: boolean;
10
13
  }
11
- export interface IRocosSDKConfigApp extends IRocosSDKConfigBase {
14
+ export interface IRocosSDKConfigApp extends ConfigBase {
12
15
  appId: string;
13
16
  appKey: string;
14
17
  }
15
- export interface IRocosSDKConfigToken extends IRocosSDKConfigBase {
18
+ export interface IRocosSDKConfigToken extends ConfigBase {
16
19
  token: string;
17
20
  refreshToken?: string;
18
21
  }
22
+ export type AuthorisedConfig = IRocosSDKConfigApp | IRocosSDKConfigToken;
19
23
  export {};
@@ -1,12 +1,12 @@
1
- export type ResourceType = 'Manifest' | 'AgentGraph' | 'Javascript' | 'NodeDefinition';
1
+ export type ResourceType = 'Manifest' | 'AgentGraph' | 'Javascript' | 'NodeDefinition' | 'Other';
2
2
  export interface Resource {
3
3
  type: ResourceType;
4
4
  data: string;
5
- contentLink: string;
6
5
  hash: string;
7
6
  link: string;
8
- path: string;
9
- size: number;
7
+ contentLink?: string;
8
+ size?: number;
9
+ path?: string;
10
10
  }
11
11
  export interface Resources {
12
12
  [key: string]: Resource;
@@ -16,21 +16,79 @@ export interface Author {
16
16
  name: string;
17
17
  email: string;
18
18
  }
19
+ interface ResponseBase {
20
+ id: string;
21
+ createdBy: string;
22
+ updatedBy: string;
23
+ dateCreated: number;
24
+ dateUpdated: number;
25
+ }
19
26
  export interface WorkflowBase {
20
27
  flowId: string;
21
28
  name: string;
22
- description: string;
29
+ description?: string;
23
30
  resources?: Resources;
24
31
  type?: string;
25
32
  }
26
- export interface Workflow extends WorkflowBase {
27
- id: string;
33
+ export interface Workflow extends WorkflowBase, ResponseBase {
28
34
  projectId: string;
29
- createdBy: string;
30
- updatedBy: string;
31
35
  author: Author;
32
- dateCreated: number;
33
- dateUpdated: number;
34
36
  latestVersion: string;
35
37
  type: string;
36
38
  }
39
+ export interface DeployedFlow {
40
+ flowId: string;
41
+ flow?: Workflow;
42
+ deployed?: boolean;
43
+ }
44
+ export interface WorkflowList extends Omit<WorkflowBase, 'resources'>, ResponseBase {
45
+ type?: 'cloud';
46
+ author: {
47
+ id: string;
48
+ name: string;
49
+ email: string;
50
+ } | Record<string, never>;
51
+ }
52
+ export interface WorkflowRevision {
53
+ id: string;
54
+ version: number;
55
+ authorName: string;
56
+ createdBy: string;
57
+ dateCreated: number;
58
+ description: string;
59
+ selected?: boolean;
60
+ }
61
+ export interface ManifestResource {
62
+ path: string;
63
+ provider: string;
64
+ runtime: string;
65
+ options: string[];
66
+ }
67
+ export interface ManifestArgument {
68
+ default?: string;
69
+ description?: string;
70
+ type: string;
71
+ }
72
+ export interface Manifest {
73
+ main: [string, string];
74
+ signals?: {
75
+ main: {
76
+ args: string[];
77
+ entry: [string, string];
78
+ };
79
+ };
80
+ arguments?: unknown | Record<string, ManifestArgument>;
81
+ resources: ManifestResource[];
82
+ mode?: 'cloud';
83
+ }
84
+ export interface CreateAssetResponse {
85
+ id: string;
86
+ name: string;
87
+ path: string;
88
+ dateCreated: number;
89
+ hash: string;
90
+ postContentLink: string;
91
+ type: 'file';
92
+ workflow: string;
93
+ }
94
+ export {};
@@ -1,12 +1,11 @@
1
1
  import { EvaluatorService, ScheduleService } from '../services';
2
+ import { IBaseService, ServiceEnum } from '../models';
2
3
  import { CallerServiceNode } from '../services/CallerServiceNode';
3
4
  import { CommandServiceNode } from '../services/CommandServiceNode';
4
5
  import { ControlServiceNode } from '../services/ControlServiceNode';
5
6
  import { FileAccessorServiceNode } from '../services/FileAccessorServiceNode';
6
- import { IBaseService } from '../models/IBaseService';
7
7
  import { RocosSDK } from '../RocosSDK';
8
8
  import { SearchServiceNode } from '../services/SearchServiceNode';
9
- import { ServiceEnum } from '../models/ServiceEnum';
10
9
  import { TelemetryServiceNode } from '../services/TelemetryServiceNode';
11
10
  export declare class RocosSDKNode extends RocosSDK {
12
11
  getService<T extends IBaseService>(name: ServiceEnum): T;
@@ -1,96 +1,85 @@
1
- import { MapService, ScheduleService, SpotProvisioningServiceNode } from '../services';
2
- import { AssetStorageService } from '../services/AssetStorageService';
3
- import { AuthService } from '../services/AuthService';
1
+ import { AssetStorageService, AuthService, ConfigGroupService, DashboardService, EventService, FunctionService, IntegrationService, MapService, ProfileService, ProjectService, RobotService, ScheduleService, SpotProvisioningServiceNode, StreamService, UserService, } from '../services';
2
+ import { ServiceEnum } from '../models';
4
3
  import { CallerServiceNode } from '../services/CallerServiceNode';
5
4
  import { CommandServiceNode } from '../services/CommandServiceNode';
6
- import { ConfigGroupService } from '../services/ConfigGroupService';
7
5
  import { ControlServiceNode } from '../services/ControlServiceNode';
8
- import { DashboardService } from '../services/DashboardService';
9
- import { EventService } from '../services/EventService';
10
6
  import { FileAccessorServiceNode } from '../services/FileAccessorServiceNode';
11
- import { FunctionService } from '../services/FunctionService';
12
- import { IntegrationService } from '../services/IntegrationService';
13
- import { ProfileService } from '../services/ProfileService';
14
- import { ProjectService } from '../services/ProjectService';
15
- import { RobotService } from '../services/RobotService';
16
7
  import { RocosSDK } from '../RocosSDK';
17
8
  import { SearchServiceNode } from '../services/SearchServiceNode';
18
- import { ServiceEnum } from '../models/ServiceEnum';
19
- import { StreamService } from '../services/StreamService';
20
9
  import { TelemetryServiceNode } from '../services/TelemetryServiceNode';
21
- import { UserService } from '../services/UserService';
22
- import { WorkflowService } from '../services/WorkflowService';
10
+ import { WorkflowServiceNode } from '../services/WorkflowServiceNode';
23
11
  export class RocosSDKNode extends RocosSDK {
24
12
  // eslint-disable-next-line max-lines-per-function
25
13
  getService(name) {
26
14
  if (!this.services?.[name]) {
27
15
  this.logger.info(`Service ${name} does not exist, creating it now.`);
16
+ const config = this.overrideConfig(name);
28
17
  switch (name) {
29
18
  case ServiceEnum.AUTH:
30
- this.services[name] = new AuthService(this.config);
19
+ this.services[name] = new AuthService(config);
31
20
  break;
32
21
  case ServiceEnum.TELEMETRY:
33
- this.services[name] = new TelemetryServiceNode(this.config);
22
+ this.services[name] = new TelemetryServiceNode(config);
34
23
  break;
35
24
  case ServiceEnum.ROBOT:
36
- this.services[name] = new RobotService(this.config);
25
+ this.services[name] = new RobotService(config);
37
26
  break;
38
27
  case ServiceEnum.EVENT:
39
- this.services[name] = new EventService(this.config);
28
+ this.services[name] = new EventService(config);
40
29
  break;
41
30
  case ServiceEnum.PROJECT:
42
- this.services[name] = new ProjectService(this.config);
31
+ this.services[name] = new ProjectService(config);
43
32
  break;
44
33
  case ServiceEnum.CALLER:
45
- this.services[name] = new CallerServiceNode(this.config);
34
+ this.services[name] = new CallerServiceNode(config);
46
35
  break;
47
36
  case ServiceEnum.COMMAND:
48
- this.services[name] = new CommandServiceNode(this.config);
37
+ this.services[name] = new CommandServiceNode(config);
49
38
  break;
50
39
  case ServiceEnum.CONTROL:
51
- this.services[name] = new ControlServiceNode(this.config);
40
+ this.services[name] = new ControlServiceNode(config);
52
41
  break;
53
42
  case ServiceEnum.SEARCH:
54
- this.services[name] = new SearchServiceNode(this.config);
43
+ this.services[name] = new SearchServiceNode(config);
55
44
  break;
56
45
  case ServiceEnum.CONFIG_GROUP:
57
- this.services[name] = new ConfigGroupService(this.config);
46
+ this.services[name] = new ConfigGroupService(config);
58
47
  break;
59
48
  case ServiceEnum.DASHBOARD:
60
- this.services[name] = new DashboardService(this.config);
49
+ this.services[name] = new DashboardService(config);
61
50
  break;
62
51
  case ServiceEnum.FUNCTION:
63
- this.services[name] = new FunctionService(this.config);
52
+ this.services[name] = new FunctionService(config);
64
53
  break;
65
54
  case ServiceEnum.STREAM:
66
- this.services[name] = new StreamService(this.config);
55
+ this.services[name] = new StreamService(config);
67
56
  break;
68
57
  case ServiceEnum.USER:
69
- this.services[name] = new UserService(this.config);
58
+ this.services[name] = new UserService(config);
70
59
  break;
71
60
  case ServiceEnum.WORKFLOW:
72
- this.services[name] = new WorkflowService(this.config);
61
+ this.services[name] = new WorkflowServiceNode(config);
73
62
  break;
74
63
  case ServiceEnum.ASSET_STORAGE:
75
- this.services[name] = new AssetStorageService(this.config);
64
+ this.services[name] = new AssetStorageService(config);
76
65
  break;
77
66
  case ServiceEnum.PROFILE:
78
- this.services[name] = new ProfileService(this.config);
67
+ this.services[name] = new ProfileService(config);
79
68
  break;
80
69
  case ServiceEnum.FILE_ACCESSOR:
81
- this.services[name] = new FileAccessorServiceNode(this.config);
70
+ this.services[name] = new FileAccessorServiceNode(config);
82
71
  break;
83
72
  case ServiceEnum.SPOT_PROVISIONER:
84
- this.services[name] = new SpotProvisioningServiceNode(this.config);
73
+ this.services[name] = new SpotProvisioningServiceNode(config);
85
74
  break;
86
75
  case ServiceEnum.SCHEDULE:
87
- this.services[name] = new ScheduleService(this.config);
76
+ this.services[name] = new ScheduleService(config);
88
77
  break;
89
78
  case ServiceEnum.INTEGRATION:
90
- this.services[name] = new IntegrationService(this.config);
79
+ this.services[name] = new IntegrationService(config);
91
80
  break;
92
81
  case ServiceEnum.MAP:
93
- this.services[name] = new MapService(this.config);
82
+ this.services[name] = new MapService(config);
94
83
  break;
95
84
  }
96
85
  }
@@ -1,5 +1,5 @@
1
- import { API_PROJECT_ASSET_INTEGRATION_PATH_URL, API_PROJECT_ASSET_INTEGRATION_PROVIDERS_PATH_URL, API_PROJECT_ASSET_INTEGRATIONS_PATH_URL, API_PROJECT_ASSET_PATH_URL, API_PROJECT_ASSET_PROFILES_SYNC_DEFINITION_PATH_URL, API_PROJECT_ASSET_ROBOTS_SYNC_DEFINITION_PATH_URL, API_PROJECT_ASSET_URL, API_PROJECT_FLOW_ASSET_PATH_URL, API_PROJECT_MAPPED_ASSETS_PATH_URL, API_PROJECT_MISSION_ASSET_PATH_URL, API_PROJECT_MISSION_ASSETS_PATH_URL, API_PROJECT_ROBOT_ASSET_PATH_URL, } from '../constants/api';
2
- import { errorCodes, RocosError } from '../models/RocosError';
1
+ import { API_PROJECT_ASSET_INTEGRATIONS_PATH_URL, API_PROJECT_ASSET_INTEGRATION_PATH_URL, API_PROJECT_ASSET_INTEGRATION_PROVIDERS_PATH_URL, API_PROJECT_ASSET_PATH_URL, API_PROJECT_ASSET_PROFILES_SYNC_DEFINITION_PATH_URL, API_PROJECT_ASSET_ROBOTS_SYNC_DEFINITION_PATH_URL, API_PROJECT_ASSET_URL, API_PROJECT_FLOW_ASSET_PATH_URL, API_PROJECT_MAPPED_ASSETS_PATH_URL, API_PROJECT_MISSION_ASSETS_PATH_URL, API_PROJECT_MISSION_ASSET_PATH_URL, API_PROJECT_ROBOT_ASSET_PATH_URL, } from '../constants/api';
2
+ import { RocosError, errorCodes } from '../models/RocosError';
3
3
  import { BaseServiceAbstract } from './BaseServiceAbstract';
4
4
  import { RocosLogger } from '../logger/RocosLogger';
5
5
  import { formatServiceUrl } from '../helpers/formatServiceUrl';
@@ -208,7 +208,7 @@ export class AssetStorageService extends BaseServiceAbstract {
208
208
  * @param integration
209
209
  */
210
210
  updateSyncIntegration(projectId, integrationId, integration) {
211
- return this.callPut(formatServiceUrl(API_PROJECT_ASSET_INTEGRATION_PATH_URL, { url: this.config.url, projectId, integrationId: integrationId }, this.config.insecure), integration, `Failed to update asset sync integrations for ${projectId}.`);
211
+ return this.callPut(formatServiceUrl(API_PROJECT_ASSET_INTEGRATION_PATH_URL, { url: this.config.url, projectId, integrationId }, this.config.insecure), integration, `Failed to update asset sync integrations for ${projectId}.`);
212
212
  }
213
213
  /**
214
214
  * Delete a Sync Integration
@@ -14,6 +14,8 @@ export class BaseStreamService {
14
14
  return this.status;
15
15
  }
16
16
  async initStream(stream) {
17
+ if (this.config.insecure)
18
+ return; // no need to init auth for insecure connections
17
19
  const authService = RocosStore.getSDKInstance(this.config).getAuthService();
18
20
  if (!this.config.token || !authService.isTokenValid(new Token(this.config.token))) {
19
21
  try {
@@ -1,5 +1,5 @@
1
- import { RocosError } from '../models/RocosError';
2
1
  import type { Workflow, WorkflowBase } from '../models/workflow/Workflow';
2
+ import { RocosError } from '../models/RocosError';
3
3
  import { BaseServiceAbstract } from './BaseServiceAbstract';
4
4
  import { IBaseService } from '../models/IBaseService';
5
5
  import { IRocosSDKConfig } from '../models/IRocosSDKConfig';
@@ -13,11 +13,6 @@ export declare class WorkflowService extends BaseServiceAbstract implements IBas
13
13
  * @param projectId - Project Id
14
14
  */
15
15
  list(projectId: string): Promise<Workflow[]>;
16
- /**
17
- * @deprecated This method is deprecated and will be removed in a future version.
18
- * Use list instead.
19
- */
20
- getAll: (projectId: string) => Promise<Workflow[]>;
21
16
  /**
22
17
  * Create a workflow
23
18
  *
@@ -32,11 +27,6 @@ export declare class WorkflowService extends BaseServiceAbstract implements IBas
32
27
  * @param workflowId - Workflow Id
33
28
  */
34
29
  get(projectId: string, workflowId: string): Promise<Workflow>;
35
- /**
36
- * @deprecated This method is deprecated and will be removed in a future version.
37
- * Use get instead.
38
- */
39
- getOne: (projectId: string, workflowId: string) => Promise<Workflow>;
40
30
  /**
41
31
  * Update a workflow
42
32
  *
@@ -45,6 +35,16 @@ export declare class WorkflowService extends BaseServiceAbstract implements IBas
45
35
  * @param model - Workflow
46
36
  */
47
37
  update(projectId: string, workflowId: string, model: Workflow): Promise<Workflow>;
38
+ /**
39
+ * Upload workflow asset
40
+ *
41
+ * @param projectId - Project Id
42
+ * @param workflowId - Workflow Id
43
+ * @param fileName - Name of file including extension
44
+ * @param arrayBuffer - data to upload
45
+ */
46
+ uploadAsset(projectId: string, workflowId: string, fileName: string, arrayBuffer: ArrayBuffer): Promise<void>;
47
+ protected uploadAssetWithHash(projectId: string, workflowId: string, fileName: string, arrayBuffer: ArrayBuffer, hash: string): Promise<void>;
48
48
  /**
49
49
  * Delete a workflow
50
50
  *
@@ -59,9 +59,4 @@ export declare class WorkflowService extends BaseServiceAbstract implements IBas
59
59
  * @param callsign - Robot callsign
60
60
  */
61
61
  getDeployedWorkflows(projectId: string, callsign: string): Promise<Workflow[]>;
62
- /**
63
- * @deprecated This method is deprecated and will be removed in a future version.
64
- * Use getDeployedWorkflow instead.
65
- */
66
- getDeployedOnRobot: (projectId: string, callsign: string) => Promise<Workflow[]>;
67
62
  }
@@ -1,26 +1,12 @@
1
- import { API_PROJECT_DEPLOYED_WORKFLOW_URL, API_PROJECT_WORKFLOW_ID_URL, API_PROJECT_WORKFLOW_URL, } from '../constants/api';
1
+ import { API_PROJECT_DEPLOYED_WORKFLOW_URL, API_PROJECT_WORKFLOW_ASSET_URL, API_PROJECT_WORKFLOW_ID_URL, API_PROJECT_WORKFLOW_URL, } from '../constants/api';
2
2
  import { RocosError, errorCodes } from '../models/RocosError';
3
3
  import { BaseServiceAbstract } from './BaseServiceAbstract';
4
4
  import { RocosLogger } from '../logger/RocosLogger';
5
5
  import { formatServiceUrl } from '../helpers/formatServiceUrl';
6
+ import { getSha256Hex } from '../helpers/getSha256Hex';
6
7
  export class WorkflowService extends BaseServiceAbstract {
7
8
  constructor(config) {
8
9
  super(config);
9
- /**
10
- * @deprecated This method is deprecated and will be removed in a future version.
11
- * Use list instead.
12
- */
13
- this.getAll = this.list;
14
- /**
15
- * @deprecated This method is deprecated and will be removed in a future version.
16
- * Use get instead.
17
- */
18
- this.getOne = this.get;
19
- /**
20
- * @deprecated This method is deprecated and will be removed in a future version.
21
- * Use getDeployedWorkflow instead.
22
- */
23
- this.getDeployedOnRobot = this.getDeployedWorkflows;
24
10
  this.logger = RocosLogger.getInstance(`WorkflowService(${this.config.url})`);
25
11
  }
26
12
  getError(e) {
@@ -65,6 +51,37 @@ export class WorkflowService extends BaseServiceAbstract {
65
51
  async update(projectId, workflowId, model) {
66
52
  return this.callPut(formatServiceUrl(API_PROJECT_WORKFLOW_ID_URL, { url: this.config.url, projectId, workflowId }, this.config.insecure), model, `Failed to update workflow for ${projectId}, workflowId ${workflowId}.`);
67
53
  }
54
+ /**
55
+ * Upload workflow asset
56
+ *
57
+ * @param projectId - Project Id
58
+ * @param workflowId - Workflow Id
59
+ * @param fileName - Name of file including extension
60
+ * @param arrayBuffer - data to upload
61
+ */
62
+ async uploadAsset(projectId, workflowId, fileName, arrayBuffer) {
63
+ const hash = await getSha256Hex(arrayBuffer);
64
+ return this.uploadAssetWithHash(projectId, workflowId, fileName, arrayBuffer, hash);
65
+ }
66
+ async uploadAssetWithHash(projectId, workflowId, fileName, arrayBuffer, hash) {
67
+ // Get asset upload link
68
+ const createAssetResponse = await this.callPost(formatServiceUrl(API_PROJECT_WORKFLOW_ASSET_URL, { url: this.config.url, projectId, workflowId }, this.config.insecure), { name: fileName, type: 'file', hash }, `Failed to create asset for ${projectId}, workflowId ${workflowId}.`);
69
+ const { postContentLink } = createAssetResponse;
70
+ if (!postContentLink)
71
+ throw new Error('postContentLink is undefined');
72
+ // Get Google upload location
73
+ const postFileResponse = await fetch(postContentLink, {
74
+ method: 'POST',
75
+ headers: { 'x-goog-resumable': 'start' },
76
+ });
77
+ const putLocation = postFileResponse.headers.get('location');
78
+ if (!putLocation)
79
+ throw new Error('putLocation is undefined');
80
+ // Upload file to Google
81
+ const uploadResponse = await fetch(putLocation, { method: 'PUT', body: arrayBuffer });
82
+ if (uploadResponse.status !== 200)
83
+ throw new Error(`Failed to upload file: ${uploadResponse.statusText}`);
84
+ }
68
85
  /**
69
86
  * Delete a workflow
70
87
  *
@@ -0,0 +1,4 @@
1
+ import { WorkflowService } from './WorkflowService';
2
+ export declare class WorkflowServiceNode extends WorkflowService {
3
+ uploadAsset(projectId: string, workflowId: string, fileName: string, arrayBuffer: ArrayBuffer): Promise<void>;
4
+ }
@@ -0,0 +1,8 @@
1
+ import { WorkflowService } from './WorkflowService';
2
+ import { getSha256HexNode } from '../helpers/getSha256HexNode';
3
+ export class WorkflowServiceNode extends WorkflowService {
4
+ async uploadAsset(projectId, workflowId, fileName, arrayBuffer) {
5
+ const hash = await getSha256HexNode(arrayBuffer);
6
+ return this.uploadAssetWithHash(projectId, workflowId, fileName, arrayBuffer, hash);
7
+ }
8
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dronedeploy/rocos-js-sdk",
3
- "version": "3.0.1-alpha.5",
3
+ "version": "3.0.1-alpha.7",
4
4
  "description": "Javascript SDK for rocos",
5
5
  "main": "cjs/index.js",
6
6
  "module": "esm/index.js",