@salesforce/lds-drafts 1.160.0 → 1.160.1

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/ldsDrafts.js CHANGED
@@ -192,10 +192,56 @@ const { keys, create, assign, values } = Object;
192
192
  const { stringify, parse } = JSON;
193
193
  const { isArray } = Array;
194
194
 
195
+ function clone(obj) {
196
+ return parse(stringify(obj));
197
+ }
198
+
199
+ /**
200
+ * Generates a time-ordered, unique id to associate with a DraftAction. Ensures
201
+ * no collisions with existing draft action IDs.
202
+ */
203
+ function generateUniqueDraftActionId(existingIds) {
204
+ // new id in milliseconds with some extra digits for collisions
205
+ let newId = new Date().getTime() * 100;
206
+ const existingAsNumbers = existingIds
207
+ .map((id) => parseInt(id, 10))
208
+ .filter((parsed) => !isNaN(parsed));
209
+ let counter = 0;
210
+ while (existingAsNumbers.includes(newId)) {
211
+ newId += 1;
212
+ counter += 1;
213
+ // if the counter is 100+ then somehow this method has been called 100
214
+ // times in one millisecond
215
+ if (counter >= 100) {
216
+ // eslint-disable-next-line @salesforce/lds/no-error-in-production
217
+ throw new Error('Unable to generate unique new draft ID');
218
+ }
219
+ }
220
+ return newId.toString();
221
+ }
222
+ /**
223
+ Use Math.random to generate v4 RFC4122 compliant uuid
224
+ */
225
+ function uuidv4() {
226
+ const uuid = [];
227
+ for (let i = 0; i < 32; i++) {
228
+ const random = (Math.random() * 16) | 0;
229
+ if (i === 8 || i === 12 || i === 16 || i === 20) {
230
+ uuid.push('-');
231
+ }
232
+ uuid.push((i === 12 ? 4 : i === 16 ? (random & 3) | 8 : random).toString(16));
233
+ }
234
+ return uuid.join('');
235
+ }
236
+
237
+ const IdempotencyKey = 'Idempotency-Key';
195
238
  function buildLuvioOverrideForDraftAdapters(luvio, handler, extractTargetIdFromCacheKey, options = {}) {
196
239
  // override this to create and enqueue a new draft action, and return synthetic response
197
240
  const dispatchResourceRequest = async function (resourceRequest, _context) {
198
- const { data } = await handler.enqueue(resourceRequest).catch((err) => {
241
+ const resourceRequestCopy = clone(resourceRequest);
242
+ resourceRequestCopy.headers = resourceRequestCopy.headers || {};
243
+ resourceRequestCopy.headers[IdempotencyKey] = uuidv4();
244
+ const { data } = await handler.enqueue(resourceRequestCopy).catch((err) => {
199
245
  throw transformErrorToDraftSynthesisError(err);
200
246
  });
201
247
  if (data === undefined) {
@@ -287,30 +333,6 @@ async function clearDraftIdSegment(durableStore) {
287
333
  }
288
334
  }
289
335
 
290
- /**
291
- * Generates a time-ordered, unique id to associate with a DraftAction. Ensures
292
- * no collisions with existing draft action IDs.
293
- */
294
- function generateUniqueDraftActionId(existingIds) {
295
- // new id in milliseconds with some extra digits for collisions
296
- let newId = new Date().getTime() * 100;
297
- const existingAsNumbers = existingIds
298
- .map((id) => parseInt(id, 10))
299
- .filter((parsed) => !isNaN(parsed));
300
- let counter = 0;
301
- while (existingAsNumbers.includes(newId)) {
302
- newId += 1;
303
- counter += 1;
304
- // if the counter is 100+ then somehow this method has been called 100
305
- // times in one millisecond
306
- if (counter >= 100) {
307
- // eslint-disable-next-line @salesforce/lds/no-error-in-production
308
- throw new Error('Unable to generate unique new draft ID');
309
- }
310
- }
311
- return newId.toString();
312
- }
313
-
314
336
  var CustomActionResultType;
315
337
  (function (CustomActionResultType) {
316
338
  CustomActionResultType["SUCCESS"] = "SUCCESS";
@@ -1,6 +1,7 @@
1
1
  import type { DispatchResourceRequestContext, Luvio, Snapshot } from '@luvio/engine';
2
2
  import type { AbstractResourceRequestActionHandler } from './actionHandlers/AbstractResourceRequestActionHandler';
3
3
  export type AdapterBuildNetworkSnapshot<Config, Response> = (luvio: Luvio, config: Config, dispatchResourceRequestContext?: DispatchResourceRequestContext) => Promise<Snapshot<Response>>;
4
+ export declare const IdempotencyKey = "Idempotency-Key";
4
5
  export declare function buildLuvioOverrideForDraftAdapters<ResponseType = unknown, DraftMetadata = unknown>(luvio: Luvio, handler: AbstractResourceRequestActionHandler<ResponseType, DraftMetadata>, extractTargetIdFromCacheKey: (cacheKey: string) => string | undefined, options?: {
5
6
  forDeleteAdapter?: boolean;
6
7
  }): Luvio;
@@ -3,3 +3,7 @@
3
3
  * no collisions with existing draft action IDs.
4
4
  */
5
5
  export declare function generateUniqueDraftActionId(existingIds: string[]): string;
6
+ /**
7
+ Use Math.random to generate v4 RFC4122 compliant uuid
8
+ */
9
+ export declare function uuidv4(): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforce/lds-drafts",
3
- "version": "1.160.0",
3
+ "version": "1.160.1",
4
4
  "license": "SEE LICENSE IN LICENSE.txt",
5
5
  "description": "LDS Drafts",
6
6
  "main": "dist/ldsDrafts.js",