Skip to content

Commit c6a4ef8

Browse files
committed
fix: refactor implementation and keep stable interface
This is a refactor proposal that will make it easier to understand and develop further this library. It uses a configuration for the endpoints and the `axios` library for the HTTP requests instead of the custom implementation that relied on the `http` and `https` node modules. This version keeps the public interface the same to not break backwards compatibility and has feature parity with the existing implementation. It also fixes an issue with processing requests that have more items and iteration should be used.
1 parent 3834ccf commit c6a4ef8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+4204
-2889
lines changed

.babelrc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
{
2-
"presets": [
3-
"es2015", "stage-0"
4-
]
2+
"presets": ["env"]
53
}

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dist
2+
coverage
3+
node_modules

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8.9.3
1+
8

circle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ dependencies:
1010

1111
test:
1212
override:
13-
- yarn test
13+
- yarn test:ci
1414

1515
deployment:
1616
release:

example/example.js

Lines changed: 142 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -3,150 +3,176 @@
33
* argv[1] Secret Key
44
*/
55
const args = require('yargs').argv._;
6-
const Usabilla = require('../dist');
6+
const Usabilla = require('../dist/api-js-node');
77

88
const usabilla = new Usabilla(args[0], args[1]);
99

1010
// Get all buttons for this account.
11-
usabilla.websites.buttons.get().then((buttons) => {
11+
usabilla.websites.buttons
12+
.get()
13+
.then(buttons => {
14+
const button = buttons[0];
1215

13-
const button = buttons[0];
14-
15-
if (!button) {
16-
return;
17-
}
18-
19-
// Use the button id to get feedback for this button id.
20-
let buttonFeedbackQuery = {
21-
id: button.id,
22-
params: {
23-
limit: 10
16+
if (!button) {
17+
return;
2418
}
25-
};
26-
usabilla.websites.buttons.feedback.get(buttonFeedbackQuery).then((feedback) => {
27-
console.log('# button feedback', feedback.length);
28-
}).catch((reason) => {
19+
20+
// Use the button id to get feedback for this button id.
21+
let buttonFeedbackQuery = {
22+
id: button.id,
23+
params: {
24+
limit: 10
25+
}
26+
};
27+
usabilla.websites.buttons.feedback
28+
.get(buttonFeedbackQuery)
29+
.then(feedback => {
30+
console.log('# button feedback', feedback);
31+
})
32+
.catch(reason => {
33+
console.error(reason);
34+
});
35+
})
36+
.catch(reason => {
37+
// If the usabilla call fails, we want to see the error message
2938
console.error(reason);
3039
});
3140

32-
}).catch((reason) => {
33-
// If the usabilla call fails, we want to see the error message
34-
console.error(reason);
35-
});
36-
37-
3841
// Get all campaigns for this account.
39-
usabilla.websites.campaigns.get().then((campaigns) => {
40-
41-
const campaign = campaigns[0];
42+
usabilla.websites.campaigns
43+
.get()
44+
.then(campaigns => {
45+
const campaign = campaigns[0];
4246

43-
if (!campaign) {
44-
return;
45-
}
47+
console.log('# campaigns', campaigns.length);
4648

47-
// Get the results for a campaign with id.
48-
let campaignQuery = {
49-
id: campaign.id
50-
};
51-
52-
// Get the responses of the first campaign
53-
usabilla.websites.campaigns.results.get(campaignQuery).then((responses) => {
54-
console.log('# web campaign responses', responses.length);
55-
}).catch((reason) => {
56-
console.error(reason);
57-
});
49+
if (!campaign) {
50+
return;
51+
}
5852

59-
// Get the stats of the first campaign
60-
usabilla.websites.campaigns.stats.get(campaignQuery).then((stats) => {
61-
console.log('web campaign stats', stats);
62-
}).catch((reason) => {
53+
// Get the results for a campaign with id.
54+
let campaignQuery = {
55+
id: campaign.id
56+
};
57+
58+
// Get the responses of the first campaign
59+
usabilla.websites.campaigns.results
60+
.get(campaignQuery)
61+
.then(responses => {
62+
console.log('# web campaign responses', responses.length);
63+
})
64+
.catch(reason => {
65+
console.error(reason);
66+
});
67+
68+
// Get the stats of the first campaign
69+
usabilla.websites.campaigns.stats
70+
.get(campaignQuery)
71+
.then(stats => {
72+
console.log('# web campaign stats', stats);
73+
})
74+
.catch(reason => {
75+
console.error(reason);
76+
});
77+
})
78+
.catch(reason => {
79+
// If the usabilla call fails, we want to see the error message
6380
console.error(reason);
6481
});
65-
}).catch((reason) => {
66-
// If the usabilla call fails, we want to see the error message
67-
console.error(reason);
68-
});
6982

7083
// Get all inpage widgets for this account.
71-
usabilla.websites.inpage.get().then((inPageWidgets) => {
72-
73-
// Get the feedback for a inpage widget with id.
74-
let inPageQuery = {
75-
id: inPageWidgets[0].id
76-
};
77-
78-
// Get the feedback of the first inpage widget
79-
usabilla.websites.inpage.feedback.get(inPageQuery).then((feedback) => {
80-
console.log('# inpage feedback', feedback.length);
81-
}).catch((reason) => {
84+
usabilla.websites.inpage
85+
.get()
86+
.then(inPageWidgets => {
87+
// Get the feedback for a inpage widget with id.
88+
let inPageQuery = {
89+
id: inPageWidgets[0].id
90+
};
91+
92+
// Get the feedback of the first inpage widget
93+
usabilla.websites.inpage.feedback
94+
.get(inPageQuery)
95+
.then(feedback => {
96+
console.log('# inpage feedback', feedback.length);
97+
})
98+
.catch(reason => {
99+
console.error(reason);
100+
});
101+
})
102+
.catch(reason => {
103+
// If the usabilla call fails, we want to see the error message
82104
console.error(reason);
83105
});
84-
}).catch((reason) => {
85-
// If the usabilla call fails, we want to see the error message
86-
console.error(reason);
87-
});
88106

89107
// Get all email widgets for this account.
90-
usabilla.email.widgets.get().then((emailWidgets) => {
91-
92-
// Get the feedback for a email widget with id.
93-
let emailQuery = {
94-
id: emailWidgets[0].id
95-
};
96-
97-
// Get the feedback of the first email widget
98-
usabilla.email.widgets.feedback.get(emailQuery).then((feedback) => {
99-
console.log('# email feedback', feedback.length);
108+
usabilla.email.widgets
109+
.get()
110+
.then(emailWidgets => {
111+
// Get the feedback for a email widget with id.
112+
let emailQuery = {
113+
id: emailWidgets[0].id
114+
};
115+
116+
// Get the feedback of the first email widget
117+
usabilla.email.widgets.feedback.get(emailQuery).then(feedback => {
118+
console.log('# email feedback', feedback.length);
119+
});
120+
})
121+
.catch(reason => {
122+
// If the usabilla call fails, we want to see the error message
123+
console.error(reason);
100124
});
101-
}).catch((reason) => {
102-
// If the usabilla call fails, we want to see the error message
103-
console.error(reason);
104-
});
105125

106126
// Get all apps forms for this account.
107-
usabilla.apps.forms.get().then((appsForms) => {
108-
109-
const appsForm = appsForms[0];
110-
111-
if (!appsForm) {
112-
return;
113-
}
127+
usabilla.apps.forms
128+
.get()
129+
.then(appsForms => {
130+
const appsForm = appsForms[0];
114131

115-
// Get the feedback for a apps form with id.
116-
let appsQuery = {
117-
id: appsForm.id
118-
};
132+
if (!appsForm) {
133+
return;
134+
}
119135

120-
// Get the feedback of the second app form
121-
usabilla.apps.forms.feedback.get(appsQuery).then((feedback) => {
122-
console.log('# apps feedback', feedback.length);
136+
// Get the feedback for a apps form with id.
137+
let appsQuery = {
138+
id: appsForm.id
139+
};
140+
141+
// Get the feedback of the second app form
142+
usabilla.apps.forms.feedback.get(appsQuery).then(feedback => {
143+
console.log('# apps feedback', feedback.length);
144+
});
145+
})
146+
.catch(reason => {
147+
// If the usabilla call fails, we want to see the error message
148+
console.error(reason);
123149
});
124-
}).catch((reason) => {
125-
// If the usabilla call fails, we want to see the error message
126-
console.error(reason);
127-
});
128150

129151
// Get all apps campaigns for this account.
130-
usabilla.apps.campaigns.get().then((appsCampaigns) => {
131-
132-
// Use the first campaign found
133-
const appsCampaign = appsCampaigns[0];
134-
if (!appsCampaign) {
135-
return;
136-
}
137-
138-
// Get the feedback for a apps form with id.
139-
let appsCampaignQuery = {
140-
id: appsCampaign.id
141-
};
142-
143-
// Get the feedback of the second app form
144-
usabilla.apps.campaigns.results.get(appsCampaignQuery).then((responses) => {
145-
console.log('# apps campaign responses', responses.length);
146-
}).catch((reason) => {
152+
usabilla.apps.campaigns
153+
.get()
154+
.then(appsCampaigns => {
155+
// Use the first campaign found
156+
const appsCampaign = appsCampaigns[0];
157+
if (!appsCampaign) {
158+
return;
159+
}
160+
161+
// Get the feedback for a apps form with id.
162+
let appsCampaignQuery = {
163+
id: appsCampaign.id
164+
};
165+
166+
// Get the feedback of the second app form
167+
usabilla.apps.campaigns.results
168+
.get(appsCampaignQuery)
169+
.then(responses => {
170+
console.log('# apps campaign responses', responses.length);
171+
})
172+
.catch(reason => {
173+
console.error(reason);
174+
});
175+
})
176+
.catch(reason => {
147177
console.error(reason);
148178
});
149-
150-
}).catch((reason) => {
151-
console.error(reason);
152-
});

gulpfile.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)