4.0.1 • Published 3 years ago

oc-client v4.0.1

Weekly downloads
213
License
MIT
Repository
github
Last release
3 years ago

oc-client

Node.js client for OpenComponents

NPM

Node.js version: 6 required

Build status: Linux: Build Status | Windows:Build status

Disclaimer: This project is still under heavy development and the API is likely to change at any time. In case you would find any issues, check the troubleshooting page.

API

new Client(options)

It will create an instance of the client. Options:

Parametertypemandatorydescription
cacheobjectnoCache options. If null or empty will use default settings (never flush the cache)
cache.flushIntervalnumber (seconds)noThe interval for flushing the cache
componentsobjectyesThe components to consume with versions
components[name]stringyesThe component version
forwardAcceptLanguageToClientbooleannoDefault false. When true, when doing client-side requests (normal or failover) appends a custom parameter to the browser's component hrefs so that the framework will ignore the browser's Accept-Language in favour of the query-string value
registriesobjectyesThe registries' endpoints
registries.serverRenderingstringnoThe baseUrl for server-side rendering requests
registries.clientRenderingstringnoThe baseUrl for client-side rendering requests
templatesarraynoThe templates available to the client, will extend the default: require('oc-template-handlebars'), require('oc-template-jade')

Example:

var Client = require('oc-client');

var client = new Client({
  registries: { serverRendering: 'https://myregistry.com/'},
  components: {
    hello: '1.2.3',
    world: '~2.2.5',
    bla: ''
  }
});

Client#init(options, callback)

It will warmup the components that have been declared during the instantiation. Options:

Parametertypemandatorydescription
headersobjectnoAn object containing all the headers that must be forwarded to the components' requests
timeoutnumber (seconds)noDefault 5. Maximum amount of time to wait during requests
renderComponentsfunctionnoA function to renderComponents on warmup. Defaults to client own implementation

Example:

var Client = require('oc-client');

var client = new Client({
  registries: { serverRendering: 'https://myregistry.com/'},
  components: {
    hello: '1.2.3'
  }
});

client.init({
  headers: { 'accept-language': 'en-US'}
}, function(error, responses){
  console.log(error);
  // => something like null or Error making request to registry

  console.log(responses);
  // => something like { hello: '<b>hello</b>'}
});

Client#getComponentsInfo(components, callback)

It will get the components' resolved versions for given requested versions. Useful for polling mechanism and caches management.

Example:

...
client.getComponentsInfo([{
  name: 'header',
  version: '1.X.X'
}], function(error, infos){
  console.log(infos);
  /* => [{
    componentName: 'header',
    requestedVersion: '1.X.X',
    apiResponse: {
      name: 'header',
      requestVersion: '1.X.X',
      type: 'oc-component',
      version: '1.2.4'
    }
  }] */
});

Client#renderComponent(componentName , options, callback)

It will resolve a component href, will make a request to the registry, and will render the component. The callback will contain an error (if present), rendered html, and details (which includes headers).

Options:

Parametertypemandatorydescription
containerbooleannoDefault false, when false, renders a component without its container
disableFailoverRenderingbooleannoDisables the automatic failover rendering in case the registry times-out (in case configuration.registries.clientRendering contains a valid value.) Default false
forwardAcceptLanguageToClientbooleannoWhen not specified in config, defaults to false. When true, when doing client-side requests (normal or failover) appends a custom parameter to the browser's component hrefs so that the framework will ignore the browser's Accept-Language in favour of the query-string value
headersobjectnoAn object containing all the headers that must be forwarded to the component
parametersobjectnoAn object containing the parameters for component's request
registriesobjectnoThe registries' endpoints (overrides the parameters defined during instantiation)
registries.serverRenderingstringnoThe baseUrl for server-side rendering requests (overrides the parameter defined during instantiation)
registries.clientRenderingstringnoThe baseUrl for client-side rendering requests (overrides the parameter defined during instantiation)
renderstringnoDefault server. When server, it will return html. When client will produce the html to put in the page for post-poning the rendering to the browser
timeoutnumber (seconds)noDefault 5. When request times-out, the callback will be fired with a timeout error and a client-side rendering response (unless disableFailoverRendering is set to true)

Example:

...
client.renderComponent('header', {
  container: false,
  headers: {
    'accept-language': 'en-GB'
  },
  parameters: {
    loggedIn: true
  },
  timeout: 2
}, function(err, html, details){
  console.log(html, details.headers);
  // => "<div>This is the header. <a>Log-out</a></div>"
});

Client#renderComponents(components , options, callback)

