@peassoft/mnr-web-topline 3.0.0 → 4.0.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.
Files changed (93) hide show
  1. package/README.md +105 -18
  2. package/dist/css/index.css +2 -2
  3. package/dist/en/modules/keyboard-navigation/key-codes.d.ts +16 -18
  4. package/dist/en/modules/keyboard-navigation/key-codes.js +17 -18
  5. package/dist/en/modules/keyboard-navigation/vertical-menu.d.ts +1 -1
  6. package/dist/en/modules/keyboard-navigation/vertical-menu.js +8 -8
  7. package/dist/en/modules/local-db/actions/delete-all-data/index.js +2 -2
  8. package/dist/en/modules/local-db/actions/get-all-data/index.js +2 -2
  9. package/dist/en/modules/local-db/actions/get-is-user-known/index.d.ts +5 -0
  10. package/dist/en/modules/local-db/actions/get-is-user-known/index.js +33 -0
  11. package/dist/en/modules/local-db/actions/update-grant-token/index.js +3 -3
  12. package/dist/en/modules/local-db/actions/update-is-user-known/index.d.ts +5 -0
  13. package/dist/en/modules/local-db/actions/update-is-user-known/index.js +27 -0
  14. package/dist/en/modules/local-db/actions/update-refresh-token/index.js +3 -3
  15. package/dist/en/modules/local-db/actions/update-user/index.js +3 -3
  16. package/dist/en/modules/local-db/index.d.ts +2 -0
  17. package/dist/en/modules/local-db/index.js +3 -1
  18. package/dist/en/modules/local-db/init-db.d.ts +2 -1
  19. package/dist/en/modules/local-db/init-db.js +19 -10
  20. package/dist/en/modules/topline-service/index.d.ts +3 -1
  21. package/dist/en/modules/topline-service/index.js +6 -0
  22. package/dist/en/modules/topline-service/inner-service.d.ts +6 -1
  23. package/dist/en/modules/topline-service/inner-service.js +18 -0
  24. package/dist/en/modules/topline-service/types.d.ts +12 -0
  25. package/dist/en/modules/topline-service/types.js +4 -0
  26. package/dist/en/parts/login/actions/perform-login/index.d.ts +1 -15
  27. package/dist/en/parts/login/actions/perform-login/index.js +8 -17
  28. package/dist/en/parts/password-recovery/actions/save-password/index.d.ts +1 -15
  29. package/dist/en/parts/password-recovery/actions/save-password/index.js +8 -17
  30. package/dist/en/parts/profile/ui/password-change-confirmation/index.js +1 -0
  31. package/dist/en/parts/profile/ui/password-change-form/use-state-with-validation-reset.js +1 -1
  32. package/dist/en/parts/shell/context.d.ts +1 -1
  33. package/dist/en/parts/shell/ui/logged-out-user-menu/index.js +2 -0
  34. package/dist/en/parts/shell/ui/loggeg-in-user-menu/index.js +2 -0
  35. package/dist/en/parts/shell/ui/shell/index.js +11 -5
  36. package/dist/en/parts/shell/ui/user-menu-item/index.d.ts +1 -0
  37. package/dist/en/parts/shell/ui/user-menu-item/index.js +2 -0
  38. package/dist/en/parts/signup/actions/perform-signup/index.d.ts +1 -15
  39. package/dist/en/parts/signup/actions/perform-signup/index.js +8 -17
  40. package/dist/en/shared/components/modal/index.js +2 -2
  41. package/dist/en/shared/procedures/process-response.d.ts +13 -0
  42. package/dist/en/shared/procedures/process-response.js +53 -0
  43. package/dist/en/topline.d.ts +1 -1
  44. package/dist/en/types/result.d.ts +9 -0
  45. package/dist/en/types/result.js +12 -0
  46. package/dist/ru/modules/keyboard-navigation/key-codes.d.ts +16 -18
  47. package/dist/ru/modules/keyboard-navigation/key-codes.js +17 -18
  48. package/dist/ru/modules/keyboard-navigation/vertical-menu.d.ts +1 -1
  49. package/dist/ru/modules/keyboard-navigation/vertical-menu.js +8 -8
  50. package/dist/ru/modules/local-db/actions/delete-all-data/index.js +2 -2
  51. package/dist/ru/modules/local-db/actions/get-all-data/index.js +2 -2
  52. package/dist/ru/modules/local-db/actions/get-is-user-known/index.d.ts +5 -0
  53. package/dist/ru/modules/local-db/actions/get-is-user-known/index.js +33 -0
  54. package/dist/ru/modules/local-db/actions/update-grant-token/index.js +3 -3
  55. package/dist/ru/modules/local-db/actions/update-is-user-known/index.d.ts +5 -0
  56. package/dist/ru/modules/local-db/actions/update-is-user-known/index.js +27 -0
  57. package/dist/ru/modules/local-db/actions/update-refresh-token/index.js +3 -3
  58. package/dist/ru/modules/local-db/actions/update-user/index.js +3 -3
  59. package/dist/ru/modules/local-db/index.d.ts +2 -0
  60. package/dist/ru/modules/local-db/index.js +3 -1
  61. package/dist/ru/modules/local-db/init-db.d.ts +2 -1
  62. package/dist/ru/modules/local-db/init-db.js +19 -10
  63. package/dist/ru/modules/topline-service/index.d.ts +3 -1
  64. package/dist/ru/modules/topline-service/index.js +6 -0
  65. package/dist/ru/modules/topline-service/inner-service.d.ts +6 -1
  66. package/dist/ru/modules/topline-service/inner-service.js +18 -0
  67. package/dist/ru/modules/topline-service/types.d.ts +12 -0
  68. package/dist/ru/modules/topline-service/types.js +4 -0
  69. package/dist/ru/parts/login/actions/perform-login/index.d.ts +1 -15
  70. package/dist/ru/parts/login/actions/perform-login/index.js +8 -17
  71. package/dist/ru/parts/password-recovery/actions/save-password/index.d.ts +1 -15
  72. package/dist/ru/parts/password-recovery/actions/save-password/index.js +8 -17
  73. package/dist/ru/parts/profile/ui/password-change-confirmation/index.js +1 -0
  74. package/dist/ru/parts/profile/ui/password-change-form/use-state-with-validation-reset.js +1 -1
  75. package/dist/ru/parts/shell/context.d.ts +1 -1
  76. package/dist/ru/parts/shell/ui/logged-out-user-menu/index.js +2 -0
  77. package/dist/ru/parts/shell/ui/loggeg-in-user-menu/index.js +2 -0
  78. package/dist/ru/parts/shell/ui/shell/index.js +11 -5
  79. package/dist/ru/parts/shell/ui/user-menu-item/index.d.ts +1 -0
  80. package/dist/ru/parts/shell/ui/user-menu-item/index.js +2 -0
  81. package/dist/ru/parts/signup/actions/perform-signup/index.d.ts +1 -15
  82. package/dist/ru/parts/signup/actions/perform-signup/index.js +8 -17
  83. package/dist/ru/shared/components/modal/index.js +2 -2
  84. package/dist/ru/shared/procedures/process-response.d.ts +13 -0
  85. package/dist/ru/shared/procedures/process-response.js +53 -0
  86. package/dist/ru/topline.d.ts +1 -1
  87. package/dist/ru/types/result.d.ts +9 -0
  88. package/dist/ru/types/result.js +12 -0
  89. package/package.json +10 -4
  90. package/dist/en/shared/procedures/process-successful-response/index.d.ts +0 -30
  91. package/dist/en/shared/procedures/process-successful-response/index.js +0 -66
  92. package/dist/ru/shared/procedures/process-successful-response/index.d.ts +0 -30
  93. package/dist/ru/shared/procedures/process-successful-response/index.js +0 -66
