File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2,7 +2,6 @@ export type { Config } from "./src/config.ts";
22export { config } from "./src/config.ts" ;
33
44export {
5- ImageApiError ,
65 InvalidArgumentError ,
76 InvalidTimeoutError ,
87 MissingApiKeyError ,
Original file line number Diff line number Diff line change @@ -21,14 +21,6 @@ export class InvalidTimeoutError extends Error {
2121 }
2222}
2323
24- export class ImageApiError extends Error {
25- constructor ( message : string ) {
26- super ( message ) ;
27- this . name = "ImageApiError" ;
28- Object . setPrototypeOf ( this , ImageApiError . prototype ) ;
29- }
30- }
31-
3224export class RequestTimeoutError extends Error {
3325 constructor ( ) {
3426 super ( "The request was timed out" ) ;
Original file line number Diff line number Diff line change 1- import { ImageApiError , InvalidArgumentError } from "./errors.ts" ;
1+ import { InvalidArgumentError } from "./errors.ts" ;
22import { readFile } from "node:fs" ;
33import type {
44 AccountApiParameters ,
@@ -348,6 +348,8 @@ export async function uploadImage(
348348 parameters : ImageApiParameters ,
349349 callback ?: ( result : ImageApiResponse ) => void ,
350350) : Promise < ImageApiResponse > {
351+ if ( ! parameters ?. image ) throw new InvalidArgumentError ( ) ;
352+
351353 const key = validateApiKey ( parameters . api_key ) ;
352354 const timeout = validateTimeout ( parameters . timeout ) ;
353355 let image : Uint8Array | ArrayBuffer ;
@@ -362,23 +364,14 @@ export async function uploadImage(
362364 } else {
363365 image = parameters . image ;
364366 }
365- let response : string ;
366- try {
367- response = await _internals . uploadImage (
368- image ,
369- {
370- api_key : key ,
371- requestOptions : parameters . requestOptions ,
372- } ,
373- timeout ,
374- ) ;
375- } catch ( error ) {
376- let message = "Image upload failed" ;
377- try {
378- message = JSON . parse ( String ( error ) ) . error || message ;
379- } catch { /* */ }
380- throw new ImageApiError ( message ) ;
381- }
367+ const response = await _internals . uploadImage (
368+ image ,
369+ {
370+ api_key : key ,
371+ requestOptions : parameters . requestOptions ,
372+ } ,
373+ timeout ,
374+ ) ;
382375 const result = JSON . parse ( response ) as ImageApiResponse ;
383376 callback ?.( result ) ;
384377 return result ;
Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ export type ImageApiParameters = {
2929} ;
3030
3131export type ImageApiResponse = {
32- message : string ;
33- image_id : string ;
32+ message ?: string ;
33+ image_id ?: string ;
34+ error ?: string ;
3435} ;
Original file line number Diff line number Diff line change @@ -32,7 +32,6 @@ import {
3232 getJson ,
3333 getJsonBySearchId ,
3434 getLocations ,
35- ImageApiError ,
3635 InvalidArgumentError ,
3736 InvalidTimeoutError ,
3837 MissingApiKeyError ,
@@ -226,6 +225,14 @@ describe("uploadImage", () => {
226225 ) ;
227226 } ) ;
228227
228+ it ( "with no image" , ( ) => {
229+ assertRejects (
230+ // @ts -expect-error Test runtime validation for JavaScript callers.
231+ async ( ) => await uploadImage ( { api_key : "test_api_key" } ) ,
232+ InvalidArgumentError ,
233+ ) ;
234+ } ) ;
235+
229236 it ( "accepts image bytes" , async ( ) => {
230237 await assertImageUpload ( image ) ;
231238 } ) ;
@@ -240,19 +247,17 @@ describe("uploadImage", () => {
240247 }
241248 } ) ;
242249
243- it ( "throws ImageApiError" , async ( ) => {
250+ it ( "rejects upon error response" , async ( ) => {
251+ const apiError = '{"error":"Invalid image"}' ;
244252 const executeStub = stub (
245253 _internals ,
246254 "uploadImage" ,
247- ( ) => Promise . reject ( '{"error":"Invalid image"}' ) ,
255+ ( ) => Promise . reject ( apiError ) ,
248256 ) ;
249257 config . api_key = "test_api_key" ;
250258 try {
251- await assertRejects (
252- async ( ) => await uploadImage ( { image } ) ,
253- ImageApiError ,
254- "Invalid image" ,
255- ) ;
259+ const error = await uploadImage ( { image } ) . catch ( ( error ) => error ) ;
260+ assertEquals ( error , apiError ) ;
256261 } finally {
257262 executeStub . restore ( ) ;
258263 }
You can’t perform that action at this time.
0 commit comments