npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@builder.io/plugin-tools

v0.0.6

Published

Easily connect your custom data sources and ecommerce backends to your Builder.io content!

Downloads

183

Readme

Builder.io Plugin Tools

Easily connect your custom data sources and ecommerce backends to your Builder.io content!

Shopify data example Data source demo

This package's main exports are registerCommercePlugin and registerDataPlugin, which allow you to define what your ecommerce backend or data API needs (e.g., apiToken, password, environment), prompt Builder.io users for those settings, and register multiple field types per resource that allow for easy embedding and custom targeting for each resource (products, collections, personas, etc.) in your store or data source.

For real-world examples, check the @builder.io/ecom-swell-is and @builder.io/plugin-contentful folders.

Usage

Commerce Plugin

import { registerCommercePlugin } from '@builder.io/plugin-tools';

registerCommercePlugin(
  {
    name: 'Swell',
    id: '@builder.io/ecome-swell-is',
    settings: [
      {
        name: 'storeId',
        type: 'string',
        required: true,
        helperText:
          'Get your Store ID from swell store settings https://swell.store/docs/api/?javascript#authentication',
      },
      {
        name: 'secretKey',
        type: 'string',
        required: true,
        helperText:
          'Get your Secret key from swell store settings https://swell.store/docs/api/?javascript#authentication',
      },
    ],
    ctaText: `Connect your swell.is store`,
  },
  settings => {
    // setup basic cache for a better user experience
    const basicCache = new Map();
    const baseUrl = (url: string) =>
      'https://builder.io/api/v1/proxy-api?url=' +
      encodeURIComponent('https://api.swell.store/' + url);
    const headers = {
      Authorization: `Basic ${btoa(`${settings.get('storeId')}:${settings.get('secretKey')}`)}`,
      Accept: 'application/json',
      'Content-Type': 'application/json',
    };

    // always transform product to match Resource interface, ts will notify you if not
    const transformProduct = (product: any) => ({
      id: product.id,
      title: product.name,
      handle: product.slug,
      ...(product.images && {
        image: {
          src: product.images[0]?.file.url,
        },
      }),
    });

    return {
      product: {
        async findById(id: string) {
          const key = `${id}productById`;
          const product =
            basicCache.get(key) ||
            (await fetch(baseUrl(`products/${id}`), { headers })
              .then(res => res.json())
              .then(transformProduct));
          basicCache.set(key, product);
          return product;
        },
        async findByHandle(handle: string) {
          const key = `${handle}productByHandle`;
          const response =
            basicCache.get(key) ||
            (await fetch(baseUrl(`products?where[active]=true&where[slug]=${handle}`), {
              headers,
            }).then(res => res.json()));
          basicCache.set(key, response);
          const product = response.results.map(transformProduct)[0];
          return product;
        },
        async search(search: string) {
          const response = await fetch(baseUrl(`products?where[active]=true&search=${search}`), {
            headers,
          }).then(res => res.json());
          return response.results.map(transformProduct);
        },

        getRequestObject(id: string) {
          return {
            '@type': '@builder.io/core:Request',
            request: {
              url: baseUrl(`products/${id}`),
            },
            options: {
              product: id,
            },
          };
        },
      },
    };
  }
);

Data Plugin

import { registerDataPlugin } from '@builder.io/plugin-tools';

registerDataPlugin(
  {
    name: 'Foobar',
    id: '@builder.io/plugin-foobar',
    settings: [
      {
        name: 'foo',
        friendlyName: 'Lorem Ipsum',
        type: 'string',
        required: true,
        helperText:
          'Lorem Ipsum',
      }
    ],
    ctaText: `Connect FooBar`,
  },
  async settings => {
    const spaceId = settings.get('spaceId')?.trim();
    return {
      async getResourceTypes() {
        return [
          {
          name: 'Resource 1',
          id: `resource-1`,
          canPickEntries: true,
          // inputs are the query parameter definitions for your public API
          inputs: () => {
            return [
              {
                name: 'include',
                friendlyName: 'Retrieve linked assets level',
                advanced: true,
                defaultValue: 2,
                // contentful api restricts include to be between 0 and 10
                min: 0,
                max: 10,
                type: 'number',
              },
              {
                name: 'limit',
                defaultValue: 10,
                // contentful api restricts limit to be between 0 and 100
                min: 0,
                max: 100,
                type: 'number',
              },

              {
                name: 'order',
                friendlyName: 'Order By'
                type: 'string',
                enum: ['a', 'b']
              },
            ];
          },
          toUrl: (options: any) => {
            // by entry
            // https://cdn.contentful.com/spaces/{space_id}/environments/{environment_id}/entries/{entry_id}?access_token={access_token}
            if (options.entry) {
              return `https://public.example.com/spacs/${spaceId}/resource/${options.entry}`
            }
            // by query
            const params = qs.stringify(options,
              { allowDots: true, skipNulls: true }
            );
            return  `https://public.example.com/spaces/${spaceId}/search?${params}`
            );
          },
          }
        ]
      },
      async getEntriesByResourceType(typeId: string, options) {
        const results = await fetch(`https://public.example.com/spaces/${spaceId}?${qs.stringify(options)}`)
      },
    };
  }
);

API

registerCommercePlugin(config, settingsCallback)

Registers a commerce plugin with Builder.io.

export declare const registerCommercePlugin: (
  config: CommercePluginConfig,
  apiOperationsFromSettings: (
    settings: any
  ) => CommerceAPIOperations | Promise<CommerceAPIOperations>
) => Promise<void>;
  • config: An object containing the plugin's name, ID, settings, and CTA text.
  • settingsCallback: A function that receives the user-provided settings and returns an object with methods for querying and transforming product data.

registerDataPlugin(config, settingsCallback)

Registers a data plugin with Builder.io.

export declare const registerDataPlugin: (
  config: DataPluginOptions,
  apiOperationsFromSettings: (settings: any) => APIOperations | Promise<APIOperations>
) => Promise<void>;
  • config: An object containing the plugin's name, ID, settings, and CTA text.
  • settingsCallback: An asynchronous function that receives the user-provided settings and returns an object with methods for querying and transforming data from the external API.

Both registerCommercePlugin and registerDataPlugin allow you to define the required settings for connecting to your backend, prompt users for these settings, and register various resources and their corresponding field types for easy integration with Builder.io content.