Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit 75b8a5c

Browse files
committed
feat: Adds retries for hard-failures
1 parent 3304e92 commit 75b8a5c

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
"src/index.ts"
2020
],
2121
"format": [
22-
"esm",
23-
"cjs"
22+
"esm"
2423
],
2524
"splitting": false,
2625
"sourcemap": true,
@@ -29,14 +28,14 @@
2928
"minify": false
3029
},
3130
"type": "module",
32-
"main": "dist/index.cjs",
31+
"main": "dist/index.js",
3332
"module": "dist/index.js",
3433
"types": "dist/index.d.ts",
3534
"exports": {
3635
".": {
3736
"types": "./dist/index.d.ts",
3837
"import": "./dist/index.js",
39-
"require": "./dist/index.cjs"
38+
"require": null
4039
}
4140
},
4241
"files": [
@@ -46,12 +45,13 @@
4645
"src"
4746
],
4847
"engines": {
49-
"node": ">=14.19.0",
48+
"node": ">=18.0.0",
5049
"pnpm": "8.x"
5150
},
5251
"dependencies": {
5352
"bson": "^6.2.0",
5453
"mongodb": "^6.2.0",
54+
"p-retry": "^6.1.0",
5555
"tslib": "^2.6.2"
5656
},
5757
"devDependencies": {

pnpm-lock.yaml

+21-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client.ts

+21-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
WithoutId,
1010
Document,
1111
} from "mongodb";
12+
import pRetry from "p-retry";
1213
import type { AuthOptions } from "./authTypes.js";
1314
import { MongoDataAPIError } from "./errors.js";
1415

@@ -435,16 +436,26 @@ export class Collection<TSchema = Document> {
435436
const { endpoint, dataSource, headers } = this.client;
436437
const url = `${endpoint}/action/${method}`;
437438

438-
const response = await this.client.fetch(url, {
439-
method: "POST",
440-
headers,
441-
body: EJSON.stringify({
442-
collection: this.name,
443-
database: this.database.name,
444-
dataSource,
445-
...removeEmptyKeys(body),
446-
}),
447-
});
439+
const response = await pRetry(
440+
async () => {
441+
const r = await this.client.fetch(url, {
442+
method: "POST",
443+
headers,
444+
body: EJSON.stringify({
445+
collection: this.name,
446+
database: this.database.name,
447+
dataSource,
448+
...removeEmptyKeys(body),
449+
}),
450+
});
451+
452+
return r;
453+
},
454+
{
455+
minTimeout: 10,
456+
maxTimeout: 1000,
457+
}
458+
);
448459

449460
// interpret response code. Log error for anything outside of 2xx 3xx
450461
// https://www.mongodb.com/docs/atlas/api/data-api-resources/#error-codes

0 commit comments

Comments
 (0)