@spritz-finance/service-client 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ interface GraphClientConfig {
2
+ env: string;
3
+ authEndpoint: string;
4
+ graphEndpoint: string;
5
+ }
6
+ export declare const config: GraphClientConfig;
7
+ export {};
package/lib/config.js ADDED
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ var _a;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.config = void 0;
5
+ const config_1 = require("@spritz-finance/config");
6
+ const envConfigs = {
7
+ base: {
8
+ env: (_a = process.env.STAGE) !== null && _a !== void 0 ? _a : process.env.NODE_ENV,
9
+ },
10
+ test: {
11
+ env: 'dev',
12
+ authEndpoint: 'https://auth-dev.spritz.finance/oauth2/token',
13
+ graphEndpoint: 'https://api-dev.spritz.finance/graphql',
14
+ },
15
+ dev: {
16
+ authEndpoint: 'https://auth-dev.spritz.finance/oauth2/token',
17
+ graphEndpoint: 'https://api-dev.spritz.finance/graphql',
18
+ },
19
+ staging: {
20
+ authEndpoint: 'https://auth-staging.spritz.finance/oauth2/token',
21
+ graphEndpoint: 'https://api-staging.spritz.finance/graphql',
22
+ },
23
+ production: {
24
+ authEndpoint: 'https://auth.spritz.finance/oauth2/token',
25
+ graphEndpoint: 'https://api.spritz.finance/graphql',
26
+ },
27
+ };
28
+ exports.config = (0, config_1.setupEnvConfig)(envConfigs);
@@ -0,0 +1 @@
1
+ export declare const graphClient: import("axios").AxiosInstance;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __importDefault = (this && this.__importDefault) || function (mod) {
22
+ return (mod && mod.__esModule) ? mod : { "default": mod };
23
+ };
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ exports.graphClient = void 0;
26
+ const qs = __importStar(require("qs"));
27
+ const axios_1 = __importDefault(require("axios"));
28
+ const axios_token_interceptor_1 = __importDefault(require("axios-token-interceptor"));
29
+ const params_1 = require("./params");
30
+ const config_1 = require("./config");
31
+ const getCredentials = async () => {
32
+ const clientIdParam = `/${config_1.config.env}/auth/serviceClientId`;
33
+ const clientSecretParam = `/${config_1.config.env}/auth/serviceClientSecret`;
34
+ const paramsByKey = await (0, params_1.getParams)([clientIdParam, clientSecretParam]);
35
+ const { data } = await axios_1.default.post(config_1.config.authEndpoint, qs.stringify({
36
+ grant_type: 'client_credentials',
37
+ scope: 'spritz/service',
38
+ client_id: paramsByKey[clientIdParam],
39
+ client_secret: paramsByKey[clientSecretParam],
40
+ }));
41
+ return data;
42
+ };
43
+ const getMaxAge = (res) => {
44
+ return res.expires_in * 1000;
45
+ };
46
+ const headerFormatter = (res) => {
47
+ return 'Bearer ' + res.access_token;
48
+ };
49
+ const interceptor = function (tokenProvider, authenticate) {
50
+ const getToken = tokenProvider.tokenCache(authenticate, { getMaxAge });
51
+ return tokenProvider({ getToken, headerFormatter });
52
+ };
53
+ exports.graphClient = axios_1.default.create({
54
+ baseURL: config_1.config.graphEndpoint,
55
+ });
56
+ exports.graphClient.interceptors.request.use(interceptor(axios_token_interceptor_1.default, getCredentials));
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const graphClient_1 = require("./graphClient");
4
+ const query = `
5
+ query ASDF {
6
+ getUsers {
7
+ email
8
+ }
9
+ }
10
+ `;
11
+ describe('graphClient', () => {
12
+ it('should work', async () => {
13
+ try {
14
+ const { data } = await graphClient_1.graphClient.post('', {
15
+ query,
16
+ });
17
+ console.log(data);
18
+ }
19
+ catch (e) {
20
+ console.error(e);
21
+ }
22
+ });
23
+ });
package/lib/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './graphClient';
package/lib/index.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./graphClient"), exports);
@@ -0,0 +1,2 @@
1
+ import { SSM } from 'aws-sdk';
2
+ export declare const getParams: (keys: string[]) => Promise<SSM.Parameter>;
package/lib/params.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getParams = void 0;
4
+ const aws_sdk_1 = require("aws-sdk");
5
+ const ssm = new aws_sdk_1.SSM({
6
+ region: 'us-east-1',
7
+ });
8
+ const getParams = async (keys) => {
9
+ const params = await ssm
10
+ .getParameters({
11
+ Names: keys,
12
+ WithDecryption: true,
13
+ })
14
+ .promise();
15
+ return params.Parameters.reduce((acc, val) => {
16
+ return {
17
+ ...acc,
18
+ [val.Name]: val.Value,
19
+ };
20
+ }, {});
21
+ };
22
+ exports.getParams = getParams;
File without changes
File without changes
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@spritz-finance/service-client",
3
+ "version": "0.1.0",
4
+ "description": "Service client",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "files": [
8
+ "lib/**/*"
9
+ ],
10
+ "scripts": {
11
+ "prebuild": "rimraf lib/*",
12
+ "prepare": "npm run build",
13
+ "test": "jest",
14
+ "build": "tsc",
15
+ "lint": "eslint \"src/**/*.ts\"",
16
+ "lint:fix": "eslint \"src/**/*.ts\" --fix"
17
+ },
18
+ "author": "",
19
+ "license": "ISC",
20
+ "dependencies": {
21
+ "axios": "^0.26.1",
22
+ "axios-oauth-client": "^1.4.4",
23
+ "axios-token-interceptor": "^0.2.0"
24
+ },
25
+ "devDependencies": {
26
+ "aws-sdk": "2.1115.0",
27
+ "jest": "27.5.1",
28
+ "ts-jest": "^27.1.3",
29
+ "typescript": "4.5.5"
30
+ },
31
+ "jest": {
32
+ "moduleFileExtensions": [
33
+ "js",
34
+ "json",
35
+ "ts"
36
+ ],
37
+ "rootDir": "src",
38
+ "testRegex": ".*\\.test\\.ts$",
39
+ "transform": {
40
+ "^.+\\.(t|j)s$": "ts-jest"
41
+ },
42
+ "collectCoverageFrom": [
43
+ "**/*.(t|j)s"
44
+ ],
45
+ "coverageDirectory": "../coverage",
46
+ "testEnvironment": "node"
47
+ },
48
+ "gitHead": "5bf0766fae7a16e910652c4f7516197192568ead"
49
+ }