UNPKG

@devicecloud.dev/dcd

Version:

Better cloud maestro testing

150 lines (149 loc) 5.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiGateway = void 0; const fs = require("node:fs/promises"); const path = require("node:path"); exports.ApiGateway = { async checkForExistingUpload(baseUrl, apiKey, sha) { const res = await fetch(`${baseUrl}/uploads/checkForExistingUpload`, { body: JSON.stringify({ sha }), headers: { 'content-type': 'application/json', 'x-app-api-key': apiKey, }, method: 'POST', }); return res.json(); }, async downloadReport(baseUrl, apiKey, uploadId, reportPath) { const finalReportPath = reportPath || path.resolve(process.cwd(), `report-${uploadId}.xml`); const url = `${baseUrl}/results/${uploadId}/report`; const res = await fetch(url, { headers: { 'x-app-api-key': apiKey, }, method: 'GET', }); if (!res.ok) { const errorText = await res.text(); if (res.status === 404) { throw new Error(`Upload ID '${uploadId}' not found or no results available for this upload`); } throw new Error(`Failed to download report: ${res.status} ${errorText}`); } const buffer = await res.arrayBuffer(); await fs.writeFile(finalReportPath, Buffer.from(buffer)); }, async downloadArtifactsZip(baseUrl, apiKey, uploadId, results, artifactsPath = './artifacts.zip') { const res = await fetch(`${baseUrl}/results/${uploadId}/download`, { body: JSON.stringify({ results }), headers: { 'content-type': 'application/json', 'x-app-api-key': apiKey, }, method: 'POST', }); if (!res.ok) { throw new Error(await res.text()); } // Handle tilde expansion for home directory if (artifactsPath.startsWith('~/') || artifactsPath === '~') { artifactsPath = artifactsPath.replace(/^~(?=$|\/|\\)/, // eslint-disable-next-line unicorn/prefer-module require('node:os').homedir()); } // Create directory structure if it doesn't exist // eslint-disable-next-line unicorn/prefer-module const { dirname } = require('node:path'); // eslint-disable-next-line unicorn/prefer-module const { createWriteStream, mkdirSync } = require('node:fs'); // eslint-disable-next-line unicorn/prefer-module const { finished } = require('node:stream/promises'); // eslint-disable-next-line unicorn/prefer-module const { Readable } = require('node:stream'); const directory = dirname(artifactsPath); if (directory !== '.') { try { mkdirSync(directory, { recursive: true }); } catch (error) { // Ignore if directory already exists if (error.code !== 'EEXIST') { throw error; } } } const fileStream = createWriteStream(artifactsPath, { flags: 'wx' }); await finished(Readable.fromWeb(res.body).pipe(fileStream)); }, // eslint-disable-next-line max-params async finaliseUpload(baseUrl, apiKey, id, metadata, path, sha) { const res = await fetch(`${baseUrl}/uploads/finaliseUpload`, { body: JSON.stringify({ id, metadata, path, sha }), headers: { 'content-type': 'application/json', 'x-app-api-key': apiKey, }, method: 'POST', }); if (!res.ok) { throw new Error(await res.text()); } return res.json(); }, async getBinaryUploadUrl(baseUrl, apiKey, platform) { const res = await fetch(`${baseUrl}/uploads/getBinaryUploadUrl`, { body: JSON.stringify({ platform }), headers: { 'content-type': 'application/json', 'x-app-api-key': apiKey, }, method: 'POST', }); if (!res.ok) { throw new Error(await res.text()); } return res.json(); }, async getResultsForUpload(baseUrl, apiKey, uploadId) { // TODO: merge with getUploadStatus const res = await fetch(`${baseUrl}/results/${uploadId}`, { headers: { 'x-app-api-key': apiKey }, }); if (!res.ok) { throw new Error(await res.text()); } return res.json(); }, async getUploadStatus(baseUrl, apiKey, options) { const queryParams = new URLSearchParams(); if (options.uploadId) { queryParams.append('uploadId', options.uploadId); } if (options.name) { queryParams.append('name', options.name); } const response = await fetch(`${baseUrl}/uploads/status?${queryParams}`, { headers: { 'x-app-api-key': apiKey, }, }); if (!response.ok) { throw new Error(`Failed to fetch status: ${response.statusText}`); } return response.json(); }, async uploadFlow(baseUrl, apiKey, testFormData) { const res = await fetch(`${baseUrl}/uploads/flow`, { body: testFormData, headers: { 'x-app-api-key': apiKey, }, method: 'POST', }); if (!res.ok) { throw new Error(await res.text()); } return res.json(); }, };