@builder.io/react 8.1.2 → 8.2.1

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.
@@ -309,6 +309,19 @@ export interface BuilderComponentState {
309
309
  breakpoints?: Breakpoints;
310
310
  }
311
311
 
312
+ interface BuilderRequest {
313
+ '@type': '@builder.io/core:Request';
314
+ request: {
315
+ url: string;
316
+ query?: { [key: string]: string };
317
+ headers?: { [key: string]: string };
318
+ method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
319
+ body?: any;
320
+ };
321
+ options?: { [key: string]: any };
322
+ bindings?: { [key: string]: string };
323
+ }
324
+
312
325
  function searchToObject(location: Location | Url) {
313
326
  const pairs = (location.search || '').substring(1).split('&');
314
327
  const obj: { [key: string]: string } = {};
@@ -1232,22 +1245,37 @@ export class BuilderComponent extends React.Component<
1232
1245
  );
1233
1246
  }
1234
1247
 
1235
- async handleRequest(propertyName: string, url: string) {
1248
+ async handleRequest(
1249
+ propertyName: string,
1250
+ httpRequest: {
1251
+ url: string;
1252
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
1253
+ body?: any;
1254
+ headers?: Record<string, string>;
1255
+ }
1256
+ ) {
1257
+ const { url, method, body, headers } = httpRequest;
1258
+
1259
+ const fetchCacheKey = JSON.stringify({ url, method, body, headers });
1236
1260
  // TODO: Builder.isEditing = just checks if iframe and parent page is this.builder.io or localhost:1234
1237
- if (Builder.isIframe && fetchCache[url]) {
1261
+ if (Builder.isIframe && fetchCache[fetchCacheKey]) {
1238
1262
  this.updateState(ctx => {
1239
- ctx[propertyName] = fetchCache[url];
1263
+ ctx[propertyName] = fetchCache[fetchCacheKey];
1240
1264
  });
1241
- return fetchCache[url];
1265
+ return fetchCache[fetchCacheKey];
1242
1266
  }
1243
1267
  const request = async () => {
1244
1268
  const requestStart = Date.now();
1245
1269
  if (!Builder.isBrowser) {
1246
- console.time('Fetch ' + url);
1270
+ console.time('Fetch ' + fetchCacheKey);
1247
1271
  }
1248
1272
  let json: any;
1249
1273
  try {
1250
- const result = await fetch(url);
1274
+ const result = await fetch(url, {
1275
+ method,
1276
+ headers,
1277
+ body,
1278
+ });
1251
1279
  json = await result.json();
1252
1280
  } catch (err) {
1253
1281
  const error = toError(err);
@@ -1255,21 +1283,21 @@ export class BuilderComponent extends React.Component<
1255
1283
  this._errors.push(error);
1256
1284
  }
1257
1285
  if (this._logs) {
1258
- this._logs.push(`Fetch to ${url} errored in ${Date.now() - requestStart}ms`);
1286
+ this._logs.push(`Fetch to ${fetchCacheKey} errored in ${Date.now() - requestStart}ms`);
1259
1287
  }
1260
1288
  return;
1261
1289
  } finally {
1262
1290
  if (!Builder.isBrowser) {
1263
- console.timeEnd('Fetch ' + url);
1291
+ console.timeEnd('Fetch ' + fetchCacheKey);
1264
1292
  if (this._logs) {
1265
- this._logs.push(`Fetched ${url} in ${Date.now() - requestStart}ms`);
1293
+ this._logs.push(`Fetched ${fetchCacheKey} in ${Date.now() - requestStart}ms`);
1266
1294
  }
1267
1295
  }
1268
1296
  }
1269
1297
 
1270
1298
  if (json) {
1271
1299
  if (Builder.isIframe) {
1272
- fetchCache[url] = json;
1300
+ fetchCache[fetchCacheKey] = json;
1273
1301
  }
1274
1302
  // TODO: debounce next tick all of these when there are a bunch
1275
1303
  this.updateState(ctx => {
@@ -1467,18 +1495,34 @@ export class BuilderComponent extends React.Component<
1467
1495
  if (!skip) {
1468
1496
  // TODO: another structure for this
1469
1497
  for (const key in data.httpRequests) {
1470
- const url: string | undefined = data.httpRequests[key];
1471
- if (url && (!this.data[key] || Builder.isEditing)) {
1498
+ const httpRequest: BuilderRequest | string | undefined = data.httpRequests[key];
1499
+ if (httpRequest && (!this.data[key] || Builder.isEditing)) {
1500
+ const isCoreRequest =
1501
+ typeof httpRequest === 'object' &&
1502
+ httpRequest['@type'] === '@builder.io/core:Request';
1472
1503
  if (Builder.isBrowser) {
1473
- const finalUrl = this.evalExpression(url);
1504
+ const finalUrl = isCoreRequest
1505
+ ? this.evalExpression(httpRequest.request.url)
1506
+ : this.evalExpression(httpRequest as string);
1507
+
1474
1508
  if (Builder.isEditing && this.lastHttpRequests[key] === finalUrl) {
1475
1509
  continue;
1476
1510
  }
1477
1511
  this.lastHttpRequests[key] = finalUrl;
1478
- const builderModelRe = /builder\.io\/api\/v2\/([^\/\?]+)/i;
1479
- const builderModelMatch = url.match(builderModelRe);
1480
- const model = builderModelMatch && builderModelMatch[1];
1481
- this.handleRequest(key, finalUrl);
1512
+
1513
+ if (isCoreRequest) {
1514
+ this.handleRequest(key, {
1515
+ url: finalUrl,
1516
+ method: httpRequest.request.method,
1517
+ body: httpRequest.request.body,
1518
+ headers: httpRequest.request.headers,
1519
+ });
1520
+ } else {
1521
+ this.handleRequest(key, {
1522
+ url: finalUrl,
1523
+ method: 'GET',
1524
+ });
1525
+ }
1482
1526
  const currentSubscription = this.httpSubscriptionPerKey[key];
1483
1527
  if (currentSubscription) {
1484
1528
  currentSubscription.unsubscribe();
@@ -1487,15 +1531,41 @@ export class BuilderComponent extends React.Component<
1487
1531
  // TODO: fix this
1488
1532
  const newSubscription = (this.httpSubscriptionPerKey[key] =
1489
1533
  this.onStateChange.subscribe(() => {
1490
- const newUrl = this.evalExpression(url);
1534
+ const newUrl = isCoreRequest
1535
+ ? this.evalExpression(httpRequest.request.url)
1536
+ : this.evalExpression(httpRequest as string);
1491
1537
  if (newUrl !== finalUrl) {
1492
- this.handleRequest(key, newUrl);
1538
+ if (isCoreRequest) {
1539
+ this.handleRequest(key, {
1540
+ url: newUrl,
1541
+ method: httpRequest.request.method,
1542
+ body: httpRequest.request.body,
1543
+ headers: httpRequest.request.headers,
1544
+ });
1545
+ } else {
1546
+ this.handleRequest(key, {
1547
+ url: newUrl,
1548
+ method: 'GET',
1549
+ });
1550
+ }
1493
1551
  this.lastHttpRequests[key] = newUrl;
1494
1552
  }
1495
1553
  }));
1496
1554
  this.subscriptions.add(newSubscription);
1497
1555
  } else {
1498
- this.handleRequest(key, this.evalExpression(url));
1556
+ if (isCoreRequest) {
1557
+ this.handleRequest(key, {
1558
+ url: this.evalExpression(httpRequest.request.url),
1559
+ method: httpRequest.request.method,
1560
+ body: httpRequest.request.body,
1561
+ headers: httpRequest.request.headers,
1562
+ });
1563
+ } else {
1564
+ this.handleRequest(key, {
1565
+ url: this.evalExpression(httpRequest as string),
1566
+ method: 'GET',
1567
+ });
1568
+ }
1499
1569
  }
1500
1570
  }
1501
1571
  }