Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
Add 'debug' package to log calling API
Browse files Browse the repository at this point in the history
- Removed 'chalk' package and 'logger.js' to log color messages to console
- Added 'debug' package to log all API calls and server responses
- Refactored request-helper

AFX-5165
  • Loading branch information
Maksym Shykov committed Aug 25, 2020
1 parent 4415e75 commit 525c1a4
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 39 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
# qa-api-services

The library to get access to the 'Setapp' API services in your E2E UI tests

## Contributing guide

1. Create feature branch and perform changes.
2. Push changes to the origin and create pull-request.
3. After pull-request merging update local master.
4. Bump version and add a tag (for example using `npm version`).
5. Push changes to the origin.
6. Publish new version to NPM.
6. Publish a new version to the GitHub Packages.

**TIP**: you can use [np](https://github.com/sindresorhus/np) to perform steps 4-6 automatically

## Versioning rules

Given a version number MAJOR.MINOR.PATCH, increment the:
* **patch** - when you make backwards-compatible bug fixes
* **minor** - when you add functionality in a backwards-compatible manner
* **major** - when you make incompatible API changes

## Debug mode

The [debug](https://www.npmjs.com/package/debug) package is using to log API requests/responses data. By default, all logging of API calls disabled
Please, use `DEBUG=macpaw:qa-api-services` key in the main project to enable logging:

```js
DEBUG=macpaw:qa-api-services npm run e2e-local
```
2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict';

const logger = require('./lib/logger');
const request = require('./lib/request');
const requestHelper = require('./lib/request-helper');
const waiters = require('./lib/waiters');

module.exports = {
logger,
request,
requestHelper,
waiters,
Expand Down
17 changes: 0 additions & 17 deletions lib/logger.js

This file was deleted.

24 changes: 18 additions & 6 deletions lib/request-helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const request = require('then-request');
const log = require('debug')('macpaw:qa-api-services');

module.exports = {
isSuccess(statusCode) {
Expand All @@ -15,17 +16,28 @@ module.exports = {
return regExp.test(statusCode);
},

async getResponseBody(method, url, options) {
return request(method, url, options);
async sendRequest(method, url, options) {
log(`Executing '${method}: ${url}' with options: %O`, options);
const response = await request(method, url, options);
log(`Server responded with status code: '${response.statusCode}' and body: ${response.body.toString()}'`);

this.catchError(response);

return response;
},

getResponseBody(response) {
const body = response.body.toString();

return body ? JSON.parse(body) : {};
},

catchError(response) {
const {statusCode} = response;
const body = response.body.toString();

if (!this.isSuccess(response.statusCode)) {
throw new Error(`Server responded with code ${response.statusCode}: ${body}`);
if (!this.isSuccess(statusCode)) {
throw new Error(`Oops, the server responded with status code: '${statusCode}' and body: ${body}`);
}

return body ? JSON.parse(body) : {};
},
};
20 changes: 10 additions & 10 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@ module.exports = {
},

get(url, options) {
const response = this.browser.call(() => requestHelper.getResponseBody('GET', url, options));
const response = this.browser.call(() => requestHelper.sendRequest('GET', url, options));

return requestHelper.catchError(response);
return requestHelper.getResponseBody(response);
},

post(url, options) {
const response = this.browser.call(() => requestHelper.getResponseBody('POST', url, options));
const response = this.browser.call(() => requestHelper.sendRequest('POST', url, options));

return requestHelper.catchError(response);
return requestHelper.getResponseBody(response);
},

patch(url, options) {
const response = this.browser.call(() => requestHelper.getResponseBody('PATCH', url, options));
const response = this.browser.call(() => requestHelper.sendRequest('PATCH', url, options));

return requestHelper.catchError(response);
return requestHelper.getResponseBody(response);
},

put(url, options) {
const response = this.browser.call(() => requestHelper.getResponseBody('PUT', url, options));
const response = this.browser.call(() => requestHelper.sendRequest('PUT', url, options));

return requestHelper.catchError(response);
return requestHelper.getResponseBody(response);
},

del(url, options) {
const response = this.browser.call(() => requestHelper.getResponseBody('DELETE', url, options));
const response = this.browser.call(() => requestHelper.sendRequest('DELETE', url, options));

return requestHelper.catchError(response);
return requestHelper.getResponseBody(response);
},
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"lint": "eslint ."
},
"dependencies": {
"chalk": "^4.1.0",
"debug": "^4.1.1",
"then-request": "^6.0.2"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ chalk@^2.0.0:
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"

chalk@^4.0.0, chalk@^4.1.0:
chalk@^4.0.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.0.tgz#4e14870a618d9e2edd97dd8345fd9d9dc315646a"
integrity sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A==
Expand Down Expand Up @@ -376,7 +376,7 @@ debug@^2.6.9:
dependencies:
ms "2.0.0"

debug@^4.0.1, debug@^4.1.0:
debug@^4.0.1, debug@^4.1.0, debug@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
integrity sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==
Expand Down

0 comments on commit 525c1a4

Please sign in to comment.