This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 337
/
Copy pathutil.js
executable file
·117 lines (107 loc) · 3.48 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import config from 'config';
import crypto from 'crypto';
const algorithm = 'aes-256-ctr';
/**
* Get current store code from parameter passed from the vue storefront frotnend app
* @param {Express.Request} req
*/
export function getCurrentStoreCode (req) {
if (req.headers['x-vs-store-code']) {
return req.headers['x-vs-store-code']
}
if (req.query.storeCode) {
return req.query.storeCode
}
return null
}
/**
* Get the config.storeViews[storeCode]
* @param {string} storeCode
*/
export function getCurrentStoreView (storeCode = null) {
let storeView = { // current, default store
tax: config.tax,
i18n: config.i18n,
elasticsearch: config.elasticsearch,
storeCode: null,
storeId: config.defaultStoreCode && config.defaultStoreCode !== '' ? config.storeViews[config.defaultStoreCode].storeId : 1
}
if (storeCode && config.storeViews[storeCode]) {
storeView = config.storeViews[storeCode]
}
return storeView // main config is used as default storeview
}
/** Creates a callback that proxies node callback style arguments to an Express Response object.
* @param {express.Response} res Express HTTP Response
* @param {number} [status=200] Status code to send on success
*
* @example
* list(req, res) {
* collection.find({}, toRes(res));
* }
*/
export function toRes (res, status = 200) {
return (err, thing) => {
if (err) return res.status(500).send(err);
if (thing && typeof thing.toObject === 'function') {
thing = thing.toObject();
}
res.status(status).json(thing);
};
}
export function sgnSrc (sgnObj, item) {
if (config.tax.alwaysSyncPlatformPricesOver) {
sgnObj.id = item.id
} else {
sgnObj.sku = item.sku
}
// console.log(sgnObj)
return sgnObj
}
/** Creates a api status call and sends it thru to Express Response object.
* @param {express.Response} res Express HTTP Response
* @param {number} [code=200] Status code to send on success
* @param {json} [result='OK'] Text message or result information object
*/
export function apiStatus (res, result = 'OK', code = 200, meta = null) {
let apiResult = { code: code, result: result };
if (meta !== null) {
apiResult.meta = meta;
}
res.status(code).json(apiResult);
return result;
}
/**
* Creates an error for API status of Express Response object.
*
* @param {express.Response} res Express HTTP Response
* @param {object|string} error Error object or error message
* @return {json} [result='OK'] Text message or result information object
*/
export function apiError (res, error) {
let errorCode = error.code || error.status;
let errorMessage = error.errorMessage || error;
if (error instanceof Error) {
// Class 'Error' is not serializable with JSON.stringify, extract data explicitly.
errorCode = error.code || errorCode;
errorMessage = error.message;
}
return apiStatus(res, errorMessage, Number(errorCode) || 500);
}
export function encryptToken (textToken, secret) {
const cipher = crypto.createCipher(algorithm, secret)
let crypted = cipher.update(textToken, 'utf8', 'hex')
crypted += cipher.final('hex');
return crypted;
}
export function decryptToken (textToken, secret) {
const decipher = crypto.createDecipher(algorithm, secret)
let dec = decipher.update(textToken, 'hex', 'utf8')
dec += decipher.final('utf8');
return dec;
}
export function getToken (req) {
return config.users.tokenInHeader
? (req.headers.authorization || '').replace('Bearer ', '')
: req.query.token
}