@apolitical/sdk 0.0.1

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/.gitlab-ci.yml ADDED
@@ -0,0 +1,25 @@
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
@@ -0,0 +1,8 @@
1
+ #!/bin/sh
2
+ . "$(dirname "$0")/_/husky.sh"
3
+
4
+ yarn lint-format
5
+
6
+ yarn build
7
+
8
+ yarn test
package/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.0.1] - 2021-01-18
9
+ ### Added
10
+ - Initial setup
package/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # Apolitical SDK
2
+
3
+ Browser library to interact with Apolitical's APIs
4
+
5
+ ## Requirements
6
+
7
+ Requires the following to run:
8
+
9
+ - [node.js][node] 12.20.1+
10
+ - [yarn][yarn]
11
+
12
+ [node]: https://nodejs.org/en/download/
13
+ [yarn]: https://classic.yarnpkg.com/en/docs/install
14
+
15
+ ## Installation
16
+
17
+ Install with `yarn`:
18
+
19
+ ```sh
20
+ yarn add @apolitical/sdk
21
+ ```
22
+
23
+ ## Available Scripts
24
+
25
+ In the project directory, you can run:
26
+ ### `yarn run test`
27
+
28
+ Runs the test (and the interactive mode can be enabled with `--watchAll`).
29
+
30
+ ### `yarn build`
31
+
32
+ Builds the library for production to the `build` folder.
33
+ It correctly bundles the code on production mode and optimizes the build for the best performance.
34
+
35
+ ### `yarn publish`
36
+
37
+ Publishes the library to NPM.
38
+
39
+ ## Usage
40
+
41
+ The recommended way to use `@apolitical/sdk` is to use the provided functionality:
42
+
43
+ ```js
44
+ const { ping } = require('@apolitical/sdk');
45
+
46
+ const result = ping();
47
+ console.log(result);
48
+ ```
package/build/index.js ADDED
@@ -0,0 +1 @@
1
+ !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("axios")):"function"==typeof define&&define.amd?define(["axios"],t):"object"==typeof exports?exports.ApoliticalSDK=t(require("axios")):e.ApoliticalSDK=t(e.axios)}(this,(function(e){return function(){"use strict";var t={300:function(t){t.exports=e}},o={};function n(e){var r=o[e];if(void 0!==r)return r.exports;var u=o[e]={exports:{}};return t[e](u,u.exports,n),u.exports}n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,{a:t}),t},n.d=function(e,t){for(var o in t)n.o(t,o)&&!n.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var r={};return function(){n.r(r),n.d(r,{default:function(){return a}});var e=n(300),t=n.n(e);const{APIS:{BACKEND_TEMPLATE:{BASE_URL:o,ENDPOINTS:{DUMMY:u}}}}={APIS:{BACKEND_TEMPLATE:{BASE_URL:"/api/backend-template",ENDPOINTS:{DUMMY:"/dummy"}}}};var a={template:{ping:async()=>{let e=null;try{e=await t().get(`${o}${u}`)}catch(e){(e=>{e.response?console.warn(e.response.data,e.response.status,e.response.headers):e.request?console.warn(e.request):console.warn(e.message)})(e)}return e}}}}(),r}()}));
package/default.env ADDED
File without changes
@@ -0,0 +1,27 @@
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 ADDED
@@ -0,0 +1,12 @@
1
+ const config = {
2
+ APIS: {
3
+ BACKEND_TEMPLATE: {
4
+ BASE_URL: '/api/backend-template',
5
+ ENDPOINTS: {
6
+ DUMMY: '/dummy',
7
+ },
8
+ },
9
+ },
10
+ };
11
+
12
+ export default config;
package/lib/context.js ADDED
@@ -0,0 +1,13 @@
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
+ // };
@@ -0,0 +1,12 @@
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 ADDED
@@ -0,0 +1,9 @@
1
+ import { ping } from './apis/template';
2
+
3
+ const sdk = {
4
+ template: {
5
+ ping,
6
+ },
7
+ };
8
+
9
+ export default sdk;
package/package.json ADDED
@@ -0,0 +1,108 @@
1
+ {
2
+ "name": "@apolitical/sdk",
3
+ "version": "0.0.1",
4
+ "description": "Browser library to interact with Apolitical's APIs",
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
+ "license": "MIT",
16
+ "main": "build/index.js",
17
+ "scripts": {
18
+ "test": "jest --runInBand",
19
+ "unit-test": "jest test/unit/* --bail --runInBand --passWithNoTests",
20
+ "integration-test": "jest test/integration/* --bail --runInBand --passWithNoTests",
21
+ "build": "webpack --config webpack.config.js",
22
+ "lint": "eslint --ext .js ./lib",
23
+ "format": "prettier --write 'lib/**/*.+(js|json)'",
24
+ "lint-format": "lint-staged",
25
+ "prepare": "husky install"
26
+ },
27
+ "keywords": [
28
+ "JavaScript",
29
+ "Browser",
30
+ "SDK"
31
+ ],
32
+ "dependencies": {
33
+ "axios": "0.24.0"
34
+ },
35
+ "peerDependencies": {
36
+ "axios": "0.24.0"
37
+ },
38
+ "devDependencies": {
39
+ "@apolitical/eslint-config": "0.0.1",
40
+ "@babel/core": "7.16.7",
41
+ "babel-eslint": "10.1.0",
42
+ "babel-loader": "8.2.3",
43
+ "babel-preset-react-app": "10.0.1",
44
+ "husky": "7.0.4",
45
+ "jest": "27.4.7",
46
+ "jest-junit": "13.0.0",
47
+ "lint-staged": "12.1.5",
48
+ "webpack": "5.65.0",
49
+ "webpack-cli": "4.9.1"
50
+ },
51
+ "eslintConfig": {
52
+ "extends": [
53
+ "eslint:recommended",
54
+ "plugin:prettier/recommended",
55
+ "plugin:jest/recommended"
56
+ ],
57
+ "env": {
58
+ "browser": true,
59
+ "node": true,
60
+ "es6": true,
61
+ "jest": true
62
+ },
63
+ "plugins": [
64
+ "jest"
65
+ ],
66
+ "parser": "babel-eslint"
67
+ },
68
+ "prettier": "@apolitical/eslint-config/prettier.config",
69
+ "browserslist": {
70
+ "production": [
71
+ ">0.2%",
72
+ "not dead",
73
+ "not op_mini all",
74
+ "ie 11"
75
+ ]
76
+ },
77
+ "jest": {
78
+ "bail": true,
79
+ "clearMocks": true,
80
+ "collectCoverage": true,
81
+ "collectCoverageFrom": [
82
+ "<rootDir>/lib/**/*.js"
83
+ ],
84
+ "coveragePathIgnorePatterns": [
85
+ "<rootDir>/node_modules/"
86
+ ],
87
+ "setupFiles": [
88
+ "./test/setupTests.js"
89
+ ],
90
+ "reporters": [
91
+ "default",
92
+ "jest-junit"
93
+ ],
94
+ "testResultsProcessor": "jest-junit"
95
+ },
96
+ "babel": {
97
+ "presets": [
98
+ "react-app"
99
+ ]
100
+ },
101
+ "engines": {
102
+ "node": ">=12.20.1"
103
+ },
104
+ "lint-staged": {
105
+ "*.js": "eslint --cache --fix --ignore-path .gitignore",
106
+ "*.+(js|json)": "prettier --write --ignore-path .gitignore"
107
+ }
108
+ }
@@ -0,0 +1,4 @@
1
+ // Overrides console error & warn when testing
2
+ // Importnat: Comment out this line to check errors when testing
3
+ console.error = jest.fn();
4
+ console.warn = jest.fn();
@@ -0,0 +1,27 @@
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
+ });
@@ -0,0 +1,28 @@
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
+ };