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.cjs
CHANGED
|
@@ -450,7 +450,8 @@ var AuthService = class extends Service {
|
|
|
450
450
|
}
|
|
451
451
|
const body = await response.json();
|
|
452
452
|
this.setAuthFromResponse(body, preset, credentials.email);
|
|
453
|
-
|
|
453
|
+
const unwrapped = this.unwrapBody(body);
|
|
454
|
+
return unwrapped[preset.auth.userField] ?? unwrapped;
|
|
454
455
|
}
|
|
455
456
|
async httpRegister(data) {
|
|
456
457
|
const preset = this.httpDriver.preset;
|
|
@@ -470,7 +471,8 @@ var AuthService = class extends Service {
|
|
|
470
471
|
}
|
|
471
472
|
const body = await response.json();
|
|
472
473
|
this.setAuthFromResponse(body, preset, data.email);
|
|
473
|
-
|
|
474
|
+
const unwrapped = this.unwrapBody(body);
|
|
475
|
+
return unwrapped[preset.auth.userField] ?? unwrapped;
|
|
474
476
|
}
|
|
475
477
|
async httpRefresh() {
|
|
476
478
|
const preset = this.httpDriver.preset;
|
|
@@ -489,9 +491,10 @@ var AuthService = class extends Service {
|
|
|
489
491
|
throw new ForbiddenError("Session expired. Please log in again.");
|
|
490
492
|
}
|
|
491
493
|
const body = await response.json();
|
|
492
|
-
const
|
|
493
|
-
const
|
|
494
|
-
const
|
|
494
|
+
const unwrapped = this.unwrapBody(body);
|
|
495
|
+
const token = unwrapped[preset.auth.tokenField];
|
|
496
|
+
const refreshToken = preset.auth.refreshTokenField ? unwrapped[preset.auth.refreshTokenField] : this.authState.refreshToken;
|
|
497
|
+
const expiresIn = preset.auth.expiresInField ? unwrapped[preset.auth.expiresInField] : null;
|
|
495
498
|
const expiresAt = expiresIn ? Date.now() + expiresIn * 1e3 : void 0;
|
|
496
499
|
this.authState = {
|
|
497
500
|
...this.authState,
|
|
@@ -503,11 +506,18 @@ var AuthService = class extends Service {
|
|
|
503
506
|
this.scheduleRefresh();
|
|
504
507
|
return token;
|
|
505
508
|
}
|
|
509
|
+
unwrapBody(body) {
|
|
510
|
+
if (body.data && typeof body.data === "object" && !Array.isArray(body.data)) {
|
|
511
|
+
return body.data;
|
|
512
|
+
}
|
|
513
|
+
return body;
|
|
514
|
+
}
|
|
506
515
|
setAuthFromResponse(body, preset, fallbackEmail) {
|
|
507
|
-
const
|
|
508
|
-
const
|
|
509
|
-
const
|
|
510
|
-
const
|
|
516
|
+
const unwrapped = this.unwrapBody(body);
|
|
517
|
+
const token = unwrapped[preset.auth.tokenField];
|
|
518
|
+
const user = unwrapped[preset.auth.userField] ?? unwrapped;
|
|
519
|
+
const refreshToken = preset.auth.refreshTokenField ? unwrapped[preset.auth.refreshTokenField] : void 0;
|
|
520
|
+
const expiresIn = preset.auth.expiresInField ? unwrapped[preset.auth.expiresInField] : null;
|
|
511
521
|
const expiresAt = expiresIn ? Date.now() + expiresIn * 1e3 : void 0;
|
|
512
522
|
this.authState = {
|
|
513
523
|
userId: user.id,
|
|
@@ -1128,7 +1138,7 @@ var defaultPreset = definePreset({
|
|
|
1128
1138
|
var springBootPreset = definePreset({
|
|
1129
1139
|
name: "spring-boot",
|
|
1130
1140
|
response: {
|
|
1131
|
-
single: (raw) => ({ data: raw }),
|
|
1141
|
+
single: (raw) => ({ data: raw.data ?? raw }),
|
|
1132
1142
|
list: (raw) => ({
|
|
1133
1143
|
items: raw.content ?? [],
|
|
1134
1144
|
meta: {
|
|
@@ -1214,7 +1224,7 @@ var laravelPreset = definePreset({
|
|
|
1214
1224
|
var djangoPreset = definePreset({
|
|
1215
1225
|
name: "django",
|
|
1216
1226
|
response: {
|
|
1217
|
-
single: (raw) => ({ data: raw }),
|
|
1227
|
+
single: (raw) => ({ data: raw.data ?? raw }),
|
|
1218
1228
|
list: (raw) => ({
|
|
1219
1229
|
items: raw.results ?? [],
|
|
1220
1230
|
meta: {
|
|
@@ -1581,7 +1591,7 @@ var HttpDriver = class {
|
|
|
1581
1591
|
url += `?${params.toString()}`;
|
|
1582
1592
|
}
|
|
1583
1593
|
return this._fetch(url, {
|
|
1584
|
-
method: options?.method ?? "POST",
|
|
1594
|
+
method: options?.method ?? (options?.body !== void 0 ? "POST" : "GET"),
|
|
1585
1595
|
body: options?.body !== void 0 ? JSON.stringify(options.body) : void 0
|
|
1586
1596
|
});
|
|
1587
1597
|
}
|