@scayle/storefront-core 7.26.0 → 7.28.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 (51) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/api/oauth.cjs +136 -0
  3. package/dist/api/oauth.d.ts +57 -0
  4. package/dist/api/oauth.mjs +125 -0
  5. package/dist/cache/providers/unstorage.cjs +25 -2
  6. package/dist/cache/providers/unstorage.d.ts +10 -0
  7. package/dist/cache/providers/unstorage.mjs +25 -2
  8. package/dist/constants/basket.cjs +2 -3
  9. package/dist/constants/cache.cjs +1 -2
  10. package/dist/constants/hash.cjs +2 -3
  11. package/dist/constants/httpStatus.cjs +3 -5
  12. package/dist/constants/product.cjs +2 -3
  13. package/dist/constants/promotion.cjs +4 -7
  14. package/dist/constants/sorting.cjs +3 -5
  15. package/dist/constants/withParameters.cjs +7 -13
  16. package/dist/helpers/product.fixture.cjs +4 -8
  17. package/dist/index.cjs +2 -2
  18. package/dist/rpc/methods/basket/basket.cjs +6 -12
  19. package/dist/rpc/methods/brands.cjs +3 -5
  20. package/dist/rpc/methods/categories.cjs +4 -7
  21. package/dist/rpc/methods/cbd.cjs +4 -9
  22. package/dist/rpc/methods/cbd.mjs +2 -3
  23. package/dist/rpc/methods/checkout/order.cjs +5 -8
  24. package/dist/rpc/methods/checkout/order.mjs +3 -4
  25. package/dist/rpc/methods/checkout/shopUser.cjs +32 -31
  26. package/dist/rpc/methods/checkout/shopUser.mjs +18 -23
  27. package/dist/rpc/methods/checkout/shopUserAddresses.cjs +9 -8
  28. package/dist/rpc/methods/checkout/shopUserAddresses.mjs +7 -4
  29. package/dist/rpc/methods/navigationTrees.cjs +3 -5
  30. package/dist/rpc/methods/products.cjs +6 -12
  31. package/dist/rpc/methods/promotion.cjs +4 -7
  32. package/dist/rpc/methods/search.cjs +1 -2
  33. package/dist/rpc/methods/session.cjs +52 -99
  34. package/dist/rpc/methods/session.d.ts +9 -4
  35. package/dist/rpc/methods/session.mjs +62 -139
  36. package/dist/rpc/methods/shopConfiguration.cjs +2 -3
  37. package/dist/rpc/methods/user.cjs +24 -21
  38. package/dist/rpc/methods/user.mjs +20 -13
  39. package/dist/rpc/methods/variants.cjs +2 -3
  40. package/dist/rpc/methods/wishlist.cjs +5 -9
  41. package/dist/types/api/auth.d.ts +9 -4
  42. package/dist/utils/fetch.cjs +19 -0
  43. package/dist/utils/fetch.d.ts +9 -0
  44. package/dist/utils/fetch.mjs +12 -0
  45. package/dist/utils/index.cjs +11 -0
  46. package/dist/utils/index.d.ts +1 -0
  47. package/dist/utils/index.mjs +1 -0
  48. package/package.json +7 -12
  49. package/dist/utils/rpc.cjs +0 -41
  50. package/dist/utils/rpc.d.ts +0 -6
  51. package/dist/utils/rpc.mjs +0 -35
@@ -4,16 +4,14 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.refreshUser = exports.getUser = exports.fetchUser = void 0;
7
- var _axios = _interopRequireDefault(require("axios"));
7
+ var _fetch = require("../../utils/fetch.cjs");
8
8
  var _httpStatus = require("../../constants/httpStatus.cjs");
