@builder.io/sdk-react-native 0.1.15 → 0.2.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.
@@ -160,6 +160,7 @@ function RenderBlock(props) {
160
160
  };
161
161
  return {
162
162
  apiKey: props.context.apiKey,
163
+ apiVersion: props.context.apiVersion,
163
164
  state: props.context.state,
164
165
  content: props.context.content,
165
166
  context: props.context.context,
@@ -42,6 +42,7 @@ function RenderComponent(props) {
42
42
  apiKey: props.context.apiKey,
43
43
  registeredComponents: props.context.registeredComponents,
44
44
  inheritedStyles: props.context.inheritedStyles,
45
+ apiVersion: props.context.apiVersion,
45
46
  } }, props.componentRef ? (React.createElement(React.Fragment, null,
46
47
  React.createElement(ComponentRefRef, { ...props.componentOptions }, (_a = props.blockChildren) === null || _a === void 0 ? void 0 :
47
48
  _a.map((child) => (React.createElement(render_block_1.default, { key: "render-block-" + child.id, block: child, context: props.context }))), (_b = props.blockChildren) === null || _b === void 0 ? void 0 :
@@ -189,7 +189,7 @@ function RenderContent(props) {
189
189
  setContextState(newState);
190
190
  })
191
191
  .catch((err) => {
192
- console.log("error fetching dynamic data", url, err);
192
+ console.error("error fetching dynamic data", url, err);
193
193
  });
194
194
  }
195
195
  function runHttpRequests() {
@@ -19,8 +19,9 @@ var __spreadValues = (a, b) => {
19
19
  };
20
20
  const flatten_js_1 = require("../../helpers/flatten.js");
21
21
  const index_js_1 = require("../get-builder-search-params/index.js");
22
+ const api_version_1 = require("../../types/api-version");
22
23
  const generateContentUrl = (options) => {
23
- const { limit = 30, userAttributes, query, noTraverse = false, model, apiKey, includeRefs = true, locale, apiVersion = "v2" } = options;
24
+ const { limit = 30, userAttributes, query, noTraverse = false, model, apiKey, includeRefs = true, locale, apiVersion = api_version_1.DEFAULT_API_VERSION } = options;
24
25
  if (!apiKey) {
25
26
  throw new Error("Missing API key");
26
27
  }
@@ -30,6 +30,15 @@ describe("Generate Content URL", () => {
30
30
  });
31
31
  expect(output).toMatchSnapshot();
32
32
  });
33
+ test("generate content url with apiVersion as default", () => {
34
+ const output = (0, generate_content_url_1.generateContentUrl)({
35
+ apiKey: testKey,
36
+ model: testModel,
37
+ query: { id: testId },
38
+ options
39
+ });
40
+ expect(output).toMatchSnapshot();
41
+ });
33
42
  test("generate content url with apiVersion as v2", () => {
34
43
  const output = (0, generate_content_url_1.generateContentUrl)({
35
44
  apiKey: testKey,
@@ -47,7 +47,11 @@ const ab_testing_js_1 = require("./ab-testing.js");
47
47
  const generate_content_url_js_1 = require("./generate-content-url.js");
48
48
  function getContent(options) {
49
49
  return __async(this, null, function* () {
50
- return (yield getAllContent(__spreadProps(__spreadValues({}, options), { limit: 1 }))).results[0] || null;
50
+ const allContent = yield getAllContent(__spreadProps(__spreadValues({}, options), { limit: 1 }));
51
+ if ("results" in allContent) {
52
+ return (allContent == null ? void 0 : allContent.results[0]) || null;
53
+ }
54
+ return null;
51
55
  });
52
56
  }
53
57
  exports.getContent = getContent;
@@ -56,12 +60,21 @@ function getAllContent(options) {
56
60
  const url = (0, generate_content_url_js_1.generateContentUrl)(options);
57
61
  const res = yield (0, get_fetch_js_1.fetch)(url.href);
58
62
  const content = yield res.json();
63
+ if ("status" in content && !("results" in content)) {
64
+ console.error("[Builder.io]: Error fetching data. ", content, options);
65
+ return content;
66
+ }
59
67
  const canTrack = options.canTrack !== false;
60
- if (canTrack && Array.isArray(content.results)) {
61
- for (const item of content.results) {
62
- yield (0, ab_testing_js_1.handleABTesting)({ item, canTrack });
68
+ try {
69
+ if (canTrack && Array.isArray(content.results)) {
70
+ for (const item of content.results) {
71
+ yield (0, ab_testing_js_1.handleABTesting)({ item, canTrack });
72
+ }
63
73
  }
64
74
  }
75
+ catch (e) {
76
+ console.error("[Builder.io]: Could not setup A/B testing. ", e);
77
+ }
65
78
  return content;
66
79
  });
67
80
  }
@@ -1 +1,5 @@
1
1
  "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_API_VERSION = void 0;
4
+ const DEFAULT_API_VERSION = "v3";
5
+ exports.DEFAULT_API_VERSION = DEFAULT_API_VERSION;
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.15",
4
+ "version": "0.2.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",
@@ -160,6 +160,7 @@ function RenderBlock(props) {
160
160
  };
161
161
  return {
162
162
  apiKey: props.context.apiKey,
163
+ apiVersion: props.context.apiVersion,
163
164
  state: props.context.state,
164
165
  content: props.context.content,
165
166
  context: props.context.context,
@@ -24,6 +24,7 @@ function RenderComponent(props) {
24
24
  apiKey: props.context.apiKey,
25
25
  registeredComponents: props.context.registeredComponents,
26
26
  inheritedStyles: props.context.inheritedStyles,
27
+ apiVersion: props.context.apiVersion,
27
28
  }}
28
29
  >
29
30
  {props.componentRef ? (
@@ -207,7 +207,7 @@ function RenderContent(props) {
207
207
  setContextState(newState);
208
208
  })
209
209
  .catch((err) => {
210
- console.log("error fetching dynamic data", url, err);
210
+ console.error("error fetching dynamic data", url, err);
211
211
  });
212
212
  }
213
213
 
@@ -19,6 +19,7 @@ import {
19
19
  getBuilderSearchParamsFromWindow,
20
20
  normalizeSearchParams
21
21
  } from "../get-builder-search-params/index.js";
22
+ import { DEFAULT_API_VERSION } from "../../types/api-version";
22
23
  const generateContentUrl = (options) => {
23
24
  const {
24
25
  limit = 30,
@@ -29,7 +30,7 @@ const generateContentUrl = (options) => {
29
30
  apiKey,
30
31
  includeRefs = true,
31
32
  locale,
32
- apiVersion = "v2"
33
+ apiVersion = DEFAULT_API_VERSION
33
34
  } = options;
34
35
  if (!apiKey) {
35
36
  throw new Error("Missing API key");
@@ -28,6 +28,15 @@ describe("Generate Content URL", () => {
28
28
  });
29
29
  expect(output).toMatchSnapshot();
30
30
  });
31
+ test("generate content url with apiVersion as default", () => {
32
+ const output = generateContentUrl({
33
+ apiKey: testKey,
34
+ model: testModel,
35
+ query: { id: testId },
36
+ options
37
+ });
38
+ expect(output).toMatchSnapshot();
39
+ });
31
40
  test("generate content url with apiVersion as v2", () => {
32
41
  const output = generateContentUrl({
33
42
  apiKey: testKey,
@@ -42,7 +42,11 @@ import { handleABTesting } from "./ab-testing.js";
42
42
  import { generateContentUrl } from "./generate-content-url.js";
43
43
  function getContent(options) {
44
44
  return __async(this, null, function* () {
45
- return (yield getAllContent(__spreadProps(__spreadValues({}, options), { limit: 1 }))).results[0] || null;
45
+ const allContent = yield getAllContent(__spreadProps(__spreadValues({}, options), { limit: 1 }));
46
+ if ("results" in allContent) {
47
+ return (allContent == null ? void 0 : allContent.results[0]) || null;
48
+ }
49
+ return null;
46
50
  });
47
51
  }
48
52
  function getAllContent(options) {
@@ -50,11 +54,19 @@ function getAllContent(options) {
50
54
  const url = generateContentUrl(options);
51
55
  const res = yield fetch(url.href);
52
56
  const content = yield res.json();
57
+ if ("status" in content && !("results" in content)) {
58
+ console.error("[Builder.io]: Error fetching data. ", content, options);
59
+ return content;
60
+ }
53
61
  const canTrack = options.canTrack !== false;
54
- if (canTrack && Array.isArray(content.results)) {
55
- for (const item of content.results) {
56
- yield handleABTesting({ item, canTrack });
62
+ try {
63
+ if (canTrack && Array.isArray(content.results)) {
64
+ for (const item of content.results) {
65
+ yield handleABTesting({ item, canTrack });
66
+ }
57
67
  }
68
+ } catch (e) {
69
+ console.error("[Builder.io]: Could not setup A/B testing. ", e);
58
70
  }
59
71
  return content;
60
72
  });
@@ -0,0 +1,4 @@
1
+ const DEFAULT_API_VERSION = "v3";
2
+ export {
3
+ DEFAULT_API_VERSION
4
+ };