fauxbase 0.5.7 → 0.5.8
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 +22 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +22 -12
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -353,6 +353,7 @@ declare abstract class AuthService<T extends Entity> extends Service<T> {
|
|
|
353
353
|
private httpLogin;
|
|
354
354
|
private httpRegister;
|
|
355
355
|
private httpRefresh;
|
|
356
|
+
private unwrapBody;
|
|
356
357
|
private setAuthFromResponse;
|
|
357
358
|
private generateToken;
|
|
358
359
|
private generateRefreshToken;
|
package/dist/index.d.ts
CHANGED
|
@@ -353,6 +353,7 @@ declare abstract class AuthService<T extends Entity> extends Service<T> {
|
|
|
353
353
|
private httpLogin;
|
|
354
354
|
private httpRegister;
|
|
355
355
|
private httpRefresh;
|
|
356
|
+
private unwrapBody;
|
|
356
357
|
private setAuthFromResponse;
|
|
357
358
|
private generateToken;
|
|
358
359
|
private generateRefreshToken;
|
package/dist/index.js
CHANGED
|
@@ -448,7 +448,8 @@ var AuthService = class extends Service {
|
|
|
448
448
|
}
|
|
449
449
|
const body = await response.json();
|
|
450
450
|
this.setAuthFromResponse(body, preset, credentials.email);
|
|
451
|
-
|
|
451
|
+
const unwrapped = this.unwrapBody(body);
|
|
452
|
+
return unwrapped[preset.auth.userField] ?? unwrapped;
|
|
452
453
|
}
|
|
453
454
|
async httpRegister(data) {
|
|
454
455
|
const preset = this.httpDriver.preset;
|
|
@@ -468,7 +469,8 @@ var AuthService = class extends Service {
|
|
|
468
469
|
}
|
|
469
470
|
const body = await response.json();
|
|
470
471
|
this.setAuthFromResponse(body, preset, data.email);
|
|
471
|
-
|
|
472
|
+
const unwrapped = this.unwrapBody(body);
|
|
473
|
+
return unwrapped[preset.auth.userField] ?? unwrapped;
|
|
472
474
|
}
|
|
473
475
|
async httpRefresh() {
|
|
474
476
|
const preset = this.httpDriver.preset;
|
|
@@ -487,9 +489,10 @@ var AuthService = class extends Service {
|
|
|
487
489
|
throw new ForbiddenError("Session expired. Please log in again.");
|
|
488
490
|
}
|
|
489
491
|
const body = await response.json();
|
|
490
|
-
const
|
|
491
|
-
const
|
|
492
|
-
const
|
|
492
|
+
const unwrapped = this.unwrapBody(body);
|
|
493
|
+
const token = unwrapped[preset.auth.tokenField];
|
|
494
|
+
const refreshToken = preset.auth.refreshTokenField ? unwrapped[preset.auth.refreshTokenField] : this.authState.refreshToken;
|
|
495
|
+
const expiresIn = preset.auth.expiresInField ? unwrapped[preset.auth.expiresInField] : null;
|
|
493
496
|
const expiresAt = expiresIn ? Date.now() + expiresIn * 1e3 : void 0;
|
|
494
497
|
this.authState = {
|
|
495
498
|
...this.authState,
|
|
@@ -501,11 +504,18 @@ var AuthService = class extends Service {
|
|
|
501
504
|
this.scheduleRefresh();
|
|
502
505
|
return token;
|
|
503
506
|
}
|
|
507
|
+
unwrapBody(body) {
|
|
508
|
+
if (body.data && typeof body.data === "object" && !Array.isArray(body.data)) {
|
|
509
|
+
return body.data;
|
|
510
|
+
}
|
|
511
|
+
return body;
|
|
512
|
+
}
|
|
504
513
|
setAuthFromResponse(body, preset, fallbackEmail) {
|
|
505
|
-
const
|
|
506
|
-
const
|
|
507
|
-
const
|
|
508
|
-
const
|
|
514
|
+
const unwrapped = this.unwrapBody(body);
|
|
515
|
+
const token = unwrapped[preset.auth.tokenField];
|
|
516
|
+
const user = unwrapped[preset.auth.userField] ?? unwrapped;
|
|
517
|
+
const refreshToken = preset.auth.refreshTokenField ? unwrapped[preset.auth.refreshTokenField] : void 0;
|
|
518
|
+
const expiresIn = preset.auth.expiresInField ? unwrapped[preset.auth.expiresInField] : null;
|
|
509
519
|
const expiresAt = expiresIn ? Date.now() + expiresIn * 1e3 : void 0;
|
|
510
520
|
this.authState = {
|
|
511
521
|
userId: user.id,
|
|
@@ -1126,7 +1136,7 @@ var defaultPreset = definePreset({
|
|
|
1126
1136
|
var springBootPreset = definePreset({
|
|
1127
1137
|
name: "spring-boot",
|
|
1128
1138
|
response: {
|
|
1129
|
-
single: (raw) => ({ data: raw }),
|
|
1139
|
+
single: (raw) => ({ data: raw.data ?? raw }),
|
|
1130
1140
|
list: (raw) => ({
|
|
1131
1141
|
items: raw.content ?? [],
|
|
1132
1142
|
meta: {
|
|
@@ -1212,7 +1222,7 @@ var laravelPreset = definePreset({
|
|
|
1212
1222
|
var djangoPreset = definePreset({
|
|
1213
1223
|
name: "django",
|
|
1214
1224
|
response: {
|
|
1215
|
-
single: (raw) => ({ data: raw }),
|
|
1225
|
+
single: (raw) => ({ data: raw.data ?? raw }),
|
|
1216
1226
|
list: (raw) => ({
|
|
1217
1227
|
items: raw.results ?? [],
|
|
1218
1228
|
meta: {
|
|
@@ -1579,7 +1589,7 @@ var HttpDriver = class {
|
|
|
1579
1589
|
url += `?${params.toString()}`;
|
|
1580
1590
|
}
|
|
1581
1591
|
return this._fetch(url, {
|
|
1582
|
-
method: options?.method ?? "POST",
|
|
1592
|
+
method: options?.method ?? (options?.body !== void 0 ? "POST" : "GET"),
|
|
1583
1593
|
body: options?.body !== void 0 ? JSON.stringify(options.body) : void 0
|
|
1584
1594
|
});
|
|
1585
1595
|
}
|