@foxtware/mineral 0.1.27 → 0.1.28

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 (119) hide show
  1. package/.creds.yml.sample +17 -0
  2. package/AGENTS.md +17 -0
  3. package/api/fastsimon/.gitkeep +0 -0
  4. package/api/fastsimon/docs.md +14 -0
  5. package/api/fastsimon/fastsimon.constants.js +18 -0
  6. package/api/fastsimon/fastsimon.utils.js +246 -0
  7. package/api/fastsimon/fastsimonSearch.js +84 -0
  8. package/api/figma/docs.md +3 -3
  9. package/api/figma/figma.constants.js +1 -1
  10. package/api/figma/figma.utils.js +13 -2
  11. package/api/figma/figmaActivityLogsGet.js +44 -0
  12. package/api/figma/figmaAiUsageDailyGet.js +44 -0
  13. package/api/figma/figmaComponentGet.js +48 -0
  14. package/api/figma/figmaComponentSetGet.js +48 -0
  15. package/api/figma/figmaDevResourcesCreate.js +47 -0
  16. package/api/figma/figmaDevResourcesUpdate.js +47 -0
  17. package/api/figma/figmaDeveloperLogsCreate.js +47 -0
  18. package/api/figma/figmaDiscoveryGet.js +44 -0
  19. package/api/figma/figmaFileCommentCreate.js +50 -0
  20. package/api/figma/figmaFileCommentDelete.js +51 -0
  21. package/api/figma/figmaFileCommentReactionCreate.js +53 -0
  22. package/api/figma/figmaFileCommentReactionDelete.js +51 -0
  23. package/api/figma/figmaFileCommentReactionsGet.js +51 -0
  24. package/api/figma/figmaFileCommentsGet.js +48 -0
  25. package/api/figma/figmaFileComponentSetsGet.js +48 -0
  26. package/api/figma/figmaFileComponentsGet.js +48 -0
  27. package/api/figma/figmaFileDevResourceDelete.js +51 -0
  28. package/api/figma/figmaFileDevResourcesGet.js +48 -0
  29. package/api/figma/figmaFileGet.js +48 -0
  30. package/api/figma/figmaFileImageFillsGet.js +48 -0
  31. package/api/figma/figmaFileImagesGet.js +48 -0
  32. package/api/figma/figmaFileMetaGet.js +48 -0
  33. package/api/figma/figmaFileNodesGet.js +48 -0
  34. package/api/figma/figmaFileStylesGet.js +48 -0
  35. package/api/figma/figmaFileVersionsGet.js +48 -0
  36. package/api/figma/figmaFolderFilesGet.js +48 -0
  37. package/api/figma/figmaFolderFoldersGet.js +48 -0
  38. package/api/figma/figmaFolderMetaGet.js +48 -0
  39. package/api/figma/figmaGet.js +15 -63
  40. package/api/figma/figmaMeGet.js +53 -0
  41. package/api/figma/figmaOembedGet.js +44 -0
  42. package/api/figma/figmaPaymentsGet.js +44 -0
  43. package/api/figma/figmaProjectFilesGet.js +48 -0
  44. package/api/figma/figmaProjectMetaGet.js +48 -0
  45. package/api/figma/figmaStyleGet.js +48 -0
  46. package/api/figma/figmaTeamComponentSetsGet.js +48 -0
  47. package/api/figma/figmaTeamComponentsGet.js +48 -0
  48. package/api/figma/figmaTeamFoldersGet.js +48 -0
  49. package/api/figma/figmaTeamProjectsGet.js +48 -0
  50. package/api/figma/figmaTeamStylesGet.js +48 -0
  51. package/api/figma/figmaTeamWebhooksGet.js +47 -0
  52. package/api/figma/figmaVariablesLocalGet.js +48 -0
  53. package/api/figma/figmaVariablesPublishedGet.js +48 -0
  54. package/api/figma/figmaVariablesUpdate.js +50 -0
  55. package/api/figma/figmaWebhookCreate.js +47 -0
  56. package/api/figma/figmaWebhookDelete.js +48 -0
  57. package/api/figma/figmaWebhookGet.js +46 -0
  58. package/api/figma/figmaWebhookRequestsGet.js +47 -0
  59. package/api/figma/figmaWebhookUpdate.js +50 -0
  60. package/api/figma/figmaWebhooksGet.js +47 -0
  61. package/api/gorgias/gorgiasTicketCreate.js +1 -1
  62. package/api/gorgias/gorgiasTicketMessageCreate.js +1 -1
  63. package/api/gorgias/gorgiasTicketUpdate.js +1 -1
  64. package/api/linear/linearAttachmentCreate.js +1 -1
  65. package/api/linear/linearCommentCreate.js +1 -1
  66. package/api/linear/linearCommentUpdate.js +1 -1
  67. package/api/linear/linearIssueLabelCreate.js +1 -1
  68. package/api/linear/linearIssueUpdate.js +1 -1
  69. package/api/linear/linearProjectCreate.js +1 -1
  70. package/api/linear/linearProjectUpdate.js +1 -1
  71. package/api/linear/linearWebhookCreate.js +1 -1
  72. package/api/salesforce/salesforce.constants.js +13 -0
  73. package/api/salesforce/salesforce.utils.js +61 -0
  74. package/api/salesforce/salesforceAuthGet.js +115 -0
  75. package/api/salesforce/salesforceComposite.js +79 -0
  76. package/api/salesforce/salesforceCompositeBatch.js +73 -0
  77. package/api/salesforce/salesforceLimitsGet.js +55 -0
  78. package/api/salesforce/salesforceQuery.js +69 -0
  79. package/api/salesforce/salesforceQueryAll.js +62 -0
  80. package/api/salesforce/salesforceQueryMore.js +75 -0
  81. package/api/salesforce/salesforceRecentItemsGet.js +63 -0
  82. package/api/salesforce/salesforceResourcesGet.js +55 -0
  83. package/api/salesforce/salesforceSearch.js +62 -0
  84. package/api/salesforce/salesforceSobjectBasicInfoGet.js +81 -0
  85. package/api/salesforce/salesforceSobjectBlobGet.js +67 -0
  86. package/api/salesforce/salesforceSobjectCollectionCreate.js +78 -0
  87. package/api/salesforce/salesforceSobjectCollectionDelete.js +70 -0
  88. package/api/salesforce/salesforceSobjectCollectionGet.js +75 -0
  89. package/api/salesforce/salesforceSobjectCollectionUpdate.js +74 -0
  90. package/api/salesforce/salesforceSobjectCreate.js +66 -0
  91. package/api/salesforce/salesforceSobjectDelete.js +86 -0
  92. package/api/salesforce/salesforceSobjectDeleteByExternalId.js +91 -0
  93. package/api/salesforce/salesforceSobjectDeletedGet.js +71 -0
  94. package/api/salesforce/salesforceSobjectDescribe.js +81 -0
  95. package/api/salesforce/salesforceSobjectDescribeLayouts.js +64 -0
  96. package/api/salesforce/salesforceSobjectGet.js +93 -0
  97. package/api/salesforce/salesforceSobjectGetByExternalId.js +98 -0
  98. package/api/salesforce/salesforceSobjectTreeCreate.js +79 -0
  99. package/api/salesforce/salesforceSobjectUpdate.js +94 -0
  100. package/api/salesforce/salesforceSobjectUpdatedGet.js +71 -0
  101. package/api/salesforce/salesforceSobjectUpsert.js +74 -0
  102. package/api/salesforce/salesforceSobjectsDescribeGlobal.js +55 -0
  103. package/api/salesforce/salesforceVersionsGet.js +62 -0
  104. package/api/shopify/shopifyMetaobjectCreate.js +72 -0
  105. package/api/tableau/tableau.constants.js +8 -0
  106. package/api/tableau/tableau.utils.js +93 -0
  107. package/api/tableau/tableauGet.js +174 -0
  108. package/api/tableau/tableauUserAdd.js +16 -94
  109. package/api/tableau/tableauUsersGet.js +46 -134
  110. package/api/tagalys/docs.md +2 -2
  111. package/api/tagalys/tagalys.constants.js +17 -0
  112. package/api/tagalys/tagalys.utils.js +125 -0
  113. package/api/tagalys/tagalysAnalyticsEventSend.js +125 -0
  114. package/api/tagalys/tagalysCollectionGet.js +100 -0
  115. package/api/tagalys/tagalysPopularSearchesGet.js +60 -0
  116. package/api/tagalys/tagalysRecommendationGet.js +109 -0
  117. package/api/tagalys/tagalysSearch.js +15 -89
  118. package/api/tagalys/tagalysSearchSuggestionsGet.js +76 -0
  119. package/package.json +2 -1
