@foxtware/mineral 0.1.30 → 0.1.32

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.
@@ -0,0 +1,78 @@
1
+ const { json2csv } = require('json-2-csv');
2
+ const { credsValidator } = require('../validators');
3
+ const { ensureArray, everyIfArray, ArgsWarden } = require('../utils');
4
+ const { peoplevoxClient } = require('../peoplevox/peoplevox.utils');
5
+ const { MAX_REQUEST_ITEMS } = require('../peoplevox/peoplevox.constants');
6
+
7
+ const itemPayloadValidator = (itemPayload) => itemPayload?.ItemCode;
8
+
9
+ const argsWarden = new ArgsWarden([
10
+ ['credsPayload', credsValidator],
11
+ ['itemPayload', (item) => everyIfArray(itemPayloadValidator, item)],
12
+ ]);
13
+
14
+ const peoplevoxItemEdit = async (
15
+ credsPayload,
16
+ itemPayload,
17
+ {
18
+ fetchClient = peoplevoxClient,
19
+ } = {},
20
+ ) => {
21
+
22
+ const rejectResponse = await argsWarden.responseIfRejectingArgs({
23
+ credsPayload,
24
+ itemPayload,
25
+ });
26
+ if (rejectResponse) {
27
+ return rejectResponse;
28
+ }
29
+
30
+ const csvData = await json2csv(ensureArray(itemPayload));
31
+
32
+ if (csvData.length > MAX_REQUEST_ITEMS) {
33
+ return {
34
+ ok: false,
35
+ error: {
36
+ code: 'MAX_REQUEST_ITEMS_EXCEEDED',
37
+ message: `Max request items exceeded. Max is ${ MAX_REQUEST_ITEMS }.`,
38
+ },
39
+ };
40
+ }
41
+
42
+ return fetchClient.fetch({
43
+ requestPayload: {
44
+ method: 'post',
45
+ body: {
46
+ saveRequest: {
47
+ TemplateName: 'Item types',
48
+ CsvData: csvData,
49
+ },
50
+ },
51
+ },
52
+ context: {
53
+ credsPayload,
54
+ action: 'SaveData',
55
+ },
56
+ });
57
+ };
58
+
59
+ const funcApiConfig = {
60
+ argsWarden,
61
+ };
62
+
63
+ module.exports = {
64
+ peoplevoxItemEdit,
65
+ funcApiConfig,
66
+ };
67
+
68
+ /*
69
+ curl -X POST "http://localhost:8000/peoplevoxItemEdit" \
70
+ -H "Content-Type: application/json" \
71
+ -d '{
72
+ "credsPayload": { "credsPath": "peoplevox" },
73
+ "itemPayload": {
74
+ "ItemCode": "100335-CHC-L",
75
+ "Attribute7": "ATTR7"
76
+ }
77
+ }'
78
+ */
@@ -1,5 +1,5 @@
1
1
  const { credsValidator } = require('../validators');
2
- const { ArgsWarden } = require('../utils');
2
+ const { ArgsWarden, getWithLocalCachedFile } = require('../utils');
3
3
  const { peoplevoxClient } = require('../peoplevox/peoplevox.utils');
4
4
 
