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

solidus-client

v1.4.1

Published

Render your [Solidus](https://github.com/solidusjs/solidus) views in the browser! Just like Solidus, this library will:

Downloads

12

Readme

SolidusClient

Render your Solidus views in the browser! Just like Solidus, this library will:

  1. Fetch data from static and dynamic resources, and make it available in the context.
  2. Add and transform the context with custom preprocessors.
  3. Render Handlebars templates, with partials and helpers. The handlebars-helper helpers are automatically made available.

Refer to the Solidus documentation to know more about those concepts.

Usage

Rendering a template starts with the render function call, and ends with the end callback. The various options can be passed directly to the render method as a config object, or by chaining method calls on the returned object.

  • resources - Object of named urls or options. The urls can have dynamic segments, which will be replaced by the matching params. If an error occurs while fetching a resource, the template is not rendered, except if the resource's optional option is true.
var resources = {
  blogs: 'http://my-site.com/blogs?page={page}',
  news: {
    url: 'http://news-site.com/news',
    proxy: true
  }
}
};

Available options:

  • url - Resource URL.
  • query - Object of query values to add to the resource URL.
  • query_options - Options passed to qs.stringify, the function used to serialize the query object. An additional option is available, objectFormat: 'json', which will encode query values of type object into JSON strings.
  • headers - Object of HTTP headers to add to the resource request.
  • auth - Object with user and pass values for HTTP Basic authentication.
  • with_credentials - Whether to send cookies from the origin. Defaults to false.
  • jsonp - Whether to use jsonp instead of ajax for the request. Defaults to false.
  • proxy - Whether to fetch the resource through Solidus, instead of directly hitting the resource URL. If true, all other options are ignored. Defaults to false.
  • solidus_api_route - Route to the Solidus resource.json endpoint if proxy is true. Defaults to /api/.
  • timeout - Maximum time to wait for the resource, in milliseconds. Defaults to 20,000.
  • optional - If true and the resource cannot be fetched, the template is rendered anyway. Defaults to false. Error conditions:
    • Invalid URL.
    • A required param is missing.
    • HTTP error, like a network error.
    • Timeout is exceeded.
    • Returned status code is not in the 2xx range.
    • Returned data is not valid JSON.
    • Returned data has a root property named status with value error.
  • params - Object of named values. The values will be interpolated into the dynamic resource urls.
var params = {
  page: 123
};
  • required_params - Array of required param names. If any param is missing, an error is returned.
  • preprocessor - Function that modifies the context. The preprocessor is run after the resources are fetched, but before the template is rendered. If an error occurs while running the preprocessor, the template is not rendered.
// Sync
var preprocessor = function(context) {
  context.blogs_count = context.resources.blogs.length;
  return context;
};
// Async
var preprocessor = function(context, callback) {
  context.blogs_count = context.resources.blogs.length;
  if (context.resources.blog.some_condition) {
    solidus_client.getResource('http://www.my-site.com/something', null, function(err, data) {
      if (err) throw err;
      context.more_data = data;
      callback(context);
    });
  } else {
    callback(context);
  }
};

Return values:

  • object - Modified context.

  • string - URL to immediately redirect the browser to, instead of rendering the template.

  • array - Item 0 is ignored, item 1 is URL to immediately redirect the browser to, instead of rendering the template.

  • Any other value results in a preprocessor error.

  • template - Compiled Handlebars template.

var template = Handlebars.compile('{{blogs_count}} posts: <ul>{{#each resources.blogs}}<li>{{> blog}}</li>{{/each}}</ul>');
  • template_options - Object of options to use when rendering the template: data, helpers and partials.
var template_options = {
  helpers: {
    uppercase: function(string) {
      return string.toUpperCase();
    }
  },
  partials: {
    blog: Handlebars.compile('{{uppercase name}}');
  }
};

Config Object Example

var view = {
  resources: resources,
  params: params,
  preprocessor: preprocessor,
  template: template,
  template_options: template_options
};

var solidus_client = new SolidusClient();
solidus_client.render(view)
  .end(function(err, html) {
    console.log(html);
  });

Methods Chain Example

var solidus_client = new SolidusClient();
solidus_client.render(template)
  .params(params)
  .template_options(template_options)
  .get(resources)
  .then(preprocessor)
  .end(function(err, html) {
    console.log(html);
  });

Combined Example

var view = {
  resources: resources,
  preprocessor: preprocessor,
  template: template,
  template_options: template_options
};

var solidus_client = new SolidusClient();
solidus_client.render(view)
  .params(params)
  .end(function(err, html) {
    console.log(html);
  });

Auth

Just like in Solidus, resource security settings are configured globally. The resources_options property of the SolidusClient instance is scanned whenever a resource is fetched, to find matching options. See the Resources documentation above for the available options. Example:

var resources_options = {
  "http://proxy.storyteller.io/*": {
    headers: {
      "Api-Key": "0000aaaa-aa00-00aa-a00a-aaaa000000"
    }
  },
  "http://services.sparkart.net/*": {
    query: {
      key: "1111bbbb-bb11-11bb-b11b-bbbb111111"
    }
  }
};

// Two ways to assign the resources options
var solidus_client = new SolidusClient({resources_options: resources_options});
solidus_client.resources_options = resources_options;

Building

$ npm run build

Testing

Node and automated browser tests

$ npm run test

Node test

$ npm run node-test

Automated browser test

$ npm run browser-test

Manual browser test

$ npm run test-server

Then access http://localhost:8081/test/browser/test.html in your browser