@supatype/react 0.3.5 → 0.7.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/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +19 -0
- package/.turbo/turbo-typecheck.log +2 -2
- package/LICENSE +1 -1
- package/dist/RichText.d.ts +9 -6
- package/dist/RichText.d.ts.map +1 -1
- package/dist/RichText.js +27 -10
- package/dist/RichText.js.map +1 -1
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +2 -1
- package/dist/context.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/live-preview-message.d.ts +19 -0
- package/dist/live-preview-message.d.ts.map +1 -0
- package/dist/live-preview-message.js +45 -0
- package/dist/live-preview-message.js.map +1 -0
- package/dist/useAuth.d.ts +7 -2
- package/dist/useAuth.d.ts.map +1 -1
- package/dist/useAuth.js +15 -10
- package/dist/useAuth.js.map +1 -1
- package/dist/useFunction.d.ts.map +1 -1
- package/dist/useFunction.js +15 -9
- package/dist/useFunction.js.map +1 -1
- package/dist/useLivePreview.d.ts +12 -1
- package/dist/useLivePreview.d.ts.map +1 -1
- package/dist/useLivePreview.js +18 -12
- package/dist/useLivePreview.js.map +1 -1
- package/dist/useMutation.d.ts +11 -6
- package/dist/useMutation.d.ts.map +1 -1
- package/dist/useMutation.js +14 -30
- package/dist/useMutation.js.map +1 -1
- package/dist/useQuery.d.ts.map +1 -1
- package/dist/useQuery.js +5 -6
- package/dist/useQuery.js.map +1 -1
- package/package.json +10 -5
- package/src/RichText.tsx +32 -19
- package/src/context.tsx +3 -2
- package/src/index.ts +1 -0
- package/src/live-preview-message.ts +52 -0
- package/src/useAuth.ts +21 -12
- package/src/useFunction.ts +13 -9
- package/src/useLivePreview.ts +26 -13
- package/src/useMutation.ts +21 -34
- package/src/useQuery.ts +8 -9
- package/tests/RichText.test.tsx +77 -0
- package/tests/tsconfig.json +9 -0
- package/tests/useAuth-redirect.test.tsx +89 -0
- package/tests/useAuth-signup.test.tsx +61 -0
- package/tests/useLivePreview-origin.test.tsx +108 -0
- package/tests/useMutation-requests.test.tsx +188 -0
- package/tests/useSubscription-status.test.tsx +48 -0
- package/tsconfig.json +1 -1
- package/tsconfig.tsbuildinfo +1 -1
package/.turbo/turbo-build.log
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
> @supatype/react@0.7.0 test /home/runner/work/supatype/supatype/packages/react
|
|
3
|
+
> vitest run
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
[1m[46m RUN [49m[22m [36mv4.0.18 [39m[90m/home/runner/work/supatype/supatype/packages/react[39m
|
|
7
|
+
|
|
8
|
+
[32m✓[39m tests/useLivePreview-origin.test.tsx [2m([22m[2m6 tests[22m[2m)[22m[32m 158[2mms[22m[39m
|
|
9
|
+
[32m✓[39m tests/useAuth-redirect.test.tsx [2m([22m[2m2 tests[22m[2m)[22m[32m 293[2mms[22m[39m
|
|
10
|
+
[32m✓[39m tests/useMutation-requests.test.tsx [2m([22m[2m12 tests[22m[2m)[22m[33m 539[2mms[22m[39m
|
|
11
|
+
[32m✓[39m tests/RichText.test.tsx [2m([22m[2m16 tests[22m[2m)[22m[32m 172[2mms[22m[39m
|
|
12
|
+
[32m✓[39m tests/useAuth-signup.test.tsx [2m([22m[2m1 test[22m[2m)[22m[32m 212[2mms[22m[39m
|
|
13
|
+
[32m✓[39m tests/useSubscription-status.test.tsx [2m([22m[2m2 tests[22m[2m)[22m[32m 268[2mms[22m[39m
|
|
14
|
+
|
|
15
|
+
[2m Test Files [22m [1m[32m6 passed[39m[22m[90m (6)[39m
|
|
16
|
+
[2m Tests [22m [1m[32m39 passed[39m[22m[90m (39)[39m
|
|
17
|
+
[2m Start at [22m 12:11:52
|
|
18
|
+
[2m Duration [22m 8.36s[2m (transform 8.06s, setup 0ms, import 13.56s, tests 1.64s, environment 1ms)[22m
|
|
19
|
+
|
package/LICENSE
CHANGED
package/dist/RichText.d.ts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type ReactElement } from "react";
|
|
2
|
+
import { type RichTextContent } from "@supatype/common/richtext";
|
|
2
3
|
export interface RichTextProps {
|
|
3
|
-
/** Lexical
|
|
4
|
-
content:
|
|
4
|
+
/** A saved Lexical document: the editor state (`{ root }`), its root node, a list of nodes, or null. */
|
|
5
|
+
content: RichTextContent;
|
|
5
6
|
/** Optional CSS class applied to the wrapper element. */
|
|
6
7
|
className?: string;
|
|
7
8
|
/** Wrapper element tag. Defaults to `"div"`. */
|
|
8
9
|
as?: "div" | "article" | "section" | "aside" | "main";
|
|
9
10
|
}
|
|
10
11
|
/**
|
|
11
|
-
* Renders a Lexical rich
|
|
12
|
+
* Renders a saved Lexical rich text document as React elements.
|
|
12
13
|
*
|
|
13
|
-
*
|
|
14
|
+
* The document is walked by `richTextTree` from @supatype/common, the same walker the HTML string
|
|
15
|
+
* renderer uses, so both print the same thing. Text becomes React text children, never HTML, and
|
|
16
|
+
* links only keep a safe href. Works in both Server Components and Client Components.
|
|
14
17
|
*/
|
|
15
|
-
export declare function RichText({ content, className, as: Tag
|
|
18
|
+
export declare function RichText({ content, className, as: Tag }: RichTextProps): ReactElement | null;
|
|
16
19
|
//# sourceMappingURL=RichText.d.ts.map
|
package/dist/RichText.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RichText.d.ts","sourceRoot":"","sources":["../src/RichText.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"RichText.d.ts","sourceRoot":"","sources":["../src/RichText.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAiB,KAAK,YAAY,EAAkB,MAAM,OAAO,CAAA;AACxE,OAAO,EAIL,KAAK,eAAe,EACrB,MAAM,2BAA2B,CAAA;AAElC,MAAM,WAAW,aAAa;IAC5B,wGAAwG;IACxG,OAAO,EAAE,eAAe,CAAA;IACxB,yDAAyD;IACzD,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,gDAAgD;IAChD,EAAE,CAAC,EAAE,KAAK,GAAG,SAAS,GAAG,SAAS,GAAG,OAAO,GAAG,MAAM,CAAA;CACtD;AAkBD;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,EAAE,EAAE,GAAW,EAAE,EAAE,aAAa,GAAG,YAAY,GAAG,IAAI,CAGpG"}
|
package/dist/RichText.js
CHANGED
|
@@ -1,17 +1,34 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import {
|
|
2
|
+
import { createElement } from "react";
|
|
3
|
+
import { richTextTree, } from "@supatype/common/richtext";
|
|
4
|
+
/** React's names for the two attributes that differ from HTML, in the order the walker set them. */
|
|
5
|
+
function reactProps(attributes, key) {
|
|
6
|
+
const props = { key };
|
|
7
|
+
for (const [name, value] of Object.entries(attributes)) {
|
|
8
|
+
if (name === "class")
|
|
9
|
+
props["className"] = value;
|
|
10
|
+
else if (name === "checked")
|
|
11
|
+
props["defaultChecked"] = value;
|
|
12
|
+
else
|
|
13
|
+
props[name] = value;
|
|
14
|
+
}
|
|
15
|
+
return props;
|
|
16
|
+
}
|
|
17
|
+
function toReactNode(child, index) {
|
|
18
|
+
if (typeof child === "string")
|
|
19
|
+
return child;
|
|
20
|
+
return createElement(child.tag, reactProps(child.attributes, index), ...child.children.map(toReactNode));
|
|
21
|
+
}
|
|
3
22
|
/**
|
|
4
|
-
* Renders a Lexical rich
|
|
23
|
+
* Renders a saved Lexical rich text document as React elements.
|
|
5
24
|
*
|
|
6
|
-
*
|
|
25
|
+
* The document is walked by `richTextTree` from @supatype/common, the same walker the HTML string
|
|
26
|
+
* renderer uses, so both print the same thing. Text becomes React text children, never HTML, and
|
|
27
|
+
* links only keep a safe href. Works in both Server Components and Client Components.
|
|
7
28
|
*/
|
|
8
|
-
export function RichText({ content, className, as: Tag = "div"
|
|
9
|
-
if (content
|
|
29
|
+
export function RichText({ content, className, as: Tag = "div" }) {
|
|
30
|
+
if (content === null || content === undefined)
|
|
10
31
|
return null;
|
|
11
|
-
|
|
12
|
-
? { type: "root", children: content }
|
|
13
|
-
: content;
|
|
14
|
-
const html = renderRichText(node);
|
|
15
|
-
return _jsx(Tag, { className: className, dangerouslySetInnerHTML: { __html: html } });
|
|
32
|
+
return _jsx(Tag, { ...(className !== undefined && { className }), children: richTextTree(content).map(toReactNode) });
|
|
16
33
|
}
|
|
17
34
|
//# sourceMappingURL=RichText.js.map
|
package/dist/RichText.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RichText.js","sourceRoot":"","sources":["../src/RichText.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"RichText.js","sourceRoot":"","sources":["../src/RichText.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,aAAa,EAAqC,MAAM,OAAO,CAAA;AACxE,OAAO,EACL,YAAY,GAIb,MAAM,2BAA2B,CAAA;AAWlC,oGAAoG;AACpG,SAAS,UAAU,CAAC,UAA8B,EAAE,GAAW;IAC7D,MAAM,KAAK,GAA4B,EAAE,GAAG,EAAE,CAAA;IAC9C,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC;QACvD,IAAI,IAAI,KAAK,OAAO;YAAE,KAAK,CAAC,WAAW,CAAC,GAAG,KAAK,CAAA;aAC3C,IAAI,IAAI,KAAK,SAAS;YAAE,KAAK,CAAC,gBAAgB,CAAC,GAAG,KAAK,CAAA;;YACvD,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,CAAA;IAC1B,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAED,SAAS,WAAW,CAAC,KAAoB,EAAE,KAAa;IACtD,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAA;IAC3C,OAAO,aAAa,CAAC,KAAK,CAAC,GAAG,EAAE,UAAU,CAAC,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,EAAE,GAAG,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,CAAA;AAC1G,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,QAAQ,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,EAAE,EAAE,GAAG,GAAG,KAAK,EAAiB;IAC7E,IAAI,OAAO,KAAK,IAAI,IAAI,OAAO,KAAK,SAAS;QAAE,OAAO,IAAI,CAAA;IAC1D,OAAO,KAAC,GAAG,OAAK,CAAC,SAAS,KAAK,SAAS,IAAI,EAAE,SAAS,EAAE,CAAC,YAAG,YAAY,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,WAAW,CAAC,GAAO,CAAA;AAC5G,CAAC"}
|
package/dist/context.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,KAAK,EAAE,SAAS,EAAE,cAAc,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,KAAK,EAAE,SAAS,EAAE,cAAc,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAGjG,uGAAuG;AACvG,eAAO,MAAM,eAAe,iCAA8C,CAAA;AAE1E,MAAM,WAAW,qBAAqB;IACpC,MAAM,EAAE,SAAS,CAAA;IACjB,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;CAC1B;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,gBAAgB,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,qBAAqB,GAAG,KAAK,CAAC,YAAY,CAEhG;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,SAAS,SAAS,WAAW,GAAG,iBAAiB,KAAK,cAAc,CAAC,SAAS,CAAC,CAM1G"}
|
package/dist/context.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import React from "react";
|
|
3
|
+
import { isClientFor } from "@supatype/client/bindings";
|
|
3
4
|
/** Typed at `AnyClient`, narrowed on read. See `AnyClient` for why it is neither generic nor `any`. */
|
|
4
5
|
export const SupatypeContext = React.createContext(null);
|
|
5
6
|
/**
|
|
@@ -28,7 +29,7 @@ export function SupatypeProvider({ client, children }) {
|
|
|
28
29
|
*/
|
|
29
30
|
export function useSupatype() {
|
|
30
31
|
const client = React.useContext(SupatypeContext);
|
|
31
|
-
if (client === null) {
|
|
32
|
+
if (client === null || !isClientFor(client)) {
|
|
32
33
|
throw new Error("useSupatype must be used inside a <SupatypeProvider>");
|
|
33
34
|
}
|
|
34
35
|
return client;
|
package/dist/context.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,OAAO,KAAK,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAA;AAEvD,uGAAuG;AACvG,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,CAAC,aAAa,CAAmB,IAAI,CAAC,CAAA;AAO1E;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,gBAAgB,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAyB;IAC1E,OAAO,KAAK,CAAC,aAAa,CAAC,eAAe,CAAC,QAAQ,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,QAAQ,CAAC,CAAA;AACnF,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW;IACzB,MAAM,MAAM,GAAG,KAAK,CAAC,UAAU,CAAC,eAAe,CAAC,CAAA;IAChD,IAAI,MAAM,KAAK,IAAI,IAAI,CAAC,WAAW,CAAY,MAAM,CAAC,EAAE,CAAC;QACvD,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAA;IACzE,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export { useSubscription } from "./useSubscription.js";
|
|
|
10
10
|
export type { UseSubscriptionOptions, UseSubscriptionResult, SubscriptionStatus } from "./useSubscription.js";
|
|
11
11
|
export { useLivePreview } from "./useLivePreview.js";
|
|
12
12
|
export type { UseLivePreviewOptions, UseLivePreviewResult } from "./useLivePreview.js";
|
|
13
|
+
export type { StudioOrigins } from "./live-preview-message.js";
|
|
13
14
|
export { useFunction } from "./useFunction.js";
|
|
14
15
|
export type { UseFunctionResult } from "./useFunction.js";
|
|
15
16
|
export { RichText } from "./RichText.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAC7E,YAAY,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAA;AAEzD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,YAAY,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAEjD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AACxC,YAAY,EAAE,eAAe,EAAE,cAAc,EAA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M,2BAA2B,CAAA;AAE9D,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,YAAY,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAEzD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AACxC,YAAY,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA"}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAG7E,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAGtC,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAGxC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAG9C,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAGtD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAG7E,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAGtC,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAGxC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAG9C,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAGtD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAIpD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAG9C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** One URL or origin, or several, that Studio may post previews from. */
|
|
2
|
+
export type StudioOrigins = string | readonly string[];
|
|
3
|
+
/**
|
|
4
|
+
* The URLs Studio may post from, as one string: the ones `configured` names, else the client URL.
|
|
5
|
+
*
|
|
6
|
+
* A string so a hook can depend on it: a caller's inline array is a new array every render, and the
|
|
7
|
+
* listener should be replaced only when the URLs themselves change.
|
|
8
|
+
*/
|
|
9
|
+
export declare function studioUrlsKey(configured: StudioOrigins | undefined, clientUrl: string | undefined): string;
|
|
10
|
+
/** The exact origins a `studioUrlsKey` names; an entry that is not an absolute URL names none. */
|
|
11
|
+
export declare function trustedStudioOrigins(urlsKey: string): ReadonlySet<string>;
|
|
12
|
+
/**
|
|
13
|
+
* Say, once per origin, that a preview from `origin` was refused.
|
|
14
|
+
*
|
|
15
|
+
* A Studio served somewhere other than the client's URL is a configuration a developer has to make,
|
|
16
|
+
* and without this the only symptom is a preview that never updates.
|
|
17
|
+
*/
|
|
18
|
+
export declare function warnRefusedOrigin(origin: string, warned: Set<string>): void;
|
|
19
|
+
//# sourceMappingURL=live-preview-message.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"live-preview-message.d.ts","sourceRoot":"","sources":["../src/live-preview-message.ts"],"names":[],"mappings":"AAgBA,yEAAyE;AACzE,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,SAAS,MAAM,EAAE,CAAA;AAKtD;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,UAAU,EAAE,aAAa,GAAG,SAAS,EAAE,SAAS,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAG1G;AAED,kGAAkG;AAClG,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,CAEzE;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,IAAI,CAO3E"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which `message` events `useLivePreview` believes.
|
|
3
|
+
*
|
|
4
|
+
* A preview message repaints the page with whatever it carries, so it is only applied when Studio
|
|
5
|
+
* sent it. Two checks, taken from the two editors that solved this first:
|
|
6
|
+
*
|
|
7
|
+
* - The sender's origin is compared exactly with the origins Studio is served from, as Payload's
|
|
8
|
+
* live preview compares `event.origin` with its `serverURL`.
|
|
9
|
+
* - The sending window must be the one this page is framed by, as Sanity's comlink pins the one
|
|
10
|
+
* window it handshook with. Studio's pane is that parent; any other window is not Studio.
|
|
11
|
+
*
|
|
12
|
+
* Studio is served at `/studio/` on the project's API URL, so the client's URL names its origin
|
|
13
|
+
* unless the app says otherwise. With neither, nothing is trusted: an unknown sender is never one.
|
|
14
|
+
*/
|
|
15
|
+
import { originOf } from "@supatype/client/bindings";
|
|
16
|
+
/** A space cannot appear in a URL, so it cannot split one. */
|
|
17
|
+
const URL_SEPARATOR = " ";
|
|
18
|
+
/**
|
|
19
|
+
* The URLs Studio may post from, as one string: the ones `configured` names, else the client URL.
|
|
20
|
+
*
|
|
21
|
+
* A string so a hook can depend on it: a caller's inline array is a new array every render, and the
|
|
22
|
+
* listener should be replaced only when the URLs themselves change.
|
|
23
|
+
*/
|
|
24
|
+
export function studioUrlsKey(configured, clientUrl) {
|
|
25
|
+
const urls = configured ?? clientUrl ?? [];
|
|
26
|
+
return typeof urls === "string" ? urls : urls.join(URL_SEPARATOR);
|
|
27
|
+
}
|
|
28
|
+
/** The exact origins a `studioUrlsKey` names; an entry that is not an absolute URL names none. */
|
|
29
|
+
export function trustedStudioOrigins(urlsKey) {
|
|
30
|
+
return new Set(urlsKey.split(URL_SEPARATOR).map((url) => originOf(url)).filter((origin) => origin !== null));
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Say, once per origin, that a preview from `origin` was refused.
|
|
34
|
+
*
|
|
35
|
+
* A Studio served somewhere other than the client's URL is a configuration a developer has to make,
|
|
36
|
+
* and without this the only symptom is a preview that never updates.
|
|
37
|
+
*/
|
|
38
|
+
export function warnRefusedOrigin(origin, warned) {
|
|
39
|
+
if (warned.has(origin))
|
|
40
|
+
return;
|
|
41
|
+
warned.add(origin);
|
|
42
|
+
console.warn(`[supatype] useLivePreview ignored a live preview message from ${origin}, which is not a trusted ` +
|
|
43
|
+
"Studio origin. If Studio is served from there, pass it as `studioOrigin`.");
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=live-preview-message.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"live-preview-message.js","sourceRoot":"","sources":["../src/live-preview-message.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAA;AAKpD,8DAA8D;AAC9D,MAAM,aAAa,GAAG,GAAG,CAAA;AAEzB;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,UAAqC,EAAE,SAA6B;IAChG,MAAM,IAAI,GAAG,UAAU,IAAI,SAAS,IAAI,EAAE,CAAA;IAC1C,OAAO,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,CAAA;AACnE,CAAC;AAED,kGAAkG;AAClG,MAAM,UAAU,oBAAoB,CAAC,OAAe;IAClD,OAAO,IAAI,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,KAAK,IAAI,CAAC,CAAC,CAAA;AAC9G,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAc,EAAE,MAAmB;IACnE,IAAI,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC;QAAE,OAAM;IAC9B,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;IAClB,OAAO,CAAC,IAAI,CACV,iEAAiE,MAAM,2BAA2B;QAChG,2EAA2E,CAC9E,CAAA;AACH,CAAC"}
|
package/dist/useAuth.d.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
import type { Session, User, SupatypeError } from "@supatype/client";
|
|
1
|
+
import type { Session, User, AuthRedirectError, SupatypeError } from "@supatype/client";
|
|
2
2
|
export interface UseAuthReturn {
|
|
3
3
|
/** The currently authenticated user, or null if not signed in. */
|
|
4
4
|
user: User | null;
|
|
5
5
|
/** The current session (includes access token), or null. */
|
|
6
6
|
session: Session | null;
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* True while loading the initial auth state, which includes finishing a sign-in the page's URL
|
|
9
|
+
* carries (an OAuth or email-link redirect).
|
|
10
|
+
*/
|
|
8
11
|
loading: boolean;
|
|
12
|
+
/** The sign-in the auth server refused and reported in the page's URL, or null. */
|
|
13
|
+
error: AuthRedirectError | null;
|
|
9
14
|
/** Sign up with email and password. */
|
|
10
15
|
signUp(credentials: {
|
|
11
16
|
email: string;
|
package/dist/useAuth.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../src/useAuth.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAmB,aAAa,EAAE,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../src/useAuth.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAmB,iBAAiB,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAGxG,MAAM,WAAW,aAAa;IAC5B,kEAAkE;IAClE,IAAI,EAAE,IAAI,GAAG,IAAI,CAAA;IACjB,4DAA4D;IAC5D,OAAO,EAAE,OAAO,GAAG,IAAI,CAAA;IACvB;;;O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AAO,CAAC,EAAE;YAAE,eAAe,CAAC,EAAE,MAAM,CAAA;SAAE,GAAG,SAAS,CAAA;KACnD,GAAG,OAAO,CAAC;QAAE,IAAI,EAAE;YAAE,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;SAAE,CAAC;QAAC,KAAK,EAAE,aAAa,GAAG,IAAI,CAAA;KAAE,CAAC,CAAA;IACtF,iCAAiC;IACjC,OAAO,IAAI,OAAO,CAAC;QAAE,KAAK,EAAE,aAAa,GAAG,IAAI,CAAA;KAAE,CAAC,CAAA;CACpD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,OAAO,IAAI,aAAa,CAuCvC"}
|
package/dist/useAuth.js
CHANGED
|
@@ -15,27 +15,32 @@ import { useSupatype } from "./context.js";
|
|
|
15
15
|
*/
|
|
16
16
|
export function useAuth() {
|
|
17
17
|
const client = useSupatype();
|
|
18
|
-
const [user, setUser] = useState(null);
|
|
19
18
|
const [session, setSession] = useState(null);
|
|
20
19
|
const [loading, setLoading] = useState(true);
|
|
20
|
+
const [error, setError] = useState(null);
|
|
21
21
|
useEffect(() => {
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
let active = true;
|
|
23
|
+
// Seed once the client has started: the stored session, or the one the page's URL carried.
|
|
24
|
+
void client.auth.initialize().then(async ({ error: redirectError }) => {
|
|
25
|
+
const { data } = await client.auth.getSession();
|
|
26
|
+
if (!active)
|
|
27
|
+
return;
|
|
24
28
|
setSession(data.session);
|
|
25
|
-
|
|
29
|
+
setError(redirectError);
|
|
26
30
|
setLoading(false);
|
|
27
31
|
});
|
|
28
32
|
// Subscribe to future changes
|
|
29
|
-
const { data: { subscription } } = client.auth.onAuthStateChange((_event, newSession) =>
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
const { data: { subscription } } = client.auth.onAuthStateChange((_event, newSession) => setSession(newSession));
|
|
34
|
+
return () => {
|
|
35
|
+
active = false;
|
|
36
|
+
subscription.unsubscribe();
|
|
37
|
+
};
|
|
34
38
|
}, [client]);
|
|
35
39
|
return {
|
|
36
|
-
user,
|
|
40
|
+
user: session?.user ?? null,
|
|
37
41
|
session,
|
|
38
42
|
loading,
|
|
43
|
+
error,
|
|
39
44
|
signUp: (creds) => client.auth.signUp(creds),
|
|
40
45
|
signIn: (creds) => client.auth.signInWithPassword(creds),
|
|
41
46
|
signInWithOAuth: (opts) => client.auth.signInWithOAuth(opts),
|
package/dist/useAuth.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAuth.js","sourceRoot":"","sources":["../src/useAuth.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAE3C,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"useAuth.js","sourceRoot":"","sources":["../src/useAuth.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAE3C,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAuC1C;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,OAAO;IACrB,MAAM,MAAM,GAAG,WAAW,EAAE,CAAA;IAC5B,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAiB,IAAI,CAAC,CAAA;IAC5D,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAA;IAC5C,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAA2B,IAAI,CAAC,CAAA;IAElE,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,MAAM,GAAG,IAAI,CAAA;QACjB,2FAA2F;QAC3F,KAAK,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,EA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AC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useFunction.d.ts","sourceRoot":"","sources":["../src/useFunction.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"useFunction.d.ts","sourceRoot":"","sources":["../src/useFunction.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAIrF,MAAM,WAAW,mBAAmB;IAClC,iCAAiC;IACjC,MAAM,CAAC,EAAE,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,OAAO,GAAG,QAAQ,GAAG,SAAS,CAAA;IAChE,4BAA4B;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,CAAA;CAC7C;AAED,MAAM,WAAW,iBAAiB,CAAC,SAAS;IAC1C,gDAAgD;IAChD,MAAM,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,SAAS,GAAG,OAAO,CAAC;QAAE,IAAI,EAAE,SAAS,GAAG,IAAI,CAAC;QAAC,KAAK,EAAE,aAAa,GAAG,IAAI,CAAA;KAAE,CAAC,CAAA;IACnI,gCAAgC;IAChC,IAAI,EAAE,SAAS,GAAG,IAAI,CAAA;IACtB,wBAAwB;IACxB,KAAK,EAAE,aAAa,GAAG,IAAI,CAAA;IAC3B,2CAA2C;IAC3C,OAAO,EAAE,OAAO,CAAA;CACjB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CACzB,SAAS,GAAG,OAAO,EACnB,SAAS,SAAS,WAAW,GAAG,iBAAiB,EAEjD,YAAY,EAAE,MAAM,GACnB,iBAAiB,CAAC,SAAS,CAAC,CAgC9B"}
|
package/dist/useFunction.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { useState, useCallback } from "react";
|
|
3
3
|
import { useSupatype } from "./context.js";
|
|
4
|
+
import { isCallerValue } from "@supatype/client/bindings";
|
|
4
5
|
/**
|
|
5
6
|
* Invoke a Supatype Edge Function.
|
|
6
7
|
*
|
|
@@ -22,15 +23,20 @@ export function useFunction(functionName) {
|
|
|
22
23
|
const invoke = useCallback(async (body, options) => {
|
|
23
24
|
setLoading(true);
|
|
24
25
|
setError(null);
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
26
|
+
try {
|
|
27
|
+
const result = await client.functions.invoke(functionName, {
|
|
28
|
+
...options,
|
|
29
|
+
...(body !== undefined ? { body } : {}),
|
|
30
|
+
});
|
|
31
|
+
const answer = { data: isCallerValue(result.data) ? result.data : null, error: result.error };
|
|
32
|
+
setData(answer.data);
|
|
33
|
+
if (answer.error !== null)
|
|
34
|
+
setError(answer.error);
|
|
35
|
+
return answer;
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
setLoading(false);
|
|
39
|
+
}
|
|
34
40
|
},
|
|
35
41
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
36
42
|
[client, functionName]);
|
package/dist/useFunction.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useFunction.js","sourceRoot":"","sources":["../src/useFunction.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,OAAO,CAAA;AAE7C,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"useFunction.js","sourceRoot":"","sources":["../src/useFunction.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,OAAO,CAAA;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K,CAAC,CAAA;QACnB,CAAC;IACH,CAAC;IACD,uDAAuD;IACvD,CAAC,MAAM,EAAE,YAAY,CAAC,CACvB,CAAA;IAED,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,CAAA;AACzC,CAAC"}
|
package/dist/useLivePreview.d.ts
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
|
+
import { type StudioOrigins } from "./live-preview-message.js";
|
|
1
2
|
export interface UseLivePreviewOptions<TData> {
|
|
2
3
|
/** The initial/server data to display when not in preview mode. */
|
|
3
4
|
initialData: TData;
|
|
4
5
|
/** Optional model name filter, only accept preview data for this model. */
|
|
5
6
|
model?: string;
|
|
7
|
+
/**
|
|
8
|
+
* Where Studio is served, as a URL or origin, or a list of them. Only messages from these origins
|
|
9
|
+
* are applied.
|
|
10
|
+
*
|
|
11
|
+
* Defaults to the origin of the `<SupatypeProvider>` client's URL, since Studio is served at
|
|
12
|
+
* `/studio/` on the project's API URL. Set it when Studio lives elsewhere, or when this page has
|
|
13
|
+
* no provider: with neither, every preview message is ignored.
|
|
14
|
+
*/
|
|
15
|
+
studioOrigin?: StudioOrigins;
|
|
6
16
|
}
|
|
7
17
|
export interface UseLivePreviewResult<TData> {
|
|
8
18
|
/** The current data: either live preview data or initial data. */
|
|
@@ -15,7 +25,8 @@ export interface UseLivePreviewResult<TData> {
|
|
|
15
25
|
*
|
|
16
26
|
* When the admin panel's edit view is open with Live Preview enabled,
|
|
17
27
|
* it sends `postMessage` events with form data as the editor types.
|
|
18
|
-
* This hook receives those events and returns the live data.
|
|
28
|
+
* This hook receives those events and returns the live data. A message is applied only when it
|
|
29
|
+
* comes from the window framing this page, at a Studio origin (see `studioOrigin`).
|
|
19
30
|
*
|
|
20
31
|
* @example
|
|
21
32
|
* ```tsx
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useLivePreview.d.ts","sourceRoot":"","sources":["../src/useLivePreview.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useLivePreview.d.ts","sourceRoot":"","sources":["../src/useLivePreview.ts"],"names":[],"mappings":"AAKA,OAAO,EAA0D,KAAK,aAAa,EAAE,MAAM,2BAA2B,CAAA;AAEtH,MAAM,WAAW,qBAAqB,CAAC,KAAK;IAC1C,mEAAmE;IACnE,WAAW,EAAE,KAAK,CAAA;IAClB,2EAA2E;IAC3E,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;;;;;;OAOG;IACH,YAAY,CAAC,EAAE,aAAa,CAAA;CAC7B;AAED,MAAM,WAAW,oBAAoB,CAAC,KAAK;IACzC,kEAAkE;IAClE,IAAI,EAAE,KAAK,CAAA;IACX,yEAAyE;IACzE,SAAS,EAAE,OAAO,CAAA;CACnB;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAClC,IAAI,EAAE,qBAAqB,CAAC,KAAK,CAAC,GACjC,oBAAoB,CAAC,KAAK,CAAC,CAsB7B"}
|
package/dist/useLivePreview.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useState, useEffect, useRef } from "react";
|
|
2
|
+
import { useState, useEffect, useRef, useContext } from "react";
|
|
3
|
+
import { isCallerValue, previewMessageOf } from "@supatype/client/bindings";
|
|
4
|
+
import { SupatypeContext } from "./context.js";
|
|
5
|
+
import { studioUrlsKey, trustedStudioOrigins, warnRefusedOrigin } from "./live-preview-message.js";
|
|
3
6
|
/**
|
|
4
7
|
* Hook for receiving live preview updates from the Supatype admin panel.
|
|
5
8
|
*
|
|
6
9
|
* When the admin panel's edit view is open with Live Preview enabled,
|
|
7
10
|
* it sends `postMessage` events with form data as the editor types.
|
|
8
|
-
* This hook receives those events and returns the live data.
|
|
11
|
+
* This hook receives those events and returns the live data. A message is applied only when it
|
|
12
|
+
* comes from the window framing this page, at a Studio origin (see `studioOrigin`).
|
|
9
13
|
*
|
|
10
14
|
* @example
|
|
11
15
|
* ```tsx
|
|
@@ -25,23 +29,25 @@ export function useLivePreview(opts) {
|
|
|
25
29
|
const [previewData, setPreviewData] = useState(null);
|
|
26
30
|
const initialRef = useRef(opts.initialData);
|
|
27
31
|
initialRef.current = opts.initialData;
|
|
32
|
+
const urlsKey = studioUrlsKey(opts.studioOrigin, useContext(SupatypeContext)?.url);
|
|
33
|
+
const warned = useRef(new Set());
|
|
28
34
|
useEffect(() => {
|
|
35
|
+
const trusted = trustedStudioOrigins(urlsKey);
|
|
36
|
+
// Applied only when the framing window sent it, from an origin Studio is served at.
|
|
29
37
|
const handler = (event) => {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const msg = event.data;
|
|
33
|
-
if (msg.type !== "supatype:live-preview")
|
|
38
|
+
const msg = previewMessageOf(event.data);
|
|
39
|
+
if (msg === null || event.source !== window.parent)
|
|
34
40
|
return;
|
|
41
|
+
if (!trusted.has(event.origin))
|
|
42
|
+
return warnRefusedOrigin(event.origin, warned.current);
|
|
35
43
|
if (opts.model && msg.model !== opts.model)
|
|
36
44
|
return;
|
|
37
|
-
|
|
45
|
+
if (isCallerValue(msg.data))
|
|
46
|
+
setPreviewData(msg.data);
|
|
38
47
|
};
|
|
39
48
|
window.addEventListener("message", handler);
|
|
40
49
|
return () => { window.removeEventListener("message", handler); };
|
|
41
|
-
}, [opts.model]);
|
|
42
|
-
return {
|
|
43
|
-
data: previewData ?? initialRef.current,
|
|
44
|
-
isPreview: previewData !== null,
|
|
45
|
-
};
|
|
50
|
+
}, [opts.model, urlsKey]);
|
|
51
|
+
return { data: previewData ?? initialRef.current, isPreview: previewData !== null };
|
|
46
52
|
}
|
|
47
53
|
//# sourceMappingURL=useLivePreview.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useLivePreview.js","sourceRoot":"","sources":["../src/useLivePreview.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,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AE,OAAO,iBAAiB,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC,OAAO,CAAC,CAAA;YACtF,IAAI,IAAI,CAAC,KAAK,IAAI,GAAG,CAAC,KAAK,KAAK,IAAI,CAAC,KAAK;gBAAE,OAAM;YAClD,IAAI,aAAa,CAAQ,GAAG,CAAC,IAAI,CAAC;gBAAE,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;QAC9D,CAAC,CAAA;QACD,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAA;QAC3C,OAAO,GAAG,EAAE,GAAG,MAAM,CAAC,mBAAmB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAA,CAAC,CAAC,CAAA;IACjE,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAA;IAEzB,OAAO,EAAE,IAAI,EAAE,WAAW,IAAI,UAAU,CAAC,OAAO,EAAE,SAAS,EAAE,WAAW,KAAK,IAAI,EAAE,CAAA;AACrF,CAAC"}
|
package/dist/useMutation.d.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import type { AnyDatabase, AugmentedDatabase, SupatypeError } from "@supatype/client";
|
|
2
|
-
|
|
2
|
+
import { type MutationOperation, type MutationOutcome } from "@supatype/client/bindings";
|
|
3
|
+
export type { MutationOperation };
|
|
3
4
|
export interface MutationOptions {
|
|
4
5
|
/** Simple equality filters for update/delete operations */
|
|
5
6
|
filter?: Record<string, unknown> | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* For an update on a model that keeps drafts: publish the drafts it saves in the same call. Without
|
|
9
|
+
* it the update saves a new draft of each record and the live rows stay as they are.
|
|
10
|
+
*/
|
|
11
|
+
publish?: boolean | undefined;
|
|
6
12
|
}
|
|
7
13
|
export interface UseMutationResult<TRow> {
|
|
8
|
-
mutate(data?: Record<string, unknown> | Record<string, unknown>[] | undefined, options?: MutationOptions | undefined): Promise<
|
|
9
|
-
data: TRow[] | null;
|
|
10
|
-
error: SupatypeError | null;
|
|
11
|
-
count: number | null;
|
|
12
|
-
}>;
|
|
14
|
+
mutate(data?: Record<string, unknown> | Record<string, unknown>[] | undefined, options?: MutationOptions | undefined): Promise<MutationOutcome<TRow>>;
|
|
13
15
|
loading: boolean;
|
|
14
16
|
error: SupatypeError | null;
|
|
15
17
|
}
|
|
@@ -21,6 +23,9 @@ export interface UseMutationResult<TRow> {
|
|
|
21
23
|
* const { mutate: createPost, loading } = useMutation('posts', 'insert')
|
|
22
24
|
* await createPost({ title: 'Hello', status: 'draft' })
|
|
23
25
|
*
|
|
26
|
+
* const { mutate: editPost } = useMutation('posts', 'update')
|
|
27
|
+
* await editPost({ title: 'Hello again' }, { filter: { id: postId }, publish: true })
|
|
28
|
+
*
|
|
24
29
|
* const { mutate: deletePost } = useMutation('posts', 'delete')
|
|
25
30
|
* await deletePost(undefined, { filter: { id: postId } })
|
|
26
31
|
* ```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutation.d.ts","sourceRoot":"","sources":["../src/useMutation.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"useMutation.d.ts","sourceRoot":"","sources":["../src/useMutation.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAa,WAAW,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAEhG,OAAO,EAAe,KAAK,iBAAiB,EAAE,KAAK,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAErG,YAAY,EAAE,iBAAiB,EAAE,CAAA;AAEjC,MAAM,WAAW,eAAe;IAC9B,2DAA2D;IAC3D,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAA;IAC5C;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;CAC9B;AAED,MAAM,WAAW,iBAAiB,CAAC,IAAI;IACrC,MAAM,CACJ,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,SAAS,EACtE,OAAO,CAAC,EAAE,eAAe,GAAG,SAAS,GACpC,OAAO,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAA;IACjC,OAAO,EAAE,OAAO,CAAA;IAChB,KAAK,EAAE,aAAa,GAAG,IAAI,CAAA;CAC5B;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,WAAW,CACzB,SAAS,SAAS,WAAW,GAAG,iBAAiB,EACjD,MAAM,SAAS,MAAM,SAAS,CAAC,QAAQ,CAAC,CAAC,QAAQ,CAAC,GAAG,MAAM,GAAG,MAAM,SAAS,CAAC,QAAQ,CAAC,CAAC,QAAQ,CAAC,GAAG,MAAM,EAC1G,IAAI,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,EAEnD,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,iBAAiB,GAC3B,iBAAiB,CAAC,IAAI,CAAC,CA4BzB"}
|
package/dist/useMutation.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { useState, useCallback } from "react";
|
|
3
3
|
import { useSupatype } from "./context.js";
|
|
4
|
+
import { runMutation } from "@supatype/client/bindings";
|
|
4
5
|
/**
|
|
5
6
|
* Perform insert, update, delete, or upsert on a table.
|
|
6
7
|
*
|
|
@@ -9,6 +10,9 @@ import { useSupatype } from "./context.js";
|
|
|
9
10
|
* const { mutate: createPost, loading } = useMutation('posts', 'insert')
|
|
10
11
|
* await createPost({ title: 'Hello', status: 'draft' })
|
|
11
12
|
*
|
|
13
|
+
* const { mutate: editPost } = useMutation('posts', 'update')
|
|
14
|
+
* await editPost({ title: 'Hello again' }, { filter: { id: postId }, publish: true })
|
|
15
|
+
*
|
|
12
16
|
* const { mutate: deletePost } = useMutation('posts', 'delete')
|
|
13
17
|
* await deletePost(undefined, { filter: { id: postId } })
|
|
14
18
|
* ```
|
|
@@ -20,38 +24,18 @@ export function useMutation(table, operation) {
|
|
|
20
24
|
const mutate = useCallback(async (data, options) => {
|
|
21
25
|
setLoading(true);
|
|
22
26
|
setError(null);
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
result
|
|
31
|
-
}
|
|
32
|
-
else if (operation === "update") {
|
|
33
|
-
let q = tableClient.update(data);
|
|
34
|
-
if (options?.filter !== undefined) {
|
|
35
|
-
for (const [col, val] of Object.entries(options.filter)) {
|
|
36
|
-
q = q.eq(col, val);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
result = await q;
|
|
27
|
+
try {
|
|
28
|
+
const publish = options?.publish;
|
|
29
|
+
const result = await runMutation(client, table, operation, data, options?.filter, {
|
|
30
|
+
...(publish !== undefined && { publish }),
|
|
31
|
+
});
|
|
32
|
+
if (result.error !== null)
|
|
33
|
+
setError(result.error);
|
|
34
|
+
return result;
|
|
40
35
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
let q = tableClient.delete();
|
|
44
|
-
if (options?.filter !== undefined) {
|
|
45
|
-
for (const [col, val] of Object.entries(options.filter)) {
|
|
46
|
-
q = q.eq(col, val);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
result = await q;
|
|
36
|
+
finally {
|
|
37
|
+
setLoading(false);
|
|
50
38
|
}
|
|
51
|
-
setLoading(false);
|
|
52
|
-
if (result.error !== null)
|
|
53
|
-
setError(result.error);
|
|
54
|
-
return result;
|
|
55
39
|
},
|
|
56
40
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
57
41
|
[client, table, operation]);
|