@faststore/api 1.12.51 → 1.12.52
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/dist/api.cjs.development.js +130 -10
- package/dist/api.cjs.development.js.map +1 -1
- package/dist/api.cjs.production.min.js +1 -1
- package/dist/api.cjs.production.min.js.map +1 -1
- package/dist/api.esm.js +130 -10
- package/dist/api.esm.js.map +1 -1
- package/dist/platforms/vtex/clients/commerce/index.d.ts +16 -2
- package/dist/platforms/vtex/clients/fetch.d.ts +1 -0
- package/package.json +2 -2
- package/src/platforms/vtex/clients/commerce/index.ts +74 -3
- package/src/platforms/vtex/clients/fetch.ts +52 -1
- package/src/platforms/vtex/resolvers/subscribeToNewsletter.ts +27 -1
|
@@ -14,6 +14,8 @@ import { MasterDataResponse } from './types/Newsletter';
|
|
|
14
14
|
import type { Address, AddressInput } from './types/Address';
|
|
15
15
|
import { ShippingDataBody } from './types/ShippingData';
|
|
16
16
|
declare type ValueOf<T> = T extends Record<string, infer K> ? K : never;
|
|
17
|
+
/** Test-only: forget which accounts were warned about the anonymous fallback. */
|
|
18
|
+
export declare const resetAnonymousNewsletterWarnings: () => void;
|
|
17
19
|
export declare const VtexCommerce: ({ account, environment }: Options, ctx: Context) => {
|
|
18
20
|
catalog: {
|
|
19
21
|
salesChannel: (sc: string) => Promise<SalesChannel>;
|
|
@@ -62,8 +64,20 @@ export declare const VtexCommerce: ({ account, environment }: Options, ctx: Cont
|
|
|
62
64
|
address: ({ postalCode, country, }: AddressInput) => Promise<Address>;
|
|
63
65
|
};
|
|
64
66
|
session: (search: string) => Promise<Session>;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
+
/**
|
|
68
|
+
* Creates a newsletter subscription in the `NL` Master Data entity.
|
|
69
|
+
*
|
|
70
|
+
* With `FS_NEWSLETTER_APP_KEY`/`FS_NEWSLETTER_APP_TOKEN` set, the document is
|
|
71
|
+
* created with a `POST` authenticated by that dedicated key, so `NL` fields
|
|
72
|
+
* can stay private. The request is create-only: the body is built here from
|
|
73
|
+
* the validated fields and never carries a document id, so the caller can't
|
|
74
|
+
* reach an existing record.
|
|
75
|
+
*
|
|
76
|
+
* Without the credentials, it falls back to the legacy anonymous `PATCH`,
|
|
77
|
+
* which only works while `NL` fields are public.
|
|
78
|
+
*/
|
|
79
|
+
subscribeToNewsletter: ({ name, email, }: {
|
|
80
|
+
name?: string | undefined;
|
|
67
81
|
email: string;
|
|
68
82
|
}) => Promise<MasterDataResponse>;
|
|
69
83
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faststore/api",
|
|
3
|
-
"version": "1.12.
|
|
3
|
+
"version": "1.12.52",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -53,5 +53,5 @@
|
|
|
53
53
|
"@envelop/core": "^1 || ^2",
|
|
54
54
|
"graphql": "^15.6.0"
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "f3dba5bfc0d3516fec52145565dae375263d511e"
|
|
57
57
|
}
|
|
@@ -30,6 +30,19 @@ const BASE_INIT = {
|
|
|
30
30
|
},
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
// Accounts already warned that they rely on public `NL` fields. The commerce
|
|
34
|
+
// client is created per request, so the warning is kept once per account
|
|
35
|
+
// instead of per instance, to avoid flooding the logs. The account comes from
|
|
36
|
+
// the store's API options, not from the request, so the set holds at most one
|
|
37
|
+
// entry per account the process is configured for (usually one). It's still
|
|
38
|
+
// capped, so it can't grow without bound if that ever changes.
|
|
39
|
+
const MAX_WARNED_NEWSLETTER_ACCOUNTS = 100
|
|
40
|
+
const accountsWarnedAboutAnonymousNewsletter = new Set<string>()
|
|
41
|
+
|
|
42
|
+
/** Test-only: forget which accounts were warned about the anonymous fallback. */
|
|
43
|
+
export const resetAnonymousNewsletterWarnings = () =>
|
|
44
|
+
accountsWarnedAboutAnonymousNewsletter.clear()
|
|
45
|
+
|
|
33
46
|
const getHeader = (headers: Context['headers'] | undefined, name: string) => {
|
|
34
47
|
const value: string | string[] | undefined = (headers as any)?.[name]
|
|
35
48
|
|
|
@@ -260,13 +273,71 @@ export const VtexCommerce = (
|
|
|
260
273
|
body: '{}',
|
|
261
274
|
})
|
|
262
275
|
},
|
|
263
|
-
|
|
264
|
-
|
|
276
|
+
/**
|
|
277
|
+
* Creates a newsletter subscription in the `NL` Master Data entity.
|
|
278
|
+
*
|
|
279
|
+
* With `FS_NEWSLETTER_APP_KEY`/`FS_NEWSLETTER_APP_TOKEN` set, the document is
|
|
280
|
+
* created with a `POST` authenticated by that dedicated key, so `NL` fields
|
|
281
|
+
* can stay private. The request is create-only: the body is built here from
|
|
282
|
+
* the validated fields and never carries a document id, so the caller can't
|
|
283
|
+
* reach an existing record.
|
|
284
|
+
*
|
|
285
|
+
* Without the credentials, it falls back to the legacy anonymous `PATCH`,
|
|
286
|
+
* which only works while `NL` fields are public.
|
|
287
|
+
*/
|
|
288
|
+
subscribeToNewsletter: ({
|
|
289
|
+
name,
|
|
290
|
+
email,
|
|
291
|
+
}: {
|
|
292
|
+
name?: string
|
|
265
293
|
email: string
|
|
266
294
|
}): Promise<MasterDataResponse> => {
|
|
295
|
+
const body = JSON.stringify({
|
|
296
|
+
...(name ? { name } : {}),
|
|
297
|
+
email,
|
|
298
|
+
isNewsletterOptIn: true,
|
|
299
|
+
})
|
|
300
|
+
const appKey = process.env.FS_NEWSLETTER_APP_KEY
|
|
301
|
+
const appToken = process.env.FS_NEWSLETTER_APP_TOKEN
|
|
302
|
+
|
|
303
|
+
if (appKey && appToken) {
|
|
304
|
+
return fetchAPI(`${base}/api/dataentities/NL/documents`, {
|
|
305
|
+
method: 'POST',
|
|
306
|
+
headers: {
|
|
307
|
+
'content-type': 'application/json',
|
|
308
|
+
accept: 'application/json',
|
|
309
|
+
'X-VTEX-API-AppKey': appKey,
|
|
310
|
+
'X-VTEX-API-AppToken': appToken,
|
|
311
|
+
},
|
|
312
|
+
body,
|
|
313
|
+
})
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (!accountsWarnedAboutAnonymousNewsletter.has(account)) {
|
|
317
|
+
if (
|
|
318
|
+
accountsWarnedAboutAnonymousNewsletter.size >=
|
|
319
|
+
MAX_WARNED_NEWSLETTER_ACCOUNTS
|
|
320
|
+
) {
|
|
321
|
+
accountsWarnedAboutAnonymousNewsletter.clear()
|
|
322
|
+
}
|
|
323
|
+
accountsWarnedAboutAnonymousNewsletter.add(account)
|
|
324
|
+
|
|
325
|
+
// Name only the missing variables, so a partial setup is easy to spot.
|
|
326
|
+
const missing = [
|
|
327
|
+
!appKey && 'FS_NEWSLETTER_APP_KEY',
|
|
328
|
+
!appToken && 'FS_NEWSLETTER_APP_TOKEN',
|
|
329
|
+
].filter(Boolean)
|
|
330
|
+
|
|
331
|
+
console.warn(
|
|
332
|
+
`[subscribeToNewsletter] ${missing.join(' and ')} ${
|
|
333
|
+
missing.length > 1 ? 'are' : 'is'
|
|
334
|
+
} not set. Falling back to an anonymous Master Data request, which requires public fields in the NL entity. Configure both credentials and make the NL fields private.`
|
|
335
|
+
)
|
|
336
|
+
}
|
|
337
|
+
|
|
267
338
|
return fetchAPI(`${base}/api/dataentities/NL/documents/`, {
|
|
268
339
|
...BASE_INIT,
|
|
269
|
-
body
|
|
340
|
+
body,
|
|
270
341
|
method: 'PATCH',
|
|
271
342
|
})
|
|
272
343
|
},
|
|
@@ -3,6 +3,41 @@ import packageJson from '../../../../package.json'
|
|
|
3
3
|
|
|
4
4
|
const USER_AGENT = `${packageJson.name}@${packageJson.version}`
|
|
5
5
|
|
|
6
|
+
// Headers that may carry credentials: store API keys (`X-VTEX-API-AppKey`,
|
|
7
|
+
// `X-VTEX-API-AppToken`), shopper cookies (`Cookie`, `Set-Cookie`,
|
|
8
|
+
// `VtexIdclientAutCookie_*`), `Authorization`, and generic token, secret or
|
|
9
|
+
// API key headers. They are replaced before a failed request and its response
|
|
10
|
+
// are logged. Matched by substring so new credential headers are covered too.
|
|
11
|
+
const SENSITIVE_HEADER = /authorization|cookie|token|secret|api-?key|appkey/i
|
|
12
|
+
|
|
13
|
+
const REDACTED = '[redacted]'
|
|
14
|
+
|
|
15
|
+
export const redactHeaders = (
|
|
16
|
+
headers?: HeadersInit
|
|
17
|
+
): Record<string, string> | undefined => {
|
|
18
|
+
if (!headers) {
|
|
19
|
+
return undefined
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const redacted: Record<string, string> = {}
|
|
23
|
+
const add = (name: string, value: string) => {
|
|
24
|
+
redacted[name] = SENSITIVE_HEADER.test(name) ? REDACTED : value
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (Array.isArray(headers)) {
|
|
28
|
+
headers.forEach(([name, value]) => add(name, value))
|
|
29
|
+
} else if (typeof (headers as Headers).forEach === 'function') {
|
|
30
|
+
// Matches both the global `Headers` and the polyfill returned by
|
|
31
|
+
// `isomorphic-unfetch` on Node, which isn't an instance of the global one.
|
|
32
|
+
const headerList = headers as Headers
|
|
33
|
+
headerList.forEach((value, name) => add(name, value))
|
|
34
|
+
} else {
|
|
35
|
+
Object.entries(headers).forEach(([name, value]) => add(name, value))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return redacted
|
|
39
|
+
}
|
|
40
|
+
|
|
6
41
|
interface FetchAPIOptions {
|
|
7
42
|
storeCookies?: (headers: Headers) => void
|
|
8
43
|
}
|
|
@@ -28,7 +63,23 @@ export const fetchAPI = async (
|
|
|
28
63
|
return response.status !== 204 ? response.json() : undefined
|
|
29
64
|
}
|
|
30
65
|
|
|
31
|
-
|
|
66
|
+
// Log a summary instead of the raw Response, whose headers may include
|
|
67
|
+
// `Set-Cookie` with shopper credentials. The request body is omitted because
|
|
68
|
+
// it can carry personal data (e.g. the newsletter email and name).
|
|
69
|
+
console.error(
|
|
70
|
+
// Only the URL: a `Request` instance could carry unredacted headers.
|
|
71
|
+
typeof info === 'string' ? info : info.url,
|
|
72
|
+
init && {
|
|
73
|
+
...init,
|
|
74
|
+
headers: redactHeaders(init.headers),
|
|
75
|
+
body: init.body ? REDACTED : undefined,
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
status: response.status,
|
|
79
|
+
statusText: response.statusText,
|
|
80
|
+
headers: redactHeaders(response.headers),
|
|
81
|
+
}
|
|
82
|
+
)
|
|
32
83
|
const text = await response.text()
|
|
33
84
|
|
|
34
85
|
throw new Error(text)
|
|
@@ -3,12 +3,38 @@ import type {
|
|
|
3
3
|
MutationSubscribeToNewsletterArgs,
|
|
4
4
|
PersonNewsletter,
|
|
5
5
|
} from '../../../__generated__/schema'
|
|
6
|
+
import { BadRequestError } from '../../errors'
|
|
7
|
+
|
|
8
|
+
const EMAIL_MAX_LENGTH = 254
|
|
9
|
+
const NAME_MAX_LENGTH = 100
|
|
10
|
+
// Character classes exclude `.`, so each domain label is matched once and the
|
|
11
|
+
// pattern stays linear on long inputs.
|
|
12
|
+
const EMAIL_PATTERN = /^[^\s@]+@[^\s@.]+(\.[^\s@.]+)+$/
|
|
6
13
|
|
|
7
14
|
export const subscribeToNewsletter = async (
|
|
8
15
|
_: any,
|
|
9
16
|
{ data }: MutationSubscribeToNewsletterArgs,
|
|
10
17
|
{ clients: { commerce } }: Context
|
|
11
18
|
): Promise<PersonNewsletter | null> => {
|
|
12
|
-
|
|
19
|
+
// The subscription is written with a store credential, so only validated
|
|
20
|
+
// values reach Master Data.
|
|
21
|
+
const email = data.email?.trim().toLowerCase() ?? ''
|
|
22
|
+
const name = data.name?.trim() ?? ''
|
|
23
|
+
|
|
24
|
+
if (email.length > EMAIL_MAX_LENGTH || !EMAIL_PATTERN.test(email)) {
|
|
25
|
+
throw new BadRequestError('Invalid email')
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (name.length > NAME_MAX_LENGTH) {
|
|
29
|
+
throw new BadRequestError(
|
|
30
|
+
`Invalid name: must have at most ${NAME_MAX_LENGTH} characters`
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const response = await commerce.subscribeToNewsletter({
|
|
35
|
+
email,
|
|
36
|
+
name: name || undefined,
|
|
37
|
+
})
|
|
38
|
+
|
|
13
39
|
return { id: response?.Id }
|
|
14
40
|
}
|