@@ -0,0 +1,125 @@
1
+ // https://tagalys.notion.site/Storefront-API-v2-20eacdd38c2080d58d3fd0cf6f24e435
2
+
3
+ const { resolveCreds } = require('../pipelineSteps');
4
+ const {
5
+ FetchClient,
6
+ fetchClientCommonSteps,
7
+ } = require('../utils');
8
+
9
+ // Recursively flattens nested params into Rails/PHP-style bracket query
10
+ // params, e.g. { filter: { color: ['red'] } } -> filter[color][]=red
11
+ // and { filter: { price: { selected_min: 100 } } } -> filter[price][selected_min]=100
12
+ // This also covers the search_suggestions `sections[section_id][*]` shape,
13
+ // e.g. { sections: { products: { count: 6 } } } -> sections[products][count]=6
14
+ const appendParams = (searchParams, key, value) => {
15
+ if (value === undefined || value === null) {
16
+ return;
17
+ }
18
+ if (Array.isArray(value)) {
19
+ value.forEach((item) => appendParams(searchParams, `${ key }[]`, item));
20
+ } else if (typeof value === 'object') {
21
+ Object.entries(value).forEach(([subKey, subValue]) => {
22
+ appendParams(searchParams, `${ key }[${ subKey }]`, subValue);
23
+ });
24
+ } else {
25
+ searchParams.append(key, value);
26
+ }
27
+ };
28
+
29
+ // Request context (country, language, segmentTag) is supported by the
30
+ // collections, search and recommendations endpoints per the docs. Callers
31
+ // pass these via `context` (like `apiVersion` in the shopify client),
32
+ // rather than merging them into `query` themselves, so the shared client
33
+ // applies them consistently across every endpoint that uses it.
34
+ const useRequestContextParams = async (state) => {
35
+ const { requestPayload, context } = state;
36
+ const { country, language, segmentTag } = context;
37
+
38
+ if (!country && !language && !segmentTag) {
39
+ return {};
40
+ }
41
+
42
+ return {
43
+ requestPayload: {
44
+ ...requestPayload,
45
+ query: {
46
+ ...requestPayload.query,
47
+ ...(country ? { country } : {}),
48
+ ...(language ? { language } : {}),
49
+ ...(segmentTag ? { segment_tag: segmentTag } : {}),
50
+ },
51
+ },
52
+ };
53
+ };
54
+
55
+ // Base URL is region-specific per account (e.g. https://api-r1.tagalys.com),
56
+ // so per AGENTS.md it's resolved from creds rather than tagalys.constants.js.
57
+ const useUrlAndQuery = async (state) => {
58
+ const { requestPayload, context } = state;
59
+ const { creds } = context;
60
+
61
+ const {
62
+ BASE_URL,
63
+ STORE_HANDLE,
64
+ STOREFRONT_API_KEY,
65
+ } = creds;
66
+
67
+ const searchParams = new URLSearchParams();
68
+ searchParams.append('shop_id', `${ STORE_HANDLE }.myshopify.com`);
69
+ if (STOREFRONT_API_KEY) {
70
+ // NOTE: the published docs call this param `storefront_api_key`, but a
71
+ // live request with that name returned `api_key_invalid`. `api_key`
72
+ // is what actually authenticates -- trusting the working behaviour
73
+ // over the doc text here. Revisit if Tagalys ships the documented name.
74
+ searchParams.append('api_key', STOREFRONT_API_KEY);
75
+ }
76
+ Object.entries(requestPayload.query || {}).forEach(([key, value]) => {
77
+ appendParams(searchParams, key, value);
78
+ });
79
+
80
+ return {
81
+ requestPayload: {
82
+ ...requestPayload,
83
+ url: `${ BASE_URL }${ requestPayload.url }?${ searchParams.toString() }`,
84
+ headers: {
85
+ Accept: 'application/json',
86
+ ...requestPayload.headers,
87
+ },
88
+ },
89
+ };
90
+ };
91
+
92
+ // A configured redirect takes precedence over the rest of the response --
93
+ // surface it plainly rather than making callers dig for it. Only /v2/search
94
+ // and /v2/search_suggestions ever populate `redirect_url`, so this is a
95
+ // no-op for every other endpoint on the shared client.
96
+ const useRedirectResponse = async (state) => {
97
+ const { response } = state;
98
+
99
+ if (!response?.ok || !response?.data?.redirect_url) {
100
+ return {};
101
+ }
102
+
103
+ return {
104
+ response: {
105
+ redirected: true,
106
+ redirectUrl: response.data.redirect_url,
107
+ },
108
+ };
109
+ };
110
+
111
+ const tagalysClient = new FetchClient({
112
+ pipeline: [
113
+ resolveCreds,
114
+ useRequestContextParams,
115
+ useUrlAndQuery,
116
+ 'fetch',
117
+ fetchClientCommonSteps.exitEarlyOnNotOk,
118
+ useRedirectResponse,
119
+ ],
120
+ });
121
+
122
+ module.exports = {
123
+ tagalysClient,
124
+ appendParams,
125
+ };
@@ -0,0 +1,125 @@
1
+ // https://tagalys.notion.site/Storefront-API-v2-20eacdd38c2080d58d3fd0cf6f24e435
2
+
3
+ const { credsValidator } = require('../validators');
4
+ const { ArgsWarden, actionSingleOrMultiple, everyIfArray, valueProvided, logDeep } = require('../utils');
5
+ const { tagalysClient } = require('../tagalys/tagalys.utils');
6
+
7
+ // Supported `type` values and their `payload` shape:
8
+ // product_viewed { productId }
9
+ // product_added_to_cart { productId, quantity }
10
+ // checkout_completed { orderId, lineItems: [{ productId, quantity }] }
11
+ // search_results_viewed { query, productIds, page, totalCount, queryHandling?, sortedBy?, filteredBy? }
12
+ // collection_viewed { collectionId, productIds?, page?, totalCount?, sortedBy?, filteredBy? }
13
+ // recommendations_viewed { id, recommendedProductIds, sourceProductIds? (required when `id` is contextual to a product) }
14
+ const eventPayloadValidator = (eventPayload) => {
15
+ return valueProvided(eventPayload?.type)
16
+ && valueProvided(eventPayload?.payload)
17
+ && valueProvided(eventPayload?.context?.deviceId)
18
+ && valueProvided(eventPayload?.context?.visitId);
19
+ };
20
+
21
+ const argsWarden = new ArgsWarden([
22
+ ['credsPayload', credsValidator],
23
+ ['eventPayload', (eventPayload) => everyIfArray(eventPayloadValidator, eventPayload)],
24
+ ]);
25
+
26
+ const tagalysAnalyticsEventSendSingle = async (
27
+ credsPayload,
28
+ eventPayload,
29
+ ) => {
30
+ const { type, payload, context } = eventPayload;
31
+ const {
32
+ deviceId,
33
+ visitId,
34
+ userId,
35
+ country,
36
+ language,
37
+ segmentTag,
38
+ apiClientName,
39
+ apiClientVersion,
40
+ } = context;
41
+
42
+ const response = await tagalysClient.fetch({
43
+ context: {
44
+ credsPayload,
45
+ },
46
+ requestPayload: {
47
+ url: '/v2/analytics/events',
48
+ method: 'post',
49
+ body: {
50
+ type,
51
+ payload,
52
+ context: {
53
+ device_id: deviceId,
54
+ visit_id: visitId,
55
+ ...(userId ? { user_id: userId } : {}),
56
+ ...(country ? { country } : {}),
57
+ ...(language ? { language } : {}),
58
+ ...(segmentTag ? { segment_tag: segmentTag } : {}),
59
+ ...((apiClientName || apiClientVersion) ? {
60
+ api_client: {
61
+ ...(apiClientName ? { name: apiClientName } : {}),
62
+ ...(apiClientVersion ? { version: apiClientVersion } : {}),
63
+ },
64
+ } : {}),
65
+ },
66
+ },
67
+ },
68
+ });
69
+
70
+ const { ok, error } = response;
71
+ if (!ok) {
72
+ logDeep({ error });
73
+ }
74
+
75
+ return response;
76
+ };
77
+
78
+ const tagalysAnalyticsEventSend = async (
79
+ credsPayload,
80
+ eventPayload,
81
+ {
82
+ queueRunOptions,
83
+ } = {},
84
+ ) => {
85
+
86
+ const rejectResponse = await argsWarden.responseIfRejectingArgs({
87
+ credsPayload,
88
+ eventPayload,
89
+ });
90
+ if (rejectResponse) {
91
+ return rejectResponse;
92
+ }
93
+
94
+ return actionSingleOrMultiple(
95
+ eventPayload,
96
+ tagalysAnalyticsEventSendSingle,
97
+ (eventPayloadItem) => ({
98
+ args: [credsPayload, eventPayloadItem],
99
+ }),
100
+ {
101
+ ...(queueRunOptions ? { queueRunOptions } : {}),
102
+ },
103
+ );
104
+ };
105
+
106
+ const funcApiConfig = {
107
+ argsWarden,
108
+ };
109
+
110
+ module.exports = {
111
+ tagalysAnalyticsEventSend,
112
+ funcApiConfig,
113
+ };
114
+
115
+ /*
116
+ curl -X POST "http://localhost:8000/tagalysAnalyticsEventSend" \
117
+ -d '{
118
+ "credsPayload": { "credsPath": "tagalys.au" },
119
+ "eventPayload": {
120
+ "type": "product_viewed",
121
+ "payload": { "productId": "7286848028804" },
122
+ "context": { "deviceId": "d123", "visitId": "v123" }
123
+ }
124
+ }'
125
+ */
@@ -0,0 +1,100 @@
1
+ // https://tagalys.notion.site/Storefront-API-v2-20eacdd38c2080d58d3fd0cf6f24e435
2
+
3
+ const { credsValidator } = require('../validators');
4
+ const { ArgsWarden, actionSingleOrMultiple, logDeep } = require('../utils');
5
+ const { DEFAULT_INCLUDE } = require('../tagalys/tagalys.constants');
6
+ const { tagalysClient } = require('../tagalys/tagalys.utils');
7
+
8
+ const argsWarden = new ArgsWarden([
9
+ ['credsPayload', credsValidator],
10
+ ['collectionId'],
11
+ ]);
12
+
13
+ const tagalysCollectionGetSingle = async (
14
+ credsPayload,
15
+ collectionId,
16
+ {
17
+ include = DEFAULT_INCLUDE, // e.g. ['products', 'filters', 'sort_options', 'total_count']
18
+ filter, // e.g. { color: ['red'], price: { selected_min: 100, selected_max: 200 } }
19
+ scope, // e.g. { gender: ['female'] }
20
+ sort, // e.g. 'price-asc'
21
+ page,
22
+ perPage,
23
+ bannerPositions, // { source: 'dashboard' | 'custom' | 'none', fixed: [4, 6], repeating: { every: 16, at: [5] } }
24
+ country,
25
+ language,
26
+ segmentTag,
27
+ } = {},
28
+ ) => {
29
+ const response = await tagalysClient.fetch({
30
+ context: {
31
+ credsPayload,
32
+ country,
33
+ language,
34
+ segmentTag,
35
+ },
36
+ requestPayload: {
37
+ url: `/v2/collections/${ collectionId }`,
38
+ method: 'get',
39
+ query: {
40
+ include,
41
+ ...(filter ? { filter } : {}),
42
+ ...(scope ? { scope } : {}),
43
+ ...(sort ? { sort } : {}),
44
+ ...(page ? { page } : {}),
45
+ ...(perPage ? { per_page: perPage } : {}),
46
+ ...(bannerPositions ? { banner_positions: bannerPositions } : {}),
47
+ },
48
+ },
49
+ });
50
+
51
+ const { ok, error } = response;
52
+ if (!ok) {
53
+ logDeep({ error });
54
+ }
55
+
56
+ return response;
57
+ };
58
+
59
+ const tagalysCollectionGet = async (
60
+ credsPayload,
61
+ collectionId,
62
+ {
63
+ queueRunOptions,
64
+ ...options
65
+ } = {},
66
+ ) => {
67
+
68
+ const rejectResponse = await argsWarden.responseIfRejectingArgs({
69
+ credsPayload,
70
+ collectionId,
71
+ });
72
+ if (rejectResponse) {
73
+ return rejectResponse;
74
+ }
75
+
76
+ return actionSingleOrMultiple(
77
+ collectionId,
78
+ tagalysCollectionGetSingle,
79
+ (collectionIdItem) => ({
80
+ args: [credsPayload, collectionIdItem, options],
81
+ }),
82
+ {
83
+ ...(queueRunOptions ? { queueRunOptions } : {}),
84
+ },
85
+ );
86
+ };
87
+
88
+ const funcApiConfig = {
89
+ argsWarden,
90
+ };
91
+
92
+ module.exports = {
93
+ tagalysCollectionGet,
94
+ funcApiConfig,
95
+ };
96
+
97
+ /*
98
+ curl -X POST "http://localhost:8000/tagalysCollectionGet" \
99
+ -d '{ "credsPayload": { "credsPath": "tagalys.au" }, "collectionId": "123456789" }'
100
+ */
@@ -0,0 +1,60 @@
1
+ // https://tagalys.notion.site/Storefront-API-v2-20eacdd38c2080d58d3fd0cf6f24e435
2
+
3
+ const { credsValidator } = require('../validators');
4
+ const { ArgsWarden, logDeep } = require('../utils');
5
+ const { tagalysClient } = require('../tagalys/tagalys.utils');
6
+
7
+ const argsWarden = new ArgsWarden([
8
+ ['credsPayload', credsValidator],
9
+ ]);
10
+
11
+ // Returns at most 10 queries. When the store is configured for country-based
12
+ // availability, `country` filters the response (see the docs).
13
+ const tagalysPopularSearchesGet = async (
14
+ credsPayload,
15
+ {
16
+ country,
17
+ } = {},
18
+ ) => {
19
+ const rejectResponse = await argsWarden.responseIfRejectingArgs({
20
+ credsPayload,
21
+ });
22
+ if (rejectResponse) {
23
+ return rejectResponse;
24
+ }
25
+
26
+ const response = await tagalysClient.fetch({
27
+ context: {
28
+ credsPayload,
29
+ },
30
+ requestPayload: {
31
+ url: '/v2/popular_searches',
32
+ method: 'get',
33
+ query: {
34
+ ...(country ? { country } : {}),
35
+ },
36
+ },
37
+ });
38
+
39
+ const { ok, data, error } = response;
40
+ if (!ok) {
41
+ logDeep({ error });
42
+ return { ok: false, error };
43
+ }
44
+
45
+ return { ok: true, data };
46
+ };
47
+
48
+ const funcApiConfig = {
49
+ argsWarden,
50
+ };
51
+
52
+ module.exports = {
53
+ tagalysPopularSearchesGet,
54
+ funcApiConfig,
55
+ };
56
+
57
+ /*
58
+ curl -X POST "http://localhost:8000/tagalysPopularSearchesGet" \
59
+ -d '{ "credsPayload": { "credsPath": "tagalys.au" } }'
60
+ */
@@ -0,0 +1,109 @@
1
+ // https://tagalys.notion.site/Storefront-API-v2-20eacdd38c2080d58d3fd0cf6f24e435
2
+
3
+ const { credsValidator } = require('../validators');
4
+ const { ArgsWarden, actionSingleOrMultiple, ensureArray, logDeep } = require('../utils');
5
+ const { MAX_RECOMMENDATION_PRODUCT_IDS } = require('../tagalys/tagalys.constants');
6
+ const { tagalysClient } = require('../tagalys/tagalys.utils');
7
+
8
+ // System-defined ids: bought_also_bought, viewed_also_viewed, atc_also_atc,
9
+ // recently_viewed, personalized. Custom recommendations use a dashboard slug
10
+ // (e.g. 8d72jc02jd719ddc6hw0).
11
+ const argsWarden = new ArgsWarden([
12
+ ['credsPayload', credsValidator],
13
+ ['recommendationId'],
14
+ ]);
15
+
16
+ const tagalysRecommendationGetSingle = async (
17
+ credsPayload,
18
+ recommendationId,
19
+ {
20
+ productIds, // up to 3, for product-contextual recommendations
21
+ count,
22
+
23
+ // personalized / recently_viewed: either deviceId or userId is required
24
+ deviceId,
25
+ userId,
26
+
27
+ fallbackRecommendationId, // personalized only
28
+ visitId, // recently_viewed only
29
+
30
+ country,
31
+ language,
32
+ segmentTag,
33
+ } = {},
34
+ ) => {
35
+ const response = await tagalysClient.fetch({
36
+ context: {
37
+ credsPayload,
38
+ country,
39
+ language,
40
+ segmentTag,
41
+ },
42
+ requestPayload: {
43
+ url: `/v2/recommendations/${ recommendationId }`,
44
+ method: 'get',
45
+ query: {
46
+ ...(productIds ? { product_ids: ensureArray(productIds).slice(0, MAX_RECOMMENDATION_PRODUCT_IDS) } : {}),
47
+ ...(count ? { count } : {}),
48
+ ...(deviceId ? { device_id: deviceId } : {}),
49
+ ...(userId ? { user_id: userId } : {}),
50
+ ...(fallbackRecommendationId ? { fallback_recommendation_id: fallbackRecommendationId } : {}),
51
+ ...(visitId ? { visit_id: visitId } : {}),
52
+ },
53
+ },
54
+ });
55
+
56
+ const { ok, error } = response;
57
+ if (!ok) {
58
+ logDeep({ error });
59
+ }
60
+
61
+ return response;
62
+ };
63
+
64
+ const tagalysRecommendationGet = async (
65
+ credsPayload,
66
+ recommendationId,
67
+ {
68
+ queueRunOptions,
69
+ ...options
70
+ } = {},
71
+ ) => {
72
+
73
+ const rejectResponse = await argsWarden.responseIfRejectingArgs({
74
+ credsPayload,
75
+ recommendationId,
76
+ });
77
+ if (rejectResponse) {
78
+ return rejectResponse;
79
+ }
80
+
81
+ return actionSingleOrMultiple(
82
+ recommendationId,
83
+ tagalysRecommendationGetSingle,
84
+ (recommendationIdItem) => ({
85
+ args: [credsPayload, recommendationIdItem, options],
86
+ }),
87
+ {
88
+ ...(queueRunOptions ? { queueRunOptions } : {}),
89
+ },
90
+ );
91
+ };
92
+
93
+ const funcApiConfig = {
94
+ argsWarden,
95
+ };
96
+
97
+ module.exports = {
98
+ tagalysRecommendationGet,
99
+ funcApiConfig,
100
+ };
101
+
102
+ /*
103
+ curl -X POST "http://localhost:8000/tagalysRecommendationGet" \
104
+ -d '{
105
+ "credsPayload": { "credsPath": "tagalys.au" },
106
+ "recommendationId": "bought_also_bought",
107
+ "options": { "productIds": ["1234567890"], "count": 10 }
108
+ }'
109
+ */
@@ -2,84 +2,8 @@
2
2
 
