1- import _axios , { AxiosInstance , AxiosRequestConfig } from 'axios'
21import type { Filter , FindOptions , Sort , UpdateFilter , Document } from 'mongodb'
32
43// https://github.com/surmon-china/mongodb-data-api/pull/3/files @maxfi
@@ -58,19 +57,17 @@ export type Config = XOR<UrlEndpointConfig, PackEndpointConfig>
5857export class MongoDBDataAPI < InnerDoc = Document > {
5958 #config: Config
6059 #baseParams: BaseParams
61- #axios: AxiosInstance
6260
63- constructor ( config : Config , baseParams ?: BaseParams , axios ?: AxiosInstance ) {
61+ constructor ( config : Config , baseParams ?: BaseParams ) {
6462 if ( ! config . apiKey ) {
6563 throw new Error ( 'Invalid API key!' )
6664 }
6765
6866 this . #config = config
6967 this . #baseParams = baseParams || { }
70- this . #axios = axios || _axios . create ( )
7168 }
7269
73- #newAPI< D > ( params : BaseParams ) {
70+ #newAPI< D > ( params : BaseParams ) {
7471 return new MongoDBDataAPI < D > (
7572 { ...this . #config } ,
7673 {
@@ -81,23 +78,23 @@ export class MongoDBDataAPI<InnerDoc = Document> {
8178 }
8279
8380 /** Select a cluster. */
84- public $cluster ( clusterName : string ) {
81+ public $cluster ( clusterName : string ) {
8582 return this . #newAPI< InnerDoc > ( { dataSource : clusterName } ) as Omit <
8683 MongoDBDataAPI < InnerDoc > ,
8784 '$cluster' | '$collection'
8885 >
8986 }
9087
9188 /** Select a database. */
92- public $database ( database : string ) {
89+ public $database ( database : string ) {
9390 return this . #newAPI< InnerDoc > ( { database } ) as Omit <
9491 MongoDBDataAPI < InnerDoc > ,
9592 '$cluster' | '$database'
9693 >
9794 }
9895
9996 /** Select a collection. */
100- public $collection < Doc = InnerDoc > ( collection : string ) {
97+ public $collection < Doc = InnerDoc > ( collection : string ) {
10198 return this . #newAPI< Doc > ( { collection } ) as Omit <
10299 MongoDBDataAPI < Doc > ,
103100 '$cluster' | '$database' | '$collection'
@@ -108,10 +105,9 @@ export class MongoDBDataAPI<InnerDoc = Document> {
108105 * Execute a API action.
109106 * @link https://www.mongodb.com/docs/atlas/api/data-api-resources/
110107 */
111- public $$action < Result = unknown > (
108+ public $$action < Result = unknown > (
112109 type : string ,
113- params : BaseParams = { } ,
114- axiosConfig ?: AxiosRequestConfig
110+ params : BaseParams = { }
115111 ) : Promise < Result > {
116112 const mergedParams = {
117113 ...this . #baseParams,
@@ -134,36 +130,37 @@ export class MongoDBDataAPI<InnerDoc = Document> {
134130 return `${ endpoint } /action/${ action } `
135131 }
136132
137- return this . #axios( {
138- method : 'post' ,
139- data : JSON . stringify ( mergedParams ) ,
140- url : this . #config. urlEndpoint
133+ return fetch (
134+ this . #config. urlEndpoint
141135 ? getActionUrl ( this . #config. urlEndpoint , type )
142136 : getActionUrl (
143137 getUrlEndpoint ( this . #config. appId ! , this . #config. region , this . #config. cloud ) ,
144138 type
145139 ) ,
146- headers : {
147- 'Content-Type' : 'application/json' ,
148- 'Access-Control-Request-Headers' : '*' ,
149- 'api-key' : this . #config. apiKey
150- } ,
151- ...axiosConfig
152- } )
153- . then ( ( response ) => {
154- return response . data
140+ {
141+ method : 'post' ,
142+ body : JSON . stringify ( mergedParams ) ,
143+ headers : {
144+ 'Content-Type' : 'application/json' ,
145+ 'Access-Control-Request-Headers' : '*' ,
146+ 'api-key' : this . #config. apiKey
147+ }
148+ }
149+ )
150+ . then ( ( response : any ) => {
151+ return response . json ( )
155152 } )
156- . catch ( ( error ) => {
153+ . catch ( ( error : any ) => {
157154 // https://www.mongodb.com/docs/atlas/api/data-api-resources/#error-codes
158- return Promise . reject ( _axios . isAxiosError ( error ) ? error . toJSON ( ) : error )
155+ return Promise . reject ( error )
159156 } )
160157 }
161158
162159 /**
163160 * Find a Single Document.
164161 * @link https://www.mongodb.com/docs/atlas/api/data-api-resources/#find-a-single-document
165162 */
166- public findOne < D = InnerDoc , T = NoInfer < D > > (
163+ public findOne < D = InnerDoc , T = NoInfer < D > > (
167164 params ?: ExtendBaseParams < {
168165 filter ?: Filter < T >
169166 projection ?: Projection
@@ -176,7 +173,7 @@ export class MongoDBDataAPI<InnerDoc = Document> {
176173 * Find Multiple Documents.
177174 * @link https://www.mongodb.com/docs/atlas/api/data-api-resources/#find-multiple-documents
178175 */
179- public find < D = InnerDoc , T = NoInfer < D > > (
176+ public find < D = InnerDoc , T = NoInfer < D > > (
180177 params ?: ExtendBaseParams < {
181178 filter ?: Filter < T >
182179 projection ?: Projection
@@ -192,7 +189,7 @@ export class MongoDBDataAPI<InnerDoc = Document> {
192189 * Insert a Single Document.
193190 * @link https://www.mongodb.com/docs/atlas/api/data-api-resources/#insert-a-single-document
194191 */
195- public insertOne < D = InnerDoc , T = NoInfer < D > > (
192+ public insertOne < D = InnerDoc , T = NoInfer < D > > (
196193 params : ExtendBaseParams < { document : AnyKeys < T > | Document } >
197194 ) {
198195 return this . $$action < { insertedId : string } > ( 'insertOne' , params )
@@ -202,7 +199,7 @@ export class MongoDBDataAPI<InnerDoc = Document> {
202199 * Insert Multiple Documents.
203200 * @link https://www.mongodb.com/docs/atlas/api/data-api-resources/#insert-multiple-documents
204201 */
205- public insertMany < D = InnerDoc , T = NoInfer < D > > (
202+ public insertMany < D = InnerDoc , T = NoInfer < D > > (
206203 params : ExtendBaseParams < { documents : Array < AnyKeys < T > | Document > } >
207204 ) {
208205 return this . $$action < { insertedIds : Array < string > } > ( 'insertMany' , params )
@@ -212,7 +209,7 @@ export class MongoDBDataAPI<InnerDoc = Document> {
212209 * Update a Single Document.
213210 * @link https://www.mongodb.com/docs/atlas/api/data-api-resources/#update-a-single-document
214211 */
215- public updateOne < D = InnerDoc , T = NoInfer < D > > (
212+ public updateOne < D = InnerDoc , T = NoInfer < D > > (
216213 params : ExtendBaseParams < {
217214 filter : Filter < T >
218215 update : UpdateFilter < T >
@@ -230,7 +227,7 @@ export class MongoDBDataAPI<InnerDoc = Document> {
230227 * Update Multiple Documents.
231228 * @link https://www.mongodb.com/docs/atlas/api/data-api-resources/#update-multiple-documents
232229 */
233- public updateMany < D = InnerDoc , T = NoInfer < D > > (
230+ public updateMany < D = InnerDoc , T = NoInfer < D > > (
234231 params : ExtendBaseParams < {
235232 filter : Filter < T >
236233 update : UpdateFilter < T >
@@ -248,7 +245,7 @@ export class MongoDBDataAPI<InnerDoc = Document> {
248245 * Replace a Single Document.
249246 * @link https://www.mongodb.com/docs/atlas/api/data-api-resources/#replace-a-single-document
250247 */
251- public replaceOne < D = InnerDoc , T = NoInfer < D > > (
248+ public replaceOne < D = InnerDoc , T = NoInfer < D > > (
252249 params : ExtendBaseParams < {
253250 filter : Filter < T >
254251 replacement : any
@@ -266,7 +263,7 @@ export class MongoDBDataAPI<InnerDoc = Document> {
266263 * Delete a Single Document.
267264 * @link https://www.mongodb.com/docs/atlas/api/data-api-resources/#delete-a-single-document
268265 */
269- public deleteOne < D = InnerDoc , T = NoInfer < D > > (
266+ public deleteOne < D = InnerDoc , T = NoInfer < D > > (
270267 params : ExtendBaseParams < { filter : Filter < T > } >
271268 ) {
272269 return this . $$action < { deletedCount : number } > ( 'deleteOne' , params )
@@ -276,7 +273,7 @@ export class MongoDBDataAPI<InnerDoc = Document> {
276273 * Delete Multiple Documents.
277274 * @link https://www.mongodb.com/docs/atlas/api/data-api-resources/#delete-multiple-documents
278275 */
279- public deleteMany < D = InnerDoc , T = NoInfer < D > > (
276+ public deleteMany < D = InnerDoc , T = NoInfer < D > > (
280277 params : ExtendBaseParams < { filter : Filter < T > } >
281278 ) {
282279 return this . $$action < { deletedCount : number } > ( 'deleteMany' , params )
@@ -286,13 +283,13 @@ export class MongoDBDataAPI<InnerDoc = Document> {
286283 * Run an Aggregation Pipeline.
287284 * @link https://www.mongodb.com/docs/atlas/api/data-api-resources/#run-an-aggregation-pipeline
288285 */
289- public aggregate < T extends Array < any > > (
286+ public aggregate < T extends Array < any > > (
290287 params : ExtendBaseParams < { pipeline : Array < Document > } >
291288 ) {
292289 return this . $$action < { documents : T } > ( 'aggregate' , params )
293290 }
294291}
295292
296- export const createMongoDBDataAPI = ( config : Config , axios ?: AxiosInstance ) => {
297- return new MongoDBDataAPI ( config , void 0 , axios )
293+ export const createMongoDBDataAPI = ( config : Config ) => {
294+ return new MongoDBDataAPI ( config , void 0 )
298295}
0 commit comments