From e9ee70cdda05102523a2c953c5af9954a8989bfb Mon Sep 17 00:00:00 2001 From: Tanmai Gopal Date: Wed, 28 Jun 2017 15:47:23 +0530 Subject: [PATCH 1/2] Update README.md --- README.md | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 165 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b687291..5f32bf4 100644 --- a/README.md +++ b/README.md @@ -1 +1,165 @@ -# js-sdk ## Installation Add this to your HTML: #### Hasura projects created via beta.hasura.io ```html ... ``` #### Hasura projects created via local-development or other methods ```html ... ``` ## Quickstart ```javascript /* New session */ hasura.user // Will be anonymous user // { // username: 'anonymous', // id: 0, // roles: ['anonymous'], // token: null // } /* Login and create new session */ hasura.setUsername('user1'); // Will set curename for current object and save to localStorage hasura.login('user1password', onSuccess, onError); // Will log the current user hasura.user // will be logged in user // { // username: 'user1', // id: 3, // roles: ['user'], // token: 'xxxxxxxxxxxxxxxx' // } /* If you refresh the page */ hasura.user // will be the logged in user // { // username: 'user1', // id: 3, // roles: ['user'], // token: 'xxxxxxxxxxxxxxxx' // } hasura.auth.logout(onSuccess, onError); hasura.user // will be reset to anonymous user ``` ### Data query ```javascript // This will use the hasura.user session object to send // if hasura.user.token === null, then request is made as an anonymous user (no auth token) hasura.data.query({ type: 'select', args: { table: 'test', columns: ['*'] }, onSuccess, onError); // Query with a specific role hasura.data.queryAsRole('user' type: 'select', args: { table: 'test', columns: ['*'] }, onSuccess, onError); ``` ### Data query-templates ```javascript // This will use the hasura.user session object to send // if hasura.user.token === null, then request is made as an anonymous user (no auth token) hasura.data.queryTemplate( 'query-template-name', { param: , param2: }, onSuccess, onError); // Query with a specific role hasura.data.queryTemplateAsRole( 'user', 'query-template-name', { param: , param2: }, onSuccess, onError); ``` ### Filestore uasage The Hasura JS SDK provides convenience functions to upload and download files. ```html ``` ```javascript var input = document.getElementById('my-file'); var file = input.files[0]; var fileId; hasura.file.upload(file, (successResponse) => { fileId = successResponse.file_id; }, onError); hasura.file.download(fileId); // This will use the HTML5 download attribute to start downloading the file hasura.file.delete(fileId); ``` # Contribution & Development For development builds: ```sh npm install ./node_modules/rollup/bin/rollup -c ``` This will output: ```sh build/js/main.min.js ``` For production builds: ```sh npm install NODE_ENV=production ./node_modules/rollup/bin/rollup -c ``` \ No newline at end of file +# js-sdk + +## Installation +Add this to your HTML: + +#### Hasura projects created via beta.hasura.io + +```html + + ... + + + +``` + +#### Hasura projects created via local-development or other methods + +```html + + ... + + + +``` + + +## Quickstart + +```javascript +/* New session */ +hasura.user // Will be anonymous user +// { +// username: 'anonymous', +// id: 0, +// roles: ['anonymous'], +// token: null +// } + +/* Login and create new session */ +hasura.setUsername('user1'); // Will set curename for current object and save to localStorage +hasura.login('user1password', onSuccess, onError); // Will log the current user +hasura.user // will be logged in user +// { +// username: 'user1', +// id: 3, +// roles: ['user'], +// token: 'xxxxxxxxxxxxxxxx' +// } + +/* If you refresh the page */ +hasura.user // will be the logged in user +// { +// username: 'user1', +// id: 3, +// roles: ['user'], +// token: 'xxxxxxxxxxxxxxxx' +// } +hasura.auth.logout(onSuccess, onError); +hasura.user // will be reset to anonymous user +``` + +### Data query + +```javascript +// This will use the hasura.user session object to send +// if hasura.user.token === null, then request is made as an anonymous user (no auth token) +hasura.data.query({ + type: 'select', + args: { + table: 'test', + columns: ['*'] + }, + onSuccess, + onError); + +// Query with a specific role +hasura.data.queryAsRole('user' + type: 'select', + args: { + table: 'test', + columns: ['*'] + }, + onSuccess, + onError); +``` + +### Data query-templates + +```javascript +// This will use the hasura.user session object to send +// if hasura.user.token === null, then request is made as an anonymous user (no auth token) +hasura.data.queryTemplate( + 'query-template-name', + { + param: , + param2: + }, + onSuccess, + onError); + +// Query with a specific role +hasura.data.queryTemplateAsRole( + 'user', + 'query-template-name', + { + param: , + param2: + }, + onSuccess, + onError); +``` + +### Filestore uasage + +The Hasura JS SDK provides convenience functions to upload and download files. + +```html + +``` + +```javascript + var input = document.getElementById('my-file'); + var file = input.files[0]; + var fileId; + hasura.file.upload( + file, + (successResponse) => { + fileId = successResponse.file_id; + console.log('Uploaded file: ' + fileId); + }, + (errorResponse) => { + console.log('Error uploading file'); + console.log(errorResponse); + }); + + hasura.file.download(fileId); // This will use the HTML5 download attribute to start downloading the file + + hasura.file.delete(fileId); +``` + + +# Contribution & Development + +For development builds: +```sh +npm install +./node_modules/rollup/bin/rollup -c +``` + +This will output: + +```sh +build/js/main.min.js +``` + +For production builds: +```sh +npm install +NODE_ENV=production ./node_modules/rollup/bin/rollup -c +``` From ed8410d096031d36ad3bafb3d3531afbb33c81b7 Mon Sep 17 00:00:00 2001 From: Tanmai Gopal Date: Wed, 28 Jun 2017 16:29:43 +0530 Subject: [PATCH 2/2] Minor udpates to README Adds info about `onSuccess` and `onError` in the README. --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 5f32bf4..113a0a4 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,8 @@ hasura.user // will be reset to anonymous user ### Data query +**NOTE**: In the examples below, `onSuccess` and `onError` are callback functions that you must implement. + ```javascript // This will use the hasura.user session object to send // if hasura.user.token === null, then request is made as an anonymous user (no auth token) @@ -91,6 +93,8 @@ hasura.data.queryAsRole('user' ### Data query-templates +**NOTE**: In the examples below, `onSuccess` and `onError` are callback functions that you must implement. + ```javascript // This will use the hasura.user session object to send // if hasura.user.token === null, then request is made as an anonymous user (no auth token) @@ -132,10 +136,12 @@ The Hasura JS SDK provides convenience functions to upload and download files. (successResponse) => { fileId = successResponse.file_id; console.log('Uploaded file: ' + fileId); + // your code goes here }, (errorResponse) => { console.log('Error uploading file'); console.log(errorResponse); + // your code goes here }); hasura.file.download(fileId); // This will use the HTML5 download attribute to start downloading the file