Skip to content

Commit

Permalink
started sunshine, full package overhall
Browse files Browse the repository at this point in the history
  • Loading branch information
androozka committed Dec 23, 2019
1 parent 008c302 commit 74cb3e7
Show file tree
Hide file tree
Showing 92 changed files with 621 additions and 4,297 deletions.
60 changes: 30 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,50 @@ A JS library for interacting with the Zendesk API.
- Send prepared object directly to axios 🤯
- Mirrors [Zendesk's API documentation](https://developer.zendesk.com/rest_api/docs/zendesk-apis/resources) 👀

## Installing
_**Note**: Only v2 framework, [v1 deprecated](https://support.zendesk.com/hc/en-us/articles/360002106888-Removal-of-Zendesk-Apps-framework-v1)_

### Using npm
## Installing

```bash
# using npm
npm install @androozka/zendesk-api-js
```

### Using yarn

```bash
# using yarn
yarn add @androozka/zendesk-api-js
```

## Usage

### Suggested libraries
### Getting Started

```javascript
const base64 = require("js-base64").Base64;
const axios = require("axios");
const axios = require('axios'); // Suggested library
const zdApi = require('@androozka/zendesk-api-js');
```

### Zendesk account information
### Zendesk Info

```javascript
const instance = ""; // Name of Zendesk instance
const email = ""; // Email address of Zendesk user
const token = ""; // Generated Zendesk token

const encoded = base64.encode(`${email}/token:${token}`);

const headers = {
"Content-Type": "application/json",
Authorization: `Basic ${encoded}`
const options = {
instance: '', // Zendesk subdomain
email: '', // User account to perform requests
password: '', // Password for user account
token: '' // Generated Zendesk token
};
/* Note: Either "password" or "token" is required */
```

### Choose framework version
### Initalize

```javascript
const zdApi = require("@androozka/zendesk-api-js");
const zaf_v2 = zdApi.v2({ instance, headers });
```

- _**Note**: Only v2, [v1 being deprecated](https://support.zendesk.com/hc/en-us/articles/360002106888-Removal-of-Zendesk-Apps-framework-v1)_
// Load entire library
const { support, sunshine } = zdApi.init(options);

### Selecting API endpoints
// Load entire API
const { tickets, groups } = zdApi.support.init(options);

```javascript
const { support, sunshine } = zaf_v2;
// Load specific endpoint
const endpoint = zdApi.support.tickets.init(options);
```

## Examples
Expand All @@ -75,8 +68,9 @@ const { support, sunshine } = zaf_v2;

```javascript
try {
const tags = zdApi.support.tags.init(options);
const data = { tags: ['tag_1', 'tag_2', ... ] }
const req = support.tags.add({ type: 'tickets', id: 123, data });
const req = tags.add({ type: 'tickets', id: 123, data });
const res = await axios(req);
} catch (error) {
// ...
Expand All @@ -89,7 +83,13 @@ try {

Search, Users, End Users, Groups, Group Memberships, Custom Agent Roles, Organizations, Organization Subscriptions, Organization Memberships, Tickets, Ticket Import, Suspended Tickets, Ticket Comments, Ticket Metrics, Ticket Activities, Tags, Views, Ticket Forms, Ticket Fields, User Fields, Organization Fields

#### Under Construction
### Sunshine API

Custom Object Types, Custom Object Records

### Under Construction

#### Support

- [ ] Brands
- [ ] User Identities
Expand Down
21 changes: 19 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
module.exports = {
v2: ({ instance, headers }) => require('./src/v2')({ instance, headers })
// array list for each folder in src
const fs = require('fs');
const validate = require('./src/validate');

// Load all APIs
const zdAPIs = {};
const endpoints = fs.readdirSync('./src').filter(i => i !== 'validate.js');
endpoints.forEach(i => (zdAPIs[i] = require(`./src/${i}`)));

const init = (options = {}) => {
const { error } = validate(options);
if (error) throw new Error(error.details[0].message);

const api = {};
endpoints.forEach(i => (api[i] = zdAPIs[i].init(options)));

return api;
};

module.exports = { init, ...zdAPIs };
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"src/*"
],
"dependencies": {
"@hapi/joi": "^16.1.1"
"@hapi/joi": "^16.1.1",
"js-base64": "^2.5.1"
},
"devDependencies": {
"@types/jest": "^24.0.18",
Expand Down
178 changes: 178 additions & 0 deletions src/api/sunshine/object_records.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
const Joi = require('@hapi/joi');
const { validate, prepare } = require('../../utils/options');

// Validation
const _object_type = Joi.string().min(1);
const _type = Joi.string().min(1);
const _id = Joi.number().min(1);
const _ids = Joi.string().min(1);
const _relationship_type = Joi.string().min(1);
const _external_id = Joi.string().min(1);
const _external_ids = Joi.string().min(1);
const _data = Joi.object();

// Initialize Endpoint
module.exports = (options = {}) => {
const { error } = validate(options);
if (error) throw new Error(error.details[0].message);

const { url, headers } = prepare(options);

return {
/**
* List Object Records (by Object Type, IDs, or Relationship)
*
* GET /api/sunshine/objects/records?type={object_type}
* https://developer.zendesk.com/rest_api/docs/sunshine/resources#list-object-records-by-type
*
* GET /api/sunshine/objects/records?ids={id, id, ...}
* https://developer.zendesk.com/rest_api/docs/sunshine/resources#list-object-records-by-id
*
* GET /api/sunshine/objects/records/{id}/related/{relationship_type}
* https://developer.zendesk.com/rest_api/docs/sunshine/resources#list-related-object-records
*/
list: (options = {}) => {
const { error } = Joi.object({
object_type: _object_type,
ids: _ids,
id: _id,
relationship_type: _relationship_type
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { object_type, ids, id, relationship_type } = options;
if (!object_type && !ids && !(id && relationship_type))
throw new Error(
'object_type, ids, or id with relationship_type required'
);

return {
method: 'GET',
url: `${url}/api/sunshine/objects/records${
object_type
? `?type=${object_type}`
: ids
? `?ids=${ids}`
: `/${id}/related/${relationship_type}`
}`,
headers
};
},

/**
* Show Object Record (by ID, External ID, or External IDs)
*
* GET /api/sunshine/objects/records/{id}
* https://developer.zendesk.com/rest_api/docs/sunshine/resources#show-object-record
*
* GET /api/sunshine/objects/records?type={object_type}&external_id={id}
* GET /api/sunshine/objects/records?type={object_type}&external_ids={id, id, ...}
* https://developer.zendesk.com/rest_api/docs/sunshine/resources#show-object-record-by-external-id
*
*/
show: (options = {}) => {
const { error } = Joi.object({
id: _id,
object_type: _object_type,
external_id: _external_id,
external_ids: _external_ids
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { id, object_type, external_id, external_ids } = options;
if (!id && !object_type) throw new Error('id or object_type required');
if (object_type && !external_id && !external_ids)
throw new Error(
'object_type requires either external_id or external_ids set'
);

return {
method: 'GET',
url: `${url}/api/sunshine/objects/records${
id
? `/${id}`
: `?type=${object_type}&external_id${
external_id ? `=${external_id}` : `s=${external_ids}`
}`
}`,
headers
};
},

/**
* Create Object Record
*
* POST /api/sunshine/objects/records
* https://developer.zendesk.com/rest_api/docs/sunshine/resources#create-object-record
*/
create: (options = {}) => {
const { error } = Joi.object({
data: _data.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { data } = options;
return {
method: 'POST',
url: `${url}/api/sunshine/objects/records`,
headers,
data
};
},

/**
* Update Object Record (or Set by External ID)
*
* PATCH /api/sunshine/objects/records/{id}
* https://developer.zendesk.com/rest_api/docs/sunshine/resources#update-object-record
*
* PATCH /api/sunshine/objects/records
* https://developer.zendesk.com/rest_api/docs/sunshine/resources#set-object-record-by-external-id
*/
update: (options = {}) => {
const { error } = Joi.object({
id: _id,
data: _data.required()
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { id, data } = options;
return {
method: 'PATCH',
url: `${url}/api/sunshine/objects/records${id ? `/${id}` : ''}`,
headers,
data
};
},

/**
* Delete Object Record
*
* DELETE /api/sunshine/objects/records/{id}
* https://developer.zendesk.com/rest_api/docs/sunshine/resources#delete-object-record
*
* DELETE /api/sunshine/objects/records?external_id={external_id}&type={type}
* https://developer.zendesk.com/rest_api/docs/sunshine/resources#delete-object-record-by-external-id
*/
delete: (options = {}) => {
const { error } = Joi.object({
id: _id,
external_id: _external_id,
type: _type
}).validate(options);
if (error) throw new Error(error.details[0].message);

const { id, external_id, type } = options;
if (!id && !(external_id && type))
throw new Error('id or external_id with type required');

return {
method: 'DELETE',
url: `${url}/api/sunshine/objects/records${
id ? `/${id}` : `?external_id=${external_id}&type=${type}`
}`,
headers
};
}
};
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const Joi = require('@hapi/joi');
const { validate, prepare } = require('../../utils/options');

// Validation
const _key = Joi.string()
.min(2)
.max(32);
Expand All @@ -12,10 +14,20 @@ const _data = Joi.object({
end_users_can_read: Joi.bool()
});

module.exports = ({ instance, headers }) => {
const url = `https://${instance}.zendesk.com`;
// Initialize Endpoint
module.exports = (options = {}) => {
const { error } = validate(options);
if (error) throw new Error(error.details[0].message);

const { url, headers } = prepare(options);

return {
/**
* List Object Types
*
* GET /api/sunshine/objects/types
* https://developer.zendesk.com/rest_api/docs/sunshine/resource_types#list-object-types
*/
list: () => {
// Ignore any options
return {
Expand All @@ -25,6 +37,12 @@ module.exports = ({ instance, headers }) => {
};
},

/**
* Show Object Type
*
* GET /api/sunshine/objects/types/{key}
* https://developer.zendesk.com/rest_api/docs/sunshine/resource_types#show-object-type
*/
show: (options = {}) => {
const { error } = Joi.object({
key: _key.required()
Expand All @@ -39,6 +57,10 @@ module.exports = ({ instance, headers }) => {
};
},

/**
* Create Object Type
* https://developer.zendesk.com/rest_api/docs/sunshine/resource_types#create-object-type
*/
create: (options = {}) => {
const { error } = Joi.object({
data: _data.required()
Expand All @@ -54,6 +76,10 @@ module.exports = ({ instance, headers }) => {
};
},

/**
* Update Object Type
* https://developer.zendesk.com/rest_api/docs/sunshine/resource_types#update-object-type
*/
update: (options = {}) => {
const { error } = Joi.object({
key: _key.required(),
Expand All @@ -70,6 +96,10 @@ module.exports = ({ instance, headers }) => {
};
},

/**
* Delete Object Type
* https://developer.zendesk.com/rest_api/docs/sunshine/resource_types#delete-object-type
*/
delete: (options = {}) => {
const { error } = Joi.object({
key: _key.required()
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 74cb3e7

Please sign in to comment.