Skip to content

Commit c1e00ad

Browse files
committed
Use fetch instead of axios
1 parent 5be63f1 commit c1e00ad

File tree

7 files changed

+925
-637
lines changed

7 files changed

+925
-637
lines changed

jest.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @type {import('@ts-jest/dist/types').InitialOptionsTsJest} */
22
module.exports = {
3-
preset: 'ts-jest',
4-
testEnvironment: 'node',
5-
collectCoverage: true,
6-
coverageDirectory: 'coverage'
3+
preset: 'ts-jest',
4+
testEnvironment: 'jsdom',
5+
collectCoverage: true,
6+
coverageDirectory: 'coverage'
77
}

libundler.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { defineConfig } from '@surmon-china/libundler'
22

33
export default defineConfig({
44
libName: 'MongoDBDataAPI',
5-
outFileName: 'mongodb-data-api',
5+
outFileName: 'mongodb-data-api-fetch',
66
targets: ['cjs', 'esm'],
77
entry: './src/index.ts',
88
outDir: './dist',
9-
external: ['axios', 'mongodb'],
9+
external: ['mongodb'],
1010
terser: false,
1111
sourcemap: false
1212
})

package.json

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
{
2-
"name": "mongodb-data-api",
3-
"version": "0.4.0",
4-
"description": "MongoDB atlas data API SDK for Node.js",
2+
"name": "mongodb-data-api-fetch",
3+
"version": "0.1.0",
4+
"description": "MongoDB Atlas Data API SDK for Cloudflare Workers",
55
"author": "Surmon",
6+
"contributors": [
7+
{
8+
"name": "trophologist",
9+
"email": "info@trophologist.com",
10+
"url": "https://trophologist.com"
11+
}
12+
],
613
"license": "MIT",
714
"keywords": [
8-
"MongoDB data API",
9-
"MongoDB cloud data API",
10-
"MongoDB Atlas data API SDK"
15+
"MongoDB",
16+
"MongoDB Data API",
17+
"MongoDB Atlas Data API SDK",
18+
"Cloudflare Workers",
19+
"Fetch"
1120
],
1221
"repository": {
1322
"type": "git",
14-
"url": "https://github.com/surmon-china/mongodb-data-api.git"
23+
"url": "https://github.com/trophologist/mongodb-data-api-fetch.git"
1524
},
1625
"files": [
17-
"CHANGELOG.md",
1826
"dist"
1927
],
2028
"sideEffects": false,
@@ -32,7 +40,6 @@
3240
"release": ". ./scripts/release.sh"
3341
},
3442
"dependencies": {
35-
"axios": "^1.1.0",
3643
"mongodb": "^4.12.0"
3744
},
3845
"devDependencies": {
@@ -47,6 +54,7 @@
4754
"jest": "^29.3.0",
4855
"prettier": "^2.5.1",
4956
"ts-jest": "^29.0.0",
50-
"typescript": "^4.9.0"
57+
"typescript": "^4.9.0",
58+
"jest-environment-jsdom": "^29.3.1"
5159
}
5260
}

src/index.ts

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import _axios, { AxiosInstance, AxiosRequestConfig } from 'axios'
21
import 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>
5857
export 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
}

tests/index.spec.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import axios from 'axios'
21
import { createMongoDBDataAPI, MongoDBDataAPI } from '../src'
32

43
test('<type> should be function type', () => {
@@ -57,19 +56,6 @@ test('<runtime> should be error', async () => {
5756
})
5857
await api.$cluster('test').$database('test').$collection('test').find()
5958
} catch (error) {
60-
expect(axios.isAxiosError(error)).toBeFalsy()
61-
}
62-
})
63-
64-
test('<runtime> should be axios error', async () => {
65-
try {
66-
const api = createMongoDBDataAPI({
67-
apiKey: 'test_api_key',
68-
urlEndpoint: 'https://data.mongodb-api.com/app/test_app/endpoint/data/v1'
69-
})
70-
await api.$cluster('test').$database('test').$collection('test').findOne()
71-
} catch (error: any) {
72-
expect(error.name).toBe('AxiosError')
73-
expect(error.status).toBe(404)
59+
expect(error).toBeTruthy()
7460
}
7561
})

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"module": "esnext",
44
"target": "ES6",
5-
"lib": ["ESNext"],
5+
"lib": ["ESNext", "webworker"],
66
"moduleResolution": "node",
77
"strict": true,
88
"strictNullChecks": true,

0 commit comments

Comments
 (0)