-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9fa75b9
Showing
19 changed files
with
955 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/node_modules | ||
.env | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
|
||
# @moibitjs/core | ||
> Core module of moibitjs to interact with [MoiBit](https://www.moibit.io) off-chain decentralized storage platform. | ||
This library will help _**Authorized Dapp Developers**_ to store , read , delete , pin , unpin , detail the file(s) using MoiBit as their storage platform | ||
|
||
_Click [here](https://account.moibit.io) to get authorized in MoiBit. You will be getting **API_KEY , API_SECRET , URL**_ after successful signup. | ||
|
||
## Install | ||
Using npm: | ||
```bash | ||
npm install --save @moibitjs/core | ||
``` | ||
### Getting started | ||
```js | ||
(async => { | ||
//import moibit core sdk | ||
import MoiBit from '@moibitjs/core' | ||
//create moibit instance | ||
const files = new MoiBit( | ||
// the url you got after signing up to moibit | ||
'<your-url>', { | ||
'API_KEY' : '<your-api-key>', // the api key you got after signing up to moibit | ||
'API_SECRET' : '<your-api-secret>' // the api key you got after signing up to moibit | ||
}); | ||
console.log(await files.storageDetails('GB')); | ||
})() | ||
``` | ||
|
||
### Functions | ||
- <a href="#initialize"><code>new MoiBit()</code></a> | ||
- <a href="#addFile"><code>files.<b>add()</b></code></a> | ||
- <a href="#readFile"><code>files.<b>read()</b></code></a> | ||
- <a href="#readFromHash"><code>files.<b>readFromHash()</b></code></a> | ||
- <a href="#listFiles"><code>files.<b>list()</b></code></a> | ||
- <a href="#removeFile"><code>files.<b>remove()</b></code></a> | ||
- <a href="#pinFile"><code>files.<b>pin()</b></code></a> | ||
- <a href="#unpinFile"><code>files.<b>unpin()</b></code></a> | ||
- <a href="#filedetail"><code>files.<b>filedetail()</b></code></a> | ||
- <a href="#storageDetails"><code>files.<b>storageDetails()</b></code></a> | ||
--- | ||
|
||
<a name="initialize"></a> | ||
#### new MoiBit(url,accessToken) | ||
|
||
This constructor is to wrap files module with url and access token , so that you don't need to send access token in every call. However authentication will be happen for every call. | ||
- <code>url</code> DNS/IP address of the node where MoiBit API is running | ||
- <code>accessToken</code> is an object with _API_KEY_ and _API_SECRET_ as keys | ||
```js | ||
let files = new MoiBit(<YOUR_URL>,{ | ||
API_KEY : '<YOUR_API_KEY>' , | ||
API_SECRET : '<YOUR_API_SECRET>' | ||
}); | ||
``` | ||
<a name="addFile"></a> | ||
|
||
#### files.add(file,path,options) | ||
|
||
Adds file of any type to MoiBit and returns back multi-hash of the file. | ||
|
||
- <code>file</code> can be window file object or stream | ||
|
||
- <code>path</code> is an absolute path in your files directory at which you want the file to be inserted. | ||
|
||
- <code>options</code> | ||
|
||
- `createFolders` is a boolean value which specifies to create a folder/not if it is not existing , that was mentioned in above path attribute (_default : true_) | ||
|
||
- `pinVersion` is a boolean value which tells to pin the file while adding.(_default : false_) | ||
|
||
```js | ||
await files.add(fileObject,'parent1/folder2/file3.txt'}); | ||
``` | ||
|
||
<a name="readFile"></a> | ||
|
||
#### files.read(path,responseType) | ||
|
||
Returns file in mentioned response type from its file name. | ||
|
||
**_File modified off-chain_** | ||
|
||
- `path` is an absolute path | ||
|
||
- `responseType` can be anything among | ||
|
||
- _arraybuffer , document , json , text , stream_ | ||
|
||
- _blob - browser only_ | ||
|
||
```js | ||
await files.read('parent1/folder2/file3.txt','blob'); | ||
``` | ||
|
||
|
||
|
||
<a name="readFromHash"></a> | ||
|
||
#### files.readFromHash(hash,responseType) | ||
|
||
Returns file in mentioned response type from its hash. | ||
|
||
- `hash` is multihash of the file | ||
|
||
- `responseType` can be anything among | ||
|
||
- _arraybuffer , document , json , text , stream_ | ||
|
||
- _blob - browser only_ | ||
|
||
|
||
|
||
```js | ||
await files.readFromHash('Qmbg......','json'); | ||
``` | ||
|
||
|
||
|
||
<a name="listFiles"></a> | ||
|
||
#### files.list(path) | ||
|
||
Returns array of files within the folder mentioned. | ||
|
||
- `path` is absolute path of the folder, if path is undefined or not mentioned will return all the files in your root folder. | ||
|
||
```js | ||
await files.list('parent1/folder2'); | ||
``` | ||
|
||
|
||
|
||
<a name="removeFile"></a> | ||
|
||
#### files.remove(path) | ||
|
||
Removes the file from MoiBit and returns back the acknowledgement | ||
|
||
- `path` is absolute path of the file | ||
|
||
```js | ||
await files.remove('parent1/folder2/file2.txt'); | ||
``` | ||
|
||
|
||
|
||
<a name="pinFile"></a> | ||
|
||
#### files.pin(options) | ||
|
||
Pins the file in MoiBit , so that garbage Collector won't collect the file even though the file was not accessed for a long time. | ||
|
||
- `options` | ||
|
||
- `filename` absolute file name | ||
|
||
- `hash` is the multihash of the file | ||
|
||
```js | ||
await files.pin({hash : 'QmAs...'}) | ||
``` | ||
|
||
|
||
|
||
<a name="unpinFile"></a> | ||
|
||
#### files.unpin(options) | ||
|
||
Unpins the pinned file in MoiBit , so that garbage Collector got the access to collect the file which was not accessed for a long time. | ||
|
||
- `options` | ||
|
||
- `filename` absolute file name | ||
|
||
- `hash` is the multihash of the file | ||
|
||
```js | ||
await files.unpin({filename : 'parent1/folder2/file3.txt'}); | ||
``` | ||
|
||
|
||
|
||
<a name="filedetail"></a> | ||
|
||
#### files.filedetail(path) | ||
|
||
Returns complete detail about the file | ||
|
||
```js | ||
await files.filedetail('parent1/folder2/file3.txt'); | ||
``` | ||
|
||
<a name="storageDetails"></a> | ||
|
||
#### files.storageDetails(unit) | ||
|
||
Returns all the storage details of the particular account (you did init with) in specific Unit. | ||
|
||
- `unit` is a short hand notation of storage unit. It can be _B , KB , MB , GB , TB (case insensitive)_ | ||
|
||
```js | ||
await files.storageDetails('mb'); | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const dotenv = require('dotenv') | ||
dotenv.config() | ||
module.exports = { | ||
baseUrl : process.env.baseUrl, | ||
public : process.env.public, | ||
secret : process.env.secret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"_requiredBy": [ | ||
"/@moibitjs/matic" | ||
], | ||
"author": "its-VSP", | ||
"name": "@moibitjs/core", | ||
"version": "0.1.0", | ||
"description": "The Core module of moibitjs to interact with MoiBit off-chain Storage platform", | ||
"main": "./src/sdk.js", | ||
"scripts": { | ||
"test": "jasmine tests/*.js" | ||
}, | ||
"keywords": [ | ||
"off-chain", | ||
"storage", | ||
"moibit", | ||
"js", | ||
"core", | ||
"sdk" | ||
], | ||
"license": "", | ||
"devDependencies": { | ||
"jasmine": "^2.6.0", | ||
"path": "^0.12.7", | ||
"dotenv": "^8.1.0", | ||
"form-data": "^2.5.1" | ||
}, | ||
"dependencies": { | ||
"axios": "^0.19.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/moibit/core" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/moibit/core/issues" | ||
}, | ||
"homepage": "https://github.com/moibit/core#readme" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"spec_dir": "tests", | ||
"spec_files": [ | ||
"*.js" | ||
], | ||
"helpers": [ | ||
"helpers/**/*.js" | ||
], | ||
"stopSpecOnExpectationFailure": false, | ||
"random": false, | ||
"DEFAULT_TIMEOUT_INTERVAL":5000 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
var axios = require('axios') | ||
var isUrl = require('./utils/isUrl') | ||
var isDef = require('./utils/isDef') | ||
|
||
|
||
module.exports = class { | ||
/** | ||
* Create FileApiClient for sending requests | ||
* @constructor | ||
* @param {string} baseUrl - A string with the base url | ||
* @param {Object} accessToken - An Object with api_key and api_secret | ||
*/ | ||
constructor(baseUrl,accessToken) { | ||
if (!isUrl(baseUrl)) | ||
throw new Error({message:'The base URL provided is not valid'}) | ||
|
||
this.baseUrl = baseUrl | ||
this.accessToken = accessToken | ||
} | ||
|
||
/** | ||
* sends the request from the API. | ||
* @param {*} method says get or post call | ||
* @param {*} url says which route going to call | ||
* @param {*} payload payload for the request | ||
* @param responseType indicates the type of data that the server will respond with | ||
* options are: 'arraybuffer', 'document', 'json', 'text', 'stream' | ||
* browser only: 'blob' | ||
responseType: 'json', by default | ||
* @return {Promise} - Returns a Promise , when fulfilled, will either return an JSON Object with the requested | ||
* data or an Error with the reason. | ||
*/ | ||
send(method, url, payload = {},responseType) { | ||
let requestedRoute = (this.baseUrl+'/'+url).trim() | ||
let authenticatedHeaders = {} | ||
|
||
authenticatedHeaders['api_key'] = this.accessToken.public || '' | ||
authenticatedHeaders['api_secret'] = this.accessToken.secret || '' | ||
|
||
if (method === 'POST') { | ||
authenticatedHeaders['Content-Type'] = 'application/json' | ||
} else if (Object.keys(payload).length && payload.constructor === Object) { | ||
let str = '?' | ||
let keys = Object.keys(payload) | ||
keys.map((key,index) => { | ||
str = str + key + '=' +payload[key] | ||
str = index+1 !== keys.length ? str + '&' : str | ||
}) | ||
requestedRoute = requestedRoute + str | ||
} | ||
if(!isDef(responseType)) { | ||
responseType = 'json' | ||
} | ||
return axios({ | ||
url : requestedRoute, | ||
method : method, | ||
data: payload, | ||
headers : authenticatedHeaders, | ||
responseType : responseType | ||
}) | ||
.then((response) => { | ||
if (response.status >= 200 && response.status <= 202) { //succeeded request | ||
return Promise.resolve(response) | ||
} | ||
return {} | ||
}) | ||
.catch(e => { | ||
if(isDef(e.response)) { | ||
return Promise.reject({ | ||
status: e.response.status || '', | ||
message: e.response.statusText || '', | ||
body: e.response.data || '' | ||
}) | ||
} | ||
else { | ||
return Promise.reject(e) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.