ag-common 0.0.127 → 0.0.132

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.
Files changed (34) hide show
  1. package/dist/api/helpers/api.d.ts +7 -1
  2. package/dist/api/helpers/api.js +9 -8
  3. package/dist/common/helpers/string.d.ts +1 -0
  4. package/dist/common/helpers/string.js +3 -1
  5. package/dist/ui/components/Chevron/index.d.ts +1 -0
  6. package/dist/ui/components/Chevron/index.js +6 -6
  7. package/dist/ui/components/DropdownList/index.d.ts +1 -0
  8. package/dist/ui/components/HeadersRaw/index.d.ts +1 -0
  9. package/dist/ui/components/Loader/index.d.ts +1 -0
  10. package/dist/ui/components/LoginButton/index.d.ts +1 -0
  11. package/dist/ui/components/LogoutButton/index.d.ts +1 -0
  12. package/dist/ui/components/Prompt/index.js +2 -2
  13. package/dist/ui/components/Sidebar/index.d.ts +2 -1
  14. package/dist/ui/components/Sidebar/index.js +3 -5
  15. package/dist/ui/components/Table/index.d.ts +1 -0
  16. package/dist/ui/components/TextEdit/CheckboxEdit.d.ts +1 -0
  17. package/dist/ui/components/TextEdit/ColourEdit.d.ts +1 -0
  18. package/dist/ui/components/TextEdit/ListboxEdit.d.ts +1 -0
  19. package/dist/ui/components/TextEdit/TextEdit.d.ts +9 -1
  20. package/dist/ui/components/TextEdit/TextEdit.js +5 -4
  21. package/dist/ui/components/TextEdit/images.d.ts +1 -0
  22. package/dist/ui/components/Toast/index.d.ts +1 -0
  23. package/dist/ui/components/UserImage/index.d.ts +1 -0
  24. package/dist/ui/components/index.d.ts +0 -1
  25. package/dist/ui/components/index.js +0 -1
  26. package/dist/ui/helpers/callOpenApi/cached.js +16 -9
  27. package/dist/ui/helpers/callOpenApi/direct.js +5 -1
  28. package/dist/ui/helpers/callOpenApi/hook.js +2 -1
  29. package/dist/ui/helpers/cookie.d.ts +55 -11
  30. package/dist/ui/helpers/cookie.js +103 -58
  31. package/dist/ui/helpers/lang.d.ts +1 -0
  32. package/package.json +1 -1
  33. package/dist/ui/components/TextInput/index.d.ts +0 -11
  34. package/dist/ui/components/TextInput/index.js +0 -53
@@ -2,7 +2,13 @@ import { APIGatewayProxyResult, DYNAMOKEYS } from '../types';
2
2
  export declare const returnCode: <T>(statusCode: number, body?: T | undefined, extraHeaders?: {
3
3
  [a: string]: string;
4
4
  } | undefined, fullSiteUrl?: string | undefined) => APIGatewayProxyResult;
