# @serverless/event-gateway-sdk

> Javascript library for interacting with the [Event Gateway](https://github.com/serverless/event-gateway).

Latest version **0.12.0** (published 2018-12-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install @serverless/event-gateway-sdk
pnpm add @serverless/event-gateway-sdk
yarn add @serverless/event-gateway-sdk
bun add @serverless/event-gateway-sdk
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.12.0 |
| Published | 2018-12-13 |
| First published | 2018-02-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.10 |
| Dependencies | 2 |
| Unpacked size | 227.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | mthenw, serverless-main |

## Links

- npm: https://www.npmjs.com/package/@serverless/event-gateway-sdk
- Repository: https://github.com/serverless/event-gateway-sdk
- Homepage: https://github.com/serverless/event-gateway-sdk#readme
- Issues: https://github.com/serverless/event-gateway-sdk/issues
- npm.io page: https://npm.io/package/@serverless/event-gateway-sdk

## Dependencies (2)

- [url-parse](https://npm.io/package/url-parse.md) ^1.4.3
- [isomorphic-fetch](https://npm.io/package/isomorphic-fetch.md) ^2.2.1

## Recent versions

- 0.12.0 (latest) — 2018-12-13
- 0.11.0 — 2018-11-08
- 0.10.2 — 2018-07-26
- 0.10.1 — 2018-06-28
- 0.10.0 — 2018-06-27
- 0.9.1 — 2018-06-21
- 0.9.0 — 2018-06-15
- 0.8.0 — 2018-06-15
- 0.7.0 — 2018-06-07
- 0.6.1 — 2018-05-15
- 0.6.0 — 2018-04-30
- 0.5.2 — 2018-04-30
- 0.5.0 — 2018-04-30
- 0.4.0 — 2018-04-26
- 0.3.0 — 2018-03-07
- … 2 more at https://npm.io/package/@serverless/event-gateway-sdk/versions

## README

# Event Gateway JavaScript SDK

Javascript library for interacting with the [Event Gateway](https://github.com/serverless/event-gateway).

[![Build Status](https://travis-ci.org/serverless/event-gateway-sdk.svg?branch=master)](https://travis-ci.org/serverless/event-gateway-sdk)
[![Known Vulnerabilities](https://snyk.io/test/github/serverless/event-gateway-sdk/badge.svg)](https://snyk.io/test/github/serverless/event-gateway-sdk)

## Contents

- [Background](#background)
- [Target Audience](#target-audience)
- [Installation](#installation)
- [Usage](#usage)
- [Constructor](#constructor)
- [API Reference](#api-reference)
- [Contributing](#contributing)

## Background

This is the Javascript SDK for interacting with the [Event Gateway](https://github.com/serverless/event-gateway), the hub for connecting events to serverless functions. It is designed to work both on the server with Node.js and in the browser.

## Target Audience

This SDK can be used both to *configure* the Event Gateway, by registering functions and subscriptions, and to *interact* with the Event Gateway, by emitting events from your application to be sent to subscribed functions.

This README is focused on the latter use case -- interacting with the Event Gateway by emitting events. If you're interested in using the Event Gateway SDK to configure the Event Gateway, please check the [API reference](./docs/api.md) for the available methods, as well as the main [Event Gateway repository](https://github.com/serverless/event-gateway). You may also be interested in using the [Event Gateway plugin](https://github.com/serverless/serverless-event-gateway-plugin) for the [Serverless Framework](https://github.com/serverless/serverless) to configure the Event Gateway.

## Installation

Node:

```bash
npm install @serverless/event-gateway-sdk
```

Browser:

```html
<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`

## Usage

Use the `emit` command to emit a [CloudEvent](https://github.com/cloudevents/spec) payload to your Event Gateway. The event will be received by any function that is subscribed to your event.

```javascript
// Construct your client
const SDK = require('@serverless/event-gateway-sdk');
const eventGateway = new SDK({
  url: 'https://mytenant-myapp.slsgateway.com',
})

// Emit your event
eventGateway
  .emit({
    eventID: '1',
    eventType: 'user.created',
    cloudEventsVersion: '0.1',
    source: '/services/users',
    contentType: 'application/json',
    data: {
      userID: 'foo'
    }
  }, {
    path: '/users',
    headers: {
      "Authorization": "Bearer 124567890"
    }
  })
  // If a sync subscription, then do something with the response.
  .then(res => res.json())
  .then(json => console.log(json))
```

The `emit()` function takes two arguments: an `event` which is a valid CloudEvent, plus an optional `options` object to include a path and/or headers to pass with your event.

The function returns a [`fetch`](https://github.com/bitinn/node-fetch) response object. If your event has a `sync` subscription attached, the `fetch` response will have the status code and body from the subscription. If not, the response will return a `202 Accepted` status code with an empty body.

## Constructor

In the example above, we created an Event Gateway client using the application URL from the [hosted Event Gateway](https://dashboard.serverless.com/) provided by Serverless, Inc. 

You can also use the Event Gateway SDK with your own, self-hosted Event Gateway. Constructor details are listed below.

**Parameters**

Object:

- `url` - `string` - required, Events API URL
- `configurationUrl` - `string` -  optional, Configuration API URL. By default, it's the same as `url` but with `4001` port
- `space` - `string` - optional, space name, default: `default`
- `accessKey` - `string` - optional, access key for hosted Event Gateway. Access key is required for using Configuration API methods on hosted Event Gateway
- `fetchClient` - `object` - optional, `fetch` client

**Example**

```js
const SDK = require('@serverless/event-gateway-sdk');
const eventGateway = new SDK({
  url: 'http://localhost',
  space: 'mycompany-prod',
  accessKey: '1234abcd'
})
```

## API Reference

For all available methods in the Event Gateway SDK, please see the [API reference](./docs/api.md).

## Contribute

If you are interested to contribute we recommend to check out the [Contributing](https://github.com/serverless/event-gateway-sdk/blob/master/CONTRIBUTING.md) document as it explains how to get started and some of the design decisions for this library.

---
_Source: https://npm.io/package/@serverless/event-gateway-sdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
