fiberx-backend-toolkit 0.0.64 → 0.0.66

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.
@@ -24,7 +24,7 @@ const serializeSequelizeDataType = (type) => {
24
24
  }
25
25
  return "undefined";
26
26
  };
27
- const serializeSequelizeColumnDefinition = (column) => {
27
+ const serializeSequelizeColumnDefinition = (column, indent = 4) => {
28
28
  const parts = [];
29
29
  for (const [key, value] of Object.entries(column)) {
30
30
  if (key === "type") {
@@ -44,8 +44,9 @@ const serializeSequelizeColumnDefinition = (column) => {
44
44
  }
45
45
  }
46
46
  return `{
47
- ${indentBlock(parts.join(",\n"), 1)}
48
- }`;
47
+ ${indentBlock(parts.join(",\n"), indent)}
48
+ ${indentBlock("}", indent === 1 ? 0 : 3)}
49
+ `;
49
50
  };
50
51
  const serializeIndexDefinition = (index) => {
51
52
  const clone = { ...index };
@@ -60,13 +61,14 @@ const serializeIndexDefinition = (index) => {
60
61
  const serializeSequelizeColumnsObject = (columns) => {
61
62
  const rendered = Object.entries(columns)
62
63
  .map(([col_name, col_def]) => {
63
- const col_code = serializeSequelizeColumnDefinition(col_def);
64
+ const col_code = serializeSequelizeColumnDefinition(col_def, 1);
64
65
  return `"${col_name}": ${col_code}`;
65
66
  })
66
67
  .join(",\n");
67
68
  return `{
68
- ${indentBlock(rendered, 1)}
69
- }`;
69
+ ${indentBlock(rendered, 4)}
70
+ ${indentBlock("}", 3)}
71
+ `;
70
72
  };
71
73
  const serializeSequelizeIndexesArray = (indexes) => {
72
74
  return indexes.map(index => {
@@ -142,17 +144,16 @@ class Create${schema_model_name}TableMigration {
142
144
 
143
145
  await queryInterface.createTable(
144
146
  table_name,
145
- ${indentBlock(rendered_columns, 4)}
147
+ ${indentBlock(rendered_columns, 1)}
146
148
  );
147
149
 
148
-
149
150
  ${schema_definition.indexes?.map(index => `
150
151
  await queryInterface.addIndex(
151
152
  table_name,
152
153
  ${JSON.stringify(index.fields)},
153
154
  {
154
155
  name: "${index.name}",
155
- ${indentBlock(serializeIndexDefinition(index), 2)}
156
+ ${indentBlock(serializeIndexDefinition(index), 1)}
156
157
  }
157
158
  );
158
159
  `).join("")}
@@ -182,7 +183,7 @@ export default Create${schema_model_name}TableMigration;
182
183
  exports.SEQUELIZE_CREATE_NEW_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE = SEQUELIZE_CREATE_NEW_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE;
183
184
  const SEQUELIZE_UPDATE_EXISTING_SCHEMA_MIGRATION_FILE_CODE_TEMPLATE = (table_name, model_name, diff, current_schema, previous_schema) => {
184
185
  return `
185
- import { QueryInterface, Sequelize } from "sequelize";
186
+ import { QueryInterface, Sequelize, DataTypes } from "sequelize";
186
187
  import { LoggerUtil } from "fiberx-backend-toolkit/dist/utils/main";
187
188
 
188
189
  class Update${model_name}TableMigration {
@@ -208,7 +209,7 @@ class Update${model_name}TableMigration {
208
209
  await queryInterface.addColumn(
209
210
  table_name,
210
211
  "${col}",
211
- ${indentBlock(serializeSequelizeColumnDefinition(current_schema.columns[col]), 1)}
212
+ ${indentBlock(serializeSequelizeColumnDefinition(current_schema.columns[col]), 1)}
212
213
  );
213
214
  `).join("")}
214
215
 
@@ -217,7 +218,7 @@ class Update${model_name}TableMigration {
217
218
  await queryInterface.changeColumn(
218
219
  table_name,
219
220
  "${col}",
220
- ${indentBlock(serializeSequelizeColumnDefinition(current_schema.columns[col]), 1)}
221
+ ${indentBlock(serializeSequelizeColumnDefinition(current_schema.columns[col]), 1)}
221
222
  );
222
223
  `).join("")}
223
224
 
@@ -239,7 +240,7 @@ class Update${model_name}TableMigration {
239
240
  ${JSON.stringify(idx.fields)},
240
241
  {
241
242
  name: "${idx.name}",
242
- ${indentBlock(serializeIndexDefinition(idx).replace(/^{|}$/g, ""), 2)}
243
+ ${indentBlock(serializeIndexDefinition(idx).replace(/^{|}$/g, ""), 1)}
243
244
  }
244
245
  );
245
246
  ` : "";
@@ -257,7 +258,7 @@ class Update${model_name}TableMigration {
257
258
  ${JSON.stringify(idx.fields)},
258
259
  {
259
260
  name: "${idx.name}",
260
- ${indentBlock(serializeIndexDefinition(idx).replace(/^{|}$/g, ""), 2)}
261
+ ${indentBlock(serializeIndexDefinition(idx).replace(/^{|}$/g, ""), 1)}
261
262
  }
262
263
  );
263
264
  ` : "";
@@ -296,7 +297,7 @@ class Update${model_name}TableMigration {
296
297
  ${JSON.stringify(idx.fields)},
297
298
  {
298
299
  name: "${idx.name}",
299
- ${indentBlock(serializeIndexDefinition(idx).replace(/^{|}$/g, ""), 2)}
300
+ ${indentBlock(serializeIndexDefinition(idx).replace(/^{|}$/g, ""), 1)}
300
301
  }
301
302
  );
302
303
  ` : "";
@@ -311,7 +312,7 @@ class Update${model_name}TableMigration {
311
312
  ${JSON.stringify(idx.fields)},
312
313
  {
313
314
  name: "${idx.name}",
314
- ${indentBlock(serializeIndexDefinition(idx).replace(/^{|}$/g, ""), 2)}
315
+ ${indentBlock(serializeIndexDefinition(idx).replace(/^{|}$/g, ""), 1)}
315
316
  }
316
317
  );
317
318
  ` : "";
@@ -45,20 +45,20 @@ class SchemaDiffUtil {
45
45
  // Indexes
46
46
  const prev_indexes = prev?.indexes || [];
47
47
  const curr_indexes = curr?.indexes || [];
48
- const prev_map = new Map(prev_indexes.map(i => [i.name, i]));
49
- const curr_map = new Map(curr_indexes.map(i => [i.name, i]));
50
- for (const [name, curr_idx] of curr_map) {
51
- const prev_idx = prev_map.get(name);
48
+ const prev_map = new Map(prev_indexes.map(i => [SchemaDiffUtil.indexKey(i), i]));
49
+ const curr_map = new Map(curr_indexes.map(i => [SchemaDiffUtil.indexKey(i), i]));
50
+ for (const [key, curr_idx] of curr_map) {
51
+ const prev_idx = prev_map.get(key);
52
52
  if (!prev_idx) {
53
- diff.indexes.added.push(name);
53
+ diff.indexes.added.push(curr_idx.name);
54
54
  }
55
55
  else if (!input_validator_util_1.default.isDeepEqual(prev_idx, curr_idx)) {
56
- diff.indexes.modified.push(name);
56
+ diff.indexes.modified.push(curr_idx.name);
57
57
  }
58
58
  }
59
- for (const [name] of prev_map) {
60
- if (!curr_map.has(name)) {
61
- diff.indexes.removed.push(name);
59
+ for (const [key, prev_idx] of prev_map) {
60
+ if (!curr_map.has(key)) {
61
+ diff.indexes.removed.push(prev_idx.name);
62
62
  }
63
63
  }
64
64
  return diff;
@@ -2,7 +2,8 @@ import { GenerateSecretOptionsType, VerifyResultType, VerifyOptionsType, TOTPSer
2
2
  declare class TOTPServiceUtil {
3
3
  readonly name = "totp_service_util";
4
4
  private static instance;
5
- private logger;
5
+ private readonly logger;
6
+ private readonly env_manager;
6
7
  private readonly STEP;
7
8
  private readonly DIGITS;
8
9
  private readonly WINDOW;
@@ -14,6 +15,7 @@ declare class TOTPServiceUtil {
14
15
  private base32Encode;
15
16
  private generateForCounter;
16
17
  generateSecret(options: GenerateSecretOptionsType): GenerateSecretResult;
18
+ generateAppSecret(member_email: string, custom_app_name?: string): GenerateSecretResult;
17
19
  verify(options: VerifyOptionsType & {
18
20
  last_used_counter?: number;
19
21
  }): VerifyResultType;
@@ -7,10 +7,12 @@ const crypto_1 = __importDefault(require("crypto"));
7
7
  const constants_1 = require("../config/constants");
8
8
  const logger_util_1 = __importDefault(require("./logger_util"));
9
9
  const input_validator_util_1 = __importDefault(require("./input_validator_util"));
10
+ const env_manager_util_1 = __importDefault(require("./env_manager_util"));
10
11
  class TOTPServiceUtil {
11
12
  name = "totp_service_util";
12
13
  static instance;
13
14
  logger = new logger_util_1.default(this.name);
15
+ env_manager = env_manager_util_1.default.getInstance();
14
16
  // ===============================
15
17
  // CONFIG
16
18
  // ===============================
@@ -117,6 +119,12 @@ class TOTPServiceUtil {
117
119
  otpauth_url,
118
120
  };
119
121
  }
122
+ generateAppSecret(member_email, custom_app_name = "") {
123
+ const app_name = this.env_manager.getEnvVar("APP_NAME", custom_app_name) ?? custom_app_name;
124
+ const name = input_validator_util_1.default.isProduction() ? `${app_name}:${member_email}` : `Dev ${app_name}:${member_email}`;
125
+ const issuer = (this.env_manager.getEnvVar("APP_ID", custom_app_name) ?? custom_app_name)?.toLowerCase();
126
+ return this.generateSecret({ name, issuer });
127
+ }
120
128
  // ===============================
121
129
  // PUBLIC: VERIFY TOKEN
122
130
  // ===============================
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fiberx-backend-toolkit",
3
- "version": "0.0.64",
3
+ "version": "0.0.66",
4
4
  "description": "A TypeScript backend toolkit providing shared domain logic, infrastructure helpers, and utilities for FiberX server-side applications and services.",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",