@fragmentsx/client-core 0.3.0 → 0.4.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/createAreaManager.d.ts +18 -0
- package/dist/createAreaManager.d.ts.map +1 -0
- package/dist/fragmentsClient.d.ts +0 -2
- package/dist/fragmentsClient.d.ts.map +1 -1
- package/dist/index.cjs.js +223 -225
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.js +223 -225
- package/dist/plugins/fetch/areaQueries.test.d.ts +2 -0
- package/dist/plugins/fetch/areaQueries.test.d.ts.map +1 -0
- package/dist/plugins/fetch/index.d.ts +10 -7
- package/dist/plugins/fetch/index.d.ts.map +1 -1
- package/dist/plugins/fetch/restFetcher.d.ts +6 -0
- package/dist/plugins/fetch/restFetcher.d.ts.map +1 -0
- package/dist/plugins/fetch/restFetcher.test.d.ts +2 -0
- package/dist/plugins/fetch/restFetcher.test.d.ts.map +1 -0
- package/dist/plugins/fonts/index.d.ts.map +1 -1
- package/dist/plugins/fonts/index.test.d.ts +2 -0
- package/dist/plugins/fonts/index.test.d.ts.map +1 -0
- package/dist/plugins/fragments/index.d.ts.map +1 -1
- package/dist/plugins/load/index.d.ts +1 -1
- package/dist/plugins/load/index.d.ts.map +1 -1
- package/dist/plugins/metrics/globalMetrics.d.ts +8 -1
- package/dist/plugins/metrics/globalMetrics.d.ts.map +1 -1
- package/dist/plugins/metrics/index.d.ts.map +1 -1
- package/dist/plugins/properties/index.d.ts +17 -0
- package/dist/plugins/properties/index.d.ts.map +1 -0
- package/dist/plugins/properties/index.test.d.ts +2 -0
- package/dist/plugins/properties/index.test.d.ts.map +1 -0
- package/dist/testing/createTestFragmentsClient.d.ts +4 -0
- package/dist/testing/createTestFragmentsClient.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/plugins/fetch/fetcher.d.ts +0 -8
- package/dist/plugins/fetch/fetcher.d.ts.map +0 -1
- package/dist/plugins/fetch/fetcher.test.d.ts +0 -2
- package/dist/plugins/fetch/fetcher.test.d.ts.map +0 -1
- package/dist/plugins/fetch/queries/AreaListQuery.d.ts +0 -43
- package/dist/plugins/fetch/queries/AreaListQuery.d.ts.map +0 -1
- package/dist/plugins/fetch/queries/AreaQuery.d.ts +0 -24
- package/dist/plugins/fetch/queries/AreaQuery.d.ts.map +0 -1
- package/dist/plugins/fetch/queries/FragmentQuery.d.ts +0 -31
- package/dist/plugins/fetch/queries/FragmentQuery.d.ts.map +0 -1
- package/dist/plugins/fetch/queries/parts.d.ts +0 -5
- package/dist/plugins/fetch/queries/parts.d.ts.map +0 -1
package/dist/index.es.js
CHANGED
|
@@ -1688,100 +1688,48 @@ const CssOverrideSchema = /* @__PURE__ */ object({
|
|
|
1688
1688
|
const BASE_HEADERS = {
|
|
1689
1689
|
"Content-Type": "application/json"
|
|
1690
1690
|
};
|
|
1691
|
-
const
|
|
1691
|
+
const createRestFetcher = (baseUrl, defaultHeaders = {}) => {
|
|
1692
1692
|
const cache = /* @__PURE__ */ new Map();
|
|
1693
1693
|
const inflightRequests = /* @__PURE__ */ new Map();
|
|
1694
|
-
const
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
return cache.get(cacheKey);
|
|
1694
|
+
const get = (url) => {
|
|
1695
|
+
const fullUrl = `${baseUrl}${url}`;
|
|
1696
|
+
if (cache.has(fullUrl)) {
|
|
1697
|
+
return Promise.resolve(cache.get(fullUrl));
|
|
1699
1698
|
}
|
|
1700
|
-
if (inflightRequests.has(
|
|
1701
|
-
return inflightRequests.get(
|
|
1699
|
+
if (inflightRequests.has(fullUrl)) {
|
|
1700
|
+
return inflightRequests.get(fullUrl);
|
|
1702
1701
|
}
|
|
1703
|
-
const request = fetch(
|
|
1704
|
-
|
|
1705
|
-
method: "POST",
|
|
1706
|
-
body: JSON.stringify({ query: query2, variables }),
|
|
1707
|
-
credentials: "include",
|
|
1702
|
+
const request = fetch(fullUrl, {
|
|
1703
|
+
method: "GET",
|
|
1708
1704
|
headers: {
|
|
1709
1705
|
...BASE_HEADERS,
|
|
1710
|
-
...defaultHeaders
|
|
1711
|
-
...options.headers
|
|
1706
|
+
...defaultHeaders
|
|
1712
1707
|
}
|
|
1713
1708
|
}).then(async (res) => {
|
|
1714
1709
|
if (!res.ok) {
|
|
1715
|
-
|
|
1716
|
-
console.error(`Fetch error: ${res.status}`);
|
|
1717
|
-
return null;
|
|
1718
|
-
}
|
|
1719
|
-
throw new Error(`Fetch error: ${res.status}`);
|
|
1710
|
+
throw new Error(`Fetch error: ${res.status} ${res.statusText}`);
|
|
1720
1711
|
}
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1712
|
+
let data;
|
|
1713
|
+
try {
|
|
1714
|
+
data = await res.json();
|
|
1715
|
+
} catch {
|
|
1716
|
+
return null;
|
|
1724
1717
|
}
|
|
1718
|
+
cache.set(fullUrl, data);
|
|
1725
1719
|
return data;
|
|
1726
1720
|
}).finally(() => {
|
|
1727
|
-
inflightRequests.delete(
|
|
1721
|
+
inflightRequests.delete(fullUrl);
|
|
1728
1722
|
});
|
|
1729
|
-
inflightRequests.set(
|
|
1723
|
+
inflightRequests.set(fullUrl, request);
|
|
1730
1724
|
return request;
|
|
1731
1725
|
};
|
|
1732
|
-
const invalidate = (
|
|
1733
|
-
cache.delete(
|
|
1726
|
+
const invalidate = (url) => {
|
|
1727
|
+
cache.delete(`${baseUrl}${url}`);
|
|
1734
1728
|
};
|
|
1735
|
-
const clearCache = () =>
|
|
1736
|
-
|
|
1737
|
-
};
|
|
1738
|
-
const googleFonts = `googleFonts {
|
|
1739
|
-
id
|
|
1740
|
-
variants
|
|
1741
|
-
subsets
|
|
1742
|
-
family
|
|
1743
|
-
version
|
|
1744
|
-
files {
|
|
1745
|
-
url
|
|
1746
|
-
variant
|
|
1747
|
-
}
|
|
1748
|
-
category
|
|
1749
|
-
}`;
|
|
1750
|
-
const linkedCssChunk = `linkedCssChunk {
|
|
1751
|
-
id
|
|
1752
|
-
content
|
|
1753
|
-
}`;
|
|
1754
|
-
const linkedFragments = `{
|
|
1755
|
-
id
|
|
1756
|
-
document
|
|
1757
|
-
${googleFonts}
|
|
1758
|
-
${linkedCssChunk}
|
|
1759
|
-
}`;
|
|
1760
|
-
const fragment = `
|
|
1761
|
-
{
|
|
1762
|
-
id
|
|
1763
|
-
document
|
|
1764
|
-
linkedFragments ${linkedFragments}
|
|
1765
|
-
${googleFonts}
|
|
1766
|
-
${linkedCssChunk}
|
|
1767
|
-
}
|
|
1768
|
-
`;
|
|
1769
|
-
const getFragmentQuery = (fragmentId, isSelf) => {
|
|
1770
|
-
return {
|
|
1771
|
-
query: isSelf ? `
|
|
1772
|
-
query FragmentDocument($fragmentId: Int!) {
|
|
1773
|
-
fragment(fragmentIds: [$fragmentId]) ${fragment}
|
|
1774
|
-
}
|
|
1775
|
-
` : `
|
|
1776
|
-
query FragmentDocument($fragmentId: Int!) {
|
|
1777
|
-
clientFragment(fragmentId: $fragmentId) ${fragment}
|
|
1778
|
-
}
|
|
1779
|
-
}`,
|
|
1780
|
-
variables: {
|
|
1781
|
-
fragmentId
|
|
1782
|
-
},
|
|
1783
|
-
_type: null
|
|
1729
|
+
const clearCache = () => {
|
|
1730
|
+
cache.clear();
|
|
1784
1731
|
};
|
|
1732
|
+
return { get, invalidate, clearCache };
|
|
1785
1733
|
};
|
|
1786
1734
|
var isObject = (input) => {
|
|
1787
1735
|
return typeof input === "object" && input !== null && !Array.isArray(input);
|
|
@@ -1796,6 +1744,7 @@ var createConstants = (...constants) => {
|
|
|
1796
1744
|
};
|
|
1797
1745
|
var noop = () => void 0;
|
|
1798
1746
|
var isBrowser_default = typeof window !== "undefined";
|
|
1747
|
+
var generateId = () => Math.random().toString(16).slice(2);
|
|
1799
1748
|
var getKey = (v) => isKey(v) ? v.slice(1) : null;
|
|
1800
1749
|
var isKey = (v) => typeof v === "string" && v.startsWith("$");
|
|
1801
1750
|
function hashGenerator(layerKey) {
|
|
@@ -1953,159 +1902,88 @@ const fetchBeacon = (baseUrl) => {
|
|
|
1953
1902
|
sendBeacon
|
|
1954
1903
|
};
|
|
1955
1904
|
};
|
|
1956
|
-
const getAreaListQuery = (areaCodes) => {
|
|
1957
|
-
return {
|
|
1958
|
-
query: `query($areaCodes: [String!]!) {
|
|
1959
|
-
clientAreas(areaCodes: $areaCodes) {
|
|
1960
|
-
areaId
|
|
1961
|
-
campaignId
|
|
1962
|
-
areaProperties
|
|
1963
|
-
projectProperties
|
|
1964
|
-
font {
|
|
1965
|
-
id
|
|
1966
|
-
variants
|
|
1967
|
-
subsets
|
|
1968
|
-
family
|
|
1969
|
-
version
|
|
1970
|
-
files {
|
|
1971
|
-
url
|
|
1972
|
-
variant
|
|
1973
|
-
}
|
|
1974
|
-
category
|
|
1975
|
-
}
|
|
1976
|
-
variant {
|
|
1977
|
-
id
|
|
1978
|
-
fragment {
|
|
1979
|
-
props
|
|
1980
|
-
fragment ${fragment}
|
|
1981
|
-
}
|
|
1982
|
-
}
|
|
1983
|
-
}
|
|
1984
|
-
}`,
|
|
1985
|
-
variables: {
|
|
1986
|
-
areaCodes
|
|
1987
|
-
}
|
|
1988
|
-
};
|
|
1989
|
-
};
|
|
1990
1905
|
const fetchPlugin = (state) => {
|
|
1991
|
-
var _a, _b, _c
|
|
1992
|
-
const
|
|
1993
|
-
const
|
|
1994
|
-
const
|
|
1995
|
-
const referer = (_d = state == null ? void 0 : state.env) == null ? void 0 : _d.referer;
|
|
1906
|
+
var _a, _b, _c;
|
|
1907
|
+
const url = (_a = state == null ? void 0 : state.env) == null ? void 0 : _a.backendEndpoint;
|
|
1908
|
+
const apiToken = (_b = state == null ? void 0 : state.env) == null ? void 0 : _b.apiToken;
|
|
1909
|
+
const referer = (_c = state == null ? void 0 : state.env) == null ? void 0 : _c.referer;
|
|
1996
1910
|
let headers = {
|
|
1997
|
-
|
|
1911
|
+
"X-Api-Key": apiToken
|
|
1998
1912
|
};
|
|
1999
1913
|
if (referer) {
|
|
2000
1914
|
headers.Referer = referer;
|
|
2001
1915
|
}
|
|
2002
|
-
const
|
|
1916
|
+
const restFetcher = createRestFetcher(url, headers);
|
|
2003
1917
|
const beaconFetcher = fetchBeacon();
|
|
2004
|
-
const registerFragmentDocument = (fragmentId,
|
|
2005
|
-
|
|
2006
|
-
const fragmentDocument = fragment2 == null ? void 0 : fragment2.document;
|
|
1918
|
+
const registerFragmentDocument = (fragmentId, fragment) => {
|
|
1919
|
+
const fragmentDocument = fragment == null ? void 0 : fragment.document;
|
|
2007
1920
|
if (!fragmentDocument) {
|
|
2008
1921
|
console.error("Empty document");
|
|
2009
1922
|
return null;
|
|
2010
1923
|
}
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
(
|
|
2016
|
-
|
|
2017
|
-
}
|
|
2018
|
-
if (Array.isArray(fragment2.linkedCssChunk)) {
|
|
2019
|
-
fragment2.linkedCssChunk.forEach(
|
|
2020
|
-
(linkedChunk) => state.$fetch.cacheCssChunks.set(linkedChunk.id, linkedChunk.content)
|
|
2021
|
-
);
|
|
2022
|
-
}
|
|
2023
|
-
if ("$fonts" in state) {
|
|
2024
|
-
(_b2 = fragment2 == null ? void 0 : fragment2.googleFonts) == null ? void 0 : _b2.forEach((_a2 = state.$fonts) == null ? void 0 : _a2.registerFont);
|
|
2025
|
-
}
|
|
2026
|
-
return fragmentDocument;
|
|
2027
|
-
}
|
|
2028
|
-
return null;
|
|
2029
|
-
};
|
|
2030
|
-
const queryFragment = async (fragmentId) => {
|
|
2031
|
-
var _a2;
|
|
2032
|
-
if (!apiToken || !fragmentId) return null;
|
|
2033
|
-
if (state.$fetch.cacheDocuments.has(fragmentId)) {
|
|
2034
|
-
return state.$fetch.cacheDocuments.get(fragmentId);
|
|
2035
|
-
}
|
|
2036
|
-
const fragmentQuery = getFragmentQuery(fragmentId, isSelf);
|
|
2037
|
-
const response = await fetcher.query(
|
|
2038
|
-
fragmentQuery.query,
|
|
2039
|
-
fragmentQuery.variables,
|
|
2040
|
-
{ referrerPolicy: "unsafe-url" }
|
|
2041
|
-
);
|
|
2042
|
-
let fragment2 = null;
|
|
2043
|
-
if (!!(response == null ? void 0 : response.data) && "clientFragment" in response.data) {
|
|
2044
|
-
fragment2 = response.data.clientFragment;
|
|
1924
|
+
state.$fetch.cacheDocuments.set(fragmentId, fragmentDocument);
|
|
1925
|
+
if (fragment.linkedFragments !== void 0) {
|
|
1926
|
+
fragment.linkedFragments.forEach((linked) => {
|
|
1927
|
+
if ((linked == null ? void 0 : linked.id) && (linked == null ? void 0 : linked.document)) {
|
|
1928
|
+
state.$fetch.cacheDocuments.set(linked.id, linked.document);
|
|
1929
|
+
}
|
|
1930
|
+
});
|
|
2045
1931
|
}
|
|
2046
|
-
if (
|
|
2047
|
-
|
|
1932
|
+
if (Array.isArray(fragment.fragmentCssChunks)) {
|
|
1933
|
+
fragment.fragmentCssChunks.forEach(({ cssChunk }) => {
|
|
1934
|
+
if (cssChunk) {
|
|
1935
|
+
state.$fetch.cacheCssChunks.set(cssChunk.id, cssChunk.content);
|
|
1936
|
+
}
|
|
1937
|
+
});
|
|
2048
1938
|
}
|
|
2049
|
-
return
|
|
1939
|
+
return fragmentDocument;
|
|
2050
1940
|
};
|
|
2051
|
-
const
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
const queryAreaList = async (areaCodes2) => {
|
|
2055
|
-
var _a2;
|
|
2056
|
-
if (!apiToken || !areaCodes2) return null;
|
|
2057
|
-
const nonLoadedAreas = areaCodes2.filter(
|
|
1941
|
+
const queryAreaList = async (areaCodes) => {
|
|
1942
|
+
if (!apiToken || !(areaCodes == null ? void 0 : areaCodes.length)) return [];
|
|
1943
|
+
const nonLoadedCodes = areaCodes.filter(
|
|
2058
1944
|
(code) => !state.$fetch.cacheAreaDocuments.has(code)
|
|
2059
1945
|
);
|
|
2060
|
-
if (
|
|
2061
|
-
|
|
1946
|
+
if (nonLoadedCodes.length > 0) {
|
|
1947
|
+
await Promise.all(
|
|
1948
|
+
nonLoadedCodes.map(async (code) => {
|
|
1949
|
+
var _a2, _b2, _c2, _d;
|
|
1950
|
+
const dto = await restFetcher.get(`/api/area/${code}`);
|
|
1951
|
+
if (!dto) return;
|
|
1952
|
+
const fragment = (_a2 = dto.variant) == null ? void 0 : _a2.fragment;
|
|
1953
|
+
if (fragment) {
|
|
1954
|
+
registerFragmentDocument(fragment.id, fragment);
|
|
1955
|
+
}
|
|
1956
|
+
if ("$fonts" in state && dto.font) {
|
|
1957
|
+
(_b2 = state.$fonts) == null ? void 0 : _b2.registerFont(dto.font);
|
|
1958
|
+
}
|
|
1959
|
+
const entity = {
|
|
1960
|
+
fragmentId: fragment == null ? void 0 : fragment.id,
|
|
1961
|
+
props: ((_c2 = dto.variant) == null ? void 0 : _c2.props) ?? {},
|
|
1962
|
+
font: dto.font,
|
|
1963
|
+
areaId: dto.areaId,
|
|
1964
|
+
campaignId: dto.campaignId,
|
|
1965
|
+
variantId: (_d = dto.variant) == null ? void 0 : _d.id
|
|
1966
|
+
};
|
|
1967
|
+
state.$fetch.cacheAreaDocuments.set(code, entity);
|
|
1968
|
+
})
|
|
1969
|
+
);
|
|
2062
1970
|
}
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
areaQuery.query,
|
|
2066
|
-
areaQuery.variables,
|
|
2067
|
-
{ referrerPolicy: "unsafe-url" }
|
|
1971
|
+
return areaCodes.map(
|
|
1972
|
+
(code) => state.$fetch.cacheAreaDocuments.get(code) ?? null
|
|
2068
1973
|
);
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
var _a3;
|
|
2073
|
-
const areaCode = areaCodes2 == null ? void 0 : areaCodes2.at(index2);
|
|
2074
|
-
const areaFragment = area.variant.fragment.fragment;
|
|
2075
|
-
registerFragmentDocument(areaFragment.id, areaFragment);
|
|
2076
|
-
const resultProps = [
|
|
2077
|
-
...area.projectProperties ?? [],
|
|
2078
|
-
...area.areaProperties ?? []
|
|
2079
|
-
].reduce((acc, prop) => {
|
|
2080
|
-
acc[prop._id] = prop.defaultValue;
|
|
2081
|
-
return acc;
|
|
2082
|
-
}, area.variant.fragment.props);
|
|
2083
|
-
const entity = {
|
|
2084
|
-
areaId: area.areaId,
|
|
2085
|
-
campaignId: area.campaignId,
|
|
2086
|
-
variantId: area.variant.id,
|
|
2087
|
-
fragmentId: area.variant.fragment.fragment.id,
|
|
2088
|
-
font: area.font,
|
|
2089
|
-
props: resultProps
|
|
2090
|
-
};
|
|
2091
|
-
if ("$fonts" in state) {
|
|
2092
|
-
(_a3 = state.$fonts) == null ? void 0 : _a3.registerFont(area.font);
|
|
2093
|
-
}
|
|
2094
|
-
state.$fetch.cacheAreaDocuments.set(areaCode, entity);
|
|
2095
|
-
});
|
|
2096
|
-
return areaCodes2.map((code) => state.$fetch.cacheAreaDocuments.get(code));
|
|
2097
|
-
}
|
|
2098
|
-
return null;
|
|
1974
|
+
};
|
|
1975
|
+
const queryArea = async (areaCode) => {
|
|
1976
|
+
return queryAreaList([areaCode]).then((res) => (res == null ? void 0 : res.at(0)) ?? null);
|
|
2099
1977
|
};
|
|
2100
1978
|
state.$fetch = {
|
|
2101
1979
|
cacheCssChunks: /* @__PURE__ */ new Map(),
|
|
2102
1980
|
cacheDocuments: /* @__PURE__ */ new Map(),
|
|
2103
1981
|
cacheAreaDocuments: /* @__PURE__ */ new Map(),
|
|
2104
|
-
|
|
1982
|
+
registerFragmentDocument,
|
|
2105
1983
|
queryArea,
|
|
2106
1984
|
queryAreaList,
|
|
2107
|
-
query: fetcher.query,
|
|
2108
1985
|
sendBeacon: beaconFetcher.sendBeacon,
|
|
1986
|
+
get: restFetcher.get,
|
|
2109
1987
|
readCssChunk: (id) => state.$fetch.cacheCssChunks.get(id) ?? null,
|
|
2110
1988
|
readFragment: (fragmentId) => state.$fetch.cacheDocuments.get(fragmentId) ?? null,
|
|
2111
1989
|
readArea: (areaCode) => state.$fetch.cacheAreaDocuments.get(areaCode) ?? null
|
|
@@ -2408,6 +2286,84 @@ const scopesPlugin = (state) => {
|
|
|
2408
2286
|
};
|
|
2409
2287
|
return state;
|
|
2410
2288
|
};
|
|
2289
|
+
const PROPERTIES_ROOT_KEY = "PropertiesRoot:root";
|
|
2290
|
+
const propertiesPlugin = (state) => {
|
|
2291
|
+
const hydrate = (properties) => {
|
|
2292
|
+
if (!Array.isArray(properties)) return;
|
|
2293
|
+
const keys = [];
|
|
2294
|
+
for (const prop of properties) {
|
|
2295
|
+
if (!prop || !prop._id) continue;
|
|
2296
|
+
const key = `${index.nodes.Variable}:${prop._id}`;
|
|
2297
|
+
state.mutate(
|
|
2298
|
+
{ ...prop, _type: index.nodes.Variable, _id: String(prop._id) },
|
|
2299
|
+
{ replace: true }
|
|
2300
|
+
);
|
|
2301
|
+
keys.push(key);
|
|
2302
|
+
}
|
|
2303
|
+
state.mutate(
|
|
2304
|
+
PROPERTIES_ROOT_KEY,
|
|
2305
|
+
{ propertyKeys: keys },
|
|
2306
|
+
{ replace: true }
|
|
2307
|
+
);
|
|
2308
|
+
};
|
|
2309
|
+
const addProperty = (type, initialData) => {
|
|
2310
|
+
const id = (initialData == null ? void 0 : initialData._id) || generateId();
|
|
2311
|
+
const key = `${index.nodes.Variable}:${id}`;
|
|
2312
|
+
state.mutate({
|
|
2313
|
+
_type: index.nodes.Variable,
|
|
2314
|
+
_id: String(id),
|
|
2315
|
+
type,
|
|
2316
|
+
name: `${type} property`,
|
|
2317
|
+
...initialData
|
|
2318
|
+
});
|
|
2319
|
+
const root = state.resolve(PROPERTIES_ROOT_KEY);
|
|
2320
|
+
const currentKeys = (root == null ? void 0 : root.propertyKeys) ?? [];
|
|
2321
|
+
state.mutate(
|
|
2322
|
+
PROPERTIES_ROOT_KEY,
|
|
2323
|
+
{ propertyKeys: [...currentKeys, key] },
|
|
2324
|
+
{ replace: true }
|
|
2325
|
+
);
|
|
2326
|
+
return key;
|
|
2327
|
+
};
|
|
2328
|
+
const removeProperty = (key) => {
|
|
2329
|
+
state.invalidate(key);
|
|
2330
|
+
const root = state.resolve(PROPERTIES_ROOT_KEY);
|
|
2331
|
+
const currentKeys = (root == null ? void 0 : root.propertyKeys) ?? [];
|
|
2332
|
+
state.mutate(
|
|
2333
|
+
PROPERTIES_ROOT_KEY,
|
|
2334
|
+
{ propertyKeys: currentKeys.filter((k) => k !== key) },
|
|
2335
|
+
{ replace: true }
|
|
2336
|
+
);
|
|
2337
|
+
};
|
|
2338
|
+
const extractProperties = () => {
|
|
2339
|
+
const root = state.resolve(PROPERTIES_ROOT_KEY);
|
|
2340
|
+
const keys = (root == null ? void 0 : root.propertyKeys) ?? [];
|
|
2341
|
+
return keys.map((key) => state.resolve(key, { deep: true })).filter(Boolean);
|
|
2342
|
+
};
|
|
2343
|
+
const getPropertyKeys = () => {
|
|
2344
|
+
const root = state.resolve(PROPERTIES_ROOT_KEY);
|
|
2345
|
+
return (root == null ? void 0 : root.propertyKeys) ?? [];
|
|
2346
|
+
};
|
|
2347
|
+
state.addSkip((dataField) => {
|
|
2348
|
+
if (typeof dataField === "object" && dataField !== null && !Array.isArray(dataField)) {
|
|
2349
|
+
return dataField._type === index.nodes.Variable && (dataField._id === "replaceBeforeSave" || !dataField._id);
|
|
2350
|
+
}
|
|
2351
|
+
return false;
|
|
2352
|
+
});
|
|
2353
|
+
state.mutate({
|
|
2354
|
+
_type: "PropertiesRoot",
|
|
2355
|
+
_id: "root",
|
|
2356
|
+
propertyKeys: []
|
|
2357
|
+
});
|
|
2358
|
+
state.$properties = {
|
|
2359
|
+
key: PROPERTIES_ROOT_KEY,
|
|
2360
|
+
hydrate,
|
|
2361
|
+
addProperty,
|
|
2362
|
+
removeProperty,
|
|
2363
|
+
extractProperties,
|
|
2364
|
+
getPropertyKeys
|
|
2365
|
+
};
|
|
2366
|
+
};
|
|
2411
2367
|
const fragmentsPlugin = (options) => (state) => {
|
|
2412
2368
|
const plugins = (options == null ? void 0 : options.plugins) ?? [];
|
|
2413
2369
|
const createFragmentManager = (fragmentId, initialDocument = {}) => {
|
|
@@ -2444,6 +2400,7 @@ const fragmentsPlugin = (options) => (state) => {
|
|
|
2444
2400
|
// cssPlugin,
|
|
2445
2401
|
fragmentStylesheetPlugin(state),
|
|
2446
2402
|
scopesPlugin,
|
|
2403
|
+
propertiesPlugin,
|
|
2447
2404
|
...plugins
|
|
2448
2405
|
],
|
|
2449
2406
|
skip: [
|
|
@@ -2454,12 +2411,19 @@ const fragmentsPlugin = (options) => (state) => {
|
|
|
2454
2411
|
...Object.keys(index.nodes),
|
|
2455
2412
|
"Temp",
|
|
2456
2413
|
"Spring",
|
|
2457
|
-
PLUGIN_TYPES.FragmentStylesheet
|
|
2414
|
+
PLUGIN_TYPES.FragmentStylesheet,
|
|
2415
|
+
"PropertiesRoot"
|
|
2458
2416
|
])
|
|
2459
2417
|
]
|
|
2460
2418
|
});
|
|
2461
2419
|
manager.mutate(tempGraph);
|
|
2462
2420
|
manager.mutate(springGraph);
|
|
2421
|
+
const fragmentRoot = manager.resolve(manager.$fragment.root);
|
|
2422
|
+
const propertyLinks = (fragmentRoot == null ? void 0 : fragmentRoot.properties) ?? [];
|
|
2423
|
+
const propertyEntities = propertyLinks.map((link) => manager.resolve(link)).filter(Boolean);
|
|
2424
|
+
if (propertyEntities.length > 0) {
|
|
2425
|
+
manager.$properties.hydrate(propertyEntities);
|
|
2426
|
+
}
|
|
2463
2427
|
state.mutate(state.$fragments.key, {
|
|
2464
2428
|
managers: {
|
|
2465
2429
|
[fragmentId]: manager
|
|
@@ -2488,9 +2452,6 @@ const fragmentsPlugin = (options) => (state) => {
|
|
|
2488
2452
|
});
|
|
2489
2453
|
return state;
|
|
2490
2454
|
};
|
|
2491
|
-
const addClientMetric = `mutation AddClientMetric($type: ClientMetricType!, $value: GoalAchievementPost!) {
|
|
2492
|
-
addClientMetric(metric: {metricType: $type, achievement: $value})
|
|
2493
|
-
}`;
|
|
2494
2455
|
const pendingPixels = /* @__PURE__ */ new Set();
|
|
2495
2456
|
const sendWithImage = (url) => {
|
|
2496
2457
|
try {
|
|
@@ -2564,10 +2525,16 @@ const types = createConstants(
|
|
|
2564
2525
|
"REACH_PROJECT_GOAL"
|
|
2565
2526
|
);
|
|
2566
2527
|
const globalMetricsPlugin = (state) => {
|
|
2567
|
-
const sendMetric =
|
|
2528
|
+
const sendMetric = (type, value) => {
|
|
2568
2529
|
var _a;
|
|
2569
|
-
|
|
2570
|
-
|
|
2530
|
+
if (type !== types.REACH_PROJECT_GOAL || !value) return;
|
|
2531
|
+
const params = new URLSearchParams({
|
|
2532
|
+
goal_id: String(value.goalId),
|
|
2533
|
+
variant_id: String(value.variantId),
|
|
2534
|
+
area_id: String(value.areaId),
|
|
2535
|
+
campaign_id: String(value.campaignId)
|
|
2536
|
+
});
|
|
2537
|
+
(_a = state.$fetch) == null ? void 0 : _a.get(`/api/metric/goal?${params}`).catch(() => void 0);
|
|
2571
2538
|
};
|
|
2572
2539
|
const reachGoal = (goal) => {
|
|
2573
2540
|
sendMetric(types.REACH_PROJECT_GOAL, goal);
|
|
@@ -2591,23 +2558,17 @@ const loadPlugin = (state) => {
|
|
|
2591
2558
|
const readFragment = (_a = state == null ? void 0 : state.$fetch) == null ? void 0 : _a.readFragment(fragmentId);
|
|
2592
2559
|
const fragmentManager = (_b = state == null ? void 0 : state.$fragments) == null ? void 0 : _b.getManager(fragmentId);
|
|
2593
2560
|
if (readFragment && !fragmentManager) {
|
|
2594
|
-
|
|
2595
|
-
fragmentId,
|
|
2596
|
-
readFragment
|
|
2597
|
-
);
|
|
2598
|
-
return fragmentManager2;
|
|
2561
|
+
return state.$fragments.createFragmentManager(fragmentId, readFragment);
|
|
2599
2562
|
}
|
|
2600
2563
|
if (readFragment && fragmentManager) return fragmentManager;
|
|
2601
|
-
return
|
|
2602
|
-
(fragmentDocument) => state.$fragments.createFragmentManager(fragmentId, fragmentDocument)
|
|
2603
|
-
);
|
|
2564
|
+
return null;
|
|
2604
2565
|
};
|
|
2605
2566
|
const loadArea = (areaCode) => {
|
|
2606
2567
|
var _a;
|
|
2607
2568
|
const readArea2 = (_a = state == null ? void 0 : state.$fetch) == null ? void 0 : _a.readArea(areaCode);
|
|
2608
2569
|
if (readArea2) return readArea2;
|
|
2609
|
-
return state.$fetch.queryArea(areaCode).then(
|
|
2610
|
-
|
|
2570
|
+
return state.$fetch.queryArea(areaCode).then((areaEntity) => {
|
|
2571
|
+
loadFragment(areaEntity == null ? void 0 : areaEntity.fragmentId);
|
|
2611
2572
|
return areaEntity;
|
|
2612
2573
|
});
|
|
2613
2574
|
};
|
|
@@ -2624,6 +2585,7 @@ const fontsPlugin = (state) => {
|
|
|
2624
2585
|
const fonts = /* @__PURE__ */ new Map();
|
|
2625
2586
|
let defaultFontFamily = null;
|
|
2626
2587
|
const registerFont = (font) => {
|
|
2588
|
+
if (!font) return;
|
|
2627
2589
|
const fontFamily = font.family;
|
|
2628
2590
|
if (!fonts.has(fontFamily)) {
|
|
2629
2591
|
fonts.set(fontFamily, font);
|
|
@@ -2637,7 +2599,8 @@ const fontsPlugin = (state) => {
|
|
|
2637
2599
|
const getStyles = () => {
|
|
2638
2600
|
const styles = [];
|
|
2639
2601
|
for (const [fontFamily, font] of fonts) {
|
|
2640
|
-
const
|
|
2602
|
+
const rawFiles = font.files ?? [];
|
|
2603
|
+
const files = Array.isArray(rawFiles) ? rawFiles : Object.entries(rawFiles).map(([variant, url]) => ({ variant, url }));
|
|
2641
2604
|
for (const file of files) {
|
|
2642
2605
|
const [weightItem] = getFontWeights([file.variant]);
|
|
2643
2606
|
styles.push(`@font-face {
|
|
@@ -2676,27 +2639,27 @@ const PLUGIN_TYPES = createConstants(
|
|
|
2676
2639
|
"FragmentStylesheet"
|
|
2677
2640
|
);
|
|
2678
2641
|
const createFragmentsClient = (options) => {
|
|
2679
|
-
const BACKEND_TARGET = (options == null ? void 0 : options.isSelf) ? "/graphql" : "http://85.192.29.65/graphql";
|
|
2680
2642
|
return kt({
|
|
2681
2643
|
_type: "GlobalManager",
|
|
2682
2644
|
initialState: {},
|
|
2683
2645
|
skip: [
|
|
2684
2646
|
u([
|
|
2685
2647
|
...Object.keys(index.nodes),
|
|
2686
|
-
...Object.keys(PLUGIN_TYPES)
|
|
2648
|
+
...Object.keys(PLUGIN_TYPES),
|
|
2649
|
+
"PropertiesRoot"
|
|
2687
2650
|
])
|
|
2688
2651
|
],
|
|
2689
2652
|
plugins: [
|
|
2690
2653
|
(state) => {
|
|
2691
2654
|
state.env = {
|
|
2692
|
-
|
|
2693
|
-
backendEndpoint: (options == null ? void 0 : options.backendEndpoint) ?? BACKEND_TARGET,
|
|
2655
|
+
backendEndpoint: (options == null ? void 0 : options.backendEndpoint) ?? "https://client-api.fragmentsx.com",
|
|
2694
2656
|
apiToken: options == null ? void 0 : options.apiToken,
|
|
2695
2657
|
referer: options == null ? void 0 : options.referer
|
|
2696
2658
|
};
|
|
2697
2659
|
},
|
|
2698
2660
|
fetchPlugin,
|
|
2699
2661
|
fontsPlugin,
|
|
2662
|
+
propertiesPlugin,
|
|
2700
2663
|
fragmentsPlugin({
|
|
2701
2664
|
plugins: options == null ? void 0 : options.fragmentPlugins
|
|
2702
2665
|
}),
|
|
@@ -2707,6 +2670,39 @@ const createFragmentsClient = (options) => {
|
|
|
2707
2670
|
]
|
|
2708
2671
|
});
|
|
2709
2672
|
};
|
|
2673
|
+
const areaMetaPlugin = (options) => (state) => {
|
|
2674
|
+
state.$area = {
|
|
2675
|
+
areaId: options.areaId,
|
|
2676
|
+
campaignId: options.campaignId,
|
|
2677
|
+
variantId: options.variantId
|
|
2678
|
+
};
|
|
2679
|
+
};
|
|
2680
|
+
const createAreaManager = (options) => {
|
|
2681
|
+
var _a;
|
|
2682
|
+
const manager = kt({
|
|
2683
|
+
_type: "AreaManager",
|
|
2684
|
+
_id: String(options.areaId),
|
|
2685
|
+
initialState: {},
|
|
2686
|
+
skip: [
|
|
2687
|
+
u([
|
|
2688
|
+
...Object.keys(index.nodes),
|
|
2689
|
+
"PropertiesRoot"
|
|
2690
|
+
])
|
|
2691
|
+
],
|
|
2692
|
+
plugins: [
|
|
2693
|
+
areaMetaPlugin({
|
|
2694
|
+
areaId: options.areaId,
|
|
2695
|
+
campaignId: options.campaignId,
|
|
2696
|
+
variantId: options.variantId
|
|
2697
|
+
}),
|
|
2698
|
+
propertiesPlugin
|
|
2699
|
+
]
|
|
2700
|
+
});
|
|
2701
|
+
if ((_a = options.areaProperties) == null ? void 0 : _a.length) {
|
|
2702
|
+
manager.$properties.hydrate(options.areaProperties);
|
|
2703
|
+
}
|
|
2704
|
+
return manager;
|
|
2705
|
+
};
|
|
2710
2706
|
const ssrPlugin = (state) => {
|
|
2711
2707
|
var _a, _b, _c;
|
|
2712
2708
|
if (!["$fragments"].every((field) => field in state)) {
|
|
@@ -2768,7 +2764,9 @@ function createTestFragmentsClient(options) {
|
|
|
2768
2764
|
return client;
|
|
2769
2765
|
}
|
|
2770
2766
|
export {
|
|
2767
|
+
createAreaManager,
|
|
2771
2768
|
createFragmentsClient,
|
|
2772
2769
|
createTestFragmentsClient,
|
|
2770
|
+
propertiesPlugin,
|
|
2773
2771
|
ssrPlugin
|
|
2774
2772
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"areaQueries.test.d.ts","sourceRoot":"","sources":["../../../src/plugins/fetch/areaQueries.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { Plugin } from '@graph-state/core';
|
|
2
|
-
import { createFetcher } from './fetcher';
|
|
3
2
|
import { fetchBeacon } from './beacon';
|
|
4
3
|
interface AreaCacheEntity {
|
|
5
4
|
fragmentId: number;
|
|
6
5
|
props: Record<string, unknown>;
|
|
6
|
+
font: unknown;
|
|
7
|
+
areaId: number;
|
|
8
|
+
campaignId: number;
|
|
9
|
+
variantId: number;
|
|
7
10
|
}
|
|
8
11
|
declare module "@graph-state/core" {
|
|
9
12
|
interface GraphState {
|
|
@@ -11,17 +14,17 @@ declare module "@graph-state/core" {
|
|
|
11
14
|
/**
|
|
12
15
|
* Хранятся документы от фрагментов
|
|
13
16
|
*/
|
|
14
|
-
cacheDocuments: Map<number,
|
|
17
|
+
cacheDocuments: Map<number, unknown>;
|
|
15
18
|
cacheCssChunks: Map<number, string>;
|
|
16
19
|
cacheAreaDocuments: Map<string, AreaCacheEntity>;
|
|
17
20
|
readCssChunk: (id: number) => string | null;
|
|
18
|
-
readFragment: (fragmentId: number) =>
|
|
21
|
+
readFragment: (fragmentId: number) => unknown | null;
|
|
19
22
|
readArea: (areaCode: string) => AreaCacheEntity | null;
|
|
20
|
-
|
|
21
|
-
queryArea: (areaCode: string) => AreaCacheEntity
|
|
22
|
-
queryAreaList: (areaCodes: string[]) => AreaCacheEntity[]
|
|
23
|
-
query: ReturnType<typeof createFetcher>["query"];
|
|
23
|
+
registerFragmentDocument: (fragmentId: number, fragment: unknown) => unknown;
|
|
24
|
+
queryArea: (areaCode: string) => Promise<AreaCacheEntity | null>;
|
|
25
|
+
queryAreaList: (areaCodes: string[]) => Promise<(AreaCacheEntity | null)[]>;
|
|
24
26
|
sendBeacon: ReturnType<typeof fetchBeacon>["sendBeacon"];
|
|
27
|
+
get: <T>(path: string) => Promise<T>;
|
|
25
28
|
};
|
|
26
29
|
}
|
|
27
30
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/plugins/fetch/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/plugins/fetch/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAE3C,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAErD,UAAU,eAAe;IACvB,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,IAAI,EAAE,OAAO,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,OAAO,QAAQ,mBAAmB,CAAC;IACjC,UAAU,UAAU;QAClB,MAAM,EAAE;YACN;;eAEG;YACH,cAAc,EAAE,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;YACrC,cAAc,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;YACpC,kBAAkB,EAAE,GAAG,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;YACjD,YAAY,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,CAAC;YAC5C,YAAY,EAAE,CAAC,UAAU,EAAE,MAAM,KAAK,OAAO,GAAG,IAAI,CAAC;YACrD,QAAQ,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,eAAe,GAAG,IAAI,CAAC;YACvD,wBAAwB,EAAE,CACxB,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,OAAO,KACd,OAAO,CAAC;YACb,SAAS,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,CAAC;YACjE,aAAa,EAAE,CACb,SAAS,EAAE,MAAM,EAAE,KAChB,OAAO,CAAC,CAAC,eAAe,GAAG,IAAI,CAAC,EAAE,CAAC,CAAC;YACzC,UAAU,EAAE,UAAU,CAAC,OAAO,WAAW,CAAC,CAAC,YAAY,CAAC,CAAC;YACzD,GAAG,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;SACtC,CAAC;KACH;CACF;AAED,eAAO,MAAM,WAAW,EAAE,MA6GzB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"restFetcher.d.ts","sourceRoot":"","sources":["../../../src/plugins/fetch/restFetcher.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,iBAAiB,YACnB,MAAM,mBACC,WAAW;UAKd,CAAC,OAAO,MAAM,KAAG,OAAO,CAAC,CAAC,CAAC;sBAuCf,MAAM,KAAG,IAAI;sBAIf,IAAI;CAK5B,CAAC"}
|