@@ -4,6 +4,7 @@ import { fileTypeFromBuffer } from "file-type";
44import ky , { HTTPError , type KyInstance , type ResponsePromise } from "ky" ;
55import { IMGProcessingAPIError } from "./api-error.js" ;
66import { ImageObject } from "./image-object.js" ;
7+ import { PaginatedImages } from "./paginated-images.js" ;
78
89export class IMGProcessingClient {
910 protected readonly client : KyInstance ;
@@ -61,7 +62,7 @@ export class IMGProcessingClient {
6162 imageId,
6263 } : IMGProcessingClient . download . Params ) : Promise < Blob > {
6364 try {
64- const response = await this . client . get ( `v1/images/${ imageId } /download` )
65+ const response = await this . client . get ( `v1/images/${ imageId } /download` ) ;
6566 return await response . blob ( ) ;
6667 } catch ( error ) {
6768 if ( error instanceof HTTPError ) {
@@ -74,6 +75,63 @@ export class IMGProcessingClient {
7475 }
7576 }
7677
78+ /**
79+ * Got to the page in the paginated list of images.
80+ * @internal
81+ */
82+ async goToPage <
83+ Format extends ImageObject . SupportedFormat = ImageObject . SupportedFormat ,
84+ > ( {
85+ page,
86+ } : IMGProcessingClient . goToPage . Params ) : Promise < PaginatedImages < Format > > {
87+ const urlWithoutDomain = page . replace (
88+ "https://api.img-processing.com/" ,
89+ "" ,
90+ ) ;
91+ const response = await this . request < PaginatedImages < Format > > ( ( ) =>
92+ this . client . get ( urlWithoutDomain ) ,
93+ ) ;
94+ return new PaginatedImages ( {
95+ ...response ,
96+ apiClient : this ,
97+ } ) ;
98+ }
99+
100+ /**
101+ * Retrieves an image object by its unique identifier.
102+ */
103+ async getImage ( {
104+ imageId,
105+ } : IMGProcessingClient . getImage . Params ) : Promise < ImageObject > {
106+ return this . imageRequest ( ( ) => this . client . get ( `v1/images/${ imageId } ` ) ) ;
107+ }
108+
109+ /**
110+ * Retrieves a list of all the images created by the user.
111+ * The images are returned in descending order of creation date, with the most recent images first
112+ * in the list.
113+ *
114+ * Images are paginated, following the [pagination](https://docs.img-processing/api-reference/pagination) rules.
115+ */
116+ async listImages <
117+ Format extends ImageObject . SupportedFormat = ImageObject . SupportedFormat ,
118+ > ( {
119+ take,
120+ from,
121+ } : IMGProcessingClient . listImages . Params ) : Promise < PaginatedImages < Format > > {
122+ const response = await this . request < PaginatedImages < Format > > ( ( ) =>
123+ this . client . get ( "v1/images" , {
124+ searchParams : {
125+ take : take ?. toString ( ) as never ,
126+ from : from as never ,
127+ } ,
128+ } ) ,
129+ ) ;
130+ return new PaginatedImages ( {
131+ ...response ,
132+ apiClient : this ,
133+ } ) ;
134+ }
77135
78136 /**
79137 * -----------------------------------------
@@ -96,7 +154,6 @@ export class IMGProcessingClient {
96154 ) ;
97155 }
98156
99-
100157 /**
101158 * -----------------------------------------
102159 * Creation
@@ -629,4 +686,29 @@ export declare namespace IMGProcessingClient {
629686 imageId : ImageId ;
630687 } ;
631688 }
689+
690+ export namespace getImage {
691+ export type Params = {
692+ /** The unique identifier of the image to retrieve. */
693+ imageId : ImageId ;
694+ } ;
695+ }
696+
697+ export namespace goToPage {
698+ export type Params = {
699+ /** The url of the page to go to. */
700+ page : string ;
701+ } ;
702+ }
703+
704+ export namespace listImages {
705+ export type Params = {
706+ /** The number of items to return per page. */
707+ take ?: number ;
708+ /** The cursor indicating where to start fetching the next set of results. It corresponds to the ID of the first item on the current page.
709+ * If not provided, the first page of results will be returned.
710+ * */
711+ from ?: string ;
712+ } ;
713+ }
632714}
0 commit comments