screenright-client 0.9.1 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.js +39 -15
- package/package.json +1 -1
- package/src/index.ts +8 -19
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
|
-
|
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
|
-
|
22
|
-
|
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
|
-
|
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,33 @@ export const capture = (page, key, title, parentKey) => __awaiter(void 0, void 0
|
|
55
59
|
if (deploymentId === null) {
|
56
60
|
return;
|
57
61
|
}
|
58
|
-
|
59
|
-
|
60
|
-
|
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
|
-
|
88
|
+
url: page.url(),
|
65
89
|
childrens: [],
|
66
90
|
};
|
67
91
|
if (parentKey) {
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
@@ -7,7 +7,7 @@ import FormData from 'form-data'
|
|
7
7
|
type ScreenshotItemAttribute = {
|
8
8
|
key: string
|
9
9
|
title: string
|
10
|
-
|
10
|
+
url: string
|
11
11
|
childrens: ScreenshotItemAttribute[]
|
12
12
|
}
|
13
13
|
|
@@ -15,8 +15,6 @@ type Result = {
|
|
15
15
|
screenshotItemAttributes: ScreenshotItemAttribute[]
|
16
16
|
}
|
17
17
|
|
18
|
-
const tmpDir = 'screenright/tmp'
|
19
|
-
|
20
18
|
const result: Result = { screenshotItemAttributes: [] }
|
21
19
|
|
22
20
|
let deploymentId: string | null = null
|
@@ -63,17 +61,10 @@ export const finalize = async () => {
|
|
63
61
|
return
|
64
62
|
}
|
65
63
|
|
66
|
-
fs.writeFileSync(
|
67
|
-
`${tmpDir}/result.json`,
|
68
|
-
JSON.stringify({
|
69
|
-
screenshotItemAttributes: result.screenshotItemAttributes,
|
70
|
-
})
|
71
|
-
)
|
72
|
-
|
73
64
|
const diagramId = process.env.SCREENRIGHT_DIAGRAM_ID
|
74
65
|
await fetch(`${baseUrl()}/diagrams/${diagramId}/deployments/${deploymentId}/done_upload`, {
|
75
66
|
method: 'PUT',
|
76
|
-
body: JSON.stringify({ deployment_token: deploymentToken, blueprint: { screenshotItemAttributes: result.screenshotItemAttributes
|
67
|
+
body: JSON.stringify({ deployment_token: deploymentToken, blueprint: JSON.stringify({ screenshotItemAttributes: result.screenshotItemAttributes}) }),
|
77
68
|
headers: { 'Content-Type': 'application/json' }
|
78
69
|
})
|
79
70
|
|
@@ -90,21 +81,19 @@ export const capture = async (
|
|
90
81
|
return
|
91
82
|
}
|
92
83
|
|
93
|
-
const fileName = `${key}.
|
94
|
-
const outPath = `${tmpDir}/${fileName}`
|
84
|
+
const fileName = `${key}.jpg`
|
95
85
|
try {
|
96
|
-
|
97
|
-
const buffer = new Uint8Array(await page.screenshot({ path: outPath, fullPage: true }))
|
86
|
+
const buffer = await page.screenshot({ fullPage: true, type: 'jpeg' })
|
98
87
|
const formData = new FormData()
|
99
88
|
|
100
|
-
formData.append('file',
|
89
|
+
formData.append('file', buffer, fileName)
|
101
90
|
|
102
91
|
const diagramId = process.env.SCREENRIGHT_DIAGRAM_ID
|
103
92
|
const response = await fetch(`${baseUrl()}/diagrams/${diagramId}/deployments/${deploymentId}/screenshot`, {
|
104
93
|
method: 'POST',
|
105
94
|
headers: {
|
106
95
|
'X-File-Key': key,
|
107
|
-
'X-Deployment-Token': deploymentToken
|
96
|
+
'X-Deployment-Token': deploymentToken,
|
108
97
|
},
|
109
98
|
body: formData
|
110
99
|
})
|
@@ -114,14 +103,14 @@ export const capture = async (
|
|
114
103
|
return
|
115
104
|
}
|
116
105
|
} catch(e: any) {
|
117
|
-
errorOccurred(`capture: ${key}, ${
|
106
|
+
errorOccurred(`capture: ${key}, ${e.message}`)
|
118
107
|
return
|
119
108
|
}
|
120
109
|
|
121
110
|
const attribute: ScreenshotItemAttribute = {
|
122
111
|
key,
|
123
112
|
title,
|
124
|
-
|
113
|
+
url: page.url(),
|
125
114
|
childrens: [],
|
126
115
|
}
|
127
116
|
|