It will make a request to the registry, and will render the components. The callback will contain an array of errors (array of null in case there aren't any), an array of rendered html snippets, and an array of details. It will follow the same order of the request. This method will make 1 request to the registry + n requests for each component to get the views of components that aren't cached yet. After caching the views, this will make just 1 request to the registry.

Components parameter:

Parametertypemandatorydescription
componentsarray of objectsyesThe array of components to retrieve and render
components[index].namestringyesThe component's name
components[index].versionstringnoThe component's version. When not speficied, it will use globally specified one (doing client initialisation); when not specified and not globally specified, it will default to "" (latest)
components[index].parametersobjectnoThe component's parameters
components[index].containerbooleannoThe component's container option. When not specified, it will be the one specified in the options (for all components); if none is specified in options, it will default to true. When false, renders a component without its container
components[index].renderstringnoThe component's render mode. When not specified, it will be the one specified in the options (for all components); if none is specified in options, it will default to server. When server, the rendering will be performed on the server-side and the result will be component's html. If client, the html will contain a promise to do the rendering on the browser.

Options:

Parametertypemandatorydescription
containerbooleannoDefault true, when false, renders a component without its container
disableFailoverRenderingbooleannoDisables the automatic failover rendering in case the registry times-out (in case configuration.registries.clientRendering contains a valid value.) Default false
forwardAcceptLanguageToClientbooleannoWhen not specified in config, defaults to false. When true, when doing client-side requests (normal or failover) appends a custom parameter to the browser's component hrefs so that the framework will ignore the browser's Accept-Language in favour of the query-string value
headersobjectnoAn object containing all the headers that must be forwarded to the component
ie8booleannoDefault false, if true puts in place the necessary polyfills to make all the stuff work with ie8
parametersobjectnoGlobal parameters for all components to retrieve. When component has its own parameters, globals will be overwritten
registriesobjectnoThe registries' endpoints (overrides the parameters defined during instantiation)
registries.serverRenderingstringnoThe baseUrl for server-side rendering requests (overrides the parameter defined during instantiation)
registries.clientRenderingstringnoThe baseUrl for client-side rendering requests (overrides the parameter defined during instantiation)
renderstringnoDefault server. When server, it will return html. When client will produce the html to put in the page for post-poning the rendering to the browser
timeoutnumber (seconds)noDefault 5. When request times-out, the callback will be fired with a timeout error and a client-side rendering response (unless disableFailoverRendering is set to true)

Example:

...
client.renderComponents([{
  name: 'header',
  parameters: { loggedIn: true }
}, {
  name: 'footer',
  version: '4.5.X'
}, {
  name: 'advert',
  parameters: { position: 'left' },
  render: 'client'
}], {
  container: false,
  headers: {
    'accept-language': 'en-US'
  },
  timeout: 3.0
}, function(errors, htmls, details){
  for ( let i; i < htmls.length; i++) {
    console.log(htmls[i], details[i].headers);
  }
  // => ["<div>Header</div>",
  //     "<p>Footer</p>",
  //     "<oc-component href=\"\/\/registry.com\/advert\/?position=left\"><\/oc-component>"]
});
4.0.1

3 years ago

4.0.0

3 years ago

3.2.12

4 years ago

3.2.11

4 years ago

3.2.10

5 years ago

3.2.9

5 years ago

3.2.8

5 years ago

3.2.7

5 years ago

3.2.6

5 years ago

3.2.5

5 years ago

3.2.4

5 years ago

3.2.3

5 years ago

3.2.2

6 years ago

3.2.1

6 years ago

3.2.0

6 years ago

3.1.0

6 years ago

3.0.11

6 years ago

3.0.10

6 years ago

3.0.9

6 years ago

3.0.8

6 years ago

3.0.7

6 years ago

3.0.6

6 years ago

3.0.5

6 years ago

3.0.4

6 years ago

3.0.3

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.1.33

6 years ago

2.1.32

7 years ago

2.1.31

7 years ago

2.1.30

7 years ago

2.1.29

7 years ago

2.1.28

7 years ago

2.1.27

7 years ago

2.1.26

7 years ago

2.1.25

7 years ago

2.1.24

7 years ago

2.1.23

7 years ago

2.1.22

7 years ago

2.1.21

7 years ago

2.1.20

7 years ago

2.1.19

7 years ago

2.1.18

7 years ago

2.1.17

7 years ago

2.1.16

7 years ago

2.1.15

7 years ago

2.1.14

7 years ago

2.1.13

7 years ago

2.1.12

7 years ago

2.1.11

7 years ago

2.1.10

7 years ago

2.1.9

7 years ago

2.1.8

7 years ago

2.1.7

7 years ago

2.1.6

7 years ago

2.1.5

7 years ago

2.1.4

7 years ago

2.1.3

7 years ago

2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.14.0

7 years ago

1.13.1

7 years ago

1.13.0

8 years ago

1.12.1

8 years ago

1.12.0

8 years ago

1.11.1

8 years ago

1.11.0

8 years ago

1.10.0

8 years ago

1.9.1

8 years ago

1.9.0

8 years ago

1.8.0

8 years ago

1.7.3

8 years ago

1.7.2

8 years ago

1.7.1

8 years ago

1.7.0

8 years ago

1.6.2

8 years ago

1.6.1

8 years ago

1.6.0

8 years ago

1.5.1

8 years ago

1.5.0

8 years ago

1.4.0

8 years ago

1.3.3

8 years ago

1.3.2

8 years ago

1.3.1

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago

0.1.13

8 years ago

0.1.12

8 years ago

0.1.11

8 years ago

0.1.10

8 years ago

0.1.9

8 years ago

0.1.8

8 years ago

0.1.7

9 years ago

0.1.6

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.13

9 years ago

0.0.12

9 years ago

0.0.11

9 years ago

0.0.10

9 years ago

0.0.9

9 years ago

0.0.7

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago