@builder.io/sdk-react-native 0.1.10 → 0.1.12

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.
@@ -22,15 +22,10 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
25
  Object.defineProperty(exports, "__esModule", { value: true });
29
- const BaseText_1 = __importDefault(require("../BaseText"));
30
26
  const React = __importStar(require("react"));
31
27
  const react_native_1 = require("react-native");
32
28
  function FragmentComponent(props) {
33
- return (React.createElement(react_native_1.View, null,
34
- React.createElement(BaseText_1.default, null, props.children)));
29
+ return React.createElement(react_native_1.View, null, props.children);
35
30
  }
36
31
  exports.default = FragmentComponent;
@@ -22,11 +22,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
25
  Object.defineProperty(exports, "__esModule", { value: true });
29
- const BaseText_1 = __importDefault(require("../BaseText"));
30
26
  const React = __importStar(require("react"));
31
27
  const react_native_1 = require("react-native");
32
28
  function SectionComponent(props) {
@@ -41,7 +37,6 @@ function SectionComponent(props) {
41
37
  alignItems: "stretch",
42
38
  marginLeft: "auto",
43
39
  marginRight: "auto",
44
- } },
45
- React.createElement(BaseText_1.default, null, props.children)));
40
+ } }, props.children));
46
41
  }
47
42
  exports.default = SectionComponent;
