opticedge-cloud-utils 1.0.44 → 1.1.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.
Files changed (86) hide show
  1. package/dist/db/mongo.js +1 -1
  2. package/dist/db/mongo2.js +1 -1
  3. package/dist/db/mongo3.js +1 -1
  4. package/dist/index.d.ts +7 -14
  5. package/dist/index.js +7 -14
  6. package/dist/{utils → tw}/tw-wallet.js +1 -1
  7. package/package.json +5 -4
  8. package/src/db/mongo.ts +1 -1
  9. package/src/db/mongo2.ts +1 -1
  10. package/src/db/mongo3.ts +1 -1
  11. package/src/index.ts +7 -14
  12. package/src/{utils → tw}/tw-wallet.ts +1 -1
  13. package/{src/auth/verify.test.ts → tests/auth.test.ts} +1 -1
  14. package/{src → tests}/db/mongo.test.ts +4 -4
  15. package/{src → tests}/db/mongo2.test.ts +5 -5
  16. package/{src → tests}/db/mongo3.test.ts +5 -5
  17. package/{src/utils → tests}/env.test.ts +1 -1
  18. package/{src/utils → tests}/regex.test.ts +1 -1
  19. package/{src/utils → tests}/secrets.test.ts +1 -1
  20. package/{src/utils → tests}/task.test.ts +1 -1
  21. package/{src/utils → tests/tw}/tw-utils.test.ts +1 -1
  22. package/{src/utils → tests/tw}/tw-wallet.test.ts +3 -4
  23. package/dist/auth/verify.test.d.ts +0 -1
  24. package/dist/auth/verify.test.js +0 -79
  25. package/dist/constants.d.ts +0 -2
  26. package/dist/constants.js +0 -7
  27. package/dist/db/mongo.test.d.ts +0 -1
  28. package/dist/db/mongo.test.js +0 -73
  29. package/dist/db/mongo2.test.d.ts +0 -1
  30. package/dist/db/mongo2.test.js +0 -86
  31. package/dist/db/mongo3.test.d.ts +0 -1
  32. package/dist/db/mongo3.test.js +0 -108
  33. package/dist/types/card.d.ts +0 -78
  34. package/dist/types/card.js +0 -18
  35. package/dist/types/notify.d.ts +0 -4
  36. package/dist/types/notify.js +0 -2
  37. package/dist/types/payment.d.ts +0 -1
  38. package/dist/types/payment.js +0 -2
  39. package/dist/types/pokemontcg.d.ts +0 -158
  40. package/dist/types/pokemontcg.js +0 -2
  41. package/dist/types/pricecharting.d.ts +0 -40
  42. package/dist/types/pricecharting.js +0 -2
  43. package/dist/types/user.d.ts +0 -37
  44. package/dist/types/user.js +0 -10
  45. package/dist/utils/env.test.d.ts +0 -1
  46. package/dist/utils/env.test.js +0 -17
  47. package/dist/utils/regex.test.d.ts +0 -1
  48. package/dist/utils/regex.test.js +0 -22
  49. package/dist/utils/secrets.test.d.ts +0 -1
  50. package/dist/utils/secrets.test.js +0 -38
  51. package/dist/utils/task.test.d.ts +0 -1
  52. package/dist/utils/task.test.js +0 -67
  53. package/dist/utils/thirdweb.d.ts +0 -1
  54. package/dist/utils/thirdweb.js +0 -11
  55. package/dist/utils/thirdweb.test.d.ts +0 -1
  56. package/dist/utils/thirdweb.test.js +0 -26
  57. package/dist/utils/tw-utils.test.d.ts +0 -1
  58. package/dist/utils/tw-utils.test.js +0 -26
  59. package/dist/utils/tw-wallet.test.d.ts +0 -1
  60. package/dist/utils/tw-wallet.test.js +0 -94
  61. package/src/constants.ts +0 -6
  62. package/src/types/card.ts +0 -81
  63. package/src/types/notify.ts +0 -4
  64. package/src/types/payment.ts +0 -1
  65. package/src/types/pokemontcg.ts +0 -162
  66. package/src/types/pricecharting.ts +0 -42
  67. package/src/types/user.ts +0 -38
  68. /package/dist/{auth/verify.d.ts → auth.d.ts} +0 -0
  69. /package/dist/{auth/verify.js → auth.js} +0 -0
  70. /package/dist/{utils/env.d.ts → env.d.ts} +0 -0
  71. /package/dist/{utils/env.js → env.js} +0 -0
  72. /package/dist/{utils/regex.d.ts → regex.d.ts} +0 -0
  73. /package/dist/{utils/regex.js → regex.js} +0 -0
  74. /package/dist/{utils/secrets.d.ts → secrets.d.ts} +0 -0
  75. /package/dist/{utils/secrets.js → secrets.js} +0 -0
  76. /package/dist/{utils/task.d.ts → task.d.ts} +0 -0
  77. /package/dist/{utils/task.js → task.js} +0 -0
  78. /package/dist/{utils → tw}/tw-utils.d.ts +0 -0
  79. /package/dist/{utils → tw}/tw-utils.js +0 -0
  80. /package/dist/{utils → tw}/tw-wallet.d.ts +0 -0
  81. /package/src/{auth/verify.ts → auth.ts} +0 -0
  82. /package/src/{utils/env.ts → env.ts} +0 -0
  83. /package/src/{utils/regex.ts → regex.ts} +0 -0
  84. /package/src/{utils/secrets.ts → secrets.ts} +0 -0
  85. /package/src/{utils/task.ts → task.ts} +0 -0
  86. /package/src/{utils → tw}/tw-utils.ts +0 -0
