@culturefy/shared 1.0.0 → 1.0.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.
Files changed (115) hide show
  1. package/dist/cjs/enums/index.js +10 -0
  2. package/dist/cjs/enums/index.js.map +1 -0
  3. package/dist/cjs/enums/secretKeys.enum.js +22 -0
  4. package/dist/cjs/enums/secretKeys.enum.js.map +1 -0
  5. package/dist/cjs/index.js +22 -0
  6. package/dist/cjs/index.js.map +1 -0
  7. package/dist/cjs/types/create-enterprise-business.js +2 -0
  8. package/dist/cjs/types/create-enterprise-business.js.map +1 -0
  9. package/dist/cjs/types/events.js +2 -0
  10. package/dist/cjs/types/events.js.map +1 -0
  11. package/dist/cjs/types/index.js +16 -0
  12. package/dist/cjs/types/index.js.map +1 -0
  13. package/dist/cjs/types/payloads.js +2 -0
  14. package/dist/cjs/types/payloads.js.map +1 -0
  15. package/dist/cjs/utils/index.js +22 -0
  16. package/dist/cjs/utils/index.js.map +1 -0
  17. package/dist/cjs/utils/initializers.js +114 -0
  18. package/dist/cjs/utils/initializers.js.map +1 -0
  19. package/dist/cjs/utils/response.js +36 -0
  20. package/dist/cjs/utils/response.js.map +1 -0
  21. package/dist/cjs/utils/secrets.js +42 -0
  22. package/dist/cjs/utils/secrets.js.map +1 -0
  23. package/dist/esm/enums/index.js +2 -0
  24. package/dist/esm/enums/index.js.map +1 -0
  25. package/dist/esm/enums/secretKeys.enum.js +18 -0
  26. package/dist/esm/enums/secretKeys.enum.js.map +1 -0
  27. package/dist/esm/index.js +4 -0
  28. package/dist/esm/index.js.map +1 -0
  29. package/dist/esm/types/create-enterprise-business.js +2 -0
  30. package/dist/esm/types/create-enterprise-business.js.map +1 -0
  31. package/dist/esm/types/events.js +2 -0
  32. package/dist/esm/types/events.js.map +1 -0
  33. package/dist/esm/types/index.js +3 -0
  34. package/dist/esm/types/index.js.map +1 -0
  35. package/dist/esm/types/payloads.js +2 -0
  36. package/dist/esm/types/payloads.js.map +1 -0
  37. package/dist/esm/utils/index.js +4 -0
  38. package/dist/esm/utils/index.js.map +1 -0
  39. package/dist/esm/utils/initializers.js +108 -0
  40. package/dist/esm/utils/initializers.js.map +1 -0
  41. package/dist/esm/utils/response.js +31 -0
  42. package/dist/esm/utils/response.js.map +1 -0
  43. package/dist/esm/utils/secrets.js +39 -0
  44. package/dist/esm/utils/secrets.js.map +1 -0
  45. package/dist/package.json +37 -0
  46. package/dist/src/packages/shared/index.d.ts +3 -0
  47. package/dist/src/packages/shared/index.js +7 -0
  48. package/dist/src/packages/shared/index.js.map +1 -0
  49. package/dist/src/packages/shared/src/enums/index.d.ts +1 -0
  50. package/dist/src/packages/shared/src/enums/index.js +5 -0
  51. package/dist/src/packages/shared/src/enums/index.js.map +1 -0
  52. package/dist/src/packages/shared/src/enums/secretKeys.enum.d.ts +9 -0
  53. package/dist/src/packages/shared/src/enums/secretKeys.enum.js +16 -0
  54. package/dist/src/packages/shared/src/enums/secretKeys.enum.js.map +1 -0
  55. package/dist/src/packages/shared/src/index.d.ts +3 -0
  56. package/dist/src/packages/shared/src/index.js +7 -0
  57. package/dist/src/packages/shared/src/index.js.map +1 -0
  58. package/dist/src/packages/shared/src/types/create-enterprise-business.js +3 -0
  59. package/dist/src/packages/shared/src/types/create-enterprise-business.js.map +1 -0
  60. package/dist/src/packages/shared/src/types/events.js +3 -0
  61. package/dist/src/packages/shared/src/types/events.js.map +1 -0
  62. package/dist/src/packages/shared/src/types/index.js +6 -0
  63. package/dist/src/packages/shared/src/types/index.js.map +1 -0
  64. package/dist/src/packages/shared/src/types/payloads.js +3 -0
  65. package/dist/src/packages/shared/src/types/payloads.js.map +1 -0
  66. package/dist/src/packages/shared/src/utils/index.js +7 -0
  67. package/dist/src/packages/shared/src/utils/index.js.map +1 -0
  68. package/dist/src/packages/shared/src/utils/initializers.js +111 -0
  69. package/dist/src/packages/shared/src/utils/initializers.js.map +1 -0
  70. package/dist/src/packages/shared/src/utils/response.js +34 -0
  71. package/dist/src/packages/shared/src/utils/response.js.map +1 -0
  72. package/dist/src/packages/shared/src/utils/secrets.js +43 -0
  73. package/dist/src/packages/shared/src/utils/secrets.js.map +1 -0
  74. package/dist/src/types/create-enterprise-business.d.ts +27 -0
  75. package/dist/src/types/events.d.ts +5 -0
  76. package/dist/src/types/index.d.ts +2 -0
  77. package/dist/src/types/payloads.d.ts +12 -0
  78. package/dist/src/utils/index.d.ts +3 -0
  79. package/dist/src/utils/initializers.d.ts +7 -0
  80. package/dist/src/utils/response.d.ts +3 -0
  81. package/dist/src/utils/secrets.d.ts +9 -0
  82. package/package.json +15 -5
  83. package/src/enums/index.ts +1 -0
  84. package/src/enums/secretKeys.enum.ts +11 -0
  85. package/src/types/create-enterprise-business.ts +29 -0
  86. package/src/types/events.ts +5 -0
  87. package/src/types/index.ts +2 -0
  88. package/src/types/payloads.ts +12 -0
  89. package/src/utils/index.ts +3 -0
  90. package/src/utils/initializers.ts +131 -0
  91. package/src/utils/response.ts +35 -0
  92. package/src/utils/secrets.ts +45 -0
  93. package/dist/tsconfig.tsbuildinfo +0 -1
  94. /package/dist/{enums → src/enums}/index.d.ts +0 -0
  95. /package/dist/{enums → src/enums}/index.js +0 -0
  96. /package/dist/{enums → src/enums}/secretKeys.enum.d.ts +0 -0
  97. /package/dist/{enums → src/enums}/secretKeys.enum.js +0 -0
  98. /package/dist/{index.d.ts → src/index.d.ts} +0 -0
  99. /package/dist/{index.js → src/index.js} +0 -0
  100. /package/dist/{types → src/packages/shared/src/types}/create-enterprise-business.d.ts +0 -0
  101. /package/dist/{types → src/packages/shared/src/types}/events.d.ts +0 -0
  102. /package/dist/{types → src/packages/shared/src/types}/index.d.ts +0 -0
  103. /package/dist/{types → src/packages/shared/src/types}/payloads.d.ts +0 -0
  104. /package/dist/{utils → src/packages/shared/src/utils}/index.d.ts +0 -0
  105. /package/dist/{utils → src/packages/shared/src/utils}/initializers.d.ts +0 -0
  106. /package/dist/{utils → src/packages/shared/src/utils}/response.d.ts +0 -0
  107. /package/dist/{utils → src/packages/shared/src/utils}/secrets.d.ts +0 -0
  108. /package/dist/{types → src/types}/create-enterprise-business.js +0 -0
  109. /package/dist/{types → src/types}/events.js +0 -0
  110. /package/dist/{types → src/types}/index.js +0 -0
  111. /package/dist/{types → src/types}/payloads.js +0 -0
  112. /package/dist/{utils → src/utils}/index.js +0 -0
  113. /package/dist/{utils → src/utils}/initializers.js +0 -0
  114. /package/dist/{utils → src/utils}/response.js +0 -0
  115. /package/dist/{utils → src/utils}/secrets.js +0 -0
