0.0.106 • Published 3 months ago

@indigov/zendesk-api v0.0.106

Weekly downloads
258
License
ISC
Repository
github
Last release
3 months ago

Zendesk API

The goal of this repo is to be a super-simple, strongly-typed interface to the Zendesk API.

Usage

import createClient, { Errors, Zendesk, jobCompletion, retry } from '@indigov/zendesk-api'

const api = createClient({
  subdomain: 'whatever',
  email: 'what@ever.com',
  token: 'token',
})

-or-

const api = createClient({
  subdomain: 'whatever',
  base64Token: btoa('what@ever.com/email:token'),
})

-or-

// if using AWS Parameter Store, make sure AWS creds + region are available
const api = createClient({
  subdomain: 'whatever',
  getAwsParameterStoreName: (subdomain) => `/tokens/${subdomain}`,
  awsRegion: 'us-east-2', // pass this specifically to avoid env usage
})

try {
  // tell the API what to expect in the result body
  const result = await api<Zendesk.PaginatedResults.Users>('/users')

  // now you can access the body as well as several useful attributes
  // result.body == {count: number, users: Zendesk.User[], next_page: string, previous_page: string}
  // result.rateLimit == number, how many requests have been recently made
  // result.rateLimitRemaining == number, how many requests can be made in the near future
  // result.retryAfter == number | null, how many seconds to wait before retrying
} catch (e) {
  // errors are all strongly-typed as well, and the result body will appear as properties of the error
  if (e instanceOf Errors.Authentication) {
    // e.error = "Couldn't authenticate you"
  }
  // rate-limit errors can be acted up on easily
  if (e instanceof Errors.RateLimit) {
    await new Promise(resolve => setTimeout(resolve, 1000 * 60))
    // now resume where you left off
  }
}

// you can fetch things from sunshine
const sunshineRes = await api<Zendesk.PaginatedResults.Sunshine.ObjectTypes>('/sunshine/objects/types')

// you can utilize a helper function to wait for jobs to complete
const jobRes = await jobCompletion({
  api,
  onProgress: async (progress: number) => {
    // do something with the progress number
  },
})('/users/create_many.json', {
  body: JSON.stringify({users: [{name: 'A user', email: 'email@email.com'}]}),
  method: 'POST',
})

// retry wrapper method (for rate-limit errors):
// simply wrap the method in "retry", move the generic outside of it, and wrap the function in another function (so that the promise isn't immediately invoked)
//
// before
const users = await api<Zendesk.PaginatedResults.Users>('/users')
// after
const users = await retry<Zendesk.PaginatedResults.Users>(() => api('/users'))

// you can also invoke the same logic in other helper methods:
await fastPaginate({ retryRateLimitErrors: true })
await createOrUpdateManyUsers({ retryRateLimitErrors: true })
await jobCompletion({ retryRateLimitErrors: true })('/thejob')

// there are helper methods available directly on the function itself
const user = await api.findUserByEmail('user@domain.com')

// there is a helper method to fetch all objects via cursor pagination
await zendeskAPI.fetchAll<Zendesk.User>({
  path: '/users?role=agent',
  bodyKey: 'users',
  onPage: async (users) => {
    // do something with a page of users here
  },
})

// there are helper methods to fetch all tickets or users between a start and end date (optional) using the incremental API
await zendeskAPI.fetchAllIncrementalTickets({
  startDate: '2022-01-01',
  endDate: '2022-02-03',
  onPage: async (tickets) => {
    // do something with a page of tickets here
  },
})
await zendeskAPI.fetchAllIncrementalUsers({
  startDate: '2022-01-01',
  onPage: async (users) => {
    // do something with a page of users here
  },
})

// you can access the underlying credentials
const { email, token, base64Token } = await api.getCreds()

Development

Setup

Set up .env

cp .env.example .env

Edit .env to fill out missing data

Testing

These are end to end tests against whatever account is configured in your .env. Expect them to fail if .env and the zendesk instance are not correctly configured.

yarn test

Publishing

Simply run npm publish and follow the prompts. IMPORTANT Do not run yarn publish, it is not compatible and will not include all files.

0.0.106

3 months ago

0.0.104

3 months ago

0.0.103

3 months ago

0.0.102

3 months ago

0.0.101

6 months ago

0.0.100

9 months ago

0.0.99

12 months ago

0.0.98

1 year ago

0.0.95

2 years ago

0.0.96

2 years ago

0.0.97

2 years ago

0.0.94

2 years ago

0.0.91

2 years ago

0.0.92

2 years ago

0.0.93

2 years ago

0.0.86

2 years ago

0.0.87

2 years ago

0.0.88

2 years ago

0.0.89

2 years ago

0.0.90

2 years ago

0.0.84

2 years ago

0.0.85

2 years ago

0.0.81

3 years ago

0.0.82

3 years ago

0.0.83

3 years ago

0.0.80

3 years ago

0.0.78

3 years ago

0.0.79

3 years ago

0.0.77

3 years ago

0.0.76

3 years ago

0.0.75

3 years ago

0.0.74

3 years ago

0.0.73

3 years ago

0.0.72

3 years ago

0.0.71

3 years ago

0.0.70

3 years ago

0.0.69

3 years ago

0.0.68

3 years ago

0.0.67

3 years ago

0.0.66

3 years ago

0.0.65

3 years ago

0.0.64

3 years ago

0.0.63

3 years ago

0.0.62

3 years ago

0.0.60

3 years ago

0.0.61

3 years ago

0.0.59

3 years ago

0.0.58

3 years ago

0.0.57

3 years ago

0.0.56

3 years ago

0.0.53

3 years ago

0.0.54

3 years ago

0.0.55

3 years ago

0.0.51

3 years ago

0.0.52

3 years ago

0.0.50

3 years ago

0.0.49

3 years ago

0.0.48

3 years ago

0.0.46

3 years ago

0.0.47

3 years ago

0.0.45

3 years ago

0.0.44

3 years ago

0.0.43

3 years ago

0.0.42

3 years ago

0.0.41

3 years ago

0.0.40

3 years ago

0.0.37

3 years ago

0.0.38

3 years ago

0.0.39

3 years ago

0.0.35

3 years ago

0.0.36

3 years ago

0.0.34

3 years ago

0.0.33

4 years ago

0.0.32

4 years ago

0.0.31

4 years ago

0.0.30

4 years ago

0.0.29

4 years ago

0.0.28

4 years ago

0.0.27

4 years ago

0.0.25

4 years ago

0.0.26

4 years ago

0.0.24

4 years ago

0.0.23

4 years ago

0.0.22

4 years ago

0.0.21

4 years ago

0.0.20

4 years ago

0.0.19

4 years ago

0.0.18

4 years ago

0.0.17

4 years ago

0.0.16

4 years ago

0.0.15

4 years ago

0.0.13

4 years ago

0.0.14

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago