@scayle/storefront-core 7.27.0 → 7.28.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.
@@ -1,4 +1,4 @@
1
- import axios from "axios";
1
+ import { FetchError } from "../../utils/fetch.mjs";
2
2
  import { HttpStatusCode } from "../../constants/httpStatus.mjs";
3
3
  const getUser = function getUser2(context) {
4
4
  return {
@@ -8,18 +8,22 @@ const getUser = function getUser2(context) {
8
8
  const fetchUser = async function fetchUser2(payload, options) {
9
9
  const { accessToken } = payload;
10
10
  const { checkoutUrl, shopId, accessHeader } = options;
11
- const user = await axios.get(`${checkoutUrl}/api/oauth/me`, {
11
+ const response = await fetch(`${checkoutUrl}/api/oauth/me`, {
12
12
  headers: {
13
13
  Authorization: `Bearer ${accessToken}`,
14
- "X-Shop-Id": shopId,
14
+ "X-Shop-Id": shopId.toString(),
15
15
  "Content-Type": "application/json",
16
16
  ...accessHeader && { "X-Access-Header": accessHeader }
17
17
  }
18
18
  });
19
+ if (!response.ok) {
20
+ throw new FetchError(response);
21
+ }
22
+ const user = await response.json();
19
23
  return {
20
- ...user.data,
24
+ ...user,
21
25
  authentication: {
22
- ...user.data?.authentication,
26
+ ...user?.authentication,
23
27
  storefrontAccessToken: accessToken
24
28
  },
25
29
  ...{ loginShopId: shopId }
@@ -31,24 +35,27 @@ const refreshUser = async function refreshUser2(context) {
31
35
  const { OK, MULTIPLE_CHOICES, FORBIDDEN } = HttpStatusCode;
32
36
  return status >= OK && status < MULTIPLE_CHOICES || status === FORBIDDEN;
33
37
  };
34
- const user = await axios.get(`${checkout.url}/api/oauth/me`, {
38
+ const response = await fetch(`${checkout.url}/api/oauth/me`, {
35
39
  headers: {
36
40
  Authorization: `Bearer ${accessToken}`,
37
- "X-Shop-Id": shopId,
41
+ "X-Shop-Id": shopId.toString(),
38
42
  "Content-Type": "application/json"
39
- },
40
- validateStatus
43
+ }
41
44
  });
42
- if (user.data.id) {
45
+ if (!response.ok || !validateStatus(response.status)) {
46
+ throw new FetchError(response);
47
+ }
48
+ const user = await response.json();
49
+ if (user.id) {
43
50
  context.updateUser({
44
- ...user.data,
51
+ ...user,
45
52
  authentication: {
46
- ...user.data?.authentication,
53
+ ...user?.authentication,
47
54
  storefrontAccessToken: accessToken
48
55
  },
49
56
  ...{ loginShopId: shopId }
50
57
  });
51
- return { user: user.data };
58
+ return { user };
52
59
  }
53
60
  await context.destroySession();
54
61
  return { user: void 0 };
@@ -1,4 +1,4 @@
1
- import { Gender } from '../user';
1
+ import type { Gender } from '../user';
2
2
  export interface AuthConfig {
3
3
  callback: string;
4
4
  scopes?: string[];
@@ -30,7 +30,7 @@ export interface UnauthorizedError extends BaseError {
30
30
  export interface LoginRequest {
31
31
  email: string;
32
32
  password: string;
33
- shop_id?: number;
33
+ shop_id: number;
34
34
  }
35
35
  export interface RegisterRequest {
36
36
  first_name: string;
@@ -38,14 +38,14 @@ export interface RegisterRequest {
38
38
  email: string;
39
39
  password: string;
40
40
  gender: Gender;
41
- shop_id?: number;
41
+ shop_id: number;
42
42
  }
43
43
  export interface GuestRequest {
44
44
  first_name: string;
45
45
  last_name: string;
46
46
  email: string;
47
47
  gender: Gender;
48
- shop_id?: number;
48
+ shop_id: number;
49
49
  }
50
50
  export interface SendResetPasswordEmailRequest {
51
51
  email: string;
@@ -53,6 +53,11 @@ export interface SendResetPasswordEmailRequest {
53
53
  export interface UpdatePasswordByHashRequest {
54
54
  password: string;
55
55
  hash: string;
56
+ shop_id: number;
57
+ }
58
+ export interface RefreshTokenRequest {
59
+ grant_type: 'refresh_token';
60
+ refresh_token: string;
56
61
  }
57
62
  export interface Oauth {
58
63
  token_type: 'Bearer';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.FetchError = void 0;
7
+ class FetchError extends Error {
8
+ response;
9
+ data;
10
+ constructor(response, data) {
11
+ const message = `Failed to fetch ${response.url}. ${response.status} ${response.statusText}`;
12
+ super(message);
13
+ this.response = response;
14
+ this.data = data;
15
+ this.name = "FetchError";
16
+ this.message = message;
17
+ }
18
+ }
19
+ exports.FetchError = FetchError;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * A custom Error type for errors that occur in a fetch() request
3
+ * Contains the Response object from the request, and optionally the response data
4
+ */
5
+ export declare class FetchError extends Error {
6
+ response: Response;
7
+ data?: unknown;
8
+ constructor(response: Response, data?: unknown);
9
+ }
@@ -0,0 +1,12 @@
1
+ export class FetchError extends Error {
2
+ response;
3
+ data;
4
+ constructor(response, data) {
5
+ const message = `Failed to fetch ${response.url}. ${response.status} ${response.statusText}`;
6
+ super(message);
7
+ this.response = response;
8
+ this.data = data;
9
+ this.name = "FetchError";
10
+ this.message = message;
11
+ }
12
+ }
@@ -24,4 +24,15 @@ Object.keys(_log).forEach(function (key) {
24
24
  return _log[key];
25
25
  }
26
26
  });
27
+ });
28
+ var _fetch = require("./fetch.cjs");
29
+ Object.keys(_fetch).forEach(function (key) {
30
+ if (key === "default" || key === "__esModule") return;
31
+ if (key in exports && exports[key] === _fetch[key]) return;
32
+ Object.defineProperty(exports, key, {
33
+ enumerable: true,
34
+ get: function () {
35
+ return _fetch[key];
36
+ }
37
+ });
27
38
  });
@@ -1,2 +1,3 @@
1
1
  export * from './timeout';
2
2
  export * from './log';
3
+ export * from './fetch';
@@ -1,2 +1,3 @@
1
1
  export * from "./timeout.mjs";
2
2
  export * from "./log.mjs";
3
+ export * from "./fetch.mjs";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scayle/storefront-core",
3
- "version": "7.27.0",
3
+ "version": "7.28.1",
4
4
  "description": "Collection of essential utilities to work with the Storefront API",
5
5
  "author": "SCAYLE Commerce Engine",
6
6
  "license": "MIT",
@@ -55,17 +55,12 @@
55
55
  "test": "jest --passWithNoTests --maxWorkers=50%",
56
56
  "test:ci": "jest --passWithNoTests --runInBand --coverage --reporters=default --reporters=jest-junit"
57
57
  },
58
- "peerDependencies": {
59
- "@aboutyou/backbone": "15.14.3",
60
- "axios": "^0.21.1 || ^0.27.0"
61
- },
62
58
  "dependencies": {
63
59
  "@aboutyou/backbone": "15.14.3",
64
60
  "crypto-js": "4.2.0",
65
61
  "jose": "^4.14.4",
66
62
  "radash": "^11.0.0",
67
63
  "slugify": "^1.6.0",
68
- "typescript": "^5.0.0",
69
64
  "ufo": "^1.1.1",
70
65
  "uncrypto": "^0.1.3",
71
66
  "utility-types": "^3.10.0"
@@ -75,10 +70,9 @@
75
70
  "@scayle/prettier-config-storefront": "2.0.2",
76
71
  "@types/crypto-js": "4.2.1",
77
72
  "@types/jest": "29.5.10",
78
- "@types/node": "20.9.4",
73
+ "@types/node": "20.10.0",
79
74
  "@types/webpack-env": "1.18.4",
80
75
  "unbuild": "2.0.0",
81
- "axios": "0.27.2",
82
76
  "eslint": "8.54.0",
83
77
  "eslint-formatter-gitlab": "5.1.0",
84
78
  "jest": "29.7.0",
@@ -88,6 +82,7 @@
88
82
  "rimraf": "5.0.5",
89
83
  "ts-jest": "29.1.1",
90
84
  "ts-node": "10.9.1",
85
+ "typescript": "5.3.2",
91
86
  "unstorage": "1.10.1"
92
87
  },
93
88
  "optionalDependencies": {
@@ -1,41 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.sanitizeErrorObject = exports.convertErrorForRpcCall = void 0;
7
- var _axios = _interopRequireDefault(require("axios"));
8
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
- const isErrorWithConfig = error => {
10
- return !error || error.constructor !== Object || !("config" in error) || !error.config.headers;
11
- };
12
- const sanitizeErrorObject = error => {
13
- if (!isErrorWithConfig(error)) {
14
- return error;
15
- }
16
- return {
17
- ...error,
18
- config: {
19
- ...error?.config,
20
- headers: {
21
- ...error.config.headers,
22
- Authorization: void 0
23
- }
24
- }
25
- };
26
- };
27
- exports.sanitizeErrorObject = sanitizeErrorObject;
28
- const convertErrorForRpcCall = (error, httpStatuses) => {
29
- if (_axios.default.isAxiosError(error) && error.response?.status && httpStatuses.includes(error.response.status)) {
30
- const data = Object.assign({
31
- code: error.response.status,
32
- message: error.response.data
33
- }, error.response.data);
34
- error.response = {
35
- ...error.response,
36
- data
37
- };
38
- return error;
39
- }
40
- };
41
- exports.convertErrorForRpcCall = convertErrorForRpcCall;
@@ -1,6 +0,0 @@
1
- export declare const sanitizeErrorObject: (error: object) => object;
2
- /**
3
- * It is important to convert error to the format that catch in rpcCall expects,
4
- * rpcCall waits for code and message be present in the error otherwise it falls back to 500
5
- */
6
- export declare const convertErrorForRpcCall: (error: any, httpStatuses: Array<number>) => Error;
@@ -1,35 +0,0 @@
1
- import axios from "axios";
2
- const isErrorWithConfig = (error) => {
3
- return !error || error.constructor !== Object || !("config" in error) || !error.config.headers;
4
- };
5
- export const sanitizeErrorObject = (error) => {
6
- if (!isErrorWithConfig(error)) {
7
- return error;
8
- }
9
- return {
10
- ...error,
11
- config: {
12
- ...error?.config,
13
- headers: {
14
- ...error.config.headers,
15
- Authorization: void 0
16
- }
17
- }
18
- };
19
- };
20
- export const convertErrorForRpcCall = (error, httpStatuses) => {
21
- if (axios.isAxiosError(error) && error.response?.status && httpStatuses.includes(error.response.status)) {
22
- const data = Object.assign(
23
- {
24
- code: error.response.status,
25
- message: error.response.data
26
- },
27
- error.response.data
28
- );
29
- error.response = {
30
- ...error.response,
31
- data
32
- };
33
- return error;
34
- }
35
- };