1- import config from "./config.js" ;
1+ import { log } from "console" ;
2+ import config from "../../config.js" ;
3+
4+ import { Group , PaginatedOrgGroupView , PaginatedAtlasGroupView , ClusterDescription20240805 , PaginatedClusterDescription20240805 } from "./openapi.js"
25
36export interface OAuthToken {
47 access_token : string ;
@@ -10,27 +13,6 @@ export interface OAuthToken {
1013 expiry : Date ;
1114}
1215
13- export interface AtlasProject {
14- id : string ;
15- name : string ;
16- created ?: {
17- $date : string ;
18- } ;
19- }
20-
21- export interface AtlasCluster {
22- id ?: string ;
23- name : string ;
24- stateName : string ;
25- mongoDBVersion ?: string ;
26- providerSettings ?: {
27- regionName ?: string ;
28- } ;
29- connectionStrings ?: {
30- standard ?: string ;
31- } ;
32- }
33-
3416export interface OauthDeviceCode {
3517 user_code : string ;
3618 verification_uri : string ;
@@ -39,11 +21,6 @@ export interface OauthDeviceCode {
3921 interval : string ;
4022}
4123
42- export interface AtlasResponse < T > {
43- results : T [ ] ;
44- totalCount ?: number ;
45- }
46-
4724export type saveTokenFunction = ( token : OAuthToken ) => void | Promise < void > ;
4825
4926export class ApiClientError extends Error {
@@ -120,10 +97,13 @@ export class ApiClient {
12097 ...options ?. headers ,
12198 } ,
12299 } ;
100+
101+ console . error ( `Calling Atlas API: ${ url . toString ( ) } ` ) ;
102+ console . error ( `with: ${ JSON . stringify ( opt ) } ` ) ;
123103 const response = await fetch ( url , opt ) ;
124104
125105 if ( ! response . ok ) {
126- throw new ApiClientError ( `Error calling Atlas API: ${ response . statusText } ` , response ) ;
106+ throw new ApiClientError ( `Error calling Atlas API: ${ await response . text ( ) } ` , response ) ;
127107 }
128108
129109 return ( await response . json ( ) ) as T ;
@@ -282,31 +262,33 @@ export class ApiClient {
282262 }
283263 }
284264
285- /**
286- * Get all projects for the authenticated user
287- */
288- async listProjects ( ) : Promise < AtlasResponse < AtlasProject > > {
289- return await this . do < AtlasResponse < AtlasProject > > ( "/groups" ) ;
265+ async listProjects ( ) : Promise < PaginatedAtlasGroupView > {
266+ return await this . do < PaginatedAtlasGroupView > ( "/groups" ) ;
290267 }
291268
292- /**
293- * Get a specific project by ID
294- */
295- async getProject ( projectId : string ) : Promise < AtlasProject > {
296- return await this . do < AtlasProject > ( `/groups/${ projectId } ` ) ;
269+ async getProject ( groupId : string ) : Promise < Group > {
270+ return await this . do < Group > ( `/groups/${ groupId } ` ) ;
297271 }
298272
299- /**
300- * Get clusters for a specific project
301- */
302- async listProjectClusters ( projectId : string ) : Promise < AtlasResponse < AtlasCluster > > {
303- return await this . do < AtlasResponse < AtlasCluster > > ( `/groups/${ projectId } /clusters` ) ;
273+ async listClusters ( groupId : string ) : Promise < PaginatedClusterDescription20240805 > {
274+ return await this . do < PaginatedClusterDescription20240805 > ( `/groups/${ groupId } /clusters` ) ;
304275 }
305276
306- /**
307- * Get clusters for a specific project
308- */
309- async listAllClusters ( ) : Promise < AtlasResponse < AtlasCluster > > {
310- return await this . do < AtlasResponse < AtlasCluster > > ( `/clusters` ) ;
277+ async listClustersForAllProjects ( ) : Promise < PaginatedOrgGroupView > {
278+ return await this . do < PaginatedOrgGroupView > ( `/clusters` ) ;
279+ }
280+
281+ async getCluster ( groupId : string , clusterName : string ) : Promise < ClusterDescription20240805 > {
282+ return await this . do < ClusterDescription20240805 > ( `/groups/${ groupId } /clusters/${ clusterName } ` ) ;
283+ }
284+
285+ async createCluster ( groupId : string , cluster : ClusterDescription20240805 ) : Promise < ClusterDescription20240805 > {
286+ if ( ! cluster . groupId ) {
287+ throw new Error ( "Cluster groupId is required" ) ;
288+ }
289+ return await this . do < ClusterDescription20240805 > ( `/groups/${ groupId } /clusters` , {
290+ method : "POST" ,
291+ body : JSON . stringify ( cluster ) ,
292+ } ) ;
311293 }
312294}
0 commit comments