mongodb-atlas-api-client 4.12.0 → 4.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/copilot-instructions.md +239 -0
- package/package.json +2 -2
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
# MongoDB Atlas API Client - Copilot Instructions
|
|
2
|
+
|
|
3
|
+
## Project Overview
|
|
4
|
+
|
|
5
|
+
This is a MongoDB Atlas API client library for Node.js that provides a programmatic interface to the MongoDB Atlas REST API. The library uses HTTP Digest Authentication and wraps the urllib HTTP client library.
|
|
6
|
+
|
|
7
|
+
## Architecture and Code Organization
|
|
8
|
+
|
|
9
|
+
### Module Structure
|
|
10
|
+
- Each API resource (User, Cluster, Project, Organization, etc.) is implemented as a separate class in `src/`
|
|
11
|
+
- Each class follows a consistent pattern:
|
|
12
|
+
- Constructor receives: `client`, `baseUrl`, and optionally `projectId`
|
|
13
|
+
- Methods return promises
|
|
14
|
+
- All API methods accept an optional `options` parameter
|
|
15
|
+
- The main entry point (`src/index.js`) exports a factory function that creates instances of all API resource classes
|
|
16
|
+
- TypeScript definitions (`.d.ts` files) are provided alongside each JavaScript file
|
|
17
|
+
|
|
18
|
+
### Key Components
|
|
19
|
+
- `httpClient.js`: HTTP client wrapper that handles digest authentication
|
|
20
|
+
- `helper.js`: Utility functions for query string generation
|
|
21
|
+
- `index.js`: Main entry point that instantiates and exports all API resource clients
|
|
22
|
+
|
|
23
|
+
## Code Patterns and Conventions
|
|
24
|
+
|
|
25
|
+
### API Method Pattern
|
|
26
|
+
```javascript
|
|
27
|
+
async methodName(param1, param2, options = {}) {
|
|
28
|
+
const queryString = getQueryStringFromOptions(options);
|
|
29
|
+
const httpOptions = options.httpOptions;
|
|
30
|
+
const response = await this.client_.fetch(`${this.baseUrl_}/path/${param1}?${queryString}`, {
|
|
31
|
+
"method": "METHOD",
|
|
32
|
+
"data": body,
|
|
33
|
+
...httpOptions
|
|
34
|
+
});
|
|
35
|
+
return response;
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Class Pattern
|
|
40
|
+
- Private properties use trailing underscore: `this.client_`, `this.baseUrl_`, `this.projectId_`
|
|
41
|
+
- All methods are async and return promises
|
|
42
|
+
- Methods should handle `options` parameter for query strings and HTTP options
|
|
43
|
+
- The `httpOptions` parameter is extracted from options and passed separately (not included in query string)
|
|
44
|
+
|
|
45
|
+
### Naming Conventions
|
|
46
|
+
- Use camelCase for variables and function names
|
|
47
|
+
- Use PascalCase for class names
|
|
48
|
+
- Use descriptive names (e.g., `getAll`, `getById`, `delete`, `create`, `update`)
|
|
49
|
+
- Private instance variables end with underscore: `this.client_`, `this.baseUrl_`
|
|
50
|
+
|
|
51
|
+
## Testing Requirements
|
|
52
|
+
|
|
53
|
+
### Test Framework
|
|
54
|
+
- Uses `@hapi/lab` for test execution
|
|
55
|
+
- Uses `@hapi/code` for assertions
|
|
56
|
+
- Uses `sinon` for stubbing/mocking when needed
|
|
57
|
+
- Uses `urllib`'s MockAgent for HTTP mocking
|
|
58
|
+
- **100% code coverage is required** - all tests must pass with full coverage
|
|
59
|
+
|
|
60
|
+
### Test Structure Pattern
|
|
61
|
+
```javascript
|
|
62
|
+
const {describe, it, afterEach, before, beforeEach} = exports.lab = require("@hapi/lab").script();
|
|
63
|
+
const {expect} = require('@hapi/code');
|
|
64
|
+
const getClient = require('../src/index.js');
|
|
65
|
+
const {MockAgent, setGlobalDispatcher} = require('urllib');
|
|
66
|
+
|
|
67
|
+
const baseUrl = "http://localhost:7001";
|
|
68
|
+
const projectId = "dummyProjectId";
|
|
69
|
+
|
|
70
|
+
const client = getClient({
|
|
71
|
+
"publicKey": "dummyPublicKey",
|
|
72
|
+
"privateKey": "dummyPrivateKey",
|
|
73
|
+
"baseUrl": baseUrl,
|
|
74
|
+
"projectId": projectId
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe("Mongo Atlas Api Client - ResourceName", () => {
|
|
78
|
+
let mockAgent;
|
|
79
|
+
let mockPool;
|
|
80
|
+
|
|
81
|
+
before(() => {
|
|
82
|
+
mockAgent = new MockAgent();
|
|
83
|
+
setGlobalDispatcher(mockAgent);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
beforeEach(() => {
|
|
87
|
+
mockPool = mockAgent.get(baseUrl);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
afterEach(() => {
|
|
91
|
+
mockAgent.assertNoPendingInterceptors();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe("When method is called", () => {
|
|
95
|
+
it("should return expected result", async () => {
|
|
96
|
+
mockPool.intercept({
|
|
97
|
+
"path": `/expected/path`,
|
|
98
|
+
"method": "get"
|
|
99
|
+
}).reply(200, {"response": "data"});
|
|
100
|
+
|
|
101
|
+
const result = await client.resource.method();
|
|
102
|
+
expect(result).to.equal({"response": "data"});
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Test Guidelines
|
|
109
|
+
- Test file names match source files: `user.js` → `user.test.js`
|
|
110
|
+
- Each resource should have tests for:
|
|
111
|
+
- Verification that methods are exported from index
|
|
112
|
+
- Each public method with various parameter combinations
|
|
113
|
+
- Query string handling
|
|
114
|
+
- HTTP options handling
|
|
115
|
+
- Use MockAgent to intercept HTTP requests
|
|
116
|
+
- Always call `mockAgent.assertNoPendingInterceptors()` in afterEach
|
|
117
|
+
|
|
118
|
+
## Linting and Code Style
|
|
119
|
+
|
|
120
|
+
### ESLint Configuration
|
|
121
|
+
- Uses ESLint 9 with `@eslint/js` recommended config
|
|
122
|
+
- ES2015+ (latest) JavaScript syntax
|
|
123
|
+
- CommonJS module system (not ES modules)
|
|
124
|
+
- Strict rules including:
|
|
125
|
+
- No console logs (`no-console: error`)
|
|
126
|
+
- No var declarations (`no-var: error`)
|
|
127
|
+
- Prefer const (`prefer-const: error`)
|
|
128
|
+
- Prefer template literals (`prefer-template: error`)
|
|
129
|
+
- Complexity limit of 9 (`complexity: [warn, 9]`)
|
|
130
|
+
- One variable declaration per statement (`one-var: [warn, never]`)
|
|
131
|
+
- Variable name length: 2-70 characters (with exceptions for i, j, k, n, Q, _)
|
|
132
|
+
|
|
133
|
+
### Code Style Guidelines
|
|
134
|
+
- Use double quotes for strings (no enforcement, but check existing code)
|
|
135
|
+
- Use semicolons at end of statements (no enforcement, but check existing code)
|
|
136
|
+
- No trailing commas in arrays/objects (`comma-dangle: [error, never]`)
|
|
137
|
+
- Use arrow functions for callbacks where appropriate
|
|
138
|
+
- Prefer object method shorthand
|
|
139
|
+
|
|
140
|
+
## Build and Development Commands
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
# Install dependencies
|
|
144
|
+
npm install
|
|
145
|
+
|
|
146
|
+
# Run tests (includes linting and depcheck)
|
|
147
|
+
npm test
|
|
148
|
+
|
|
149
|
+
# Run linting only
|
|
150
|
+
npm run lint
|
|
151
|
+
|
|
152
|
+
# Run dependency check
|
|
153
|
+
npm run depcheck
|
|
154
|
+
|
|
155
|
+
# Version bumping and publishing
|
|
156
|
+
npm run patch # Bump patch version
|
|
157
|
+
npm run minor # Bump minor version
|
|
158
|
+
npm run major # Bump major version
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### Pre-test Steps
|
|
162
|
+
The `pretest` script automatically runs:
|
|
163
|
+
1. Dependency check with `depcheck`
|
|
164
|
+
2. ESLint on all source and test files
|
|
165
|
+
|
|
166
|
+
## TypeScript Definitions
|
|
167
|
+
|
|
168
|
+
- All source files should have corresponding `.d.ts` TypeScript definition files
|
|
169
|
+
- Type definitions should match the JavaScript implementation
|
|
170
|
+
- Export types for all public classes and their methods
|
|
171
|
+
- Method signatures should include parameter types and return types
|
|
172
|
+
|
|
173
|
+
## Dependencies
|
|
174
|
+
|
|
175
|
+
### Production
|
|
176
|
+
- `urllib`: HTTP client library (version 4.x)
|
|
177
|
+
|
|
178
|
+
### Development
|
|
179
|
+
- `@hapi/lab`: Test runner
|
|
180
|
+
- `@hapi/code`: Assertion library
|
|
181
|
+
- `eslint`: Linting
|
|
182
|
+
- `depcheck`: Dependency checker
|
|
183
|
+
- `sinon`: Test stubbing/mocking
|
|
184
|
+
|
|
185
|
+
## MongoDB Atlas API Specifics
|
|
186
|
+
|
|
187
|
+
### Authentication
|
|
188
|
+
- Uses HTTP Digest Authentication
|
|
189
|
+
- Requires public key (username) and private key (password)
|
|
190
|
+
- Authentication is handled by HttpClient class
|
|
191
|
+
|
|
192
|
+
### Common Parameters
|
|
193
|
+
- `baseUrl`: MongoDB Atlas API base URL (e.g., `https://cloud.mongodb.com/api/atlas/v1.0`)
|
|
194
|
+
- `projectId`: MongoDB Atlas project/group ID
|
|
195
|
+
- `options`: Object that can contain:
|
|
196
|
+
- Query parameters (e.g., `envelope`, `itemsPerPage`, `pretty`)
|
|
197
|
+
- `httpOptions`: Additional options passed to urllib (e.g., `timeout`)
|
|
198
|
+
|
|
199
|
+
### URL Structure
|
|
200
|
+
- Groups and projects are synonymous in MongoDB Atlas API
|
|
201
|
+
- URLs typically follow pattern: `/groups/{projectId}/resource/{identifier}`
|
|
202
|
+
|
|
203
|
+
## Best Practices for Contributing
|
|
204
|
+
|
|
205
|
+
1. **Minimal Changes**: Make the smallest possible changes to accomplish the goal
|
|
206
|
+
2. **Test Coverage**: Maintain 100% test coverage - add tests for all new code
|
|
207
|
+
3. **Consistency**: Follow existing patterns in the codebase
|
|
208
|
+
4. **No Breaking Changes**: Don't modify existing public APIs unless necessary
|
|
209
|
+
5. **Documentation**: Update README.md if adding new features
|
|
210
|
+
6. **Type Safety**: Update corresponding `.d.ts` files when modifying JavaScript
|
|
211
|
+
7. **Error Handling**: Follow existing error handling patterns (let errors propagate)
|
|
212
|
+
8. **Async/Await**: Use async/await for all asynchronous operations (not callbacks or raw promises)
|
|
213
|
+
|
|
214
|
+
## Common Tasks
|
|
215
|
+
|
|
216
|
+
### Adding a New API Resource
|
|
217
|
+
|
|
218
|
+
1. Create `src/resourceName.js` with class following the established pattern
|
|
219
|
+
2. Create `src/resourceName.d.ts` with TypeScript definitions
|
|
220
|
+
3. Add resource to `src/index.js` initialization and exports
|
|
221
|
+
4. Create `test/resourceName.test.js` with comprehensive tests
|
|
222
|
+
5. Ensure 100% coverage and all tests pass
|
|
223
|
+
6. Update README.md if the resource is a significant new feature
|
|
224
|
+
|
|
225
|
+
### Adding a Method to Existing Resource
|
|
226
|
+
|
|
227
|
+
1. Add method to the class in `src/`
|
|
228
|
+
2. Update corresponding `.d.ts` file
|
|
229
|
+
3. Add comprehensive tests in `test/` file
|
|
230
|
+
4. Ensure 100% coverage maintained
|
|
231
|
+
5. Run linter and fix any issues
|
|
232
|
+
|
|
233
|
+
## Important Notes
|
|
234
|
+
|
|
235
|
+
- This library is production code used by many users - stability is critical
|
|
236
|
+
- All changes must pass existing tests without modification
|
|
237
|
+
- HTTP interactions are mocked in tests using urllib's MockAgent
|
|
238
|
+
- The library uses MongoDB Atlas API v1.0
|
|
239
|
+
- Digest authentication is required for all API calls
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mongodb-atlas-api-client",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.13.0",
|
|
4
4
|
"description": "A mongodb atlas api client for nodejs.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"pretest": "npm run depcheck && eslint --cache \"src/**/*.js\" \"test/**/*.js\"",
|
|
8
8
|
"depcheck": "depcheck",
|
|
9
|
-
"test": "./node_modules/@hapi/lab/bin/lab -I '@@any-promise/REGISTRATION,Symbol(undici.globalDispatcher.1)' ./test/ -v -S --assert @hapi/code --threshold 100 -p 1 -o test-results/result.json -r json -r console -o stdout",
|
|
9
|
+
"test": "./node_modules/@hapi/lab/bin/lab -I '@@any-promise/REGISTRATION,Symbol(undici.globalDispatcher.1),File' ./test/ -v -S --assert @hapi/code --threshold 100 -p 1 -o test-results/result.json -r json -r console -o stdout",
|
|
10
10
|
"lint": "./node_modules/.bin/eslint ./src --fix",
|
|
11
11
|
"premajor": "npm run test",
|
|
12
12
|
"major": "npm version major -m \"published to npm as v%s\" && git push --follow-tags && npm publish",
|