NodeJs SDK

Source code with detailed examples can be found here.

Installation

We have first-class TypeScript support, but this library can also be used in a javascript project. We provide a useful function to prevent errors in input config variables.

npm

npm install @aidbox/node-server-sdk

yarn

yarn add @aidbox/node-server-sdk

Requirements

To start working with the backend application you should enter the required env variables:

Client id with basic auth grant type to work with aidbox:

AIDBOX_CLIENT_ID=

Client secret:

AIDBOX_CLIENT_SECRET=secret

Your aidbox url:

AIDBOX_URL=http://0.0.0.0:8085

Toggle debug mode:

APP_DEBUG=false

App name to identify the application in aidbox:

APP_ID=you-business-app

Secret for use application (aidbox will use it):

APP_SECRET=secret

Backend application url (aidbox will send a request to this base url):

APP_URL=http://0.0.0.0:8090

Port for your backend application:

APP_PORT=8090

App example

Typescript usage

Firstly, you should create a config object. By default, we use env variables but you can optionally enter process.env as an input parameter.

import { createConfig } from '@aidbox/node-server-sdk/lib/config';

const config = createConfig();

(optional) Add your specific context helpers

type TContextHelpers = {
  greet(name: string): void;
};

const contextHelpers: TContextHelpers = {
  greet: (name: string) => {
    console.log(`Hello, ${name}`);
  },
};

Next step is defining the manifest object. For example:

import { TRawManifest } from '../src/types';

// pass type if your define your specific context helpers
const manifest: TRawManifest<TContextHelpers> = {
  resources: {
    AccessPolicy: {},
  },
  entities: {},
  operations: {
    test: {
      method: 'GET',
      path: ['$test-operation'],
      handler: async (context) => {
        context.greet('Alice'); // your specific context helper
        return { resource: { work: true } };
      },
    },
  },
  subscriptions: {
    Patient: {
      handler: () => {
        console.log('qwerty');
        return true;
      },
    },
  },
};

After you prepare the config object and define the manifest, you can run your backend application. Typescript won't let you miss any required config keys. There are additional check input parameters before the app is created:

import { createApp, startApp } from '@aidbox/node-server-sdk';

const app = createApp<TContextHelpers>(config, manifest, contextHelpers);
if (!app) {
  console.error(`Unable to create app. Check config/manifest errors.`);
  process.exit(1);
}

await startApp(app);

Then you can go to your Aidbox. There you will find your new application in the Apps menu. To test the app, run this request in the Aidbox Rest Console:

Request

GET /$test-operation

Response

work: true

Last updated