@@ -0,0 +1,3 @@
1
+ export * from './secrets';
2
+ export * from './response';
3
+ export * from './initializers';
@@ -0,0 +1,131 @@
1
+ // packages/libs/src/db-initializers.ts
2
+
3
+ import mongoose, { Connection } from "mongoose";
4
+ import { InvocationContext } from "@azure/functions";
5
+ import { getAzureVaultSecretByKey } from "./secrets";
6
+ import { AzureSecretKeysEnum } from "../enums";
7
+
8
+ export abstract class Initializers {
9
+ protected db!: Connection;
10
+
11
+ constructor(
12
+ context: InvocationContext,
13
+ dbSecret: AzureSecretKeysEnum
14
+ ) {
15
+ MongoConnectionSingleton
16
+ .getInstance(context, dbSecret)
17
+ .then(conn => {
18
+ this.db = conn;
19
+ context.info("✅ MongoDB initialized");
20
+ })
21
+ .catch(err => {
22
+ context.error("❌ Failed to initialize MongoDB:", err);
23
+ throw err;
24
+ });
25
+ }
26
+ }
27
+
28
+ class MongoConnectionSingleton {
29
+ private static instances = new Map<AzureSecretKeysEnum, Connection>();
30
+
31
+ static async getInstance(
32
+ context: InvocationContext,
33
+ secretKey: AzureSecretKeysEnum
34
+ ): Promise<Connection> {
35
+ if (!this.instances.has(secretKey)) {
36
+ const vaultName = process.env.KEY_VAULT_NAME!;
37
+ if (!vaultName) throw new Error("Missing KEY_VAULT_NAME env var");
38
+
39
+ context.info(`[Mongo] fetching secret "${secretKey}" from vault "${vaultName}"`);
40
+ const uri = await getAzureVaultSecretByKey(context, vaultName, secretKey);
41
+ if (!uri) throw new Error(`No value for secret "${secretKey}"`);
42
+
43
+ context.info("[Mongo] connecting to MongoDB…");
44
+ const conn = await mongoose
45
+ .createConnection(uri, {})
46
+ .asPromise();
47
+ context.info("[Mongo] connected to MongoDB");
48
+
49
+ this.instances.set(secretKey, conn);
50
+ }
51
+ return this.instances.get(secretKey)!;
52
+ }
53
+ }
54
+
55
+
56
+
57
+
58
+
59
+ // import mongoose, { Mongoose } from "mongoose";
60
+ // import { InvocationContext } from "@azure/functions";
61
+ // import { getAzureVaultSecretByKey } from "./secrets";
62
+ // import { AzureSecretKeysEnum } from "../enums";
63
+
64
+ // /**
65
+ // * Base class for any service that needs a MongoDB connection.
66
+ // * Pass in the name of the Key Vault secret that holds your MongoDB URI.
67
+ // */
68
+ // export abstract class Initializers {
69
+ // mongoose!: Mongoose;
70
+
71
+ // constructor(
72
+ // context: InvocationContext,
73
+ // dbConnectionSecretKey: AzureSecretKeysEnum
74
+ // ) {
75
+ // MongoConnectionSingleton
76
+ // .getInstance(context, dbConnectionSecretKey)
77
+ // .then(singleton => {
78
+ // this.mongoose = singleton.mongoose;
79
+ // })
80
+ // .catch(err => {
81
+ // context.error("Failed to initialize MongoDB:", err);
82
+ // throw err;
83
+ // });
84
+ // }
85
+ // }
86
+
87
+ // /**
88
+ // * A singleton manager that caches one mongoose connection per secret key.
89
+ // */
90
+ // class MongoConnectionSingleton {
91
+ // private static instances: Map<AzureSecretKeysEnum, MongoConnectionSingleton> = new Map();
92
+ // public mongoose!: Mongoose;
93
+
94
+ // private constructor() { }
95
+
96
+ // public static async getInstance(
97
+ // context: InvocationContext,
98
+ // secretKey: AzureSecretKeysEnum
99
+ // ): Promise<MongoConnectionSingleton> {
100
+ // if (!this.instances.has(secretKey)) {
101
+ // const inst = new MongoConnectionSingleton();
102
+ // await inst.connect(context, secretKey);
103
+ // this.instances.set(secretKey, inst);
104
+ // }
105
+ // return this.instances.get(secretKey)!;
106
+ // }
107
+
108
+ // private async connect(
109
+ // context: InvocationContext,
110
+ // secretKey: AzureSecretKeysEnum
111
+ // ): Promise<void> {
112
+ // // 1) Fetch vault name and secret
113
+ // const vaultName = process.env.AZURE_KEY_VAULT_NAME!;
114
+ // if (!vaultName) throw new Error("Missing KEY_VAULT_NAME env var");
115
+
116
+ // context.info(`[Mongo] Fetching connection string from vault "${vaultName}", secret "${secretKey}"`);
117
+ // const connString = await getAzureVaultSecretByKey(
118
+ // context,
119
+ // vaultName,
120
+ // secretKey
121
+ // );
122
+ // if (!connString) {
123
+ // throw new Error(`No value found for secret "${secretKey}"`);
124
+ // }
125
+
126
+ // // 2) Connect via mongoose
127
+ // context.info(`[Mongo] Connecting to MongoDB…`);
128
+ // this.mongoose = await mongoose.connect(connString);
129
+ // context.info(`[Mongo] Connected using secret "${secretKey}"`);
130
+ // }
131
+ // }
@@ -0,0 +1,35 @@
1
+ import { HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions';
2
+
3
+ // This function generates a standard response with a structured body: { status, message, data }
4
+ export function sendResponse(
5
+ statusCode: number,
6
+ data: any = null,
7
+ message: string = '',
8
+ headers: Record<string, string> = { 'Content-Type': 'application/json' }
9
+ ): HttpResponseInit {
10
+ return {
11
+ status: statusCode,
12
+ body: JSON.stringify({
13
+ status: statusCode,
14
+ message: message || (statusCode >= 200 && statusCode < 300 ? 'Success' : 'Error'),
15
+ data: data,
16
+ }),
17
+ headers: headers,
18
+ };
19
+ }
20
+
21
+ // Centralized asyncHandler to catch all errors
22
+ export function asyncHandler(handler: Function) {
23
+ return async (request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> => {
24
+ try {
25
+ context.log('Handling request:', request.url);
26
+ const response = await handler(request, context); // Call the actual handler logic
27
+ context.log('Response sent:', response.status);
28
+ return response;
29
+ } catch (error: any) {
30
+ console.error('Error occurred:', error);
31
+ context.error('Error occurred:', error);
32
+ return sendResponse(500, null, error.message ? error.message : 'An unexpected error occurred');
33
+ }
34
+ };
35
+ }
@@ -0,0 +1,45 @@
1
+ import { DefaultAzureCredential } from "@azure/identity";
2
+ import { SecretClient } from "@azure/keyvault-secrets";
3
+ import { InvocationContext } from "@azure/functions";
4
+ import { AzureSecretKeysEnum } from "../enums";
5
+
6
+ /**
7
+ * Fetches a secret value from Azure Key Vault by key.
8
+ * @param key - The key name from the AzureSecretKeysEnum enum.
9
+ * @returns The secret value.
10
+ * @throws Error if the key is invalid or if the secret fetch fails.
11
+ */
12
+ export async function getAzureVaultSecretByKey(
13
+ context: InvocationContext,
14
+ azureVaultName: string,
15
+ key: AzureSecretKeysEnum
16
+ ): Promise<string> {
17
+ try {
18
+
19
+ // Azure Key Vault URL
20
+ const vaultName = azureVaultName || "";
21
+ const vaultUrl = `https://${vaultName}.vault.azure.net`;
22
+
23
+ // Initialize SecretClient
24
+ const credential = new DefaultAzureCredential();
25
+ const client = new SecretClient(vaultUrl, credential);
26
+
27
+ // Validate the key using a switch case
28
+ switch (key) {
29
+
30
+ case AzureSecretKeysEnum.STRIPE_SECRET_KEY:
31
+ case AzureSecretKeysEnum.STRIPE_PAYMENT_WEBHOOK_SECRET_KEY:
32
+ case AzureSecretKeysEnum.STRIPE_PRODUCT_WEBHOOK_SECRET:
33
+ case AzureSecretKeysEnum.STRIPE_PRICE_WEBHOOK_SECRET:
34
+ case AzureSecretKeysEnum.EMAIL_SERVICE_URL:
35
+ case AzureSecretKeysEnum.STRIPE_CUSTOMER_SYNC_WEBHOOK_SECRET_KEY:
36
+ const secret = await client.getSecret(key);
37
+ return secret.value || "";
38
+ default:
39
+ throw new Error(`Invalid key name: ${key}`);
40
+ }
41
+ } catch (err) {
42
+ context.error(`Error fetching secret for key ${key}:`, err);
43
+ throw new Error(`Failed to fetch secret for key ${key}`);
44
+ }
45
+ }
@@ -1 +0,0 @@
1
- {"root":["../src/index.ts","../src/enums/index.ts","../src/enums/secretkeys.enum.ts","../src/types/create-enterprise-business.ts","../src/types/events.ts","../src/types/index.ts","../src/types/payloads.ts","../src/utils/index.ts","../src/utils/initializers.ts","../src/utils/response.ts","../src/utils/secrets.ts"],"version":"5.8.3"}
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes