Skip to content
This repository was archived by the owner on Aug 27, 2025. It is now read-only.

Commit 9d70ac5

Browse files
refactor: update dependencies and freshen up project
2 parents acd1d02 + 1a9b165 commit 9d70ac5

21 files changed

Lines changed: 9799 additions & 26437 deletions

.gitpod.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tasks:
2+
- init: npm install

__mocks__/http-client.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const httpClient = {
2+
get: jest.fn(),
3+
post: jest.fn(),
4+
};
5+
6+
module.exports = httpClient;

__mocks__/search/passivaOk.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
2-
statusCode: 200,
3-
body: {
2+
status: 200,
3+
data: {
44
id: '0123456789d6132158591d2d',
55
sender: {
66
description: 'NODEJS UNIT TEST SRL',

babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: [['@babel/preset-env', { targets: { node: 'current' } }]],
3+
};

jest.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ module.exports = {
33
coverageDirectory: 'coverage',
44
testEnvironment: 'node',
55
verbose: false,
6+
transformIgnorePatterns: ['/node_modules/(?!axios)/'],
7+
transform: {
8+
'^.+\\.[t|j]sx?$': 'babel-jest',
9+
},
610
};

lib/auth.js

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

lib/auth/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const signIn = require('./sign-in');
2+
const refreshToken = require('./refresh-token');
3+
4+
module.exports = (config) => ({
5+
signIn: signIn(config),
6+
refreshToken: refreshToken(config),
7+
});

lib/auth/refresh-token/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const schema = require('./schema');
2+
const auth = require('../shared/auth');
3+
4+
/**
5+
* Refresh the access token using the existing refresh token
6+
* @ignore
7+
* @param {Object} [data]
8+
* @param {string} [data.refresh_token] Token coming from the initial auth
9+
* @returns {Promise} Resolves to an boolean if successful, or an object
10+
* containing the error message
11+
*/
12+
const refreshToken =
13+
(config) =>
14+
async (data = {}, opts = {}) => {
15+
const validation = schema.validate(data, {
16+
abortEarly: false,
17+
});
18+
19+
if (validation.error) {
20+
throw validation.error;
21+
}
22+
23+
const payload = {
24+
grant_type: 'refresh_token',
25+
...data,
26+
};
27+
28+
return auth(config)(payload, opts);
29+
};
30+
31+
module.exports = refreshToken;

lib/auth/refresh-token/schema.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const Joi = require('@hapi/joi');
2+
3+
const schema = Joi.object().keys({
4+
refresh_token: Joi.string().required(),
5+
});
6+
7+
module.exports = schema;

lib/auth/shared/auth.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const formUrlencoded = require('form-urlencoded');
2+
const httpClient = require('../../http-client');
3+
4+
/**
5+
* Internal method that performs the actual authentication with Aruba
6+
* @ignore
7+
* @param {Object} [data]
8+
* @param {string} [data.username] Aruba username
9+
* @param {string} [data.password] Aruba password
10+
* @param {string} [data.grant_type] Aruba username
11+
* @param {string} [data.refresh_token] Aruba password*
12+
* @param {Object} [opts] Optional HTTP client options
13+
* @returns {Promise} Resolves to an boolean if successful, or an object
14+
* containing the error message
15+
*/
16+
const auth =
17+
(config) =>
18+
async (data = {}, opts = {}) => {
19+
const options = { ...config.endpoints.auth.httpOptions, ...opts };
20+
21+
try {
22+
const res = await httpClient.post(
23+
`${config.endpoints.auth.url}/auth/signin`,
24+
formUrlencoded(data),
25+
options
26+
);
27+
28+
return {
29+
success: true,
30+
...res.data,
31+
};
32+
} catch (error) {
33+
if (error.response && error.response.data && error.response.data.error) {
34+
return {
35+
success: false,
36+
...error.response.data,
37+
};
38+
}
39+
throw error;
40+
}
41+
};
42+
43+
module.exports = auth;

0 commit comments

Comments
 (0)