oneentry 1.0.96 → 1.0.97

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/README.md CHANGED
@@ -139,6 +139,34 @@ const api = defineOneEntry('your-url', {
139
139
  })
140
140
  ```
141
141
 
142
+ ### Errors
143
+
144
+ If you want to escape errors inside the sc, leave the "errors" property by default.
145
+ In this case, you will receive either the entity data or the error object.
146
+ You need to do a type check. for example, by checking the statusCode property with ".hasOwnProperty"
147
+
148
+ However, if you want to use the construction "try {} catch(e) {}", set the property "isShell" to the value "false".
149
+ In this case, you need to handle the error using "try {} catch(e) {}".
150
+
151
+ Also, you can pass custom functions that will be called inside the sdk with the appropriate error code.
152
+ These functions receive an error object as an argument. You can process it yourself.
153
+
154
+ ```js
155
+ const api = defineOneEntry('your-url', {
156
+ token: 'my-token',
157
+ langCode:'my-langCode',
158
+ errors: {
159
+ isShell: false,
160
+ customErrors: {
161
+ 400: (error) => console.error(error.message),
162
+ 404: (error) => console.error(error.message),
163
+ 500: (error) => console.error(error.message)
164
+ }
165
+ }
166
+ })
167
+ ```
168
+
169
+
142
170
  Now you can use the following links to jump to specific entries:
143
171
  - [Admins](#admins)
144
172
  - [AttributesSets](#attributessets)
@@ -163,11 +163,36 @@ class AsyncModules extends syncModules_1.default {
163
163
  return await secondResponse.json();
164
164
  }
165
165
  }
166
- return await response.json();
166
+ else if (response.status == 400) {
167
+ if (this.state.errorsFunctions["400"]) {
168
+ this.state.errorsFunctions["400"](await response.json());
169
+ }
170
+ }
171
+ else if (response.status == 404) {
172
+ if (this.state.errorsFunctions["404"]) {
173
+ this.state.errorsFunctions["404"](await response.json());
174
+ }
175
+ }
176
+ else if (response.status == 500) {
177
+ if (this.state.errorsFunctions["500"]) {
178
+ this.state.errorsFunctions["500"](await response.json());
179
+ }
180
+ }
181
+ if (this.state.isShell) {
182
+ return await response.json();
183
+ }
184
+ else {
185
+ throw await response.json();
186
+ }
167
187
  }
168
188
  }
169
189
  catch (e) {
170
- return e;
190
+ if (this.state.isShell) {
191
+ return e;
192
+ }
193
+ else {
194
+ throw e;
195
+ }
171
196
  }
172
197
  }
173
198
  }
@@ -1,4 +1,4 @@
1
- import { IConfig } from "./utils";
1
+ import { IConfig, IError } from "./utils";
2
2
  export default class StateModule {
3
3
  url: string | undefined;
4
4
  lang: string | undefined;
@@ -9,6 +9,12 @@ export default class StateModule {
9
9
  customAuth: boolean;
10
10
  _NO_FETCH: boolean;
11
11
  _https: any;
12
+ isShell: boolean;
13
+ errorsFunctions: {
14
+ 404?: (data: IError) => any | null;
15
+ 400?: (data: IError) => any | null;
16
+ 500?: (data: IError) => any | null;
17
+ };
12
18
  saveFunction: (param: string) => void | null;
13
19
  constructor(url: string, config: IConfig);
14
20
  }
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  class StateModule {
4
4
  constructor(url, config) {
5
- var _a, _b, _c, _d, _e, _f, _g, _h;
5
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
6
6
  this.url = url;
7
7
  this.lang = (_a = config.langCode) !== null && _a !== void 0 ? _a : 'en_US';
8
8
  this.token = config.token;
@@ -10,6 +10,15 @@ class StateModule {
10
10
  this.refreshToken = (_d = (_c = config.auth) === null || _c === void 0 ? void 0 : _c.refreshToken) !== null && _d !== void 0 ? _d : undefined;
11
11
  this.customAuth = (_f = (_e = config.auth) === null || _e === void 0 ? void 0 : _e.customAuth) !== null && _f !== void 0 ? _f : false;
12
12
  this.saveFunction = (_h = (_g = config.auth) === null || _g === void 0 ? void 0 : _g.saveFunction) !== null && _h !== void 0 ? _h : null;
13
+ if (config.errors) {
14
+ this.isShell = (_j = config.errors.isShell) !== null && _j !== void 0 ? _j : true;
15
+ this.errorsFunctions["400"] = (_k = config.errors.customErrors["400"]) !== null && _k !== void 0 ? _k : null;
16
+ this.errorsFunctions["404"] = (_l = config.errors.customErrors["400"]) !== null && _l !== void 0 ? _l : null;
17
+ this.errorsFunctions["500"] = (_m = config.errors.customErrors["400"]) !== null && _m !== void 0 ? _m : null;
18
+ }
19
+ else {
20
+ this.isShell = false;
21
+ }
13
22
  try {
14
23
  if (typeof process === 'object' && +process.versions.node.split('.')[0] < 18) {
15
24
  try {
@@ -21,6 +21,13 @@ type Types = 'forCatalogProducts' | 'forBasketPage' | 'forErrorPage' | 'forCatal
21
21
  * customAuth: false,
22
22
  * refreshToken: 'user.refresh.token',
23
23
  * saveFunction: saveToken
24
+ * },
25
+ * errors: {
26
+ * isShell: true,
27
+ * customErrors: {
28
+ * '404': notFoundFunction,
29
+ * '400': errorFunction
30
+ * }
24
31
  * }
25
32
  * }
26
33
  *
@@ -34,6 +41,14 @@ interface IConfig {
34
41
  refreshToken?: string;
35
42
  saveFunction?: (refreshToken: string) => void;
36
43
  };
44
+ errors: {
45
+ isShell?: boolean;
46
+ customErrors: {
47
+ 404?: (data?: IError) => any;
48
+ 400?: (data?: IError) => any;
49
+ 500?: (data?: IError) => any;
50
+ };
51
+ };
37
52
  }
38
53
  interface IAttributes {
39
54
  listTitles: Array<{
@@ -28,16 +28,16 @@ interface IUserEntity {
28
28
  interface IUserBody {
29
29
  formIdentifier: string;
30
30
  langCode?: string;
31
- authData: Array<{
31
+ authData?: Array<{
32
32
  marker: string;
33
33
  value: string;
34
34
  }>;
35
35
  formData?: IAuthFormData | Array<IAuthFormData>;
36
- notificationData: {
36
+ notificationData?: {
37
37
  email: string;
38
38
  phonePush: Array<string>;
39
39
  phoneSMS: string;
40
40
  };
41
- state: Record<string, any>;
41
+ state?: Record<string, any>;
42
42
  }
43
43
  export { IUsers, IUserEntity, IUserBody, };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oneentry",
3
- "version": "1.0.96",
3
+ "version": "1.0.97",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",