2.2.0 • Published 4 years ago

@bearer/node v2.2.0

Weekly downloads
290
License
MIT
Repository
github
Last release
4 years ago

@bearer/node

TODO: description

Usage

Call a Bearer intent

// somewhere in your application, we'll use an express route here
import clientFactory from '@bearer/node/lib/client'

const bearerClient = clientFactory(process.env.BEARER_SECRET_TOKEN)
// You can pass query or body parameter depending on Intent requirement
const options = { query: { status: 'open' }, body: { title: 'title' } }

bearerClient
  .call('1234-scenario-to-call', 'intentName', options)
  .then(() => {
    console.log('Successfully called intent')
  })
  .catch(() => {
    console.log('Something wrong happened')
  })

//async await wait
try {
  const reponse = await bearerClient.call('1234-scenario-to-call', 'intentName', options)
} catch (e) {
  // handler error
}
// play with response here

Note: we are using axios a http client. Each .call() returns an Axios Promise. https://github.com/axios/axios

Scenario client

Scenario client facilitates intent calls and prevent you to pass scenario name on every call

import { ScenarioClient } from '@bearer/node/lib/client'

const scenarioClient = new ScenarioClient(process.env.BEARER_SECRET_TOKEN, 'a-scenario-uuid')

const reponse = await scenarioClient.call('intentName', options)

If you are a Typescript user, you can provide a list of intents to use for a scenario:

const scenarioClient = new ScenarioClient<'intentName' | 'other-intent'>(
  process.env.BEARER_SECRET_TOKEN,
  'a-scenario-uuid'
)

scenarioClient.call('intentName', options) // OK
scenarioClient.call('other-intent', options) // OK
scenarioClient.call('unknow-intent', options) // Argument of type '"unknow-intent"' is not assignable to parameter of type 'TScenarioIntentNames'.

Use Bearer express webhook middleware

// your server.ts
import express from 'express'
import bearerWebhooks from '@bearer/node/lib/express'

const app = express()

// each valueS must be a fonction returning a promise
const webhookHandlers = {
  ['scenario-name-to_handle']: req =>
    new Promise(() => {
      // you logic goes here
      if (something) resolve()
      else {
        reject()
      }
    }),
  ['with-async-await']: async req => {
    // you logic goes here
    // ex: console.log(req.body)
    const reponse = await somethingYouWantToWaitFor
    if (response.success) {
      return whatever
    } else {
      throw new Error('An error occured')
    }
  }
}
// Without options
app.use('/whaterver_path_you_want/webhhoks', bearerWebhooks(webhookHandlers))

// With options
app.use('/whaterver_path_you_want/webhhoks', bearerWebhooks(webhookHandlers), { token: 'YOU_SECRET_TOKEN' })
2.2.0

4 years ago

2.1.1

4 years ago

2.1.0

5 years ago

2.0.1

5 years ago

2.0.1-alpha.0

5 years ago

2.0.1-test.4

5 years ago

2.0.0

5 years ago

1.13.10

5 years ago

1.13.9

5 years ago

1.13.8

5 years ago

1.13.7

5 years ago

1.13.4

5 years ago

1.13.0

5 years ago

1.11.0

5 years ago

1.8.0

5 years ago

1.7.1-canary.2

5 years ago

1.8.0-canary.2

5 years ago

1.7.0

5 years ago

1.6.8

5 years ago

1.6.6

5 years ago

1.6.5

5 years ago

1.6.4

5 years ago

1.6.2-canary-4.1

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.4.2-canary.24

5 years ago

1.5.0

5 years ago

1.4.2-canary.17

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.115.1

5 years ago

0.115.0

5 years ago

0.114.1

5 years ago

0.113.0

5 years ago

0.112.0

5 years ago

0.111.0-canary.9

5 years ago

0.111.0

5 years ago

0.111.0-canary.2

5 years ago

0.110.0

5 years ago

0.109.0

5 years ago

0.108.0

5 years ago

0.107.0

5 years ago

0.107.0-canary.1

5 years ago

0.107.0-canary.0

5 years ago

0.107.0-canary.6

5 years ago

0.106.0

5 years ago

0.106.0-canary.9

5 years ago

0.106.0-canary.5

5 years ago

0.106.0-canary.1

5 years ago

0.105.0

5 years ago

0.104.0

5 years ago

0.103.0

5 years ago

0.102.0

5 years ago

0.101.0

5 years ago

0.100.1-next.40

5 years ago

0.100.1-next.35

5 years ago

0.100.1-next.48

5 years ago

0.100.1-next.47

5 years ago

0.100.1-next.27

5 years ago

0.100.1-next.25

5 years ago

0.100.1-next.21

5 years ago

0.100.1-next.23

5 years ago

0.100.1-next.17

5 years ago

0.100.1-next.15

5 years ago

0.100.1-next.1

5 years ago

0.100.0

5 years ago

0.99.2

5 years ago

0.99.1

5 years ago

0.99.0

5 years ago

0.98.0

5 years ago

0.97.3

5 years ago

0.97.2

5 years ago

0.97.0

5 years ago

0.96.2-next.13

5 years ago

0.96.1

5 years ago

0.96.0

5 years ago

0.95.1

5 years ago

0.95.0

5 years ago

0.94.1

5 years ago

0.94.0

5 years ago

0.93.0

5 years ago

0.92.2

5 years ago

0.92.1

5 years ago

0.92.0

5 years ago

0.91.5

5 years ago

0.91.0

5 years ago

0.90.2

5 years ago

0.90.1

5 years ago

0.90.0

5 years ago

0.89.0

5 years ago

0.88.0

5 years ago

0.87.0

5 years ago

0.86.2

5 years ago

0.86.0

5 years ago

0.85.3

5 years ago

0.85.2

5 years ago

0.85.0

5 years ago

0.84.2

5 years ago

0.84.1

5 years ago

0.84.0

5 years ago

0.83.4

5 years ago

0.83.3

5 years ago

0.83.0

5 years ago

0.82.0

5 years ago

0.81.7

5 years ago

0.81.3

5 years ago

0.81.0

5 years ago

0.80.0

5 years ago

0.79.0

5 years ago

0.78.0

5 years ago

0.77.0

5 years ago

0.76.0

5 years ago

0.75.0

5 years ago