5
- export declare const stripPKs: <T>(r: T, excludePK?: boolean) => T;
5
+ /**
6
+ * strip all dynamo generated keys. can optionally keep PK
7
+ * @param record
8
+ * @param keepPk if true, will keep PK. default true
9
+ * @returns stripped record
10
+ */
11
+ export declare const stripPKs: <T>(record: T, keepPk?: boolean) => T;
6
12
  export declare const generateDynamoPKS: ({ type, L1, L2, L3, L4, L5, L6, additionalPKValues, }: {
7
13
  type: string;
8
14
  L1: string;
@@ -33,21 +33,22 @@ const returnCode = (statusCode, body, extraHeaders, fullSiteUrl) => {
33
33
  };
34
34
  };
35
35
  exports.returnCode = returnCode;
36
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
- const stripPKs = (r,
38
36
  /**
39
- * default true
40
- * if true, will keep PK
37
+ * strip all dynamo generated keys. can optionally keep PK
38
+ * @param record
39
+ * @param keepPk if true, will keep PK. default true
40
+ * @returns stripped record
41
41
  */
42
- excludePK = true) => {
43
- if (!r) {
42
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
+ const stripPKs = (record, keepPk = true) => {
44
+ if (!record) {
44
45
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
45
46
  return null;
46
47
  }
47
48
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
48
49
  // @ts-ignore
49
- const { PK, PK1, PK2, PK3, PK4, PK5, L1, L2, L3, L4, L5, L6 } = r, rest = __rest(r, ["PK", "PK1", "PK2", "PK3", "PK4", "PK5", "L1", "L2", "L3", "L4", "L5", "L6"]);
50
- if (!excludePK) {
50
+ const { PK, PK1, PK2, PK3, PK4, PK5, L1, L2, L3, L4, L5, L6 } = record, rest = __rest(record, ["PK", "PK1", "PK2", "PK3", "PK4", "PK5", "L1", "L2", "L3", "L4", "L5", "L6"]);
51
+ if (keepPk) {
51
52
  //@ts-ignore
52
53
  rest.PK = PK;
53
54
  }
@@ -33,3 +33,4 @@ export declare function containsInsensitive(str: string, ...args: string[]): boo
33
33
  * @returns
34
34
  */
35
35
  export declare const safeStringify: (obj: any, indent?: number) => string;
36
+ export declare const chunkString: (str: string, length: number) => string[];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.safeStringify = exports.containsInsensitive = exports.replaceRemove = exports.toTitleCase = exports.niceUrl = exports.truncate = exports.trim = exports.trimSide = exports.csvJSON = exports.fromBase64 = exports.toBase64 = void 0;
3
+ exports.chunkString = exports.safeStringify = exports.containsInsensitive = exports.replaceRemove = exports.toTitleCase = exports.niceUrl = exports.truncate = exports.trim = exports.trimSide = exports.csvJSON = exports.fromBase64 = exports.toBase64 = void 0;
4
4
  const toBase64 = (str) => Buffer.from(str).toString('base64');
5
5
  exports.toBase64 = toBase64;
6
6
  const fromBase64 = (str) => Buffer.from(decodeURIComponent(str), 'base64').toString();
@@ -140,3 +140,5 @@ const safeStringify = (obj, indent = 2) => {
140
140
  return retVal;
141
141
  };
142
142
  exports.safeStringify = safeStringify;
143
+ const chunkString = (str, length) => str.match(new RegExp(`.{1,${length}}`, 'g'));
144
+ exports.chunkString = chunkString;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const Chevron: ({ width, className, colour, onToggle, point, }: {
2
3
  /**
3
4
  * default right
@@ -18,25 +18,25 @@ const IconStyled = (0, styled_components_1.default)(Icon_1.Icon) `
18
18
  margin: 0;
19
19
  padding: 0;
20
20
  `;
21
- const ChevronIcon = (react_1.default.createElement("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 -256 1792 1792" },
22
- react_1.default.createElement("path", { d: "M1679.339 301.56q0 53-37 90l-651 651q-38 38-91 38-54 0-90-38l-651-651q-38-36-38-90 0-53 38-91l74-75q39-37 91-37 53 0 90 37l486 486 486-486q37-37 90-37 52 0 91 37l75 75q37 39 37 91z" })));
21
+ const ChevronIcon = (react_1.default.createElement("svg", { width: "24", height: "24", fill: "none", xmlns: "http://www.w3.org/2000/svg" },
22
+ react_1.default.createElement("path", { d: "M10.294 9.698a.988.988 0 0 1 0-1.407 1.01 1.01 0 0 1 1.419 0l2.965 2.94a1.09 1.09 0 0 1 0 1.548l-2.955 2.93a1.01 1.01 0 0 1-1.638-.322.988.988 0 0 1 .218-1.085l2.318-2.297-2.327-2.307Z", fill: "inherit", className: "_14TyY7wB85QoVU" })));
23
23
  const Chevron = ({ width = '1.2rem', className, colour = 'black', onToggle, point = 'right', }) => {
24
24
  let rotate = 0;
25
25
  switch (point) {
26
26
  case 'down': {
27
- rotate = 180;
27
+ rotate = 270;
28
28
  break;
29
29
  }
30
30
  case 'left': {
31
- rotate = 270;
31
+ rotate = 0;
32
32
  break;
33
33
  }
34
34
  case 'up': {
35
- rotate = 0;
35
+ rotate = 90;
36
36
  break;
37
37
  }
38
38
  case 'right': {
39
- rotate = 90;
39
+ rotate = 180;
40
40
  }
41
41
  }
42
42
  return (react_1.default.createElement(SChevron, { className: className, onClick: () => onToggle === null || onToggle === void 0 ? void 0 : onToggle(), onTouchStart: () => onToggle === null || onToggle === void 0 ? void 0 : onToggle(), onKeyPress: (e) => e.key === 'Enter' && (onToggle === null || onToggle === void 0 ? void 0 : onToggle()) },
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare function DropdownList<T>({ options, value, onChange, placeholder, className, renderF, children, shadow, }: {
2
3
  options: T[];
3
4
  value?: T;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const HeadersRaw: ({ title, image, SiteShort, FullSiteUrl, siteDesc, }: {
2
3
  title?: string | undefined;
3
4
  image?: string | undefined;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const Loader: ({ name }: {
2
3
  name: string;
3
4
  }) => JSX.Element;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const LoginButton: ({ className, text, invert, savePath, loginPath, }: {
2
3
  invert?: boolean | undefined;
3
4
  text: string;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const LogoutButton: ({ className, invert, logout, }: {
2
3
  invert?: boolean | undefined;
3
4
  className?: string | undefined;
@@ -41,7 +41,7 @@ const Button_1 = require("../Button");
41
41
  const FlexColumn_1 = require("../FlexColumn");
42
42
  const FlexRow_1 = require("../FlexRow");
43
43
  const Modal_1 = require("../Modal");
44
- const TextInput_1 = require("../TextInput");
44
+ const TextEdit_1 = require("../TextEdit");
45
45
  const Base = styled_components_1.default.div `
46
46
  width: 95vw;
47
47
  max-width: 30rem;
@@ -85,7 +85,7 @@ const PromptModal = ({ wrapper, res, bottomText, topText, okText = 'OK', cancelT
85
85
  react_1.default.createElement(Content, null,
86
86
  topText && react_1.default.createElement(TopText, null, topText),
87
87
  react_1.default.createElement(BottomText, null, bottomText),
88
- react_1.default.createElement(TextInput_1.TextInput, { value: text, onChange: (c) => setText(c), placeholder: placeholder, focus: true, onKeyPress: (e) => e.key === 'Enter' && ret(text) }),
88
+ react_1.default.createElement(TextEdit_1.TextEdit, { defaultValue: text, onSubmit: (c) => setText(c), placeholder: placeholder, defaultEditing: { focus: true }, singleLine: true, noGrow: true }),
89
89
  react_1.default.createElement(Bottom, { noGrow: true },
90
90
  react_1.default.createElement(Button_1.Button, { onClick: () => ret(text) }, okText),
91
91
  react_1.default.createElement(Button_1.Button, { invert: true, onClick: () => ret(undefined) }, cancelText))))));
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const Sidebar: ({ children, className, key, cookieDocument, }: {
2
3
  children: any;
3
4
  className?: string | undefined;
@@ -6,7 +7,7 @@ export declare const Sidebar: ({ children, className, key, cookieDocument, }: {
6
7
  */
7
8
  key?: string | undefined;
8
9
  /**
9
- * pass in SSR cookiedocument
10
+ * optionally pass in SSR cookiedocument
10
11
  */
11
12
  cookieDocument?: string | undefined;
12
13
  }) => JSX.Element;
@@ -92,13 +92,11 @@ const ChevronStyled = (0, styled_components_1.default)(Chevron_1.Chevron) `
92
92
  }
93
93
  `;
94
94
  const Sidebar = ({ children, className, key = 'sidebar', cookieDocument, }) => {
95
- const [openRaw, setOpenRaw] = (0, cookie_1.useCookie)({
96
- key,
97
- defaultValue: 'false',
95
+ const [open, setOpen] = (0, cookie_1.useCookieBoolean)({
96
+ name: key,
97
+ defaultValue: false,
98
98
  cookieDocument: cookieDocument,
99
99
  });
100
- const open = openRaw === 'true';
101
- const setOpen = (o) => setOpenRaw(o.toString());
102
100
  return (react_1.default.createElement(Base, { className: className, "data-open": open, onClick: () => !open && setOpen(true), "data-hover": true },
103
101
  react_1.default.createElement(Hamburger, { "data-open": open, onClick: () => setOpen(!open), "data-hover": true },
104
102
  react_1.default.createElement(ChevronStyled, { point: open ? 'right' : 'left', width: "100%" })),
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export interface TableItem {
2
3
  content: JSX.Element;
3
4
  groupTitle: string;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const CheckboxEdit: ({ defaultValue, onSubmit, noGrow, }: {
2
3
  defaultValue: boolean;
3
4
  onSubmit: (val: boolean) => void;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const ColourEdit: ({ defaultValue, onSubmit, }: {
2
3
  defaultValue: string;
3
4
  onSubmit: (val: string) => void;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const ListboxEdit: ({ defaultValue, onSubmit, values, }: {
2
3
  defaultValue: string;
3
4
  onSubmit: (val: string[]) => void;
@@ -1,6 +1,10 @@
1
+ /// <reference types="react" />
1
2
  import { StyledComponent } from 'styled-components';
2
3
  export declare const ValueReadonly: StyledComponent<"div", any, {}, never>;
3
- export declare const TextEdit: ({ defaultValue, defaultEditing, onSubmit, disableEdit, placeholder, onEditingChange, onClickOutsideWithNoValue, onClickNotEditing, className, singleLine, }: {
4
+ export declare const TextEdit: ({ defaultValue, defaultEditing, onSubmit, disableEdit, placeholder, onEditingChange, onClickOutsideWithNoValue, onClickNotEditing, className, singleLine, noGrow, }: {
5
+ /**
6
+ * forces single row input style. will also enable 'Enter' to auto submit
7
+ */
4
8
  singleLine?: boolean | undefined;
5
9
  className?: string | undefined;
6
10
  defaultValue: string;
@@ -16,4 +20,8 @@ export declare const TextEdit: ({ defaultValue, defaultEditing, onSubmit, disabl
16
20
  onEditingChange?: ((editing: boolean) => void) | undefined;
17
21
  onClickOutsideWithNoValue?: (() => void) | undefined;
18
22
  onClickNotEditing?: (() => void) | undefined;
23
+ /**
24
+ * if true, will not grow. default false
25
+ */
26
+ noGrow?: boolean | undefined;
19
27
  }) => JSX.Element;
@@ -63,12 +63,13 @@ const Right = styled_components_1.default.div `
63
63
  `;
64
64
  const Icon = styled_components_1.default.div `
65
65
  width: 1.5rem;
66
+ display: flex;
66
67
  cursor: pointer;
67
68
  &:hover {
68
69
  filter: saturate(3);
69
70
  }
70
71
  `;
71
- const TextEdit = ({ defaultValue, defaultEditing, onSubmit, disableEdit = false, placeholder, onEditingChange, onClickOutsideWithNoValue, onClickNotEditing, className, singleLine = false, }) => {
72
+ const TextEdit = ({ defaultValue, defaultEditing, onSubmit, disableEdit = false, placeholder, onEditingChange, onClickOutsideWithNoValue, onClickNotEditing, className, singleLine = false, noGrow = false, }) => {
72
73
  const ref = (0, react_1.useRef)(null);
73
74
  const taref = (0, react_1.useRef)(null);
74
75
  const [value, setValue] = (0, react_1.useState)(defaultValue);
@@ -106,7 +107,7 @@ const TextEdit = ({ defaultValue, defaultEditing, onSubmit, disableEdit = false,
106
107
  // eslint-disable-next-line react-hooks/exhaustive-deps
107
108
  }, [defaultEditing]);
108
109
  if (!editing || disableEdit) {
109
- return (react_1.default.createElement(common_1.ValueBox, Object.assign({}, common_2.noDrag, { className: className, "data-editing": "false", onClick: () => onClickNotEditing === null || onClickNotEditing === void 0 ? void 0 : onClickNotEditing(), "data-pointer": onClickNotEditing ? 'true' : 'false' }),
110
+ return (react_1.default.createElement(common_1.ValueBox, Object.assign({}, common_2.noDrag, { className: className, "data-editing": "false", onClick: () => onClickNotEditing === null || onClickNotEditing === void 0 ? void 0 : onClickNotEditing(), "data-pointer": onClickNotEditing ? 'true' : 'false', "data-nogrow": noGrow }),
110
111
  react_1.default.createElement(exports.ValueReadonly, { "data-type": "text" }, value),
111
112
  react_1.default.createElement(Right, null, !disableEdit && (react_1.default.createElement(Icon, { style: common_1.iconRight, onClick: (e) => {
112
113
  e.stopPropagation();
@@ -122,9 +123,9 @@ const TextEdit = ({ defaultValue, defaultEditing, onSubmit, disableEdit = false,
122
123
  ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
123
124
  ValueTextBox
124
125
  : ValueTextArea;
125
- return (react_1.default.createElement(ValueBoxEdit, Object.assign({ "data-editing": "true" }, common_2.noDrag, {
126
+ return (react_1.default.createElement(ValueBoxEdit, Object.assign({}, common_2.noDrag, { className: className, "data-editing": "true",
126
127
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
127
- ref: ref, tabIndex: editing ? 0 : undefined, className: className }),
128
+ ref: ref, tabIndex: editing ? 0 : undefined, "data-nogrow": noGrow }),
128
129
  react_1.default.createElement(Comp, { "data-editing": "true", "data-valuechange": valueChange.toString(), ref: taref, "data-type": "text", value: value, onChange: (v) => setValue(v.currentTarget.value), placeholder: placeholder, rows: singleLine ? 1 : undefined, onKeyDown: (e) => singleLine && e.code.endsWith('Enter') && onSubmit(value) && false }),
129
130
  react_1.default.createElement(Right, null,
130
131
  valueChange && (react_1.default.createElement(Icon, { style: common_1.iconLeft, onClick: () => valueChange && onSubmit(value) },
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export declare const UndoIcon: () => JSX.Element;
2
3
  export declare const PencilIcon: () => JSX.Element;
3
4
  export declare const SaveIcon: () => JSX.Element;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  import { ToastPosition } from 'react-hot-toast';
2
3
  interface Options {
3
4
  appearance: 'error' | 'success';
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  import { AxiosWrapper, User } from '../../helpers/jwt';
2
3
  export declare const UserImageIcon: JSX.Element;
3
4
  export declare const UserImage: ({ image, className, }: {
@@ -18,6 +18,5 @@ export * from './RowOrColumn';
18
18
  export * from './Sidebar';
19
19
  export * from './Table';
20
20
  export * from './TextEdit';
21
- export * from './TextInput';
22
21
  export * from './Toast';
23
22
  export * from './UserImage';
@@ -30,6 +30,5 @@ __exportStar(require("./RowOrColumn"), exports);
30
30
  __exportStar(require("./Sidebar"), exports);
31
31
  __exportStar(require("./Table"), exports);
32
32
  __exportStar(require("./TextEdit"), exports);
33
- __exportStar(require("./TextInput"), exports);
34
33
  __exportStar(require("./Toast"), exports);
35
34
  __exportStar(require("./UserImage"), exports);
@@ -19,7 +19,12 @@ const direct_1 = require("./direct");
19
19
  const string_1 = require("../../../common/helpers/string");
20
20
  let callOpenApiCache;
21
21
  function getCacheKey({ cacheKey, overrideAuth, }) {
22
- const authkeyPrefix = (overrideAuth === null || overrideAuth === void 0 ? void 0 : overrideAuth.id_token) || (0, cookie_1.getCookieWrapper)({ cname: 'id_token' });
22
+ const authkeyPrefix = (overrideAuth === null || overrideAuth === void 0 ? void 0 : overrideAuth.id_token) ||
23
+ (0, cookie_1.getCookieWrapper)({
24
+ name: 'id_token',
25
+ defaultValue: '',
26
+ parse: (s) => s,
27
+ });
23
28
  let cacheKeyRet;
24
29
  if (cacheKey) {
25
30
  const pref = !authkeyPrefix ? '' : (0, string_1.toBase64)(authkeyPrefix);
@@ -41,16 +46,18 @@ const callOpenApiCachedRaw = (p) => {
41
46
  if (!callOpenApiCache) {
42
47
  callOpenApiCache = new node_cache_1.default({ stdTTL: p.cacheTtl || 120 });
43
48
  }
44
- const ssrCached = (_a = p.ssrCacheItems) === null || _a === void 0 ? void 0 : _a.find((s) => s.cacheKey === p.cacheKey);
45
- if (!callOpenApiCache.get(userPrefixedCacheKey) &&
46
- ((_b = ssrCached === null || ssrCached === void 0 ? void 0 : ssrCached.prefillData) === null || _b === void 0 ? void 0 : _b.data)) {
47
- callOpenApiCache.set(userPrefixedCacheKey, (_c = ssrCached.prefillData) === null || _c === void 0 ? void 0 : _c.data);
49
+ //get ssr cache value
50
+ const ssrCached = (_c = (_b = (_a = p.ssrCacheItems) === null || _a === void 0 ? void 0 : _a.find((s) => s.cacheKey === p.cacheKey)) === null || _b === void 0 ? void 0 : _b.prefillData) === null || _c === void 0 ? void 0 : _c.data;
51
+ //if we have ssr cache and there is no existing cache then set
52
+ if (!callOpenApiCache.get(userPrefixedCacheKey) && ssrCached) {
53
+ callOpenApiCache.set(userPrefixedCacheKey, ssrCached);
48
54
  }
49
- const cached = callOpenApiCache.get(userPrefixedCacheKey);
50
- if (cached) {
51
- return { data: cached };
55
+ //return cached data, or ssr data if that has already expired (ttl <=0)
56
+ const data = callOpenApiCache.get(userPrefixedCacheKey) || ssrCached;
57
+ if (!data) {
58
+ return undefined;
52
59
  }
53
- return undefined;
60
+ return { data };
54
61
  };
55
62
  exports.callOpenApiCachedRaw = callOpenApiCachedRaw;
56
63
  const callOpenApiCached = (p) => __awaiter(void 0, void 0, void 0, function* () {
@@ -26,7 +26,11 @@ const callOpenApi = ({ func, apiUrl, overrideAuth, refreshToken, logout, newDefa
26
26
  config.baseOptions.headers.authorization = `Bearer ${overrideAuth === null || overrideAuth === void 0 ? void 0 : overrideAuth.id_token}`;
27
27
  }
28
28
  else {
29
- const isAuthed = !!(0, cookie_1.getCookieWrapper)({ cname: 'id_token' });
29
+ const isAuthed = !!(0, cookie_1.getCookieWrapper)({
30
+ name: 'id_token',
31
+ defaultValue: '',
32
+ parse: (s) => s,
33
+ });
30
34
  if (isAuthed) {
31
35
  const updated = yield refreshToken();
32
36
  if ((_a = updated === null || updated === void 0 ? void 0 : updated.jwt) === null || _a === void 0 ? void 0 : _a.id_token) {
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.useCallOpenApi = void 0;
13
13
  const react_1 = require("react");
14
14
  const cached_1 = require("./cached");
15
+ const direct_1 = require("./direct");
15
16
  /**
16
17
  * hooks+cached call to callOpenApi
17
18
  * @param p
@@ -33,7 +34,7 @@ const useCallOpenApi = (p) => {
33
34
  (0, react_1.useEffect)(() => {
34
35
  function run() {
35
36
  return __awaiter(this, void 0, void 0, function* () {
36
- const resp = yield (0, cached_1.callOpenApiCached)(p);
37
+ const resp = yield (0, direct_1.callOpenApi)(p);
37
38
  setData((d) => (Object.assign(Object.assign({}, resp), { loaded: true, loading: false, loadcount: d.loadcount + 1, reFetch: () => __awaiter(this, void 0, void 0, function* () { }), url: '', datetime: new Date().getTime() })));
38
39
  });
39
40
  }
@@ -1,18 +1,62 @@
1
- export declare function setCookieWrapper<T>(cname: string, raw: T, exdays?: number): void;
2
- export declare function wipeCookies(cname: string): void;
3
- export declare function getCookieWrapper<T>({ cname, cookieDocument, defaultValue, }: {
4
- cname: string;
1
+ export declare function wipeCookies(name: string): void;
2
+ /**
3
+ * json+b64 incoming. chunk. write chunks
4
+ * @param p
5
+ * @returns
6
+ */
7
+ export declare function setCookieWrapper<T>(p: {
8
+ value: T;
9
+ name: string;
10
+ cookieDocument?: string;
5
11
  /**
6
- * set for ssr
12
+ * required for objects. defaults to JSON.stringify
7
13
  */
14
+ stringify?: (s: T) => string;
15
+ }): void;
16
+ /**
17
+ * read chunks. json parse+unb64
18
+ * @param param0
19
+ * @returns
20
+ */
21
+ export declare function getCookieWrapper<T>({ name, cookieDocument, defaultValue, parse: parseRaw, }: {
22
+ defaultValue: T;
23
+ name: string;
8
24
  cookieDocument?: string;
9
- defaultValue?: string;
25
+ /**
26
+ * required for objects. defaults to JSON.parse
27
+ */
28
+ parse?: (s: string) => T;
10
29
  }): T;
11
- export declare function useCookie<T>({ key, defaultValue, cookieDocument, }: {
12
- key: string;
13
- defaultValue?: string;
30
+ declare type Dispatch<A> = (value: A) => void;
31
+ declare type SetStateAction<S> = S | ((prevState: S) => S);
32
+ declare type ReturnType<T> = [T, Dispatch<SetStateAction<T>>];
33
+ declare type TParse<T> = (s: string | null | undefined) => T;
34
+ export declare function useCookie<T>(p: {
35
+ defaultValue: T;
36
+ name: string;
37
+ cookieDocument?: string;
14
38
  /**
15
- * set for ssr
39
+ * required for objects. defaults to JSON.parse
16
40
  */
41
+ parse?: TParse<T>;
42
+ /**
43
+ * required for objects. defaults to JSON.stringify
44
+ */
45
+ stringify?: (v: T) => string;
46
+ }): ReturnType<T>;
47
+ export declare const useCookieString: (p: {
48
+ defaultValue: string;
49
+ name: string;
50
+ cookieDocument?: string;
51
+ }) => ReturnType<string>;
52
+ export declare const useCookieNumber: (p: {
53
+ defaultValue: number | undefined;
54
+ name: string;
55
+ cookieDocument?: string;
56
+ }) => ReturnType<number | undefined>;
57
+ export declare const useCookieBoolean: (p: {
58
+ defaultValue: boolean;
59
+ name: string;
17
60
  cookieDocument?: string;
18
- }): [T, (v: T) => void];
61
+ }) => ReturnType<boolean>;
62
+ export {};
@@ -1,62 +1,55 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useCookie = exports.getCookieWrapper = exports.wipeCookies = exports.setCookieWrapper = void 0;
3
+ exports.useCookieBoolean = exports.useCookieNumber = exports.useCookieString = exports.useCookie = exports.getCookieWrapper = exports.setCookieWrapper = exports.wipeCookies = void 0;
4
4
  /* eslint-disable guard-for-in */
5
5
  /* eslint-disable no-restricted-syntax */
6
6
  const react_1 = require("react");
7
+ const log_1 = require("../../common/helpers/log");
7
8
  const string_1 = require("../../common/helpers/string");
8
9
  const expireDate = 'Thu, 01 Jan 1970 00:00:00 UTC';
9
10
  const maxCookieLen = 4000;
10
- const chunkString = (str, length) => str.match(new RegExp(`.{1,${length}}`, 'g'));
11
- function setCookie(cname, raw, exdays = 1) {
11
+ /**
12
+ * expiryDays <0 will delete
13
+ * @param param0
14
+ * @returns
15
+ */
16
+ function setCookieRaw({ name, value, expiryDays = 1, }) {
12
17
  if (typeof window === undefined) {
13
18
  return;
14
19
  }
15
20
  const d = new Date();
16
- d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
17
- const expires = `expires=${!raw || exdays < 0 ? expireDate : d.toUTCString()}`;
18
- document.cookie = `${cname}=${!raw ? '' : raw};${expires};path=/`;
21
+ d.setTime(d.getTime() + expiryDays * 24 * 60 * 60 * 1000);
22
+ const expires = `expires=${!value || expiryDays < 0 ? expireDate : d.toUTCString()}`;
23
+ document.cookie = `${name}=${!value ? '' : value};${expires};path=/`;
19
24
  }
20
- function getCookie({ cname, cookieDocument, }) {
21
- const name = `${cname}=`;
25
+ function getCookieRaw({ name, cookieDocument, }) {
26
+ const nameeq = `${name}=`;
22
27
  const ca1 = cookieDocument || (typeof window !== 'undefined' && document.cookie);
23
28
  if (!ca1 || !(ca1 === null || ca1 === void 0 ? void 0 : ca1.trim())) {
24
29
  return undefined;
25
30
  }
26
31
  const ca = ca1.split(';').map((t) => t.trim());
27
32
  for (const c of ca) {
28
- if (c.indexOf(name) === 0) {
29
- return c.substr(name.length, c.length);
33
+ if (c.indexOf(nameeq) === 0) {
34
+ const raw = c.substr(nameeq.length, c.length);
35
+ return raw;
30
36
  }
31
37
  }
32
38
  return undefined;
33
39
  }
34
- function setCookieWrapper(cname, raw, exdays = 1) {
35
- let index = 0;
36
- while (getCookie({ cname: cname + index })) {
37
- setCookie(cname + index, null, -1);
38
- index += 1;
39
- }
40
- if (!raw) {
40
+ function wipeCookies(name) {
41
+ if (typeof window === 'undefined') {
42
+ (0, log_1.warn)('cant wipe cookies on server');
41
43
  return;
42
44
  }
43
- const str = (0, string_1.toBase64)(JSON.stringify(raw));
44
- let chunks = [str];
45
- if (str.length > maxCookieLen) {
46
- chunks = chunkString(str, maxCookieLen);
47
- }
48
- for (const index1 in chunks) {
49
- const chunk = chunks[index1];
50
- setCookie(cname + index1, chunk, exdays);
51
- }
52
- }
53
- exports.setCookieWrapper = setCookieWrapper;
54
- function wipeCookies(cname) {
55
45
  let currentCount = 0;
56
46
  // eslint-disable-next-line no-constant-condition
57
47
  while (true) {
58
- if (getCookie({ cname: cname + currentCount })) {
59
- setCookie(cname + currentCount, undefined);
48
+ if (getCookieRaw({
49
+ name: name + currentCount,
50
+ cookieDocument: '',
51
+ })) {
52
+ setCookieRaw({ name: name + currentCount, value: '', expiryDays: -1 });
60
53
  currentCount += 1;
61
54
  }
62
55
  else {
@@ -65,46 +58,98 @@ function wipeCookies(cname) {
65
58
  }
66
59
  }
67
60
  exports.wipeCookies = wipeCookies;
68
- function getCookieWrapper({ cname, cookieDocument, defaultValue, }) {
61
+ /**
62
+ * json+b64 incoming. chunk. write chunks
63
+ * @param p
64
+ * @returns
65
+ */
66
+ function setCookieWrapper(p) {
67
+ const stringify = (s) => {
68
+ if (p.stringify) {
69
+ return p.stringify(s);
70
+ }
71
+ return JSON.stringify(s);
72
+ };
73
+ wipeCookies(p.name);
74
+ if (!p.value) {
75
+ return;
76
+ }
77
+ const str = (0, string_1.toBase64)(stringify(p.value));
78
+ const chunks = (0, string_1.chunkString)(str, maxCookieLen);
79
+ for (const index1 in chunks) {
80
+ const chunk = chunks[index1];
81
+ setCookieRaw(Object.assign(Object.assign({}, p), { name: p.name + index1, value: chunk }));
82
+ }
83
+ }
84
+ exports.setCookieWrapper = setCookieWrapper;
85
+ /**
86
+ * read chunks. json parse+unb64
87
+ * @param param0
88
+ * @returns
89
+ */
90
+ function getCookieWrapper({ name, cookieDocument, defaultValue, parse: parseRaw, }) {
91
+ const parse = (s) => {
92
+ if (!s) {
93
+ return defaultValue;
94
+ }
95
+ if (parseRaw) {
96
+ return parseRaw(s);
97
+ }
98
+ return JSON.parse(s);
99
+ };
69
100
  let raw = '';
70
101
  let currentCount = 0;
71
102
  // eslint-disable-next-line no-constant-condition
72
103
  while (true) {
73
- const newv = getCookie({
74
- cname: cname + currentCount,
104
+ const newv = getCookieRaw({
105
+ name: name + currentCount,
75
106
  cookieDocument,
76
107
  });
77
108
  if (!newv) {
78
- if (currentCount === 0) {
79
- if (defaultValue) {
80
- setCookie(cname, defaultValue);
81
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
82
- return defaultValue;
83
- }
84
- return undefined;
85
- }
86
- try {
87
- return JSON.parse((0, string_1.fromBase64)(raw));
88
- }
89
- catch (e) {
90
- wipeCookies(cname);
91
- return undefined;
92
- }
109
+ break;
93
110
  }
94
111
  raw += newv;
95
112
  currentCount += 1;
96
113
  }
114
+ try {
115
+ return parse((0, string_1.fromBase64)(raw));
116
+ }
117
+ catch (e) {
118
+ (0, log_1.warn)('cookie error:', e);
119
+ wipeCookies(name);
120
+ return defaultValue;
121
+ }
97
122
  }
98
123
  exports.getCookieWrapper = getCookieWrapper;
99
124
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
100
- function useCookie({ key, defaultValue, cookieDocument, }) {
101
- const [cookie, setC] = (0, react_1.useState)(getCookieWrapper({ cname: key, defaultValue, cookieDocument }));
102
- return [
103
- cookie,
104
- (v) => {
105
- setCookieWrapper(key, v, 2);
106
- setC(v);
107
- },
108
- ];
125
+ function useCookie(p) {
126
+ const parse = (s) => {
127
+ if (!s) {
128
+ return p.defaultValue;
129
+ }
130
+ if (p.parse) {
131
+ return p.parse(s);
132
+ }
133
+ return JSON.parse(s);
134
+ };
135
+ const stringify = (s) => {
136
+ if (p.stringify) {
137
+ return p.stringify(s);
138
+ }
139
+ return JSON.stringify(s);
140
+ };
141
+ const [cookie, setCookie] = (0, react_1.useState)(getCookieWrapper(Object.assign(Object.assign({}, p), { parse })) || p.defaultValue);
142
+ const setState = (valueRaw) => {
143
+ const value = (valueRaw instanceof Function ? valueRaw(cookie) : valueRaw);
144
+ setCookieWrapper(Object.assign(Object.assign({}, p), { stringify, value }));
145
+ setCookie(value);
146
+ };
147
+ return [cookie, setState];
109
148
  }
110
149
  exports.useCookie = useCookie;
150
+ const useCookieString = (p) => useCookie(Object.assign(Object.assign({}, p), { parse: (s) => s || '', stringify: (s) => s }));
151
+ exports.useCookieString = useCookieString;
152
+ const useCookieNumber = (p) => useCookie(Object.assign(Object.assign({}, p), { parse: (s) => (!s ? undefined : Number.parseFloat(s)), stringify: (s) => (!s ? '' : s.toString()) }));
153
+ exports.useCookieNumber = useCookieNumber;
154
+ const useCookieBoolean = (p) => useCookie(Object.assign(Object.assign({}, p), { parse: (s) => s === 'true', stringify: (s) => s.toString() }));
155
+ exports.useCookieBoolean = useCookieBoolean;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  import { TLang, TResource } from '../../common/helpers/i18n';
2
3
  export declare const useTranslation: <T extends {
3
4
  [a: string]: TResource;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.127",
3
+ "version": "0.0.132",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",
@@ -1,11 +0,0 @@
1
- import React from 'react';
2
- export declare const TextInput: ({ placeholder, value, onChange, onKeyPress, label, disabled, className, focus, }: {
3
- focus?: boolean | undefined;
4
- className?: string | undefined;
5
- disabled?: boolean | undefined;
6
- label?: string | JSX.Element | undefined;
7
- placeholder?: string | undefined;
8
- value: string;
9
- onChange: (value: string) => void;
10
- onKeyPress?: ((event: React.KeyboardEvent<HTMLInputElement>) => void) | undefined;
11
- }) => JSX.Element;
@@ -1,53 +0,0 @@
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.TextInput = void 0;
26
- const react_1 = __importStar(require("react"));
27
- const styled_components_1 = __importDefault(require("styled-components"));
28
- const math_1 = require("../../../common/helpers/math");
29
- const colours_1 = require("../../styles/colours");
30
- const Base = styled_components_1.default.input `
31
- font-size: 1.2rem;
32
- border: solid 1px ${colours_1.colours.lightCharcoal};
33
- border-radius: 0.5rem;
34
- padding: 0.5rem;
35
- width: calc(100% - 1.5rem);
36
- &:disabled {
37
- background-color: ${colours_1.colours.darker};
38
- cursor: not-allowed;
39
- }
40
- `;
41
- const TextInput = ({ placeholder, value, onChange, onKeyPress, label, disabled = false, className, focus, }) => {
42
- const [id] = (0, react_1.useState)((0, math_1.getRandomInt)(1000).toString());
43
- const tiref = (0, react_1.useRef)(null);
44
- (0, react_1.useEffect)(() => {
45
- if (focus && (tiref === null || tiref === void 0 ? void 0 : tiref.current)) {
46
- tiref.current.focus();
47
- }
48
- }, [focus]);
49
- return (react_1.default.createElement(react_1.default.Fragment, null,
50
- id && label ? react_1.default.createElement("label", { htmlFor: id }, label) : '',
51
- react_1.default.createElement(Base, { ref: tiref, className: className, disabled: disabled, id: id, placeholder: placeholder, required: true, value: value, onChange: (e) => onChange(e.target.value), onKeyPress: (e) => onKeyPress && onKeyPress(e) })));
52
- };
53
- exports.TextInput = TextInput;