@scayle/storefront-core 7.61.0 → 7.63.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @scayle/storefront-core
2
2
 
3
+ ## 7.63.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Deprecate `RpcContext.storeCampaignKeyword`
8
+
9
+ ## 7.62.0
10
+
11
+ ### Minor Changes
12
+
13
+ - Return `ErrorResponse` from RPC when Session is missing
14
+
3
15
  ## 7.61.0
4
16
 
5
17
  ### Minor Changes
@@ -166,6 +166,7 @@ class OAuthClient {
166
166
  }
167
167
  /**
168
168
  * Revoke an access token
169
+ * @param shopId
169
170
  * @param accessToken
170
171
  */
171
172
  async revokeToken(shopId, accessToken) {
@@ -181,8 +182,8 @@ class OAuthClient {
181
182
  }).then(emptyOAuthResponseHandler);
182
183
  }
183
184
  /**
184
- * Revoke all tokens for a user
185
- * @param userId
185
+ * Generate a new token based on authorization code
186
+ * @param code
186
187
  */
187
188
  async generateToken(code) {
188
189
  return await fetch(`${this.baseURL}/oauth/token`, {
@@ -60,12 +60,13 @@ export declare class OAuthClient {
60
60
  validateToken(accessToken: string): Promise<void>;
61
61
  /**
62
62
  * Revoke an access token
63
+ * @param shopId
63
64
  * @param accessToken
64
65
  */
65
66
  revokeToken(shopId: number, accessToken: string): Promise<void>;
66
67
  /**
67
- * Revoke all tokens for a user
68
- * @param userId
68
+ * Generate a new token based on authorization code
69
+ * @param code
69
70
  */
70
71
  generateToken(code: string): Promise<Oauth>;
71
72
  }
@@ -151,6 +151,7 @@ export class OAuthClient {
151
151
  }
152
152
  /**
153
153
  * Revoke an access token
154
+ * @param shopId
154
155
  * @param accessToken
155
156
  */
156
157
  async revokeToken(shopId, accessToken) {
@@ -166,8 +167,8 @@ export class OAuthClient {
166
167
  }).then(emptyOAuthResponseHandler);
167
168
  }
168
169
  /**
169
- * Revoke all tokens for a user
170
- * @param userId
170
+ * Generate a new token based on authorization code
171
+ * @param code
171
172
  */
172
173
  async generateToken(code) {
173
174
  return await fetch(`${this.baseURL}/oauth/token`, {
@@ -80,6 +80,6 @@ export declare const serializeFilters: (filter: Filter) => SerializedFilter | un
80
80
  /**
81
81
  * Converts filter values back into their original data type.
82
82
  *
83
- * @param filters
83
+ * @param serializedFilter
84
84
  */
85
85
  export declare const deserializeFilters: (serializedFilter: SerializedFilter) => Filter;
@@ -23,7 +23,9 @@ const addItemToBasket = exports.addItemToBasket = async function addItemToBasket
23
23
  itemGroup,
24
24
  with: _with
25
25
  }, context) {
26
- (0, _types.assertSession)(context);
26
+ if (!(0, _types.hasSession)(context)) {
27
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusMessage.BAD_REQUEST, "No Session found");
28
+ }
27
29
  const {
28
30
  campaignKey,
29
31
  bapiClient,
@@ -67,7 +69,9 @@ const addItemToBasket = exports.addItemToBasket = async function addItemToBasket
67
69
  }
68
70
  };
69
71
  const addItemsToBasket = exports.addItemsToBasket = async function addItemsToBasket2(params, context) {
70
- (0, _types.assertSession)(context);
72
+ if (!(0, _types.hasSession)(context)) {
73
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusMessage.BAD_REQUEST, "No Session found");
74
+ }
71
75
  const {
72
76
  campaignKey,
73
77
  bapiClient,
@@ -111,7 +115,9 @@ const addItemsToBasket = exports.addItemsToBasket = async function addItemsToBas
111
115
  }
112
116
  };
113
117
  const getBasket = exports.getBasket = async function getBasket2(options, context) {
114
- (0, _types.assertSession)(context);
118
+ if (!(0, _types.hasSession)(context)) {
119
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusMessage.BAD_REQUEST, "No Session found");
120
+ }
115
121
  const {
116
122
  bapiClient,
117
123
  campaignKey,
@@ -137,7 +143,9 @@ const getBasket = exports.getBasket = async function getBasket2(options, context
137
143
  return response.basket;
138
144
  };
139
145
  const removeItemFromBasket = exports.removeItemFromBasket = async function removeItemFromBasket2(options, context) {
140
- (0, _types.assertSession)(context);
146
+ if (!(0, _types.hasSession)(context)) {
147
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusMessage.BAD_REQUEST, "No Session found");
148
+ }
141
149
  const {
142
150
  bapiClient,
143
151
  campaignKey,
@@ -15,7 +15,7 @@ export declare const getBasket: (options: BasketWithOptions | undefined, context
15
15
  export declare const removeItemFromBasket: (options: {
16
16
  itemKey: string;
17
17
  with?: BasketWithOptions;
18
- }, context: RpcContext) => Promise<BasketResponseData<Product, Variant>>;
18
+ }, context: RpcContext) => Promise<ErrorResponse | BasketResponseData<Product, Variant>>;
19
19
  export declare const clearBasket: (context: RpcContext) => Promise<true | ErrorResponse>;
20
20
  export declare const mergeBaskets: ({ fromBasketKey, toBasketKey, with: _with }: {
21
21
  fromBasketKey: string;
@@ -1,7 +1,9 @@
1
1
  import { ErrorResponse } from "../../../errors/index.mjs";
2
- import { assertSession } from "../../../types/index.mjs";
2
+ import { hasSession } from "../../../types/index.mjs";
3
3
  import {
4
4
  ExistingItemHandling,
5
+ HttpStatusCode,
6
+ HttpStatusMessage,
5
7
  MIN_WITH_PARAMS_BASKET
6
8
  } from "../../../constants/index.mjs";
7
9
  import { mergeBaskets as mergeBasketFunction } from "../../../utils/user.mjs";
@@ -20,7 +22,13 @@ export const addItemToBasket = async function addItemToBasket2({
20
22
  itemGroup,
21
23
  with: _with
22
24
  }, context) {
23
- assertSession(context);
25
+ if (!hasSession(context)) {
26
+ return new ErrorResponse(
27
+ HttpStatusCode.BAD_REQUEST,
28
+ HttpStatusMessage.BAD_REQUEST,
29
+ "No Session found"
30
+ );
31
+ }
24
32
  const { campaignKey, bapiClient, basketKey } = context;
25
33
  const resolvedWith = getWithParams(
26
34
  { with: _with },
@@ -67,7 +75,13 @@ export const addItemToBasket = async function addItemToBasket2({
67
75
  }
68
76
  };
69
77
  export const addItemsToBasket = async function addItemsToBasket2(params, context) {
70
- assertSession(context);
78
+ if (!hasSession(context)) {
79
+ return new ErrorResponse(
80
+ HttpStatusCode.BAD_REQUEST,
81
+ HttpStatusMessage.BAD_REQUEST,
82
+ "No Session found"
83
+ );
84
+ }
71
85
  const { campaignKey, bapiClient, basketKey } = context;
72
86
  const resolvedWith = getWithParams(params, context);
73
87
  const itemsToBeAddedOrUpdated = params.items.map((item) => ({
@@ -114,7 +128,13 @@ export const addItemsToBasket = async function addItemsToBasket2(params, context
114
128
  }
115
129
  };
116
130
  export const getBasket = async function getBasket2(options, context) {
117
- assertSession(context);
131
+ if (!hasSession(context)) {
132
+ return new ErrorResponse(
133
+ HttpStatusCode.BAD_REQUEST,
134
+ HttpStatusMessage.BAD_REQUEST,
135
+ "No Session found"
136
+ );
137
+ }
118
138
  const { bapiClient, campaignKey, basketKey } = context;
119
139
  const resolvedWith = getWithParams(
120
140
  { with: options },
@@ -139,7 +159,13 @@ export const getBasket = async function getBasket2(options, context) {
139
159
  return response.basket;
140
160
  };
141
161
  export const removeItemFromBasket = async function removeItemFromBasket2(options, context) {
142
- assertSession(context);
162
+ if (!hasSession(context)) {
163
+ return new ErrorResponse(
164
+ HttpStatusCode.BAD_REQUEST,
165
+ HttpStatusMessage.BAD_REQUEST,
166
+ "No Session found"
167
+ );
168
+ }
143
169
  const { bapiClient, campaignKey, basketKey } = context;
144
170
  const resolvedWith = getWithParams(options, context);
145
171
  return await bapiClient.basket.deleteItem(basketKey, options.itemKey, {
@@ -33,7 +33,7 @@ const getCheckoutToken = exports.getCheckoutToken = async function getCheckoutTo
33
33
  carrier,
34
34
  basketId: context.basketKey,
35
35
  campaignKey: context.campaignKey
36
- }).setIssuedAt(now).setNotBefore(now).setExpirationTime("1h").setIssuer(`${"@scayle/storefront-core"}@${"7.61.0"}`).setProtectedHeader({
36
+ }).setIssuedAt(now).setNotBefore(now).setExpirationTime("1h").setIssuer(`${"@scayle/storefront-core"}@${"7.63.0"}`).setProtectedHeader({
37
37
  alg: "HS256",
38
38
  typ: "JWT"
39
39
  }).sign(secret);
@@ -1,3 +1,4 @@
1
+ import { ErrorResponse } from '../../../errors';
1
2
  import type { RpcContext } from '../../../types';
2
3
  interface CheckoutJwtPayload {
3
4
  voucher?: string;
@@ -8,7 +9,7 @@ interface CheckoutJwtPayload {
8
9
  };
9
10
  carrier?: string;
10
11
  }
11
- export declare const getCheckoutToken: (jwtPayload: CheckoutJwtPayload | undefined, context: RpcContext) => Promise<Response | {
12
+ export declare const getCheckoutToken: (jwtPayload: CheckoutJwtPayload | undefined, context: RpcContext) => Promise<ErrorResponse | {
12
13
  accessToken: string;
13
14
  checkoutJwt: string;
14
15
  }>;
@@ -27,7 +27,7 @@ export const getCheckoutToken = async function getCheckoutToken2(jwtPayload = {}
27
27
  carrier,
28
28
  basketId: context.basketKey,
29
29
  campaignKey: context.campaignKey
30
- }).setIssuedAt(now).setNotBefore(now).setExpirationTime("1h").setIssuer(`${"@scayle/storefront-core"}@${"7.61.0"}`).setProtectedHeader({ alg: "HS256", typ: "JWT" }).sign(secret);
30
+ }).setIssuedAt(now).setNotBefore(now).setExpirationTime("1h").setIssuer(`${"@scayle/storefront-core"}@${"7.63.0"}`).setProtectedHeader({ alg: "HS256", typ: "JWT" }).sign(secret);
31
31
  return {
32
32
  accessToken: refreshedAccessToken,
33
33
  checkoutJwt
@@ -47,7 +47,9 @@ const getExternalIdpRedirect = exports.getExternalIdpRedirect = async function g
47
47
  return Object.fromEntries(results);
48
48
  };
49
49
  const handleIDPLoginCallback = exports.handleIDPLoginCallback = async function handleIDPLoginCallback2(payload, context) {
50
- (0, _types.assertSession)(context);
50
+ if (!(0, _types.hasSession)(context)) {
51
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusMessage.BAD_REQUEST, "No Session found");
52
+ }
51
53
  const OAuthClient = (0, _oauth.getOAuthClient)(context);
52
54
  const code = typeof payload === "string" ? payload : payload.code;
53
55
  const tokens = await OAuthClient.generateToken(code);
@@ -7,6 +7,6 @@ export declare const getExternalIdpRedirect: ({ queryParams }: {
7
7
  }>;
8
8
  export declare const handleIDPLoginCallback: (payload: {
9
9
  code: string;
10
- } | string, context: RpcContext) => Promise<{
10
+ } | string, context: RpcContext) => Promise<ErrorResponse | {
11
11
  message: string;
12
12
  }>;
@@ -1,7 +1,7 @@
1
1
  import { SignJWT } from "jose";
2
2
  import { ErrorResponse } from "../../../errors/index.mjs";
3
3
  import { HttpStatusCode, HttpStatusMessage } from "../../../constants/index.mjs";
4
- import { assertSession } from "../../../types/index.mjs";
4
+ import { hasSession } from "../../../types/index.mjs";
5
5
  import { getOAuthClient } from "../../../api/oauth.mjs";
6
6
  import { postLogin } from "../session.mjs";
7
7
  export const getExternalIdpRedirect = async function getExternalIdpRedirect2({ queryParams }, context) {
@@ -46,7 +46,13 @@ export const getExternalIdpRedirect = async function getExternalIdpRedirect2({ q
46
46
  return Object.fromEntries(results);
47
47
  };
48
48
  export const handleIDPLoginCallback = async function handleIDPLoginCallback2(payload, context) {
49
- assertSession(context);
49
+ if (!hasSession(context)) {
50
+ return new ErrorResponse(
51
+ HttpStatusCode.BAD_REQUEST,
52
+ HttpStatusMessage.BAD_REQUEST,
53
+ "No Session found"
54
+ );
55
+ }
50
56
  const OAuthClient = getOAuthClient(context);
51
57
  const code = typeof payload === "string" ? payload : payload.code;
52
58
  const tokens = await OAuthClient.generateToken(code);
@@ -46,7 +46,9 @@ async function postLogin(context, tokens) {
46
46
  }, context);
47
47
  }
48
48
  const oauthLogin = async (login, context) => {
49
- (0, _types.assertSession)(context);
49
+ if (!(0, _types.hasSession)(context)) {
50
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusMessage.BAD_REQUEST, "No Session found");
51
+ }
50
52
  const shopId = context.shopId;
51
53
  const client = (0, _oauth.getOAuthClient)(context);
52
54
  if (!login.email || !login.password) {
@@ -71,7 +73,9 @@ const oauthLogin = async (login, context) => {
71
73
  };
72
74
  exports.oauthLogin = oauthLogin;
73
75
  const oauthRegister = async (register, context) => {
74
- (0, _types.assertSession)(context);
76
+ if (!(0, _types.hasSession)(context)) {
77
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusMessage.BAD_REQUEST, "No Session found");
78
+ }
75
79
  const shopId = context.shopId;
76
80
  const client = (0, _oauth.getOAuthClient)(context);
77
81
  try {
@@ -93,7 +97,9 @@ const oauthRegister = async (register, context) => {
93
97
  };
94
98
  exports.oauthRegister = oauthRegister;
95
99
  const oauthGuestLogin = async (guest, context) => {
96
- (0, _types.assertSession)(context);
100
+ if (!(0, _types.hasSession)(context)) {
101
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusMessage.BAD_REQUEST, "No Session found");
102
+ }
97
103
  const shopId = context.shopId;
98
104
  const client = (0, _oauth.getOAuthClient)(context);
99
105
  try {
@@ -115,7 +121,9 @@ const oauthGuestLogin = async (guest, context) => {
115
121
  };
116
122
  exports.oauthGuestLogin = oauthGuestLogin;
117
123
  const refreshAccessToken = async context => {
118
- (0, _types.assertSession)(context);
124
+ if (!(0, _types.hasSession)(context)) {
125
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusMessage.BAD_REQUEST, "No Session found");
126
+ }
119
127
  const refreshToken = context.refreshToken;
120
128
  const client = (0, _oauth.getOAuthClient)(context);
121
129
  if (!refreshToken) {
@@ -145,7 +153,9 @@ const refreshAccessToken = async context => {
145
153
  };
146
154
  exports.refreshAccessToken = refreshAccessToken;
147
155
  const oauthRevokeToken = async context => {
148
- (0, _types.assertSession)(context);
156
+ if (!(0, _types.hasSession)(context)) {
157
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusMessage.BAD_REQUEST, "No Session found");
158
+ }
149
159
  const accessToken = context.accessToken;
150
160
  if (!accessToken) {
151
161
  return new _errors.ErrorResponse(_constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusMessage.UNAUTHORIZED, "No access token present");
@@ -177,7 +187,9 @@ exports.oauthRevokeToken = oauthRevokeToken;
177
187
  const oauthForgetPassword = async ({
178
188
  email
179
189
  }, context) => {
180
- (0, _types.assertSession)(context);
190
+ if (!(0, _types.hasSession)(context)) {
191
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusMessage.BAD_REQUEST, "No Session found");
192
+ }
181
193
  const shopId = context.shopId;
182
194
  const client = (0, _oauth.getOAuthClient)(context);
183
195
  try {
@@ -210,7 +222,9 @@ const oauthForgetPassword = async ({
210
222
  };
211
223
  exports.oauthForgetPassword = oauthForgetPassword;
212
224
  const updatePasswordByHash = async (passwordHash, context) => {
213
- (0, _types.assertSession)(context);
225
+ if (!(0, _types.hasSession)(context)) {
226
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusMessage.BAD_REQUEST, "No Session found");
227
+ }
214
228
  const shopId = context.shopId;
215
229
  const client = (0, _oauth.getOAuthClient)(context);
216
230
  try {
@@ -1,5 +1,5 @@
1
1
  import { decodeJwt } from "jose";
2
- import { assertSession } from "../../types/index.mjs";
2
+ import { hasSession } from "../../types/index.mjs";
3
3
  import {
4
4
  DEFAULT_WITH_LISTING,
5
5
  HttpStatusCode,
@@ -58,7 +58,13 @@ export async function postLogin(context, tokens) {
58
58
  }, context);
59
59
  }
60
60
  export const oauthLogin = async (login, context) => {
61
- assertSession(context);
61
+ if (!hasSession(context)) {
62
+ return new ErrorResponse(
63
+ HttpStatusCode.BAD_REQUEST,
64
+ HttpStatusMessage.BAD_REQUEST,
65
+ "No Session found"
66
+ );
67
+ }
62
68
  const shopId = context.shopId;
63
69
  const client = getOAuthClient(context);
64
70
  if (!login.email || !login.password) {
@@ -85,7 +91,13 @@ export const oauthLogin = async (login, context) => {
85
91
  }
86
92
  };
87
93
  export const oauthRegister = async (register, context) => {
88
- assertSession(context);
94
+ if (!hasSession(context)) {
95
+ return new ErrorResponse(
96
+ HttpStatusCode.BAD_REQUEST,
97
+ HttpStatusMessage.BAD_REQUEST,
98
+ "No Session found"
99
+ );
100
+ }
89
101
  const shopId = context.shopId;
90
102
  const client = getOAuthClient(context);
91
103
  try {
@@ -108,7 +120,13 @@ export const oauthRegister = async (register, context) => {
108
120
  }
109
121
  };
110
122
  export const oauthGuestLogin = async (guest, context) => {
111
- assertSession(context);
123
+ if (!hasSession(context)) {
124
+ return new ErrorResponse(
125
+ HttpStatusCode.BAD_REQUEST,
126
+ HttpStatusMessage.BAD_REQUEST,
127
+ "No Session found"
128
+ );
129
+ }
112
130
  const shopId = context.shopId;
113
131
  const client = getOAuthClient(context);
114
132
  try {
@@ -131,7 +149,13 @@ export const oauthGuestLogin = async (guest, context) => {
131
149
  }
132
150
  };
133
151
  export const refreshAccessToken = async (context) => {
134
- assertSession(context);
152
+ if (!hasSession(context)) {
153
+ return new ErrorResponse(
154
+ HttpStatusCode.BAD_REQUEST,
155
+ HttpStatusMessage.BAD_REQUEST,
156
+ "No Session found"
157
+ );
158
+ }
135
159
  const refreshToken = context.refreshToken;
136
160
  const client = getOAuthClient(context);
137
161
  if (!refreshToken) {
@@ -163,7 +187,13 @@ export const refreshAccessToken = async (context) => {
163
187
  }
164
188
  };
165
189
  export const oauthRevokeToken = async (context) => {
166
- assertSession(context);
190
+ if (!hasSession(context)) {
191
+ return new ErrorResponse(
192
+ HttpStatusCode.BAD_REQUEST,
193
+ HttpStatusMessage.BAD_REQUEST,
194
+ "No Session found"
195
+ );
196
+ }
167
197
  const accessToken = context.accessToken;
168
198
  if (!accessToken) {
169
199
  return new ErrorResponse(
@@ -196,7 +226,13 @@ export const oauthRevokeToken = async (context) => {
196
226
  }
197
227
  };
198
228
  export const oauthForgetPassword = async ({ email }, context) => {
199
- assertSession(context);
229
+ if (!hasSession(context)) {
230
+ return new ErrorResponse(
231
+ HttpStatusCode.BAD_REQUEST,
232
+ HttpStatusMessage.BAD_REQUEST,
233
+ "No Session found"
234
+ );
235
+ }
200
236
  const shopId = context.shopId;
201
237
  const client = getOAuthClient(context);
202
238
  try {
@@ -233,7 +269,13 @@ export const oauthForgetPassword = async ({ email }, context) => {
233
269
  }
234
270
  };
235
271
  export const updatePasswordByHash = async (passwordHash, context) => {
236
- assertSession(context);
272
+ if (!hasSession(context)) {
273
+ return new ErrorResponse(
274
+ HttpStatusCode.BAD_REQUEST,
275
+ HttpStatusMessage.BAD_REQUEST,
276
+ "No Session found"
277
+ );
278
+ }
237
279
  const shopId = context.shopId;
238
280
  const client = getOAuthClient(context);
239
281
  try {
@@ -7,6 +7,8 @@ exports.refreshUser = exports.getUser = exports.getAccessToken = exports.fetchUs
7
7
  var _types = require("../../types/index.cjs");
8
8
  var _customer = require("../../api/customer.cjs");
9
9
  var _oauth = require("../../api/oauth.cjs");
10
+ var _constants = require("../../constants/index.cjs");
11
+ var _errors = require("../../errors/index.cjs");
10
12
  const getUser = exports.getUser = function getUser2(context) {
11
13
  const {
12
14
  user,
@@ -42,7 +44,9 @@ const fetchUser = exports.fetchUser = async function fetchUser2({
42
44
  return user;
43
45
  };
44
46
  const refreshUser = exports.refreshUser = async function refreshUser2(context) {
45
- (0, _types.assertSession)(context);
47
+ if (!(0, _types.hasSession)(context)) {
48
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusMessage.BAD_REQUEST, "No Session found");
49
+ }
46
50
  const {
47
51
  accessToken
48
52
  } = context;
@@ -69,17 +73,15 @@ const refreshUser = exports.refreshUser = async function refreshUser2(context) {
69
73
  const getAccessToken = exports.getAccessToken = async function getAccessToken2({
70
74
  forceTokenRefresh = false
71
75
  }, context) {
72
- (0, _types.assertSession)(context);
76
+ if (!(0, _types.hasSession)(context)) {
77
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusMessage.BAD_REQUEST, "No Session found");
78
+ }
73
79
  if (!context.accessToken) {
74
- return new Response("No access token present", {
75
- status: 401
76
- });
80
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusMessage.UNAUTHORIZED, "No access token present");
77
81
  }
78
82
  if (forceTokenRefresh) {
79
83
  if (!context.refreshToken) {
80
- return new Response("No refresh token present", {
81
- status: 401
82
- });
84
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.UNAUTHORIZED, _constants.HttpStatusMessage.UNAUTHORIZED, "No refresh token present");
83
85
  }
84
86
  const client = (0, _oauth.getOAuthClient)(context);
85
87
  const tokens = await client.refreshToken({
@@ -1,4 +1,5 @@
1
1
  import type { RpcContext, ShopUser } from '../../types';
2
+ import { ErrorResponse } from '../../errors';
2
3
  declare const getUser: (context: RpcContext) => {
3
4
  user: ShopUser | undefined;
4
5
  };
@@ -11,12 +12,12 @@ declare const fetchUser: ({ accessToken }: {
11
12
  * @param context
12
13
  * @returns ShopUser
13
14
  */
14
- declare const refreshUser: (context: RpcContext) => Promise<{
15
+ declare const refreshUser: (context: RpcContext) => Promise<ErrorResponse | {
15
16
  user: undefined;
16
17
  } | {
17
18
  user: ShopUser;
18
19
  }>;
19
20
  declare const getAccessToken: ({ forceTokenRefresh }: {
20
21
  forceTokenRefresh?: boolean;
21
- }, context: RpcContext) => Promise<string | Response>;
22
+ }, context: RpcContext) => Promise<string | ErrorResponse>;
22
23
  export { getUser, fetchUser, refreshUser, getAccessToken };
@@ -1,6 +1,8 @@
1
- import { assertSession } from "../../types/index.mjs";
1
+ import { hasSession } from "../../types/index.mjs";
2
2
  import { CustomerAPIClient } from "../../api/customer.mjs";
3
3
  import { getOAuthClient } from "../../api/oauth.mjs";
4
+ import { HttpStatusCode, HttpStatusMessage } from "../../constants/index.mjs";
5
+ import { ErrorResponse } from "../../errors/index.mjs";
4
6
  const getUser = function getUser2(context) {
5
7
  const { user, accessToken, shopId } = context;
6
8
  if (user) {
@@ -28,7 +30,13 @@ const fetchUser = async function fetchUser2({ accessToken }, context) {
28
30
  return user;
29
31
  };
30
32
  const refreshUser = async function refreshUser2(context) {
31
- assertSession(context);
33
+ if (!hasSession(context)) {
34
+ return new ErrorResponse(
35
+ HttpStatusCode.BAD_REQUEST,
36
+ HttpStatusMessage.BAD_REQUEST,
37
+ "No Session found"
38
+ );
39
+ }
32
40
  const { accessToken } = context;
33
41
  if (!accessToken) {
34
42
  return { user: void 0 };
@@ -43,13 +51,27 @@ const refreshUser = async function refreshUser2(context) {
43
51
  }
44
52
  };
45
53
  const getAccessToken = async function getAccessToken2({ forceTokenRefresh = false }, context) {
46
- assertSession(context);
54
+ if (!hasSession(context)) {
55
+ return new ErrorResponse(
56
+ HttpStatusCode.BAD_REQUEST,
57
+ HttpStatusMessage.BAD_REQUEST,
58
+ "No Session found"
59
+ );
60
+ }
47
61
  if (!context.accessToken) {
48
- return new Response("No access token present", { status: 401 });
62
+ return new ErrorResponse(
63
+ HttpStatusCode.UNAUTHORIZED,
64
+ HttpStatusMessage.UNAUTHORIZED,
65
+ "No access token present"
66
+ );
49
67
  }
50
68
  if (forceTokenRefresh) {
51
69
  if (!context.refreshToken) {
52
- return new Response("No refresh token present", { status: 401 });
70
+ return new ErrorResponse(
71
+ HttpStatusCode.UNAUTHORIZED,
72
+ HttpStatusMessage.UNAUTHORIZED,
73
+ "No refresh token present"
74
+ );
53
75
  }
54
76
  const client = getOAuthClient(context);
55
77
  const tokens = await client.refreshToken({
@@ -12,7 +12,9 @@ function getWithParams(params, context) {
12
12
  return params.with ?? context.withParams?.wishlist ?? _constants.MIN_WITH_PARAMS_WISHLIST;
13
13
  }
14
14
  const getWishlist = exports.getWishlist = async function getWishlist2(options, context) {
15
- (0, _types.assertSession)(context);
15
+ if (!(0, _types.hasSession)(context)) {
16
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusMessage.BAD_REQUEST, "No Session found");
17
+ }
16
18
  const {
17
19
  bapiClient,
18
20
  campaignKey,
@@ -28,7 +30,9 @@ const getWishlist = exports.getWishlist = async function getWishlist2(options, c
28
30
  });
29
31
  };
30
32
  const addItemToWishlist = exports.addItemToWishlist = async function addItemToWishlist2(options, context) {
31
- (0, _types.assertSession)(context);
33
+ if (!(0, _types.hasSession)(context)) {
34
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusMessage.BAD_REQUEST, "No Session found");
35
+ }
32
36
  const {
33
37
  bapiClient,
34
38
  campaignKey,
@@ -70,7 +74,9 @@ const addItemToWishlist = exports.addItemToWishlist = async function addItemToWi
70
74
  }
71
75
  };
72
76
  const removeItemFromWishlist = exports.removeItemFromWishlist = async function removeItemFromWishlist2(options, context) {
73
- (0, _types.assertSession)(context);
77
+ if (!(0, _types.hasSession)(context)) {
78
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusMessage.BAD_REQUEST, "No Session found");
79
+ }
74
80
  const {
75
81
  bapiClient,
76
82
  campaignKey,
@@ -1,6 +1,7 @@
1
1
  import type { Wishlist } from '@scayle/storefront-api';
2
2
  import type { RpcContext, WishlistWithOptions } from '../../types';
3
- export declare const getWishlist: (options: WishlistWithOptions | undefined, context: RpcContext) => Promise<Wishlist>;
3
+ import { ErrorResponse } from '../../errors';
4
+ export declare const getWishlist: (options: WishlistWithOptions | undefined, context: RpcContext) => Promise<ErrorResponse | Wishlist>;
4
5
  export declare const addItemToWishlist: (options: {
5
6
  variantId?: number;
6
7
  productId?: number;
@@ -9,5 +10,5 @@ export declare const addItemToWishlist: (options: {
9
10
  export declare const removeItemFromWishlist: (options: {
10
11
  itemKey: string;
11
12
  with?: WishlistWithOptions;
12
- }, context: RpcContext) => Promise<Wishlist>;
13
- export declare const clearWishlist: (context: RpcContext) => Promise<Wishlist>;
13
+ }, context: RpcContext) => Promise<ErrorResponse | Wishlist>;
14
+ export declare const clearWishlist: (context: RpcContext) => Promise<ErrorResponse | Wishlist>;
@@ -1,4 +1,4 @@
1
- import { assertSession } from "../../types/index.mjs";
1
+ import { hasSession } from "../../types/index.mjs";
2
2
  import {
3
3
  HttpStatusCode,
4
4
  HttpStatusMessage,
@@ -10,7 +10,13 @@ function getWithParams(params, context) {
10
10
  return params.with ?? context.withParams?.wishlist ?? MIN_WITH_PARAMS_WISHLIST;
11
11
  }
12
12
  export const getWishlist = async function getWishlist2(options, context) {
13
- assertSession(context);
13
+ if (!hasSession(context)) {
14
+ return new ErrorResponse(
15
+ HttpStatusCode.BAD_REQUEST,
16
+ HttpStatusMessage.BAD_REQUEST,
17
+ "No Session found"
18
+ );
19
+ }
14
20
  const { bapiClient, campaignKey, wishlistKey } = context;
15
21
  const resolvedWith = getWithParams({ with: options }, context);
16
22
  return await bapiClient.wishlist.get(wishlistKey, {
@@ -20,7 +26,13 @@ export const getWishlist = async function getWishlist2(options, context) {
20
26
  });
21
27
  };
22
28
  export const addItemToWishlist = async function addItemToWishlist2(options, context) {
23
- assertSession(context);
29
+ if (!hasSession(context)) {
30
+ return new ErrorResponse(
31
+ HttpStatusCode.BAD_REQUEST,
32
+ HttpStatusMessage.BAD_REQUEST,
33
+ "No Session found"
34
+ );
35
+ }
24
36
  const { bapiClient, campaignKey, wishlistKey } = context;
25
37
  const { productId, variantId } = options;
26
38
  const resolvedWith = getWithParams(options, context);
@@ -50,7 +62,13 @@ export const addItemToWishlist = async function addItemToWishlist2(options, cont
50
62
  }
51
63
  };
52
64
  export const removeItemFromWishlist = async function removeItemFromWishlist2(options, context) {
53
- assertSession(context);
65
+ if (!hasSession(context)) {
66
+ return new ErrorResponse(
67
+ HttpStatusCode.BAD_REQUEST,
68
+ HttpStatusMessage.BAD_REQUEST,
69
+ "No Session found"
70
+ );
71
+ }
54
72
  const { bapiClient, campaignKey, wishlistKey } = context;
55
73
  const resolvedWith = getWithParams(options, context);
56
74
  return await bapiClient.wishlist.deleteItem(wishlistKey, options.itemKey, {
@@ -4,8 +4,12 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.assertSession = assertSession;
7
+ exports.hasSession = hasSession;
7
8
  function assertSession(context) {
8
9
  if (!context.sessionId) {
9
10
  throw new Error("No session for RpcContext available");
10
11
  }
12
+ }
13
+ function hasSession(context) {
14
+ return !!context.sessionId;
11
15
  }
@@ -89,6 +89,9 @@ export type RpcContext = {
89
89
  destroySessionsForUserId: (userId: number, sessionsToKeep?: string[]) => Promise<void>;
90
90
  generateBasketKeyForUserId: (userId: string) => Promise<string>;
91
91
  generateWishlistKeyForUserId: (userId: string) => Promise<string>;
92
+ /**
93
+ * @deprecated storeCampaignKeyword will be removed in a future release.
94
+ */
92
95
  storeCampaignKeyword?: string;
93
96
  routerBasePath?: string;
94
97
  ip?: string | undefined;
@@ -110,4 +113,5 @@ export type RpcContext = {
110
113
  } & AdditionalRpcContext & (ContextWithoutSession | ContextWithSession);
111
114
  export type RpcContextWithSession = RpcContext & ContextWithSession;
112
115
  export declare function assertSession(context: RpcContext): asserts context is RpcContextWithSession;
116
+ export declare function hasSession(context: RpcContext): context is RpcContextWithSession;
113
117
  export {};
@@ -3,3 +3,6 @@ export function assertSession(context) {
3
3
  throw new Error("No session for RpcContext available");
4
4
  }
5
5
  }
6
+ export function hasSession(context) {
7
+ return !!context.sessionId;
8
+ }
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.decompress = exports.compress = void 0;
7
7
  var _util = require("util");
8
8
  var _zlib = require("zlib");
9
+ var _nodeBuffer = require("node:buffer");
9
10
  const asyncDeflate = (0, _util.promisify)(_zlib.gzip);
10
11
  const asyncUnzip = (0, _util.promisify)(_zlib.gunzip);
11
12
  const compress = async data => {
@@ -15,9 +16,9 @@ const compress = async data => {
15
16
  exports.compress = compress;
16
17
  const decompress = async data => {
17
18
  try {
18
- const buffer = Buffer.from(data, "base64");
19
+ const buffer = _nodeBuffer.Buffer.from(data, "base64");
19
20
  return (await asyncUnzip(buffer)).toString();
20
- } catch (e) {
21
+ } catch {
21
22
  return data;
22
23
  }
23
24
  };
@@ -1,5 +1,6 @@
1
1
  import { promisify } from "util";
2
2
  import { gunzip, gzip } from "zlib";
3
+ import { Buffer } from "node:buffer";
3
4
  const asyncDeflate = promisify(gzip);
4
5
  const asyncUnzip = promisify(gunzip);
5
6
  export const compress = async (data) => {
@@ -10,7 +11,7 @@ export const decompress = async (data) => {
10
11
  try {
11
12
  const buffer = Buffer.from(data, "base64");
12
13
  return (await asyncUnzip(buffer)).toString();
13
- } catch (e) {
14
+ } catch {
14
15
  return data;
15
16
  }
16
17
  };
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Unwrap a possible response into its body
3
3
  * @param res
4
- * @returns
4
+ * @returns unwrapped response
5
5
  */
6
6
  export declare function unwrap<T>(res: Promise<Response | T> | Response | T): Promise<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scayle/storefront-core",
3
- "version": "7.61.0",
3
+ "version": "7.63.0",
4
4
  "description": "Collection of essential utilities to work with the Storefront API",
5
5
  "author": "SCAYLE Commerce Engine",
6
6
  "license": "MIT",
@@ -70,18 +70,18 @@
70
70
  "utility-types": "^3.11.0"
71
71
  },
72
72
  "devDependencies": {
73
- "@scayle/eslint-config-storefront": "4.2.0",
73
+ "@scayle/eslint-config-storefront": "4.3.0",
74
74
  "@types/crypto-js": "4.2.2",
75
- "@types/node": "20.14.11",
75
+ "@types/node": "20.14.12",
76
76
  "@types/webpack-env": "1.18.5",
77
77
  "@vitest/coverage-v8": "2.0.4",
78
78
  "dprint": "0.47.2",
79
- "eslint": "9.7.0",
79
+ "eslint": "9.8.0",
80
80
  "eslint-formatter-gitlab": "5.1.0",
81
81
  "publint": "0.2.9",
82
82
  "rimraf": "6.0.1",
83
83
  "ts-node": "10.9.2",
84
- "typescript": "5.5.3",
84
+ "typescript": "5.5.4",
85
85
  "unbuild": "2.0.0",
86
86
  "unstorage": "1.10.2",
87
87
  "vitest": "2.0.4"