3
3
  const { credsValidator } = require('../validators');
4
4
  const { ArgsWarden, logDeep } = require('../utils');
5
- const { resolveCreds } = require('../pipelineSteps');
6
- const {
7
- FetchClient,
8
- fetchClientCommonSteps,
9
- } = require('../utils');
10
-
11
- // NOTE: the Storefront API v2 docs authenticate with `shop_id` (the
12
- // Shopify myshopify.com domain) + an optional `storefront_api_key`, and
13
- // don't mention a client_code param on this endpoint at all -- that's a
14
- // holdover from Tagalys' older v1 API. Mapped below as best guess:
15
- // BASE_URL -> used directly (docs say it's region-specific,
16
- // e.g. https://api-r1.tagalys.com -- assumed already
17
- // resolved to the right region in the creds store)
18
- // STORE_HANDLE -> expanded to the `shop_id` myshopify domain
19
- // STOREFRONT_API_KEY -> sent as `api_key`
20
- // CLIENT_CODE -> not used by /v2/search per the docs; left unused below.
21
- // Flag if your account actually needs it sent somewhere.
22
- const DEFAULT_INCLUDE = ['products', 'total_count'];
23
-
24
- // Recursively flattens nested params into Rails/PHP-style bracket query
25
- // params, e.g. { filter: { color: ['red'] } } -> filter[color][]=red
26
- // and { filter: { price: { selected_min: 100 } } } -> filter[price][selected_min]=100
27
- const appendParams = (searchParams, key, value) => {
28
- if (value === undefined || value === null) {
29
- return;
30
- }
31
- if (Array.isArray(value)) {
32
- value.forEach((item) => appendParams(searchParams, `${ key }[]`, item));
33
- } else if (typeof value === 'object') {
34
- Object.entries(value).forEach(([subKey, subValue]) => {
35
- appendParams(searchParams, `${ key }[${ subKey }]`, subValue);
36
- });
37
- } else {
38
- searchParams.append(key, value);
39
- }
40
- };
41
-
42
- const useUrlAndQuery = async (state) => {
43
- const { requestPayload, context } = state;
44
- const { creds } = context;
45
-
46
- const {
47
- BASE_URL,
48
- STORE_HANDLE,
49
- STOREFRONT_API_KEY,
50
- } = creds;
51
- const shopId = `${ STORE_HANDLE }.myshopify.com`;
52
- const storefrontApiKey = STOREFRONT_API_KEY;
53
-
54
- const searchParams = new URLSearchParams();
55
- searchParams.append('shop_id', shopId);
56
- if (storefrontApiKey) {
57
- searchParams.append('api_key', storefrontApiKey);
58
- }
59
- Object.entries(requestPayload.query || {}).forEach(([key, value]) => {
60
- appendParams(searchParams, key, value);
61
- });
62
-
63
- return {
64
- requestPayload: {
65
- ...requestPayload,
66
- url: `${ BASE_URL }${ requestPayload.url }?${ searchParams.toString() }`,
67
- headers: {
68
- Accept: 'application/json',
69
- ...requestPayload.headers,
70
- },
71
- },
72
- };
73
- };
74
-
75
- const tagalysClient = new FetchClient({
76
- pipeline: [
77
- resolveCreds,
78
- useUrlAndQuery,
79
- 'fetch',
80
- fetchClientCommonSteps.exitEarlyOnNotOk,
81
- ],
82
- });
5
+ const { DEFAULT_INCLUDE } = require('../tagalys/tagalys.constants');
6
+ const { tagalysClient } = require('../tagalys/tagalys.utils');
83
7
 
