Code Monkey home page Code Monkey logo

gateway-js-sdk's Introduction

Gateway Javascript SDK

NPM version NPM downloads Join Discord Run Eslint & Test cases Coverage Status

Introduction

A TypeScript SDK for the Gateway API. This library is built with TypeScript developers in mind, but it also works with JavaScript.

Note: Our latest version of sdk works without adding any extra configuration for frontend environments.

Features

  • Full type information for methods and responses.
  • Bearer Token Support
  • Supports Node.js 18+.
  • Works in frontend environments without adding any extra configuration.

Installation

Using npm

npm install @gateway-dao/sdk

Using pnpm

pnpm add @gateway-dao/sdk

Using yarn

yarn add @gateway-dao/sdk

Gateway Client

To setup the gateway client we will authenticate with a bearer-token and a Api key as follows

import { Gateway } from '@gateway-dao/sdk';

const gateway = new Gateway({
  apiKey: 'your-api-key',
  token: 'your-token',
  url: 'https://sandbox.protocol.mygateway.xyz/graphql',
});

Make sure you add token without Bearer as we add Bearer automatically when you make request. Else it will give you Unauthorized error even if your token is correct For example

import { Gateway } from '@gateway-dao/sdk';

const gateway = new Gateway({
  apiKey: 'your-api-key',
  token: 'Bearer your-token',
  // wrong will not work just use token: 'your-token'
  url: 'https://sandbox.protocol.mygateway.xyz/graphql',
});

This library supports Bearer Token along with Api Key. Do not share your authentication token with people you don’t trust. This gives the user control over your account and they will be able to manage PDAs (and more) with it. Use environment variables to keep it safe.

Examples

Make sure to add try catch blocks around methods to catch all the validation and protocol based errors.

Creating a PDA

import { Gateway, UserIdentifierType } from '@gateway-dao/sdk';

const gateway = new Gateway({
  apiKey: 'your-api-key',
  token: 'your-token',
  url: 'https://sandbox.protocol.mygateway.xyz/graphql',
});

async function main() {
  try {
    let obj = {
      dataModelId: 'uuid-here',
      description: 'test',
      title: 'test',
      claim: {
        gatewayUse: 'test',
      },
      owner: {
        type: UserIdentifierType.GATEWAY_ID,
        value: 'test',
      },
    };
    const { createPDA } = await gateway.pda.createPDA(obj);
  } catch (error) {
    console.log(error); // Can log it for degugging
  }
}

main();

Getting a Organization

import { Gateway } from '@gateway-dao/sdk';

const gateway = new Gateway({
  apiKey: 'your-api-key',
  token: 'your-token',
  url: 'https://sandbox.protocol.mygateway.xyz/graphql',
});

async function main() {
  try {
    let obj = {
      username: 'test_for_sdk_2',
      name: 'test org sdk 2',
      description: 'test organization',
    };
    const { createOrganization } =
      await gateway.organization.createOrganization(obj);
  } catch (error) {
    console.log(error); // Can log it for degugging
  }
}

main();

Create a Data request template

import { Gateway } from '@gateway-dao/sdk';

const gateway = new Gateway({
  apiKey: 'your-api-key',
  token: 'your-token',
  url: 'https://sandbox.protocol.mygateway.xyz/graphql',
});

async function main() {
  try {
    const { createDataRequestTemplate } =
      await gateway.dataRequestTemplate.createDataRequestTemplate({
        title: 'Create Data Request Template Example',
        description: 'Lorem ipsum dolor sit amet.',
        dataModels: [
          {
            id: 'uuid-here',
            required: true,
            claimValidations: {
              type: 'object',
              properties: {
                gatewayUse: {
                  type: 'string',
                },
              },
              required: ['gatewayUse'],
            },
          },
        ],
      });
  } catch (error) {
    console.log(error); // Can log it for degugging
  }
}

main();

Error Handling

All the methods throw a validation error if the validation does not match for example:- invalid wallet, invalid uuid for all ids, Incase of any protocol errors we will throw a custom message which is a string which has all neccessary info regarding error. Make sure to use try catch blocks to handle those.

License

The Gateway Javascript SDK is licensed under the MIT License.

Contributing

If you want to support the active development of the SDK. Please go through our Contribution guide

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.