0.9.431-0 • Published 17 days ago

@hexagramio/saga-ts v0.9.431-0

Weekly downloads
-
License
MIT
Repository
-
Last release
17 days ago

Table of Contents

Saga Typescript SDK

Install

npm install @hexagramio/saga-ts

Description

Typescript SDK to interface with a Saga API. The implementation is inspired by the Command Design Pattern. A command is send to a Reveiver. A command encapsulates all the data needed to call the API except the domain name. Currently there two kinds of command receivers, HTTP and Socket. Almost all commands except log in and some user creation methods require a accessToken. If a command fails an exception is being thrown that mirrors SAGA API errors.

SDK Documentation

Fundamental Types

Both HTTPCommand and SocketCommand below show the entire complexity of both types. Easy to adopt for specialized and new commands or to use for HTTP Scripts.

HTTPCommand

/**
 * Interfaces to represent the various mutations of a HTTP Command profile.
 */
export interface HTTPGetCommand<T> {
  method: "GET" ,
  path: string,
  params?: URLSearchParams,
  accessToken?: string
}
export interface HTTPPostPutCommand<T> {
  method: "PUT" | "POST",
  path: string,
  data: any ,
  accessToken?: string
}

export interface HTTPDeleteCommand<T> {
  method: "DELETE",
  path: string,
  params?: URLSearchParams,
  accessToken?: string
}

export type HTTPCommand<T> = HTTPGetCommand<T> | HTTPPostPutCommand<T> | HTTPDeleteCommand<T>

Example Custom Command

Sends HTTP post to requests/slack with the assembled body.

/**
 * Get a bot command
 * @param id the id of the bot
 * @param accessToken the required accessToken
 * @group HTTP Commands
 */
export const PostSlackMessageCommand = (accessToken: string, slack_id: string, slack_channel: string, message: string):HTTPCommand<{ok:boolean}> =>{
  return {
    method:"POST",
    path:`requests/slack`,
    accessToken,
    data: {slack_id,slack_channel,message}
  }
}

SocketCommand

/**
 * A socket join command
 */
export interface SocketJoinCommand {
    method: "/users" | "/bots" | "/globals" |
    "/jobs/runnable_calls" | "/jobs/versions" |
    "/scripts/runnable_calls" | "/scripts/versions" |
    "/system" | "/npm_packages"
    id: string, 
    join: boolean,
  }

Quickstart

For the unhurried among us ;)

Register User

import {
  RegisterUserCommand,
  sendHTTPCommand} from "saga-ts";

const baseURL=new URL("https://saga-api.com");
const user = await sendHTTPCommand(baseUrl,RegisterUserCommand({username:`foo`,password:`bar`}))

Login User and Add User Property

import {
  AddUserPropertyCommand,
  LoginUserCommand,
  sendHTTPCommand} from "saga-ts";

const baseURL=new URL("https://saga-api.com");
const user = await sendHTTPCommand(baseUrl,LoginUserCommand({username:`foo`,password:`bar`}))
await sendHTTPCommand(baseUrl,AddUserPropertyCommand(user.accessToken,{parent_id:user._id,name:"hello",value:"world"}))

Paginate Listing, example using Listing

import {
  ListBotsCommand,
  sendHTTPCommand} from "saga-ts";

const baseURL=new URL("https://saga-api.com");
const accessToken="123213132";//from cache
const botsListing = await sendHTTPCommand(baseUrl,ListBotsCommand(accessToken))

//do we have more? nextCommand contains all the data needed for the call
if (botsListing.nextCommand) {
  const botsNext = await sendHTTPCommand(baseUrl, botsListing.nextCommand);
}

//OR

