0.1.4 • Published 3 years ago

@nebulrab/nebulr-platform v0.1.4

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

Nebulr Platform Lib

Welcome to the Nebulr platform: the plug and play service that enables developers to focus more on your unique product logic than boring hygiene factors.

This library helps you get up and running in no time. Sign up, register your project and obtain an access token today here!

Sign up for access

Sign up, register your project and obtain an access token today here!

Installing

npm i @nebulrgroup/nebulr-platform

Usage

  1. Import AppClient from the package
import { AppClient } from '@nebulrgroup/nebulr-platform'
  1. Instantiate a new AppClient with your access token.
const app = new AppClient("ACCESS_KEY");

This library is written in typescript and comes with types and documentation built in.

Examples

List all accounts in your app

import { AppClient, Tenant } from '@nebulrgroup/nebulr-platform'

app.tenants.list().then(tenants:Tenant[] => {
  tenants.map(t: Tenant => console.log(`Name: ${t.name}, ID:${t.id}`));
}));

Create a new user in a specific account

const tenantClient = app.tenant("TENANT_ID");

const user = await tenantClient.users.create(
    { username: "john.doe@example.com", role: "ADMIN" }
);

await tenantClient.user(user.id).sendEmail(
    {
        subject: "What's up?",
        emailBody: "Although you've received onboarding emails from the platform by now\n
         have a look at this fancy email with my logo and brand",
        ctaTitle: "Read more!",
        ctaUrl: "http://example.com"
    }
);

Deactivate a user from logging in

await app.tenant("TENANT_ID").user("USER_ID").update({enabled: false});

Resolve user data from request and authorize for a certain action

const authResponse = await app.auth.authorize({req, resource: "secure/resource"});

if (authResponse.granted) {
  console.log(`User: ${user.fullName} of Account: ${user.tenant.name} was granted access to secure/resource`);
}

Upload a file #1

import { AppClient, FileUploadRequest } from '@nebulrgroup/nebulr-platform'

// Obtain the file client 
const fileClient = app.tenant("TENANT_ID").file;

const uploadData = fileClient.prepareUpload(metaData);

const uploadResult = fileClient.finishUpload(uploadData);

console.log(`Uploaded cat.png is now available on: ${uploadResult.url}`)

Upload a file #2

import { AppClient, FileUploadRequest } from '@nebulrgroup/nebulr-platform'

const metaData: FileUploadRequest = {
    "fileName": "cat.png",
    "contentType": "image/png"
};
const file:Blob = [....];

// Retrieves upload data that allows cat.png to be uploaded within 10 minutes
const fileClient = app.tenant("TENANT_ID").file;

const uploadData = fileClient.prepareUpload(metaData);

// Use Axios or any other Http client to POST the file data straight our file storage
// Preferrably on the client side for best performance (uploadData is safe to output to client)
const formData: FormData = {...uploadData.fields, {file: file}}
await axios.post(uploadData.url, formData);

// Retrieve a signed url for direct access to the uploaded file
const uploadResult = fileClient.finishUpload(uploadData);

console.log(`Uploaded cat.png is now available on: ${uploadResult.url}`)

The Nebulr Platform

Background

As developers, we've been developing a lot of products over the years. Each time we dealt with the repeting pain of setting up user management, login and access control, recurring subscriptions and service provisioning, file uploads, emailing etc. etc.

One day we asked ourselves why we yet hadn't start working on the idéa that we had thought of many times before; to take all the repeating tasks, components and building blocks of an application and expose these as api services that could be reused over and over again. Talk about a DRY approach!

Over the last years we've built several products that utilize the power our platform and we now we're opening it up for everyone else to use.

Simple integration

As developers, that have always scan through the markets and communities for good libraries, we appriciate a simple but not limiting developer experiences. We hope that we've simplified things enough for devs and product owners to understand the potential of outsourcing all boring logic to us and just focus on the fun stuff and what your app should bring to the market.

Documentation

....

More in depth examples

Generate and send One Time Password as SMS message

const otpMessage = await app.tenant("TENANT_ID").user("USER_ID").sendOtpSms();
console.log(`User ${user.id} recieved OTP: ${otpMessage.code} as SMS`);

NestJS secure controller example where Platform Authorize returns current user and tenant context

@Controller('secure')
export class MySecureController {
  
  .....

  @Get('/resource/:id')
  async getResource(
    @Req() req: Request,
    @Param('id') id: string,
  ): Promise<Record<string, unknown>> {

    const authResponse = await app.auth.authorize({req, resource: "secure/resource"});

    if (!authResponse.granted)
        throw new UnauthorizedError()

    const user = authResponse.user;
    console.log(`User: ${user.fullName} is allowed to do this`);

    // Render resource filtered by tenant id
    return this.mySecretService.getResource(id, user.tenant.id);
    ....
  }

Changelog

TBD as soon as the library leaves beta

0.1.2

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago