@protobase/ui 0.26.9 → 0.27.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/dist/app/app.d.ts CHANGED
@@ -12,7 +12,7 @@ export type AppProps = {
12
12
  client?: Client;
13
13
  /** Ready-made clients of the sources `/meta` names, by their URL, for stories and tests. */
14
14
  sources?: Record<string, Client>;
15
- /** Sign-in state for the app; made from the page's origin when omitted. */
15
+ /** Sign-in state for the app; made from `baseUrl`'s origin when omitted. */
16
16
  auth?: AuthSession;
17
17
  /** Path prefix the app is mounted under, for example `/admin`. */
18
18
  basePath?: string;
package/dist/app/app.js CHANGED
@@ -309,6 +309,7 @@ const App = ({ auth, workspace, basePath, initialUrl, pluginLoader, locale, ...r
309
309
  setFallbackLocale(locale ?? "en");
310
310
  return /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(AuthProvider, {
311
311
  session: auth,
312
+ ...rest.baseUrl && { origin: new URL(rest.baseUrl, window.location.href).origin },
312
313
  children: /* @__PURE__ */ jsx(Router, {
313
314
  basePath,
314
315
  initialUrl,
package/dist/app/main.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { browserLocale } from "../locale/active-locale.js";
2
2
  import { uiFromModule } from "./pages/ui-from-module.js";
3
3
  import { App } from "./app.js";
4
+ import { pageApiOrigin } from "./page-api-origin.js";
4
5
  import { StrictMode } from "react";
5
6
  import { jsx } from "react/jsx-runtime";
6
7
  import { createRoot } from "react-dom/client";
@@ -10,7 +11,7 @@ import "../styles.css";
10
11
  const root = document.getElementById("root");
11
12
  if (!root) throw new Error("index.html needs an element with id \"root\"");
12
13
  createRoot(root).render(/* @__PURE__ */ jsx(StrictMode, { children: /* @__PURE__ */ jsx(App, {
13
- baseUrl: "/api/v1",
14
+ baseUrl: `${pageApiOrigin() ?? ""}/api/v1`,
14
15
  ui: uiFromModule(projectUi),
15
16
  locale: browserLocale()
16
17
  }) }));
@@ -0,0 +1,18 @@
1
+ import { apiOriginMeta } from "@protobase/schema/internal";
2
+ //#region src/app/page-api-origin.ts
3
+ /**
4
+ * The API origin the page's `<meta name="protobase-api-origin">` tags name: `undefined` without one. Anything but one
5
+ * tag holding exactly an http or https origin, such as `https://shop.example.com`, is an error.
6
+ */
7
+ const apiOriginOf = (contents) => {
8
+ if (contents.length === 0) return void 0;
9
+ if (contents.length > 1) throw new Error(`The page has ${contents.length} ${apiOriginMeta} meta tags; it may have one`);
10
+ const value = contents[0];
11
+ const url = URL.canParse(value) ? new URL(value) : void 0;
12
+ if (!url || url.protocol !== "http:" && url.protocol !== "https:" || url.origin !== value) throw new Error(`The page's ${apiOriginMeta} meta tag holds "${value}", which is not an http or https origin such as https://shop.example.com`);
13
+ return value;
14
+ };
15
+ /** The API origin this page names in its `<meta name="protobase-api-origin">` (see `apiOriginOf`). */
16
+ const pageApiOrigin = () => apiOriginOf([...document.querySelectorAll(`meta[name="${apiOriginMeta}"]`)].map((meta) => meta.content));
17
+ //#endregion
18
+ export { apiOriginOf, pageApiOrigin };
@@ -10,8 +10,11 @@ const useAuth = () => {
10
10
  if (!api) throw new Error("useAuth must be used inside AuthProvider");
11
11
  return api;
12
12
  };
13
- /** Finds out whether the server has a login and who is signed in, and keeps that state for the app. */
14
- const AuthProvider = ({ session: given, children }) => {
13
+ /**
14
+ * Finds out whether the server has a login and who is signed in, and keeps that state for the app. Without a `session`,
15
+ * it makes one on `origin`, the page's own by default.
16
+ */
17
+ const AuthProvider = ({ session: given, origin, children }) => {
15
18
  const [state, setState] = useState({ kind: "loading" });
16
19
  const [signInMethods, setSignInMethods] = useState(["password"]);
17
20
  const [passwordReset, setPasswordReset] = useState(false);
@@ -29,7 +32,10 @@ const AuthProvider = ({ session: given, children }) => {
29
32
  const markSignedOut = useCallback(() => setState((current) => current.kind === "signed-in" ? { kind: "signed-out" } : current), []);
30
33
  const signedOut = useRef(markSignedOut);
31
34
  signedOut.current = markSignedOut;
32
- const session = useMemo(() => given ?? createAuthSession({ onSignedOut: () => signedOut.current() }), [given]);
35
+ const session = useMemo(() => given ?? createAuthSession({
36
+ ...origin && { origin },
37
+ onSignedOut: () => signedOut.current()
38
+ }), [given, origin]);
33
39
  const enter = useCallback(async (user) => {
34
40
  const [{ missing }, staff] = await Promise.all([session.account.signInMethods(), session.staff.current()]);
35
41
  setState(missing.length > 0 ? {
package/dist/package.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "0.26.9";
2
+ var version = "0.27.0";
3
3
  var protobase = { "pluginApi": 1 };
4
4
  //#endregion
5
5
  export { protobase, version };