opticedge-cloud-utils 1.0.45 → 1.1.1
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/package.json +47 -6
- package/src/db/mongo.ts +1 -1
- package/src/db/mongo2.ts +1 -1
- package/src/db/mongo3.ts +1 -1
- package/src/{utils/tw-wallet.ts → tw/wallet.ts} +1 -1
- package/{src/auth/verify.test.ts → tests/auth.test.ts} +1 -1
- package/{src → tests}/db/mongo.test.ts +4 -4
- package/{src → tests}/db/mongo2.test.ts +5 -5
- package/{src → tests}/db/mongo3.test.ts +5 -5
- package/{src/utils → tests}/env.test.ts +1 -1
- package/{src/utils → tests}/regex.test.ts +1 -1
- package/{src/utils → tests}/secrets.test.ts +1 -1
- package/{src/utils → tests}/task.test.ts +1 -1
- package/{src/utils/tw-utils.test.ts → tests/tw/utils.test.ts} +1 -1
- package/{src/utils/tw-wallet.test.ts → tests/tw/wallet.test.ts} +3 -4
- package/dist/auth/verify.d.ts +0 -7
- package/dist/auth/verify.js +0 -23
- package/dist/auth/verify.test.d.ts +0 -1
- package/dist/auth/verify.test.js +0 -79
- package/dist/constants.d.ts +0 -2
- package/dist/constants.js +0 -7
- package/dist/db/mongo.d.ts +0 -3
- package/dist/db/mongo.js +0 -27
- package/dist/db/mongo.test.d.ts +0 -1
- package/dist/db/mongo.test.js +0 -73
- package/dist/db/mongo2.d.ts +0 -4
- package/dist/db/mongo2.js +0 -30
- package/dist/db/mongo2.test.d.ts +0 -1
- package/dist/db/mongo2.test.js +0 -86
- package/dist/db/mongo3.d.ts +0 -6
- package/dist/db/mongo3.js +0 -39
- package/dist/db/mongo3.test.d.ts +0 -1
- package/dist/db/mongo3.test.js +0 -108
- package/dist/index.d.ts +0 -17
- package/dist/index.js +0 -33
- package/dist/types/card.d.ts +0 -79
- package/dist/types/card.js +0 -19
- package/dist/types/notify.d.ts +0 -4
- package/dist/types/notify.js +0 -2
- package/dist/types/payment.d.ts +0 -1
- package/dist/types/payment.js +0 -2
- package/dist/types/pokemontcg.d.ts +0 -158
- package/dist/types/pokemontcg.js +0 -2
- package/dist/types/pricecharting.d.ts +0 -40
- package/dist/types/pricecharting.js +0 -2
- package/dist/types/user.d.ts +0 -37
- package/dist/types/user.js +0 -10
- package/dist/utils/env.d.ts +0 -1
- package/dist/utils/env.js +0 -12
- package/dist/utils/env.test.d.ts +0 -1
- package/dist/utils/env.test.js +0 -17
- package/dist/utils/regex.d.ts +0 -1
- package/dist/utils/regex.js +0 -6
- package/dist/utils/regex.test.d.ts +0 -1
- package/dist/utils/regex.test.js +0 -22
- package/dist/utils/secrets.d.ts +0 -8
- package/dist/utils/secrets.js +0 -22
- package/dist/utils/secrets.test.d.ts +0 -1
- package/dist/utils/secrets.test.js +0 -38
- package/dist/utils/task.d.ts +0 -1
- package/dist/utils/task.js +0 -38
- package/dist/utils/task.test.d.ts +0 -1
- package/dist/utils/task.test.js +0 -67
- package/dist/utils/tw-utils.d.ts +0 -1
- package/dist/utils/tw-utils.js +0 -19
- package/dist/utils/tw-utils.test.d.ts +0 -1
- package/dist/utils/tw-utils.test.js +0 -26
- package/dist/utils/tw-wallet.d.ts +0 -1
- package/dist/utils/tw-wallet.js +0 -32
- package/dist/utils/tw-wallet.test.d.ts +0 -1
- package/dist/utils/tw-wallet.test.js +0 -94
- package/src/constants.ts +0 -6
- package/src/index.ts +0 -17
- package/src/types/card.ts +0 -82
- package/src/types/notify.ts +0 -4
- package/src/types/payment.ts +0 -1
- package/src/types/pokemontcg.ts +0 -162
- package/src/types/pricecharting.ts +0 -42
- package/src/types/user.ts +0 -38
- /package/src/{auth/verify.ts → auth.ts} +0 -0
- /package/src/{utils/env.ts → env.ts} +0 -0
- /package/src/{utils/regex.ts → regex.ts} +0 -0
- /package/src/{utils/secrets.ts → secrets.ts} +0 -0
- /package/src/{utils/task.ts → task.ts} +0 -0
- /package/src/{utils/tw-utils.ts → tw/utils.ts} +0 -0
package/package.json
CHANGED
|
@@ -1,16 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opticedge-cloud-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Common utilities for cloud functions",
|
|
5
|
-
"
|
|
6
|
-
|
|
5
|
+
"exports": {
|
|
6
|
+
"./db/mongo": {
|
|
7
|
+
"import": "./dist/db/mongo.js",
|
|
8
|
+
"types": "./dist/db/mongo.d.ts"
|
|
9
|
+
},
|
|
10
|
+
"./db/mongo2": {
|
|
11
|
+
"import": "./dist/db/mongo2.js",
|
|
12
|
+
"types": "./dist/db/mongo2.d.ts"
|
|
13
|
+
},
|
|
14
|
+
"./db/mongo3": {
|
|
15
|
+
"import": "./dist/db/mongo3.js",
|
|
16
|
+
"types": "./dist/db/mongo3.d.ts"
|
|
17
|
+
},
|
|
18
|
+
"./auth": {
|
|
19
|
+
"import": "./dist/auth/index.js",
|
|
20
|
+
"types": "./dist/auth/index.d.ts"
|
|
21
|
+
},
|
|
22
|
+
"./env": {
|
|
23
|
+
"import": "./dist/env.js",
|
|
24
|
+
"types": "./dist/env.d.ts"
|
|
25
|
+
},
|
|
26
|
+
"./regex": {
|
|
27
|
+
"import": "./dist/regex.js",
|
|
28
|
+
"types": "./dist/regex.d.ts"
|
|
29
|
+
},
|
|
30
|
+
"./secrets": {
|
|
31
|
+
"import": "./dist/secrets.js",
|
|
32
|
+
"types": "./dist/secrets.d.ts"
|
|
33
|
+
},
|
|
34
|
+
"./task": {
|
|
35
|
+
"import": "./dist/task.js",
|
|
36
|
+
"types": "./dist/task.d.ts"
|
|
37
|
+
},
|
|
38
|
+
"./tw/utils": {
|
|
39
|
+
"import": "./dist/tw/tw-utils.js",
|
|
40
|
+
"types": "./dist/tw/tw-utils.d.ts"
|
|
41
|
+
},
|
|
42
|
+
"./tw/wallet": {
|
|
43
|
+
"import": "./dist/tw/wallet.js",
|
|
44
|
+
"types": "./dist/tw/wallet.d.ts"
|
|
45
|
+
}
|
|
46
|
+
},
|
|
7
47
|
"scripts": {
|
|
8
|
-
"
|
|
48
|
+
"clean": "rimraf dist coverage *.tsbuildinfo",
|
|
49
|
+
"build": "npm run clean && tsc",
|
|
50
|
+
"prepare": "npm run build",
|
|
9
51
|
"test": "jest --coverage",
|
|
10
52
|
"lint": "eslint . --ext .ts --fix",
|
|
11
53
|
"format": "prettier --write .",
|
|
12
|
-
"fix": "npm run lint && npm run format"
|
|
13
|
-
"prepare": "npm run build"
|
|
54
|
+
"fix": "npm run lint && npm run format"
|
|
14
55
|
},
|
|
15
56
|
"author": "Evans Musonda",
|
|
16
57
|
"license": "MIT",
|
package/src/db/mongo.ts
CHANGED
package/src/db/mongo2.ts
CHANGED
package/src/db/mongo3.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Multi cluster safe util
|
|
2
2
|
import { MongoClient, Db, Collection, Document } from 'mongodb'
|
|
3
|
-
import { getSecret } from '../
|
|
3
|
+
import { getSecret } from '../secrets'
|
|
4
4
|
|
|
5
5
|
const clients: Map<string, MongoClient> = new Map()
|
|
6
6
|
const connectPromises: Map<string, Promise<void>> = new Map()
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { connectToMongo, getCollection } from '
|
|
1
|
+
import { connectToMongo, getCollection } from '../../src/db/mongo'
|
|
2
2
|
import { MongoClient, Db } from 'mongodb'
|
|
3
|
-
import * as secretsModule from '
|
|
3
|
+
import * as secretsModule from '../../src/secrets'
|
|
4
4
|
|
|
5
5
|
jest.mock('mongodb')
|
|
6
|
-
jest.mock('
|
|
6
|
+
jest.mock('../../src/secrets')
|
|
7
7
|
|
|
8
8
|
const mockDb = {
|
|
9
9
|
collection: jest.fn()
|
|
@@ -40,7 +40,7 @@ describe('Mongo Utils', () => {
|
|
|
40
40
|
it('should throw error if db is not initialized', () => {
|
|
41
41
|
jest.isolateModules(() => {
|
|
42
42
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
43
|
-
const { getCollection } = require('
|
|
43
|
+
const { getCollection } = require('../../src/db/mongo')
|
|
44
44
|
expect(() => getCollection('users')).toThrow('Mongo not initialized')
|
|
45
45
|
})
|
|
46
46
|
})
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { connectToMongo2, getDb2, getCollection2 } from '
|
|
1
|
+
import { connectToMongo2, getDb2, getCollection2 } from '../../src/db/mongo2'
|
|
2
2
|
import { MongoClient, Db } from 'mongodb'
|
|
3
|
-
import * as secretsModule from '
|
|
3
|
+
import * as secretsModule from '../../src/secrets'
|
|
4
4
|
|
|
5
5
|
jest.mock('mongodb')
|
|
6
|
-
jest.mock('
|
|
6
|
+
jest.mock('../../src/secrets')
|
|
7
7
|
|
|
8
8
|
const mockDb = {
|
|
9
9
|
collection: jest.fn()
|
|
@@ -48,7 +48,7 @@ describe('Mongo Utils', () => {
|
|
|
48
48
|
it('should throw error if client is not initialized (getDb)', () => {
|
|
49
49
|
jest.isolateModules(() => {
|
|
50
50
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
51
|
-
const { getDb2 } = require('
|
|
51
|
+
const { getDb2 } = require('../../src/db/mongo2')
|
|
52
52
|
expect(() => getDb2('test-db')).toThrow('Mongo not initialized')
|
|
53
53
|
})
|
|
54
54
|
})
|
|
@@ -56,7 +56,7 @@ describe('Mongo Utils', () => {
|
|
|
56
56
|
it('should throw error if client is not initialized (getCollection)', () => {
|
|
57
57
|
jest.isolateModules(() => {
|
|
58
58
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
59
|
-
const { getCollection2 } = require('
|
|
59
|
+
const { getCollection2 } = require('../../src/db/mongo2')
|
|
60
60
|
expect(() => getCollection2('test-db', 'users')).toThrow('Mongo not initialized')
|
|
61
61
|
})
|
|
62
62
|
})
|
|
@@ -4,12 +4,12 @@ import {
|
|
|
4
4
|
getCollection3,
|
|
5
5
|
__mongoClients,
|
|
6
6
|
__connectPromises
|
|
7
|
-
} from '
|
|
7
|
+
} from '../../src/db/mongo3'
|
|
8
8
|
import { MongoClient, Db } from 'mongodb'
|
|
9
|
-
import * as secretsModule from '
|
|
9
|
+
import * as secretsModule from '../../src/secrets'
|
|
10
10
|
|
|
11
11
|
jest.mock('mongodb')
|
|
12
|
-
jest.mock('
|
|
12
|
+
jest.mock('../../src/secrets')
|
|
13
13
|
|
|
14
14
|
const mockDb = {
|
|
15
15
|
collection: jest.fn()
|
|
@@ -81,7 +81,7 @@ describe('MongoClientManager (multi-cluster)', () => {
|
|
|
81
81
|
it('should throw error if client not initialized (getDb)', () => {
|
|
82
82
|
jest.isolateModules(() => {
|
|
83
83
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
84
|
-
const { getDb3 } = require('
|
|
84
|
+
const { getDb3 } = require('../../src/db/mongo3')
|
|
85
85
|
expect(() => getDb3('unknown-uri-secret', 'some-db')).toThrow(
|
|
86
86
|
'Mongo client for secret "unknown-uri-secret" not initialized'
|
|
87
87
|
)
|
|
@@ -91,7 +91,7 @@ describe('MongoClientManager (multi-cluster)', () => {
|
|
|
91
91
|
it('should throw error if client not initialized (getCollection)', () => {
|
|
92
92
|
jest.isolateModules(() => {
|
|
93
93
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
94
|
-
const { getCollection3 } = require('
|
|
94
|
+
const { getCollection3 } = require('../../src/db/mongo3')
|
|
95
95
|
expect(() => getCollection3('unknown-uri-secret', 'some-db', 'users')).toThrow(
|
|
96
96
|
'Mongo client for secret "unknown-uri-secret" not initialized'
|
|
97
97
|
)
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import axios from 'axios'
|
|
2
|
-
import { pregenerateInAppWallet } from '
|
|
3
|
-
import * as secrets from '
|
|
2
|
+
import { pregenerateInAppWallet } from '../../src/tw/wallet'
|
|
3
|
+
import * as secrets from '../../src/secrets'
|
|
4
4
|
|
|
5
5
|
jest.mock('axios')
|
|
6
|
-
jest.mock('
|
|
7
|
-
jest.mock('./secrets')
|
|
6
|
+
jest.mock('../../src/secrets')
|
|
8
7
|
|
|
9
8
|
const mockedAxios = axios as jest.Mocked<typeof axios>
|
|
10
9
|
const mockedGetSecret = secrets.getSecret as jest.Mock
|
package/dist/auth/verify.d.ts
DELETED
package/dist/auth/verify.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.verifyRequest = verifyRequest;
|
|
4
|
-
const google_auth_library_1 = require("google-auth-library");
|
|
5
|
-
async function verifyRequest(req, options) {
|
|
6
|
-
const authHeader = req.headers['authorization'];
|
|
7
|
-
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
8
|
-
return false;
|
|
9
|
-
}
|
|
10
|
-
const token = authHeader.split(' ')[1];
|
|
11
|
-
const client = new google_auth_library_1.OAuth2Client();
|
|
12
|
-
try {
|
|
13
|
-
const ticket = await client.verifyIdToken({
|
|
14
|
-
idToken: token,
|
|
15
|
-
audience: options.allowedAudience
|
|
16
|
-
});
|
|
17
|
-
const payload = ticket.getPayload();
|
|
18
|
-
return payload?.email === options.allowedServiceAccount;
|
|
19
|
-
}
|
|
20
|
-
catch {
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/auth/verify.test.js
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const verify_1 = require("./verify");
|
|
4
|
-
// Mock the google-auth-library
|
|
5
|
-
jest.mock('google-auth-library', () => {
|
|
6
|
-
return {
|
|
7
|
-
OAuth2Client: jest.fn().mockImplementation(() => ({
|
|
8
|
-
verifyIdToken: jest.fn(({ idToken, audience }) => {
|
|
9
|
-
if (idToken === 'valid-token' && audience === 'audience') {
|
|
10
|
-
return {
|
|
11
|
-
getPayload: () => ({
|
|
12
|
-
email: 'allowed@example.com'
|
|
13
|
-
})
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
else if (idToken === 'wrong-email-token' && audience === 'audience') {
|
|
17
|
-
return {
|
|
18
|
-
getPayload: () => ({
|
|
19
|
-
email: 'unauthorized@example.com'
|
|
20
|
-
})
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
throw new Error('Invalid token');
|
|
24
|
-
})
|
|
25
|
-
}))
|
|
26
|
-
};
|
|
27
|
-
});
|
|
28
|
-
describe('verifyRequest', () => {
|
|
29
|
-
it('returns true for valid token and matching email', async () => {
|
|
30
|
-
const mockReq = {
|
|
31
|
-
headers: {
|
|
32
|
-
authorization: 'Bearer valid-token'
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
-
const result = await (0, verify_1.verifyRequest)(mockReq, {
|
|
37
|
-
allowedAudience: 'audience',
|
|
38
|
-
allowedServiceAccount: 'allowed@example.com'
|
|
39
|
-
});
|
|
40
|
-
expect(result).toBe(true);
|
|
41
|
-
});
|
|
42
|
-
it('returns false for invalid token', async () => {
|
|
43
|
-
const mockReq = {
|
|
44
|
-
headers: {
|
|
45
|
-
authorization: 'Bearer invalid-token'
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
49
|
-
const result = await (0, verify_1.verifyRequest)(mockReq, {
|
|
50
|
-
allowedAudience: 'audience',
|
|
51
|
-
allowedServiceAccount: 'allowed@example.com'
|
|
52
|
-
});
|
|
53
|
-
expect(result).toBe(false);
|
|
54
|
-
});
|
|
55
|
-
it('returns false for missing authorization header', async () => {
|
|
56
|
-
const mockReq = {
|
|
57
|
-
headers: {}
|
|
58
|
-
};
|
|
59
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
60
|
-
const result = await (0, verify_1.verifyRequest)(mockReq, {
|
|
61
|
-
allowedAudience: 'audience',
|
|
62
|
-
allowedServiceAccount: 'allowed@example.com'
|
|
63
|
-
});
|
|
64
|
-
expect(result).toBe(false);
|
|
65
|
-
});
|
|
66
|
-
it('returns false for incorrect email', async () => {
|
|
67
|
-
const mockReq = {
|
|
68
|
-
headers: {
|
|
69
|
-
authorization: 'Bearer wrong-email-token'
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
73
|
-
const result = await (0, verify_1.verifyRequest)(mockReq, {
|
|
74
|
-
allowedAudience: 'audience',
|
|
75
|
-
allowedServiceAccount: 'allowed@example.com'
|
|
76
|
-
});
|
|
77
|
-
expect(result).toBe(false);
|
|
78
|
-
});
|
|
79
|
-
});
|
package/dist/constants.d.ts
DELETED
package/dist/constants.js
DELETED
package/dist/db/mongo.d.ts
DELETED
package/dist/db/mongo.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.connectToMongo = connectToMongo;
|
|
4
|
-
exports.getCollection = getCollection;
|
|
5
|
-
const mongodb_1 = require("mongodb");
|
|
6
|
-
const secrets_1 = require("../utils/secrets");
|
|
7
|
-
let client;
|
|
8
|
-
let db;
|
|
9
|
-
let connectPromise;
|
|
10
|
-
async function connectToMongo(projectId, uriSecret, dbName) {
|
|
11
|
-
if (db)
|
|
12
|
-
return; // already connected
|
|
13
|
-
if (!connectPromise) {
|
|
14
|
-
connectPromise = (async () => {
|
|
15
|
-
const uri = await (0, secrets_1.getSecret)(projectId, uriSecret);
|
|
16
|
-
client = new mongodb_1.MongoClient(uri);
|
|
17
|
-
await client.connect();
|
|
18
|
-
db = client.db(dbName);
|
|
19
|
-
})();
|
|
20
|
-
}
|
|
21
|
-
await connectPromise;
|
|
22
|
-
}
|
|
23
|
-
function getCollection(name) {
|
|
24
|
-
if (!db)
|
|
25
|
-
throw new Error('Mongo not initialized');
|
|
26
|
-
return db.collection(name);
|
|
27
|
-
}
|
package/dist/db/mongo.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/db/mongo.test.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
const mongo_1 = require("./mongo");
|
|
37
|
-
const mongodb_1 = require("mongodb");
|
|
38
|
-
const secretsModule = __importStar(require("../utils/secrets"));
|
|
39
|
-
jest.mock('mongodb');
|
|
40
|
-
jest.mock('../utils/secrets');
|
|
41
|
-
const mockDb = {
|
|
42
|
-
collection: jest.fn()
|
|
43
|
-
};
|
|
44
|
-
const mockConnect = jest.fn();
|
|
45
|
-
const mockClient = {
|
|
46
|
-
connect: mockConnect,
|
|
47
|
-
db: jest.fn().mockReturnValue(mockDb)
|
|
48
|
-
};
|
|
49
|
-
beforeEach(() => {
|
|
50
|
-
jest.clearAllMocks();
|
|
51
|
-
secretsModule.getSecret.mockResolvedValue('mongodb://mock-uri');
|
|
52
|
-
mongodb_1.MongoClient.mockImplementation(() => mockClient);
|
|
53
|
-
});
|
|
54
|
-
describe('Mongo Utils', () => {
|
|
55
|
-
it('should connect to MongoDB and store client/db', async () => {
|
|
56
|
-
await (0, mongo_1.connectToMongo)('test-project', 'mongo-uri-secret', 'test-db');
|
|
57
|
-
expect(secretsModule.getSecret).toHaveBeenCalledWith('test-project', 'mongo-uri-secret');
|
|
58
|
-
expect(mockConnect).toHaveBeenCalled();
|
|
59
|
-
expect(mockClient.db).toHaveBeenCalledWith('test-db');
|
|
60
|
-
});
|
|
61
|
-
it('should return a collection when db is initialized', async () => {
|
|
62
|
-
await (0, mongo_1.connectToMongo)('test-project', 'mongo-uri-secret', 'test-db');
|
|
63
|
-
(0, mongo_1.getCollection)('users');
|
|
64
|
-
expect(mockDb.collection).toHaveBeenCalledWith('users');
|
|
65
|
-
});
|
|
66
|
-
it('should throw error if db is not initialized', () => {
|
|
67
|
-
jest.isolateModules(() => {
|
|
68
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
69
|
-
const { getCollection } = require('./mongo');
|
|
70
|
-
expect(() => getCollection('users')).toThrow('Mongo not initialized');
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
});
|
package/dist/db/mongo2.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { Db, Collection, Document } from 'mongodb';
|
|
2
|
-
export declare function connectToMongo2(projectId: string, uriSecret: string): Promise<void>;
|
|
3
|
-
export declare function getDb2(dbName: string): Db;
|
|
4
|
-
export declare function getCollection2<T extends Document = Document>(dbName: string, collectionName: string): Collection<T>;
|
package/dist/db/mongo2.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.connectToMongo2 = connectToMongo2;
|
|
4
|
-
exports.getDb2 = getDb2;
|
|
5
|
-
exports.getCollection2 = getCollection2;
|
|
6
|
-
// Multi DB safe util
|
|
7
|
-
const mongodb_1 = require("mongodb");
|
|
8
|
-
const secrets_1 = require("../utils/secrets");
|
|
9
|
-
let client;
|
|
10
|
-
let connectPromise;
|
|
11
|
-
async function connectToMongo2(projectId, uriSecret) {
|
|
12
|
-
if (client)
|
|
13
|
-
return; // already connected
|
|
14
|
-
if (!connectPromise) {
|
|
15
|
-
connectPromise = (async () => {
|
|
16
|
-
const uri = await (0, secrets_1.getSecret)(projectId, uriSecret);
|
|
17
|
-
client = new mongodb_1.MongoClient(uri);
|
|
18
|
-
await client.connect();
|
|
19
|
-
})();
|
|
20
|
-
}
|
|
21
|
-
await connectPromise;
|
|
22
|
-
}
|
|
23
|
-
function getDb2(dbName) {
|
|
24
|
-
if (!client)
|
|
25
|
-
throw new Error('Mongo not initialized');
|
|
26
|
-
return client.db(dbName);
|
|
27
|
-
}
|
|
28
|
-
function getCollection2(dbName, collectionName) {
|
|
29
|
-
return getDb2(dbName).collection(collectionName);
|
|
30
|
-
}
|
package/dist/db/mongo2.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/db/mongo2.test.js
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
const mongo2_1 = require("./mongo2");
|
|
37
|
-
const mongodb_1 = require("mongodb");
|
|
38
|
-
const secretsModule = __importStar(require("../utils/secrets"));
|
|
39
|
-
jest.mock('mongodb');
|
|
40
|
-
jest.mock('../utils/secrets');
|
|
41
|
-
const mockDb = {
|
|
42
|
-
collection: jest.fn()
|
|
43
|
-
};
|
|
44
|
-
const mockConnect = jest.fn();
|
|
45
|
-
const mockClient = {
|
|
46
|
-
connect: mockConnect,
|
|
47
|
-
db: jest.fn().mockReturnValue(mockDb)
|
|
48
|
-
};
|
|
49
|
-
beforeEach(() => {
|
|
50
|
-
jest.clearAllMocks();
|
|
51
|
-
secretsModule.getSecret.mockResolvedValue('mongodb://mock-uri');
|
|
52
|
-
mongodb_1.MongoClient.mockImplementation(() => mockClient);
|
|
53
|
-
});
|
|
54
|
-
describe('Mongo Utils', () => {
|
|
55
|
-
it('should connect to MongoDB and store client', async () => {
|
|
56
|
-
await (0, mongo2_1.connectToMongo2)('test-project', 'mongo-uri-secret');
|
|
57
|
-
expect(secretsModule.getSecret).toHaveBeenCalledWith('test-project', 'mongo-uri-secret');
|
|
58
|
-
expect(mockConnect).toHaveBeenCalled();
|
|
59
|
-
});
|
|
60
|
-
it('should return a Db instance when client is initialized', async () => {
|
|
61
|
-
await (0, mongo2_1.connectToMongo2)('test-project', 'mongo-uri-secret');
|
|
62
|
-
const db = (0, mongo2_1.getDb2)('test-db');
|
|
63
|
-
expect(mockClient.db).toHaveBeenCalledWith('test-db');
|
|
64
|
-
expect(db).toBe(mockDb);
|
|
65
|
-
});
|
|
66
|
-
it('should return a collection when client is initialized', async () => {
|
|
67
|
-
await (0, mongo2_1.connectToMongo2)('test-project', 'mongo-uri-secret');
|
|
68
|
-
(0, mongo2_1.getCollection2)('test-db', 'users');
|
|
69
|
-
expect(mockClient.db).toHaveBeenCalledWith('test-db');
|
|
70
|
-
expect(mockDb.collection).toHaveBeenCalledWith('users');
|
|
71
|
-
});
|
|
72
|
-
it('should throw error if client is not initialized (getDb)', () => {
|
|
73
|
-
jest.isolateModules(() => {
|
|
74
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
75
|
-
const { getDb2 } = require('./mongo2');
|
|
76
|
-
expect(() => getDb2('test-db')).toThrow('Mongo not initialized');
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
it('should throw error if client is not initialized (getCollection)', () => {
|
|
80
|
-
jest.isolateModules(() => {
|
|
81
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
82
|
-
const { getCollection2 } = require('./mongo2');
|
|
83
|
-
expect(() => getCollection2('test-db', 'users')).toThrow('Mongo not initialized');
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
});
|
package/dist/db/mongo3.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { MongoClient, Db, Collection, Document } from 'mongodb';
|
|
2
|
-
export declare function connectToMongo3(projectId: string, uriSecret: string): Promise<MongoClient>;
|
|
3
|
-
export declare function getDb3(uriSecret: string, dbName: string): Db;
|
|
4
|
-
export declare function getCollection3<T extends Document = Document>(uriSecret: string, dbName: string, collectionName: string): Collection<T>;
|
|
5
|
-
export declare const __mongoClients: Map<string, MongoClient>;
|
|
6
|
-
export declare const __connectPromises: Map<string, Promise<void>>;
|