@vtex/faststore-plugin-buyer-portal 2.0.38 → 2.0.39

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vtex/faststore-plugin-buyer-portal",
3
- "version": "2.0.38",
3
+ "version": "2.0.39",
4
4
  "description": "A plugin for faststore with buyer portal",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -8,6 +8,7 @@ import { toQueryParams } from "../utils/toQueryParams";
8
8
 
9
9
  const BUYER_PORTAL_CLIENT_HEADER = "x-buyer-portal-client";
10
10
  const BUYER_PORTAL_CLIENT = `organization-account/${CURRENT_VERSION}`;
11
+ const OPERATION_ID_HEADER = "x-operation-id";
11
12
 
12
13
  interface RequestConfig<Input = unknown> {
13
14
  url: string;
@@ -98,9 +99,14 @@ class Client {
98
99
  }: RequestConfig<Input>): Promise<Return> {
99
100
  // fetch joins names that differ only in case into one comma-separated
100
101
  // value, which the BFF rejects.
101
- const callerSetsClientHeader = Object.keys(headers).some(
102
- (name) => name.toLowerCase() === BUYER_PORTAL_CLIENT_HEADER
102
+ const callerHeaderNames = Object.keys(headers).map((name) =>
103
+ name.toLowerCase()
103
104
  );
105
+ const callerSetsClientHeader = callerHeaderNames.includes(
106
+ BUYER_PORTAL_CLIENT_HEADER
107
+ );
108
+ const callerSetsOperationId =
109
+ callerHeaderNames.includes(OPERATION_ID_HEADER);
104
110
 
105
111
  const config: RequestInit = {
106
112
  method,
@@ -110,6 +116,9 @@ class Client {
110
116
  ...(this.isBffClient && !callerSetsClientHeader
111
117
  ? { [BUYER_PORTAL_CLIENT_HEADER]: BUYER_PORTAL_CLIENT }
112
118
  : {}),
119
+ ...(this.isBffClient && !callerSetsOperationId
120
+ ? { [OPERATION_ID_HEADER]: crypto.randomUUID() }
121
+ : {}),
113
122
  ...(headers.Cookie
114
123
  ? getCookieWithoutSessionObjects(headers.Cookie ?? "")
115
124
  : {}),
@@ -12,6 +12,7 @@ import { CURRENT_VERSION } from "../../utils/constants";
12
12
  import { Client, ClientError } from "../Client";
13
13
 
14
14
  const CLIENT_HEADER = "x-buyer-portal-client";
15
+ const OPERATION_ID_HEADER = "x-operation-id";
15
16
 
16
17
  function sentHeaders(fetchMock: ReturnType<typeof makeFetchMock>) {
17
18
  const [, init] = fetchMock.mock.calls[0] as [string, RequestInit];
@@ -76,6 +77,89 @@ describe("Client", () => {
76
77
  });
77
78
  });
78
79
 
80
+ describe("x-operation-id", () => {
81
+ const UUID_V4 =
82
+ /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
83
+
84
+ it("sends a UUID on BFF requests", async () => {
85
+ const fetchMock = makeFetchMock({ ok: true });
86
+ vi.stubGlobal("fetch", fetchMock);
87
+
88
+ await new Client(getApiUrl()).get("units/unit-1/summary");
89
+
90
+ expect(sentHeaders(fetchMock)[OPERATION_ID_HEADER]).toMatch(UUID_V4);
91
+ });
92
+
93
+ it("sends it next to the cookie and auth headers on server-side requests", async () => {
94
+ const fetchMock = makeFetchMock({ ok: true });
95
+ vi.stubGlobal("fetch", fetchMock);
96
+
97
+ await new Client(getApiUrl()).get("granted", {
98
+ headers: {
99
+ Cookie: "VtexIdclientAutCookie_test-store=token-value;other=1",
100
+ },
101
+ });
102
+
103
+ const headers = sentHeaders(fetchMock);
104
+
105
+ expect(headers[OPERATION_ID_HEADER]).toMatch(UUID_V4);
106
+ expect(headers).toMatchObject({
107
+ "VtexIdclientAutCookie_test-store": "token-value",
108
+ });
109
+ });
110
+
111
+ it("uses a different id on each request", async () => {
112
+ const fetchMock = makeFetchMock({ ok: true });
113
+ vi.stubGlobal("fetch", fetchMock);
114
+
115
+ const client = new Client(getApiUrl());
116
+ await client.get("resource");
117
+ await client.get("resource");
118
+
119
+ const ids = fetchMock.mock.calls.map(
120
+ ([, init]) =>
121
+ (init as RequestInit & { headers: Record<string, string> }).headers[
122
+ OPERATION_ID_HEADER
123
+ ]
124
+ );
125
+
126
+ expect(ids[0]).not.toBe(ids[1]);
127
+ });
128
+
129
+ it.each(["", "https://test.vtex.com/api/", "https://api.example.com/"])(
130
+ "is not sent to base URL %j",
131
+ async (baseURL) => {
132
+ const fetchMock = makeFetchMock({ ok: true });
133
+ vi.stubGlobal("fetch", fetchMock);
134
+
135
+ await new Client(baseURL).get("resource");
136
+
137
+ expect(sentHeaders(fetchMock)).not.toHaveProperty(OPERATION_ID_HEADER);
138
+ }
139
+ );
140
+
141
+ it.each([OPERATION_ID_HEADER, "X-Operation-Id"])(
142
+ "lets a header passed by the caller as %s win, sent once",
143
+ async (name) => {
144
+ const fetchMock = makeFetchMock({ ok: true });
145
+ vi.stubGlobal("fetch", fetchMock);
146
+
147
+ await new Client(getApiUrl()).get("resource", {
148
+ headers: { [name]: "caller-id" },
149
+ });
150
+
151
+ const headers = sentHeaders(fetchMock);
152
+
153
+ expect(new Headers(headers).get(OPERATION_ID_HEADER)).toBe("caller-id");
154
+ expect(
155
+ Object.keys(headers).filter(
156
+ (key) => key.toLowerCase() === OPERATION_ID_HEADER
157
+ )
158
+ ).toHaveLength(1);
159
+ }
160
+ );
161
+ });
162
+
79
163
  describe("x-buyer-portal-client", () => {
80
164
  it("identifies the plugin on BFF requests", async () => {
81
165
  const fetchMock = makeFetchMock({ ok: true });
@@ -33,4 +33,4 @@ export const STOREFRONT_RESOURCE_NAMES = {
33
33
  export type StorefrontResourceName =
34
34
  (typeof STOREFRONT_RESOURCE_NAMES)[keyof typeof STOREFRONT_RESOURCE_NAMES];
35
35
 
36
- export const CURRENT_VERSION = "2.0.38";
36
+ export const CURRENT_VERSION = "2.0.39";