@@ -10,10 +10,11 @@ import { loadConfig } from './transform/config'
1010import { FLYTRAP_API_BASE , setFlytrapConfig } from './core/config'
1111import { log } from './core/logging'
1212import { Artifact , extractArtifacts } from './transform/artifacts'
13- import { post } from './core/util'
13+ import { tryCatch } from './core/util'
1414import { readFileSync } from 'node:fs'
1515import { excludeDirectoriesIncludeFilePath } from './transform/excludes'
1616import { containsScriptTags , parseScriptTags } from './transform/parseScriptTags'
17+ import { batchedArtifactsUpload } from './transform/batchedArtifactsUpload'
1718
1819const transformedFiles : string [ ] = [ ]
1920
@@ -141,7 +142,23 @@ export const unpluginOptions: UnpluginOptions = {
141142 } ,
142143 async buildEnd ( ) {
143144 const config = await loadConfig ( )
144- if ( ! config ) return
145+ if ( ! config ) {
146+ const log = createHumanLog ( {
147+ event : 'transform_failed' ,
148+ explanation : 'config_not_found' ,
149+ solution : 'define_flytrap_config'
150+ } )
151+ throw log . toString ( )
152+ }
153+
154+ if ( ! config . projectId || ! config . secretApiKey ) {
155+ const log = createHumanLog ( {
156+ event : 'transform_failed' ,
157+ explanation : 'invalid_config' ,
158+ solution : 'configuration_fix'
159+ } )
160+ throw log . toString ( )
161+ }
145162 // Find package root
146163 const pkgDirPath = packageDirectorySync ( )
147164 if ( ! pkgDirPath ) {
@@ -185,37 +202,22 @@ export const unpluginOptions: UnpluginOptions = {
185202 'storage' ,
186203 `Created ${ artifacts . length } artifacts. Size: ${ JSON . stringify ( artifacts ) . length } `
187204 )
188- log . info (
189- 'api-calls' ,
190- `Pushing ${ artifacts . length } artifacts to the Flytrap API. Payload size: ${
191- JSON . stringify ( artifacts ) . length
192- } `
193- )
194205
195- const { data, error } = await post (
196- `${ FLYTRAP_API_BASE } /api/v1/artifacts/${ config ?. projectId } ` ,
197- JSON . stringify ( {
198- artifacts
199- } ) ,
200- {
201- headers : new Headers ( {
202- Authorization : `Bearer ${ config ?. secretApiKey } ` ,
203- 'Content-Type' : 'application/json'
204- } )
205- }
206+ const { data : uploadedBatches , error } = await tryCatch (
207+ batchedArtifactsUpload ( artifacts , config . secretApiKey , config . projectId )
206208 )
207209 if ( error ) {
208210 console . error (
209211 `Oops! Something went wrong while pushing artifacts to the Flytrap API. Error:`
210212 )
211213 console . error ( error )
212214 }
213- if ( data ) {
214- log . info (
215- 'api-calls' ,
216- `Successfully pushed ${ artifacts . length } artifacts to the Flytrap API.`
217- )
218- }
215+ log . info (
216+ 'api-calls' ,
217+ `Pushed ${ artifacts . length } artifacts in ${
218+ uploadedBatches ? .length
219+ } batches to the Flytrap API. Payload size: ${ JSON . stringify ( artifacts ) . length } `
220+ )
219221 }
220222 }
221223}
0 commit comments