5
5
  const argsWarden = new ArgsWarden([
@@ -17,6 +17,7 @@ const peoplevoxReportGet = async (
17
17
  orderBy,
18
18
  columns,
19
19
  fetchClient = peoplevoxClient,
20
+ useLocalCachedFile,
20
21
  } = {},
21
22
  ) => {
22
23
 
@@ -28,27 +29,28 @@ const peoplevoxReportGet = async (
28
29
  return rejectResponse;
29
30
  }
30
31
 
31
- const reportGetResponse = await fetchClient.fetch({
32
- requestPayload: {
33
- method: 'post',
34
- body: {
35
- getReportRequest: {
36
- TemplateName: reportName,
37
- ...(searchClause ? { SearchClause: searchClause } : {}),
38
- ...(perPage ? { ItemsPerPage: perPage } : {}),
39
- ...(filter ? { FilterClause: filter } : {}),
40
- ...(orderBy ? { OrderBy: orderBy } : {}),
41
- ...(columns ? { Columns: columns.join(',') } : {}),
32
+ return getWithLocalCachedFile(
33
+ useLocalCachedFile,
34
+ () => fetchClient.fetch({
35
+ requestPayload: {
36
+ method: 'post',
37
+ body: {
38
+ getReportRequest: {
39
+ TemplateName: reportName,
40
+ ...(searchClause ? { SearchClause: searchClause } : {}),
41
+ ...(perPage ? { ItemsPerPage: perPage } : {}),
42
+ ...(filter ? { FilterClause: filter } : {}),
43
+ ...(orderBy ? { OrderBy: orderBy } : {}),
44
+ ...(columns ? { Columns: columns.join(',') } : {}),
45
+ },
42
46
  },
43
47
  },
44
- },
45
- context: {
46
- credsPayload,
47
- action: 'GetReportData',
48
- },
49
- });
50
-
51
- return reportGetResponse;
48
+ context: {
49
+ credsPayload,
50
+ action: 'GetReportData',
51
+ },
52
+ }),
53
+ );
52
54
  };
53
55
 
54
56
  const funcApiConfig = {
@@ -1,4 +1,4 @@
1
- const { Getter, capitaliseString, ArgsWarden } = require('../utils');
1
+ const { Getter, capitaliseString, ArgsWarden, getWithLocalCachedFile } = require('../utils');
2
2
  const { credsValidator } = require('../validators');
3
3
  const { shopifyClient } = require('./shopify.utils');
4
4
  const { MAX_PER_PAGE } = require('./shopify.constants');
@@ -221,6 +221,7 @@ const shopifyGet = async (
221
221
  resources = `${ resource }s`, // for when plural of the resource isn't `${ resource }s`
222
222
  argumentTypeOverrides = {}, // for when a commonly-named API option is not just a single type, e.g. type being String vs CatalogType
223
223
 
224
+ useLocalCachedFile,
224
225
  ...getterOptions // e.g. limit
225
226
  } = {},
226
227
  ) => {
@@ -276,12 +277,17 @@ const shopifyGet = async (
276
277
  return getter;
277
278
  }
278
279
 
279
- const data = await getter.run({ returnAll: true });
280
+ return getWithLocalCachedFile(
281
+ useLocalCachedFile,
282
+ async () => {
283
+ const data = await getter.run({ returnAll: true });
280
284
 
281
- return {
282
- ok: true,
283
- data,
284
- };
285
+ return {
286
+ ok: true,
287
+ data,
288
+ };
289
+ },
290
+ );
285
291
  };
286
292
 
287
293
  const funcApiConfig = {
@@ -1,4 +1,4 @@
1
- const { credsFromPayload, ArgsWarden, Getter } = require('../utils');
1
+ const { credsFromPayload, ArgsWarden, Getter, getWithLocalCachedFile } = require('../utils');
2
2
  const { credsValidator } = require('../validators');
3
3
  const { starshipitClient } = require('../starshipit/starshipit.utils');
4
4
  const { MAX_PER_PAGE } = require('../starshipit/starshipit.constants');
@@ -74,6 +74,7 @@ const starshipitGet = async (
74
74
  params,
75
75
  perPage = MAX_PER_PAGE,
76
76
  nodeName = 'results',
77
+ useLocalCachedFile,
77
78
  ...getterOptions
78
79
  } = {},
79
80
  ) => {
@@ -114,12 +115,17 @@ const starshipitGet = async (
114
115
  return getter;
115
116
  }
116
117
 
117
- const data = await getter.run({ returnAll: true });
118
+ return getWithLocalCachedFile(
119
+ useLocalCachedFile,
120
+ async () => {
121
+ const data = await getter.run({ returnAll: true });
118
122
 
119
- return {
120
- ok: true,
121
- data,
122
- };
123
+ return {
124
+ ok: true,
125
+ data,
126
+ };
127
+ },
128
+ );
123
129
  };
124
130
 
125
131
  const funcApiConfig = {
@@ -1,4 +1,4 @@
1
- const { ArgsWarden, Getter, valueProvided } = require('../utils');
1
+ const { ArgsWarden, Getter, getWithLocalCachedFile, valueProvided } = require('../utils');
2
2
  const { credsValidator } = require('../validators');
3
3
  const { stylearcadeClient } = require('../stylearcade/stylearcade.utils');
4
4
  const { MAX_PER_PAGE } = require('../stylearcade/stylearcade.constants');
@@ -74,6 +74,7 @@ const stylearcadeGet = async (
74
74
  perPage = MAX_PER_PAGE,
75
75
  nodeName = 'records',
76
76
  url = '/export',
77
+ useLocalCachedFile,
77
78
  ...getterOptions
78
79
  } = {},
79
80
  ) => {
@@ -106,12 +107,17 @@ const stylearcadeGet = async (
106
107
  return getter;
107
108
  }
108
109
 
109
- const data = await getter.run({ returnAll: true });
110
+ return getWithLocalCachedFile(
111
+ useLocalCachedFile,
112
+ async () => {
113
+ const data = await getter.run({ returnAll: true });
110
114
 
111
- return {
112
- ok: true,
113
- data,
114
- };
115
+ return {
116
+ ok: true,
117
+ data,
118
+ };
119
+ },
120
+ );
115
121
  };
116
122
 
117
123
  const funcApiConfig = {
package/api/utils.js CHANGED
@@ -32,6 +32,33 @@ const normalise = (value) => (value || '').toString().trim().toLowerCase();
32
32
 
33
33
  const valueProvided = (value) => value !== undefined && value !== null;
34
34
 
35
+ const getWithLocalCachedFile = async (
36
+ filename,
37
+ getFunction,
38
+ ) => {
39
+ if (!filename || HOSTED) {
40
+ return getFunction();
41
+ }
42
+
43
+ const directory = `${ getWorkspace() }/api/_temp`;
44
+ const filepath = `${ directory }/${ filename }`;
45
+
46
+ try {
47
+ const cachedFile = await fs.readFile(filepath, 'utf8');
48
+ return JSON.parse(cachedFile);
49
+ } catch (error) {
50
+ if (error.code !== 'ENOENT') {
51
+ throw error;
52
+ }
53
+ }
54
+
55
+ const response = await getFunction();
56
+ await fs.mkdir(directory, { recursive: true });
57
+ await fs.writeFile(filepath, JSON.stringify(response, null, 2), 'utf8');
58
+
59
+ return response;
60
+ };
61
+
35
62
  const credsByPath = (credsPath, credsObject) => {
36
63
  const pathNodes = pathAsArray(credsPath);
37
64
 
@@ -1448,6 +1475,10 @@ const objectPropsTranslate = (
1448
1475
  return translatedObj;
1449
1476
  };
1450
1477
 
1478
+ const randomArrayItem = (array) => {
1479
+ return array[Math.floor(Math.random() * array.length)];
1480
+ };
1481
+
1451
1482
  module.exports = {
1452
1483
  wait,
1453
1484
  timeMs,
@@ -1456,6 +1487,7 @@ module.exports = {
1456
1487
  capitaliseString,
1457
1488
  normalise,
1458
1489
  credsFromPayload,
1490
+ getWithLocalCachedFile,
1459
1491
  resolveFromCreds,
1460
1492
  customFetch,
1461
1493
  logDeep,
@@ -1491,4 +1523,5 @@ module.exports = {
1491
1523
  surveyObject,
1492
1524
  diffObjects,
1493
1525
  objectPropsTranslate,
1526
+ randomArrayItem,
1494
1527
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foxtware/mineral",
3
- "version": "0.1.30",
3
+ "version": "0.1.32",
4
4
  "bin": {
5
5
  "mineral": "bin/mineral.js"
6
6
  },