@rebasepro/client 0.35.1 → 0.37.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/README.md +1 -1
- package/dist/admin.d.ts +2 -1
- package/dist/index.es.js +59 -14
- package/dist/index.es.js.map +1 -1
- package/package.json +8 -6
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @rebasepro/client
|
|
2
2
|
|
|
3
|
-
HTTP SDK
|
|
3
|
+
Typed HTTP SDK for [Rebase](https://rebase.pro), the open-source Postgres BaaS — CRUD, auth, storage, realtime WebSockets, offline / local-first sync, admin, cron, and custom functions.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
package/dist/admin.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Transport } from "./transport.js";
|
|
2
|
-
import type { AdminGuest, AdminUser, DeleteGuestsResult, ImportUserInput, ImportUsersResult, RoleSummary } from "@rebasepro/types";
|
|
2
|
+
import type { AdminGuest, AdminUser, DeleteGuestsResult, ImportUserInput, ImportUsersRequest, ImportUsersResult, RoleSummary } from "@rebasepro/types";
|
|
3
3
|
export type { AdminUser };
|
|
4
4
|
export interface CreateAdminOptions {
|
|
5
5
|
adminPath?: string;
|
|
@@ -57,6 +57,7 @@ export declare function createAdmin(transport: Transport, options?: CreateAdminO
|
|
|
57
57
|
}>;
|
|
58
58
|
importUsers: (users: ImportUserInput[], options?: {
|
|
59
59
|
dryRun?: boolean;
|
|
60
|
+
passwordHashAlgorithm?: ImportUsersRequest["passwordHashAlgorithm"];
|
|
60
61
|
}) => Promise<ImportUsersResult>;
|
|
61
62
|
updateUser: (userId: string, data: {
|
|
62
63
|
email?: string;
|
package/dist/index.es.js
CHANGED
|
@@ -277,9 +277,13 @@ function createAuth(transport, options) {
|
|
|
277
277
|
* 0.23, with 400 INVALID_INPUT) never read the cookie — so no tab that outlived
|
|
278
278
|
* its access token could renew it, while a reload, which had no session in
|
|
279
279
|
* memory and so sent no field, could.
|
|
280
|
+
*
|
|
281
|
+
* `probe` marks the load-time restore of {@link probeSession}: in cookie
|
|
282
|
+
* mode the body is then `{ "probe": true }`.
|
|
280
283
|
*/
|
|
281
|
-
function refreshTokenBody() {
|
|
282
|
-
|
|
284
|
+
function refreshTokenBody(probe = false) {
|
|
285
|
+
if (authFlowMode === "cookie") return JSON.stringify(probe ? { probe: true } : {});
|
|
286
|
+
return JSON.stringify({ refreshToken: currentSession?.refreshToken });
|
|
283
287
|
}
|
|
284
288
|
/**
|
|
285
289
|
* The refresh token a new session keeps, given the one an answer carried.
|
|
@@ -839,8 +843,29 @@ function createAuth(transport, options) {
|
|
|
839
843
|
}
|
|
840
844
|
}
|
|
841
845
|
function refreshSession() {
|
|
846
|
+
return startRefresh(false);
|
|
847
|
+
}
|
|
848
|
+
/**
|
|
849
|
+
* The cookie-mode restore on load: a refresh that says it is only asking
|
|
850
|
+
* whether a session exists (`{ "probe": true }`).
|
|
851
|
+
*
|
|
852
|
+
* The refresh token is an HttpOnly cookie this page cannot read, and a
|
|
853
|
+
* session can begin without this client seeing it — an OAuth redirect, a
|
|
854
|
+
* sign-in in another tab, a cookie a sibling app set on a shared
|
|
855
|
+
* `cookieAuth.domain` — so the question is asked on every load. To a probe
|
|
856
|
+
* that presents no cookie at all the server answers `204`, which this
|
|
857
|
+
* client takes as "nobody is signed in" (`NOT_SIGNED_IN`), where a 401
|
|
858
|
+
* would put a red "Failed to load resource" line in every new visitor's
|
|
859
|
+
* console. A server that predates the probe ignores the field and answers
|
|
860
|
+
* `401 NO_SESSION`, which means the same. A cookie that is presented and
|
|
861
|
+
* refused is a 401 either way.
|
|
862
|
+
*/
|
|
863
|
+
function probeSession() {
|
|
864
|
+
return startRefresh(true);
|
|
865
|
+
}
|
|
866
|
+
function startRefresh(probe) {
|
|
842
867
|
if (inFlightRefresh && inFlightEpoch === sessionEpoch) return inFlightRefresh;
|
|
843
|
-
const refresh = withRefreshLock(() => doRefreshSession()).finally(() => {
|
|
868
|
+
const refresh = withRefreshLock(() => doRefreshSession(probe)).finally(() => {
|
|
844
869
|
if (inFlightRefresh === refresh) inFlightRefresh = null;
|
|
845
870
|
});
|
|
846
871
|
inFlightRefresh = refresh;
|
|
@@ -897,7 +922,7 @@ function createAuth(transport, options) {
|
|
|
897
922
|
emit("TOKEN_REFRESHED", sibling);
|
|
898
923
|
return sibling;
|
|
899
924
|
}
|
|
900
|
-
async function doRefreshSession() {
|
|
925
|
+
async function doRefreshSession(probe) {
|
|
901
926
|
const epoch = sessionEpoch;
|
|
902
927
|
try {
|
|
903
928
|
const sibling = siblingSession();
|
|
@@ -906,27 +931,28 @@ function createAuth(transport, options) {
|
|
|
906
931
|
if (adopted) return adopted;
|
|
907
932
|
}
|
|
908
933
|
try {
|
|
909
|
-
return await requestRefresh(epoch);
|
|
934
|
+
return await requestRefresh(epoch, probe);
|
|
910
935
|
} catch (err) {
|
|
911
936
|
const late = epoch === sessionEpoch && err instanceof RebaseApiError && err.code === "TOKEN_ALREADY_USED" ? siblingSession() : null;
|
|
912
937
|
if (!late) throw err;
|
|
913
|
-
return adoptSibling(late) ?? await requestRefresh(epoch);
|
|
938
|
+
return adoptSibling(late) ?? await requestRefresh(epoch, probe);
|
|
914
939
|
}
|
|
915
940
|
} catch (err) {
|
|
916
941
|
if (epoch !== sessionEpoch) return supersededRefresh();
|
|
917
942
|
throw err;
|
|
918
943
|
}
|
|
919
944
|
}
|
|
920
|
-
async function requestRefresh(epoch) {
|
|
945
|
+
async function requestRefresh(epoch, probe) {
|
|
921
946
|
if (authFlowMode !== "cookie" && !currentSession?.refreshToken) throw new RebaseClientError$1("No active session to refresh. Sign in first, or set `authFlowMode: \"cookie\"` if the refresh token is held in an HttpOnly cookie.", { code: "NOT_SIGNED_IN" });
|
|
922
947
|
const res = await getFetch()(authUrl("/refresh"), {
|
|
923
948
|
method: "POST",
|
|
924
949
|
headers: { "Content-Type": "application/json" },
|
|
925
|
-
body: refreshTokenBody(),
|
|
950
|
+
body: refreshTokenBody(probe),
|
|
926
951
|
credentials: authFlowMode === "cookie" ? "include" : void 0
|
|
927
952
|
});
|
|
928
953
|
const body = await res.json().catch(() => ({}));
|
|
929
954
|
if (epoch !== sessionEpoch) return supersededRefresh();
|
|
955
|
+
if (res.status === 204) throw new RebaseClientError$1("Not signed in: this browser holds no session to restore.", { code: "NOT_SIGNED_IN" });
|
|
930
956
|
if (!res.ok) {
|
|
931
957
|
const err = apiError(res.status, body, res.statusText);
|
|
932
958
|
if (isMalformedRefreshError(err)) console.error(`[rebase] POST ${authPath}/refresh was refused as malformed (${res.status}${err.code ? ` ${err.code}` : ""}): ${err.message}. The client and server disagree on the refresh request, so this session cannot be renewed and retrying will not help. Check that @rebasepro/client and @rebasepro/server are on compatible versions, and that \`authFlowMode\` matches the server's \`cookieAuth\`.`);
|
|
@@ -1644,10 +1670,10 @@ function createAuth(transport, options) {
|
|
|
1644
1670
|
});
|
|
1645
1671
|
} else settleRestore("none");
|
|
1646
1672
|
else if (authFlowMode === "cookie") {
|
|
1647
|
-
const restore =
|
|
1673
|
+
const restore = probeSession().then(() => {
|
|
1648
1674
|
settleRestore("restored");
|
|
1649
1675
|
}).catch((err) => {
|
|
1650
|
-
settleRestore(isFatalRefreshError(err) || isMalformedRefreshError(err) ? currentSession ? "restored" : "none" : currentSession ? "restored" : "unreachable");
|
|
1676
|
+
settleRestore(isFatalRefreshError(err) || isMalformedRefreshError(err) || err instanceof RebaseClientError$1 && err.code === "NOT_SIGNED_IN" ? currentSession ? "restored" : "none" : currentSession ? "restored" : "unreachable");
|
|
1651
1677
|
}).finally(() => {
|
|
1652
1678
|
if (cookieRestore === restore) cookieRestore = null;
|
|
1653
1679
|
});
|
|
@@ -1801,16 +1827,19 @@ function createAdmin(transport, options) {
|
|
|
1801
1827
|
}
|
|
1802
1828
|
/**
|
|
1803
1829
|
* Import accounts from another system, keeping their ids (`id`, often a
|
|
1804
|
-
* uuid) so the rows that point at them still do. No
|
|
1805
|
-
*
|
|
1806
|
-
*
|
|
1830
|
+
* uuid) so the rows that point at them still do. No email, and no
|
|
1831
|
+
* password unless each account carries the other system's hash as
|
|
1832
|
+
* `passwordHash` and `passwordHashAlgorithm` names its format: without
|
|
1833
|
+
* one, an imported account signs in by magic link, an email code, a
|
|
1834
|
+
* provider or a password reset. An account whose id or email is already taken is
|
|
1807
1835
|
* `skipped`, never updated. At most `MAX_USERS_PER_IMPORT` (100) accounts
|
|
1808
1836
|
* per call; `dryRun: true` checks them and writes nothing.
|
|
1809
1837
|
*/
|
|
1810
1838
|
async function importUsers(users, options) {
|
|
1811
1839
|
const body = {
|
|
1812
1840
|
users,
|
|
1813
|
-
...options?.dryRun ? { dryRun: true } : {}
|
|
1841
|
+
...options?.dryRun ? { dryRun: true } : {},
|
|
1842
|
+
...options?.passwordHashAlgorithm ? { passwordHashAlgorithm: options.passwordHashAlgorithm } : {}
|
|
1814
1843
|
};
|
|
1815
1844
|
return transport.request(adminPath + "/users/import", {
|
|
1816
1845
|
method: "POST",
|
|
@@ -2653,8 +2682,24 @@ function readSqlScriptResult(payload) {
|
|
|
2653
2682
|
if (typeof record?.rowCount === "number") result.rowCount = record.rowCount;
|
|
2654
2683
|
if (record?.truncated === true) result.truncated = true;
|
|
2655
2684
|
if (typeof record?.effectiveRole === "string") result.effectiveRole = record.effectiveRole;
|
|
2685
|
+
if (Array.isArray(record?.statements) && record.statements.length > 0) {
|
|
2686
|
+
const entries = record.statements;
|
|
2687
|
+
result.statements = entries.map((entry, index) => readStatement(entry, index === entries.length - 1 ? rows : []));
|
|
2688
|
+
}
|
|
2656
2689
|
return result;
|
|
2657
2690
|
}
|
|
2691
|
+
function readStatement(value, fallbackRows) {
|
|
2692
|
+
const record = asRecord(value);
|
|
2693
|
+
const statement = {
|
|
2694
|
+
rows: Array.isArray(record?.rows) ? record.rows.map(asRecord).filter(isDefined).map(textRow) : fallbackRows,
|
|
2695
|
+
columns: Array.isArray(record?.columns) ? record.columns.map(readColumn).filter(isDefined) : [],
|
|
2696
|
+
tables: Array.isArray(record?.tables) ? record.tables.map(readTable).filter(isDefined) : []
|
|
2697
|
+
};
|
|
2698
|
+
if (typeof record?.command === "string") statement.command = record.command;
|
|
2699
|
+
if (typeof record?.rowCount === "number") statement.rowCount = record.rowCount;
|
|
2700
|
+
if (record?.truncated === true) statement.truncated = true;
|
|
2701
|
+
return statement;
|
|
2702
|
+
}
|
|
2658
2703
|
function isDefined(value) {
|
|
2659
2704
|
return value !== void 0;
|
|
2660
2705
|
}
|