@excofy/utils 2.1.0 → 2.1.2

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/dist/index.cjs CHANGED
@@ -30,6 +30,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
+ ExpiredTokenError: () => ExpiredTokenError,
34
+ InvalidTokenError: () => InvalidTokenError,
33
35
  createValidator: () => createValidator,
34
36
  cryptoUtils: () => cryptoUtils,
35
37
  generateCode: () => generateCode,
@@ -657,8 +659,8 @@ function createValidator() {
657
659
 
658
660
  // src/helpers/generateCode.ts
659
661
  var generateCode = (length = 6, type = "alphanumeric") => {
660
- const digits = "0123456789";
661
- const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
662
+ const digits = "123456789";
663
+ const letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
662
664
  let charset;
663
665
  switch (type) {
664
666
  case "numeric":
@@ -684,6 +686,26 @@ var stringUtils = {
684
686
  removeFileExtension: (fileName) => fileName.replace(/\.[^/.]+$/, "")
685
687
  };
686
688
 
689
+ // src/errors/crypto/CryptoError.ts
690
+ var CryptoError = class extends Error {
691
+ constructor(message) {
692
+ super(message);
693
+ this.name = "CryptoError";
694
+ }
695
+ };
696
+ var InvalidTokenError = class extends CryptoError {
697
+ constructor(message = "Invalid or malformed token") {
698
+ super(message);
699
+ this.name = "InvalidTokenError";
700
+ }
701
+ };
702
+ var ExpiredTokenError = class extends CryptoError {
703
+ constructor(message = "Token has expired") {
704
+ super(message);
705
+ this.name = "ExpiredTokenError";
706
+ }
707
+ };
708
+
687
709
  // src/helpers/crypto.ts
688
710
  var encoder = new TextEncoder();
689
711
  var decoder = new TextDecoder();
@@ -809,11 +831,11 @@ var cryptoUtils = {
809
831
  );
810
832
  if (!valid) throw new Error("Invalid access token");
811
833
  } catch (err) {
812
- throw new Error("Invalid access token");
834
+ throw new InvalidTokenError("Invalid or malformed access token");
813
835
  }
814
836
  const { expiresAt } = JSON.parse(payloadDecrypted);
815
837
  if (Date.now() > new Date(expiresAt).getTime()) {
816
- throw new Error("Expired access token");
838
+ throw new ExpiredTokenError("Access token has expired");
817
839
  }
818
840
  return payloadDecrypted;
819
841
  },
@@ -866,6 +888,8 @@ var toDecimal = (value) => {
866
888
  };
867
889
  // Annotate the CommonJS export names for ESM import in node:
868
890
  0 && (module.exports = {
891
+ ExpiredTokenError,
892
+ InvalidTokenError,
869
893
  createValidator,
870
894
  cryptoUtils,
871
895
  generateCode,
package/dist/index.d.cts CHANGED
@@ -200,4 +200,23 @@ declare namespace number {
200
200
  export { number_toCents as toCents, number_toDecimal as toDecimal };
201
201
  }
202
202
 
203
- export { createValidator, cryptoUtils, generateCode, htmlEntityDecode, number as numberUtils, slug as slugUtils, stringUtils };
203
+ /**
204
+ * Classe base para erros de criptografia
205
+ */
206
+ declare class CryptoError extends Error {
207
+ constructor(message: string);
208
+ }
209
+ /**
210
+ * Token inválido, malformado ou corrompido
211
+ */
212
+ declare class InvalidTokenError extends CryptoError {
213
+ constructor(message?: string);
214
+ }
215
+ /**
216
+ * Token válido mas expirado
217
+ */
218
+ declare class ExpiredTokenError extends CryptoError {
219
+ constructor(message?: string);
220
+ }
221
+
222
+ export { ExpiredTokenError, InvalidTokenError, createValidator, cryptoUtils, generateCode, htmlEntityDecode, number as numberUtils, slug as slugUtils, stringUtils };
package/dist/index.d.ts CHANGED
@@ -200,4 +200,23 @@ declare namespace number {
200
200
  export { number_toCents as toCents, number_toDecimal as toDecimal };
201
201
  }
202
202
 
203
- export { createValidator, cryptoUtils, generateCode, htmlEntityDecode, number as numberUtils, slug as slugUtils, stringUtils };
203
+ /**
204
+ * Classe base para erros de criptografia
205
+ */
206
+ declare class CryptoError extends Error {
207
+ constructor(message: string);
208
+ }
209
+ /**
210
+ * Token inválido, malformado ou corrompido
211
+ */
212
+ declare class InvalidTokenError extends CryptoError {
213
+ constructor(message?: string);
214
+ }
215
+ /**
216
+ * Token válido mas expirado
217
+ */
218
+ declare class ExpiredTokenError extends CryptoError {
219
+ constructor(message?: string);
220
+ }
221
+
222
+ export { ExpiredTokenError, InvalidTokenError, createValidator, cryptoUtils, generateCode, htmlEntityDecode, number as numberUtils, slug as slugUtils, stringUtils };
package/dist/index.js CHANGED
@@ -621,8 +621,8 @@ function createValidator() {
621
621
 
622
622
  // src/helpers/generateCode.ts
623
623
  var generateCode = (length = 6, type = "alphanumeric") => {
624
- const digits = "0123456789";
625
- const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
624
+ const digits = "123456789";
625
+ const letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
626
626
  let charset;
627
627
  switch (type) {
628
628
  case "numeric":
@@ -648,6 +648,26 @@ var stringUtils = {
648
648
  removeFileExtension: (fileName) => fileName.replace(/\.[^/.]+$/, "")
649
649
  };
650
650
 
651
+ // src/errors/crypto/CryptoError.ts
652
+ var CryptoError = class extends Error {
653
+ constructor(message) {
654
+ super(message);
655
+ this.name = "CryptoError";
656
+ }
657
+ };
658
+ var InvalidTokenError = class extends CryptoError {
659
+ constructor(message = "Invalid or malformed token") {
660
+ super(message);
661
+ this.name = "InvalidTokenError";
662
+ }
663
+ };
664
+ var ExpiredTokenError = class extends CryptoError {
665
+ constructor(message = "Token has expired") {
666
+ super(message);
667
+ this.name = "ExpiredTokenError";
668
+ }
669
+ };
670
+
651
671
  // src/helpers/crypto.ts
652
672
  var encoder = new TextEncoder();
653
673
  var decoder = new TextDecoder();
@@ -773,11 +793,11 @@ var cryptoUtils = {
773
793
  );
774
794
  if (!valid) throw new Error("Invalid access token");
775
795
  } catch (err) {
776
- throw new Error("Invalid access token");
796
+ throw new InvalidTokenError("Invalid or malformed access token");
777
797
  }
778
798
  const { expiresAt } = JSON.parse(payloadDecrypted);
779
799
  if (Date.now() > new Date(expiresAt).getTime()) {
780
- throw new Error("Expired access token");
800
+ throw new ExpiredTokenError("Access token has expired");
781
801
  }
782
802
  return payloadDecrypted;
783
803
  },
@@ -829,6 +849,8 @@ var toDecimal = (value) => {
829
849
  return new Big(value).div(100).round(2).toNumber();
830
850
  };
831
851
  export {
852
+ ExpiredTokenError,
853
+ InvalidTokenError,
832
854
  createValidator,
833
855
  cryptoUtils,
834
856
  generateCode,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@excofy/utils",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "Biblioteca de utilitários para o Excofy",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Classe base para erros de criptografia
3
+ */
4
+ export class CryptoError extends Error {
5
+ constructor(message: string) {
6
+ super(message);
7
+ this.name = 'CryptoError';
8
+ }
9
+ }
10
+
11
+ /**
12
+ * Token inválido, malformado ou corrompido
13
+ */
14
+ export class InvalidTokenError extends CryptoError {
15
+ constructor(message: string = 'Invalid or malformed token') {
16
+ super(message);
17
+ this.name = 'InvalidTokenError';
18
+ }
19
+ }
20
+
21
+ /**
22
+ * Token válido mas expirado
23
+ */
24
+ export class ExpiredTokenError extends CryptoError {
25
+ constructor(message: string = 'Token has expired') {
26
+ super(message);
27
+ this.name = 'ExpiredTokenError';
28
+ }
29
+ }
@@ -0,0 +1 @@
1
+ export { InvalidTokenError, ExpiredTokenError } from './crypto/CryptoError';
@@ -1,3 +1,8 @@
1
+ import {
2
+ ExpiredTokenError,
3
+ InvalidTokenError,
4
+ } from '../errors/crypto/CryptoError';
5
+
1
6
  interface IGenerateAccessToken {
2
7
  AUTH_SIGN_SECRET: string;
3
8
  AUTH_PAYLOAD_SECRET: string;
@@ -228,13 +233,13 @@ export const cryptoUtils: ICrypto = {
228
233
  if (!valid) throw new Error('Invalid access token');
229
234
  } catch (err) {
230
235
  // qualquer falha na decriptação ou verificação -> token inválido
231
- throw new Error('Invalid access token');
236
+ throw new InvalidTokenError('Invalid or malformed access token');
232
237
  }
233
238
 
234
239
  // verifica expiração **fora do try/catch**, para não ser capturada como token inválido
235
240
  const { expiresAt } = JSON.parse(payloadDecrypted);
236
241
  if (Date.now() > new Date(expiresAt).getTime()) {
237
- throw new Error('Expired access token');
242
+ throw new ExpiredTokenError('Access token has expired');
238
243
  }
239
244
 
240
245
  return payloadDecrypted;
@@ -18,8 +18,8 @@ export const generateCode = (
18
18
  length = 6,
19
19
  type: 'alphanumeric' | 'numeric' | 'letters' = 'alphanumeric'
20
20
  ): string => {
21
- const digits = '0123456789';
22
- const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
21
+ const digits = '123456789';
22
+ const letters = 'ABCDEFGHIJKLMNPQRSTUVWXYZ';
23
23
  let charset: string;
24
24
 
25
25
  switch (type) {
package/src/index.ts CHANGED
@@ -15,3 +15,6 @@ export {
15
15
  cryptoUtils,
16
16
  generateCode,
17
17
  };
18
+
19
+ // 🚀 Classes de erro para autenticação
20
+ export { InvalidTokenError, ExpiredTokenError } from './errors';