Giter Club home page Giter Club logo

event-gateway-sdk's Introduction

Event Gateway JavaScript SDK

JavaScript library for interacting with the Event Gateway.

Build Status

Contents

Installation

Node:

npm install @serverless/event-gateway-sdk

Browser:

<script type="text/javascript" src="https://unpkg.com/@serverless/event-gateway-sdk@latest/dist/event-gateway-sdk.min.js"></script>

The EventGateway SDK will then be attached to window e.g. and you can access it via window.EventGatewaySDK

Application Usage

When using in your application logic, you'll usually interact with the invoke and emit APIs.

Use the invoke command to synchronously invoke backend function by name. This is similar to an RPC call.

// Construct your client
const SDK = require('@serverless/event-gateway-sdk');
const eventGateway = new SDK({
  url: 'http://myeventgateway.io',
  space: 'prod'
})

// Call your function
eventGateway.invoke({
  functionId: 'users.getUsers',
  data: { 'limit': 100 }
}).then((resp) => resp.json())
  .then((users) => console.log(users))

Use the emit command to emit a named event and payload to your Event Gateway. The event will be received by any function that is subscribed to your event.

// Construct your client
const SDK = require('@serverless/event-gateway-sdk');
const eventGateway = new SDK({
  url: 'http://myeventgateway.io',
  space: 'prod'
})

// Emit your event
eventGateway.emit({
  event: 'user.completedTutorial'
  data: { 'userId': 1234 }
})

API Reference

Constructor

Parameters

Object:

  • url - string - required, Events API URL
  • configurationUrl - string - Configuration API URL. By default, it's the same as url but with 4001 port
  • space - string - Space, default: default
  • apikey - string - API key for hosted Event Gateway.
  • fetchClient - object - fetch client
const SDK = require('@serverless/event-gateway-sdk');
const eventGateway = new SDK({
  url: 'http://localhost',
  space: 'mycompany-prod',
  apikey: '1234abcd'
})

Configuration APIs

List Functions

Returns

Promise object resolving to array of function objects

eventGateway.listFunctions()

Register Function

Parameters

Object:

  • functionId - string - function ID
  • provider - object - provider spec

For more details see Event Gateway Register Functions docs.

Returns

Promise object resolving to Function object

eventGateway.registerFunction({
  functionId: 'sendEmail',
  provider: {
    type:'awslambda',
    arn: 'xxx',
    region: 'us-west-2',
  }
})

Delete Function

Parameters

Object:

  • functionId - string - function ID
eventGateway.deleteFunction({ functionId: 'sendEmail' })

List Subscriptions

Returns

Promise object resolving to array of subscription objects

eventGateway.listSubscriptions()

Subscribe

Parameters

Object:

  • event - string - event type
  • functionId - string - function ID
  • path - string - optional, subscription path, default: /
  • method - string - required for HTTP subscription, HTTP method
  • cors - object - optional for HTTP subscriptions, CORS configuration

For more details see Event Gateway Create Subscription docs.

Returns

Promise object resolving to Subscription object

eventGateway.subscribe({
  event: 'user.created',
  functionId: 'sendEmail'
})

Unsubscribe

Parameters

Object:

  • subscriptionId - string - subscription ID
eventGateway.unsubscribe({
  subscriptionId: 'user.created-sendEmail'
})

Events API

Emit

Parameters

Object:

  • event - string - Name of event to emit
  • data - object or string - Payload to include with event. If dataType is "application/json", data will be stringified before sending.
  • dataType - string - Data type of payload. Default is "application/json"

Returns

fetch response object.

eventGateway.emit({
  event: 'user.completedTutorial'
  data: { 'userId': 1234 }
})

Invoke

Parameters

Object:

  • functionId - string - Name of function to invoke
  • data - object or string - Payload to include with invocation. If dataType is "application/json", data will be stringified before sending.
  • dataType - string - Data type of payload. Default is "application/json"

Returns

fetch response object.

eventGateway.invoke({
  functionId: 'users.getUsers'
  data: { 'limit': 100 }
}).then((resp) => resp.json())
  .then((users) => console.log(users))

Contribute

If you are interested to contribute we recommend to check out the Contributing document as it explains how to get started and some of the design decisions for this library.

event-gateway-sdk's People

Contributors

nikgraf avatar mthenw avatar raeesbhatti avatar pmuens avatar alexdebrie avatar brianneisler avatar flomotlik avatar

Watchers

James Cloos avatar Liam Ellis avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.