screenright-client 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -7,46 +7,50 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import * as fs from 'fs';
11
10
  import fetch from 'node-fetch';
12
11
  import process from 'node:process';
13
- const tmpDir = 'screenright/tmp';
12
+ import FormData from 'form-data';
14
13
  const result = { screenshotItemAttributes: [] };
15
14
  let deploymentId = null;
15
+ const deploymentToken = process.env.SCREENRIGHT_DEPLOYMENT_TOKEN || '';
16
16
  const baseUrl = () => {
17
17
  return `${process.env.SCREENRIGHT_ENDPOINT}/client_api`;
18
18
  };
19
+ const errorOccurred = (message) => {
20
+ console.error('[ScreenRight] Error occurred', message);
21
+ deploymentId = null;
22
+ };
19
23
  export const initializeScreenwright = () => __awaiter(void 0, void 0, void 0, function* () {
20
24
  const diagramId = process.env.SCREENRIGHT_DIAGRAM_ID;
21
- const deploymentToken = process.env.SCREENRIGHT_DEPLOYMENT_TOKEN;
22
- console.log("#", deploymentToken);
25
+ if (!diagramId || !deploymentToken) {
26
+ errorOccurred('Not set require environments.');
27
+ return;
28
+ }
23
29
  try {
24
30
  const response = yield fetch(`${baseUrl()}/diagrams/${diagramId}/deployments`, {
25
31
  method: 'POST',
26
32
  body: JSON.stringify({ deployment_token: deploymentToken }),
27
33
  headers: { 'Content-Type': 'application/json' }
28
34
  });
35
+ if (!response.ok) {
36
+ errorOccurred('Failed create deployment.');
37
+ }
29
38
  const body = yield response.text();
30
39
  const json = JSON.parse(body);
31
40
  deploymentId = json.id;
32
41
  }
33
42
  catch (e) {
34
- console.error('[ScreenRight] Error catch', e.message);
43
+ errorOccurred(e.message);
35
44
  }
36
45
  });
