@opencloudcms/client 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +337 -2
- package/dist/cjs/actions/runner.js +434 -0
- package/dist/cjs/core/account.js +22 -0
- package/dist/cjs/core/actions.js +56 -0
- package/dist/cjs/core/auth.js +73 -0
- package/dist/cjs/core/client.js +160 -0
- package/dist/cjs/core/content.js +89 -0
- package/dist/cjs/core/default-client.js +27 -0
- package/dist/cjs/core/errors.js +53 -0
- package/dist/cjs/core/typed.js +22 -0
- package/dist/cjs/core/types.js +2 -0
- package/dist/cjs/core/utils.js +64 -0
- package/dist/cjs/index.js +37 -0
- package/dist/cjs/next/loopback.js +12 -0
- package/dist/cjs/next/server.js +99 -0
- package/dist/cjs/next/stubs.js +10 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/react/provider.js +162 -0
- package/dist/cjs/react/stubs.js +13 -0
- package/dist/cjs/server.js +38 -0
- package/dist/esm/actions/runner.d.ts +92 -0
- package/dist/esm/actions/runner.js +428 -0
- package/dist/esm/core/account.d.ts +20 -0
- package/dist/esm/core/account.js +19 -0
- package/dist/esm/core/actions.d.ts +81 -0
- package/dist/esm/core/actions.js +51 -0
- package/dist/esm/core/auth.d.ts +61 -0
- package/dist/esm/core/auth.js +69 -0
- package/dist/esm/core/client.d.ts +2 -0
- package/dist/esm/core/client.js +157 -0
- package/dist/esm/core/content.d.ts +5 -0
- package/dist/esm/core/content.js +85 -0
- package/dist/esm/core/default-client.d.ts +9 -0
- package/dist/esm/core/default-client.js +23 -0
- package/dist/esm/core/errors.d.ts +13 -0
- package/dist/esm/core/errors.js +47 -0
- package/dist/esm/core/typed.d.ts +31 -0
- package/dist/esm/core/typed.js +19 -0
- package/dist/esm/core/types.d.ts +90 -0
- package/dist/esm/core/types.js +1 -0
- package/dist/esm/core/utils.d.ts +27 -0
- package/dist/esm/core/utils.js +56 -0
- package/dist/esm/index.d.ts +20 -0
- package/dist/esm/index.js +12 -0
- package/dist/esm/next/loopback.d.ts +6 -0
- package/dist/esm/next/loopback.js +9 -0
- package/dist/esm/next/server.d.ts +19 -0
- package/dist/esm/next/server.js +60 -0
- package/dist/esm/next/stubs.d.ts +5 -0
- package/dist/esm/next/stubs.js +7 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/react/provider.d.ts +38 -0
- package/dist/esm/react/provider.js +153 -0
- package/dist/esm/react/stubs.d.ts +6 -0
- package/dist/esm/react/stubs.js +10 -0
- package/dist/esm/server.d.ts +21 -0
- package/dist/esm/server.js +13 -0
- package/package.json +61 -3
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export { createClient } from './core/client.js';
|
|
2
|
+
export { createContentApi } from './core/typed.js';
|
|
3
|
+
export { setDefaultClientFactory } from './core/default-client.js';
|
|
4
|
+
export { ApiError, isApiError, errorMessage } from './core/errors.js';
|
|
5
|
+
export type { ApiIssue, Client, ClientOptions, ErrorMessages, HttpMethod, MessageLocale, RequestOptions, ResolvedClientOptions, } from './core/types.js';
|
|
6
|
+
export type { ContentApi, TypedContent, TypedListOptions } from './core/typed.js';
|
|
7
|
+
export type { AssetRef, AssetsClient, ContentClient, FilterValue, ListOptions, ListResult, ReadOptions } from './core/types.js';
|
|
8
|
+
export { utils } from './core/utils.js';
|
|
9
|
+
export type { Money, IdentifierKind } from './core/utils.js';
|
|
10
|
+
export type { AuthClient, Customer, SignInMethods } from './core/auth.js';
|
|
11
|
+
export type { AccountClient } from './core/account.js';
|
|
12
|
+
export { loopbackUrl } from './next/loopback.js';
|
|
13
|
+
export { serverClient, publicClient, getCustomer, requireCustomer } from './next/stubs.js';
|
|
14
|
+
export type { ServerClientOptions } from './next/server.js';
|
|
15
|
+
export { ActionRunner, ACTION_EVENT } from './actions/runner.js';
|
|
16
|
+
export type { ActionEventDetail, ActionPhase, ActionRunInput, ActionRunResult, ActionRunnerOptions, UiAdapter } from './actions/runner.js';
|
|
17
|
+
export { defineActionClient } from './core/actions.js';
|
|
18
|
+
export type { ActionClientHooks, ActionManifest, ActionRequest, ActionUi, ActionsClient, RunActionOptions, } from './core/actions.js';
|
|
19
|
+
export { OpenCloudCmsProvider, useClient, useCustomer, useSignIn, useSignOut, useAction, useActionRunner } from './react/provider.js';
|
|
20
|
+
export type { OpenCloudCmsProviderProps, ActionClientDefinition, UseActionState } from './react/provider.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Full build (`import` / `require`): everything, React provider and hooks included. The Next server helpers are
|
|
2
|
+
// stubs here (they throw when called); the `react-server` build (server.ts) has the real ones.
|
|
3
|
+
export { createClient } from './core/client.js';
|
|
4
|
+
export { createContentApi } from './core/typed.js';
|
|
5
|
+
export { setDefaultClientFactory } from './core/default-client.js';
|
|
6
|
+
export { ApiError, isApiError, errorMessage } from './core/errors.js';
|
|
7
|
+
export { utils } from './core/utils.js';
|
|
8
|
+
export { loopbackUrl } from './next/loopback.js';
|
|
9
|
+
export { serverClient, publicClient, getCustomer, requireCustomer } from './next/stubs.js';
|
|
10
|
+
export { ActionRunner, ACTION_EVENT } from './actions/runner.js';
|
|
11
|
+
export { defineActionClient } from './core/actions.js';
|
|
12
|
+
export { OpenCloudCmsProvider, useClient, useCustomer, useSignIn, useSignOut, useAction, useActionRunner } from './react/provider.js';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loopback base URL of the OpenCloudCMS API for server-side calls: `http://127.0.0.1:${PORT ?? 4000}`, the same rule
|
|
3
|
+
* as `core/blocks`' `resolveBaseUrl` (there is no `OPENCLOUDCMS_INTERNAL_URL`; it was removed). Read per call, so a
|
|
4
|
+
* `PORT` set after import counts.
|
|
5
|
+
*/
|
|
6
|
+
export declare function loopbackUrl(): string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loopback base URL of the OpenCloudCMS API for server-side calls: `http://127.0.0.1:${PORT ?? 4000}`, the same rule
|
|
3
|
+
* as `core/blocks`' `resolveBaseUrl` (there is no `OPENCLOUDCMS_INTERNAL_URL`; it was removed). Read per call, so a
|
|
4
|
+
* `PORT` set after import counts.
|
|
5
|
+
*/
|
|
6
|
+
export function loopbackUrl() {
|
|
7
|
+
const env = typeof process !== 'undefined' && process?.env ? process.env : {};
|
|
8
|
+
return `http://127.0.0.1:${env.PORT ?? 4000}`;
|
|
9
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Client, ClientOptions } from '../core/types.js';
|
|
2
|
+
import type { Customer } from '../core/auth.js';
|
|
3
|
+
export type ServerClientOptions = Omit<ClientOptions, 'baseUrl' | 'auth'>;
|
|
4
|
+
/** Loopback client without cookie forwarding: pages that only use it stay static / cacheable (`next` options pass through). */
|
|
5
|
+
export declare function publicClient(o?: ServerClientOptions): Client;
|
|
6
|
+
/**
|
|
7
|
+
* Loopback client that forwards the incoming request's `cookie` header (read from `next/headers` on every request,
|
|
8
|
+
* only to the loopback origin). Requests default to `cache: 'no-store'`. Using it makes the page dynamic.
|
|
9
|
+
*/
|
|
10
|
+
export declare function serverClient(o?: ServerClientOptions): Client;
|
|
11
|
+
/** The signed-in customer of the current request, `null` when signed out. Never cached across requests. */
|
|
12
|
+
export declare function getCustomer(): Promise<Customer | null>;
|
|
13
|
+
/**
|
|
14
|
+
* The signed-in customer, or a `redirect()` to the sign-in route. `returnTo` defaults to the request path from the
|
|
15
|
+
* `x-pathname` header (set it in middleware), then Next's `next-url`, else `/`; always passed through `safeReturnTo`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function requireCustomer(o?: {
|
|
18
|
+
returnTo?: string;
|
|
19
|
+
}): Promise<Customer>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// Next.js server helpers (react-server build only). `next/headers` / `next/navigation` are imported lazily inside
|
|
2
|
+
// the functions, never at module load, so this file can be loaded anywhere without Next.
|
|
3
|
+
import { createClient } from '../core/client.js';
|
|
4
|
+
import { safeReturnTo } from '../core/utils.js';
|
|
5
|
+
import { loopbackUrl } from './loopback.js';
|
|
6
|
+
const sameOrigin = (url, base) => {
|
|
7
|
+
try {
|
|
8
|
+
return new URL(url, base).origin === new URL(base).origin;
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
async function incomingHeaders() {
|
|
15
|
+
const { headers } = await import('next/headers');
|
|
16
|
+
return (await headers());
|
|
17
|
+
}
|
|
18
|
+
/** Loopback client without cookie forwarding: pages that only use it stay static / cacheable (`next` options pass through). */
|
|
19
|
+
export function publicClient(o = {}) {
|
|
20
|
+
return createClient({ ...o, baseUrl: loopbackUrl(), auth: 'cookie' });
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Loopback client that forwards the incoming request's `cookie` header (read from `next/headers` on every request,
|
|
24
|
+
* only to the loopback origin). Requests default to `cache: 'no-store'`. Using it makes the page dynamic.
|
|
25
|
+
*/
|
|
26
|
+
export function serverClient(o = {}) {
|
|
27
|
+
const baseUrl = loopbackUrl();
|
|
28
|
+
const inner = o.fetch ?? ((input, init) => globalThis.fetch(input, init));
|
|
29
|
+
const forwarding = (async (input, init) => {
|
|
30
|
+
const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
|
|
31
|
+
if (!sameOrigin(url, baseUrl))
|
|
32
|
+
return inner(input, init);
|
|
33
|
+
const cookie = (await incomingHeaders()).get('cookie');
|
|
34
|
+
const headers = new Headers(init?.headers);
|
|
35
|
+
if (cookie && !headers.has('cookie'))
|
|
36
|
+
headers.set('cookie', cookie);
|
|
37
|
+
return inner(input, { ...init, headers, cache: init?.cache ?? 'no-store' });
|
|
38
|
+
});
|
|
39
|
+
return createClient({ ...o, baseUrl, auth: 'cookie', fetch: forwarding });
|
|
40
|
+
}
|
|
41
|
+
/** The signed-in customer of the current request, `null` when signed out. Never cached across requests. */
|
|
42
|
+
export function getCustomer() {
|
|
43
|
+
return serverClient().auth.me();
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The signed-in customer, or a `redirect()` to the sign-in route. `returnTo` defaults to the request path from the
|
|
47
|
+
* `x-pathname` header (set it in middleware), then Next's `next-url`, else `/`; always passed through `safeReturnTo`.
|
|
48
|
+
*/
|
|
49
|
+
export async function requireCustomer(o = {}) {
|
|
50
|
+
const customer = await getCustomer();
|
|
51
|
+
if (customer)
|
|
52
|
+
return customer;
|
|
53
|
+
let returnTo = o.returnTo;
|
|
54
|
+
if (returnTo === undefined) {
|
|
55
|
+
const h = await incomingHeaders();
|
|
56
|
+
returnTo = h.get('x-pathname') ?? h.get('next-url') ?? '/';
|
|
57
|
+
}
|
|
58
|
+
const { redirect } = await import('next/navigation');
|
|
59
|
+
return redirect(`/api/customer/auth/login?returnTo=${encodeURIComponent(safeReturnTo(returnTo))}`);
|
|
60
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { getCustomer as GetCustomer, publicClient as PublicClient, requireCustomer as RequireCustomer, serverClient as ServerClient } from './server.js';
|
|
2
|
+
export declare const serverClient: typeof ServerClient;
|
|
3
|
+
export declare const publicClient: typeof PublicClient;
|
|
4
|
+
export declare const getCustomer: typeof GetCustomer;
|
|
5
|
+
export declare const requireCustomer: typeof RequireCustomer;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
const only = (name) => () => {
|
|
2
|
+
throw new Error(`${name} is only available in a server component`);
|
|
3
|
+
};
|
|
4
|
+
export const serverClient = only('serverClient');
|
|
5
|
+
export const publicClient = only('publicClient');
|
|
6
|
+
export const getCustomer = only('getCustomer');
|
|
7
|
+
export const requireCustomer = only('requireCustomer');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import { ActionRunner, type ActionRunResult, type UiAdapter } from '../actions/runner.js';
|
|
3
|
+
import type { ActionClientDefinition, ActionRequest } from '../core/actions.js';
|
|
4
|
+
import type { Customer } from '../core/auth.js';
|
|
5
|
+
import type { ApiError } from '../core/errors.js';
|
|
6
|
+
import type { Client } from '../core/types.js';
|
|
7
|
+
export type { ActionClientDefinition } from '../core/actions.js';
|
|
8
|
+
export interface OpenCloudCmsProviderProps {
|
|
9
|
+
client?: Client;
|
|
10
|
+
/** Browser halves of page actions (`defineActionClient`). Read on every run: an inline array is fine. */
|
|
11
|
+
actions?: ActionClientDefinition[];
|
|
12
|
+
/** Toast / confirm / overlay / navigation handlers for the action runner (read on every run); defaults are minimal. */
|
|
13
|
+
ui?: UiAdapter;
|
|
14
|
+
/** Where the action runner sends signed-out customers. Default `/account/login`. */
|
|
15
|
+
signInPath?: string;
|
|
16
|
+
/** Server-fetched customer (or `null` when signed out): no client-side `me()` call is made. */
|
|
17
|
+
initialCustomer?: Customer | null;
|
|
18
|
+
children?: ReactNode;
|
|
19
|
+
}
|
|
20
|
+
export declare function OpenCloudCmsProvider(props: OpenCloudCmsProviderProps): import("react").JSX.Element;
|
|
21
|
+
export declare function useClient(): Client;
|
|
22
|
+
export declare function useCustomer(): {
|
|
23
|
+
customer: Customer | null;
|
|
24
|
+
loading: boolean;
|
|
25
|
+
refresh(): Promise<void>;
|
|
26
|
+
};
|
|
27
|
+
export declare function useSignIn(): (returnTo?: string) => void;
|
|
28
|
+
export declare function useSignOut(): () => Promise<void>;
|
|
29
|
+
/** The provider's action runner (used by `core/blocks` triggers). */
|
|
30
|
+
export declare function useActionRunner(): ActionRunner;
|
|
31
|
+
export interface UseActionState {
|
|
32
|
+
run(req?: Partial<Omit<ActionRequest, 'name'>>): Promise<ActionRunResult>;
|
|
33
|
+
pending: boolean;
|
|
34
|
+
error: ApiError | null;
|
|
35
|
+
result: unknown;
|
|
36
|
+
}
|
|
37
|
+
/** Runs action `name` from your own components: same lifecycle, client hooks and events as page triggers. */
|
|
38
|
+
export declare function useAction(name: string): UseActionState;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
4
|
+
import { ActionRunner } from '../actions/runner.js';
|
|
5
|
+
import { createClient } from '../core/client.js';
|
|
6
|
+
const StableContext = createContext(null);
|
|
7
|
+
const CustomerContext = createContext(null);
|
|
8
|
+
export function OpenCloudCmsProvider(props) {
|
|
9
|
+
const { children, initialCustomer } = props;
|
|
10
|
+
const clientRef = useRef(null);
|
|
11
|
+
const client = props.client ?? (clientRef.current ??= createClient());
|
|
12
|
+
const hasInitial = initialCustomer !== undefined;
|
|
13
|
+
const [customer, setCustomer] = useState(hasInitial ? initialCustomer : null);
|
|
14
|
+
const [loading, setLoading] = useState(!hasInitial);
|
|
15
|
+
const started = useRef(false);
|
|
16
|
+
const live = useRef(true);
|
|
17
|
+
const seq = useRef(0);
|
|
18
|
+
// For the action runner's auth gate: the last known customer, and the me() in flight.
|
|
19
|
+
const known = useRef({ loaded: hasInitial, customer: initialCustomer ?? null });
|
|
20
|
+
const inflight = useRef(null);
|
|
21
|
+
const refresh = useCallback(async () => {
|
|
22
|
+
const mine = ++seq.current;
|
|
23
|
+
const p = (async () => {
|
|
24
|
+
const next = await client.auth.me();
|
|
25
|
+
if (mine === seq.current)
|
|
26
|
+
known.current = { loaded: true, customer: next };
|
|
27
|
+
if (live.current && mine === seq.current) {
|
|
28
|
+
setCustomer(next);
|
|
29
|
+
setLoading(false);
|
|
30
|
+
}
|
|
31
|
+
})();
|
|
32
|
+
inflight.current = p;
|
|
33
|
+
return p;
|
|
34
|
+
}, [client]);
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
live.current = true;
|
|
37
|
+
if (!hasInitial && !started.current) {
|
|
38
|
+
started.current = true; // one me() per provider mount, shared by every consumer
|
|
39
|
+
refresh().catch(() => {
|
|
40
|
+
if (live.current)
|
|
41
|
+
setLoading(false);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return () => {
|
|
45
|
+
live.current = false;
|
|
46
|
+
};
|
|
47
|
+
}, [hasInitial, refresh]);
|
|
48
|
+
// Latest props, read lazily by the runner: inline `actions` / `ui` don't rebuild it or change the context.
|
|
49
|
+
const actionsRef = useRef(props.actions ?? []);
|
|
50
|
+
actionsRef.current = props.actions ?? [];
|
|
51
|
+
const uiRef = useRef(props.ui ?? {});
|
|
52
|
+
uiRef.current = props.ui ?? {};
|
|
53
|
+
const signInPath = props.signInPath ?? '/account/login';
|
|
54
|
+
const stable = useMemo(() => {
|
|
55
|
+
const runner = new ActionRunner({
|
|
56
|
+
client,
|
|
57
|
+
get actions() {
|
|
58
|
+
return actionsRef.current;
|
|
59
|
+
},
|
|
60
|
+
signInPath,
|
|
61
|
+
get ui() {
|
|
62
|
+
return uiRef.current;
|
|
63
|
+
},
|
|
64
|
+
// Same state as useCustomer: waits for the mount-time me() (or starts one); throws when it is unknown.
|
|
65
|
+
async getCustomer() {
|
|
66
|
+
if (!known.current.loaded)
|
|
67
|
+
await (inflight.current ?? refresh());
|
|
68
|
+
if (!known.current.loaded)
|
|
69
|
+
throw new Error('customer unknown');
|
|
70
|
+
return known.current.customer;
|
|
71
|
+
},
|
|
72
|
+
onAuthChange: () => refresh(),
|
|
73
|
+
});
|
|
74
|
+
return { client, signInPath, runner };
|
|
75
|
+
}, [client, signInPath, refresh]);
|
|
76
|
+
const cust = useMemo(() => ({ customer, loading, refresh }), [customer, loading, refresh]);
|
|
77
|
+
return (_jsx(StableContext.Provider, { value: stable, children: _jsx(CustomerContext.Provider, { value: cust, children: children }) }));
|
|
78
|
+
}
|
|
79
|
+
const MISSING = 'OpenCloudCmsProvider is missing: wrap your app in <OpenCloudCmsProvider>';
|
|
80
|
+
function useStable() {
|
|
81
|
+
const ctx = useContext(StableContext);
|
|
82
|
+
if (!ctx)
|
|
83
|
+
throw new Error(MISSING);
|
|
84
|
+
return ctx;
|
|
85
|
+
}
|
|
86
|
+
function useCustomerCtx() {
|
|
87
|
+
const ctx = useContext(CustomerContext);
|
|
88
|
+
if (!ctx)
|
|
89
|
+
throw new Error(MISSING);
|
|
90
|
+
return ctx;
|
|
91
|
+
}
|
|
92
|
+
export function useClient() {
|
|
93
|
+
return useStable().client;
|
|
94
|
+
}
|
|
95
|
+
export function useCustomer() {
|
|
96
|
+
return useCustomerCtx();
|
|
97
|
+
}
|
|
98
|
+
export function useSignIn() {
|
|
99
|
+
const { client } = useStable();
|
|
100
|
+
return useCallback((returnTo) => client.auth.signIn(returnTo), [client]);
|
|
101
|
+
}
|
|
102
|
+
export function useSignOut() {
|
|
103
|
+
const { client } = useStable();
|
|
104
|
+
const { refresh } = useCustomerCtx();
|
|
105
|
+
return useCallback(async () => {
|
|
106
|
+
const out = await client.auth.signOut({ navigate: false });
|
|
107
|
+
try {
|
|
108
|
+
await refresh();
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
client.auth.followRedirect(out);
|
|
112
|
+
}
|
|
113
|
+
}, [client, refresh]);
|
|
114
|
+
}
|
|
115
|
+
/** The provider's action runner (used by `core/blocks` triggers). */
|
|
116
|
+
export function useActionRunner() {
|
|
117
|
+
return useStable().runner;
|
|
118
|
+
}
|
|
119
|
+
/** Runs action `name` from your own components: same lifecycle, client hooks and events as page triggers. */
|
|
120
|
+
export function useAction(name) {
|
|
121
|
+
const runner = useActionRunner();
|
|
122
|
+
const [state, setState] = useState({ pending: false, error: null, result: undefined });
|
|
123
|
+
const live = useRef(true);
|
|
124
|
+
// One run at a time per hook instance: a second run() while pending gets the same promise (one POST).
|
|
125
|
+
const inflight = useRef(null);
|
|
126
|
+
useEffect(() => {
|
|
127
|
+
live.current = true;
|
|
128
|
+
return () => {
|
|
129
|
+
live.current = false;
|
|
130
|
+
};
|
|
131
|
+
}, []);
|
|
132
|
+
const run = useCallback((req = {}) => {
|
|
133
|
+
if (inflight.current)
|
|
134
|
+
return inflight.current;
|
|
135
|
+
setState((s) => ({ ...s, pending: true }));
|
|
136
|
+
const p = (async () => {
|
|
137
|
+
let out = { ok: false };
|
|
138
|
+
try {
|
|
139
|
+
out = await runner.run(null, { id: req.id, name, params: req.params ?? {}, input: req.input, page: req.page });
|
|
140
|
+
}
|
|
141
|
+
finally {
|
|
142
|
+
inflight.current = null;
|
|
143
|
+
if (live.current) {
|
|
144
|
+
setState((s) => ({ pending: false, error: out.ok ? null : out.error ?? null, result: out.ok ? out.result : s.result }));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return out;
|
|
148
|
+
})();
|
|
149
|
+
inflight.current = p;
|
|
150
|
+
return p;
|
|
151
|
+
}, [runner, name]);
|
|
152
|
+
return { run, pending: state.pending, error: state.error, result: state.result };
|
|
153
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const useCustomer: (..._args: unknown[]) => never;
|
|
2
|
+
export declare const useClient: (..._args: unknown[]) => never;
|
|
3
|
+
export declare const useSignIn: (..._args: unknown[]) => never;
|
|
4
|
+
export declare const useSignOut: (..._args: unknown[]) => never;
|
|
5
|
+
export declare const useAction: (..._args: unknown[]) => never;
|
|
6
|
+
export declare const useActionRunner: (..._args: unknown[]) => never;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// react-server build: hooks cannot run in a server component. Importing them is fine; calling one throws.
|
|
2
|
+
const notHere = (name) => (..._args) => {
|
|
3
|
+
throw new Error(`${name} is not available in a server component`);
|
|
4
|
+
};
|
|
5
|
+
export const useCustomer = notHere('useCustomer');
|
|
6
|
+
export const useClient = notHere('useClient');
|
|
7
|
+
export const useSignIn = notHere('useSignIn');
|
|
8
|
+
export const useSignOut = notHere('useSignOut');
|
|
9
|
+
export const useAction = notHere('useAction');
|
|
10
|
+
export const useActionRunner = notHere('useActionRunner');
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export { createClient } from './core/client.js';
|
|
2
|
+
export { createContentApi } from './core/typed.js';
|
|
3
|
+
export { setDefaultClientFactory } from './core/default-client.js';
|
|
4
|
+
export { ApiError, isApiError, errorMessage } from './core/errors.js';
|
|
5
|
+
export type { ApiIssue, Client, ClientOptions, ErrorMessages, HttpMethod, MessageLocale, RequestOptions, ResolvedClientOptions, } from './core/types.js';
|
|
6
|
+
export type { ContentApi, TypedContent, TypedListOptions } from './core/typed.js';
|
|
7
|
+
export type { AssetRef, AssetsClient, ContentClient, FilterValue, ListOptions, ListResult, ReadOptions } from './core/types.js';
|
|
8
|
+
export { utils } from './core/utils.js';
|
|
9
|
+
export type { Money, IdentifierKind } from './core/utils.js';
|
|
10
|
+
export type { AuthClient, Customer, SignInMethods } from './core/auth.js';
|
|
11
|
+
export type { AccountClient } from './core/account.js';
|
|
12
|
+
export { loopbackUrl } from './next/loopback.js';
|
|
13
|
+
export { serverClient, publicClient, getCustomer, requireCustomer } from './next/server.js';
|
|
14
|
+
export type { ServerClientOptions } from './next/server.js';
|
|
15
|
+
export { ActionRunner, ACTION_EVENT } from './actions/runner.js';
|
|
16
|
+
export type { ActionEventDetail, ActionPhase, ActionRunInput, ActionRunResult, ActionRunnerOptions, UiAdapter } from './actions/runner.js';
|
|
17
|
+
export { defineActionClient } from './core/actions.js';
|
|
18
|
+
export type { ActionClientHooks, ActionManifest, ActionRequest, ActionUi, ActionsClient, RunActionOptions, } from './core/actions.js';
|
|
19
|
+
export { useCustomer, useClient, useSignIn, useSignOut, useAction, useActionRunner } from './react/stubs.js';
|
|
20
|
+
export { OpenCloudCmsProvider } from './react/provider.js';
|
|
21
|
+
export type { OpenCloudCmsProviderProps, ActionClientDefinition, UseActionState } from './react/provider.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// `react-server` build: no React context or hooks evaluated here. Real Next server helpers; hooks are stubs that
|
|
2
|
+
// throw when called. The Provider is re-exported from its 'use client' file (a client reference in RSC).
|
|
3
|
+
export { createClient } from './core/client.js';
|
|
4
|
+
export { createContentApi } from './core/typed.js';
|
|
5
|
+
export { setDefaultClientFactory } from './core/default-client.js';
|
|
6
|
+
export { ApiError, isApiError, errorMessage } from './core/errors.js';
|
|
7
|
+
export { utils } from './core/utils.js';
|
|
8
|
+
export { loopbackUrl } from './next/loopback.js';
|
|
9
|
+
export { serverClient, publicClient, getCustomer, requireCustomer } from './next/server.js';
|
|
10
|
+
export { ActionRunner, ACTION_EVENT } from './actions/runner.js';
|
|
11
|
+
export { defineActionClient } from './core/actions.js';
|
|
12
|
+
export { useCustomer, useClient, useSignIn, useSignOut, useAction, useActionRunner } from './react/stubs.js';
|
|
13
|
+
export { OpenCloudCmsProvider } from './react/provider.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,64 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opencloudcms/client",
|
|
3
|
-
"
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
"publishConfig": {
|
|
4
|
+
"access": "public"
|
|
5
|
+
},
|
|
6
|
+
"version": "0.1.0",
|
|
7
|
+
"description": "Storefront SDK for OpenCloudCMS: typed API client, customer auth, React hooks and page actions",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/anhdev97/Open-CMS.git",
|
|
12
|
+
"directory": "packages/client"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/anhdev97/Open-CMS#readme",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/anhdev97/Open-CMS/issues"
|
|
17
|
+
},
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=22"
|
|
20
|
+
},
|
|
21
|
+
"main": "dist/cjs/index.js",
|
|
22
|
+
"module": "dist/esm/index.js",
|
|
23
|
+
"types": "dist/esm/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/esm/index.d.ts",
|
|
27
|
+
"react-server": "./dist/esm/server.js",
|
|
28
|
+
"import": "./dist/esm/index.js",
|
|
29
|
+
"require": "./dist/cjs/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./package.json": "./package.json"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"libphonenumber-js": "^1.13.17"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"next": "^16.0.0",
|
|
41
|
+
"react": "^19.0.0"
|
|
42
|
+
},
|
|
43
|
+
"peerDependenciesMeta": {
|
|
44
|
+
"next": {
|
|
45
|
+
"optional": true
|
|
46
|
+
},
|
|
47
|
+
"react": {
|
|
48
|
+
"optional": true
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@testing-library/dom": "^10.4.2",
|
|
53
|
+
"@testing-library/react": "^16.3.3",
|
|
54
|
+
"@types/react": "^19.3.0",
|
|
55
|
+
"@types/react-dom": "^19.3.0",
|
|
56
|
+
"jsdom": "^31.0.1",
|
|
57
|
+
"react": "^19.3.1",
|
|
58
|
+
"react-dom": "^19.3.1"
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.json && tsc -p tsconfig.cjs.json && node -e \"const fs=require('fs');fs.writeFileSync('dist/esm/package.json','{\\\"type\\\":\\\"module\\\"}\\n');fs.writeFileSync('dist/cjs/package.json','{\\\"type\\\":\\\"commonjs\\\"}\\n')\"",
|
|
62
|
+
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.cjs.json --noEmit && tsc -p tsconfig.test.json"
|
|
63
|
+
}
|
|
6
64
|
}
|