@@ -4,6 +4,7 @@ export default function UserMenuItem(props) {
4
4
  label,
5
5
  itemRef,
6
6
  tabIndex,
7
+ testId,
7
8
  onKeyDown,
8
9
  onClick
9
10
  } = props;
@@ -12,6 +13,7 @@ export default function UserMenuItem(props) {
12
13
  className: 'topline_shell_userMenuItem',
13
14
  role: 'menuitem',
14
15
  tabIndex: tabIndex,
16
+ "data-testid": testId || undefined,
15
17
  onClick: onClick,
16
18
  onKeyDown: onKeyDown,
17
19
  children: label
@@ -1,7 +1,3 @@
1
- import stampit from 'stampit';
2
- import { getApiBaseUrl, getRequestCredentialsMode } from '../../../../modules/env/index.js';
3
- import request from '../../../../modules/request/index.js';
4
- import processSuccessfulResponse from '../../../../shared/procedures/process-successful-response/index.js';
5
1
  import type { ToplineUser } from '../../../../types/data.js';
6
2
  export type UserData = {
7
3
  email: string;
@@ -20,14 +16,4 @@ export type ReturnValue = {
20
16
  * - on network error or if backend responds with 5xx - "network";
21
17
  * - on 409 Conflict response - "user-exists".
22
18
  */
23
- declare function performSignup(this: This, userData: UserData): Promise<ReturnValue>;
24
- export type This = {
25
- getApiBaseUrl: typeof getApiBaseUrl;
26
- getRequestCredentialsMode: typeof getRequestCredentialsMode;
27
- request: typeof request;
28
- processSuccessfulResponse: typeof processSuccessfulResponse;
29
- performSignup: typeof performSignup;
30
- };
31
- export declare const PerformSignup: stampit.Stamp<This>;
32
- declare const _default: (userData: UserData) => Promise<ReturnValue>;
33
- export default _default;
19
+ export default function performSignup(userData: UserData): Promise<ReturnValue>;
@@ -1,7 +1,7 @@
1
- import stampit from 'stampit';
2
1
  import { getApiBaseUrl, getRequestCredentialsMode } from '../../../../modules/env/index.js';
3
2
  import request from '../../../../modules/request/index.js';
4
- import processSuccessfulResponse from '../../../../shared/procedures/process-successful-response/index.js';
3
+ import processResponse from '../../../../shared/procedures/process-response.js';
4
+ import { ToplineEventName } from '../../../../modules/topline-service/index.js';
5
5
  /**
6
6
  * Perform signing-up
7
7
  *
@@ -9,19 +9,19 @@ import processSuccessfulResponse from '../../../../shared/procedures/process-suc
9
9
  * - on network error or if backend responds with 5xx - "network";
10
10
  * - on 409 Conflict response - "user-exists".
11
11
  */
12
- async function performSignup(userData) {
13
- const url = `${this.getApiBaseUrl()}/public/signup`;
12
+ export default async function performSignup(userData) {
13
+ const url = `${getApiBaseUrl()}/public/signup`;
14
14
  const options = {
15
15
  method: 'POST',
16
16
  headers: {
17
17
  'Content-Type': 'application/json; charset=utf-8'
18
18
  },
19
19
  body: JSON.stringify(userData),
20
- credentials: this.getRequestCredentialsMode(),
20
+ credentials: getRequestCredentialsMode(),
21
21
  cache: 'no-store',
22
22
  redirect: 'error'
23
23
  };
24
- const response = await this.request(url, options, {
24
+ const response = await request(url, options, {
25
25
  expectedStatusCodes: [200, 409],
26
26
  maxRetries: 1
27
27
  });
@@ -32,7 +32,7 @@ async function performSignup(userData) {
32
32
  };
33
33
  }
34
34
  if (response.status === 200) {
35
- return await this.processSuccessfulResponse(response, url);
35
+ return await processResponse(response, url, ToplineEventName.Signup);
36
36
  }
37
37
  if (response.status === 409) {
38
38
  return {
@@ -46,13 +46,4 @@ async function performSignup(userData) {
46
46
  user: null,
47
47
  error: 'network'
48
48
  };
49
- }
50
- export const PerformSignup = stampit().props({
51
- getApiBaseUrl,
52
- getRequestCredentialsMode,
53
- request,
54
- processSuccessfulResponse
55
- }).methods({
56
- performSignup
57
- });
58
- export default performSignup.bind(PerformSignup());
49
+ }
@@ -1,6 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { useEffect } from 'react';
3
- import keyCodes from '../../../modules/keyboard-navigation/key-codes.js';
3
+ import { Key } from '../../../modules/keyboard-navigation/key-codes.js';
4
4
  export default function Modal(props) {
5
5
  const {
6
6
  title,
@@ -8,7 +8,7 @@ export default function Modal(props) {
8
8
  } = props;
9
9
  useEffect(() => {
10
10
  const handleKeyDown = e => {
11
- if (e.which === keyCodes.ESC) {
11
+ if (e.key === Key.Escape) {
12
12
  onClose();
13
13
  }
14
14
  };
@@ -0,0 +1,13 @@
1
+ import { ToplineEventName } from '../../modules/topline-service/index.js';
2
+ import type { CustomResponse } from '../../modules/request/index.js';
3
+ import { type ToplineUser } from '../../types/data.js';
4
+ type EventName = ToplineEventName.Login | ToplineEventName.Signup;
5
+ type ReturnValue = {
6
+ user: ToplineUser | null;
7
+ error: 'network' | null;
8
+ };
9
+ /**
10
+ * Process successful response on a request that implies user has to be logged-in
11
+ */
12
+ export default function processResponse(response: CustomResponse, url: string, eventName: EventName): Promise<ReturnValue>;
13
+ export {};
@@ -0,0 +1,53 @@
1
+ import WebError from '@memnrev/web-error';
2
+ import { updateUser, updateGrantToken, updateRefreshToken, updateIsUserKnown } from '../../modules/local-db/index.js';
3
+ import { setGrantToken, setRefreshToken } from '../../modules/tokens/index.js';
4
+ import { openWebsocket } from '../../modules/websocket/index.js';
5
+ import { innerToplineService, ToplineEventName } from '../../modules/topline-service/index.js';
6
+ import { logError } from '../../modules/logger/index.js';
7
+ import { isToplineUser } from '../../types/data.js';
8
+ import { isObject } from '../../types/helpers.js';
9
+ /**
10
+ * Process successful response on a request that implies user has to be logged-in
11
+ */
12
+ export default async function processResponse(response, url, eventName) {
13
+ try {
14
+ const responseBody = await response.json();
15
+ if (isObject(responseBody) && 'user' in responseBody && isToplineUser(responseBody.user) && 'grantToken' in responseBody && typeof responseBody.grantToken === 'string' && 'refreshToken' in responseBody && typeof responseBody.refreshToken === 'string') {
16
+ const {
17
+ user,
18
+ grantToken,
19
+ refreshToken
20
+ } = responseBody;
21
+ void updateUser(user);
22
+ void updateGrantToken(grantToken);
23
+ void updateRefreshToken(refreshToken);
24
+ void updateIsUserKnown(true);
25
+ setGrantToken(grantToken);
26
+ setRefreshToken(refreshToken);
27
+ openWebsocket();
28
+ innerToplineService.emit(eventName, user);
29
+ return {
30
+ user,
31
+ error: null
32
+ };
33
+ }
34
+ logError(new WebError({
35
+ name: 'UnexpectedError'
36
+ }, `unexpected payload of response to request to ${url}`));
37
+ return {
38
+ user: null,
39
+ error: 'network'
40
+ };
41
+ } catch (e) {
42
+ logError(new WebError({
43
+ name: 'UnexpectedError',
44
+ ...(e instanceof Error ? {
45
+ cause: e
46
+ } : {})
47
+ }, `parsing 200 response of request to ${url} failed`));
48
+ return {
49
+ user: null,
50
+ error: 'network'
51
+ };
52
+ }
53
+ }
@@ -13,7 +13,7 @@ export type ToplineProps = {
13
13
  */
14
14
  forceBlankHomeLink?: boolean;
15
15
  /** Callback that is invoked after user is restored from a local DB */
16
- onReady?: (toplineService: ToplineService, user: ToplineUser | null) => unknown;
16
+ onReady?: (toplineService: ToplineService, user: ToplineUser | null, isUserKnown: boolean) => unknown;
17
17
  /**
18
18
  * Callback that is invoked if an unexpected error is caught by React's error boundary
19
19
  * component
@@ -0,0 +1,9 @@
1
+ export type Result<T, E> = {
2
+ ok: true;
3
+ value: T;
4
+ } | {
5
+ ok: false;
6
+ error: E;
7
+ };
8
+ export declare const Ok: <T>(data: T) => Result<T, never>;
9
+ export declare const Err: <E>(error: E) => Result<never, E>;
@@ -0,0 +1,12 @@
1
+ export const Ok = data => {
2
+ return {
3
+ ok: true,
4
+ value: data
5
+ };
6
+ };
7
+ export const Err = error => {
8
+ return {
9
+ ok: false,
10
+ error
11
+ };
12
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peassoft/mnr-web-topline",
3
- "version": "3.0.0",
3
+ "version": "4.0.0",
4
4
  "description": "Peassoft Topline widget for mem'n'rev web applications",
5
5
  "type": "module",
6
6
  "exports": {
@@ -30,18 +30,24 @@
30
30
  "@memnrev/dict-replacer": "^0.1.0",
31
31
  "@memnrev/eslint-v9-config": "^0.4.0",
32
32
  "@microsoft/api-extractor": "^7.38.0",
33
+ "@peassoft/dict-builder": "^0.1.0",
33
34
  "@peassoft/mnr-web-ui-kit": "^2.1.0",
35
+ "@testing-library/jest-dom": "^6.9.1",
36
+ "@testing-library/react": "^16.3.2",
37
+ "@testing-library/user-event": "^14.6.1",
34
38
  "@types/md5": "^2.3.3",
35
39
  "@types/react": "^19.0.2",
36
40
  "@types/react-dom": "^19.0.2",
37
41
  "@types/stampit": "^4.3.4",
42
+ "@vitejs/plugin-react": "^5.1.4",
38
43
  "autoprefixer": "^10.3.4",
39
44
  "clean-webpack-plugin": "^4.0.0",
40
45
  "copy-webpack-plugin": "^13.0.0",
41
- "cpy-cli": "^6.0.0",
46
+ "cpy-cli": "^7.0.0",
42
47
  "css-loader": "^7.1.2",
43
48
  "eslint": "^9.8.0",
44
49
  "html-webpack-plugin": "^5.3.2",
50
+ "jsdom": "^28.0.0",
45
51
  "postcss": "^8.3.6",
46
52
  "postcss-loader": "^8.0.0",
47
53
  "postcss-normalize": "^13.0.1",
@@ -50,8 +56,8 @@
50
56
  "sass": "^1.52.1",
51
57
  "sass-loader": "^16.0.4",
52
58
  "style-loader": "^4.0.0",
53
- "stylelint": "^16.2.0",
54
- "stylelint-config-sass-guidelines": "^12.1.0",
59
+ "stylelint": "^17.0.0",
60
+ "stylelint-config-sass-guidelines": "^13.0.0",
55
61
  "ts-loader": "^9.4.4",
56
62
  "typescript": "^5.2.2",
57
63
  "vitest": "^4.0.7",
@@ -1,30 +0,0 @@
1
- import stampit from 'stampit';
2
- import { updateUser, updateGrantToken, updateRefreshToken } from '../../../modules/local-db/index.js';
3
- import { setGrantToken, setRefreshToken } from '../../../modules/tokens/index.js';
4
- import { openWebsocket } from '../../../modules/websocket/index.js';
5
- import { type InnerService } from '../../../modules/topline-service/index.js';
6
- import { logError } from '../../../modules/logger/index.js';
7
- import type { CustomResponse } from '../../../modules/request/index.js';
8
- import { type ToplineUser } from '../../../types/data.js';
9
- type ReturnValue = {
10
- user: ToplineUser | null;
11
- error: 'network' | null;
12
- };
13
- /**
14
- * Process successful response on a request that implies user has to be logged-in
15
- */
16
- declare function processSuccessfulResponse(this: This, response: CustomResponse, url: string): Promise<ReturnValue>;
17
- export type This = {
18
- updateUser: typeof updateUser;
19
- updateGrantToken: typeof updateGrantToken;
20
- updateRefreshToken: typeof updateRefreshToken;
21
- setGrantToken: typeof setGrantToken;
22
- setRefreshToken: typeof setRefreshToken;
23
- openWebsocket: typeof openWebsocket;
24
- innerToplineService: Pick<InnerService, 'emit'>;
25
- logError: typeof logError;
26
- processSuccessfulResponse: typeof processSuccessfulResponse;
27
- };
28
- export declare const ProcessSuccessfulResponse: stampit.Stamp<This>;
29
- declare const _default: (response: CustomResponse, url: string) => Promise<ReturnValue>;
30
- export default _default;
@@ -1,66 +0,0 @@
1
- import stampit from 'stampit';
2
- import WebError from '@memnrev/web-error';
3
- import { updateUser, updateGrantToken, updateRefreshToken } from '../../../modules/local-db/index.js';
4
- import { setGrantToken, setRefreshToken } from '../../../modules/tokens/index.js';
5
- import { openWebsocket } from '../../../modules/websocket/index.js';
6
- import { innerToplineService, ToplineEventName } from '../../../modules/topline-service/index.js';
7
- import { logError } from '../../../modules/logger/index.js';
8
- import { isToplineUser } from '../../../types/data.js';
9
- import { isObject } from '../../../types/helpers.js';
10
- /**
11
- * Process successful response on a request that implies user has to be logged-in
12
- */
13
- async function processSuccessfulResponse(response, url) {
14
- try {
15
- const responseBody = await response.json();
16
- if (isObject(responseBody) && 'user' in responseBody && isToplineUser(responseBody.user) && 'grantToken' in responseBody && typeof responseBody.grantToken === 'string' && 'refreshToken' in responseBody && typeof responseBody.refreshToken === 'string') {
17
- const {
18
- user,
19
- grantToken,
20
- refreshToken
21
- } = responseBody;
22
- void this.updateUser(user);
23
- void this.updateGrantToken(grantToken);
24
- void this.updateRefreshToken(refreshToken);
25
- this.setGrantToken(grantToken);
26
- this.setRefreshToken(refreshToken);
27
- this.openWebsocket();
28
- this.innerToplineService.emit(ToplineEventName.UserChange, user);
29
- return {
30
- user,
31
- error: null
32
- };
33
- }
34
- this.logError(new WebError({
35
- name: 'UnexpectedError'
36
- }, `unexpected payload of response to request to ${url}`));
37
- return {
38
- user: null,
39
- error: 'network'
40
- };
41
- } catch (e) {
42
- this.logError(new WebError({
43
- name: 'UnexpectedError',
44
- ...(e instanceof Error ? {
45
- cause: e
46
- } : {})
47
- }, `parsing 200 response of request to ${url} failed`));
48
- return {
49
- user: null,
50
- error: 'network'
51
- };
52
- }
53
- }
54
- export const ProcessSuccessfulResponse = stampit().props({
55
- updateUser,
56
- updateGrantToken,
57
- updateRefreshToken,
58
- setGrantToken,
59
- setRefreshToken,
60
- openWebsocket,
61
- innerToplineService,
62
- logError
63
- }).methods({
64
- processSuccessfulResponse
65
- });
66
- export default processSuccessfulResponse.bind(ProcessSuccessfulResponse());
@@ -1,30 +0,0 @@
1
- import stampit from 'stampit';
2
- import { updateUser, updateGrantToken, updateRefreshToken } from '../../../modules/local-db/index.js';
3
- import { setGrantToken, setRefreshToken } from '../../../modules/tokens/index.js';
4
- import { openWebsocket } from '../../../modules/websocket/index.js';
5
- import { type InnerService } from '../../../modules/topline-service/index.js';
6
- import { logError } from '../../../modules/logger/index.js';
7
- import type { CustomResponse } from '../../../modules/request/index.js';
8
- import { type ToplineUser } from '../../../types/data.js';
9
- type ReturnValue = {
10
- user: ToplineUser | null;
11
- error: 'network' | null;
12
- };
13
- /**
14
- * Process successful response on a request that implies user has to be logged-in
15
- */
16
- declare function processSuccessfulResponse(this: This, response: CustomResponse, url: string): Promise<ReturnValue>;
17
- export type This = {
18
- updateUser: typeof updateUser;
19
- updateGrantToken: typeof updateGrantToken;
20
- updateRefreshToken: typeof updateRefreshToken;
21
- setGrantToken: typeof setGrantToken;
22
- setRefreshToken: typeof setRefreshToken;
23
- openWebsocket: typeof openWebsocket;
24
- innerToplineService: Pick<InnerService, 'emit'>;
25
- logError: typeof logError;
26
- processSuccessfulResponse: typeof processSuccessfulResponse;
27
- };
28
- export declare const ProcessSuccessfulResponse: stampit.Stamp<This>;
29
- declare const _default: (response: CustomResponse, url: string) => Promise<ReturnValue>;
30
- export default _default;
@@ -1,66 +0,0 @@
1
- import stampit from 'stampit';
2
- import WebError from '@memnrev/web-error';
3
- import { updateUser, updateGrantToken, updateRefreshToken } from '../../../modules/local-db/index.js';
4
- import { setGrantToken, setRefreshToken } from '../../../modules/tokens/index.js';
5
- import { openWebsocket } from '../../../modules/websocket/index.js';
6
- import { innerToplineService, ToplineEventName } from '../../../modules/topline-service/index.js';
7
- import { logError } from '../../../modules/logger/index.js';
8
- import { isToplineUser } from '../../../types/data.js';
9
- import { isObject } from '../../../types/helpers.js';
10
- /**
11
- * Process successful response on a request that implies user has to be logged-in
12
- */
13
- async function processSuccessfulResponse(response, url) {
14
- try {
15
- const responseBody = await response.json();
16
- if (isObject(responseBody) && 'user' in responseBody && isToplineUser(responseBody.user) && 'grantToken' in responseBody && typeof responseBody.grantToken === 'string' && 'refreshToken' in responseBody && typeof responseBody.refreshToken === 'string') {
17
- const {
18
- user,
19
- grantToken,
20
- refreshToken
21
- } = responseBody;
22
- void this.updateUser(user);
23
- void this.updateGrantToken(grantToken);
24
- void this.updateRefreshToken(refreshToken);
25
- this.setGrantToken(grantToken);
26
- this.setRefreshToken(refreshToken);
27
- this.openWebsocket();
28
- this.innerToplineService.emit(ToplineEventName.UserChange, user);
29
- return {
30
- user,
31
- error: null
32
- };
33
- }
34
- this.logError(new WebError({
35
- name: 'UnexpectedError'
36
- }, `unexpected payload of response to request to ${url}`));
37
- return {
38
- user: null,
39
- error: 'network'
40
- };
41
- } catch (e) {
42
- this.logError(new WebError({
43
- name: 'UnexpectedError',
44
- ...(e instanceof Error ? {
45
- cause: e
46
- } : {})
47
- }, `parsing 200 response of request to ${url} failed`));
48
- return {
49
- user: null,
50
- error: 'network'
51
- };
52
- }
53
- }
54
- export const ProcessSuccessfulResponse = stampit().props({
55
- updateUser,
56
- updateGrantToken,
57
- updateRefreshToken,
58
- setGrantToken,
59
- setRefreshToken,
60
- openWebsocket,
61
- innerToplineService,
62
- logError
63
- }).methods({
64
- processSuccessfulResponse
65
- });
66
- export default processSuccessfulResponse.bind(ProcessSuccessfulResponse());