@codeleap/auth 6.3.0 → 7.0.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.
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Baseline error-code-to-message map shared across all apps.
3
+ * `social` keys are intentionally null — canceled/unspecified social flows are silently swallowed rather than surfaced to the user.
4
+ */
5
+ export declare const DEFAULT_AUTH_ERRORS: {
6
+ 'auth/wrong-password': string;
7
+ 'auth/not-registered': string;
8
+ 'auth/requires-recent-login': string;
9
+ 'auth/invalid-login-credentials': string;
10
+ 'auth/too-many-requests': string;
11
+ 'auth/email-in-use': string;
12
+ 'auth/email-not-found': string;
13
+ 'auth/email-already-in-use': string;
14
+ 'auth/invalid-email': string;
15
+ 'auth/user-disabled': string;
16
+ 'auth/user-not-found': string;
17
+ 'auth/missing-email': string;
18
+ social: {
19
+ '12501': null;
20
+ EUNSPECIFIED: null;
21
+ 'facebook-login-canceled': null;
22
+ '-5': null;
23
+ '1001': null;
24
+ };
25
+ };
26
+ //# sourceMappingURL=data.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"data.d.ts","sourceRoot":"","sources":["../../src/AuthErrors/data.ts"],"names":[],"mappings":"AACA;;;GAGG;AACH,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;;;;;;;;CAqB/B,CAAA"}
@@ -0,0 +1,51 @@
1
+ import { AnyFunction } from '@codeleap/types';
2
+ import { DEFAULT_AUTH_ERRORS } from './data';
3
+ import { DefaultAuthError, Err, TAuthError } from './types';
4
+ export * from './types';
5
+ /** Internal Error subclass that carries a typed `code` alongside the human-readable `msg` factory; not exported — consumers interact through `AppAuthErrors`. */
6
+ declare class AuthError extends Error {
7
+ code: TAuthError['code'];
8
+ msg: TAuthError['msg'];
9
+ constructor(message: TAuthError['msg'], code: TAuthError['code']);
10
+ }
11
+ type ErrorHandler<E> = (err: E | null, module?: string, args?: any) => void;
12
+ /**
13
+ * Registry that merges app-specific error codes with the built-in defaults and wires them to a single `onError` handler.
14
+ * Social errors are kept in a separate namespace because their codes can collide with base error codes (e.g. numeric strings).
15
+ */
16
+ export declare class AppAuthErrors<T extends Record<string, string | object | AnyFunction>, F extends ErrorHandler<AuthError>> {
17
+ errors: Omit<Record<keyof T | keyof typeof DEFAULT_AUTH_ERRORS, AuthError>, 'social'>;
18
+ social: Omit<Record<keyof T['social'] | keyof typeof DEFAULT_AUTH_ERRORS['social'], AuthError>, 'social'>;
19
+ onError: ErrorHandler<Err>;
20
+ defaultErrors: {
21
+ 'auth/wrong-password': string;
22
+ 'auth/not-registered': string;
23
+ 'auth/requires-recent-login': string;
24
+ 'auth/invalid-login-credentials': string;
25
+ 'auth/too-many-requests': string;
26
+ 'auth/email-in-use': string;
27
+ 'auth/email-not-found': string;
28
+ 'auth/email-already-in-use': string;
29
+ 'auth/invalid-email': string;
30
+ 'auth/user-disabled': string;
31
+ 'auth/user-not-found': string;
32
+ 'auth/missing-email': string;
33
+ social: {
34
+ '12501': null;
35
+ EUNSPECIFIED: null;
36
+ 'facebook-login-canceled': null;
37
+ '-5': null;
38
+ '1001': null;
39
+ };
40
+ };
41
+ private socialKey;
42
+ constructor(newErrors: T, errorFunction: F);
43
+ getError(err: Err): AuthError | null;
44
+ isError(err: Err, key: Exclude<keyof T | DefaultAuthError, 'social'>): boolean;
45
+ verifyError(err: Err): boolean;
46
+ exception(err: Err): void | AuthError;
47
+ createAuthError(message: TAuthError['msg'], code: TAuthError['code']): AuthError;
48
+ private registryErrors;
49
+ private registryErrorFunction;
50
+ }
51
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/AuthErrors/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,EAAE,mBAAmB,EAAE,MAAM,QAAQ,CAAA;AAC5C,OAAO,EAAE,gBAAgB,EAAE,GAAG,EAAE,UA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;IAMpE,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO;IAI9B,SAAS,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI,GAAG,SAAS;IAQrC,eAAe,CAAC,OAAO,EAAE,UAAU,CAAC,KAAK,CAAC,EAAE,IAAI,EAAE,UAAU,CAAC,MAAM,CAAC;IAI3E,OAAO,CAAC,cAAc;IAgBtB,OAAO,CAAC,qBAAqB;CAM9B"}
@@ -0,0 +1,11 @@
1
+ import { DEFAULT_AUTH_ERRORS } from './data';
2
+ /** Shape of a resolved auth error — a string error code paired with a lazily-evaluated message factory. */
3
+ export type TAuthError = {
4
+ code: string;
5
+ msg: (args?: any) => string | null;
6
+ };
7
+ /** Union of all values accepted as an error argument; unknown allows catch-block values to be passed without casting. */
8
+ export type Err = TAuthError | null | string | unknown;
9
+ /** Union of the built-in error code keys, used to constrain look-ups to known defaults. */
10
+ export type DefaultAuthError = keyof typeof DEFAULT_AUTH_ERRORS;
11
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/AuthErrors/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,QAAQ,CAAA;AAE5C,2GAA2G;AAC3G,MAAM,MAAM,UAAU,GAAG;IACvB,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,EAAE,CAAC,IAAI,CAAC,EAAE,GAAG,KAAK,MAAM,GAAG,IAAI,CAAA;CACnC,CAAA;AAED,yHAAyH;AACzH,MAAM,MAAM,GAAG,GAAG,UAAU,GAAG,IAAI,GAAG,MAAM,GAAG,OAAO,CAAA;AAEtD,2FAA2F;AAC3F,MAAM,MAAM,gBAAgB,GAAG,MAAM,OAAO,mBAAmB,CAAA"}
@@ -0,0 +1,2 @@
1
+ export * from './AuthErrors';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA"}
package/package.json CHANGED
@@ -1,7 +1,20 @@
1
1
  {
2
2
  "name": "@codeleap/auth",
3
- "version": "6.3.0",
3
+ "version": "7.0.0",
4
4
  "main": "src/index.ts",
5
+ "types": "dist/index.d.ts",
6
+ "exports": {
7
+ ".": {
8
+ "source": "./src/index.ts",
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js",
11
+ "default": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "src"
17
+ ],
5
18
  "license": "UNLICENSED",
6
19
  "repository": {
7
20
  "url": "https://github.com/codeleap-uk/internal-libs-monorepo.git",
@@ -9,15 +22,16 @@
9
22
  "directory": "packages/auth"
10
23
  },
11
24
  "devDependencies": {
12
- "@codeleap/config": "6.3.0",
13
- "@codeleap/types": "6.3.0",
25
+ "@codeleap/config": "7.0.0",
26
+ "@codeleap/types": "7.0.0",
14
27
  "ts-node-dev": "1.1.8"
15
28
  },
16
29
  "scripts": {
17
- "build": "echo 'No build needed'"
30
+ "build": "tsc --build tsconfig.build.json",
31
+ "typecheck": "bun tsc --noEmit -p ./tsconfig.json"
18
32
  },
19
33
  "peerDependencies": {
20
- "@codeleap/types": "6.3.0",
34
+ "@codeleap/types": "7.0.0",
21
35
  "typescript": "5.5.2"
22
36
  }
23
- }
37
+ }
@@ -1,4 +1,8 @@
1
1
 
