Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/hasura/js-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
coco98 committed Jun 29, 2017
2 parents 78f675d + ed8410d commit 185f31e
Showing 1 changed file with 171 additions and 1 deletion.
172 changes: 171 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,171 @@
# js-sdk## InstallationAdd this to your HTML:#### Hasura projects created via beta.hasura.io```html<body> ... <script src="https://github.com/hasura/js-sdk/releases/download/v0.1.1/hasura.min.js"></script> <script> hasura.setProjectName('hello70'); // If your hasura project is hello70.hasura-app.io </script></body>```#### Hasura projects created via local-development or other methods```html<body> ... <script src="https://github.com/hasura/js-sdk/releases/download/v0.1.1/hasura.min.js"></script> <script> hasura.setBaseDomain('c103.hasura.me'); hasura.disableHttps(); // No HTTPS enabled on local-development </script></body>```## 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 localStoragehasura.login('user1password', onSuccess, onError); // Will log the current userhasura.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 rolehasura.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: <value>, param2: <value2> }, onSuccess, onError);// Query with a specific rolehasura.data.queryTemplateAsRole( 'user', 'query-template-name', { param: <value>, param2: <value2> }, onSuccess, onError);```### Filestore uasageThe Hasura JS SDK provides convenience functions to upload and download files.```html <input id="my-file" type="file" />``````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 & DevelopmentFor development builds:```shnpm install./node_modules/rollup/bin/rollup -c```This will output:```shbuild/js/main.min.js```For production builds:```shnpm installNODE_ENV=production ./node_modules/rollup/bin/rollup -c```
# js-sdk

## Installation
Add this to your HTML:

#### Hasura projects created via beta.hasura.io

```html
<body>
...
<script src="https://github.com/hasura/js-sdk/releases/download/v0.1.1/hasura.min.js"></script>
<script>
hasura.setProjectName('hello70'); // If your hasura project is hello70.hasura-app.io
</script>
</body>
```

#### Hasura projects created via local-development or other methods

```html
<body>
...
<script src="https://github.com/hasura/js-sdk/releases/download/v0.1.1/hasura.min.js"></script>
<script>
hasura.setBaseDomain('c103.hasura.me');
hasura.disableHttps(); // No HTTPS enabled on local-development
</script>
</body>
```


## 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

**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)
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
**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)
hasura.data.queryTemplate(
'query-template-name',
{
param: <value>,
param2: <value2>
},
onSuccess,
onError);

// Query with a specific role
hasura.data.queryTemplateAsRole(
'user',
'query-template-name',
{
param: <value>,
param2: <value2>
},
onSuccess,
onError);
```
### Filestore uasage
The Hasura JS SDK provides convenience functions to upload and download files.
```html
<input id="my-file" type="file" />
```
```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);
// 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

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
```

0 comments on commit 185f31e

Please sign in to comment.