//do we have some previous? prevcommand contains all the data needed for the call
if (botsListing.prevCommand) {
  const botsPrev = await sendHTTPCommand(baseUrl, botsListing.prevCommand);

Handle HTTP Error

import {
  ListUserCommand,
  RegisterUserCommand,
  sendHTTPCommand} from "saga-ts";

const baseURL=new URL("https://saga-api.com");
const accessToken="89012y417114211";//from cache
try{
  await sendHTTPCommand(baseUrl,CreateBot(user.accessToken,{name:"ALREADY TAKEN"}))  
} catch(e){
  if(e.statusCode===422){
    //e.errors contains [fieldName:string]: message
    //handle the specific error messages
  }
  //...
}

Inspect Missing Fields in failed HTTP Call

import {
  ListUserCommand,
  RegisterUserCommand,
  sendHTTPCommand} from "saga-ts";

const baseURL=new URL("https://saga-api.com");
const user = await sendHTTPCommand(baseUrl,RegisterUserCommand({username:`foo`,password:`bar`}))
try{
  await sendHTTPCommand(baseUrl,ListUserCommand(user.accessToken,{parent_id:user._id,name:"hello",value:"world"}))  
} catch(e){
  //handle 401 and 403 for
}

Subscribe to Socket Bot Property Changes

import {
  Authentication,
  SocketSession} from "saga-ts";

const baseURL=new URL("https://saga-api.com");
const socketSession = new SocketSession(
  baseUrl,
  accessToken, //assumed existing
  (error)=>console.error //disconnect errors need to be handled asynchronously
)
socketSession.on("/properties", (property)=>{
  //do something with propterties
  if(property.name="location"){
    updateMapLocation(property);
  }//....
})
//join bot command
await socketSession.emitCommand(JoinBotCommand("ID OF BOT TO JOIN"))

Handle Socket Emit Comand Errors

import {
  Authentication,
  SocketSession} from "saga-ts";

const baseURL=new URL("https://saga-api.com");
const socketSession = new SocketSession(
  baseUrl,
  accessToken, //assumed existing
  (error)=>console.error //disconnect errors need to be handled asynchronously
)

try{
  await socketSession.emitCommand(JoinBotCommand("Not access to id"))  
} catch(e){
  //deal with API Error like 401,403,404 and 422 depending on the comand
}

Handle Socket Connection Error

import {SocketSession} from "saga-ts";
const baseURL=new URL("https://saga-api.com");
new SocketSession(
    baseUrl,
    "bad token",
    (err)=>{
      //handleError
    }
)
0.9.431-0

17 days ago

0.9.428-0

22 days ago

0.9.429-0

22 days ago

0.9.430-0

22 days ago

0.9.427-0

22 days ago

0.9.426-0

24 days ago

0.9.425-0

25 days ago

0.9.424-0

1 month ago

0.9.422-0

1 month ago

0.9.423-0

1 month ago

0.9.421-0

1 month ago

0.9.420-0

1 month ago

0.9.419-0

1 month ago

0.9.418-0

2 months ago

0.9.417-0

2 months ago

0.9.416-0

3 months ago

0.9.415-0

3 months ago

0.9.413-1

4 months ago

0.9.413-0

4 months ago

0.9.414-0

4 months ago

0.9.412-0

4 months ago

0.9.410-0

4 months ago

0.9.411-0

4 months ago

0.9.409-0

4 months ago

0.9.408-0

4 months ago

0.9.405-0

4 months ago

0.9.406-0

4 months ago

0.9.402-0

4 months ago

0.9.407-0

4 months ago

0.9.403-0

4 months ago

0.9.404-0

4 months ago

0.9.401-0

4 months ago

0.9.400-0

4 months ago

0.9.398-0

4 months ago

0.9.399-0

4 months ago

0.9.397-0

5 months ago

0.9.396-0

5 months ago

0.9.395-0

5 months ago

0.9.393-0

5 months ago

0.9.392-0

5 months ago

0.9.394-0

5 months ago

0.9.391-0

5 months ago

0.9.390-0

5 months ago

0.9.389-0

5 months ago

0.9.388-0

5 months ago

0.9.387-0

5 months ago

0.9.386-0

5 months ago

0.9.385-0

5 months ago

0.9.384-0

5 months ago

0.9.382-0

5 months ago

0.9.383-0

5 months ago

0.9.318-0

9 months ago

0.9.375-0

7 months ago

0.9.352-0

8 months ago

0.9.337-0

9 months ago

0.9.314-0

9 months ago

0.9.379-0

7 months ago

0.9.356-0

8 months ago

0.9.333-0

9 months ago

0.9.310-0

10 months ago

0.9.340-0

9 months ago

0.9.306-0

10 months ago

0.9.329-0

9 months ago

0.9.344-0

9 months ago

0.9.302-0

10 months ago

0.9.295-0

10 months ago

0.9.325-0

9 months ago

0.9.363-0

8 months ago

0.9.348-0

9 months ago

0.9.367-0

8 months ago

0.9.321-0

9 months ago

0.9.351-0

8 months ago

0.9.370-0

8 months ago

0.9.317-0

9 months ago

0.9.355-0

8 months ago

0.9.313-0

9 months ago

0.9.374-0

7 months ago

0.9.336-0

9 months ago

0.9.359-0

8 months ago

0.9.378-0

7 months ago

0.9.332-0

9 months ago

0.9.362-0

8 months ago

0.9.299-0

10 months ago

0.9.381-0

6 months ago

0.9.309-0

10 months ago

0.9.328-0

9 months ago

0.9.305-0

10 months ago

0.9.366-0

8 months ago

0.9.343-0

9 months ago

0.9.324-0

9 months ago

0.9.301-0

10 months ago

0.9.294-0

11 months ago

0.9.347-0

9 months ago

0.9.320-0

9 months ago

0.9.373-0

7 months ago

0.9.350-0

8 months ago

0.9.339-0

9 months ago

0.9.316-0

9 months ago

0.9.377-0

7 months ago

0.9.354-0

8 months ago

0.9.335-0

9 months ago

0.9.312-0

9 months ago

0.9.358-0

8 months ago

0.9.331-0

9 months ago

0.9.380-0

6 months ago

0.9.361-0

8 months ago

0.9.298-0

10 months ago

0.9.327-0

9 months ago

0.9.308-0

10 months ago

0.9.365-0

8 months ago

0.9.323-0

9 months ago

0.9.342-0

9 months ago

0.9.304-0

10 months ago

0.9.369-0

8 months ago

0.9.346-0

9 months ago

0.9.300-0

10 months ago

0.9.372-0

7 months ago

0.9.338-0

9 months ago

0.9.319-0

9 months ago

0.9.376-0

7 months ago

0.9.334-0

9 months ago

0.9.353-0

8 months ago

0.9.315-0

9 months ago

0.9.330-0

9 months ago

0.9.293-10

11 months ago

0.9.357-0

8 months ago

0.9.311-0

9 months ago

0.9.360-0

8 months ago

0.9.297-0

10 months ago

0.9.349-0

9 months ago

0.9.307-0

10 months ago

0.9.296-0

10 months ago

0.9.364-0

8 months ago

0.9.341-0

9 months ago

0.9.326-0

9 months ago

0.9.303-0

10 months ago

0.9.368-0

8 months ago

0.9.345-0

9 months ago

0.9.322-0

9 months ago

0.9.371-0

8 months ago

0.9.293-9

11 months ago

0.9.293-8

11 months ago

0.9.293-6

11 months ago

0.9.293-5

11 months ago

0.9.293-4

11 months ago

0.9.293-3

11 months ago

0.9.293-2

11 months ago

0.9.293-1

11 months ago

0.9.293

11 months ago