@builder.io/plugin-commercelayer 0.1.1 → 0.1.3-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/README.md CHANGED
@@ -13,9 +13,10 @@ Go to [builder.io/account/organization](https://builder.io/account/organization)
13
13
 
14
14
  The plugin provides new field types for your Builder.io models and data connection capabilities:
15
15
 
16
- ### Product Fields
16
+ ### Product & Bundle Fields
17
17
 
18
- - `Commerce Layer Product` - Search and select products from your Commerce Layer catalog
18
+ - `Commerce Layer Product` - Search and select products (SKUs) from your Commerce Layer catalog
19
+ - `Commerce Layer Bundle` - Search and select bundles from your Commerce Layer catalog
19
20
  - `Commerce Layer Product Preview` - Preview product templates with live data
20
21
 
21
22
  ### Connect Data
@@ -23,7 +24,7 @@ The plugin provides new field types for your Builder.io models and data connecti
23
24
  The plugin also provides a "Connect Data" feature that allows you to fetch live product data directly from your Commerce Layer API:
24
25
 
25
26
  - **Real-time data access** - Connect to your Commerce Layer products as a data source
26
- - **Flexible querying** - Search products by name or SKU code with configurable result limits
27
+ - **Flexible querying** - Search products or bundles by name or SKU code with configurable result limits
27
28
  - **Full product data** - Access all product attributes including SKU codes, prices, images, and metadata
28
29
  - **Dynamic content** - Use live product data in your Builder.io content and templates
29
30
 
@@ -146881,7 +146881,7 @@ System.register(['@builder.io/sdk', '@emotion/core', '@material-ui/core', 'react
146881
146881
  });
146882
146882
 
146883
146883
  var name = "@builder.io/plugin-commercelayer";
146884
- var version = "0.1.0";
146884
+ var version = "0.1.2";
146885
146885
  var description = "Commerce Layer plugin for Builder.io";
146886
146886
  var keywords = [
146887
146887
  ];
@@ -147020,38 +147020,42 @@ System.register(['@builder.io/sdk', '@emotion/core', '@material-ui/core', 'react
147020
147020
  }
147021
147021
  throw new Error("Invalid token: no organization found");
147022
147022
  };
147023
- const getProduct = async (id, accessToken, baseEndpoint) => {
147024
- const url = `${baseEndpoint}/api/skus/${id}`;
147023
+ const RESOURCE_ENDPOINTS = {
147024
+ product: "skus",
147025
+ bundle: "bundles"
147026
+ };
147027
+ const getResource = async (type, id, accessToken, baseEndpoint) => {
147028
+ const url = `${baseEndpoint}/api/${RESOURCE_ENDPOINTS[type]}/${id}`;
147025
147029
  const data = await makeApiRequest(url, accessToken);
147026
147030
  return data.data;
147027
147031
  };
147028
- const searchProducts = async (search, accessToken, baseEndpoint) => {
147032
+ const searchResources = async (type, search, accessToken, baseEndpoint) => {
147029
147033
  const params = new URLSearchParams({
147030
147034
  "filter[q][name_or_code_cont]": search,
147031
147035
  "page[size]": "25"
147032
147036
  });
147033
- const url = `${baseEndpoint}/api/skus?${params}`;
147037
+ const url = `${baseEndpoint}/api/${RESOURCE_ENDPOINTS[type]}?${params}`;
147034
147038
  const data = await makeApiRequest(url, accessToken);
147035
147039
  return data.data || [];
147036
147040
  };
147037
- const transformProduct = (product) => ({
147038
- id: product.id,
147039
- type: "product",
147040
- title: product.attributes.name,
147041
- handle: product.attributes.code,
147042
- price: product.attributes.price,
147043
- image: {
147044
- src: product.attributes.image_url
147045
- }
147046
- });
147047
- const getProductByHandle = async (handle, accessToken, baseEndpoint) => {
147041
+ const transformResource = (resource, type) => ({
147042
+ id: resource.id,
147043
+ type,
147044
+ title: resource.attributes.name,
147045
+ handle: resource.attributes.code,
147046
+ price: resource.attributes.price,
147047
+ image: resource.attributes.image_url ? {
147048
+ src: resource.attributes.image_url
147049
+ } : void 0
147050
+ });
147051
+ const getResourceByHandle = async (type, handle, accessToken, baseEndpoint) => {
147048
147052
  const params = new URLSearchParams({
147049
147053
  "filter[q][code_eq]": handle
147050
147054
  });
147051
- const url = `${baseEndpoint}/api/skus?${params}`;
147055
+ const url = `${baseEndpoint}/api/${RESOURCE_ENDPOINTS[type]}?${params}`;
147052
147056
  const data = await makeApiRequest(url, accessToken);
147053
147057
  if (!data.data?.[0]) {
147054
- throw new Error(`Product not found with handle: ${handle}`);
147058
+ throw new Error(`${type} not found with handle: ${handle}`);
147055
147059
  }
147056
147060
  return data.data[0];
147057
147061
  };
@@ -147065,15 +147069,16 @@ System.register(['@builder.io/sdk', '@emotion/core', '@material-ui/core', 'react
147065
147069
  token
147066
147070
  }) => {
147067
147071
  let url;
147072
+ const endpoint = RESOURCE_ENDPOINTS[resource];
147068
147073
  if (resourceId) {
147069
- url = `${baseEndpoint}/api/skus/${resourceId}`;
147074
+ url = `${baseEndpoint}/api/${endpoint}/${resourceId}`;
147070
147075
  } else {
147071
147076
  const params = new URLSearchParams();
147072
147077
  if (query) {
147073
147078
  params.set("filter[q][name_or_code_cont]", query);
147074
147079
  }
147075
147080
  params.set("page[size]", (limit || 25).toString());
147076
- url = `${baseEndpoint}/api/skus?${params}`;
147081
+ url = `${baseEndpoint}/api/${endpoint}?${params}`;
147077
147082
  }
147078
147083
  return {
147079
147084
  "@type": "@builder.io/core:Request",
@@ -147098,6 +147103,11 @@ System.register(['@builder.io/sdk', '@emotion/core', '@material-ui/core', 'react
147098
147103
  name: "Product",
147099
147104
  id: "product",
147100
147105
  description: "All of your Commerce Layer products (SKUs)."
147106
+ },
147107
+ {
147108
+ name: "Bundle",
147109
+ id: "bundle",
147110
+ description: "All of your Commerce Layer bundles."
147101
147111
  }
147102
147112
  ];
147103
147113
  const getDataConfig = (service, baseEndpoint, token) => {
@@ -147187,41 +147197,46 @@ System.register(['@builder.io/sdk', '@emotion/core', '@material-ui/core', 'react
147187
147197
  const scope = settings.get("scope");
147188
147198
  const auth = await authenticateClient({ clientId, clientSecret, scope });
147189
147199
  const { baseEndpoint } = await getOrganizationInfo(auth.accessToken);
147190
- const service = {
147191
- product: {
147192
- async findById(id) {
147193
- const token = await getValidToken(clientId, clientSecret, scope);
147194
- const product = await getProduct(id, token, baseEndpoint);
147195
- return transformProduct(product);
147196
- },
147197
- async findByHandle(handle) {
147198
- const token = await getValidToken(clientId, clientSecret, scope);
147199
- const product = await getProductByHandle(handle, token, baseEndpoint);
147200
- return transformProduct(product);
147201
- },
147202
- async search(search) {
147203
- const token = await getValidToken(clientId, clientSecret, scope);
147204
- const products = await searchProducts(search, token, baseEndpoint);
147205
- return products.map(transformProduct);
147206
- },
147207
- getRequestObject(id) {
147208
- const token = cachedAuth?.token || auth.accessToken;
147209
- return {
147210
- "@type": "@builder.io/core:Request",
147211
- request: {
147212
- url: `${baseEndpoint}/api/skus/${id}`,
147213
- method: "GET",
147214
- headers: {
147215
- "Authorization": `Bearer ${token}`
147216
- }
147217
- },
147218
- options: {
147219
- product: id,
147220
- pluginId: pkg.name
147200
+ const createResourceService = (type) => ({
147201
+ async findById(id) {
147202
+ const token = await getValidToken(clientId, clientSecret, scope);
147203
+ const resource = await getResource(type, id, token, baseEndpoint);
147204
+ return transformResource(resource, type);
147205
+ },
147206
+ async findByHandle(handle) {
147207
+ const token = await getValidToken(clientId, clientSecret, scope);
147208
+ const resource = await getResourceByHandle(type, handle, token, baseEndpoint);
147209
+ return transformResource(resource, type);
147210
+ },
147211
+ async search(search) {
147212
+ const token = await getValidToken(clientId, clientSecret, scope);
147213
+ const resources = await searchResources(type, search, token, baseEndpoint);
147214
+ return resources.map((res) => transformResource(res, type));
147215
+ },
147216
+ getRequestObject(id, resource) {
147217
+ const token = cachedAuth?.token || auth.accessToken;
147218
+ return {
147219
+ "@type": "@builder.io/core:Request",
147220
+ request: {
147221
+ url: `${baseEndpoint}/api/${RESOURCE_ENDPOINTS[type]}/${id}`,
147222
+ method: "GET",
147223
+ headers: {
147224
+ "Authorization": `Bearer ${token}`
147221
147225
  }
147222
- };
147223
- }
147226
+ },
147227
+ options: {
147228
+ [type]: id,
147229
+ resourceType: type,
147230
+ resourceId: id,
147231
+ ...resource?.handle && { code: resource.handle },
147232
+ pluginId: pkg.name
147233
+ }
147234
+ };
147224
147235
  }
147236
+ });
147237
+ const service = {
147238
+ product: createResourceService("product"),
147239
+ bundle: createResourceService("bundle")
147225
147240
  };
147226
147241
  appState.registerDataPlugin(getDataConfig(service, baseEndpoint, auth.accessToken));
147227
147242
  return service;