1.0.11 • Published 6 months ago

@magellanic/magellanic-sdk v1.0.11

Weekly downloads
-
License
ISC
Repository
-
Last release
6 months ago

Magellanic SDK for Node.js

The purpose of the Magellanic SDK for Node.js is to provide a quick and easy way to integrate any Node.js project with the Magellanic platform. Currently, it supports only Express.js, but there are plans to integrate with other popular frameworks, such as Fastify.

Magellanic Documentation

Learn more here.

Installation

The easiest way to get the latest version of Magellanic SDK for Node.js is to use yarn or npm:

yarn add @magellanic/magellanic-sdk
or
npm install @magellanic/magellanic-sdk

Usage

Note: MagellanicClient documentation generated using typedoc can be found here.

Examples

An example Node.js Express application that utilizes all Magellanic SDK for Node.js features can be found at ...

Configuration

Magellanic SDK can be configured either by providing configuration object during MagellanicClient initialization or using environment variables.

Environment variableConfig parameterRequiredDescription
MAGELLANIC_PROJECT_KEYprojectKeyyesMagellanic project key that is available in Magellanic web app
MAGELLANIC_WORKLOAD_NAMEprovidernoOptional workload name that will be shown in Magellanic web app (random uuid will be used otherwise)
MAGELLANIC_WORKLOAD_PROVIDERnamenoOptional provider type. Not required when apiKey is provided. Magellanic SDK will resolve it on its own, but providing it will make initialization faster
MAGELLANIC_API_KEYapiKeynoOptional API key. Required when not authenticating using any cloud provider (e.g. for development purposes)

Simple Setup

Create the MagellanicClient instance and authenticate:

import { MagellanicClient } from 'magellanic-sdk';

// Setup client
const client = new MagellanicClient({
  projectKey: 'mgl://3/2b8d8155-7530-49b0-ac4d-1a92cfc4c7ba',
  provider: 'k8s',
  name: 'My app',
});
await client.authenticate();

or

import { MagellanicClient } from 'magellanic-sdk';

const client = await MagellanicClient.createClient({
  projectKey: 'mgl://3/2b8d8155-7530-49b0-ac4d-1a92cfc4c7ba',
  provider: 'k8s',
  name: 'My app',
});

Generate Authorization Headers

When sending requests to another workload use generateHeaders() method. Example:

import axios, { AxiosError } from 'axios';
import { NextFunction, Request } from 'express';
import { magellanicClient } from 'path/of/your/setup/file';

app.get('/example-endpoint', async (req: Request, res: Response) => {
  // do something with request payload
  // ...

  try {
    await axios.post(
      'https://other-workload.com/example-workload-endpoint',
      payload,
      {
        headers: magellanicClient.generateHeaders(),
      },
    );
  } catch (err) {
    // handle 401
  }

  res.status(200).send(response);
});

Authorizing Workloads

When receiving request from another workload, validate it using validateRequest() method. Example:

import { NextFunction, Request } from 'express';
import { magellanicClient } from 'path/of/your/setup/file';

app.get('/example-workload-endpoint', async (req: Request, res: Response) => {
  try {
    await magellanicClient.validateRequest(req);
  } catch (err) {
    res.status(401).send(err.message);
  }
  // handle request as usual
  res.status(200).send(response);
});

License

Apache License 2.0

1.0.11

6 months ago

1.0.10

6 months ago

1.0.9

6 months ago

1.0.8

6 months ago

1.0.7

6 months ago

1.0.6

7 months ago

1.0.5

7 months ago

1.0.4

7 months ago

1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago