js-sdk
Installation
Add this to your HTML:
Hasura projects created via beta.hasura.io
...
Hasura projects created via local-development or other methods
...
Quickstart
/* New session */hasurauser // Will be anonymous user// {// username: 'anonymous',// id: 0,// roles: ['anonymous'],// token: null// } /* SignUp/register a new user */hasura; // Will set username for current object and save to localStoragehasuraauth; // Will log the current user//Or using a promisehasuraauth; /* Login and create new session */hasura; // Will set username for current object and save to localStoragehasuraauth; // Will log the current user//Or using a promisehasuraauth; //After signup/loginhasurauser // will be logged in user// {// username: 'user1',// id: 3,// roles: ['user'],// token: 'xxxxxxxxxxxxxxxx'// } /* If you refresh the page */hasurauser // will be the logged in user// {// username: 'user1',// id: 3,// roles: ['user'],// token: 'xxxxxxxxxxxxxxxx'// } hasuraauth;//Or using a promisehasuraauth; hasurauser // will be reset to anonymous user
Data query
Option 1:
Use lambdas or anonymous functions directly for handling success/error.
hasuradata;
Option 2:
Use predefined functions as shown below:
{ console;} { console;} hasuradata;
Option 3:
Use promises:
hasuradata;
Filestore usage
The Hasura JS SDK provides convenience functions to upload and download files.
var fileInput = document; var fileId; hasurafile; //You can also implement the above using promises instead of callbacks hasurafile; hasurafile; // This will use the HTML5 download attribute to start downloading the file hasurafile; //Using promises hasurafile;
API requests to custom APIs deployed on Hasura
The Hasura JS SDK provides a simple wrapper over fetch
to make it easy
for you to make API requests to APIs deployed as custom microservices on Hasura.
If you're making a JSON request:
hasura;
If you're making a request with a non JSON content-type:
hasura;
You can also make the above call using promises
hasura;
Contribution & Development
For development builds:
npm install./node_modules/rollup/bin/rollup -c
This will output:
build/js/main.min.js
For production builds:
npm installNODE_ENV=production ./node_modules/rollup/bin/rollup -c