@@ -11,27 +11,6 @@ type AnyKeys<T> = { [P in keyof T]?: T[P] | any }
1111type Without < T , U > = { [ P in Exclude < keyof T , keyof U > ] ?: never }
1212type XOR < T , U > = T | U extends object ? ( Without < T , U > & U ) | ( Without < U , T > & T ) : T | U
1313
14- /**
15- * Specific region of endpoint.
16- * @link https://docs.atlas.mongodb.com/api/data-api-resources/#regional-requests
17- */
18- export enum Region {
19- Virginia = 'us-east-1' ,
20- Oregon = 'us-west-2' ,
21- Ireland = 'eu-west-1' ,
22- Sydney = 'ap-southeast-2'
23- }
24-
25- // https://docs.atlas.mongodb.com/api/data-api-resources/#base-url
26- const getUrlEndpoint = ( appId : string , region ?: Region ) => {
27- return region
28- ? `https://${ region } .aws.data.mongodb-api.com/app/${ appId } /endpoint/data/beta`
29- : `https://data.mongodb-api.com/app/${ appId } /endpoint/data/beta`
30- }
31- const getActionUrl = ( endpoint : string , action : string ) => {
32- return `${ endpoint } /action/${ action } `
33- }
34-
3514type ExtendBaseParams < T > = BaseParams & T
3615interface BaseParams {
3716 dataSource ?: string
@@ -43,24 +22,35 @@ interface BaseParams {
4322interface BaseConfig {
4423 /**
4524 * Specific Data API key.
46- * @link https://docs.atlas. mongodb.com/api/data-api/#2.-create-a-data-api-key
25+ * @link https://www. mongodb.com/docs/atlas /api/data-api/#2.-create-a-data-api-key
4726 */
4827 apiKey : string
4928}
29+
5030interface UrlEndpointConfig extends BaseConfig {
5131 /**
5232 * Specific URL Endpoint.
53- * @link https://docs.atlas. mongodb.com/api/data-api/#3.-send-a-data-api-request
33+ * @link https://www. mongodb.com/docs/atlas /api/data-api/#3.-send-a-data-api-request
5434 */
5535 urlEndpoint : string
5636}
37+
5738interface PackEndpointConfig extends BaseConfig {
5839 /**
5940 * Specific Data App ID.
60- * @link https://docs.atlas. mongodb.com/api/data-api/#3.-send-a-data-api-request
41+ * @link https://www. mongodb.com/docs/atlas /api/data-api/#3.-send-a-data-api-request
6142 */
6243 appId : string
63- region ?: Region
44+ /**
45+ * Specific region name of endpoint.
46+ * @link https://www.mongodb.com/docs/atlas/api/data-api-resources/#regional-requests
47+ */
48+ region ?: string
49+ /**
50+ * Specific cloud provider of endpoint.
51+ * @link https://www.mongodb.com/docs/atlas/api/data-api-resources/#regional-requests
52+ */
53+ cloud ?: string
6454}
6555
6656export type Config = XOR < UrlEndpointConfig , PackEndpointConfig >
@@ -116,10 +106,10 @@ export class MongoDBDataAPI<InnerDoc = Document> {
116106
117107 /**
118108 * Execute a API action.
119- * @link https://docs.atlas. mongodb.com/api/data-api-resources/
109+ * @link https://www. mongodb.com/docs/atlas /api/data-api-resources/
120110 */
121111 public $$action < Result = unknown > (
122- name : string ,
112+ type : string ,
123113 params : BaseParams = { } ,
124114 axiosConfig ?: AxiosRequestConfig
125115 ) : Promise < Result > {
@@ -132,14 +122,29 @@ export class MongoDBDataAPI<InnerDoc = Document> {
132122 return Promise . reject ( 'Invalid params: dataSource, database, collection' )
133123 }
134124
125+ // https://www.mongodb.com/docs/atlas/api/data-api-resources/#base-url
126+ // https://www.mongodb.com/docs/atlas/api/data-api-resources/#regional-requests
127+ const getUrlEndpoint = ( appId : string , region ?: string , cloud ?: string ) => {
128+ return region && cloud
129+ ? `https://${ region } .${ cloud } .data.mongodb-api.com/app/${ appId } /endpoint/data/v1`
130+ : `https://data.mongodb-api.com/app/${ appId } /endpoint/data/v1`
131+ }
132+
133+ const getActionUrl = ( endpoint : string , action : string ) => {
134+ return `${ endpoint } /action/${ action } `
135+ }
136+
135137 const API_KEY_FIELD = 'api-key'
136138
137139 return this . #axios( {
138140 method : 'post' ,
139141 data : JSON . stringify ( mergedParams ) ,
140142 url : this . #config. urlEndpoint
141- ? getActionUrl ( this . #config. urlEndpoint , name )
142- : getActionUrl ( getUrlEndpoint ( this . #config. appId ! , this . #config. region ) , name ) ,
143+ ? getActionUrl ( this . #config. urlEndpoint , type )
144+ : getActionUrl (
145+ getUrlEndpoint ( this . #config. appId ! , this . #config. region , this . #config. cloud ) ,
146+ type
147+ ) ,
143148 headers : {
144149 'Content-Type' : 'application/json' ,
145150 'Access-Control-Request-Headers' : '*' ,
@@ -151,16 +156,20 @@ export class MongoDBDataAPI<InnerDoc = Document> {
151156 return response . data
152157 } )
153158 . catch ( ( error ) => {
154- // https://docs.atlas.mongodb.com/api/data-api-resources/#error-codes
155- const errorJSON = error . toJSON ( )
156- errorJSON . config . headers [ API_KEY_FIELD ] = '*****'
157- return Promise . reject ( error . toJSON ( ) )
159+ // https://www.mongodb.com/docs/atlas/api/data-api-resources/#error-codes
160+ if ( _axios . isAxiosError ( error ) ) {
161+ const errorJSON : any = error . toJSON ( )
162+ errorJSON . config . headers [ API_KEY_FIELD ] = '*****'
163+ return Promise . reject ( errorJSON )
164+ } else {
165+ return Promise . reject ( error )
166+ }
158167 } )
159168 }
160169
161170 /**
162171 * Find a Single Document.
163- * @link https://docs.atlas. mongodb.com/api/data-api-resources/#find-a-single-document
172+ * @link https://www. mongodb.com/docs/atlas /api/data-api-resources/#find-a-single-document
164173 */
165174 public findOne < D = InnerDoc , T = NoInfer < D > > (
166175 params ?: ExtendBaseParams < {
@@ -173,7 +182,7 @@ export class MongoDBDataAPI<InnerDoc = Document> {
173182
174183 /**
175184 * Find Multiple Documents.
176- * @link https://docs.atlas. mongodb.com/api/data-api-resources/#find-multiple-documents
185+ * @link https://www. mongodb.com/docs/atlas /api/data-api-resources/#find-multiple-documents
177186 */
178187 public find < D = InnerDoc , T = NoInfer < D > > (
179188 params ?: ExtendBaseParams < {
@@ -189,7 +198,7 @@ export class MongoDBDataAPI<InnerDoc = Document> {
189198
190199 /**
191200 * Insert a Single Document.
192- * @link https://docs.atlas. mongodb.com/api/data-api-resources/#insert-a-single-document
201+ * @link https://www. mongodb.com/docs/atlas /api/data-api-resources/#insert-a-single-document
193202 */
194203 public insertOne < D = InnerDoc , T = NoInfer < D > > (
195204 params : ExtendBaseParams < { document : AnyKeys < T > | Document } >
@@ -199,7 +208,7 @@ export class MongoDBDataAPI<InnerDoc = Document> {
199208
200209 /**
201210 * Insert Multiple Documents.
202- * @link https://docs.atlas. mongodb.com/api/data-api-resources/#insert-multiple-documents
211+ * @link https://www. mongodb.com/docs/atlas /api/data-api-resources/#insert-multiple-documents
203212 */
204213 public insertMany < D = InnerDoc , T = NoInfer < D > > (
205214 params : ExtendBaseParams < { documents : Array < AnyKeys < T > | Document > } >
@@ -209,7 +218,7 @@ export class MongoDBDataAPI<InnerDoc = Document> {
209218
210219 /**
211220 * Update a Single Document.
212- * @link https://docs.atlas. mongodb.com/api/data-api-resources/#update-a-single-document
221+ * @link https://www. mongodb.com/docs/atlas /api/data-api-resources/#update-a-single-document
213222 */
214223 public updateOne < D = InnerDoc , T = NoInfer < D > > (
215224 params : ExtendBaseParams < {
@@ -227,7 +236,7 @@ export class MongoDBDataAPI<InnerDoc = Document> {
227236
228237 /**
229238 * Update Multiple Documents.
230- * @link https://docs.atlas. mongodb.com/api/data-api-resources/#update-multiple-documents
239+ * @link https://www. mongodb.com/docs/atlas /api/data-api-resources/#update-multiple-documents
231240 */
232241 public updateMany < D = InnerDoc , T = NoInfer < D > > (
233242 params : ExtendBaseParams < {
@@ -245,7 +254,7 @@ export class MongoDBDataAPI<InnerDoc = Document> {
245254
246255 /**
247256 * Replace a Single Document.
248- * @link https://docs.atlas. mongodb.com/api/data-api-resources/#replace-a-single-document
257+ * @link https://www. mongodb.com/docs/atlas /api/data-api-resources/#replace-a-single-document
249258 */
250259 public replaceOne < D = InnerDoc , T = NoInfer < D > > (
251260 params : ExtendBaseParams < {
@@ -263,7 +272,7 @@ export class MongoDBDataAPI<InnerDoc = Document> {
263272
264273 /**
265274 * Delete a Single Document.
266- * @link https://docs.atlas. mongodb.com/api/data-api-resources/#delete-a-single-document
275+ * @link https://www. mongodb.com/docs/atlas /api/data-api-resources/#delete-a-single-document
267276 */
268277 public deleteOne < D = InnerDoc , T = NoInfer < D > > (
269278 params : ExtendBaseParams < { filter : Filter < T > } >
@@ -273,7 +282,7 @@ export class MongoDBDataAPI<InnerDoc = Document> {
273282
274283 /**
275284 * Delete Multiple Documents.
276- * @link https://docs.atlas. mongodb.com/api/data-api-resources/#delete-multiple-documents
285+ * @link https://www. mongodb.com/docs/atlas /api/data-api-resources/#delete-multiple-documents
277286 */
278287 public deleteMany < D = InnerDoc , T = NoInfer < D > > (
279288 params : ExtendBaseParams < { filter : Filter < T > } >
@@ -283,7 +292,7 @@ export class MongoDBDataAPI<InnerDoc = Document> {
283292
284293 /**
285294 * Run an Aggregation Pipeline.
286- * @link https://docs.atlas. mongodb.com/api/data-api-resources/#run-an-aggregation-pipeline
295+ * @link https://www. mongodb.com/docs/atlas /api/data-api-resources/#run-an-aggregation-pipeline
287296 */
288297 public aggregate < T extends Array < any > > (
289298 params : ExtendBaseParams < { pipeline : Array < Document > } >
0 commit comments