@scayle/storefront-core 7.27.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.
- package/CHANGELOG.md +6 -0
- package/dist/api/oauth.cjs +136 -0
- package/dist/api/oauth.d.ts +57 -0
- package/dist/api/oauth.mjs +125 -0
- package/dist/rpc/methods/cbd.cjs +2 -6
- package/dist/rpc/methods/cbd.mjs +2 -3
- package/dist/rpc/methods/checkout/order.cjs +3 -5
- package/dist/rpc/methods/checkout/order.mjs +3 -4
- package/dist/rpc/methods/checkout/shopUser.cjs +29 -26
- package/dist/rpc/methods/checkout/shopUser.mjs +18 -23
- package/dist/rpc/methods/checkout/shopUserAddresses.cjs +7 -5
- package/dist/rpc/methods/checkout/shopUserAddresses.mjs +7 -4
- package/dist/rpc/methods/session.cjs +52 -99
- package/dist/rpc/methods/session.d.ts +9 -4
- package/dist/rpc/methods/session.mjs +62 -139
- package/dist/rpc/methods/user.cjs +20 -14
- package/dist/rpc/methods/user.mjs +20 -13
- package/dist/types/api/auth.d.ts +9 -4
- package/dist/utils/fetch.cjs +19 -0
- package/dist/utils/fetch.d.ts +9 -0
- package/dist/utils/fetch.mjs +12 -0
- package/dist/utils/index.cjs +11 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.mjs +1 -0
- package/package.json +3 -8
- package/dist/utils/rpc.cjs +0 -41
- package/dist/utils/rpc.d.ts +0 -6
- package/dist/utils/rpc.mjs +0 -35
package/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.OAuthClient = void 0;
|
|
7
|
+
var _jose = require("jose");
|
|
8
|
+
var _fetch = require("../utils/fetch.cjs");
|
|
9
|
+
var _hash = require("../utils/hash.cjs");
|
|
10
|
+
class MissingCredentialsError extends Error {
|
|
11
|
+
constructor() {
|
|
12
|
+
super("[OAuth API] No credentials configured");
|
|
13
|
+
this.name = "MissingCredentialsError";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
async function oauthResponseHandler(response) {
|
|
17
|
+
const data = await response.json();
|
|
18
|
+
if (!response.ok) {
|
|
19
|
+
throw new _fetch.FetchError(response, data);
|
|
20
|
+
}
|
|
21
|
+
return data;
|
|
22
|
+
}
|
|
23
|
+
class OAuthClient {
|
|
24
|
+
headers;
|
|
25
|
+
baseURL;
|
|
26
|
+
constructor(options) {
|
|
27
|
+
const {
|
|
28
|
+
clientId,
|
|
29
|
+
clientSecret,
|
|
30
|
+
apiHost
|
|
31
|
+
} = options;
|
|
32
|
+
if (!clientId || !clientSecret) {
|
|
33
|
+
throw new MissingCredentialsError();
|
|
34
|
+
}
|
|
35
|
+
this.baseURL = `${apiHost}/v1`;
|
|
36
|
+
const basicAuthHash = (0, _hash.encodeBase64)(`${clientId}:${clientSecret}`);
|
|
37
|
+
this.headers = {
|
|
38
|
+
Authorization: `Basic ${basicAuthHash}`,
|
|
39
|
+
Accept: "application/json",
|
|
40
|
+
"Content-Type": "application/json"
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Register a user and retrieve a token set
|
|
45
|
+
* @param payload
|
|
46
|
+
*/
|
|
47
|
+
async register(payload) {
|
|
48
|
+
return await fetch(`${this.baseURL}/auth/register`, {
|
|
49
|
+
method: "POST",
|
|
50
|
+
headers: this.headers,
|
|
51
|
+
body: JSON.stringify(payload)
|
|
52
|
+
}).then(oauthResponseHandler);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Execute a user login on the OAuth API and receive a token set
|
|
56
|
+
* @param payload
|
|
57
|
+
*/
|
|
58
|
+
async login(payload) {
|
|
59
|
+
return await fetch(`${this.baseURL}/auth/login`, {
|
|
60
|
+
method: "POST",
|
|
61
|
+
headers: this.headers,
|
|
62
|
+
body: JSON.stringify(payload)
|
|
63
|
+
}).then(oauthResponseHandler);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Execute a guest user login on the OAuth API and receive a token set
|
|
67
|
+
* @param payload
|
|
68
|
+
*/
|
|
69
|
+
async guestLogin(payload) {
|
|
70
|
+
return await fetch(`${this.baseURL}/auth/login/guest`, {
|
|
71
|
+
method: "POST",
|
|
72
|
+
headers: this.headers,
|
|
73
|
+
body: JSON.stringify(payload)
|
|
74
|
+
}).then(oauthResponseHandler);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Send a password reset email
|
|
78
|
+
* @param payload
|
|
79
|
+
*/
|
|
80
|
+
async sendPasswordResetEmail(payload) {
|
|
81
|
+
await fetch(`${this.baseURL}/auth/password/send-reset-email`, {
|
|
82
|
+
method: "POST",
|
|
83
|
+
headers: this.headers,
|
|
84
|
+
body: JSON.stringify(payload)
|
|
85
|
+
}).then(oauthResponseHandler);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Update password by hash
|
|
89
|
+
* @param payload
|
|
90
|
+
*/
|
|
91
|
+
async updatePasswordByHash(payload) {
|
|
92
|
+
return await fetch(`${this.baseURL}/auth/password/update-by-hash`, {
|
|
93
|
+
method: "PUT",
|
|
94
|
+
headers: this.headers,
|
|
95
|
+
body: JSON.stringify(payload)
|
|
96
|
+
}).then(oauthResponseHandler);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Generate a new access token via a refresh token
|
|
100
|
+
* @param payload
|
|
101
|
+
*/
|
|
102
|
+
async refreshToken(payload) {
|
|
103
|
+
return await fetch(`${this.baseURL}/oauth/token`, {
|
|
104
|
+
method: "POST",
|
|
105
|
+
headers: this.headers,
|
|
106
|
+
body: JSON.stringify(payload)
|
|
107
|
+
}).then(oauthResponseHandler);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Validate an access token
|
|
111
|
+
* @param accessToken
|
|
112
|
+
*/
|
|
113
|
+
async validateToken(accessToken) {
|
|
114
|
+
await fetch(`${this.baseURL}/oauth/token/validate`, {
|
|
115
|
+
headers: {
|
|
116
|
+
...headers,
|
|
117
|
+
Authorization: `Bearer ${accessToken}`
|
|
118
|
+
}
|
|
119
|
+
}).then(oauthResponseHandler);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Revoke an access token
|
|
123
|
+
* @param accessToken
|
|
124
|
+
*/
|
|
125
|
+
async revokeToken(accessToken) {
|
|
126
|
+
const decodedAccessToken = (0, _jose.decodeJwt)(accessToken);
|
|
127
|
+
await fetch(`${this.baseURL}/oauth/tokens/${decodedAccessToken.jti}`, {
|
|
128
|
+
method: "DELETE",
|
|
129
|
+
headers: {
|
|
130
|
+
...headers,
|
|
131
|
+
Authorization: `Bearer ${accessToken}`
|
|
132
|
+
}
|
|
133
|
+
}).then(oauthResponseHandler);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
exports.OAuthClient = OAuthClient;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { GuestRequest, LoginRequest, Oauth, RefreshTokenRequest, RegisterRequest, SendResetPasswordEmailRequest, UpdatePasswordByHashRequest } from '../types/api/auth';
|
|
2
|
+
export interface OAuthOptions {
|
|
3
|
+
clientId: string;
|
|
4
|
+
clientSecret: string;
|
|
5
|
+
apiHost: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* A client for interacting with the Checkout OAuth API
|
|
9
|
+
* Docs for all the routes: https://gitlab.com/aboutyou/checkout/schemas/checkout-auth-api/-/blob/main/build/openapi.yaml
|
|
10
|
+
*/
|
|
11
|
+
export declare class OAuthClient {
|
|
12
|
+
headers: HeadersInit;
|
|
13
|
+
baseURL: string;
|
|
14
|
+
constructor(options: OAuthOptions);
|
|
15
|
+
/**
|
|
16
|
+
* Register a user and retrieve a token set
|
|
17
|
+
* @param payload
|
|
18
|
+
*/
|
|
19
|
+
register(payload: RegisterRequest): Oauth;
|
|
20
|
+
/**
|
|
21
|
+
* Execute a user login on the OAuth API and receive a token set
|
|
22
|
+
* @param payload
|
|
23
|
+
*/
|
|
24
|
+
login(payload: LoginRequest & {
|
|
25
|
+
shopId: string;
|
|
26
|
+
}): Oauth;
|
|
27
|
+
/**
|
|
28
|
+
* Execute a guest user login on the OAuth API and receive a token set
|
|
29
|
+
* @param payload
|
|
30
|
+
*/
|
|
31
|
+
guestLogin(payload: GuestRequest): Oauth;
|
|
32
|
+
/**
|
|
33
|
+
* Send a password reset email
|
|
34
|
+
* @param payload
|
|
35
|
+
*/
|
|
36
|
+
sendPasswordResetEmail(payload: SendResetPasswordEmailRequest): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Update password by hash
|
|
39
|
+
* @param payload
|
|
40
|
+
*/
|
|
41
|
+
updatePasswordByHash(payload: UpdatePasswordByHashRequest): Oauth;
|
|
42
|
+
/**
|
|
43
|
+
* Generate a new access token via a refresh token
|
|
44
|
+
* @param payload
|
|
45
|
+
*/
|
|
46
|
+
refreshToken(payload: RefreshTokenRequest): Promise<Oauth>;
|
|
47
|
+
/**
|
|
48
|
+
* Validate an access token
|
|
49
|
+
* @param accessToken
|
|
50
|
+
*/
|
|
51
|
+
validateToken(accessToken: string): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Revoke an access token
|
|
54
|
+
* @param accessToken
|
|
55
|
+
*/
|
|
56
|
+
revokeToken(accessToken: string): Promise<void>;
|
|
57
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { decodeJwt } from "jose";
|
|
2
|
+
import { FetchError } from "../utils/fetch.mjs";
|
|
3
|
+
import { encodeBase64 } from "../utils/hash.mjs";
|
|
4
|
+
class MissingCredentialsError extends Error {
|
|
5
|
+
constructor() {
|
|
6
|
+
super("[OAuth API] No credentials configured");
|
|
7
|
+
this.name = "MissingCredentialsError";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
async function oauthResponseHandler(response) {
|
|
11
|
+
const data = await response.json();
|
|
12
|
+
if (!response.ok) {
|
|
13
|
+
throw new FetchError(response, data);
|
|
14
|
+
}
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
17
|
+
export class OAuthClient {
|
|
18
|
+
headers;
|
|
19
|
+
baseURL;
|
|
20
|
+
constructor(options) {
|
|
21
|
+
const { clientId, clientSecret, apiHost } = options;
|
|
22
|
+
if (!clientId || !clientSecret) {
|
|
23
|
+
throw new MissingCredentialsError();
|
|
24
|
+
}
|
|
25
|
+
this.baseURL = `${apiHost}/v1`;
|
|
26
|
+
const basicAuthHash = encodeBase64(`${clientId}:${clientSecret}`);
|
|
27
|
+
this.headers = {
|
|
28
|
+
Authorization: `Basic ${basicAuthHash}`,
|
|
29
|
+
Accept: "application/json",
|
|
30
|
+
"Content-Type": "application/json"
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Register a user and retrieve a token set
|
|
35
|
+
* @param payload
|
|
36
|
+
*/
|
|
37
|
+
async register(payload) {
|
|
38
|
+
return await fetch(`${this.baseURL}/auth/register`, {
|
|
39
|
+
method: "POST",
|
|
40
|
+
headers: this.headers,
|
|
41
|
+
body: JSON.stringify(payload)
|
|
42
|
+
}).then(oauthResponseHandler);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Execute a user login on the OAuth API and receive a token set
|
|
46
|
+
* @param payload
|
|
47
|
+
*/
|
|
48
|
+
async login(payload) {
|
|
49
|
+
return await fetch(`${this.baseURL}/auth/login`, {
|
|
50
|
+
method: "POST",
|
|
51
|
+
headers: this.headers,
|
|
52
|
+
body: JSON.stringify(payload)
|
|
53
|
+
}).then(oauthResponseHandler);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Execute a guest user login on the OAuth API and receive a token set
|
|
57
|
+
* @param payload
|
|
58
|
+
*/
|
|
59
|
+
async guestLogin(payload) {
|
|
60
|
+
return await fetch(`${this.baseURL}/auth/login/guest`, {
|
|
61
|
+
method: "POST",
|
|
62
|
+
headers: this.headers,
|
|
63
|
+
body: JSON.stringify(payload)
|
|
64
|
+
}).then(oauthResponseHandler);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Send a password reset email
|
|
68
|
+
* @param payload
|
|
69
|
+
*/
|
|
70
|
+
async sendPasswordResetEmail(payload) {
|
|
71
|
+
await fetch(`${this.baseURL}/auth/password/send-reset-email`, {
|
|
72
|
+
method: "POST",
|
|
73
|
+
headers: this.headers,
|
|
74
|
+
body: JSON.stringify(payload)
|
|
75
|
+
}).then(oauthResponseHandler);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Update password by hash
|
|
79
|
+
* @param payload
|
|
80
|
+
*/
|
|
81
|
+
async updatePasswordByHash(payload) {
|
|
82
|
+
return await fetch(`${this.baseURL}/auth/password/update-by-hash`, {
|
|
83
|
+
method: "PUT",
|
|
84
|
+
headers: this.headers,
|
|
85
|
+
body: JSON.stringify(payload)
|
|
86
|
+
}).then(oauthResponseHandler);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Generate a new access token via a refresh token
|
|
90
|
+
* @param payload
|
|
91
|
+
*/
|
|
92
|
+
async refreshToken(payload) {
|
|
93
|
+
return await fetch(`${this.baseURL}/oauth/token`, {
|
|
94
|
+
method: "POST",
|
|
95
|
+
headers: this.headers,
|
|
96
|
+
body: JSON.stringify(payload)
|
|
97
|
+
}).then(oauthResponseHandler);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Validate an access token
|
|
101
|
+
* @param accessToken
|
|
102
|
+
*/
|
|
103
|
+
async validateToken(accessToken) {
|
|
104
|
+
await fetch(`${this.baseURL}/oauth/token/validate`, {
|
|
105
|
+
headers: {
|
|
106
|
+
...headers,
|
|
107
|
+
Authorization: `Bearer ${accessToken}`
|
|
108
|
+
}
|
|
109
|
+
}).then(oauthResponseHandler);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Revoke an access token
|
|
113
|
+
* @param accessToken
|
|
114
|
+
*/
|
|
115
|
+
async revokeToken(accessToken) {
|
|
116
|
+
const decodedAccessToken = decodeJwt(accessToken);
|
|
117
|
+
await fetch(`${this.baseURL}/oauth/tokens/${decodedAccessToken.jti}`, {
|
|
118
|
+
method: "DELETE",
|
|
119
|
+
headers: {
|
|
120
|
+
...headers,
|
|
121
|
+
Authorization: `Bearer ${accessToken}`
|
|
122
|
+
}
|
|
123
|
+
}).then(oauthResponseHandler);
|
|
124
|
+
}
|
|
125
|
+
}
|
package/dist/rpc/methods/cbd.cjs
CHANGED
|
@@ -4,9 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.getOrderDataByCbd = void 0;
|
|
7
|
-
var _axios = _interopRequireDefault(require("axios"));
|
|
8
7
|
var _hash = require("../../utils/hash.cjs");
|
|
9
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
8
|
const getOrderDataByCbd = exports.getOrderDataByCbd = async function getOrderDataByCbd2({
|
|
11
9
|
cbdToken
|
|
12
10
|
}, context) {
|
|
@@ -27,9 +25,7 @@ const getOrderDataByCbd = exports.getOrderDataByCbd = async function getOrderDat
|
|
|
27
25
|
return;
|
|
28
26
|
}
|
|
29
27
|
const accessToken = (0, _hash.encodeBase64)(`${checkoutUsername}:${unescape(checkoutToken)}`);
|
|
30
|
-
const {
|
|
31
|
-
data: orderSuccessData
|
|
32
|
-
} = await _axios.default.get(`${checkoutUrl}/api/v1/orders/${payload.order_id}`, {
|
|
28
|
+
const response = await fetch(`${checkoutUrl}/api/v1/orders/${payload.order_id}`, {
|
|
33
29
|
headers: {
|
|
34
30
|
Accept: "application/json",
|
|
35
31
|
Authorization: `Basic ${accessToken}`,
|
|
@@ -39,6 +35,6 @@ const getOrderDataByCbd = exports.getOrderDataByCbd = async function getOrderDat
|
|
|
39
35
|
} : {})
|
|
40
36
|
}
|
|
41
37
|
});
|
|
42
|
-
return
|
|
38
|
+
return await response.json();
|
|
43
39
|
}
|
|
44
40
|
};
|
package/dist/rpc/methods/cbd.mjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import axios from "axios";
|
|
2
1
|
import { encodeBase64, verifyToken } from "../../utils/hash.mjs";
|
|
3
2
|
export const getOrderDataByCbd = async function getOrderDataByCbd2({ cbdToken }, context) {
|
|
4
3
|
if (!cbdToken) {
|
|
@@ -20,7 +19,7 @@ export const getOrderDataByCbd = async function getOrderDataByCbd2({ cbdToken },
|
|
|
20
19
|
const accessToken = encodeBase64(
|
|
21
20
|
`${checkoutUsername}:${unescape(checkoutToken)}`
|
|
22
21
|
);
|
|
23
|
-
const
|
|
22
|
+
const response = await fetch(
|
|
24
23
|
`${checkoutUrl}/api/v1/orders/${payload.order_id}`,
|
|
25
24
|
{
|
|
26
25
|
headers: {
|
|
@@ -31,6 +30,6 @@ export const getOrderDataByCbd = async function getOrderDataByCbd2({ cbdToken },
|
|
|
31
30
|
}
|
|
32
31
|
}
|
|
33
32
|
);
|
|
34
|
-
return
|
|
33
|
+
return await response.json();
|
|
35
34
|
}
|
|
36
35
|
};
|
|
@@ -4,8 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.getOrderById = void 0;
|
|
7
|
-
var _axios = _interopRequireDefault(require("axios"));
|
|
8
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
9
7
|
const getOrderById = exports.getOrderById = async function getOrderById2({
|
|
10
8
|
orderId
|
|
11
9
|
}, context) {
|
|
@@ -28,10 +26,10 @@ const getOrderById = exports.getOrderById = async function getOrderById2({
|
|
|
28
26
|
}
|
|
29
27
|
const orderUrl = `${checkoutUrl}/api/customer/order/${orderId}`;
|
|
30
28
|
try {
|
|
31
|
-
const response = await
|
|
29
|
+
const response = await fetch(orderUrl, {
|
|
32
30
|
headers: {
|
|
33
31
|
Authorization: `Bearer ${accessToken}`,
|
|
34
|
-
"X-Shop-Id": shopId,
|
|
32
|
+
"X-Shop-Id": shopId.toString(),
|
|
35
33
|
Accept: "application/json",
|
|
36
34
|
...(accessHeader ? {
|
|
37
35
|
"X-Access-Header": accessHeader
|
|
@@ -42,7 +40,7 @@ const getOrderById = exports.getOrderById = async function getOrderById2({
|
|
|
42
40
|
throw new Error("Order not found");
|
|
43
41
|
}
|
|
44
42
|
if (response.status === 200) {
|
|
45
|
-
return response.
|
|
43
|
+
return await response.json();
|
|
46
44
|
}
|
|
47
45
|
throw new Error(`Unknown response status: ${response.status}`);
|
|
48
46
|
} catch (error) {
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import axios from "axios";
|
|
2
1
|
export const getOrderById = async function getOrderById2({ orderId }, context) {
|
|
3
2
|
const accessToken = context.accessToken;
|
|
4
3
|
const shopId = context.shopId;
|
|
@@ -21,10 +20,10 @@ export const getOrderById = async function getOrderById2({ orderId }, context) {
|
|
|
21
20
|
}
|
|
22
21
|
const orderUrl = `${checkoutUrl}/api/customer/order/${orderId}`;
|
|
23
22
|
try {
|
|
24
|
-
const response = await
|
|
23
|
+
const response = await fetch(orderUrl, {
|
|
25
24
|
headers: {
|
|
26
25
|
Authorization: `Bearer ${accessToken}`,
|
|
27
|
-
"X-Shop-Id": shopId,
|
|
26
|
+
"X-Shop-Id": shopId.toString(),
|
|
28
27
|
Accept: "application/json",
|
|
29
28
|
...accessHeader ? { "X-Access-Header": accessHeader } : {}
|
|
30
29
|
}
|
|
@@ -33,7 +32,7 @@ export const getOrderById = async function getOrderById2({ orderId }, context) {
|
|
|
33
32
|
throw new Error("Order not found");
|
|
34
33
|
}
|
|
35
34
|
if (response.status === 200) {
|
|
36
|
-
return response.
|
|
35
|
+
return await response.json();
|
|
37
36
|
}
|
|
38
37
|
throw new Error(`Unknown response status: ${response.status}`);
|
|
39
38
|
} catch (error) {
|
|
@@ -4,8 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.updateShopUser = exports.updatePassword = void 0;
|
|
7
|
-
var _axios = _interopRequireDefault(require("axios"));
|
|
8
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
9
7
|
const updateShopUser = exports.updateShopUser = async function updateShopUser2(payload, context) {
|
|
10
8
|
const shopId = context.shopId;
|
|
11
9
|
const accessHeader = context.checkout.accessHeader;
|
|
@@ -32,35 +30,39 @@ const updateShopUser = exports.updateShopUser = async function updateShopUser2(p
|
|
|
32
30
|
const personalUrl = `${checkoutUrl}/api/customer/personal`;
|
|
33
31
|
const headers = {
|
|
34
32
|
Authorization: `Bearer ${context.accessToken}`,
|
|
35
|
-
"X-Shop-Id": shopId,
|
|
33
|
+
"X-Shop-Id": shopId.toString(),
|
|
36
34
|
"Content-Type": "application/json",
|
|
37
35
|
...(accessHeader ? {
|
|
38
36
|
"X-Access-Header": accessHeader
|
|
39
37
|
} : {})
|
|
40
38
|
};
|
|
41
39
|
try {
|
|
42
|
-
const contactResponse = await
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
const contactResponse = await fetch(contactUrl, {
|
|
41
|
+
method: "PATCH",
|
|
42
|
+
headers,
|
|
43
|
+
body: JSON.stringify({
|
|
44
|
+
email: user?.email,
|
|
45
|
+
phone: user?.phone
|
|
46
|
+
})
|
|
47
47
|
});
|
|
48
48
|
if (contactResponse.status === 404) {
|
|
49
49
|
throw new Error("Failed to update user's contact information");
|
|
50
50
|
}
|
|
51
51
|
if (contactResponse.status === 200) {
|
|
52
|
-
const personalResponse = await
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
52
|
+
const personalResponse = await fetch(personalUrl, {
|
|
53
|
+
method: "PATCH",
|
|
54
|
+
headers,
|
|
55
|
+
body: JSON.stringify({
|
|
56
|
+
firstName: user?.firstName,
|
|
57
|
+
lastName: user?.lastName,
|
|
58
|
+
birthDate: user?.birthDate,
|
|
59
|
+
...(user?.gender && {
|
|
60
|
+
gender: user?.gender
|
|
61
|
+
}),
|
|
62
|
+
...(user?.title && {
|
|
63
|
+
title: user?.title
|
|
64
|
+
})
|
|
61
65
|
})
|
|
62
|
-
}, {
|
|
63
|
-
headers
|
|
64
66
|
});
|
|
65
67
|
if (personalResponse.status === 404) {
|
|
66
68
|
throw new Error("Failed to update user's personal information");
|
|
@@ -86,25 +88,26 @@ const updatePassword = exports.updatePassword = async function updatePassword2({
|
|
|
86
88
|
}, context) {
|
|
87
89
|
const shopUser = context.user;
|
|
88
90
|
try {
|
|
89
|
-
const apiResponse = await
|
|
90
|
-
password: oldPassword,
|
|
91
|
-
newPassword
|
|
92
|
-
}, {
|
|
91
|
+
const apiResponse = await fetch(`${context.checkout.url}/api/oauth/customer/password`, {
|
|
93
92
|
headers: {
|
|
94
93
|
Authorization: `Bearer ${context.accessToken}`,
|
|
95
|
-
"X-Shop-Id": context.shopId,
|
|
94
|
+
"X-Shop-Id": context.shopId.toString(),
|
|
96
95
|
"Content-Type": "application/json",
|
|
97
96
|
...(context.checkout.accessHeader ? {
|
|
98
97
|
"X-Access-Header": context.checkout.accessHeader
|
|
99
98
|
} : {})
|
|
100
|
-
}
|
|
99
|
+
},
|
|
100
|
+
body: JSON.stringify({
|
|
101
|
+
password: oldPassword,
|
|
102
|
+
newPassword
|
|
103
|
+
})
|
|
101
104
|
});
|
|
102
105
|
if (apiResponse.status === 200) {
|
|
103
106
|
if (shopUser?.id) {
|
|
104
107
|
await context.destroySessionsForUserId(shopUser.id, [context.sessionId]);
|
|
105
108
|
}
|
|
106
109
|
return {
|
|
107
|
-
user: apiResponse.
|
|
110
|
+
user: await apiResponse.json()
|
|
108
111
|
};
|
|
109
112
|
} else if (apiResponse.status === 401) {
|
|
110
113
|
throw new Error("401 - Failed to update user's password - Unauthorized request");
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import axios from "axios";
|
|
2
1
|
export const updateShopUser = async function updateShopUser2(payload, context) {
|
|
3
2
|
const shopId = context.shopId;
|
|
4
3
|
const accessHeader = context.checkout.accessHeader;
|
|
@@ -25,38 +24,34 @@ export const updateShopUser = async function updateShopUser2(payload, context) {
|
|
|
25
24
|
const personalUrl = `${checkoutUrl}/api/customer/personal`;
|
|
26
25
|
const headers = {
|
|
27
26
|
Authorization: `Bearer ${context.accessToken}`,
|
|
28
|
-
"X-Shop-Id": shopId,
|
|
27
|
+
"X-Shop-Id": shopId.toString(),
|
|
29
28
|
"Content-Type": "application/json",
|
|
30
29
|
...accessHeader ? { "X-Access-Header": accessHeader } : {}
|
|
31
30
|
};
|
|
32
31
|
try {
|
|
33
|
-
const contactResponse = await
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
const contactResponse = await fetch(contactUrl, {
|
|
33
|
+
method: "PATCH",
|
|
34
|
+
headers,
|
|
35
|
+
body: JSON.stringify({
|
|
36
36
|
email: user?.email,
|
|
37
37
|
phone: user?.phone
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
headers
|
|
41
|
-
}
|
|
42
|
-
);
|
|
38
|
+
})
|
|
39
|
+
});
|
|
43
40
|
if (contactResponse.status === 404) {
|
|
44
41
|
throw new Error("Failed to update user's contact information");
|
|
45
42
|
}
|
|
46
43
|
if (contactResponse.status === 200) {
|
|
47
|
-
const personalResponse = await
|
|
48
|
-
|
|
49
|
-
|
|
44
|
+
const personalResponse = await fetch(personalUrl, {
|
|
45
|
+
method: "PATCH",
|
|
46
|
+
headers,
|
|
47
|
+
body: JSON.stringify({
|
|
50
48
|
firstName: user?.firstName,
|
|
51
49
|
lastName: user?.lastName,
|
|
52
50
|
birthDate: user?.birthDate,
|
|
53
51
|
...user?.gender && { gender: user?.gender },
|
|
54
52
|
...user?.title && { title: user?.title }
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
headers
|
|
58
|
-
}
|
|
59
|
-
);
|
|
53
|
+
})
|
|
54
|
+
});
|
|
60
55
|
if (personalResponse.status === 404) {
|
|
61
56
|
throw new Error("Failed to update user's personal information");
|
|
62
57
|
}
|
|
@@ -74,23 +69,23 @@ export const updateShopUser = async function updateShopUser2(payload, context) {
|
|
|
74
69
|
export const updatePassword = async function updatePassword2({ oldPassword, newPassword }, context) {
|
|
75
70
|
const shopUser = context.user;
|
|
76
71
|
try {
|
|
77
|
-
const apiResponse = await
|
|
72
|
+
const apiResponse = await fetch(
|
|
78
73
|
`${context.checkout.url}/api/oauth/customer/password`,
|
|
79
|
-
{ password: oldPassword, newPassword },
|
|
80
74
|
{
|
|
81
75
|
headers: {
|
|
82
76
|
Authorization: `Bearer ${context.accessToken}`,
|
|
83
|
-
"X-Shop-Id": context.shopId,
|
|
77
|
+
"X-Shop-Id": context.shopId.toString(),
|
|
84
78
|
"Content-Type": "application/json",
|
|
85
79
|
...context.checkout.accessHeader ? { "X-Access-Header": context.checkout.accessHeader } : {}
|
|
86
|
-
}
|
|
80
|
+
},
|
|
81
|
+
body: JSON.stringify({ password: oldPassword, newPassword })
|
|
87
82
|
}
|
|
88
83
|
);
|
|
89
84
|
if (apiResponse.status === 200) {
|
|
90
85
|
if (shopUser?.id) {
|
|
91
86
|
await context.destroySessionsForUserId(shopUser.id, [context.sessionId]);
|
|
92
87
|
}
|
|
93
|
-
return { user: apiResponse.
|
|
88
|
+
return { user: await apiResponse.json() };
|
|
94
89
|
} else if (apiResponse.status === 401) {
|
|
95
90
|
throw new Error(
|
|
96
91
|
"401 - Failed to update user's password - Unauthorized request"
|
|
@@ -4,21 +4,23 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.getShopUserAddresses = void 0;
|
|
7
|
-
var
|
|
8
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
7
|
+
var _fetch = require("../../../utils/fetch.cjs");
|
|
9
8
|
const getShopUserAddresses = exports.getShopUserAddresses = async function getShopUserAddresses2(context) {
|
|
10
9
|
const shopId = context.shopId;
|
|
11
10
|
const accessHeader = context.checkout.accessHeader;
|
|
12
11
|
const checkoutUrl = context.checkout.url;
|
|
13
|
-
const response = await
|
|
12
|
+
const response = await fetch(`${checkoutUrl}/api/oauth/customer/addresses`, {
|
|
14
13
|
headers: {
|
|
15
14
|
Authorization: `Bearer ${context.accessToken}`,
|
|
16
|
-
"X-Shop-Id": shopId,
|
|
15
|
+
"X-Shop-Id": shopId.toString(),
|
|
17
16
|
Accept: "application/json",
|
|
18
17
|
...(accessHeader && {
|
|
19
18
|
"X-Access-Header": accessHeader
|
|
20
19
|
})
|
|
21
20
|
}
|
|
22
21
|
});
|
|
23
|
-
|
|
22
|
+
if (!response.ok) {
|
|
23
|
+
throw new _fetch.FetchError(response);
|
|
24
|
+
}
|
|
25
|
+
return (await response.json()).entities;
|
|
24
26
|
};
|
|
@@ -1,19 +1,22 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { FetchError } from "../../../utils/fetch.mjs";
|
|
2
2
|
const getShopUserAddresses = async function getShopUserAddresses2(context) {
|
|
3
3
|
const shopId = context.shopId;
|
|
4
4
|
const accessHeader = context.checkout.accessHeader;
|
|
5
5
|
const checkoutUrl = context.checkout.url;
|
|
6
|
-
const response = await
|
|
6
|
+
const response = await fetch(
|
|
7
7
|
`${checkoutUrl}/api/oauth/customer/addresses`,
|
|
8
8
|
{
|
|
9
9
|
headers: {
|
|
10
10
|
Authorization: `Bearer ${context.accessToken}`,
|
|
11
|
-
"X-Shop-Id": shopId,
|
|
11
|
+
"X-Shop-Id": shopId.toString(),
|
|
12
12
|
Accept: "application/json",
|
|
13
13
|
...accessHeader && { "X-Access-Header": accessHeader }
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
);
|
|
17
|
-
|
|
17
|
+
if (!response.ok) {
|
|
18
|
+
throw new FetchError(response);
|
|
19
|
+
}
|
|
20
|
+
return (await response.json()).entities;
|
|
18
21
|
};
|
|
19
22
|
export { getShopUserAddresses };
|