@genesislcap/foundation-state-machine 14.70.3 → 14.70.4-es2021.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.
@@ -25,9 +25,8 @@ export const setError = assign(({ event }) => {
25
25
  * @public
26
26
  */
27
27
  export const setErrorByKey = (key) => assign(({ context, event }) => {
28
- var _a;
29
28
  if (isErrorEvent(event)) {
30
- const errors = (_a = context.errors) !== null && _a !== void 0 ? _a : createErrorMap(logger.error);
29
+ const errors = context.errors ?? createErrorMap(logger.error);
31
30
  errors.set(key, event.data);
32
31
  return {
33
32
  errors,
@@ -18,28 +18,24 @@ export class GenesisServerError extends Error {
18
18
  * This is ugly, lets figure out if this is still an issue and remove if not.
19
19
  */
20
20
  isIgnorableError() {
21
- var _a, _b, _c, _d;
22
- if ((_d = (_c = (_b = (_a = this.received) === null || _a === void 0 ? void 0 : _a.DETAILS) === null || _b === void 0 ? void 0 : _b.ERROR) === null || _c === void 0 ? void 0 : _c.toLowerCase()) === null || _d === void 0 ? void 0 : _d.includes('request needs the user to be logged')) {
21
+ if (this.received?.DETAILS?.ERROR?.toLowerCase()?.includes('request needs the user to be logged')) {
23
22
  return true;
24
23
  }
25
24
  return false;
26
25
  }
27
26
  getMessageError() {
28
- var _a, _b;
29
- return Array.isArray((_a = this.received) === null || _a === void 0 ? void 0 : _a.ERROR)
30
- ? (_b = this.received) === null || _b === void 0 ? void 0 : _b.ERROR.map((error) => this.errorDetailFormatter(error)).join('\n')
27
+ return Array.isArray(this.received?.ERROR)
28
+ ? this.received?.ERROR.map((error) => this.errorDetailFormatter(error)).join('\n')
31
29
  : '';
32
30
  }
33
31
  getDetailsError() {
34
- var _a, _b, _c, _d;
35
- return Array.isArray((_b = (_a = this.received) === null || _a === void 0 ? void 0 : _a.DETAILS) === null || _b === void 0 ? void 0 : _b.ERROR)
36
- ? (_d = (_c = this.received) === null || _c === void 0 ? void 0 : _c.DETAILS) === null || _d === void 0 ? void 0 : _d.ERROR.map((error) => this.errorDetailFormatter(error)).join('\n')
32
+ return Array.isArray(this.received?.DETAILS?.ERROR)
33
+ ? this.received?.DETAILS?.ERROR.map((error) => this.errorDetailFormatter(error)).join('\n')
37
34
  : '';
38
35
  }
39
36
  hasErrorCode(code) {
40
- var _a, _b;
41
- return Array.isArray((_a = this.received) === null || _a === void 0 ? void 0 : _a.ERROR)
42
- ? (_b = this.received) === null || _b === void 0 ? void 0 : _b.ERROR.some((error) => error.CODE === code)
37
+ return Array.isArray(this.received?.ERROR)
38
+ ? this.received?.ERROR.some((error) => error.CODE === code)
43
39
  : false;
44
40
  }
45
41
  errorDetailFormatter(error) {
@@ -38,7 +38,7 @@ export const isErrorCustomEvent = (event) => /^xstate.error/.test(event.type);
38
38
  * @param event - An event.
39
39
  * @public
40
40
  */
41
- export const isActorErrorEvent = (actorId, event) => { var _a, _b; return ((_a = event.type) === null || _a === void 0 ? void 0 : _a.includes('error')) && ((_b = event.type) === null || _b === void 0 ? void 0 : _b.includes(actorId)); };
41
+ export const isActorErrorEvent = (actorId, event) => event.type?.includes('error') && event.type?.includes(actorId);
42
42
  /**
43
43
  * isErrorType.
44
44
  * @param type - An event type.
@@ -38,10 +38,18 @@ export class AbstractMachine {
38
38
  errors: createErrorMap(logger.error),
39
39
  error: undefined,
40
40
  };
41
- this.startingState = options === null || options === void 0 ? void 0 : options.state;
41
+ this.startingState = options?.state;
42
42
  this.startingContext = (options.input
43
- ? Object.assign(Object.assign({}, contextWithErrors), options.input) : contextWithErrors);
44
- this.actor = interpret(this.machine, Object.assign(Object.assign({}, options), { input: this.startingContext, state: this.startingState }));
43
+ ? {
44
+ ...contextWithErrors,
45
+ ...options.input,
46
+ }
47
+ : contextWithErrors);
48
+ this.actor = interpret(this.machine, {
49
+ ...options,
50
+ input: this.startingContext,
51
+ state: this.startingState,
52
+ });
45
53
  const { value: currentValue, context: currentContext } = this.actor.getSnapshot();
46
54
  this.state = currentValue;
47
55
  this.context = currentContext;
@@ -59,11 +67,10 @@ export class AbstractMachine {
59
67
  }
60
68
  this.subscription = this.actor.subscribe({
61
69
  next: (snapshot) => {
62
- var _a;
63
70
  this.state = snapshot.value;
64
71
  this.context = snapshot.context;
65
72
  this.errors = this.context.errors;
66
- this.error = (_a = this.context.error) !== null && _a !== void 0 ? _a : this.errors.lastError;
73
+ this.error = this.context.error ?? this.errors.lastError;
67
74
  logger.debug(`${this.machine.id} changed, state:`, this.state, `context:`, this.context);
68
75
  },
69
76
  error: (err) => {
@@ -87,8 +94,7 @@ export class AbstractMachine {
87
94
  * @internal
88
95
  */
89
96
  startActor() {
90
- var _a;
91
- if (((_a = this.actor) === null || _a === void 0 ? void 0 : _a.status) !== InterpreterStatus.Running) {
97
+ if (this.actor?.status !== InterpreterStatus.Running) {
92
98
  this.actor.start();
93
99
  }
94
100
  }
@@ -96,8 +102,7 @@ export class AbstractMachine {
96
102
  * @internal
97
103
  */
98
104
  stopActor() {
99
- var _a;
100
- if (((_a = this.actor) === null || _a === void 0 ? void 0 : _a.status) === InterpreterStatus.Running) {
105
+ if (this.actor?.status === InterpreterStatus.Running) {
101
106
  this.actor.stop();
102
107
  }
103
108
  }
@@ -1,4 +1,4 @@
1
- import { __awaiter, __decorate, __param } from "tslib";
1
+ import { __decorate, __param } from "tslib";
2
2
  import { DI, optional } from '@microsoft/fast-foundation';
3
3
  import { assign, createMachine, fromPromise } from 'xstate';
4
4
  import { AbstractMachine, createErrorStateNode, isAbortError, isDoneInvokeEvent, isErrorEvent, isSyntaxError, isTypeError, setError, } from '../../core';
@@ -20,7 +20,9 @@ let DefaultFetchMachine = class DefaultFetchMachine extends AbstractMachine {
20
20
  this.config = config;
21
21
  this.machine = createMachine({
22
22
  id: 'fetchMachine',
23
- context: ({ input }) => (Object.assign({}, input)),
23
+ context: ({ input }) => ({
24
+ ...input,
25
+ }),
24
26
  initial: 'idle',
25
27
  states: {
26
28
  idle: {
@@ -36,7 +38,14 @@ let DefaultFetchMachine = class DefaultFetchMachine extends AbstractMachine {
36
38
  },
37
39
  invoke: {
38
40
  src: 'fetch',
39
- input: ({ context }) => (Object.assign(Object.assign({}, context.params), { controller: context.controller })),
41
+ input: ({ context }) => ({
42
+ ...context.params,
43
+ controller: context.controller,
44
+ /**
45
+ * We can test response codes using a service like https://httpstat.us, for example:
46
+ * url: `https://httpstat.us/200`,
47
+ */
48
+ }),
40
49
  onDone: 'success',
41
50
  onError: [
42
51
  {
@@ -106,18 +115,20 @@ let DefaultFetchMachine = class DefaultFetchMachine extends AbstractMachine {
106
115
  },
107
116
  }, {
108
117
  actors: {
109
- fetch: fromPromise(({ input }) => __awaiter(this, void 0, void 0, function* () {
110
- var _a, _b, _c, _d, _e;
118
+ fetch: fromPromise(async ({ input }) => {
111
119
  const { controller, fetchConfig, requestInit, url } = input;
112
- const fetcher = (_b = (_a = fetchConfig === null || fetchConfig === void 0 ? void 0 : fetchConfig.fetcher) !== null && _a !== void 0 ? _a : this.config.fetcher) !== null && _b !== void 0 ? _b : fetch;
113
- const fetchPolicy = (_c = fetchConfig === null || fetchConfig === void 0 ? void 0 : fetchConfig.fetchPolicy) !== null && _c !== void 0 ? _c : this.config.fetchPolicy;
114
- const init = requestInit !== null && requestInit !== void 0 ? requestInit : { method: 'GET' };
115
- const okChecker = (_d = fetchConfig === null || fetchConfig === void 0 ? void 0 : fetchConfig.responseOkChecker) !== null && _d !== void 0 ? _d : this.config.responseOkChecker;
116
- const dataExtractor = (_e = fetchConfig === null || fetchConfig === void 0 ? void 0 : fetchConfig.responseDataExtractor) !== null && _e !== void 0 ? _e : this.config.responseDataExtractor;
117
- return fetchPolicy.execute(() => __awaiter(this, void 0, void 0, function* () {
120
+ const fetcher = fetchConfig?.fetcher ?? this.config.fetcher ?? fetch;
121
+ const fetchPolicy = fetchConfig?.fetchPolicy ?? this.config.fetchPolicy;
122
+ const init = requestInit ?? { method: 'GET' };
123
+ const okChecker = fetchConfig?.responseOkChecker ?? this.config.responseOkChecker;
124
+ const dataExtractor = fetchConfig?.responseDataExtractor ?? this.config.responseDataExtractor;
125
+ return fetchPolicy.execute(async () => {
118
126
  try {
119
- const response = yield fetcher(url, Object.assign(Object.assign({}, init), { signal: controller.signal }));
120
- const data = yield dataExtractor(response);
127
+ const response = await fetcher(url, {
128
+ ...init,
129
+ signal: controller.signal,
130
+ });
131
+ const data = await dataExtractor(response);
121
132
  if (!okChecker(response)) {
122
133
  throw new APIError(response.status, data);
123
134
  }
@@ -129,8 +140,8 @@ let DefaultFetchMachine = class DefaultFetchMachine extends AbstractMachine {
129
140
  }
130
141
  throw error; // < API, Syntax / JS Error
131
142
  }
132
- }));
133
- })),
143
+ });
144
+ }),
134
145
  },
135
146
  actions: {
136
147
  setParams: assign(({ event }) => {
@@ -1,4 +1,3 @@
1
- import { __awaiter } from "tslib";
2
1
  import { JSONReviver } from '@genesislcap/foundation-utils';
3
2
  import { defaultFetchPolicy } from './policies';
4
3
  /**
@@ -10,22 +9,22 @@ export const defaultResponseOkChecker = (response) => response.ok;
10
9
  * {@inheritDoc ResponseDataExtractor}
11
10
  * @public
12
11
  */
13
- export const defaultResponseDataExtractor = (response) => __awaiter(void 0, void 0, void 0, function* () {
14
- const text = yield response.text();
12
+ export const defaultResponseDataExtractor = async (response) => {
13
+ const text = await response.text();
15
14
  const contentType = response.headers.get('content-type');
16
- const expectingJson = contentType === null || contentType === void 0 ? void 0 : contentType.includes('application/json');
15
+ const expectingJson = contentType?.includes('application/json');
17
16
  return expectingJson ? JSON.parse(text, JSONReviver) : text;
18
- });
17
+ };
19
18
  /**
20
19
  * Creates an extractor that applies a deserializer to the extracted response data.
21
20
  * @param deserializer - The deserializer to apply.
22
21
  * @param extractor - An optional data extractor. Defaults to {@link defaultResponseDataExtractor}.
23
22
  * @public
24
23
  */
25
- export const createResponseDataExtractorWithDeserializer = (deserializer, extractor = defaultResponseDataExtractor) => (response) => __awaiter(void 0, void 0, void 0, function* () {
26
- const json = yield extractor(response);
24
+ export const createResponseDataExtractorWithDeserializer = (deserializer, extractor = defaultResponseDataExtractor) => async (response) => {
25
+ const json = await extractor(response);
27
26
  return deserializer(json);
28
- });
27
+ };
29
28
  /**
30
29
  * The default fetch config.
31
30
  * @public
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@genesislcap/foundation-state-machine",
3
3
  "description": "Genesis Foundation State Machine",
4
- "version": "14.70.3",
4
+ "version": "14.70.4-es2021.1",
5
5
  "sideEffects": false,
6
6
  "license": "SEE LICENSE IN license.txt",
7
7
  "main": "dist/esm/index.js",
@@ -34,13 +34,13 @@
34
34
  "test": "genx test"
35
35
  },
36
36
  "devDependencies": {
37
- "@genesislcap/foundation-testing": "14.70.3",
38
- "@genesislcap/genx": "14.70.3",
37
+ "@genesislcap/foundation-testing": "14.70.4-es2021.1",
38
+ "@genesislcap/genx": "14.70.4-es2021.1",
39
39
  "rimraf": "^3.0.2"
40
40
  },
41
41
  "dependencies": {
42
- "@genesislcap/foundation-comms": "14.70.3",
43
- "@genesislcap/foundation-utils": "14.70.3",
42
+ "@genesislcap/foundation-comms": "14.70.4-es2021.1",
43
+ "@genesislcap/foundation-utils": "14.70.4-es2021.1",
44
44
  "@microsoft/fast-element": "^1.7.0",
45
45
  "@microsoft/fast-foundation": "^2.33.2",
46
46
  "cockatiel": "^3.1.1",
@@ -56,5 +56,5 @@
56
56
  "publishConfig": {
57
57
  "access": "public"
58
58
  },
59
- "gitHead": "2bd15e0c3928fa0fbfa60ccb7e4f89e956afda73"
59
+ "gitHead": "5d0d8ee36cfd068cfc6211033b326d0a978cfc05"
60
60
  }