@axcode/axcloud-sdk 0.1.4 → 0.1.6
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.md +9 -0
- package/README.md +19 -1
- package/dist/index.d.mts +98 -8
- package/dist/index.d.ts +98 -8
- package/dist/index.js +109 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +107 -10
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.mjs
CHANGED
|
@@ -236,6 +236,88 @@ function getClient(key) {
|
|
|
236
236
|
throw new Error("AxCloud client not found. Make sure AxCloudProvider is mounted and use useAxCloudContext() to get the client.");
|
|
237
237
|
}
|
|
238
238
|
|
|
239
|
+
// src/core/contentSlots.ts
|
|
240
|
+
var SLOT_ATTRIBUTE_KEYS = ["slot", "field", "key", "name"];
|
|
241
|
+
function getContentSlotMap(content) {
|
|
242
|
+
const fields = isRecord(content?.fields) ? { ...content?.fields } : {};
|
|
243
|
+
for (const block of content?.blocks || []) {
|
|
244
|
+
const slot = getBlockSlot(block);
|
|
245
|
+
if (!slot) continue;
|
|
246
|
+
fields[slot] = blockToSlotValue(block);
|
|
247
|
+
}
|
|
248
|
+
return fields;
|
|
249
|
+
}
|
|
250
|
+
function createContentSlotReader(content) {
|
|
251
|
+
const fields = getContentSlotMap(content);
|
|
252
|
+
const value = (key, fallback) => {
|
|
253
|
+
return key in fields ? fields[key] : fallback;
|
|
254
|
+
};
|
|
255
|
+
return {
|
|
256
|
+
fields,
|
|
257
|
+
value,
|
|
258
|
+
text: (key, fallback = "") => stringifySlotValue(value(key, fallback)),
|
|
259
|
+
html: (key, fallback = "") => stringifySlotValue(value(key, fallback)),
|
|
260
|
+
list: (key, fallback = []) => slotValueToList(value(key, fallback)),
|
|
261
|
+
image: (key, fallback) => slotValueToImage(value(key, fallback))
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
function getBlockSlot(block) {
|
|
265
|
+
const attributes = block.attributes || {};
|
|
266
|
+
for (const key of SLOT_ATTRIBUTE_KEYS) {
|
|
267
|
+
const value = attributes[key];
|
|
268
|
+
if (typeof value === "string" && value.trim()) return value.trim();
|
|
269
|
+
}
|
|
270
|
+
return void 0;
|
|
271
|
+
}
|
|
272
|
+
function blockToSlotValue(block) {
|
|
273
|
+
if (block.attributes && "value" in block.attributes) {
|
|
274
|
+
return block.attributes.value;
|
|
275
|
+
}
|
|
276
|
+
if (block.type === "list") {
|
|
277
|
+
return Array.isArray(block.items) ? block.items : [];
|
|
278
|
+
}
|
|
279
|
+
if (block.type === "image" && block.url) {
|
|
280
|
+
return {
|
|
281
|
+
url: block.url,
|
|
282
|
+
alt: block.alt,
|
|
283
|
+
caption: block.caption
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
return block.content ?? "";
|
|
287
|
+
}
|
|
288
|
+
function stringifySlotValue(value) {
|
|
289
|
+
if (value === void 0 || value === null) return "";
|
|
290
|
+
if (typeof value === "string") return value;
|
|
291
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
292
|
+
if (Array.isArray(value)) return value.map((item) => stringifySlotValue(item)).filter(Boolean).join("\n");
|
|
293
|
+
return "";
|
|
294
|
+
}
|
|
295
|
+
function slotValueToList(value) {
|
|
296
|
+
if (Array.isArray(value)) {
|
|
297
|
+
return value.map((item) => stringifySlotValue(item)).filter(Boolean);
|
|
298
|
+
}
|
|
299
|
+
if (typeof value === "string") {
|
|
300
|
+
return value.split(/\r?\n/).map((item) => item.trim()).filter(Boolean);
|
|
301
|
+
}
|
|
302
|
+
return [];
|
|
303
|
+
}
|
|
304
|
+
function slotValueToImage(value) {
|
|
305
|
+
if (isRecord(value) && typeof value.url === "string") {
|
|
306
|
+
return {
|
|
307
|
+
url: value.url,
|
|
308
|
+
alt: typeof value.alt === "string" ? value.alt : void 0,
|
|
309
|
+
caption: typeof value.caption === "string" ? value.caption : void 0
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
if (typeof value === "string" && value) {
|
|
313
|
+
return { url: value };
|
|
314
|
+
}
|
|
315
|
+
return void 0;
|
|
316
|
+
}
|
|
317
|
+
function isRecord(value) {
|
|
318
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
319
|
+
}
|
|
320
|
+
|
|
239
321
|
// src/core/types.ts
|
|
240
322
|
function userHasPermission(user, permission) {
|
|
241
323
|
if (user.permissions && user.permissions.length > 0) {
|
|
@@ -704,6 +786,7 @@ function useNews(options = {}) {
|
|
|
704
786
|
};
|
|
705
787
|
}
|
|
706
788
|
var MEMBER_ENDPOINT_TYPES = ["news", "pages", "events"];
|
|
789
|
+
var PAGE_CONTENT_TYPES = ["page", "pages"];
|
|
707
790
|
function useContent(contentType, options = {}) {
|
|
708
791
|
const { client, orgId, isPublicMode } = useAxCloudContext();
|
|
709
792
|
const {
|
|
@@ -715,7 +798,8 @@ function useContent(contentType, options = {}) {
|
|
|
715
798
|
sectionId,
|
|
716
799
|
search,
|
|
717
800
|
sortBy = "createdAt",
|
|
718
|
-
sortOrder = "desc"
|
|
801
|
+
sortOrder = "desc",
|
|
802
|
+
lang
|
|
719
803
|
} = options;
|
|
720
804
|
const query = useQuery({
|
|
721
805
|
queryKey: [
|
|
@@ -732,7 +816,8 @@ function useContent(contentType, options = {}) {
|
|
|
732
816
|
sectionId,
|
|
733
817
|
search,
|
|
734
818
|
sortBy,
|
|
735
|
-
sortOrder
|
|
819
|
+
sortOrder,
|
|
820
|
+
lang
|
|
736
821
|
],
|
|
737
822
|
queryFn: async () => {
|
|
738
823
|
const useMemberEndpoint = !isPublicMode && !publicOnly && MEMBER_ENDPOINT_TYPES.includes(contentType);
|
|
@@ -745,7 +830,8 @@ function useContent(contentType, options = {}) {
|
|
|
745
830
|
sectionId,
|
|
746
831
|
search,
|
|
747
832
|
sortBy,
|
|
748
|
-
sortOrder
|
|
833
|
+
sortOrder,
|
|
834
|
+
lang
|
|
749
835
|
});
|
|
750
836
|
},
|
|
751
837
|
staleTime: 1e3 * 60 * 5
|
|
@@ -763,13 +849,15 @@ function useContent(contentType, options = {}) {
|
|
|
763
849
|
}
|
|
764
850
|
function useContentItem(contentType, idOrSlug, options = {}) {
|
|
765
851
|
const { client, orgId, isPublicMode } = useAxCloudContext();
|
|
766
|
-
const { publicOnly = false } = options;
|
|
767
|
-
const useMemberEndpoint = !isPublicMode && !publicOnly && MEMBER_ENDPOINT_TYPES.includes(contentType);
|
|
852
|
+
const { publicOnly = false, lang } = options;
|
|
853
|
+
const useMemberEndpoint = !isPublicMode && !publicOnly && (MEMBER_ENDPOINT_TYPES.includes(contentType) || PAGE_CONTENT_TYPES.includes(contentType));
|
|
768
854
|
const query = useQuery({
|
|
769
|
-
queryKey: ["axcloud", "content", orgId, contentType, idOrSlug, useMemberEndpoint],
|
|
855
|
+
queryKey: ["axcloud", "content", orgId, contentType, idOrSlug, useMemberEndpoint, lang],
|
|
770
856
|
queryFn: async () => {
|
|
771
857
|
const basePath = useMemberEndpoint ? "members" : "public";
|
|
772
|
-
|
|
858
|
+
const encodedIdOrSlug = encodeURIComponent(idOrSlug);
|
|
859
|
+
const endpoint = PAGE_CONTENT_TYPES.includes(contentType) ? `/content/${basePath}/${orgId}/page/${encodedIdOrSlug}` : `/content/${basePath}/${orgId}/${contentType}/${encodedIdOrSlug}`;
|
|
860
|
+
return lang ? client.get(endpoint, { lang }) : client.get(endpoint);
|
|
773
861
|
},
|
|
774
862
|
enabled: !!idOrSlug,
|
|
775
863
|
staleTime: 1e3 * 60 * 5
|
|
@@ -783,6 +871,15 @@ function useContentItem(contentType, idOrSlug, options = {}) {
|
|
|
783
871
|
refetch: query.refetch
|
|
784
872
|
};
|
|
785
873
|
}
|
|
874
|
+
function usePageContent(slug, options = {}) {
|
|
875
|
+
const state = useContentItem("pages", slug, options);
|
|
876
|
+
const reader = useMemo(() => createContentSlotReader(state.data), [state.data]);
|
|
877
|
+
return {
|
|
878
|
+
...state,
|
|
879
|
+
page: state.data,
|
|
880
|
+
...reader
|
|
881
|
+
};
|
|
882
|
+
}
|
|
786
883
|
function useMySection(options = {}) {
|
|
787
884
|
const { includeMembers = true } = options;
|
|
788
885
|
const { isAuthenticated } = useAuth0();
|
|
@@ -798,7 +895,7 @@ function useMySection(options = {}) {
|
|
|
798
895
|
`/orgs/api/organizations/${orgId}/sections/${sectionId}`,
|
|
799
896
|
{ includeMembers: includeMembers ? "true" : void 0 }
|
|
800
897
|
);
|
|
801
|
-
return "success" in response && response.data ? response.data : response;
|
|
898
|
+
return "success" in response && response.success === true && response.data ? response.data : response;
|
|
802
899
|
},
|
|
803
900
|
enabled: isAuthenticated && !isUserLoading && !!sectionId,
|
|
804
901
|
staleTime: 1e3 * 60 * 5
|
|
@@ -825,7 +922,7 @@ function useSections() {
|
|
|
825
922
|
queryKey: ["axcloud", "sections", orgId, stage],
|
|
826
923
|
queryFn: async () => {
|
|
827
924
|
const response = await client.get(`/orgs/api/organizations/${orgId}/sections`);
|
|
828
|
-
return "success" in response && response.data ? response.data : response;
|
|
925
|
+
return "success" in response && response.success === true && response.data ? response.data : response;
|
|
829
926
|
},
|
|
830
927
|
staleTime: 1e3 * 60 * 10
|
|
831
928
|
// 10 minutes
|
|
@@ -1302,6 +1399,6 @@ function LinkedAccountsList({
|
|
|
1302
1399
|
] });
|
|
1303
1400
|
}
|
|
1304
1401
|
|
|
1305
|
-
export { AxCloudClient, AxCloudError, AxCloudProvider, LinkedAccountsList, LoginButton, LogoutButton, ModuleGate, ProtectedRoute, createClient, createFormHelper, getClient, isAxCloudError, submitForm, useAuth, useAvatar, useAxCloudContext, useBranding, useContent, useContentItem, useFormStats, useForms, useLinkedAccounts, useLogo, useMedia, useMediaList, useModuleAccess, useMySection, useNews, useOrg, useSections, useUpdateFormSubmission, useUser, userHasAllPermissions, userHasAnyPermission, userHasPermission };
|
|
1402
|
+
export { AxCloudClient, AxCloudError, AxCloudProvider, LinkedAccountsList, LoginButton, LogoutButton, ModuleGate, ProtectedRoute, createClient, createContentSlotReader, createFormHelper, getClient, getContentSlotMap, isAxCloudError, submitForm, useAuth, useAvatar, useAxCloudContext, useBranding, useContent, useContentItem, useFormStats, useForms, useLinkedAccounts, useLogo, useMedia, useMediaList, useModuleAccess, useMySection, useNews, useOrg, usePageContent, useSections, useUpdateFormSubmission, useUser, userHasAllPermissions, userHasAnyPermission, userHasPermission };
|
|
1306
1403
|
//# sourceMappingURL=index.mjs.map
|
|
1307
1404
|
//# sourceMappingURL=index.mjs.map
|