9
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
- const getUser = function getUser2(context) {
9
+ const getUser = exports.getUser = function getUser2(context) {
11
10
  return {
12
11
  user: context.user
13
12
  };
14
13
  };
15
- exports.getUser = getUser;
16
- const fetchUser = async function fetchUser2(payload, options) {
14
+ const fetchUser = exports.fetchUser = async function fetchUser2(payload, options) {
17
15
  const {
18
16
  accessToken
19
17
  } = payload;
@@ -22,20 +20,24 @@ const fetchUser = async function fetchUser2(payload, options) {
22
20
  shopId,
23
21
  accessHeader
24
22
  } = options;
25
- const user = await _axios.default.get(`${checkoutUrl}/api/oauth/me`, {
23
+ const response = await fetch(`${checkoutUrl}/api/oauth/me`, {
26
24
  headers: {
27
25
  Authorization: `Bearer ${accessToken}`,
28
- "X-Shop-Id": shopId,
26
+ "X-Shop-Id": shopId.toString(),
29
27
  "Content-Type": "application/json",
30
28
  ...(accessHeader && {
31
29
  "X-Access-Header": accessHeader
32
30
  })
33
31
  }
34
32
  });
33
+ if (!response.ok) {
34
+ throw new _fetch.FetchError(response);
35
+ }
36
+ const user = await response.json();
35
37
  return {
36
- ...user.data,
38
+ ...user,
37
39
  authentication: {
38
- ...user.data?.authentication,
40
+ ...user?.authentication,
39
41
  storefrontAccessToken: accessToken
40
42
  },
41
43
  ...{
@@ -43,8 +45,7 @@ const fetchUser = async function fetchUser2(payload, options) {
43
45
  }
44
46
  };
45
47
  };
46
- exports.fetchUser = fetchUser;
47
- const refreshUser = async function refreshUser2(context) {
48
+ const refreshUser = exports.refreshUser = async function refreshUser2(context) {
48
49
  const {
49
50
  accessToken,
50
51
  checkout,
@@ -58,19 +59,22 @@ const refreshUser = async function refreshUser2(context) {
58
59
  } = _httpStatus.HttpStatusCode;
59
60
  return status >= OK && status < MULTIPLE_CHOICES || status === FORBIDDEN;
60
61
  };
61
- const user = await _axios.default.get(`${checkout.url}/api/oauth/me`, {
62
+ const response = await fetch(`${checkout.url}/api/oauth/me`, {
62
63
  headers: {
63
64
  Authorization: `Bearer ${accessToken}`,
64
- "X-Shop-Id": shopId,
65
+ "X-Shop-Id": shopId.toString(),
65
66
  "Content-Type": "application/json"
66
- },
67
- validateStatus
67
+ }
68
68
  });
69
- if (user.data.id) {
69
+ if (!response.ok || !validateStatus(response.status)) {
70
+ throw new _fetch.FetchError(response);
71
+ }
72
+ const user = await response.json();
73
+ if (user.id) {
70
74
  context.updateUser({
71
- ...user.data,
75
+ ...user,
72
76
  authentication: {
73
- ...user.data?.authentication,
77
+ ...user?.authentication,
74
78
  storefrontAccessToken: accessToken
75
79
  },
76
80
  ...{
@@ -78,12 +82,11 @@ const refreshUser = async function refreshUser2(context) {
78
82
  }
79
83
  });
80
84
  return {
81
- user: user.data
85
+ user
82
86
  };
83
87
  }
84
88
  await context.destroySession();
85
89
  return {
86
90
  user: void 0
87
91
  };
88
- };
89
- exports.refreshUser = refreshUser;
92
+ };
@@ -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 };
@@ -7,7 +7,7 @@ exports.getVariantById = void 0;
7
7
  const defaultWith = {
8
8
  attributes: "all"
9
9
  };
10
- const getVariantById = async function getVariantById2({
10
+ const getVariantById = exports.getVariantById = async function getVariantById2({
11
11
  ids,
12
12
  include = defaultWith,
13
13
  pricePromotionKey = "default"
@@ -25,5 +25,4 @@ const getVariantById = async function getVariantById2({
25
25
  campaignKey,
26
26
  pricePromotionKey
27
27
  } : void 0);
28
- };
29
- exports.getVariantById = getVariantById;
28
+ };
@@ -9,7 +9,7 @@ var _constants = require("../../constants/index.cjs");
9
9
  function getWithParams(params, context) {
10
10
  return params.with ?? context.withParams?.basket ?? _constants.MIN_WITH_PARAMS_WISHLIST;
11
11
  }
12
- const addItemToWishlist = async function addItemToWishlist2(options, context) {
12
+ const addItemToWishlist = exports.addItemToWishlist = async function addItemToWishlist2(options, context) {
13
13
  const {
14
14
  bapiClient,
15
15
  campaignKey,
@@ -44,8 +44,7 @@ const addItemToWishlist = async function addItemToWishlist2(options, context) {
44
44
  throw new _errors.BAPIError("BAPI ERROR", code, message, "bapiClient.wishlist.addItem");
45
45
  }
46
46
  };
47
- exports.addItemToWishlist = addItemToWishlist;
48
- const getWishlist = async function getWishlist2(options, context) {
47
+ const getWishlist = exports.getWishlist = async function getWishlist2(options, context) {
49
48
  const {
50
49
  bapiClient,
51
50
  campaignKey
@@ -59,8 +58,7 @@ const getWishlist = async function getWishlist2(options, context) {
59
58
  pricePromotionKey: resolvedWith?.pricePromotionKey ?? ""
60
59
  });
61
60
  };
62
- exports.getWishlist = getWishlist;
63
- const removeItemFromWishlist = async function removeItemFromWishlist2(options, context) {
61
+ const removeItemFromWishlist = exports.removeItemFromWishlist = async function removeItemFromWishlist2(options, context) {
64
62
  const {
65
63
  bapiClient,
66
64
  campaignKey,
@@ -72,8 +70,7 @@ const removeItemFromWishlist = async function removeItemFromWishlist2(options, c
72
70
  campaignKey
73
71
  });
74
72
  };
75
- exports.removeItemFromWishlist = removeItemFromWishlist;
76
- const clearWishlist = async function clearWishlist2(context) {
73
+ const clearWishlist = exports.clearWishlist = async function clearWishlist2(context) {
77
74
  const wishlist = await getWishlist({}, context);
78
75
  await Promise.all(wishlist.items.map(async item => {
79
76
  await removeItemFromWishlist({
@@ -81,5 +78,4 @@ const clearWishlist = async function clearWishlist2(context) {
81
78
  }, context);
82
79
  }));
83
80
  return await getWishlist({}, context);
84
- };
85
- exports.clearWishlist = clearWishlist;
81
+ };
@@ -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.26.0",
3
+ "version": "7.28.0",
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.2",
60
- "axios": "^0.21.1 || ^0.27.0"
61
- },
62
58
  "dependencies": {
63
- "@aboutyou/backbone": "15.14.2",
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"
@@ -73,13 +68,12 @@
73
68
  "devDependencies": {
74
69
  "@scayle/eslint-config-storefront": "3.2.5",
75
70
  "@scayle/prettier-config-storefront": "2.0.2",
76
- "@types/crypto-js": "4.1.3",
77
- "@types/jest": "29.5.8",
78
- "@types/node": "20.9.0",
71
+ "@types/crypto-js": "4.2.1",
72
+ "@types/jest": "29.5.10",
73
+ "@types/node": "20.9.5",
79
74
  "@types/webpack-env": "1.18.4",
80
75
  "unbuild": "2.0.0",
81
- "axios": "0.27.2",
82
- "eslint": "8.53.0",
76
+ "eslint": "8.54.0",
83
77
  "eslint-formatter-gitlab": "5.1.0",
84
78
  "jest": "29.7.0",
85
79
  "jest-junit": "16.0.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
- };