2
+ /**
3
+ * Baseline error-code-to-message map shared across all apps.
4
+ * `social` keys are intentionally null — canceled/unspecified social flows are silently swallowed rather than surfaced to the user.
5
+ */
2
6
  export const DEFAULT_AUTH_ERRORS = {
3
7
  'auth/wrong-password': 'Email or password is incorrect',
4
8
  'auth/not-registered': 'This user is not registered',
@@ -4,6 +4,7 @@ import { DefaultAuthError, Err, TAuthError } from './types'
4
4
 
5
5
  export * from './types'
6
6
 
7
+ /** Internal Error subclass that carries a typed `code` alongside the human-readable `msg` factory; not exported — consumers interact through `AppAuthErrors`. */
7
8
  class AuthError extends Error {
8
9
  code: TAuthError['code']
9
10
  msg: TAuthError['msg']
@@ -19,12 +20,16 @@ class AuthError extends Error {
19
20
 
20
21
  type ErrorHandler<E> = (err: E | null, module?: string, args?: any) => void
21
22
 
23
+ /**
24
+ * Registry that merges app-specific error codes with the built-in defaults and wires them to a single `onError` handler.
25
+ * Social errors are kept in a separate namespace because their codes can collide with base error codes (e.g. numeric strings).
26
+ */
22
27
  export class AppAuthErrors<T extends Record<string, string | object | AnyFunction>, F extends ErrorHandler<AuthError>> {
23
28
  public errors: Omit<Record<keyof T | keyof typeof DEFAULT_AUTH_ERRORS, AuthError>, 'social'>
24
29
 
25
30
  public social: Omit<Record<keyof T['social'] | keyof typeof DEFAULT_AUTH_ERRORS['social'], AuthError>, 'social'>
26
31
 
27
- public onError: ErrorHandler<Err>
32
+ public onError!: ErrorHandler<Err>
28
33
 
29
34
  public defaultErrors = DEFAULT_AUTH_ERRORS
30
35
 
@@ -34,7 +39,7 @@ export class AppAuthErrors<T extends Record<string, string | object | AnyFunctio
34
39
  const defaultErrors = DEFAULT_AUTH_ERRORS
35
40
 
36
41
  const socialAuthErrors = {
37
- ...defaultErrors[this.socialKey],
42
+ ...(defaultErrors as Record<string, any>)[this.socialKey],
38
43
  ...(newErrors?.[this.socialKey] as object ?? {}),
39
44
  } as any
40
45
 
@@ -45,8 +50,8 @@ export class AppAuthErrors<T extends Record<string, string | object | AnyFunctio
45
50
  delete newErrors['social']
46
51
  }
47
52
 
48
- if (!!defaultErrors['social']) {
49
- delete defaultErrors['social']
53
+ if (!!(defaultErrors as Record<string, any>)['social']) {
54
+ (defaultErrors as Record<string, any>)['social'] = undefined
50
55
  }
51
56
 
52
57
  const baseAuthErrors = {
@@ -64,13 +69,13 @@ export class AppAuthErrors<T extends Record<string, string | object | AnyFunctio
64
69
  if (!err) return null
65
70
 
66
71
  if (typeof err == 'string') {
67
- return this.errors?.[err] ?? this.social?.[err]
72
+ return (this.errors as Record<string, AuthError>)?.[err] ?? (this.social as Record<string, AuthError>)?.[err]
68
73
  }
69
74
 
70
75
  const authError = err as TAuthError
71
76
 
72
77
  if (!!authError?.code) {
73
- return this.errors?.[authError?.code] ?? this.social?.[authError?.code]
78
+ return (this.errors as Record<string, AuthError>)?.[authError?.code] ?? (this.social as Record<string, AuthError>)?.[authError?.code]
74
79
  }
75
80
 
76
81
  return null
@@ -99,10 +104,10 @@ export class AppAuthErrors<T extends Record<string, string | object | AnyFunctio
99
104
  }
100
105
 
101
106
  private registryErrors(unregisteredErrors: T): any {
102
- const state = {}
107
+ const state: Record<string, AuthError> = {}
103
108
 
104
109
  for (const errorKey in (unregisteredErrors as object)) {
105
- const errorMsg = unregisteredErrors[errorKey] as string
110
+ const errorMsg = (unregisteredErrors as Record<string, any>)[errorKey] as string
106
111
 
107
112
  if (typeof errorMsg === 'string' || errorMsg == null) {
108
113
  state[errorKey] = new AuthError(() => errorMsg, errorKey)
@@ -115,7 +120,7 @@ export class AppAuthErrors<T extends Record<string, string | object | AnyFunctio
115
120
  }
116
121
 
117
122
  private registryErrorFunction(pureErrorFunction: Function) {
118
- this.onError = (err, module = null, args = {}) => {
123
+ this.onError = (err, module = undefined, args = {}) => {
119
124
  const authError = this.getError(err)
120
125
  pureErrorFunction?.(authError, module, args)
121
126
  }
@@ -1,10 +1,13 @@
1
1
  import { DEFAULT_AUTH_ERRORS } from './data'
2
2
 
3
+ /** Shape of a resolved auth error — a string error code paired with a lazily-evaluated message factory. */
3
4
  export type TAuthError = {
4
5
  code: string
5
6
  msg: (args?: any) => string | null
6
7
  }
7
8
 
9
+ /** Union of all values accepted as an error argument; unknown allows catch-block values to be passed without casting. */
8
10
  export type Err = TAuthError | null | string | unknown
9
11
 
12
+ /** Union of the built-in error code keys, used to constrain look-ups to known defaults. */
10
13
  export type DefaultAuthError = keyof typeof DEFAULT_AUTH_ERRORS
package/package.json.bak DELETED
@@ -1,23 +0,0 @@
1
- {
2
- "name": "@codeleap/auth",
3
- "version": "6.3.0",
4
- "main": "src/index.ts",
5
- "license": "UNLICENSED",
6
- "repository": {
7
- "url": "https://github.com/codeleap-uk/internal-libs-monorepo.git",
8
- "type": "git",
9
- "directory": "packages/auth"
10
- },
11
- "devDependencies": {
12
- "@codeleap/config": "workspace:*",
13
- "@codeleap/types": "workspace:*",
14
- "ts-node-dev": "1.1.8"
15
- },
16
- "scripts": {
17
- "build": "echo 'No build needed'"
18
- },
19
- "peerDependencies": {
20
- "@codeleap/types": "workspace:*",
21
- "typescript": "5.5.2"
22
- }
23
- }