@@ -34,8 +34,8 @@ const builder_context_js_1 = __importDefault(require("../../context/builder.cont
34
34
  const index_js_1 = require("../../functions/get-content/index.js");
35
35
  const target_1 = require("../../constants/target");
36
36
  function Symbol(props) {
37
- var _a, _b, _c, _d, _e;
38
- function className() {
37
+ var _a, _b, _c;
38
+ const [className, setClassName] = (0, react_1.useState)(() => {
39
39
  var _a, _b;
40
40
  return [
41
41
  ...(target_1.TARGET === "vue2" || target_1.TARGET === "vue3"
@@ -49,15 +49,10 @@ function Symbol(props) {
49
49
  ]
50
50
  .filter(Boolean)
51
51
  .join(" ");
52
- }
53
- const [fetchedContent, setFetchedContent] = (0, react_1.useState)(() => null);
54
- function contentToUse() {
52
+ });
53
+ const [contentToUse, setContentToUse] = (0, react_1.useState)(() => { var _a; return (_a = props.symbol) === null || _a === void 0 ? void 0 : _a.content; });
54
+ function fetchContent() {
55
55
  var _a;
56
- return ((_a = props.symbol) === null || _a === void 0 ? void 0 : _a.content) || fetchedContent;
57
- }
58
- const builderContext = (0, react_1.useContext)(builder_context_js_1.default);
59
- (0, react_1.useEffect)(() => {
60
- const symbolToUse = props.symbol;
61
56
  /**
62
57
  * If:
63
58
  * - we have a symbol prop
@@ -67,31 +62,43 @@ function Symbol(props) {
67
62
  *
68
63
  * then we want to re-fetch the symbol content.
69
64
  */
70
- if (symbolToUse &&
71
- !symbolToUse.content &&
72
- !fetchedContent &&
73
- symbolToUse.model &&
65
+ if (!contentToUse &&
66
+ ((_a = props.symbol) === null || _a === void 0 ? void 0 : _a.model) &&
74
67
  (
75
68
  // This is a hack, we should not need to check for this, but it is needed for Svelte.
76
69
  builderContext === null || builderContext === void 0 ? void 0 : builderContext.apiKey)) {
77
70
  (0, index_js_1.getContent)({
78
- model: symbolToUse.model,
71
+ model: props.symbol.model,
79
72
  apiKey: builderContext.apiKey,
73
+ apiVersion: builderContext.apiVersion,
80
74
  query: {
81
- id: symbolToUse.entry,
75
+ id: props.symbol.entry,
82
76
  },
83
- }).then((response) => {
84
- setFetchedContent(response);
77
+ })
78
+ .then((response) => {
79
+ if (response) {
80
+ setContentToUse(response);
81
+ }
82
+ })
83
+ .catch((err) => {
84
+ console.error("[Builder.io]: Could not fetch symbol content: ", err);
85
85
  });
86
86
  }
87
- }, [props.symbol, fetchedContent]);
87
+ }
88
+ const builderContext = (0, react_1.useContext)(builder_context_js_1.default);
89
+ (0, react_1.useEffect)(() => {
90
+ fetchContent();
91
+ }, []);
92
+ (0, react_1.useEffect)(() => {
93
+ fetchContent();
94
+ }, [props.symbol]);
88
95
  return (React.createElement(react_native_1.View, { ...props.attributes, dataSet: {
89
- class: className(),
96
+ class: className,
90
97
  } },
91
- React.createElement(render_content_1.default, { apiKey: builderContext.apiKey, context: builderContext.context, customComponents: Object.values(builderContext.registeredComponents), data: {
98
+ React.createElement(render_content_1.default, { apiVersion: builderContext.apiVersion, apiKey: builderContext.apiKey, context: builderContext.context, customComponents: Object.values(builderContext.registeredComponents), data: {
92
99
  ...(_a = props.symbol) === null || _a === void 0 ? void 0 : _a.data,
93
100
  ...builderContext.state,
94
- ...(_d = (_c = (_b = props.symbol) === null || _b === void 0 ? void 0 : _b.content) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.state,
95
- }, model: (_e = props.symbol) === null || _e === void 0 ? void 0 : _e.model, content: contentToUse() })));
101
+ ...(_b = contentToUse === null || contentToUse === void 0 ? void 0 : contentToUse.data) === null || _b === void 0 ? void 0 : _b.state,
102
+ }, model: (_c = props.symbol) === null || _c === void 0 ? void 0 : _c.model, content: contentToUse })));
96
103
  }
97
104
  exports.default = Symbol;
@@ -275,6 +275,7 @@ function RenderContent(props) {
275
275
  (0, index_js_1.getContent)({
276
276
  model: props.model,
277
277
  apiKey: props.apiKey,
278
+ apiVersion: props.apiVersion,
278
279
  }).then((content) => {
279
280
  if (content) {
280
281
  mergeNewContent(content);
@@ -287,6 +288,11 @@ function RenderContent(props) {
287
288
  emitStateUpdate();
288
289
  }
289
290
  }, []);
291
+ (0, react_1.useEffect)(() => {
292
+ if (props.content) {
293
+ mergeNewContent(props.content);
294
+ }
295
+ }, [props.content]);
290
296
  (0, react_1.useEffect)(() => {
291
297
  evaluateJsCode();
292
298
  }, [(_a = useContent === null || useContent === void 0 ? void 0 : useContent.data) === null || _a === void 0 ? void 0 : _a.jsCode, contentState]);
@@ -310,6 +316,7 @@ function RenderContent(props) {
310
316
  setState: setContextState,
311
317
  context: props.context || {},
312
318
  apiKey: props.apiKey,
319
+ apiVersion: props.apiVersion,
313
320
  registeredComponents: allRegisteredComponents,
314
321
  } }, useContent ? (React.createElement(React.Fragment, null,
315
322
  React.createElement(react_native_1.ScrollView, { ref: elementRef, onClick: (event) => onClick(event), "builder-content-id": useContent === null || useContent === void 0 ? void 0 : useContent.id, "builder-model": props.model },
@@ -9,6 +9,7 @@ var stdin_default = (0, react_1.createContext)({
9
9
  setState() {
10
10
  },
11
11
  apiKey: null,
12
+ apiVersion: void 0,
12
13
  registeredComponents: {},
13
14
  inheritedStyles: {}
14
15
  });
@@ -20,11 +20,14 @@ var __spreadValues = (a, b) => {
20
20
  const flatten_js_1 = require("../../helpers/flatten.js");
21
21
  const index_js_1 = require("../get-builder-search-params/index.js");
22
22
  const generateContentUrl = (options) => {
23
- const { limit = 30, userAttributes, query, noTraverse = false, model, apiKey, includeRefs = true, locale } = options;
23
+ const { limit = 30, userAttributes, query, noTraverse = false, model, apiKey, includeRefs = true, locale, apiVersion = "v2" } = options;
24
24
  if (!apiKey) {
25
25
  throw new Error("Missing API key");
26
26
  }
27
- const url = new URL(`https://cdn.builder.io/api/v2/content/${model}?apiKey=${apiKey}&limit=${limit}&noTraverse=${noTraverse}&includeRefs=${includeRefs}${locale ? `&locale=${locale}` : ""}`);
27
+ if (!["v2", "v3"].includes(apiVersion)) {
28
+ throw new Error(`Invalid apiVersion: expected 'v2' or 'v3', received '${apiVersion}'`);
29
+ }
30
+ const url = new URL(`https://cdn.builder.io/api/${apiVersion}/content/${model}?apiKey=${apiKey}&limit=${limit}&noTraverse=${noTraverse}&includeRefs=${includeRefs}${locale ? `&locale=${locale}` : ""}`);
28
31
  const queryOptions = __spreadValues(__spreadValues({}, (0, index_js_1.getBuilderSearchParamsFromWindow)()), (0, index_js_1.normalizeSearchParams)(options.options || {}));
29
32
  const flattened = (0, flatten_js_1.flatten)(queryOptions);
30
33
  for (const key in flattened) {
@@ -30,4 +30,46 @@ describe("Generate Content URL", () => {
30
30
  });
31
31
  expect(output).toMatchSnapshot();
32
32
  });
33
+ test("generate content url with apiVersion as v2", () => {
34
+ const output = (0, generate_content_url_1.generateContentUrl)({
35
+ apiKey: testKey,
36
+ model: testModel,
37
+ query: { id: testId },
38
+ options,
39
+ apiVersion: "v2"
40
+ });
41
+ expect(output).toMatchSnapshot();
42
+ });
43
+ test("generate content url with apiVersion as v3", () => {
44
+ const output = (0, generate_content_url_1.generateContentUrl)({
45
+ apiKey: testKey,
46
+ model: testModel,
47
+ query: { id: testId },
48
+ options,
49
+ apiVersion: "v3"
50
+ });
51
+ expect(output).toMatchSnapshot();
52
+ });
53
+ test("throw error when trying to generate content url with apiVersion as v1", () => {
54
+ expect(() => {
55
+ (0, generate_content_url_1.generateContentUrl)({
56
+ apiKey: testKey,
57
+ model: testModel,
58
+ query: { id: testId },
59
+ options,
60
+ apiVersion: "v1"
61
+ });
62
+ }).toThrow(`Invalid apiVersion: expected 'v2' or 'v3', received 'v1'`);
63
+ });
64
+ test("throw error when trying to generate content url with an invalid apiVersion value", () => {
65
+ expect(() => {
66
+ (0, generate_content_url_1.generateContentUrl)({
67
+ apiKey: testKey,
68
+ model: testModel,
69
+ query: { id: testId },
70
+ options,
71
+ apiVersion: "INVALID_API_VERSION"
72
+ });
73
+ }).toThrow(`Invalid apiVersion: expected 'v2' or 'v3', received 'INVALID_API_VERSION'`);
74
+ });
33
75
  });
@@ -0,0 +1 @@
1
+ "use strict";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@builder.io/sdk-react-native",
3
3
  "description": "Builder.io SDK for React Native",
4
- "version": "0.1.10",
4
+ "version": "0.1.12",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",
@@ -1,4 +1,3 @@
1
- import BaseText from "../BaseText";
2
1
  import * as React from "react";
3
2
  import {
4
3
  FlatList,
@@ -10,11 +9,7 @@ import {
10
9
  } from "react-native";
11
10
 
12
11
  function FragmentComponent(props) {
13
- return (
14
- <View>
15
- <BaseText>{props.children}</BaseText>
16
- </View>
17
- );
12
+ return <View>{props.children}</View>;
18
13
  }
19
14
 
20
15
  export default FragmentComponent;
@@ -1,4 +1,3 @@
1
- import BaseText from "../BaseText";
2
1
  import * as React from "react";
3
2
  import {
4
3
  FlatList,
@@ -26,7 +25,7 @@ function SectionComponent(props) {
26
25
  marginRight: "auto",
27
26
  }}
28
27
  >
29
- <BaseText>{props.children}</BaseText>
28
+ {props.children}
30
29
  </View>
31
30
  );
32
31
  }
@@ -14,8 +14,8 @@ import { getContent } from "../../functions/get-content/index.js";
14
14
  import { TARGET } from "../../constants/target";
15
15
 
16
16
  function Symbol(props) {
17
- function className() {
18
- return [
17
+ const [className, setClassName] = useState(() =>
18
+ [
19
19
  ...(TARGET === "vue2" || TARGET === "vue3"
20
20
  ? Object.keys(props.attributes.class)
21
21
  : [props.attributes.class]),
@@ -26,20 +26,12 @@ function Symbol(props) {
26
26
  : undefined,
27
27
  ]
28
28
  .filter(Boolean)
29
- .join(" ");
30
- }
31
-
32
- const [fetchedContent, setFetchedContent] = useState(() => null);
33
-
34
- function contentToUse() {
35
- return props.symbol?.content || fetchedContent;
36
- }
37
-
38
- const builderContext = useContext(BuilderContext);
29
+ .join(" ")
30
+ );
39
31
 
40
- useEffect(() => {
41
- const symbolToUse = props.symbol;
32
+ const [contentToUse, setContentToUse] = useState(() => props.symbol?.content);
42
33
 
34
+ function fetchContent() {
43
35
  /**
44
36
  * If:
45
37
  * - we have a symbol prop
@@ -50,43 +42,59 @@ function Symbol(props) {
50
42
  * then we want to re-fetch the symbol content.
51
43
  */
52
44
  if (
53
- symbolToUse &&
54
- !symbolToUse.content &&
55
- !fetchedContent &&
56
- symbolToUse.model &&
45
+ !contentToUse &&
46
+ props.symbol?.model &&
57
47
  // This is a hack, we should not need to check for this, but it is needed for Svelte.
58
48
  builderContext?.apiKey
59
49
  ) {
60
50
  getContent({
61
- model: symbolToUse.model,
51
+ model: props.symbol.model,
62
52
  apiKey: builderContext.apiKey,
53
+ apiVersion: builderContext.apiVersion,
63
54
  query: {
64
- id: symbolToUse.entry,
55
+ id: props.symbol.entry,
65
56
  },
66
- }).then((response) => {
67
- setFetchedContent(response);
68
- });
57
+ })
58
+ .then((response) => {
59
+ if (response) {
60
+ setContentToUse(response);
61
+ }
62
+ })
63
+ .catch((err) => {
64
+ console.error("[Builder.io]: Could not fetch symbol content: ", err);
65
+ });
69
66
  }
70
- }, [props.symbol, fetchedContent]);
67
+ }
68
+
69
+ const builderContext = useContext(BuilderContext);
70
+
71
+ useEffect(() => {
72
+ fetchContent();
73
+ }, []);
74
+
75
+ useEffect(() => {
76
+ fetchContent();
77
+ }, [props.symbol]);
71
78
 
72
79
  return (
73
80
  <View
74
81
  {...props.attributes}
75
82
  dataSet={{
76
- class: className(),
83
+ class: className,
77
84
  }}
78
85
  >
79
86
  <RenderContent
87
+ apiVersion={builderContext.apiVersion}
80
88
  apiKey={builderContext.apiKey}
81
89
  context={builderContext.context}
82
90
  customComponents={Object.values(builderContext.registeredComponents)}
83
91
  data={{
84
92
  ...props.symbol?.data,
85
93
  ...builderContext.state,
86
- ...props.symbol?.content?.data?.state,
94
+ ...contentToUse?.data?.state,
87
95
  }}
88
96
  model={props.symbol?.model}
89
- content={contentToUse()}
97
+ content={contentToUse}
90
98
  />
91
99
  </View>
92
100
  );
@@ -308,6 +308,7 @@ function RenderContent(props) {
308
308
  getContent({
309
309
  model: props.model,
310
310
  apiKey: props.apiKey,
311
+ apiVersion: props.apiVersion,
311
312
  }).then((content) => {
312
313
  if (content) {
313
314
  mergeNewContent(content);
@@ -321,6 +322,11 @@ function RenderContent(props) {
321
322
  }
322
323
  }, []);
323
324
 
325
+ useEffect(() => {
326
+ if (props.content) {
327
+ mergeNewContent(props.content);
328
+ }
329
+ }, [props.content]);
324
330
  useEffect(() => {
325
331
  evaluateJsCode();
326
332
  }, [useContent?.data?.jsCode, contentState]);
@@ -351,6 +357,7 @@ function RenderContent(props) {
351
357
  setState: setContextState,
352
358
  context: props.context || {},
353
359
  apiKey: props.apiKey,
360
+ apiVersion: props.apiVersion,
354
361
  registeredComponents: allRegisteredComponents,
355
362
  }}
356
363
  >
@@ -6,6 +6,7 @@ var stdin_default = createContext({
6
6
  setState() {
7
7
  },
8
8
  apiKey: null,
9
+ apiVersion: void 0,
9
10
  registeredComponents: {},
10
11
  inheritedStyles: {}
11
12
  });
@@ -28,12 +28,16 @@ const generateContentUrl = (options) => {
28
28
  model,
29
29
  apiKey,
30
30
  includeRefs = true,
31
- locale
31
+ locale,
32
+ apiVersion = "v2"
32
33
  } = options;
33
34
  if (!apiKey) {
34
35
  throw new Error("Missing API key");
35
36
  }
36
- const url = new URL(`https://cdn.builder.io/api/v2/content/${model}?apiKey=${apiKey}&limit=${limit}&noTraverse=${noTraverse}&includeRefs=${includeRefs}${locale ? `&locale=${locale}` : ""}`);
37
+ if (!["v2", "v3"].includes(apiVersion)) {
38
+ throw new Error(`Invalid apiVersion: expected 'v2' or 'v3', received '${apiVersion}'`);
39
+ }
40
+ const url = new URL(`https://cdn.builder.io/api/${apiVersion}/content/${model}?apiKey=${apiKey}&limit=${limit}&noTraverse=${noTraverse}&includeRefs=${includeRefs}${locale ? `&locale=${locale}` : ""}`);
37
41
  const queryOptions = __spreadValues(__spreadValues({}, getBuilderSearchParamsFromWindow()), normalizeSearchParams(options.options || {}));
38
42
  const flattened = flatten(queryOptions);
39
43
  for (const key in flattened) {
@@ -28,4 +28,46 @@ describe("Generate Content URL", () => {
28
28
  });
29
29
  expect(output).toMatchSnapshot();
30
30
  });
31
+ test("generate content url with apiVersion as v2", () => {
32
+ const output = generateContentUrl({
33
+ apiKey: testKey,
34
+ model: testModel,
35
+ query: { id: testId },
36
+ options,
37
+ apiVersion: "v2"
38
+ });
39
+ expect(output).toMatchSnapshot();
40
+ });
41
+ test("generate content url with apiVersion as v3", () => {
42
+ const output = generateContentUrl({
43
+ apiKey: testKey,
44
+ model: testModel,
45
+ query: { id: testId },
46
+ options,
47
+ apiVersion: "v3"
48
+ });
49
+ expect(output).toMatchSnapshot();
50
+ });
51
+ test("throw error when trying to generate content url with apiVersion as v1", () => {
52
+ expect(() => {
53
+ generateContentUrl({
54
+ apiKey: testKey,
55
+ model: testModel,
56
+ query: { id: testId },
57
+ options,
58
+ apiVersion: "v1"
59
+ });
60
+ }).toThrow(`Invalid apiVersion: expected 'v2' or 'v3', received 'v1'`);
61
+ });
62
+ test("throw error when trying to generate content url with an invalid apiVersion value", () => {
63
+ expect(() => {
64
+ generateContentUrl({
65
+ apiKey: testKey,
66
+ model: testModel,
67
+ query: { id: testId },
68
+ options,
69
+ apiVersion: "INVALID_API_VERSION"
70
+ });
71
+ }).toThrow(`Invalid apiVersion: expected 'v2' or 'v3', received 'INVALID_API_VERSION'`);
72
+ });
31
73
  });
File without changes