ag-common 0.0.5 → 0.0.9

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/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ ISC License
2
+
3
+ Copyright 2021 Andrei Gec
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # ag-common
2
+
3
+ > Various reusable libaries for: common ts/js / api (aws cognito, aws-cdk) / ui (react)
4
+
5
+ [![NPM Version][npm-image]][npm-url]
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm i -S ag-common
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Please raise any issues or requests you might have [here](https://github.com/andreigec/ag-common/issues)
16
+
17
+ ## License
18
+
19
+ [ISC](./LICENSE.md)
20
+
21
+ [npm-image]: https://img.shields.io/npm/v/ag-common.svg
22
+ [npm-url]: https://www.npmjs.com/package/ag-common
@@ -68,7 +68,7 @@ const getAndValidateToken = ({ tokenRaw, COGNITO_USER_POOL_ID, }) => __awaiter(v
68
68
  error: (0, api_1.returnCode)(403, 'auth failed'),
69
69
  };
70
70
  }
71
- token = tokenRaw.substr(tokenRaw.indexOf(' ') + 1);
71
+ token = tokenRaw.substring(tokenRaw.indexOf(' ') + 1);
72
72
  let subject;
73
73
  try {
74
74
  yield jwtVerify({ token, COGNITO_USER_POOL_ID });
@@ -6,4 +6,13 @@ export interface ISite {
6
6
  siteUrl: string;
7
7
  niceSiteUrl: string;
8
8
  }
9
+ /**
10
+ * removes protocol, and trailing slashes
11
+ */
9
12
  export declare const niceUrl: (siteUrl: string) => string | ISite;
13
+ /**
14
+ * string -> String
15
+ * @param str
16
+ * @returns
17
+ */
18
+ export declare function toTitleCase(str: string): string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.niceUrl = exports.truncate = exports.trim = exports.trimSide = exports.csvJSON = void 0;
3
+ exports.toTitleCase = exports.niceUrl = exports.truncate = exports.trim = exports.trimSide = exports.csvJSON = void 0;
4
4
  const csvJSON = (csv) => {
5
5
  const lines = csv.split('\n');
6
6
  const result = [];
@@ -47,12 +47,15 @@ function truncate(str, n, ellip) {
47
47
  return str.length > n ? str.substr(0, n - 1) + ellip : str;
48
48
  }
49
49
  exports.truncate = truncate;
50
+ /**
51
+ * removes protocol, and trailing slashes
52
+ */
50
53
  const niceUrl = (siteUrl) => {
51
54
  if (!siteUrl) {
52
55
  return siteUrl;
53
56
  }
54
57
  let niceSiteUrl = siteUrl
55
- .substr(siteUrl.indexOf(':') + 1)
58
+ .substring(siteUrl.indexOf(':') + 1)
56
59
  .replace('sc-domain:', '')
57
60
  .replace('https://', '')
58
61
  .replace('http://', '');
@@ -60,3 +63,15 @@ const niceUrl = (siteUrl) => {
60
63
  return { siteUrl, niceSiteUrl };
61
64
  };
62
65
  exports.niceUrl = niceUrl;
66
+ /**
67
+ * string -> String
68
+ * @param str
69
+ * @returns
70
+ */
71
+ function toTitleCase(str) {
72
+ if (!str) {
73
+ return str;
74
+ }
75
+ return str.replace(/\w\S*/g, (txt) => txt && txt.charAt(0).toUpperCase() + txt.substring(1).toLowerCase());
76
+ }
77
+ exports.toTitleCase = toTitleCase;
@@ -21,7 +21,7 @@ const LoginButton = ({ className, text = text_1.getstarted, invert, title, saveP
21
21
  const lp = loginPath(!savePath || typeof window === 'undefined'
22
22
  ? undefined
23
23
  : {
24
- redirect: window.location.href.substr(window.location.origin.length),
24
+ redirect: window.location.href.substring(window.location.origin.length),
25
25
  });
26
26
  return (react_1.default.createElement(Button_1.Button, { lang: lang, title: title, invert: invert, className: className, onKeyPress: () => __awaiter(void 0, void 0, void 0, function* () {
27
27
  yield pushPath(lp);
@@ -0,0 +1,11 @@
1
+ import { AxiosResponse } from 'axios';
2
+ export declare const axiosHelper: <TOut>({ verb, url, data, headers, timeout, retryMax, }: {
3
+ headers?: {
4
+ [a: string]: string;
5
+ } | undefined;
6
+ verb: 'put' | 'post' | 'get' | 'patch' | 'delete';
7
+ url: string;
8
+ data?: unknown;
9
+ timeout?: number | undefined;
10
+ retryMax?: number | undefined;
11
+ }) => Promise<AxiosResponse<TOut, any>>;
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.axiosHelper = void 0;
16
+ const axios_1 = __importDefault(require("axios"));
17
+ const object_1 = require("../../common/helpers/object");
18
+ const axiosHelper = ({ verb, url, data, headers, timeout = 30000, retryMax = 0, }) => __awaiter(void 0, void 0, void 0, function* () {
19
+ let retry = 0;
20
+ do {
21
+ try {
22
+ const setHeaders = Object.assign({ accept: 'application/json' }, headers);
23
+ if (verb === 'get') {
24
+ const ret = yield axios_1.default.get(url, {
25
+ headers: setHeaders,
26
+ timeout,
27
+ timeoutErrorMessage: `${url} timeout`,
28
+ });
29
+ return ret;
30
+ }
31
+ let noBody = false;
32
+ let func = axios_1.default.post;
33
+ if (verb === 'put') {
34
+ func = axios_1.default.put;
35
+ }
36
+ else if (verb === 'post') {
37
+ func = axios_1.default.post;
38
+ }
39
+ else if (verb === 'patch') {
40
+ func = axios_1.default.patch;
41
+ }
42
+ else if (verb === 'delete') {
43
+ func = axios_1.default.delete;
44
+ noBody = true;
45
+ }
46
+ let ret;
47
+ if (noBody) {
48
+ ret = yield func(url, {
49
+ headers: setHeaders,
50
+ timeout,
51
+ timeoutErrorMessage: `${url} timeout`,
52
+ });
53
+ }
54
+ else {
55
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
56
+ if (data && (0, object_1.isJson)(data)) {
57
+ setHeaders['Content-Type'] = 'application/json';
58
+ }
59
+ ret = yield func(url, data, { headers: setHeaders });
60
+ }
61
+ return ret;
62
+ }
63
+ catch (e) {
64
+ const em = e;
65
+ // jwt expired or bad response
66
+ // 403 returned for old token - will be refreshed
67
+ if (em.code === '401' || em.code === '403') {
68
+ // eslint-disable-next-line no-console
69
+ console.log('auth expired, reset');
70
+ // retry current page
71
+ window.location.reload();
72
+ retry = retryMax;
73
+ }
74
+ if (retry >= retryMax) {
75
+ throw em;
76
+ }
77
+ }
78
+ retry += 1;
79
+ } while (retry <= retryMax);
80
+ throw new Error('unexpected');
81
+ });
82
+ exports.axiosHelper = axiosHelper;
@@ -26,7 +26,7 @@ function getCookie({ cname, cookieDocument, }) {
26
26
  const ca = ca1.split(';').map((t) => t.trim());
27
27
  for (const c of ca) {
28
28
  if (c.indexOf(name) === 0) {
29
- return c.substring(name.length, c.length);
29
+ return c.substr(name.length, c.length);
30
30
  }
31
31
  }
32
32
  return undefined;
@@ -1,3 +1,4 @@
1
+ export * from './axiosHelper';
1
2
  export * from './browserHelpers';
2
3
  export * from './callOpenApi';
3
4
  export * from './cookie';
@@ -10,6 +10,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./axiosHelper"), exports);
13
14
  __exportStar(require("./browserHelpers"), exports);
14
15
  __exportStar(require("./callOpenApi"), exports);
15
16
  __exportStar(require("./cookie"), exports);
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.5",
3
+ "version": "0.0.9",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
+ "author": "Andrei Gec <@andreigec> (https://gec.dev/)",
7
+ "repository": "github:andreigec/ag-common",
8
+ "license": "ISC",
6
9
  "dependencies": {
7
10
  "aws-lambda": "1.0.7",
8
11
  "styled-components": "5.3.3",
@@ -48,6 +51,8 @@
48
51
  "start": "tsc --watch"
49
52
  },
50
53
  "files": [
51
- "dist/**/*"
54
+ "dist/**/*",
55
+ "README.md",
56
+ "LICENSE.md"
52
57
  ]
53
58
  }