package/dist/db/mongo.js CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.connectToMongo = connectToMongo;
4
4
  exports.getCollection = getCollection;
5
5
  const mongodb_1 = require("mongodb");
6
- const secrets_1 = require("../utils/secrets");
6
+ const secrets_1 = require("../secrets");
7
7
  let client;
8
8
  let db;
9
9
  let connectPromise;
package/dist/db/mongo2.js CHANGED
@@ -5,7 +5,7 @@ exports.getDb2 = getDb2;
5
5
  exports.getCollection2 = getCollection2;
6
6
  // Multi DB safe util
7
7
  const mongodb_1 = require("mongodb");
8
- const secrets_1 = require("../utils/secrets");
8
+ const secrets_1 = require("../secrets");
9
9
  let client;
10
10
  let connectPromise;
11
11
  async function connectToMongo2(projectId, uriSecret) {
package/dist/db/mongo3.js CHANGED
@@ -6,7 +6,7 @@ exports.getDb3 = getDb3;
6
6
  exports.getCollection3 = getCollection3;
7
7
  // Multi cluster safe util
8
8
  const mongodb_1 = require("mongodb");
9
- const secrets_1 = require("../utils/secrets");
9
+ const secrets_1 = require("../secrets");
10
10
  const clients = new Map();
11
11
  const connectPromises = new Map();
12
12
  async function connectToMongo3(projectId, uriSecret) {
package/dist/index.d.ts CHANGED
@@ -1,17 +1,10 @@
1
- export * from './auth/verify';
2
1
  export * from './db/mongo';
3
2
  export * from './db/mongo2';
4
3
  export * from './db/mongo3';
5
- export * from './types/card';
6
- export * from './types/notify';
7
- export * from './types/payment';
8
- export * from './types/pokemontcg';
9
- export * from './types/pricecharting';
10
- export * from './types/user';
11
- export * from './utils/env';
12
- export * from './utils/regex';
13
- export * from './utils/secrets';
14
- export * from './utils/task';
15
- export * from './utils/tw-utils';
16
- export * from './utils/tw-wallet';
17
- export * from './constants';
4
+ export * from './auth';
5
+ export * from './env';
6
+ export * from './regex';
7
+ export * from './secrets';
8
+ export * from './task';
9
+ export * from './tw/tw-utils';
10
+ export * from './tw/tw-wallet';
package/dist/index.js CHANGED
@@ -14,20 +14,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./auth/verify"), exports);
18
17
  __exportStar(require("./db/mongo"), exports);
19
18
  __exportStar(require("./db/mongo2"), exports);
20
19
  __exportStar(require("./db/mongo3"), exports);
21
- __exportStar(require("./types/card"), exports);
22
- __exportStar(require("./types/notify"), exports);
23
- __exportStar(require("./types/payment"), exports);
24
- __exportStar(require("./types/pokemontcg"), exports);
25
- __exportStar(require("./types/pricecharting"), exports);
26
- __exportStar(require("./types/user"), exports);
27
- __exportStar(require("./utils/env"), exports);
28
- __exportStar(require("./utils/regex"), exports);
29
- __exportStar(require("./utils/secrets"), exports);
30
- __exportStar(require("./utils/task"), exports);
31
- __exportStar(require("./utils/tw-utils"), exports);
32
- __exportStar(require("./utils/tw-wallet"), exports);
33
- __exportStar(require("./constants"), exports);
20
+ __exportStar(require("./auth"), exports);
21
+ __exportStar(require("./env"), exports);
22
+ __exportStar(require("./regex"), exports);
23
+ __exportStar(require("./secrets"), exports);
24
+ __exportStar(require("./task"), exports);
25
+ __exportStar(require("./tw/tw-utils"), exports);
26
+ __exportStar(require("./tw/tw-wallet"), exports);
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.pregenerateInAppWallet = pregenerateInAppWallet;
7
7
  const axios_1 = __importDefault(require("axios"));
8
- const secrets_1 = require("./secrets");
8
+ const secrets_1 = require("../secrets");
9
9
  async function pregenerateInAppWallet(projectId, twClientId, twSecretKeyName, email) {
10
10
  const TW_SECRET_KEY = await (0, secrets_1.getSecret)(projectId, twSecretKeyName);
11
11
  try {
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "opticedge-cloud-utils",
3
- "version": "1.0.44",
3
+ "version": "1.1.0",
4
4
  "description": "Common utilities for cloud functions",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
8
- "build": "tsc",
8
+ "clean": "rimraf dist coverage *.tsbuildinfo",
9
+ "build": "npm run clean && tsc",
10
+ "prepare": "npm run build",
9
11
  "test": "jest --coverage",
10
12
  "lint": "eslint . --ext .ts --fix",
11
13
  "format": "prettier --write .",
12
- "fix": "npm run lint && npm run format",
13
- "prepare": "npm run build"
14
+ "fix": "npm run lint && npm run format"
14
15
  },
15
16
  "author": "Evans Musonda",
16
17
  "license": "MIT",
package/src/db/mongo.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { MongoClient, Db, Collection, Document } from 'mongodb'
2
- import { getSecret } from '../utils/secrets'
2
+ import { getSecret } from '../secrets'
3
3
 
4
4
  let client: MongoClient
5
5
  let db: Db
package/src/db/mongo2.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  // Multi DB safe util
2
2
  import { MongoClient, Db, Collection, Document } from 'mongodb'
3
- import { getSecret } from '../utils/secrets'
3
+ import { getSecret } from '../secrets'
4
4
 
5
5
  let client: MongoClient
6
6
  let connectPromise: Promise<void> | undefined
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 '../utils/secrets'
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()
package/src/index.ts CHANGED
@@ -1,17 +1,10 @@
1
- export * from './auth/verify'
2
1
  export * from './db/mongo'
3
2
  export * from './db/mongo2'
4
3
  export * from './db/mongo3'
5
- export * from './types/card'
6
- export * from './types/notify'
7
- export * from './types/payment'
8
- export * from './types/pokemontcg'
9
- export * from './types/pricecharting'
10
- export * from './types/user'
11
- export * from './utils/env'
12
- export * from './utils/regex'
13
- export * from './utils/secrets'
14
- export * from './utils/task'
15
- export * from './utils/tw-utils'
16
- export * from './utils/tw-wallet'
17
- export * from './constants'
4
+ export * from './auth'
5
+ export * from './env'
6
+ export * from './regex'
7
+ export * from './secrets'
8
+ export * from './task'
9
+ export * from './tw/tw-utils'
10
+ export * from './tw/tw-wallet'
@@ -1,5 +1,5 @@
1
1
  import axios from 'axios'
2
- import { getSecret } from './secrets'
2
+ import { getSecret } from '../secrets'
3
3
 
4
4
  export async function pregenerateInAppWallet(
5
5
  projectId: string,
@@ -1,4 +1,4 @@
1
- import { verifyRequest } from './verify'
1
+ import { verifyRequest } from '../src/auth'
2
2
 
3
3
  // Mock the google-auth-library
4
4
  jest.mock('google-auth-library', () => {
@@ -1,9 +1,9 @@
1
- import { connectToMongo, getCollection } from './mongo'
1
+ import { connectToMongo, getCollection } from '../../src/db/mongo'
2
2
  import { MongoClient, Db } from 'mongodb'
3
- import * as secretsModule from '../utils/secrets'
3
+ import * as secretsModule from '../../src/secrets'
4
4
 
5
5
  jest.mock('mongodb')
6
- jest.mock('../utils/secrets')
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('./mongo')
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 './mongo2'
1
+ import { connectToMongo2, getDb2, getCollection2 } from '../../src/db/mongo2'
2
2
  import { MongoClient, Db } from 'mongodb'
3
- import * as secretsModule from '../utils/secrets'
3
+ import * as secretsModule from '../../src/secrets'
4
4
 
5
5
  jest.mock('mongodb')
6
- jest.mock('../utils/secrets')
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('./mongo2')
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('./mongo2')
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 './mongo3'
7
+ } from '../../src/db/mongo3'
8
8
  import { MongoClient, Db } from 'mongodb'
9
- import * as secretsModule from '../utils/secrets'
9
+ import * as secretsModule from '../../src/secrets'
10
10
 
11
11
  jest.mock('mongodb')
12
- jest.mock('../utils/secrets')
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('./mongo3')
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('./mongo3')
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,4 +1,4 @@
1
- import { getEnv } from './env'
1
+ import { getEnv } from '../src/env'
2
2
 
3
3
  describe('getEnv', () => {
4
4
  it('returns the value of an existing env var', () => {
@@ -1,4 +1,4 @@
1
- import { escapeRegex } from './regex'
1
+ import { escapeRegex } from '../src/regex'
2
2
 
3
3
  describe('escapeRegex', () => {
4
4
  it('escapes regex special characters', () => {
@@ -1,4 +1,4 @@
1
- import { getSecret } from './secrets'
1
+ import { getSecret } from '../src/secrets'
2
2
  import { SecretManagerServiceClient } from '@google-cloud/secret-manager'
3
3
 
4
4
  jest.mock('@google-cloud/secret-manager')
@@ -25,7 +25,7 @@ jest.mock('@google-cloud/tasks', () => {
25
25
  }
26
26
  })
27
27
 
28
- import { createTask } from './task'
28
+ import { createTask } from '../src/task'
29
29
 
30
30
  describe('createTask', () => {
31
31
  beforeEach(() => {
@@ -1,4 +1,4 @@
1
- import { isValidWebhookSignature } from './tw-utils'
1
+ import { isValidWebhookSignature } from '../../src/tw/tw-utils'
2
2
  import crypto from 'crypto'
3
3
 
4
4
  describe('isValidWebhookSignature', () => {
@@ -1,10 +1,9 @@
1
1
  import axios from 'axios'
2
- import { pregenerateInAppWallet } from './tw-wallet'
3
- import * as secrets from './secrets'
2
+ import { pregenerateInAppWallet } from '../../src/tw/tw-wallet'
3
+ import * as secrets from '../../src/secrets'
4
4
 
5
5
  jest.mock('axios')
6
- jest.mock('opticedge-cloud-utils')
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
@@ -1 +0,0 @@
1
- export {};
@@ -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
- });
@@ -1,2 +0,0 @@
1
- import { PaymentEnvironment } from './types/payment';
2
- export declare const CHAIN_PAYMENTENV_MAP: Record<string, PaymentEnvironment>;
package/dist/constants.js DELETED
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CHAIN_PAYMENTENV_MAP = void 0;
4
- exports.CHAIN_PAYMENTENV_MAP = {
5
- '80002': 'SANDBOX',
6
- '137': 'PRODUCTION'
7
- };
@@ -1 +0,0 @@
1
- export {};
@@ -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
- });
@@ -1 +0,0 @@
1
- export {};
@@ -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
- });
@@ -1 +0,0 @@
1
- export {};