@salesforce/lds-drafts 1.100.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.
@@ -0,0 +1,50 @@
1
+ import type { NetworkAdapter, ResourceRequest, Luvio, DurableStoreKeyMetadataMap } from '@luvio/engine';
2
+ import type { DraftAction, CompletedDraftAction, QueueOperation, PendingDraftAction, DraftActionMetadata, DraftQueue } from '../DraftQueue';
3
+ import { ProcessActionResult } from '../DraftQueue';
4
+ import type { ActionHandler, DraftIdAndKeyMapping, ReplacingActions } from './ActionHandler';
5
+ export type ResponseIngestionEntry<T = unknown> = {
6
+ response: T;
7
+ synchronousIngest: AbstractResourceRequestActionHandler<T, unknown>['synchronousIngest'];
8
+ buildCacheKeysForResponse: AbstractResourceRequestActionHandler<T, unknown>['buildCacheKeysFromResponse'];
9
+ };
10
+ export declare abstract class AbstractResourceRequestActionHandler<ResponseType, DraftMetadata> implements ActionHandler<ResourceRequest, DraftMetadata, ResponseType> {
11
+ protected readonly draftQueue: DraftQueue;
12
+ protected readonly networkAdapter: NetworkAdapter;
13
+ protected readonly getLuvio: () => Luvio;
14
+ constructor(draftQueue: DraftQueue, networkAdapter: NetworkAdapter, getLuvio: () => Luvio);
15
+ enqueue(data: ResourceRequest): Promise<PendingDraftAction<ResourceRequest>>;
16
+ handleAction(action: DraftAction<ResourceRequest, ResponseType>, actionCompleted: (action: CompletedDraftAction<ResourceRequest, ResponseType>) => Promise<void>, actionErrored: (action: DraftAction<ResourceRequest, ResponseType>, retry: boolean) => Promise<void>): Promise<ProcessActionResult>;
17
+ buildPendingAction(request: ResourceRequest, queue: DraftAction<unknown, unknown>[]): Promise<PendingDraftAction<ResourceRequest>>;
18
+ handleActionEnqueued(action: PendingDraftAction<ResourceRequest>): Promise<void>;
19
+ handleActionRemoved(action: DraftAction<ResourceRequest, ResponseType>): Promise<void>;
20
+ getQueueOperationsForCompletingDrafts(queue: DraftAction<unknown, unknown>[], action: CompletedDraftAction<ResourceRequest, ResponseType>): QueueOperation[];
21
+ getRedirectMappings(action: CompletedDraftAction<ResourceRequest, ResponseType>): DraftIdAndKeyMapping[] | undefined;
22
+ handleActionCompleted(action: CompletedDraftAction<ResourceRequest, ResponseType>, queueOperations: QueueOperation[], _queue: DraftAction<unknown, unknown>[], allHandlers: ActionHandler<unknown, unknown, unknown>[]): Promise<void>;
23
+ handleReplaceAction<Response>(actionId: string, withActionId: string, uploadingActionId: String | undefined, actions: DraftAction<unknown, unknown>[]): ReplacingActions<ResourceRequest, Response>;
24
+ shouldDeleteActionByTagOnRemoval(action: DraftAction<ResourceRequest, ResponseType>): boolean;
25
+ updateMetadata(_existingMetadata: DraftActionMetadata, incomingMetadata: DraftActionMetadata): DraftActionMetadata;
26
+ private isActionOfType;
27
+ protected reingestRecord(action: DraftAction<ResourceRequest, ResponseType>): Promise<void>;
28
+ getDraftIdsFromAction(action: DraftAction<ResourceRequest, ResponseType>): string[];
29
+ ingestResponses(responses: ResponseIngestionEntry[], action: DraftAction<ResourceRequest, ResponseType>): Promise<void>;
30
+ evictKey(key: string): Promise<void>;
31
+ abstract handlerId: string;
32
+ abstract canHandlePublish(key: string): boolean;
33
+ abstract canRepresentationContainDraftMetadata(representationName: string): boolean;
34
+ abstract getIdFromRequest(request: ResourceRequest): Promise<string | undefined>;
35
+ abstract getIdFromResponseBody(responseBody: ResponseType): string;
36
+ abstract buildTagForTargetId(id: string): string;
37
+ abstract buildCacheKeysFromResponse(response: ResponseType): DurableStoreKeyMetadataMap;
38
+ abstract synchronousIngest(response: ResponseType, action: DraftAction<ResourceRequest, ResponseType>): void;
39
+ abstract getDataForAction(action: DraftAction<ResourceRequest, ResponseType>): Promise<ResponseType | undefined>;
40
+ abstract getDraftMetadata(key: string): Promise<DraftMetadata | undefined>;
41
+ abstract applyDraftsToIncomingData(key: string, data: unknown, draftMetadata: DraftMetadata | undefined, publishFn: (key: string, data: any) => void): void;
42
+ /**
43
+ * Returns true if the given targetId is a for a draft-created record
44
+ *
45
+ * @param targetId the targetId to check
46
+ */
47
+ abstract isDraftId(targetId: string): boolean;
48
+ abstract mergeActions(targetAction: DraftAction<ResourceRequest, ResponseType>, sourceAction: DraftAction<ResourceRequest, ResponseType>): DraftAction<ResourceRequest, ResponseType>;
49
+ }
50
+ export declare function isResourceRequestAction(action: DraftAction<unknown, unknown>): action is DraftAction<ResourceRequest, unknown>;
@@ -0,0 +1,149 @@
1
+ import type { CompletedDraftAction, DraftAction, DraftActionMetadata, PendingDraftAction, ProcessActionResult, QueueOperation } from '../DraftQueue';
2
+ import type { DraftKeyMapping } from '../DraftIdMapping';
3
+ export interface ReplacingActions<Response, Data> {
4
+ original: DraftAction<Response, Data>;
5
+ actionToReplace: DraftAction<Response, Data>;
6
+ replacingAction: DraftAction<Response, Data>;
7
+ }
8
+ export interface DraftIdAndKeyMapping extends DraftKeyMapping {
9
+ draftId: string;
10
+ canonicalId: string;
11
+ }
12
+ /**
13
+ * A Handler that knows what the Data and Response type expected are in a DraftAction.
14
+ * It knows how to process and set them up.
15
+ *
16
+ * The action handler is registered both with the DraftQueue and the Draft environment to handle
17
+ * all draft application logic to a particular type
18
+ */
19
+ export interface ActionHandler<Data, DraftMetadata, Type> {
20
+ /**
21
+ * The id for this handler. Every action in the queue will have a handlerId property
22
+ * associated with it which specifies which handler knows how to handle that draft action
23
+ */
24
+ handlerId: string;
25
+ /**
26
+ * Invoked by the draft environment while ingesting incoming data to determine if the handler knows how to apply drafts to the incoming key
27
+ * @param key
28
+ */
29
+ canHandlePublish(key: string): boolean;
30
+ /**
31
+ * Invoked by the draft environment prior to ingesting to determine if the representation type can potentially have draft metadata
32
+ * @param representationName
33
+ */
34
+ canRepresentationContainDraftMetadata(representationName: string): boolean;
35
+ /**
36
+ * Enqueues an action to the draft queue
37
+ * @param data the data that will get added to the draft action
38
+ */
39
+ enqueue(data: Data): Promise<PendingDraftAction<Data>>;
40
+ /**
41
+ * Used to build a PendingDraftAction that is specific to the type of Data and Response.
42
+ * @param data
43
+ * @param queue
44
+ *
45
+ * @throws {Error} if any preconditions to building the draft action are not satisfied
46
+ */
47
+ buildPendingAction(data: unknown, queue: DraftAction<unknown, unknown>[]): Promise<PendingDraftAction<Data>>;
48
+ /**
49
+ * Invoked after an action is enqueued into the draft queue
50
+ * @param action The action that was just enqueued
51
+ * @param queue The queue with the new action now inserted
52
+ */
53
+ handleActionEnqueued(action: PendingDraftAction<Data>, queue: DraftAction<unknown, unknown>[]): Promise<void>;
54
+ /**
55
+ * Invoked by the draft queue when it is this actions turn to execute.
56
+ * @param action: the action that is executing
57
+ * @param actionCompleted callback function when the action completes successfully
58
+ * @param actionErrored callback function invoked when the action errors
59
+ */
60
+ handleAction(action: DraftAction<Data, Type>, actionCompleted: (action: CompletedDraftAction<Data, Type>) => Promise<void>, actionErrored: (action: DraftAction<Data, Type>, retry: boolean) => Promise<void>): Promise<ProcessActionResult>;
61
+ /**
62
+ * Invoked by the draft queue after an action for this handler is removed from the queue
63
+ * @param action: the removed action
64
+ * @param queue: the queue with the action removed
65
+ */
66
+ handleActionRemoved(action: DraftAction<Data, Type>, queue: DraftAction<unknown, unknown>[]): Promise<void>;
67
+ /**
68
+ * Invoked by the draft queue while an action is completing successfully and returns
69
+ * a set of queue modification operations that should be performed as a result of the
70
+ * action completing
71
+ * @param queue
72
+ * @param action
73
+ */
74
+ getQueueOperationsForCompletingDrafts(queue: DraftAction<unknown, unknown>[], action: CompletedDraftAction<Data, Type>): QueueOperation[];
75
+ /**
76
+ * Invoked by the draft queue when an action is completing. Sometimes after an action completes we
77
+ * need to perform a key redirect (i.e. an object that used to have a draft id now has a real id and it needs to redirect the old id)
78
+ * This function returns an array of mappings if necessary or undefined if no redirect is necessary as a result of the completed operation
79
+ * @param action
80
+ */
81
+ getRedirectMappings(action: CompletedDraftAction<Data, Type>): DraftIdAndKeyMapping[] | undefined;
82
+ /**
83
+ * Invoked by the draft queue after an action completes and is removed from the queue
84
+ * @param action The completed action
85
+ * @param queueOperations The queue modification operations that were executes with the completed action
86
+ * @param queue The updated queue with the completed action removed
87
+ */
88
+ handleActionCompleted(action: CompletedDraftAction<Data, Type>, queueOperations: QueueOperation[], queue: DraftAction<unknown, unknown>[], allHandlers: ActionHandler<unknown, unknown, unknown>[]): Promise<void>;
89
+ /**
90
+ * Replace an item in the queue with another to take its place (if supported by handler)
91
+ * @param actionId
92
+ * @param withActionId
93
+ * @param uploadingActionId
94
+ * @param actions
95
+ */
96
+ handleReplaceAction<Response>(actionId: string, withActionId: string, uploadingActionId: string | undefined, actions: DraftAction<unknown, unknown>[]): ReplacingActions<Data, Response>;
97
+ /**
98
+ * Invoked by the durable store and the draft environment to get the data that is associated with this draft action
99
+ * The data should include all drafts applied to it (if any)
100
+ * @param action The action to get data for
101
+ * @param queue The draft queue
102
+ *
103
+ * @returns the data with the up to date draft changes applied. returns undefined if after replaying drafts there is no
104
+ * data to return (i.e. a draft-created record had it's post action removed)
105
+ */
106
+ getDataForAction(action: DraftAction<Data, Type>): Promise<Type | undefined>;
107
+ /**
108
+ * Asynchronous function to fetch any necessary metadata required to apply drafts during ingest
109
+ * Note that since ingest is synchronous any necessary metadata that requires asynchronicity must be fetch prior to ingest commencing
110
+ * @param key
111
+ * @returns the metadata or undefined if there's no drafts associated or no metadata for this key
112
+ */
113
+ getDraftMetadata(key: string): Promise<DraftMetadata | undefined>;
114
+ /**
115
+ * Invoked by the draft environment while ingesting normalized data that may contain drafts.
116
+ * Note that this function is synchronous since ingestion is synchronous (see note in getDraftMetadata)
117
+ * @param key The key being ingested
118
+ * @param data The normalized data being ingested
119
+ * @param draftMetadata The metadata that was previously fetched using getDraftMetadata
120
+ * @param publishFn The environment's publish method to publish the data once drafts have been applied
121
+ */
122
+ applyDraftsToIncomingData(key: string, data: unknown, draftMetadata: DraftMetadata | undefined, publishFn: (key: string, data: any) => void): void;
123
+ /**
124
+ * Invoked by the draft queue when a request to remove an action is made. This tells the queue whether it should just
125
+ * delete the requested action or any action with the same tag. This is helpful in examples where other related queue items no longer
126
+ * make sense with this action removed.
127
+ * @param action The action being removed
128
+ */
129
+ shouldDeleteActionByTagOnRemoval(action: DraftAction<Data, Type>): boolean;
130
+ /**
131
+ * Invoked by the draft queue when a request to update an actions metadata is made. This allows the handler to mutate the incoming
132
+ * metadata if necessary
133
+ * @param existingMetadata
134
+ * @param incomingMetadata
135
+ * @returns the metadata that will be persisted
136
+ */
137
+ updateMetadata(existingMetadata: DraftActionMetadata, incomingMetadata: DraftActionMetadata): DraftActionMetadata;
138
+ /**
139
+ * Merges two actions, overlaying the source over the target (ie: the data from
140
+ * both actions will be combined, if there are conflicts the source's data will
141
+ * be used).
142
+ *
143
+ * Merging draft actions with different targetIds or different tags will result
144
+ * in an error.
145
+ *
146
+ * NOTE: the resulting merged action will use the target's timestamp and id.
147
+ */
148
+ mergeActions(targetAction: DraftAction<Data, Type>, sourceAction: DraftAction<Data, Type>): DraftAction<Data, Type>;
149
+ }
@@ -0,0 +1,33 @@
1
+ import type { DraftAction, DraftQueue } from '../DraftQueue';
2
+ import type { ActionHandler } from './ActionHandler';
3
+ export declare enum CustomActionResultType {
4
+ SUCCESS = "SUCCESS",
5
+ FAILURE = "FAILURE"
6
+ }
7
+ export declare enum CustomActionErrorType {
8
+ NETWORK_ERROR = "NETWORK_ERROR",
9
+ CLIENT_ERROR = "CLIENT_ERROR"
10
+ }
11
+ export interface CustomActionError {
12
+ type: CustomActionErrorType;
13
+ message: string;
14
+ }
15
+ interface BaseCustomActionResult {
16
+ id: string;
17
+ type: CustomActionResultType;
18
+ }
19
+ export interface CustomActionSuccess extends BaseCustomActionResult {
20
+ type: CustomActionResultType.SUCCESS;
21
+ }
22
+ export interface CustomActionFailed extends BaseCustomActionResult {
23
+ type: CustomActionResultType.FAILURE;
24
+ error: CustomActionError;
25
+ }
26
+ export type CustomActionResult = CustomActionSuccess | CustomActionFailed;
27
+ export type CustomActionExecutor = (action: DraftAction<unknown, unknown>, completed: (result: CustomActionResult) => void) => void;
28
+ export type CustomActionCompletionResponse = (result: CustomActionResult) => void;
29
+ export interface CustomActionData {
30
+ [key: string]: string;
31
+ }
32
+ export declare function customActionHandler(executor: CustomActionExecutor, id: string, draftQueue: DraftQueue): ActionHandler<CustomActionData, unknown, unknown>;
33
+ export {};