| 
									
										
										
										
											2025-01-13 23:18:10 +02:00
										 |  |  | import type child_process from "child_process"; | 
					
						
							| 
									
										
										
										
											2025-02-14 08:01:30 +01:00
										 |  |  | import { describe, beforeAll, afterAll } from "vitest"; | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | let etapiAuthToken: string | undefined; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  | const getEtapiAuthorizationHeader = (): string => "Basic " + Buffer.from(`etapi:${etapiAuthToken}`).toString("base64"); | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | const PORT: string = "9999"; | 
					
						
							|  |  |  | const HOST: string = "http://localhost:" + PORT; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type SpecDefinitionsFunc = () => void; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  | function describeEtapi(description: string, specDefinitions: SpecDefinitionsFunc): void { | 
					
						
							|  |  |  |     describe(description, () => { | 
					
						
							|  |  |  |         let appProcess: ReturnType<typeof child_process.spawn>; | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         beforeAll(async () => {}); | 
					
						
							| 
									
										
										
										
											2024-12-22 15:45:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         afterAll(() => {}); | 
					
						
							| 
									
										
										
										
											2024-12-22 15:45:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |         specDefinitions(); | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getEtapiResponse(url: string): Promise<Response> { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     return await fetch(`${HOST}/etapi/${url}`, { | 
					
						
							|  |  |  |         method: "GET", | 
					
						
							|  |  |  |         headers: { | 
					
						
							|  |  |  |             Authorization: getEtapiAuthorizationHeader() | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getEtapi(url: string): Promise<any> { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     const response = await getEtapiResponse(url); | 
					
						
							|  |  |  |     return await processEtapiResponse(response); | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getEtapiContent(url: string): Promise<Response> { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     const response = await fetch(`${HOST}/etapi/${url}`, { | 
					
						
							|  |  |  |         method: "GET", | 
					
						
							|  |  |  |         headers: { | 
					
						
							|  |  |  |             Authorization: getEtapiAuthorizationHeader() | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     checkStatus(response); | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     return response; | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  | async function postEtapi(url: string, data: Record<string, unknown> = {}): Promise<any> { | 
					
						
							|  |  |  |     const response = await fetch(`${HOST}/etapi/${url}`, { | 
					
						
							|  |  |  |         method: "POST", | 
					
						
							|  |  |  |         headers: { | 
					
						
							|  |  |  |             "Content-Type": "application/json", | 
					
						
							|  |  |  |             Authorization: getEtapiAuthorizationHeader() | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         body: JSON.stringify(data) | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return await processEtapiResponse(response); | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  | async function postEtapiContent(url: string, data: BodyInit): Promise<Response> { | 
					
						
							|  |  |  |     const response = await fetch(`${HOST}/etapi/${url}`, { | 
					
						
							|  |  |  |         method: "POST", | 
					
						
							|  |  |  |         headers: { | 
					
						
							|  |  |  |             "Content-Type": "application/octet-stream", | 
					
						
							|  |  |  |             Authorization: getEtapiAuthorizationHeader() | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         body: data | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     checkStatus(response); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return response; | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  | async function putEtapi(url: string, data: Record<string, unknown> = {}): Promise<any> { | 
					
						
							|  |  |  |     const response = await fetch(`${HOST}/etapi/${url}`, { | 
					
						
							|  |  |  |         method: "PUT", | 
					
						
							|  |  |  |         headers: { | 
					
						
							|  |  |  |             "Content-Type": "application/json", | 
					
						
							|  |  |  |             Authorization: getEtapiAuthorizationHeader() | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         body: JSON.stringify(data) | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return await processEtapiResponse(response); | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  | async function putEtapiContent(url: string, data?: BodyInit): Promise<Response> { | 
					
						
							|  |  |  |     const response = await fetch(`${HOST}/etapi/${url}`, { | 
					
						
							|  |  |  |         method: "PUT", | 
					
						
							|  |  |  |         headers: { | 
					
						
							|  |  |  |             "Content-Type": "application/octet-stream", | 
					
						
							|  |  |  |             Authorization: getEtapiAuthorizationHeader() | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         body: data | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     checkStatus(response); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return response; | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  | async function patchEtapi(url: string, data: Record<string, unknown> = {}): Promise<any> { | 
					
						
							|  |  |  |     const response = await fetch(`${HOST}/etapi/${url}`, { | 
					
						
							|  |  |  |         method: "PATCH", | 
					
						
							|  |  |  |         headers: { | 
					
						
							|  |  |  |             "Content-Type": "application/json", | 
					
						
							|  |  |  |             Authorization: getEtapiAuthorizationHeader() | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         body: JSON.stringify(data) | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return await processEtapiResponse(response); | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function deleteEtapi(url: string): Promise<any> { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     const response = await fetch(`${HOST}/etapi/${url}`, { | 
					
						
							|  |  |  |         method: "DELETE", | 
					
						
							|  |  |  |         headers: { | 
					
						
							|  |  |  |             Authorization: getEtapiAuthorizationHeader() | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return await processEtapiResponse(response); | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function processEtapiResponse(response: Response): Promise<any> { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     const text = await response.text(); | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     if (response.status < 200 || response.status >= 300) { | 
					
						
							|  |  |  |         throw new Error(`ETAPI error ${response.status}: ${text}`); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     return text?.trim() ? JSON.parse(text) : null; | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function checkStatus(response: Response): void { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     if (response.status < 200 || response.status >= 300) { | 
					
						
							|  |  |  |         throw new Error(`ETAPI error ${response.status}`); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-18 21:56:20 +03:00
										 |  |  | export default { | 
					
						
							| 
									
										
										
										
											2025-01-09 18:07:02 +02:00
										 |  |  |     describeEtapi, | 
					
						
							|  |  |  |     getEtapi, | 
					
						
							|  |  |  |     getEtapiResponse, | 
					
						
							|  |  |  |     getEtapiContent, | 
					
						
							|  |  |  |     postEtapi, | 
					
						
							|  |  |  |     postEtapiContent, | 
					
						
							|  |  |  |     putEtapi, | 
					
						
							|  |  |  |     putEtapiContent, | 
					
						
							|  |  |  |     patchEtapi, | 
					
						
							|  |  |  |     deleteEtapi | 
					
						
							| 
									
										
										
										
											2024-05-03 21:18:20 +02:00
										 |  |  | }; |