@azure/core-lro 2.3.2-alpha.20220923.1 → 2.4.0

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/index.js CHANGED
@@ -44,7 +44,7 @@ function setStateError(inputs) {
44
44
  };
45
45
  }
46
46
  function processOperationStatus(result) {
47
- const { state, stateProxy, status } = result;
47
+ const { state, stateProxy, status, isDone, processResult, response, setErrorAsResult } = result;
48
48
  switch (status) {
49
49
  case "succeeded": {
50
50
  stateProxy.setSucceeded(state);
@@ -60,6 +60,15 @@ function processOperationStatus(result) {
60
60
  break;
61
61
  }
62
62
  }
63
+ if ((isDone === null || isDone === void 0 ? void 0 : isDone(response, state)) ||
64
+ (isDone === undefined &&
65
+ ["succeeded", "canceled"].concat(setErrorAsResult ? [] : ["failed"]).includes(status))) {
66
+ stateProxy.setResult(state, buildResult({
67
+ response,
68
+ state,
69
+ processResult,
70
+ }));
71
+ }
63
72
  }
64
73
  function buildResult(inputs) {
65
74
  const { processResult, response, state } = inputs;
@@ -69,7 +78,7 @@ function buildResult(inputs) {
69
78
  * Initiates the long-running operation.
70
79
  */
71
80
  async function initOperation(inputs) {
72
- const { init, stateProxy, processResult, getOperationStatus, withOperationLocation } = inputs;
81
+ const { init, stateProxy, processResult, getOperationStatus, withOperationLocation, setErrorAsResult, } = inputs;
73
82
  const { operationLocation, resourceLocation, metadata, response } = await init();
74
83
  if (operationLocation)
75
84
  withOperationLocation === null || withOperationLocation === void 0 ? void 0 : withOperationLocation(operationLocation, false);
@@ -81,14 +90,7 @@ async function initOperation(inputs) {
81
90
  logger.verbose(`LRO: Operation description:`, config);
82
91
  const state = stateProxy.initState(config);
83
92
  const status = getOperationStatus({ response, state, operationLocation });
84
- processOperationStatus({ state, status, stateProxy });
85
- if (status === "succeeded") {
86
- stateProxy.setResult(state, buildResult({
87
- response,
88
- state,
89
- processResult,
90
- }));
91
- }
93
+ processOperationStatus({ state, status, stateProxy, response, setErrorAsResult, processResult });
92
94
  return state;
93
95
  }
94
96
  async function pollOperationHelper(inputs) {
@@ -99,11 +101,6 @@ async function pollOperationHelper(inputs) {
99
101
  }));
100
102
  const status = getOperationStatus(response, state);
101
103
  logger.verbose(`LRO: Status:\n\tPolling from: ${state.config.operationLocation}\n\tOperation status: ${status}\n\tPolling status: ${terminalStates.includes(status) ? "Stopped" : "Running"}`);
102
- processOperationStatus({
103
- status,
104
- state,
105
- stateProxy,
106
- });
107
104
  if (status === "succeeded") {
108
105
  const resourceLocation = getResourceLocation(response, state);
109
106
  if (resourceLocation !== undefined) {
@@ -117,7 +114,7 @@ async function pollOperationHelper(inputs) {
117
114
  }
118
115
  /** Polls the long-running operation. */
119
116
  async function pollOperation(inputs) {
120
- const { poll, state, stateProxy, options, getOperationStatus, getResourceLocation, getOperationLocation, withOperationLocation, getPollingInterval, processResult, updateState, setDelay, isDone, } = inputs;
117
+ const { poll, state, stateProxy, options, getOperationStatus, getResourceLocation, getOperationLocation, withOperationLocation, getPollingInterval, processResult, updateState, setDelay, isDone, setErrorAsResult, } = inputs;
121
118
  const { operationLocation } = state.config;
122
119
  if (operationLocation !== undefined) {
123
120
  const { response, status } = await pollOperationHelper({
@@ -129,15 +126,16 @@ async function pollOperation(inputs) {
129
126
  getResourceLocation,
130
127
  options,
131
128
  });
132
- if ((isDone === null || isDone === void 0 ? void 0 : isDone(response, state)) ||
133
- (isDone === undefined && ["succeeded", "canceled"].includes(status))) {
134
- stateProxy.setResult(state, buildResult({
135
- response,
136
- state,
137
- processResult,
138
- }));
139
- }
140
- else {
129
+ processOperationStatus({
130
+ status,
131
+ response,
132
+ state,
133
+ stateProxy,
134
+ isDone,
135
+ processResult,
136
+ setErrorAsResult,
137
+ });
138
+ if (!terminalStates.includes(status)) {
141
139
  const intervalInMs = getPollingInterval === null || getPollingInterval === void 0 ? void 0 : getPollingInterval(response);
142
140
  if (intervalInMs)
143
141
  setDelay(intervalInMs);
@@ -317,7 +315,7 @@ function getStatusFromInitialResponse(inputs) {
317
315
  * Initiates the long-running operation.
318
316
  */
319
317
  async function initHttpOperation(inputs) {
320
- const { stateProxy, resourceLocationConfig, processResult, lro } = inputs;
318
+ const { stateProxy, resourceLocationConfig, processResult, lro, setErrorAsResult } = inputs;
321
319
  return initOperation({
322
320
  init: async () => {
323
321
  const response = await lro.sendInitialRequest();
@@ -334,6 +332,7 @@ async function initHttpOperation(inputs) {
334
332
  ? ({ flatResponse }, state) => processResult(flatResponse, state)
335
333
  : ({ flatResponse }) => flatResponse,
336
334
  getOperationStatus: getStatusFromInitialResponse,
335
+ setErrorAsResult,
337
336
  });
338
337
  }
339
338
  function getOperationLocation({ rawResponse }, state) {
@@ -383,7 +382,7 @@ function getResourceLocation({ flatResponse }, state) {
383
382
  }
384
383
  /** Polls the long-running operation. */
385
384
  async function pollHttpOperation(inputs) {
386
- const { lro, stateProxy, options, processResult, updateState, setDelay, state } = inputs;
385
+ const { lro, stateProxy, options, processResult, updateState, setDelay, state, setErrorAsResult, } = inputs;
387
386
  return pollOperation({
388
387
  state,
389
388
  stateProxy,
@@ -402,6 +401,7 @@ async function pollHttpOperation(inputs) {
402
401
  * references an inner this, so we need to preserve a reference to it.
403
402
  */
404
403
  poll: async (location, inputOptions) => lro.sendPollRequest(location, inputOptions),
404
+ setErrorAsResult,
405
405
  });
406
406
  }
407
407
 
@@ -482,7 +482,7 @@ const createStateProxy$1 = () => ({
482
482
  * Returns a poller factory.
483
483
  */
484
484
  function buildCreatePoller(inputs) {
485
- const { getOperationLocation, getStatusFromInitialResponse, getStatusFromPollResponse, getResourceLocation, getPollingInterval, } = inputs;
485
+ const { getOperationLocation, getStatusFromInitialResponse, getStatusFromPollResponse, getResourceLocation, getPollingInterval, resolveOnUnsuccessful, } = inputs;
486
486
  return async ({ init, poll }, options) => {
487
487
  const { processResult, updateState, withOperationLocation: withOperationLocationCallback, intervalInMs = POLL_INTERVAL_IN_MS, restoreFrom, } = options || {};
488
488
  const stateProxy = createStateProxy$1();
@@ -506,6 +506,7 @@ function buildCreatePoller(inputs) {
506
506
  processResult,
507
507
  getOperationStatus: getStatusFromInitialResponse,
508
508
  withOperationLocation,
509
+ setErrorAsResult: !resolveOnUnsuccessful,
509
510
  });
510
511
  let resultPromise;
511
512
  let cancelJob;
@@ -549,10 +550,14 @@ function buildCreatePoller(inputs) {
549
550
  return poller.getResult();
550
551
  }
551
552
  case "canceled": {
552
- throw new Error("Operation was canceled");
553
+ if (!resolveOnUnsuccessful)
554
+ throw new Error("Operation was canceled");
555
+ return poller.getResult();
553
556
  }
554
557
  case "failed": {
555
- throw state.error;
558
+ if (!resolveOnUnsuccessful)
559
+ throw state.error;
560
+ return poller.getResult();
556
561
  }
557
562
  case "notStarted":
558
563
  case "running": {
@@ -579,12 +584,13 @@ function buildCreatePoller(inputs) {
579
584
  setDelay: (pollIntervalInMs) => {
580
585
  currentPollIntervalInMs = pollIntervalInMs;
581
586
  },
587
+ setErrorAsResult: !resolveOnUnsuccessful,
582
588
  });
583
589
  await handleProgressEvents();
584
- if (state.status === "canceled") {
590
+ if (state.status === "canceled" && !resolveOnUnsuccessful) {
585
591
  throw new Error("Operation was canceled");
586
592
  }
587
- if (state.status === "failed") {
593
+ if (state.status === "failed" && !resolveOnUnsuccessful) {
588
594
  throw state.error;
589
595
  }
590
596
  },
@@ -601,13 +607,14 @@ function buildCreatePoller(inputs) {
601
607
  * @returns an initialized poller
602
608
  */
603
609
  async function createHttpPoller(lro, options) {
604
- const { resourceLocationConfig, intervalInMs, processResult, restoreFrom, updateState, withOperationLocation, } = options || {};
610
+ const { resourceLocationConfig, intervalInMs, processResult, restoreFrom, updateState, withOperationLocation, resolveOnUnsuccessful = false, } = options || {};
605
611
  return buildCreatePoller({
606
612
  getStatusFromInitialResponse,
607
613
  getStatusFromPollResponse: getOperationStatus,
608
614
  getOperationLocation,
609
615
  getResourceLocation,
610
616
  getPollingInterval: parseRetryAfter,
617
+ resolveOnUnsuccessful,
611
618
  })({
612
619
  init: async () => {
613
620
  const response = await lro.sendInitialRequest();
@@ -650,9 +657,10 @@ const createStateProxy = () => ({
650
657
  isSucceeded: (state) => Boolean(state.isCompleted && !state.isCancelled && !state.error),
651
658
  });
652
659
  class GenericPollOperation {
653
- constructor(state, lro, lroResourceLocationConfig, processResult, updateState, isDone) {
660
+ constructor(state, lro, setErrorAsResult, lroResourceLocationConfig, processResult, updateState, isDone) {
654
661
  this.state = state;
655
662
  this.lro = lro;
663
+ this.setErrorAsResult = setErrorAsResult;
656
664
  this.lroResourceLocationConfig = lroResourceLocationConfig;
657
665
  this.processResult = processResult;
658
666
  this.updateState = updateState;
@@ -670,6 +678,7 @@ class GenericPollOperation {
670
678
  stateProxy,
671
679
  resourceLocationConfig: this.lroResourceLocationConfig,
672
680
  processResult: this.processResult,
681
+ setErrorAsResult: this.setErrorAsResult,
673
682
  })));
674
683
  }
675
684
  const updateState = this.updateState;
@@ -690,6 +699,7 @@ class GenericPollOperation {
690
699
  setDelay: (intervalInMs) => {
691
700
  this.pollerConfig.intervalInMs = intervalInMs;
692
701
  },
702
+ setErrorAsResult: this.setErrorAsResult,
693
703
  });
694
704
  }
695
705
  (_a = options === null || options === void 0 ? void 0 : options.fireProgress) === null || _a === void 0 ? void 0 : _a.call(options, this.state);
@@ -862,6 +872,8 @@ class Poller {
862
872
  * @param operation - Must contain the basic properties of `PollOperation<State, TResult>`.
863
873
  */
864
874
  constructor(operation) {
875
+ /** controls whether to throw an error if the operation failed or was canceled. */
876
+ this.resolveOnUnsuccessful = false;
865
877
  this.stopped = true;
866
878
  this.pollProgressCallbacks = [];
867
879
  this.operation = operation;
@@ -899,15 +911,10 @@ class Poller {
899
911
  */
900
912
  async pollOnce(options = {}) {
901
913
  if (!this.isDone()) {
902
- try {
903
- this.operation = await this.operation.update({
904
- abortSignal: options.abortSignal,
905
- fireProgress: this.fireProgress.bind(this),
906
- });
907
- }
908
- catch (e) {
909
- this.operation.state.error = e;
910
- }
914
+ this.operation = await this.operation.update({
915
+ abortSignal: options.abortSignal,
916
+ fireProgress: this.fireProgress.bind(this),
917
+ });
911
918
  }
912
919
  this.processUpdatedState();
913
920
  }
@@ -951,22 +958,26 @@ class Poller {
951
958
  processUpdatedState() {
952
959
  if (this.operation.state.error) {
953
960
  this.stopped = true;
954
- this.reject(this.operation.state.error);
955
- throw this.operation.state.error;
961
+ if (!this.resolveOnUnsuccessful) {
962
+ this.reject(this.operation.state.error);
963
+ throw this.operation.state.error;
964
+ }
956
965
  }
957
966
  if (this.operation.state.isCancelled) {
958
967
  this.stopped = true;
959
- const error = new PollerCancelledError("Operation was canceled");
960
- this.reject(error);
961
- throw error;
968
+ if (!this.resolveOnUnsuccessful) {
969
+ const error = new PollerCancelledError("Operation was canceled");
970
+ this.reject(error);
971
+ throw error;
972
+ }
962
973
  }
963
- else if (this.isDone() && this.resolve) {
974
+ if (this.isDone() && this.resolve) {
964
975
  // If the poller has finished polling, this means we now have a result.
965
976
  // However, it can be the case that TResult is instantiated to void, so
966
977
  // we are not expecting a result anyway. To assert that we might not
967
978
  // have a result eventually after finishing polling, we cast the result
968
979
  // to TResult.
969
- this.resolve(this.operation.state.result);
980
+ this.resolve(this.getResult());
970
981
  }
971
982
  }
972
983
  /**
@@ -1111,12 +1122,13 @@ class Poller {
1111
1122
  */
1112
1123
  class LroEngine extends Poller {
1113
1124
  constructor(lro, options) {
1114
- const { intervalInMs = POLL_INTERVAL_IN_MS, resumeFrom } = options || {};
1125
+ const { intervalInMs = POLL_INTERVAL_IN_MS, resumeFrom, resolveOnUnsuccessful = false, isDone, lroResourceLocationConfig, processResult, updateState, } = options || {};
1115
1126
  const state = resumeFrom
1116
1127
  ? deserializeState(resumeFrom)
1117
1128
  : {};
1118
- const operation = new GenericPollOperation(state, lro, options === null || options === void 0 ? void 0 : options.lroResourceLocationConfig, options === null || options === void 0 ? void 0 : options.processResult, options === null || options === void 0 ? void 0 : options.updateState, options === null || options === void 0 ? void 0 : options.isDone);
1129
+ const operation = new GenericPollOperation(state, lro, !resolveOnUnsuccessful, lroResourceLocationConfig, processResult, updateState, isDone);
1119
1130
  super(operation);
1131
+ this.resolveOnUnsuccessful = resolveOnUnsuccessful;
1120
1132
  this.config = { intervalInMs: intervalInMs };
1121
1133
  operation.setPollerConfig(this.config);
1122
1134
  }