-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from androozka/sprint-5
Sprint 5
- Loading branch information
Showing
135 changed files
with
9,633 additions
and
7,207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,24 @@ | ||
module.exports = { | ||
v2: ({ instance, headers }) => require('./src/v2')({ instance, headers }) | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { validate } = require('./src/utils/options'); | ||
const load = require('./src/utils/load'); | ||
|
||
// Read folders to list APIs ('/src/api/*') | ||
const APIs = fs.readdirSync(path.resolve(__dirname, 'src/api')); | ||
|
||
// Load each API (support, sunshine, ...) | ||
const zdAPIs = {}; | ||
APIs.forEach(api => (zdAPIs[api] = load(api))); | ||
|
||
// Initialize each API | ||
const init = (options = {}) => { | ||
const { error } = validate(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const initialized = {}; | ||
for (const api in zdAPIs) initialized[api] = zdAPIs[api].init(options); | ||
|
||
return initialized; | ||
}; | ||
|
||
module.exports = { init, ...zdAPIs }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
}; | ||
}; |
Oops, something went wrong.