@apolitical/sdk 0.0.1-beta.3 → 0.0.1-beta.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -1
- package/build/index.js +1 -1
- package/package.json +17 -3
- package/.gitlab-ci.yml +0 -25
- package/.husky/pre-commit +0 -8
- package/default.env +0 -0
- package/lib/apis/template.js +0 -27
- package/lib/config.js +0 -12
- package/lib/context.js +0 -10
- package/lib/errors.js +0 -23
- package/lib/index.js +0 -11
- package/test/__snapshots__/context.spec.js.snap +0 -11
- package/test/context.spec.js +0 -27
- package/test/errors.spec.js +0 -39
- package/test/setupTests.js +0 -13
- package/test/template.spec.js +0 -33
- package/webpack.config.js +0 -29
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
|
|
9
|
+
## [0.0.2] - 2022-01-06
|
|
10
|
+
### Added
|
|
11
|
+
- First example function `ping`
|
|
12
|
+
|
|
13
|
+
## [0.0.1] - 2022-01-05
|
|
9
14
|
### Added
|
|
10
15
|
- Initial setup
|
package/build/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("@apolitical/error-reporting"),require("axios"),require("http-status-codes")):"function"==typeof define&&define.amd?define(["@apolitical/error-reporting","axios","http-status-codes"],t):"object"==typeof exports?exports.ApoliticalSDK=t(require("@apolitical/error-reporting"),require("axios"),require("http-status-codes")):e.ApoliticalSDK=t(e["@apolitical/error-reporting"],e.axios,e["http-status-codes"])}(this,(function(e,t,r){return function(){"use strict";var o={30:function(t){t.exports=e},300:function(e){e.exports=t},25:function(e){e.exports=r}},n={};function i(e){var t=n[e];if(void 0!==t)return t.exports;var r=n[e]={exports:{}};return o[e](r,r.exports,i),r.exports}i.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(t,{a:t}),t},i.d=function(e,t){for(var r in t)i.o(t,r)&&!i.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},i.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var a={};return function(){i.r(a),i.d(a,{default:function(){return x},people:function(){return E},reportError:function(){return y},setContext:function(){return g},template:function(){return S}});var e=i(30),t=i(300),r=i.n(t),o={APIS:{BACKEND_TEMPLATE:{BASE_URL:"/api/backend-template",ENDPOINTS:{DUMMY:"/dummy"}},PEOPLE_API:{BASE_URL:"/api/people-api",ENDPOINTS:{USERS:"/users"}}}},n=i(25);const s=async t=>{let r=!1,o="Error at Apolitical SDK: ";if(t.response){switch(t.response.status){case n.StatusCodes.NOT_FOUND:case n.StatusCodes.FORBIDDEN:r=!0}o+=JSON.stringify(t.response)}else t.request?o+=JSON.stringify(t.request):o+=t.message;if(console.warn(o),!r)try{await(0,e.reportError)(new Error(o))}catch(e){console.warn(`Cannot report error: ${e.message}`)}},{APIS:{PEOPLE_API:{BASE_URL:u,ENDPOINTS:{USERS:p}}}}=o;var c={getUser:async e=>{let t=null;try{({data:t}=await r().get(`${u}${p}/${e}`))}catch(e){await s(e)}return t}};const{APIS:{BACKEND_TEMPLATE:{BASE_URL:l,ENDPOINTS:{DUMMY:f}}}}=o,d={people:c,template:{ping:async()=>{let e=null;try{({data:e}=await r().get(`${l}${f}`))}catch(e){await s(e)}return e}},reportError:e.reportError,setContext:({origin:t,gcpErrorsApiKey:r,serviceName:o})=>{(0,e.setContext)({origin:t,accessToken:r,serviceName:o})}},{people:E,template:S,reportError:y,setContext:g}=d;var x=d}(),a}()}));
|
package/package.json
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@apolitical/sdk",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.7",
|
|
4
4
|
"description": "Browser library to interact with Apolitical's APIs",
|
|
5
5
|
"author": "Apolitical Group Limited <engineering@apolitical.co>",
|
|
6
|
+
"contributors": [
|
|
7
|
+
"Anouska Hopkins <anouska.hopkins@apolitical.co>",
|
|
8
|
+
"Antonio Cordasco <antonio.cordasco@apolitical.co>",
|
|
9
|
+
"Dorothy Yau <dorothy.yau@apolitical.co>",
|
|
10
|
+
"Laura Hanna-White <laura.hanna-white@apolitical.co>",
|
|
11
|
+
"Fatimat Gbajabiamila <fatimat.gbajabiamila@apolitical.co>",
|
|
12
|
+
"Renzo Rozza <renzo.rozza@apolitical.co>",
|
|
13
|
+
"Rihards Jukna <rihards.jukna@apolitical.co>"
|
|
14
|
+
],
|
|
6
15
|
"license": "MIT",
|
|
7
16
|
"main": "build/index.js",
|
|
17
|
+
"files": [
|
|
18
|
+
"build/**"
|
|
19
|
+
],
|
|
8
20
|
"scripts": {
|
|
9
21
|
"test": "jest --runInBand",
|
|
10
22
|
"unit-test": "jest test/unit/* --bail --runInBand --passWithNoTests",
|
|
@@ -22,11 +34,13 @@
|
|
|
22
34
|
],
|
|
23
35
|
"dependencies": {
|
|
24
36
|
"@apolitical/error-reporting": "0.0.1",
|
|
25
|
-
"axios": "0.24.0"
|
|
37
|
+
"axios": "0.24.0",
|
|
38
|
+
"http-status-codes": "2.2.0"
|
|
26
39
|
},
|
|
27
40
|
"peerDependencies": {
|
|
28
41
|
"@apolitical/error-reporting": "0.0.1",
|
|
29
|
-
"axios": "0.24.0"
|
|
42
|
+
"axios": "0.24.0",
|
|
43
|
+
"http-status-codes": "2.2.0"
|
|
30
44
|
},
|
|
31
45
|
"devDependencies": {
|
|
32
46
|
"@apolitical/eslint-config": "0.0.1",
|
package/.gitlab-ci.yml
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
# External jobs
|
|
2
|
-
include:
|
|
3
|
-
- project: 'apolitical/templates/gitlab-pipelines'
|
|
4
|
-
ref: master
|
|
5
|
-
file: 'Node-Testing.gitlab-ci.yml'
|
|
6
|
-
|
|
7
|
-
stages:
|
|
8
|
-
- cache
|
|
9
|
-
- test
|
|
10
|
-
- publish
|
|
11
|
-
|
|
12
|
-
# Publish jobs
|
|
13
|
-
|
|
14
|
-
publish-module:
|
|
15
|
-
stage: publish
|
|
16
|
-
image: node:12.20.1-alpine3.11
|
|
17
|
-
extends: .yarn-install
|
|
18
|
-
only:
|
|
19
|
-
- tags
|
|
20
|
-
- /^v\d+\.\d+\.\d+$/
|
|
21
|
-
before_script:
|
|
22
|
-
- yarn build
|
|
23
|
-
script:
|
|
24
|
-
- echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' > ~/.npmrc
|
|
25
|
-
- yarn publish --access=public
|
package/.husky/pre-commit
DELETED
package/default.env
DELETED
|
File without changes
|
package/lib/apis/template.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
|
|
3
|
-
import config from '../config';
|
|
4
|
-
import { handleError } from '../errors';
|
|
5
|
-
|
|
6
|
-
const {
|
|
7
|
-
APIS: {
|
|
8
|
-
BACKEND_TEMPLATE: {
|
|
9
|
-
BASE_URL,
|
|
10
|
-
ENDPOINTS: { DUMMY },
|
|
11
|
-
},
|
|
12
|
-
},
|
|
13
|
-
} = config;
|
|
14
|
-
|
|
15
|
-
/*
|
|
16
|
-
* Returns a random number after calling the Backend Template
|
|
17
|
-
* @return {Object} data - JSON object returned by the API
|
|
18
|
-
*/
|
|
19
|
-
export const ping = async () => {
|
|
20
|
-
let data = null;
|
|
21
|
-
try {
|
|
22
|
-
({ data } = await axios.get(`${BASE_URL}${DUMMY}`));
|
|
23
|
-
} catch (error) {
|
|
24
|
-
handleError(error);
|
|
25
|
-
}
|
|
26
|
-
return data;
|
|
27
|
-
};
|
package/lib/config.js
DELETED
package/lib/context.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { setContext as setErrorContext } from '@apolitical/error-reporting';
|
|
2
|
-
|
|
3
|
-
export const setContext = ({ origin, gcpErrorsApiKey, serviceName }) => {
|
|
4
|
-
// Setup error reporting context
|
|
5
|
-
setErrorContext({
|
|
6
|
-
origin,
|
|
7
|
-
accessToken: gcpErrorsApiKey,
|
|
8
|
-
serviceName,
|
|
9
|
-
});
|
|
10
|
-
};
|
package/lib/errors.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { reportError } from '@apolitical/error-reporting';
|
|
2
|
-
|
|
3
|
-
export const handleError = async (error) => {
|
|
4
|
-
let errorMessage = 'Error at Apolitical SDK: ';
|
|
5
|
-
if (error.response) {
|
|
6
|
-
// The request was made and the server responded with a status code that falls out of the range of 2xx
|
|
7
|
-
errorMessage += JSON.stringify(error.response);
|
|
8
|
-
} else if (error.request) {
|
|
9
|
-
// The request was made but no response was received
|
|
10
|
-
errorMessage += JSON.stringify(error.request);
|
|
11
|
-
} else {
|
|
12
|
-
// Something happened in setting up the request that triggered an error
|
|
13
|
-
errorMessage += error.message;
|
|
14
|
-
}
|
|
15
|
-
// Log the error (on the client browser)
|
|
16
|
-
console.warn(errorMessage);
|
|
17
|
-
// Report the error (to GCP) but prevent exceptions
|
|
18
|
-
try {
|
|
19
|
-
await reportError(new Error(errorMessage));
|
|
20
|
-
} catch (error2) {
|
|
21
|
-
console.warn(`Cannot report error: ${error2.message}`);
|
|
22
|
-
}
|
|
23
|
-
};
|
package/lib/index.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
-
|
|
3
|
-
exports[`Apolitical SDK Context Set Context should set error reporting context 1`] = `
|
|
4
|
-
Array [
|
|
5
|
-
Object {
|
|
6
|
-
"accessToken": "some-gcp-errors-api-key",
|
|
7
|
-
"origin": "some-origin",
|
|
8
|
-
"serviceName": "some-service-name",
|
|
9
|
-
},
|
|
10
|
-
]
|
|
11
|
-
`;
|
package/test/context.spec.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { setContext as setErrorContext } from '@apolitical/error-reporting';
|
|
2
|
-
|
|
3
|
-
import sdk from '../lib';
|
|
4
|
-
|
|
5
|
-
describe('Apolitical SDK', () => {
|
|
6
|
-
describe('Context', () => {
|
|
7
|
-
const { setContext } = sdk;
|
|
8
|
-
|
|
9
|
-
describe('Set Context', () => {
|
|
10
|
-
const options = {
|
|
11
|
-
origin: 'some-origin',
|
|
12
|
-
gcpErrorsApiKey: 'some-gcp-errors-api-key',
|
|
13
|
-
serviceName: 'some-service-name',
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
beforeEach(() => {
|
|
17
|
-
setErrorContext.mockReturnValue();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test('should set error reporting context', () => {
|
|
21
|
-
setContext(options);
|
|
22
|
-
expect(setErrorContext).toHaveBeenCalledTimes(1);
|
|
23
|
-
expect(setErrorContext.mock.calls[0]).toMatchSnapshot();
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
});
|
package/test/errors.spec.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { reportError } from '@apolitical/error-reporting';
|
|
2
|
-
|
|
3
|
-
import { handleError } from '../lib/errors';
|
|
4
|
-
|
|
5
|
-
describe('Apolitical SDK', () => {
|
|
6
|
-
describe('Errors', () => {
|
|
7
|
-
describe('Handle Error', () => {
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
reportError.mockResolvedValue();
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
test('should log response error', async () => {
|
|
13
|
-
await handleError({ response: { data: 'some response' } });
|
|
14
|
-
expect(console.warn.mock.calls[0][0]).toEqual('Error at Apolitical SDK: {"data":"some response"}');
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
test('should log request error', async () => {
|
|
18
|
-
await handleError({ request: { data: 'some request' } });
|
|
19
|
-
expect(console.warn.mock.calls[0][0]).toEqual('Error at Apolitical SDK: {"data":"some request"}');
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test('should log message error', async () => {
|
|
23
|
-
await handleError({ message: 'some message' });
|
|
24
|
-
expect(console.warn.mock.calls[0][0]).toEqual('Error at Apolitical SDK: some message');
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
test('should report error', async () => {
|
|
28
|
-
await handleError({ message: 'some message' });
|
|
29
|
-
expect(reportError).toHaveBeenCalledTimes(1);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
test('should catch errors when reporting', async () => {
|
|
33
|
-
reportError.mockRejectedValueOnce(new Error('something went wrong'));
|
|
34
|
-
await handleError({ message: 'some message' });
|
|
35
|
-
expect(console.warn.mock.calls[1][0]).toEqual('Cannot report error: something went wrong');
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
});
|
package/test/setupTests.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
// Mock dependencies
|
|
2
|
-
jest.mock('axios');
|
|
3
|
-
jest.mock('@apolitical/error-reporting', () => {
|
|
4
|
-
return {
|
|
5
|
-
reportError: jest.fn(),
|
|
6
|
-
setContext: jest.fn(),
|
|
7
|
-
};
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
// Overrides console error & warn when testing
|
|
11
|
-
// NB: Comment out this line to check errors when testing
|
|
12
|
-
console.error = jest.fn();
|
|
13
|
-
console.warn = jest.fn();
|
package/test/template.spec.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
import { reportError } from '@apolitical/error-reporting';
|
|
3
|
-
|
|
4
|
-
import sdk from '../lib';
|
|
5
|
-
|
|
6
|
-
describe('Apolitical SDK', () => {
|
|
7
|
-
describe('Backend Template', () => {
|
|
8
|
-
const {
|
|
9
|
-
template: { ping },
|
|
10
|
-
} = sdk;
|
|
11
|
-
|
|
12
|
-
describe('Ping (Dummy Endpoint)', () => {
|
|
13
|
-
const payload = { data: { result: 123 } };
|
|
14
|
-
|
|
15
|
-
beforeEach(() => {
|
|
16
|
-
reportError.mockResolvedValue();
|
|
17
|
-
axios.get.mockResolvedValue(payload);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test('should return a number', async () => {
|
|
21
|
-
const { result } = await ping();
|
|
22
|
-
expect(result).toEqual(expect.any(Number));
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
test('should catch unexpected errors', async () => {
|
|
26
|
-
axios.get.mockRejectedValueOnce(new Error('something went wrong'));
|
|
27
|
-
const result = await ping();
|
|
28
|
-
expect(result).toStrictEqual(null);
|
|
29
|
-
expect(reportError).toHaveBeenCalledTimes(1);
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
});
|
package/webpack.config.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const path = require('path');
|
|
4
|
-
|
|
5
|
-
module.exports = {
|
|
6
|
-
mode: 'production',
|
|
7
|
-
entry: {
|
|
8
|
-
index: path.resolve(__dirname, 'lib', 'index.js'),
|
|
9
|
-
},
|
|
10
|
-
output: {
|
|
11
|
-
path: path.resolve(__dirname, 'build'),
|
|
12
|
-
filename: 'index.js',
|
|
13
|
-
library: {
|
|
14
|
-
name: 'ApoliticalSDK',
|
|
15
|
-
type: 'umd',
|
|
16
|
-
},
|
|
17
|
-
globalObject: 'this',
|
|
18
|
-
},
|
|
19
|
-
externals: {
|
|
20
|
-
axios: 'axios',
|
|
21
|
-
'@apolitical/error-reporting': '@apolitical/error-reporting',
|
|
22
|
-
},
|
|
23
|
-
optimization: {
|
|
24
|
-
splitChunks: {
|
|
25
|
-
chunks: 'all',
|
|
26
|
-
},
|
|
27
|
-
minimize: true,
|
|
28
|
-
},
|
|
29
|
-
};
|