Skip to content

Commit

Permalink
updated readme, fixed code segments
Browse files Browse the repository at this point in the history
  • Loading branch information
androozka committed Dec 26, 2019
1 parent 8832e4d commit c812570
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 29 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const { support, sunshine } = zdApi.init(options);
const { tickets, groups } = zdApi.support.init(options);

// Load specific endpoint
const endpoint = zdApi.support.tickets.init(options);
const { list, create } = zdApi.support.tickets.init(options);
```

## Examples
Expand All @@ -68,9 +68,11 @@ const endpoint = zdApi.support.tickets.init(options);

```javascript
try {
const tags = zdApi.support.tags.init(options);
const { tags } = zdApi.support.init(options);
const data = { tags: ['tag_1', 'tag_2', ... ] }

const req = tags.add({ type: 'tickets', id: 123, data });

const res = await axios(req);
} catch (error) {
// ...
Expand Down
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const init = (options = {}) => {

const initialized = {};
for (const api in zdAPIs) initialized[api] = zdAPIs[api].init(options);
// zdAPIs.forEach(api => (initialized[api] = zdAPIs[api].init(options)));

return initialized;
};
Expand Down
6 changes: 2 additions & 4 deletions src/utils/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,22 @@ module.exports = folder => {

// Load each endpoint file
const api = {};
endpoints.forEach(endpoint => {
for (const endpoint of endpoints)
api[endpoint] = require(path.resolve(
__dirname,
'../api',
folder,
endpoint
));
});

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

const initialized = {};
endpoints.forEach(endpoint => {
for (const endpoint of endpoints)
initialized[endpoint] = api[endpoint](options);
});

return initialized;
};
Expand Down
10 changes: 6 additions & 4 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ describe('zdApi', () => {
options = null;
});

it(`should load api "${folder}"`, () => {
expect(zdApi).toBeTruthy();
expect(zdApi.init).toBeTruthy();
expect(zdApi[folder]).toBeTruthy();
describe('api loading', () => {
it(`should prepare to load api "${folder}"`, () => {
expect(zdApi).toBeTruthy();
expect(zdApi.init).toBeTruthy();
expect(zdApi[folder]).toBeTruthy();
});
});

describe('init', () => {
Expand Down
3 changes: 1 addition & 2 deletions tests/src/api/support/custom_agent_roles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ describe('support: custom agent roles', () => {

describe('init', () => {
it('should setup endpoint object', () => {
const ep = endpoint(options);
expect(ep).toBeTruthy();
expect(endpoint(options)).toBeTruthy();
});

it('should fail with invalid input', () => {
Expand Down
3 changes: 1 addition & 2 deletions tests/src/api/support/tickets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ describe('object types', () => {

describe('init', () => {
it('should setup endpoint object', () => {
const ep = endpoint(options);
expect(ep).toBeTruthy();
expect(endpoint(options)).toBeTruthy();
});

it('should fail with invalid input', () => {
Expand Down
27 changes: 13 additions & 14 deletions tests/src/utils/load.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const load = require('../../../src/utils/load');

// Pick API endpoint for testing
const folder = 'support';
const endpoint = 'tickets';
const action = 'list';

describe('utils: load', () => {
let folder, endpoint, action, options, api;
let options, api;

beforeEach(() => {
// Pick API endpoint for testing
folder = 'support';
endpoint = 'tickets';
action = 'list';

options = {
instance: 'instance',
email: 'test@email.com',
Expand All @@ -20,18 +20,17 @@ describe('utils: load', () => {
});

afterEach(() => {
folder = null;
endpoint = null;
action = null;
options = null;
});

it(`should load api "${folder}"`, () => {
const api = load(folder);
describe('load api', () => {
it(`should load api "${folder}"`, () => {
const api = load(folder);

expect(api).toBeTruthy();
expect(api.init).toBeTruthy();
expect(api[endpoint]).toBeTruthy();
expect(api).toBeTruthy();
expect(api.init).toBeTruthy();
expect(api[endpoint]).toBeTruthy();
});
});

describe('init', () => {
Expand Down

0 comments on commit c812570

Please sign in to comment.