Valyrian.js 5.0.8

Get started - Request plugin

What is Valyrian.js?InstallationHyperscript/JSXComponentsLifecycle methodsDirectivesServer side jsxPlugins

Request plugin


This plugin is available with the main valyrian.js package, so, you only need to add it with thev.usePlugin() method.
let Request = require('valyrian.js/plugins/request');



The Request plugin is a helper built on top of the fetch api. And it helps with the following:
  • Adds default headers to Accept application/json.
  • Adds convenience methods to perform get, post, put, patch, delete requests.
  • Automatically stringify/serialize payload when sending data.
  • Automatically throws an error when the response code is an error.
  • Scope a request with a base url to ease the api url handling.
  • Add a node url to ease server side rendering.
  • Automatically updates the view when the request has finished.

Use cases

Simple request

// v.request(method, url, data, options)
v.request('get', '', {hello: 'world'}, {})
This will perform a request to with Accept headers set to application/json. Therefore, the response will be converted to a json object if any. You can overide any previous options passing an object with your custom fetch options.

Convenience methods

You can make use of the convenience methods to perform the request. The default request helper will handle theget, post, put, patch, delete methods.
// v.request.get(url, data, options)
v.request.get('', {hello: 'world'}, {})

Scoped request

You can create another request helper function by calling the new method of the base request helper. Additionally you can create more request helpers based on a previous created helper inheriting its options like baseUrl and default request options.
//, defaultOptions);
let request ='', {});
request.get('/hello', {hello: 'world'}, {})
This will create a request helper pointing to So, the instruction will perform a request to

Allowed methods

You can create method scoped request helpers by passing an array with the methods option.
//, defaultOptions);
let request ='', {methods: ['post']});'/hello', {hello: 'world'}, {})
This will create a request helper with only the post method allowed, so you can safely use this helper without worrying about a wrong use of other methods.

For server side rendering

You can pass within the options object or set manually the urls to handle the requests for server side rendering.
This will redirect all local requests to http://localhost:3000. For most of the use cases this will be enough, but for the case where you only use full external urls, you can use the api option to convert this urls to the node version.
v.request.get('/hello') // http://localhost:3000/hello
v.request.get('') // http://localhost:3000/hello
You will need to use the Node plugin for SSR to work.