37
46
  export const finalize = () => __awaiter(void 0, void 0, void 0, function* () {
38
- console.log("################## -1");
39
47
  if (!deploymentId) {
40
48
  return;
41
49
  }
42
- fs.writeFileSync(`${tmpDir}/result.json`, JSON.stringify({
43
- screenshotItemAttributes: result.screenshotItemAttributes,
44
- }));
45
50
  const diagramId = process.env.SCREENRIGHT_DIAGRAM_ID;
46
- const deploymentToken = process.env.SCREENRIGHT_DEPLOYMENT_TOKEN;
47
51
  yield fetch(`${baseUrl()}/diagrams/${diagramId}/deployments/${deploymentId}/done_upload`, {
48
52
  method: 'PUT',
49
- body: JSON.stringify({ deployment_token: deploymentToken, screenshotItemAttributes: result.screenshotItemAttributes }),
53
+ body: JSON.stringify({ deployment_token: deploymentToken, blueprint: JSON.stringify({ screenshotItemAttributes: result.screenshotItemAttributes }) }),
50
54
  headers: { 'Content-Type': 'application/json' }
51
55
  });
52
56
  deploymentId = null;
@@ -55,13 +59,32 @@ export const capture = (page, key, title, parentKey) => __awaiter(void 0, void 0
55
59
  if (deploymentId === null) {
56
60
  return;
57
61
  }
58
- fs.mkdirSync(tmpDir, { recursive: true });
59
- const path = `${tmpDir}/${key}.png`;
60
- yield page.screenshot({ path, fullPage: true });
62
+ const fileName = `${key}.jpg`;
63
+ try {
64
+ const buffer = yield page.screenshot({ fullPage: true, type: 'jpeg' });
65
+ const formData = new FormData();
66
+ formData.append('file', buffer, fileName);
67
+ const diagramId = process.env.SCREENRIGHT_DIAGRAM_ID;
68
+ const response = yield fetch(`${baseUrl()}/diagrams/${diagramId}/deployments/${deploymentId}/screenshot`, {
69
+ method: 'POST',
70
+ headers: {
71
+ 'X-File-Key': key,
72
+ 'X-Deployment-Token': deploymentToken,
73
+ },
74
+ body: formData
75
+ });
76
+ if (!response.ok) {
77
+ errorOccurred('Faild screenshot upload');
78
+ return;
79
+ }
80
+ }
81
+ catch (e) {
82
+ errorOccurred(`capture: ${key}, ${e.message}`);
83
+ return;
84
+ }
61
85
  const attribute = {
62
86
  key,
63
87
  title,
64
- src: path,
65
88
  childrens: [],
66
89
  };
67
90
  if (parentKey) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "screenright-client",
3
- "version": "0.9.1",
3
+ "version": "0.9.2",
4
4
  "description": "screenright's nodejs client library",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
package/src/index.ts CHANGED
@@ -7,7 +7,6 @@ import FormData from 'form-data'
7
7
  type ScreenshotItemAttribute = {
8
8
  key: string
9
9
  title: string
10
- src: string
11
10
  childrens: ScreenshotItemAttribute[]
12
11
  }
13
12
 
@@ -15,8 +14,6 @@ type Result = {
15
14
  screenshotItemAttributes: ScreenshotItemAttribute[]
16
15
  }
17
16
 
18
- const tmpDir = 'screenright/tmp'
19
-
20
17
  const result: Result = { screenshotItemAttributes: [] }
21
18
 
22
19
  let deploymentId: string | null = null
@@ -63,17 +60,10 @@ export const finalize = async () => {
63
60
  return
64
61
  }
65
62
 
66
- fs.writeFileSync(
67
- `${tmpDir}/result.json`,
68
- JSON.stringify({
69
- screenshotItemAttributes: result.screenshotItemAttributes,
70
- })
71
- )
72
-
73
63
  const diagramId = process.env.SCREENRIGHT_DIAGRAM_ID
74
64
  await fetch(`${baseUrl()}/diagrams/${diagramId}/deployments/${deploymentId}/done_upload`, {
75
65
  method: 'PUT',
76
- body: JSON.stringify({ deployment_token: deploymentToken, blueprint: { screenshotItemAttributes: result.screenshotItemAttributes } }),
66
+ body: JSON.stringify({ deployment_token: deploymentToken, blueprint: JSON.stringify({ screenshotItemAttributes: result.screenshotItemAttributes}) }),
77
67
  headers: { 'Content-Type': 'application/json' }
78
68
  })
79
69
 
@@ -90,21 +80,19 @@ export const capture = async (
90
80
  return
91
81
  }
92
82
 
93
- const fileName = `${key}.png`
94
- const outPath = `${tmpDir}/${fileName}`
83
+ const fileName = `${key}.jpg`
95
84
  try {
96
- fs.mkdirSync(tmpDir, { recursive: true })
97
- const buffer = new Uint8Array(await page.screenshot({ path: outPath, fullPage: true }))
85
+ const buffer = await page.screenshot({ fullPage: true, type: 'jpeg' })
98
86
  const formData = new FormData()
99
87
 
100
- formData.append('file', fs.createReadStream(outPath))
88
+ formData.append('file', buffer, fileName)
101
89
 
102
90
  const diagramId = process.env.SCREENRIGHT_DIAGRAM_ID
103
91
  const response = await fetch(`${baseUrl()}/diagrams/${diagramId}/deployments/${deploymentId}/screenshot`, {
104
92
  method: 'POST',
105
93
  headers: {
106
94
  'X-File-Key': key,
107
- 'X-Deployment-Token': deploymentToken
95
+ 'X-Deployment-Token': deploymentToken,
108
96
  },
109
97
  body: formData
110
98
  })
@@ -114,14 +102,13 @@ export const capture = async (
114
102
  return
115
103
  }
116
104
  } catch(e: any) {
117
- errorOccurred(`capture: ${key}, ${outPath}, ${e.message}`)
105
+ errorOccurred(`capture: ${key}, ${e.message}`)
118
106
  return
119
107
  }
120
108
 
121
109
  const attribute: ScreenshotItemAttribute = {
122
110
  key,
123
111
  title,
124
- src: outPath,
125
112
  childrens: [],
126
113
  }
127
114