JavaScript SDK

This package is written in Typescript.

Installation

npm i jsonbank
# OR YARN
yarn add jsonbank

Initialize

JsonBank class can be initialized with or without api keys.
Api Keys are only required when you want to access private/secured documents

const {JsonBank} = require("jsonbank")

const jsb = new JsonBank();
const {JsonBank} = require("jsonbank")

// Initialize with Api keys.
const jsb = new JsonBank({
  keys: {
    pub: 'JSB_PUBLIC_KEY',
    prv: 'JSB_PRIVATE_KEY',
  }
});

// authenticate the api keys
await jsb.authenticate();

Public Api Key

The public api key is a READONLY key used to read public or private documents.

Private Api Key

The private api key is a WRITE-ONLY key used to create/update documents.

Check Authentication

To check if the user is authenticated, use the isAuthenticated method.
Make sure to have called authenticate method before.

// somewhere in your code
await jsb.authenticate();

// check if the user is authenticated
jsb.isAuthenticated() // true

Public Content

Public contents do not require authentication.

getDocumentMeta()

Get a public document meta details either by id or path. Does not return the content of the document. To get the content, use getContent method.

const meta = await jsb.getDocumentMeta("id_or_path");
// result is of type "JSB_Response.ContentMeta"
console.log(meta.id) // id of the document
console.log(meta.project) // project name
console.log(meta.contentSize) // size object of the document
console.log(meta.path) // path of the document
console.log(meta.updatedAt) // date of last update
console.log(meta.createdAt) // date of creation
 type ContentMeta = {
  id: string;
  project: string;
  contentSize: {
    number: number;
    string: string;
  };
  path: string;
  updatedAt: string;
  createdAt: string;
};

getContent()

Get a public document content either by id or path. It returns the json parsed object.

const content = await jsb.getContent("id_or_path");
// content is a json object

getContentAsString()

Get a public document content either by id or path as a string. It returns a string.

const content = await jsb.getContentAsString("id_or_path");
// content is a string

getGitHubContent()

Grab a public json file from Github. This will read from the default branch of the repo.
It returns the json parsed object.

Note: Referenced file must be a public json file.

await jsb.getGithubContent("org/repo/file.json");

// example
await jsb.getGithubContent("jsonbankio/documentation/github-test.json")
// This is the content of the json file.
{
  "name": "github-test.json",
  "purpose": "This file is used to test the github json fetcher"
}

getGitHubContentAsString()

Same as getGitHubContent but returns the content as a string.

Note: Referenced file must be a public json file.

await jsb.getGithubContentAsString("org/repo/file.json");

// example
await jsb.getGithubContentAsString("jsonbankio/documentation/github-test.json")
{"name": "github-test.json", "purpose": "This file is used to test the github json fetcher"}

Access Own Content

To access your own content, you need to authenticate with your api keys.

See With Api Keys for more details.

Last Updated:
Contributors: trapcodeio