screenright-client 0.9.0 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/dist/index.js +38 -15
  2. package/package.json +1 -1
  3. package/src/index.ts +43 -18
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.0",
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
@@ -2,11 +2,11 @@ import * as fs from 'fs'
2
2
  import { Page } from '@playwright/test'
3
3
  import fetch from 'node-fetch'
4
4
  import process from 'node:process'
5
+ import FormData from 'form-data'
5
6
 
6
7
  type ScreenshotItemAttribute = {
7
8
  key: string
8
9
  title: string
9
- src: string
10
10
  childrens: ScreenshotItemAttribute[]
11
11
  }
12
12
 
@@ -14,20 +14,28 @@ type Result = {
14
14
  screenshotItemAttributes: ScreenshotItemAttribute[]
15
15
  }
16
16
 
17
- const tmpDir = 'screenright/tmp'
18
-
19
17
  const result: Result = { screenshotItemAttributes: [] }
20
18
 
21
19
  let deploymentId: string | null = null
20
+ const deploymentToken: string = process.env.SCREENRIGHT_DEPLOYMENT_TOKEN || ''
22
21
 
23
22
  const baseUrl = () => {
24
23
  return `${process.env.SCREENRIGHT_ENDPOINT}/client_api`
25
24
  }
26
25
 
26
+ const errorOccurred = (message: string) => {
27
+ console.error('[ScreenRight] Error occurred', message)
28
+ deploymentId = null
29
+ }
30
+
27
31
  export const initializeScreenwright = async () => {
28
32
 
29
33
  const diagramId = process.env.SCREENRIGHT_DIAGRAM_ID
30
- const deploymentToken = process.env.SCREENRIGHT_DEPLOYMENT_TOKEN
34
+ if (!diagramId || !deploymentToken) {
35
+ errorOccurred('Not set require environments.')
36
+ return
37
+ }
38
+
31
39
  try {
32
40
  const response = await fetch(`${baseUrl()}/diagrams/${diagramId}/deployments`, {
33
41
  method: 'POST',
@@ -35,11 +43,15 @@ export const initializeScreenwright = async () => {
35
43
  headers: { 'Content-Type': 'application/json' }
36
44
  })
37
45
 
46
+ if (!response.ok) {
47
+ errorOccurred('Failed create deployment.')
48
+ }
49
+
38
50
  const body = await response.text()
39
51
  const json = JSON.parse(body)
40
52
  deploymentId = json.id
41
53
  } catch(e: any) {
42
- console.error('[ScreenRight] Error catch', e.message)
54
+ errorOccurred(e.message)
43
55
  }
44
56
  }
45
57
 
@@ -48,18 +60,10 @@ export const finalize = async () => {
48
60
  return
49
61
  }
50
62
 
51
- fs.writeFileSync(
52
- `${tmpDir}/result.json`,
53
- JSON.stringify({
54
- screenshotItemAttributes: result.screenshotItemAttributes,
55
- })
56
- )
57
-
58
63
  const diagramId = process.env.SCREENRIGHT_DIAGRAM_ID
59
- const deploymentToken = process.env.SCREENRIGHT_DEPLOYMENT_TOKEN
60
64
  await fetch(`${baseUrl()}/diagrams/${diagramId}/deployments/${deploymentId}/done_upload`, {
61
65
  method: 'PUT',
62
- body: JSON.stringify({ deployment_token: deploymentToken, screenshotItemAttributes: result.screenshotItemAttributes }),
66
+ body: JSON.stringify({ deployment_token: deploymentToken, blueprint: JSON.stringify({ screenshotItemAttributes: result.screenshotItemAttributes}) }),
63
67
  headers: { 'Content-Type': 'application/json' }
64
68
  })
65
69
 
@@ -76,14 +80,35 @@ export const capture = async (
76
80
  return
77
81
  }
78
82
 
79
- fs.mkdirSync(tmpDir, { recursive: true })
80
- const path = `${tmpDir}/${key}.png`
81
- await page.screenshot({ path, fullPage: true })
83
+ const fileName = `${key}.jpg`
84
+ try {
85
+ const buffer = await page.screenshot({ fullPage: true, type: 'jpeg' })
86
+ const formData = new FormData()
87
+
88
+ formData.append('file', buffer, fileName)
89
+
90
+ const diagramId = process.env.SCREENRIGHT_DIAGRAM_ID
91
+ const response = await fetch(`${baseUrl()}/diagrams/${diagramId}/deployments/${deploymentId}/screenshot`, {
92
+ method: 'POST',
93
+ headers: {
94
+ 'X-File-Key': key,
95
+ 'X-Deployment-Token': deploymentToken,
96
+ },
97
+ body: formData
98
+ })
99
+
100
+ if (!response.ok) {
101
+ errorOccurred('Faild screenshot upload')
102
+ return
103
+ }
104
+ } catch(e: any) {
105
+ errorOccurred(`capture: ${key}, ${e.message}`)
106
+ return
107
+ }
82
108
 
83
109
  const attribute: ScreenshotItemAttribute = {
84
110
  key,
85
111
  title,
86
- src: path,
87
112
  childrens: [],
88
113
  }
89
114