11import fetch from 'node-fetch'
22
3- const baseURL = 'http://127.0.0.1:5000/api/size?package='
3+ import type { PackageBuildInfo } from '../types/package-domain'
4+ import { createPackageApiPath } from '../utils/packageApi.utils'
45
5- type PackageSizeResponse = {
6+ const packageSizeUrl = ( packageString : string ) =>
7+ `http://127.0.0.1:5000${ createPackageApiPath ( 'size' , packageString ) } `
8+
9+ type PackageSizeResponse = PackageBuildInfo & {
610 scoped : boolean
7- name : string
8- version : string
9- description : string
10- repository : string
11- dependencyCount : number
12- hasJSNext : boolean
13- hasJSModule : boolean
14- hasSideEffects : boolean
15- size : number
16- gzip : number
17- dependencySizes : Array < { name : string ; approximateSize : number } >
18- ignoredMissingDependencies ?: string [ ]
1911}
2012
2113type ErrorResponse = {
@@ -29,7 +21,7 @@ jest.setTimeout(15000)
2921
3022describe ( 'build api' , ( ) => {
3123 it ( 'builds correct packages' , async ( ) => {
32- const resultURL = baseURL + 'react@16.5.0'
24+ const resultURL = packageSizeUrl ( 'react@16.5.0' )
3325 const result = await fetch ( resultURL )
3426 const resultJSON : PackageSizeResponse = await result . json ( )
3527
@@ -54,7 +46,7 @@ describe('build api', () => {
5446 } )
5547
5648 it ( 'handles hash bang in the beginning of packages' , async ( ) => {
57- const resultURL = baseURL + '@bundlephobia/test-build-error'
49+ const resultURL = packageSizeUrl ( '@bundlephobia/test-build-error' )
5850 const result = await fetch ( resultURL )
5951 const resultJSON : PackageSizeResponse = await result . json ( )
6052
@@ -65,7 +57,7 @@ describe('build api', () => {
6557 } )
6658
6759 it ( 'gives right error messages on when trying to build blocklisted packages' , async ( ) => {
68- const resultURL = baseURL + 'polymer-cli'
60+ const resultURL = packageSizeUrl ( 'polymer-cli' )
6961 const result = await fetch ( resultURL )
7062 const errorJSON : ErrorResponse = await result . json ( )
7163
@@ -80,7 +72,7 @@ describe('build api', () => {
8072 } )
8173
8274 it ( 'gives right error messages on when trying to build entry point error ' , async ( ) => {
83- const resultURL = baseURL + '@bundlephobia/test-entry-point-error'
75+ const resultURL = packageSizeUrl ( '@bundlephobia/test-entry-point-error' )
8476 const result = await fetch ( resultURL )
8577 const errorJSON : ErrorResponse = await result . json ( )
8678
@@ -93,7 +85,7 @@ describe('build api', () => {
9385 } )
9486
9587 it ( 'ignores errors when trying to build packages with missing dependency errors' , async ( ) => {
96- const resultURL = baseURL + '@bundlephobia/missing-dependency-error'
88+ const resultURL = packageSizeUrl ( '@bundlephobia/missing-dependency-error' )
9789 const result = await fetch ( resultURL )
9890 const resultJSON : PackageSizeResponse = await result . json ( )
9991
@@ -107,7 +99,7 @@ describe('build api', () => {
10799 } )
108100
109101 it ( "gives right error messages on when trying to build packages that don't exist" , async ( ) => {
110- const resultURL = baseURL + '@bundlephobia/does-not-exist'
102+ const resultURL = packageSizeUrl ( '@bundlephobia/does-not-exist' )
111103 const result = await fetch ( resultURL )
112104 const errorJSON : ErrorResponse = await result . json ( )
113105
@@ -120,7 +112,9 @@ describe('build api', () => {
120112 } )
121113
122114 it ( "gives right error messages on when trying to build packages versions that don't exist" , async ( ) => {
123- const resultURL = baseURL + '@bundlephobia/test-entry-point-error@459.0.0'
115+ const resultURL = packageSizeUrl (
116+ '@bundlephobia/test-entry-point-error@459.0.0'
117+ )
124118 const result = await fetch ( resultURL )
125119 const errorJSON : ErrorResponse = await result . json ( )
126120
0 commit comments