screenright-client 0.9.0 → 0.9.1

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +46 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "screenright-client",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "screenright's nodejs client library",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ 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
@@ -19,15 +20,25 @@ const tmpDir = 'screenright/tmp'
19
20
  const result: Result = { screenshotItemAttributes: [] }
20
21
 
21
22
  let deploymentId: string | null = null
23
+ const deploymentToken: string = process.env.SCREENRIGHT_DEPLOYMENT_TOKEN || ''
22
24
 
23
25
  const baseUrl = () => {
24
26
  return `${process.env.SCREENRIGHT_ENDPOINT}/client_api`
25
27
  }
26
28
 
29
+ const errorOccurred = (message: string) => {
30
+ console.error('[ScreenRight] Error occurred', message)
31
+ deploymentId = null
32
+ }
33
+
27
34
  export const initializeScreenwright = async () => {
28
35
 
29
36
  const diagramId = process.env.SCREENRIGHT_DIAGRAM_ID
30
- const deploymentToken = process.env.SCREENRIGHT_DEPLOYMENT_TOKEN
37
+ if (!diagramId || !deploymentToken) {
38
+ errorOccurred('Not set require environments.')
39
+ return
40
+ }
41
+
31
42
  try {
32
43
  const response = await fetch(`${baseUrl()}/diagrams/${diagramId}/deployments`, {
33
44
  method: 'POST',
@@ -35,11 +46,15 @@ export const initializeScreenwright = async () => {
35
46
  headers: { 'Content-Type': 'application/json' }
36
47
  })
37
48
 
49
+ if (!response.ok) {
50
+ errorOccurred('Failed create deployment.')
51
+ }
52
+
38
53
  const body = await response.text()
39
54
  const json = JSON.parse(body)
40
55
  deploymentId = json.id
41
56
  } catch(e: any) {
42
- console.error('[ScreenRight] Error catch', e.message)
57
+ errorOccurred(e.message)
43
58
  }
44
59
  }
45
60
 
@@ -56,10 +71,9 @@ export const finalize = async () => {
56
71
  )
57
72
 
58
73
  const diagramId = process.env.SCREENRIGHT_DIAGRAM_ID
59
- const deploymentToken = process.env.SCREENRIGHT_DEPLOYMENT_TOKEN
60
74
  await fetch(`${baseUrl()}/diagrams/${diagramId}/deployments/${deploymentId}/done_upload`, {
61
75
  method: 'PUT',
62
- body: JSON.stringify({ deployment_token: deploymentToken, screenshotItemAttributes: result.screenshotItemAttributes }),
76
+ body: JSON.stringify({ deployment_token: deploymentToken, blueprint: { screenshotItemAttributes: result.screenshotItemAttributes } }),
63
77
  headers: { 'Content-Type': 'application/json' }
64
78
  })
65
79
 
@@ -76,14 +90,38 @@ export const capture = async (
76
90
  return
77
91
  }
78
92
 
79
- fs.mkdirSync(tmpDir, { recursive: true })
80
- const path = `${tmpDir}/${key}.png`
81
- await page.screenshot({ path, fullPage: true })
93
+ const fileName = `${key}.png`
94
+ const outPath = `${tmpDir}/${fileName}`
95
+ try {
96
+ fs.mkdirSync(tmpDir, { recursive: true })
97
+ const buffer = new Uint8Array(await page.screenshot({ path: outPath, fullPage: true }))
98
+ const formData = new FormData()
99
+
100
+ formData.append('file', fs.createReadStream(outPath))
101
+
102
+ const diagramId = process.env.SCREENRIGHT_DIAGRAM_ID
103
+ const response = await fetch(`${baseUrl()}/diagrams/${diagramId}/deployments/${deploymentId}/screenshot`, {
104
+ method: 'POST',
105
+ headers: {
106
+ 'X-File-Key': key,
107
+ 'X-Deployment-Token': deploymentToken
108
+ },
109
+ body: formData
110
+ })
111
+
112
+ if (!response.ok) {
113
+ errorOccurred('Faild screenshot upload')
114
+ return
115
+ }
116
+ } catch(e: any) {
117
+ errorOccurred(`capture: ${key}, ${outPath}, ${e.message}`)
118
+ return
119
+ }
82
120
 
83
121
  const attribute: ScreenshotItemAttribute = {
84
122
  key,
85
123
  title,
86
- src: path,
124
+ src: outPath,
87
125
  childrens: [],
88
126
  }
89
127