@apolitical/sdk 0.0.1 → 0.0.4-beta.2
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 +16 -1
- package/README.md +16 -5
- package/build/index.js +1 -1
- package/package.json +31 -34
- 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 -13
- package/lib/helpers/errors.js +0 -12
- package/lib/index.js +0 -9
- package/test/setupTests.js +0 -4
- package/test/template.spec.js +0 -27
- package/webpack.config.js +0 -28
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,21 @@ 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
|
-
## [0.0.
|
|
8
|
+
## [0.0.4] - 2022-01-31
|
|
9
|
+
### Fixed
|
|
10
|
+
- Buffer dependency
|
|
11
|
+
- Node.js version
|
|
12
|
+
|
|
13
|
+
## [0.0.3] - 2022-01-12
|
|
14
|
+
### Added
|
|
15
|
+
- Exposed `reportError` function
|
|
16
|
+
### Fixed
|
|
17
|
+
- Resources naming (must be plural)
|
|
18
|
+
|
|
19
|
+
## [0.0.2] - 2022-01-06
|
|
20
|
+
### Added
|
|
21
|
+
- First example function `ping`
|
|
22
|
+
|
|
23
|
+
## [0.0.1] - 2022-01-05
|
|
9
24
|
### Added
|
|
10
25
|
- Initial setup
|
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ Browser library to interact with Apolitical's APIs
|
|
|
6
6
|
|
|
7
7
|
Requires the following to run:
|
|
8
8
|
|
|
9
|
-
- [node.js][node]
|
|
9
|
+
- [node.js][node] 16.13.0+
|
|
10
10
|
- [yarn][yarn]
|
|
11
11
|
|
|
12
12
|
[node]: https://nodejs.org/en/download/
|
|
@@ -38,11 +38,22 @@ Publishes the library to NPM.
|
|
|
38
38
|
|
|
39
39
|
## Usage
|
|
40
40
|
|
|
41
|
-
The recommended way to use `@apolitical/sdk` is to
|
|
41
|
+
The recommended way to use `@apolitical/sdk` is to load the SDK first:
|
|
42
42
|
|
|
43
43
|
```js
|
|
44
|
-
const
|
|
44
|
+
const loadSdk = require('@apolitical/sdk');
|
|
45
45
|
|
|
46
|
-
const
|
|
47
|
-
|
|
46
|
+
const sdk = loadSdk({
|
|
47
|
+
origin: window.origin || window.location.origin,
|
|
48
|
+
gcpErrorsApiKey: process.env.REACT_APP_GCP_ERRORS_API_KEY,
|
|
49
|
+
serviceName: 'some-service',
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
And then, you can use all the functionality provided by the SDK. For example:
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
sdk.template.dummy.read().then((result) => {
|
|
57
|
+
console.info('SDK ping:', result);
|
|
58
|
+
});
|
|
48
59
|
```
|
package/build/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,
|
|
1
|
+
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r(require("@apolitical/error-reporting"),require("axios"),require("http-status-codes")):"function"==typeof define&&define.amd?define(["@apolitical/error-reporting","axios","http-status-codes"],r):"object"==typeof exports?exports.ApoliticalSDK=r(require("@apolitical/error-reporting"),require("axios"),require("http-status-codes")):e.ApoliticalSDK=r(e["@apolitical/error-reporting"],e.axios,e["http-status-codes"])}(this,(function(e,r,t){return function(){"use strict";var o={30:function(r){r.exports=e},300:function(e){e.exports=r},25:function(e){e.exports=t}},n={};function a(e){var r=n[e];if(void 0!==r)return r.exports;var t=n[e]={exports:{}};return o[e](t,t.exports,a),t.exports}a.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return a.d(r,{a:r}),r},a.d=function(e,r){for(var t in r)a.o(r,t)&&!a.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:r[t]})},a.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},a.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var i={};return function(){a.r(i),a.d(i,{default:function(){return E},reportError:function(){return e.reportError}});var e=a(30),r=a(300),t=a.n(r),o={APIS:{BACKEND_TEMPLATE:{BASE_URL:"/api/backend-template",ENDPOINTS:{DUMMY:"/dummy"}},PEOPLE_API:{BASE_URL:"/api/people-api",ENDPOINTS:{USERS:"/users"}}}},n=a(25);const s=async r=>{try{let t=!1,o="Error at Apolitical SDK: ";if(r.response){switch(r.response.status){case n.StatusCodes.NOT_FOUND:case n.StatusCodes.FORBIDDEN:t=!0}o+=JSON.stringify(r.response)}else r.request?o+=JSON.stringify(r.request):o+=r.message;if(console.warn(o),t)return;await(0,e.reportError)(new Error(o))}catch(e){console.warn(`Cannot handle error: ${e.message}`)}},{APIS:{PEOPLE_API:{BASE_URL:u,ENDPOINTS:{USERS:c}}}}=o;var p={users:{read:async e=>{let r=null;try{({data:r}=await t().get(`${u}${c}/${e}`))}catch(e){await s(e)}return r}}};const{APIS:{BACKEND_TEMPLATE:{BASE_URL:l,ENDPOINTS:{DUMMY:f}}}}=o;var d={people:p,template:{dummy:{read:async()=>{let e=null;try{({data:e}=await t().get(`${l}${f}`))}catch(e){await s(e)}return e}}}};function E({origin:r,gcpErrorsApiKey:t,serviceName:o}){return(0,e.setContext)({origin:r,accessToken:t,serviceName:o}),{...d,reportError:e.reportError}}}(),i}()}));
|
package/package.json
CHANGED
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@apolitical/sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4-beta.2",
|
|
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
|
-
],
|
|
15
6
|
"license": "MIT",
|
|
16
7
|
"main": "build/index.js",
|
|
8
|
+
"files": [
|
|
9
|
+
"build/**"
|
|
10
|
+
],
|
|
17
11
|
"scripts": {
|
|
18
12
|
"test": "jest --runInBand",
|
|
19
13
|
"unit-test": "jest test/unit/* --bail --runInBand --passWithNoTests",
|
|
@@ -30,40 +24,36 @@
|
|
|
30
24
|
"SDK"
|
|
31
25
|
],
|
|
32
26
|
"dependencies": {
|
|
33
|
-
"
|
|
27
|
+
"@apolitical/error-reporting": "0.0.3-beta.2",
|
|
28
|
+
"axios": "0.25.0",
|
|
29
|
+
"http-status-codes": "2.2.0"
|
|
34
30
|
},
|
|
35
31
|
"peerDependencies": {
|
|
36
|
-
"
|
|
32
|
+
"@apolitical/error-reporting": "0.0.3-beta.2",
|
|
33
|
+
"axios": "0.25.0",
|
|
34
|
+
"http-status-codes": "2.2.0"
|
|
37
35
|
},
|
|
38
36
|
"devDependencies": {
|
|
39
|
-
"@apolitical/eslint-config": "
|
|
40
|
-
"@babel/core": "7.16.
|
|
41
|
-
"babel-
|
|
42
|
-
"babel-
|
|
43
|
-
"babel
|
|
37
|
+
"@apolitical/eslint-config": "1.0.2-beta.1",
|
|
38
|
+
"@babel/core": "7.16.12",
|
|
39
|
+
"@babel/eslint-parser": "7.16.5",
|
|
40
|
+
"@babel/plugin-transform-runtime": "7.16.10",
|
|
41
|
+
"@babel/preset-env": "7.16.11",
|
|
44
42
|
"husky": "7.0.4",
|
|
45
43
|
"jest": "27.4.7",
|
|
46
44
|
"jest-junit": "13.0.0",
|
|
47
|
-
"lint-staged": "12.
|
|
48
|
-
"webpack": "5.
|
|
49
|
-
"webpack-cli": "4.9.
|
|
45
|
+
"lint-staged": "12.3.2",
|
|
46
|
+
"webpack": "5.67.0",
|
|
47
|
+
"webpack-cli": "4.9.2"
|
|
50
48
|
},
|
|
51
49
|
"eslintConfig": {
|
|
52
50
|
"extends": [
|
|
53
|
-
"eslint
|
|
54
|
-
"plugin:prettier/recommended",
|
|
55
|
-
"plugin:jest/recommended"
|
|
51
|
+
"@apolitical/eslint-config/base.config"
|
|
56
52
|
],
|
|
57
53
|
"env": {
|
|
58
|
-
"browser": true
|
|
59
|
-
"node": true,
|
|
60
|
-
"es6": true,
|
|
61
|
-
"jest": true
|
|
54
|
+
"browser": true
|
|
62
55
|
},
|
|
63
|
-
"
|
|
64
|
-
"jest"
|
|
65
|
-
],
|
|
66
|
-
"parser": "babel-eslint"
|
|
56
|
+
"parser": "@babel/eslint-parser"
|
|
67
57
|
},
|
|
68
58
|
"prettier": "@apolitical/eslint-config/prettier.config",
|
|
69
59
|
"browserslist": {
|
|
@@ -95,11 +85,18 @@
|
|
|
95
85
|
},
|
|
96
86
|
"babel": {
|
|
97
87
|
"presets": [
|
|
98
|
-
"
|
|
99
|
-
]
|
|
88
|
+
"@babel/preset-env"
|
|
89
|
+
],
|
|
90
|
+
"env": {
|
|
91
|
+
"test": {
|
|
92
|
+
"plugins": [
|
|
93
|
+
"@babel/plugin-transform-runtime"
|
|
94
|
+
]
|
|
95
|
+
}
|
|
96
|
+
}
|
|
100
97
|
},
|
|
101
98
|
"engines": {
|
|
102
|
-
"node": ">=
|
|
99
|
+
"node": ">=16.13.0"
|
|
103
100
|
},
|
|
104
101
|
"lint-staged": {
|
|
105
102
|
"*.js": "eslint --cache --fix --ignore-path .gitignore",
|
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 '../helpers/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} payload - random number generated by the API
|
|
18
|
-
*/
|
|
19
|
-
export const ping = async () => {
|
|
20
|
-
let payload = null;
|
|
21
|
-
try {
|
|
22
|
-
payload = await axios.get(`${BASE_URL}${DUMMY}`);
|
|
23
|
-
} catch (error) {
|
|
24
|
-
handleError(error);
|
|
25
|
-
}
|
|
26
|
-
return payload;
|
|
27
|
-
};
|
package/lib/config.js
DELETED
package/lib/context.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
// TODO: Define context when external configurations are required for the SDK to work
|
|
2
|
-
|
|
3
|
-
// let context = {
|
|
4
|
-
// token: null,
|
|
5
|
-
// };
|
|
6
|
-
|
|
7
|
-
// export const setContext = ({ token }) => {
|
|
8
|
-
// context.token = token;
|
|
9
|
-
// };
|
|
10
|
-
|
|
11
|
-
// export const getContext = () => {
|
|
12
|
-
// return context;
|
|
13
|
-
// };
|
package/lib/helpers/errors.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export const handleError = (error) => {
|
|
2
|
-
if (error.response) {
|
|
3
|
-
// The request was made and the server responded with a status code that falls out of the range of 2xx
|
|
4
|
-
console.warn(error.response.data, error.response.status, error.response.headers);
|
|
5
|
-
} else if (error.request) {
|
|
6
|
-
// The request was made but no response was received
|
|
7
|
-
console.warn(error.request);
|
|
8
|
-
} else {
|
|
9
|
-
// Something happened in setting up the request that triggered an error
|
|
10
|
-
console.warn(error.message);
|
|
11
|
-
}
|
|
12
|
-
};
|
package/lib/index.js
DELETED
package/test/setupTests.js
DELETED
package/test/template.spec.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
|
|
3
|
-
import sdk from '../lib';
|
|
4
|
-
|
|
5
|
-
const {
|
|
6
|
-
template: { ping },
|
|
7
|
-
} = sdk;
|
|
8
|
-
|
|
9
|
-
jest.mock('axios');
|
|
10
|
-
|
|
11
|
-
describe('Apolitical SDK', () => {
|
|
12
|
-
describe('Backend Template', () => {
|
|
13
|
-
describe('Ping (Dummy)', () => {
|
|
14
|
-
test('should return a number', async () => {
|
|
15
|
-
axios.get.mockResolvedValueOnce({ result: 123 });
|
|
16
|
-
const { result } = await ping();
|
|
17
|
-
expect(result).toEqual(expect.any(Number));
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test('should catch unexpected errors', async () => {
|
|
21
|
-
axios.get.mockRejectedValueOnce(new Error('Something went wrong'));
|
|
22
|
-
const result = await ping();
|
|
23
|
-
expect(result).toStrictEqual(null);
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
});
|
package/webpack.config.js
DELETED
|
@@ -1,28 +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
|
-
},
|
|
22
|
-
optimization: {
|
|
23
|
-
splitChunks: {
|
|
24
|
-
chunks: 'all',
|
|
25
|
-
},
|
|
26
|
-
minimize: true,
|
|
27
|
-
},
|
|
28
|
-
};
|