@baliola/auth-sdk 0.2.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.
- package/CHANGELOG.md +76 -0
- package/LICENSE +37 -0
- package/README.md +305 -0
- package/dist/authError-C5g5jP5l.d.ts +102 -0
- package/dist/authError-DbEZJnC4.js +214 -0
- package/dist/client/index.d.ts +2 -0
- package/dist/client/index.js +2 -0
- package/dist/client-BcyoQ5Cj.js +504 -0
- package/dist/errors/index.d.ts +2 -0
- package/dist/errors/index.js +2 -0
- package/dist/index-G-gqveGn.d.ts +142 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +4 -0
- package/dist/requests-eTORIjY5.d.ts +42 -0
- package/dist/session-Cs_P7ojF.d.ts +49 -0
- package/dist/sessionStore-BDdEpbL8.d.ts +35 -0
- package/dist/sessionStore-DD6lON9W.js +50 -0
- package/dist/store/index.d.ts +2 -0
- package/dist/store/index.js +2 -0
- package/dist/tokens-BXrPLi5B.d.ts +29 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.js +1 -0
- package/package.json +60 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
//#region src/errors/authError.ts
|
|
2
|
+
/**
|
|
3
|
+
* Base class for every error thrown by the SDK. Concrete error subclasses
|
|
4
|
+
* (`OtpInvalidError`, `RateLimitedError`, etc.) extend this. Consumers can
|
|
5
|
+
* branch on either `instanceof` (preferred for TS narrowing) or `err.code`
|
|
6
|
+
* (when narrowing by string).
|
|
7
|
+
*
|
|
8
|
+
* - `status`: HTTP status from the server, or `0` for network/timeout failures.
|
|
9
|
+
* - `code`: stable machine-readable code (see error subclasses for values).
|
|
10
|
+
* - `serverMessage`: server's `message` field (for display or logs).
|
|
11
|
+
* - `serverName`: server's error class name from `error.details.name`.
|
|
12
|
+
* - `details`: server's `error.details` as-is (carries context fields like
|
|
13
|
+
* `attemptsRemaining`, `canResendInSeconds`, `retryAfterSeconds`).
|
|
14
|
+
* - `cause`: original underlying error (e.g. the `TypeError` from a network failure).
|
|
15
|
+
*/
|
|
16
|
+
var AuthError = class extends Error {
|
|
17
|
+
status;
|
|
18
|
+
code;
|
|
19
|
+
serverMessage;
|
|
20
|
+
serverName;
|
|
21
|
+
details;
|
|
22
|
+
cause;
|
|
23
|
+
constructor(init) {
|
|
24
|
+
super(init.serverMessage);
|
|
25
|
+
this.name = "AuthError";
|
|
26
|
+
this.status = init.status;
|
|
27
|
+
this.code = init.code ?? "";
|
|
28
|
+
this.serverMessage = init.serverMessage;
|
|
29
|
+
if (init.serverName !== void 0) this.serverName = init.serverName;
|
|
30
|
+
if (init.details !== void 0) this.details = init.details;
|
|
31
|
+
if (init.cause !== void 0) this.cause = init.cause;
|
|
32
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var OtpInvalidError = class extends AuthError {
|
|
36
|
+
attemptsRemaining;
|
|
37
|
+
canResendInSeconds;
|
|
38
|
+
constructor(init) {
|
|
39
|
+
super(init);
|
|
40
|
+
this.name = "OtpInvalidError";
|
|
41
|
+
this.attemptsRemaining = init.attemptsRemaining;
|
|
42
|
+
this.canResendInSeconds = init.canResendInSeconds;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var OtpExpiredError = class extends AuthError {
|
|
46
|
+
constructor(init) {
|
|
47
|
+
super(init);
|
|
48
|
+
this.name = "OtpExpiredError";
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
var MaxAttemptsError = class extends AuthError {
|
|
52
|
+
retryAfterSeconds;
|
|
53
|
+
constructor(init) {
|
|
54
|
+
super(init);
|
|
55
|
+
this.name = "MaxAttemptsError";
|
|
56
|
+
this.retryAfterSeconds = init.retryAfterSeconds;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
var NoPendingOtpError = class extends AuthError {
|
|
60
|
+
constructor(init) {
|
|
61
|
+
super(init);
|
|
62
|
+
this.name = "NoPendingOtpError";
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
var ResendCooldownError = class extends AuthError {
|
|
66
|
+
canResendInSeconds;
|
|
67
|
+
constructor(init) {
|
|
68
|
+
super(init);
|
|
69
|
+
this.name = "ResendCooldownError";
|
|
70
|
+
this.canResendInSeconds = init.canResendInSeconds;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
var MaxResendsError = class extends AuthError {
|
|
74
|
+
constructor(init) {
|
|
75
|
+
super(init);
|
|
76
|
+
this.name = "MaxResendsError";
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
var InvalidCredentialsError = class extends AuthError {
|
|
80
|
+
constructor(init) {
|
|
81
|
+
super(init);
|
|
82
|
+
this.name = "InvalidCredentialsError";
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
var NoPasswordSetError = class extends AuthError {
|
|
86
|
+
constructor(init) {
|
|
87
|
+
super(init);
|
|
88
|
+
this.name = "NoPasswordSetError";
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
var AccountSuspendedError = class extends AuthError {
|
|
92
|
+
constructor(init) {
|
|
93
|
+
super(init);
|
|
94
|
+
this.name = "AccountSuspendedError";
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
var EmailAlreadyHasPasswordError = class extends AuthError {
|
|
98
|
+
constructor(init) {
|
|
99
|
+
super(init);
|
|
100
|
+
this.name = "EmailAlreadyHasPasswordError";
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
var CaptchaFailedError = class extends AuthError {
|
|
104
|
+
constructor(init) {
|
|
105
|
+
super(init);
|
|
106
|
+
this.name = "CaptchaFailedError";
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
var RateLimitedError = class extends AuthError {
|
|
110
|
+
retryAfterSeconds;
|
|
111
|
+
retryAfterHuman;
|
|
112
|
+
constructor(init) {
|
|
113
|
+
super(init);
|
|
114
|
+
this.name = "RateLimitedError";
|
|
115
|
+
this.retryAfterSeconds = init.retryAfterSeconds;
|
|
116
|
+
this.retryAfterHuman = init.retryAfterHuman;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
var InvalidPasswordError = class extends AuthError {
|
|
120
|
+
reason;
|
|
121
|
+
constructor(init) {
|
|
122
|
+
super(init);
|
|
123
|
+
this.name = "InvalidPasswordError";
|
|
124
|
+
this.reason = init.reason;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
var InvalidEmailError = class extends AuthError {
|
|
128
|
+
constructor(init) {
|
|
129
|
+
super(init);
|
|
130
|
+
this.name = "InvalidEmailError";
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
function num(d, key, fallback = 0) {
|
|
134
|
+
const v = d?.[key];
|
|
135
|
+
return typeof v === "number" ? v : fallback;
|
|
136
|
+
}
|
|
137
|
+
function str(d, key, fallback = "") {
|
|
138
|
+
const v = d?.[key];
|
|
139
|
+
return typeof v === "string" ? v : fallback;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Map a backend error response onto the right AuthError subclass.
|
|
143
|
+
* Falls back to `AuthError` (with status + code preserved) for unknown codes.
|
|
144
|
+
*/
|
|
145
|
+
async function authErrorFromResponse(response) {
|
|
146
|
+
let body;
|
|
147
|
+
try {
|
|
148
|
+
body = await response.json();
|
|
149
|
+
} catch {}
|
|
150
|
+
const serverMessage = typeof body?.message === "string" ? body.message : `HTTP ${response.status}`;
|
|
151
|
+
const code = typeof body?.error?.code === "string" ? body.error.code : "";
|
|
152
|
+
const details = body?.error?.details;
|
|
153
|
+
const serverName = typeof details?.["name"] === "string" ? details["name"] : void 0;
|
|
154
|
+
const init = {
|
|
155
|
+
status: response.status,
|
|
156
|
+
code,
|
|
157
|
+
serverMessage,
|
|
158
|
+
...serverName !== void 0 && { serverName },
|
|
159
|
+
...details !== void 0 && { details }
|
|
160
|
+
};
|
|
161
|
+
switch (code) {
|
|
162
|
+
case "otp_invalid": return new OtpInvalidError({
|
|
163
|
+
...init,
|
|
164
|
+
attemptsRemaining: num(details, "attemptsRemaining"),
|
|
165
|
+
canResendInSeconds: num(details, "canResendInSeconds")
|
|
166
|
+
});
|
|
167
|
+
case "otp_expired": return new OtpExpiredError(init);
|
|
168
|
+
case "max_attempts_exceeded": return new MaxAttemptsError({
|
|
169
|
+
...init,
|
|
170
|
+
retryAfterSeconds: num(details, "retryAfterSeconds")
|
|
171
|
+
});
|
|
172
|
+
case "no_pending_otp": return new NoPendingOtpError(init);
|
|
173
|
+
case "resend_cooldown": return new ResendCooldownError({
|
|
174
|
+
...init,
|
|
175
|
+
canResendInSeconds: num(details, "canResendInSeconds")
|
|
176
|
+
});
|
|
177
|
+
case "max_resends_exceeded": return new MaxResendsError(init);
|
|
178
|
+
case "invalid_credentials": return new InvalidCredentialsError(init);
|
|
179
|
+
case "no_password_set": return new NoPasswordSetError(init);
|
|
180
|
+
case "account_suspended": return new AccountSuspendedError(init);
|
|
181
|
+
case "email_already_has_password": return new EmailAlreadyHasPasswordError(init);
|
|
182
|
+
case "captcha_failed": return new CaptchaFailedError(init);
|
|
183
|
+
case "invalid_password": {
|
|
184
|
+
const reason = str(details, "reason");
|
|
185
|
+
return new InvalidPasswordError({
|
|
186
|
+
...init,
|
|
187
|
+
reason: reason || "unknown"
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
case "invalid_email": return new InvalidEmailError(init);
|
|
191
|
+
default:
|
|
192
|
+
if (response.status === 429) {
|
|
193
|
+
const retryAfterSeconds = num(details, "retryAfter") || num(details, "retryAfterSeconds") || Number(response.headers.get("Retry-After") ?? 0);
|
|
194
|
+
const retryAfterHuman = str(details, "retryAfterHuman");
|
|
195
|
+
return new RateLimitedError({
|
|
196
|
+
...init,
|
|
197
|
+
retryAfterSeconds,
|
|
198
|
+
retryAfterHuman
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
return new AuthError(init);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/** Network/timeout failures land here. */
|
|
205
|
+
function authErrorFromFetchFailure(cause, isTimeout = false) {
|
|
206
|
+
return new AuthError({
|
|
207
|
+
status: 0,
|
|
208
|
+
code: isTimeout ? "timeout" : "network_error",
|
|
209
|
+
serverMessage: isTimeout ? "Request timeout" : "Network error",
|
|
210
|
+
cause
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
//#endregion
|
|
214
|
+
export { authErrorFromResponse as _, InvalidCredentialsError as a, MaxAttemptsError as c, NoPendingOtpError as d, OtpExpiredError as f, authErrorFromFetchFailure as g, ResendCooldownError as h, EmailAlreadyHasPasswordError as i, MaxResendsError as l, RateLimitedError as m, AuthError as n, InvalidEmailError as o, OtpInvalidError as p, CaptchaFailedError as r, InvalidPasswordError as s, AccountSuspendedError as t, NoPasswordSetError as u };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as GoogleNamespace, c as createAuthClient, i as EmailPasswordNamespace, l as AuthFetchInit, n as CreateAuthClientOptions, o as ProactiveRefreshConfig, r as EmailOtpNamespace, s as SubscribeOptions, t as AuthClient } from "../index-G-gqveGn.js";
|
|
2
|
+
export { AuthClient, AuthFetchInit, CreateAuthClientOptions, EmailOtpNamespace, EmailPasswordNamespace, GoogleNamespace, ProactiveRefreshConfig, SubscribeOptions, createAuthClient };
|