3.0.3 • Published 6 days ago

@meeco/sdk v3.0.3

Weekly downloads
282
License
MIT
Repository
github
Last release
6 days ago

Meeco TypeScript SDK

API Docs

For SDK API Docs (TypeDoc docs) See Here

Installation

$ npm install -S @meeco/sdk

Usage in Angular

Since Angular 6, the following polyfills are required to be added to your polyfills.ts (in order)

// required for bson library
(window as any).global = window;
// Required for JSRP
const buffer = require('buffer');
(window as any).Buffer = buffer;
(window as any).process = {
  browser: true
};

You will also need to configure your compilerOptions in your tsconfig.base.json as follows:

{
  "compilerOptions": {
    "paths": {
      "stream": ["./node_modules/readable-stream"]
    }
    // ...
  }
}

Usage Basics

This is not an exhaustive list of functionatlity - just some basic use-cases. For full usage see the API docs

All examples expect you have your environment configured.

An environment configuration looks something like this:

// Note this can also be an instance of Environment from `@meeco/sdk` for type safety
const environment = {
  vault: {
    url: '<target vault environment url>',
    subscription_key: '<your Meeco subscription key>
  }
  keystore: {
    url: '<target keystore environment url>',
    subscription_key: '<your Meeco subscription key>'
  }
}

TypeScript / JavaScript

All examples in this Readme use TypeScript. However, JavaScript should also work mostly the same, just replace any import statements with require statements depending on your module system.

e.g.

// TypeScript style imports
import { UserService } from '@meeco/sdk';
const service = new UserService();
// Node/JavaScript style imports
const Meeco = require('@meeco/sdk');
const service = new Meeco.UserService();

// or
const UserService = require('@meeco/sdk').UserService;
const service = new Meeco.UserService();

// or
const { UserService } = require('@meeco/sdk');
const service = new Meeco.UserService();

Configuring Fetch

The library requires a library that conforms to the Fetch API. Many modern browsers have this already but if you intend to support browsers that don't you will need to provide your own polyfill for it.

Likewise current LTS NodeJS does not have fetch built-in. We have tested the library to be working with the node-fetch polyfill

To configure the fetch library to be used for all library requests calls the configureFetch(fetch) method:

import { configureFetch } from '@meeco/sdk';
import * as fetchPolyfill from 'some-fetch-polyfill-library';

configureFetch(fetchPolyfill);

// Continue to use the Meeco SDK as normal...

Loggers

Most services allow a logger to be configured. The default for this is a no-op function (() => {}). If you would like to log message to the console, you can pass in console.log as the logger. You can pass in any function that takes strings as an argument (for exmaple, if you want to update a progress bar message).

API Factories

The SDK offers wrappers around the Meeco Vault API SDK and Keystore API SDK. These attach on common required headers for convenience when making requests. To use them, simply call the API factory with your environment configuration. You can then create factory instances by calling the factory with a user's authentication data and, finally, get the desired Model API off the result:

import { vaultAPIFactory, keystoreAPIFactory } from '@meeco/sdk';

const vaultFactory = vaultAPIFactory(environment);
const keystoreFactory = keystoreAPIFactory(environment);
const aliceVault = vaultFactory(aliceUser);
const bobKeystore = keystoreFactory(bobUser);

// Get api's off the factory instance
const UserApi = bobKeystore.UserApi;

// Alternatively, use object destructuring
const { ItemApi } = aliceVault;

Creating and Fetching Users

To perform most actions with Meeco we need a User - either an existing one or a new one we create.

In order to create a user we need a username (requested from the Keystore API), a secret (derived from the username) and a passphrase/password (entered by the user for account creation).

import { UserService, SecretService } from '@meeco/sdk';

const userService = new UserService(environment);
const secretService = new SecretService();

const username = await userService.generateUsername();
// This secret should be returned to the user for safe keeping
const secret = await secretService.generateSecret(username);
const user = await userService.create(password, secret);
// We now have the Meeco user `AuthData` to use for future calls and encryption.

In future, if we want to retrieve the user's AuthData we need them to provide their secret (which contains their username) and the passphrase.

import { UserService } from '@meeco/sdk';

const userService = new UserService(environment);
const user = await userService.get(password, secret);
// We have logged the user back in again and have the encryption keys we need

Dealing with Items

Items are typically created from a template (although you can create ones from a blank template as well). We can get a list of the available templates with the TemplateService

import { TemplateService } from '@meeco/sdk';

// Using a User we created or logged in with as above
const templatesService = new TemplatesService(environment, user.vault_access_token);
const availableTemplates = await templatesService.listTemplates();

/**
 * We now have a list of the available templates.
 * We can also view more information about one of the templates (such
 * as the slots that it provides)
 */

const templateDetails = await service.getTemplate(availableTemplates.item_templates[0].name);

Once we've selected a template we can create our first item.

import { ItemService } from '@meeco/sdk';

const service = new ItemService(environment);
const item = await service.create(user.vault_access_token, user.data_encryption_key, {
  template_name: availableTemplates.item_templates[0].name,
  item: {
    label: 'My Car'
  },
  slots: [
    {
      name: 'make_model',
      value: 'Ford Focus'
    },
    {
      name: 'year',
      value: '2017'
    }
  ]
});

We can also fetch a list of a user's items or get details about a specific item in a manner similar to templates:

import { ItemService } from '@meeco/sdk';

