@asteroidcms/core-utils 0.1.2 → 0.1.3
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/client.cjs +1466 -0
- package/dist/client.cjs.map +1 -0
- package/dist/client.d.cts +229 -0
- package/dist/client.d.ts +229 -0
- package/dist/client.js +1455 -0
- package/dist/client.js.map +1 -0
- package/dist/index.cjs +1 -747
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -182
- package/dist/index.d.ts +1 -182
- package/dist/index.js +2 -738
- package/dist/index.js.map +1 -1
- package/package.json +7 -2
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import * as react from 'react';
|
|
3
|
+
import { PropsWithChildren } from 'react';
|
|
4
|
+
import * as _apollo_client from '@apollo/client';
|
|
5
|
+
import { InMemoryCacheConfig, ApolloClientOptions, ApolloClient } from '@apollo/client';
|
|
6
|
+
import { useMutation } from '@apollo/client/react';
|
|
7
|
+
|
|
8
|
+
type AsteroidCMSConfig = {
|
|
9
|
+
/** Base URL of the Asteroid CMS API (e.g. https://cms-api.example.com). */
|
|
10
|
+
cmsUrl: string;
|
|
11
|
+
/** API key sent as the `x-api-key` header on every request. */
|
|
12
|
+
apiKey: string;
|
|
13
|
+
/** Optional path appended to `cmsUrl` for the GraphQL endpoint. Defaults to "/graphql". */
|
|
14
|
+
graphqlPath?: string;
|
|
15
|
+
/** Optional path appended to `cmsUrl` for canonical media. Defaults to "/media/canonical". */
|
|
16
|
+
mediaPath?: string;
|
|
17
|
+
/** Extra headers attached to every GraphQL request. */
|
|
18
|
+
headers?: Record<string, string>;
|
|
19
|
+
/** Called for every GraphQL/network error. Use this to wire your own toast/logger. */
|
|
20
|
+
onError?: (error: unknown) => void;
|
|
21
|
+
/** Optional cache options forwarded to the InMemoryCache constructor. */
|
|
22
|
+
cacheConfig?: InMemoryCacheConfig;
|
|
23
|
+
/** Escape hatch — override any field on the underlying ApolloClient. */
|
|
24
|
+
apolloOptions?: Partial<ApolloClientOptions>;
|
|
25
|
+
/** Provide a pre-built ApolloClient and skip the internal factory entirely. */
|
|
26
|
+
client?: ApolloClient;
|
|
27
|
+
};
|
|
28
|
+
type ResolvedAsteroidCMSConfig = Required<Pick<AsteroidCMSConfig, "cmsUrl" | "apiKey" | "graphqlPath" | "mediaPath">> & {
|
|
29
|
+
headers: Record<string, string>;
|
|
30
|
+
onError?: AsteroidCMSConfig["onError"];
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
type AsteroidCMSProviderProps = PropsWithChildren<AsteroidCMSConfig>;
|
|
34
|
+
/**
|
|
35
|
+
* Root provider for `@asteroidcms/core-utils`.
|
|
36
|
+
*
|
|
37
|
+
* Wrap your app once at the top level:
|
|
38
|
+
*
|
|
39
|
+
* ```tsx
|
|
40
|
+
* <AsteroidCMSProvider cmsUrl="https://cms.example.com" apiKey="...">
|
|
41
|
+
* <App />
|
|
42
|
+
* </AsteroidCMSProvider>
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* Everything below — `useCmsContent`, `useCmsMutate`, `useCmsImage`,
|
|
46
|
+
* `<RichTextContent>` — picks up `cmsUrl` and `apiKey` from this provider.
|
|
47
|
+
*/
|
|
48
|
+
declare function AsteroidCMSProvider({ children, ...config }: AsteroidCMSProviderProps): react_jsx_runtime.JSX.Element;
|
|
49
|
+
|
|
50
|
+
declare function useAsteroidCMSConfig(): ResolvedAsteroidCMSConfig;
|
|
51
|
+
|
|
52
|
+
type FieldSelector$1 = string | {
|
|
53
|
+
field: string;
|
|
54
|
+
as?: string;
|
|
55
|
+
};
|
|
56
|
+
type ReferenceExpansion$1 = {
|
|
57
|
+
field: string;
|
|
58
|
+
as?: string;
|
|
59
|
+
single?: boolean;
|
|
60
|
+
select?: readonly (FieldSelector$1 | ReferenceExpansion$1)[];
|
|
61
|
+
};
|
|
62
|
+
type ContentStatus = "DRAFT" | "PUBLISHED" | "ARCHIVED";
|
|
63
|
+
type CmsSearchCondition = {
|
|
64
|
+
field: string;
|
|
65
|
+
value: string;
|
|
66
|
+
mode?: string;
|
|
67
|
+
};
|
|
68
|
+
type UseCmsContentOptions = {
|
|
69
|
+
schema_slug: string;
|
|
70
|
+
entrySlug?: string;
|
|
71
|
+
select?: readonly (FieldSelector$1 | ReferenceExpansion$1)[];
|
|
72
|
+
fullData?: boolean;
|
|
73
|
+
limit?: number;
|
|
74
|
+
offset?: number;
|
|
75
|
+
status?: ContentStatus;
|
|
76
|
+
filter?: Record<string, string | number | boolean | null>;
|
|
77
|
+
search?: CmsSearchCondition[];
|
|
78
|
+
variables?: Record<string, any>;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* React hook for querying content from Asteroid CMS via a flexible GraphQL API.
|
|
82
|
+
*
|
|
83
|
+
* Supports single-entry fetches (`entrySlug`) and paginated/filtered lists,
|
|
84
|
+
* with arbitrarily nested reference expansion and field aliasing.
|
|
85
|
+
*/
|
|
86
|
+
declare function useCmsContent<T = unknown>({ schema_slug, entrySlug, select, fullData, limit, offset, status, filter, search, variables, }: UseCmsContentOptions): {
|
|
87
|
+
client: _apollo_client.ApolloClient;
|
|
88
|
+
observable: _apollo_client.ObservableQuery<unknown, _apollo_client.OperationVariables>;
|
|
89
|
+
previousData?: unknown;
|
|
90
|
+
networkStatus: _apollo_client.NetworkStatus;
|
|
91
|
+
startPolling: (pollInterval: number) => void;
|
|
92
|
+
stopPolling: () => void;
|
|
93
|
+
subscribeToMore: _apollo_client.SubscribeToMoreFunction<unknown, _apollo_client.OperationVariables>;
|
|
94
|
+
updateQuery: (mapFn: _apollo_client.UpdateQueryMapFn<unknown, _apollo_client.OperationVariables>) => void;
|
|
95
|
+
refetch: (variables?: Partial<_apollo_client.OperationVariables> | undefined) => Promise<{
|
|
96
|
+
data: unknown;
|
|
97
|
+
error?: _apollo_client.ErrorLike;
|
|
98
|
+
}>;
|
|
99
|
+
variables: _apollo_client.OperationVariables;
|
|
100
|
+
fetchMore: <TFetchData = unknown, TFetchVars extends _apollo_client.OperationVariables = _apollo_client.OperationVariables>(fetchMoreOptions: _apollo_client.ObservableQuery.FetchMoreOptions<unknown, _apollo_client.OperationVariables, TFetchData, TFetchVars>) => Promise<{
|
|
101
|
+
data: TFetchData | undefined;
|
|
102
|
+
error?: _apollo_client.ErrorLike;
|
|
103
|
+
}>;
|
|
104
|
+
dataState: "empty";
|
|
105
|
+
loading: boolean;
|
|
106
|
+
error: _apollo_client.ErrorLike | undefined;
|
|
107
|
+
data: T | undefined;
|
|
108
|
+
} | {
|
|
109
|
+
client: _apollo_client.ApolloClient;
|
|
110
|
+
observable: _apollo_client.ObservableQuery<unknown, _apollo_client.OperationVariables>;
|
|
111
|
+
previousData?: unknown;
|
|
112
|
+
networkStatus: _apollo_client.NetworkStatus;
|
|
113
|
+
startPolling: (pollInterval: number) => void;
|
|
114
|
+
stopPolling: () => void;
|
|
115
|
+
subscribeToMore: _apollo_client.SubscribeToMoreFunction<unknown, _apollo_client.OperationVariables>;
|
|
116
|
+
updateQuery: (mapFn: _apollo_client.UpdateQueryMapFn<unknown, _apollo_client.OperationVariables>) => void;
|
|
117
|
+
refetch: (variables?: Partial<_apollo_client.OperationVariables> | undefined) => Promise<{
|
|
118
|
+
data: unknown;
|
|
119
|
+
error?: _apollo_client.ErrorLike;
|
|
120
|
+
}>;
|
|
121
|
+
variables: _apollo_client.OperationVariables;
|
|
122
|
+
fetchMore: <TFetchData = unknown, TFetchVars extends _apollo_client.OperationVariables = _apollo_client.OperationVariables>(fetchMoreOptions: _apollo_client.ObservableQuery.FetchMoreOptions<unknown, _apollo_client.OperationVariables, TFetchData, TFetchVars>) => Promise<{
|
|
123
|
+
data: TFetchData | undefined;
|
|
124
|
+
error?: _apollo_client.ErrorLike;
|
|
125
|
+
}>;
|
|
126
|
+
dataState: "complete";
|
|
127
|
+
loading: boolean;
|
|
128
|
+
error: _apollo_client.ErrorLike | undefined;
|
|
129
|
+
data: T | undefined;
|
|
130
|
+
} | {
|
|
131
|
+
client: _apollo_client.ApolloClient;
|
|
132
|
+
observable: _apollo_client.ObservableQuery<unknown, _apollo_client.OperationVariables>;
|
|
133
|
+
previousData?: unknown;
|
|
134
|
+
networkStatus: _apollo_client.NetworkStatus;
|
|
135
|
+
startPolling: (pollInterval: number) => void;
|
|
136
|
+
stopPolling: () => void;
|
|
137
|
+
subscribeToMore: _apollo_client.SubscribeToMoreFunction<unknown, _apollo_client.OperationVariables>;
|
|
138
|
+
updateQuery: (mapFn: _apollo_client.UpdateQueryMapFn<unknown, _apollo_client.OperationVariables>) => void;
|
|
139
|
+
refetch: (variables?: Partial<_apollo_client.OperationVariables> | undefined) => Promise<{
|
|
140
|
+
data: unknown;
|
|
141
|
+
error?: _apollo_client.ErrorLike;
|
|
142
|
+
}>;
|
|
143
|
+
variables: _apollo_client.OperationVariables;
|
|
144
|
+
fetchMore: <TFetchData = unknown, TFetchVars extends _apollo_client.OperationVariables = _apollo_client.OperationVariables>(fetchMoreOptions: _apollo_client.ObservableQuery.FetchMoreOptions<unknown, _apollo_client.OperationVariables, TFetchData, TFetchVars>) => Promise<{
|
|
145
|
+
data: TFetchData | undefined;
|
|
146
|
+
error?: _apollo_client.ErrorLike;
|
|
147
|
+
}>;
|
|
148
|
+
dataState: "streaming";
|
|
149
|
+
loading: boolean;
|
|
150
|
+
error: _apollo_client.ErrorLike | undefined;
|
|
151
|
+
data: T | undefined;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
type FieldSelector = string | {
|
|
155
|
+
field: string;
|
|
156
|
+
as?: string;
|
|
157
|
+
single?: boolean;
|
|
158
|
+
select?: readonly (FieldSelector | ReferenceExpansion)[];
|
|
159
|
+
};
|
|
160
|
+
type ReferenceExpansion = {
|
|
161
|
+
field: string;
|
|
162
|
+
as?: string;
|
|
163
|
+
single?: boolean;
|
|
164
|
+
select?: readonly (FieldSelector | ReferenceExpansion)[];
|
|
165
|
+
};
|
|
166
|
+
type MutationType = "create" | "update" | "delete";
|
|
167
|
+
type UseCmsMutateOptions = {
|
|
168
|
+
schema_slug: string;
|
|
169
|
+
entrySlug?: string;
|
|
170
|
+
select?: readonly (FieldSelector | ReferenceExpansion)[];
|
|
171
|
+
fullData?: boolean;
|
|
172
|
+
mutationType?: MutationType;
|
|
173
|
+
entryId?: string;
|
|
174
|
+
variables?: Record<string, any>;
|
|
175
|
+
};
|
|
176
|
+
/**
|
|
177
|
+
* React hook for mutating content in Asteroid CMS (`create` / `update` / `delete`).
|
|
178
|
+
* Uses the same selection syntax as `useCmsContent`.
|
|
179
|
+
*/
|
|
180
|
+
declare function useCmsMutate<TData = unknown>({ schema_slug, select, fullData, mutationType, entryId, variables: inputVariables, }: UseCmsMutateOptions): {
|
|
181
|
+
readonly data: TData | undefined;
|
|
182
|
+
readonly loading: boolean;
|
|
183
|
+
readonly called: boolean;
|
|
184
|
+
readonly client: _apollo_client.ApolloClient;
|
|
185
|
+
readonly reset: () => void;
|
|
186
|
+
readonly error: _apollo_client.ErrorLike | undefined;
|
|
187
|
+
readonly mutate: useMutation.MutationFunction<unknown, {
|
|
188
|
+
[x: string]: any;
|
|
189
|
+
}, _apollo_client.ApolloCache, "none">;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
/** Hook variant that pulls `cmsUrl`/`mediaPath` from the provider. */
|
|
193
|
+
declare function useCmsImage(): (id?: string) => string;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Portable rich-text parser for CMS content.
|
|
197
|
+
*
|
|
198
|
+
* Zero runtime dependencies. Safe to copy into any consuming frontend.
|
|
199
|
+
*
|
|
200
|
+
* Pipeline:
|
|
201
|
+
* 1. Detect & migrate legacy "markdown-in-divs" content to clean semantic HTML.
|
|
202
|
+
* 2. Walk the HTML through a tag/attribute allowlist (strips <script>, event
|
|
203
|
+
* handlers, javascript: URLs, unknown tags).
|
|
204
|
+
* 3. Merge per-tag and per-variant classes from `options.classMap`.
|
|
205
|
+
*
|
|
206
|
+
* Idempotent: parseRichText(parseRichText(x, opts), opts) === parseRichText(x, opts).
|
|
207
|
+
*/
|
|
208
|
+
type RichTextClassKey = "p" | "br" | "hr" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "ul" | "ol" | "li" | "blockquote" | "pre" | "code" | "inlineCode" | "a" | "strong" | "em" | "u" | "s" | "kbd" | "table" | "tableWrapper" | "thead" | "tbody" | "tr" | "th" | "td" | "figure" | "figcaption" | "img" | "span" | "callout" | "calloutTitle";
|
|
209
|
+
type RichTextClassMap = Partial<Record<RichTextClassKey, string>> & {
|
|
210
|
+
/** Variant overrides keyed as `${matchKey}:${variant}`, e.g. "callout:warning". */
|
|
211
|
+
variants?: Record<string, string>;
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
interface RichTextContentProps {
|
|
215
|
+
html: string;
|
|
216
|
+
classMap?: RichTextClassMap;
|
|
217
|
+
/** Wrapper element. Defaults to `div`. Use `article` for blog content. */
|
|
218
|
+
as?: keyof React.JSX.IntrinsicElements;
|
|
219
|
+
className?: string;
|
|
220
|
+
}
|
|
221
|
+
declare function RichTextContent({ html, classMap, as, className, }: RichTextContentProps): react.DOMElement<{
|
|
222
|
+
ref: react.MutableRefObject<HTMLElement | null>;
|
|
223
|
+
className: string | undefined;
|
|
224
|
+
dangerouslySetInnerHTML: {
|
|
225
|
+
__html: string;
|
|
226
|
+
};
|
|
227
|
+
}, HTMLElement>;
|
|
228
|
+
|
|
229
|
+
export { AsteroidCMSProvider, type AsteroidCMSProviderProps, RichTextContent, type UseCmsContentOptions, type UseCmsMutateOptions, useAsteroidCMSConfig, useCmsContent, useCmsImage, useCmsMutate };
|