84
8
  const argsWarden = new ArgsWarden([
85
9
  ['credsPayload', credsValidator],
@@ -96,6 +20,9 @@ const tagalysSearch = async (
96
20
  sort, // e.g. 'price-asc'
97
21
  page,
98
22
  perPage,
23
+ country,
24
+ language,
25
+ segmentTag,
99
26
  } = {},
100
27
  ) => {
101
28
  const rejectResponse = await argsWarden.responseIfRejectingArgs({
@@ -106,9 +33,16 @@ const tagalysSearch = async (
106
33
  return rejectResponse;
107
34
  }
108
35
 
36
+ // A search can be both redirected and spelling-corrected in the same
37
+ // response -- redirected/redirectUrl and data (query_handling,
38
+ // original_query, etc.) both land on the response, see
39
+ // tagalys.utils.js#useRedirectResponse.
109
40
  const response = await tagalysClient.fetch({
110
41
  context: {
111
42
  credsPayload,
43
+ country,
44
+ language,
45
+ segmentTag,
112
46
  },
113
47
  requestPayload: {
114
48
  url: '/v2/search',
@@ -125,19 +59,12 @@ const tagalysSearch = async (
125
59
  },
126
60
  });
127
61
 
128
- const { ok, data, error } = response;
62
+ const { ok, error } = response;
129
63
  if (!ok) {
130
64
  logDeep({ error });
131
- return { ok: false, error };
132
- }
133
-
134
- // A configured redirect takes precedence over the rest of the response --
135
- // surface it plainly rather than making callers dig for it.
136
- if (data.redirect_url) {
137
- return { ok: true, redirected: true, redirectUrl: data.redirect_url, data };
138
65
  }
139
66
 
140
- return { ok: true, data };
67
+ return response;
141
68
  };
142
69
 
143
70
  const funcApiConfig = {
@@ -146,11 +73,10 @@ const funcApiConfig = {
146
73
 
147
74
  module.exports = {
148
75
  tagalysSearch,
149
- tagalysClient,
150
76
  funcApiConfig,
151
77
  };
152
78
 
153
79
  /*
154
80
  curl -X POST "http://localhost:8000/tagalysSearch" \
155
- -d '{ "credsPayload": { "credsPath": "tagalys.au" }, "query": "gold" }'
81
+ -d '{ "credsPayload": { "credsPath": "tagalys.store" }, "query": "gold" }'
156
82
  */