const service = new ItemService(environment);
// Get the user's items
const items = await service.list(user.vault_access_token);
// Get more details about a particular item
const itemDetails = await service.get(
  items[0].id,
  user.vault_access_token,
  user.data_encryption_key
);

Connecting Users and Sharing Data

A big part of Meeco is securely sharing data. In order to share data the users sharing the data must first be Connected.

import { ConnectionService } from '@meeco/sdk';

const connectionService = new ConnectionService(environment);

// Alice's Interaction, sending a connection invitation to Bob
const invitation = await connectionService.createInvitation('Bob', aliceUser);
// It is up to Alice to share `invitation.token` with Bob so he can accept it (e.g. via Email or SMS)

// Bob's interaction - accepting the invitation (providing the token he got from Alice)
const connection = await connectionService.acceptInvitation('Alice', invitationToken, bobUser);

Alice and Bob are now connected and can share data. Let's share one of Alice's items with Bob.

import { ShareService } from '@meeco/sdk';

const shareService = new ShareService(environment);

/**
 * Assuming we have connectionWithBob from ConnectionService.listConnections
 * and an aliceItem from ItemService.list
 */
const share = await shareService.shareItem(alice, connetionWithBob.id, aliceItem.id);

// Bob should now be able to fetch this item
const item = await shareService.getSharedItem(bob, sharedItem.id);

SDK Development

Generating Documentation

npm run docs to run TypeDoc. Note at time of writing we use the "library" mode for typedoc which requires the beta version (npm i typedoc@next)

Running the demo

There is a sample website that uses cryppo to perform various functions.

  1. Bootstrap the root project (npm run bootstrap in project root)
  2. npm run demo
  3. Visit http://localhost:1234

Note environment settings are saved to local storage for convenience

5.1.0-beta

6 days ago

5.0.0-beta

3 months ago

4.25.3-beta

4 months ago

4.25.2-beta

4 months ago

4.25.0-beta

5 months ago

4.25.1-beta

5 months ago

4.24.0-beta

5 months ago

4.23.0-beta

5 months ago

4.13.0-beta

7 months ago

4.17.0-beta

7 months ago

4.11.8-beta

8 months ago

4.21.0-beta

6 months ago

4.20.0-beta

7 months ago

4.11.4-beta

9 months ago

4.11.7-beta

8 months ago

4.11.5-beta

9 months ago

4.11.6-beta

9 months ago

4.15.0-beta

7 months ago

4.19.0-beta

7 months ago

4.22.0-beta

6 months ago

4.16.0-beta

7 months ago

4.14.0-beta

7 months ago

4.18.0-beta

7 months ago

4.12.0-beta

8 months ago

4.11.3-beta

11 months ago

4.9.9-beta

1 year ago

4.9.6-beta

1 year ago

4.11.2-beta

12 months ago

4.9.8-beta

1 year ago

4.9.7-beta

1 year ago

4.11.1-beta

12 months ago

4.10.1-beta

1 year ago

4.11.0-beta

1 year ago

4.10.0-beta

1 year ago

4.9.3-beta

1 year ago

4.7.3-beta

1 year ago

4.9.0-beta

1 year ago

4.7.2-beta

1 year ago

4.9.2-beta

1 year ago

4.8.0-beta

1 year ago

4.9.1-beta

1 year ago

4.9.4-beta

1 year ago

4.7.4-beta

1 year ago

4.9.5-beta

1 year ago

4.7.0-beta

1 year ago

4.4.4-beta

1 year ago

4.6.0-beta

1 year ago

4.7.1-beta

1 year ago

4.5.0-beta

1 year ago

4.3.3-beta

1 year ago

4.2.7-beta

1 year ago

4.2.4-beta

1 year ago

4.2.2-beta

1 year ago

4.2.8-beta

1 year ago

4.1.8-beta

2 years ago

4.4.2-beta

1 year ago

4.2.9-beta

1 year ago

4.1.9-beta

2 years ago

4.2.3-beta

1 year ago

4.4.3-beta

1 year ago

4.1.7-beta

2 years ago

4.4.1-beta

1 year ago

4.2.5-beta

1 year ago

4.2.1-beta

1 year ago

4.2.6-beta

1 year ago

4.1.6-beta

2 years ago

4.4.0-beta

1 year ago

4.2.0-beta

2 years ago

4.3.0-beta

1 year ago

4.1.5-beta

2 years ago

4.1.4-beta

2 years ago

4.1.3-beta

2 years ago

4.1.0-beta

2 years ago

4.1.2-beta

2 years ago

4.0.2-beta

3 years ago

3.0.3

3 years ago

4.0.1-beta

3 years ago

4.0.0-beta

3 years ago

3.0.2

3 years ago

3.0.2-beta

3 years ago

4.0.0-alpha

3 years ago

3.0.1-beta

3 years ago

3.0.0-beta

3 years ago

2.0.2

3 years ago

2.2.3

3 years ago

2.2.2

3 years ago

2.2.2-beta

3 years ago

2.2.0

3 years ago

2.1.0

3 years ago

2.1.0-beta.2

3 years ago

2.1.0-beta.1

3 years ago

2.1.0-beta

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

2.0.0-beta.3

3 years ago

2.0.2-beta

3 years ago

2.0.1-beta

3 years ago

2.0.0-beta

3 years ago

1.0.0

4 years ago

0.0.13-alpha

4 years ago

0.0.12-alpha.2

4 years ago

0.0.12-alpha

4 years ago

0.0.11-beta

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago