/
locationStrategy.ts
72 lines (60 loc) · 2.09 KB
/
locationStrategy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*
* Code generated by Microsoft (R) AutoRest Code Generator.
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
*/
import { BaseResult, LROOperationStep, LROStrategy, LROSYM } from "./models";
import { SendOperationFn } from "./lroPoller";
import { OperationSpec } from "@azure/core-http";
export function createLocationStrategy<TResult extends BaseResult>(
initialOperation: LROOperationStep<TResult>,
sendOperationFn: SendOperationFn<TResult>
): LROStrategy<TResult> {
const lroData = initialOperation.result._response[LROSYM];
if (!lroData) {
throw new Error("Expected lroData to be defined for Azure-AsyncOperation strategy");
}
let currentOperation = initialOperation;
let lastKnownPollingUrl = lroData.location;
return {
isTerminal: () => {
const currentResult = currentOperation.result._response[LROSYM];
if (!currentResult) {
throw new Error("Expected lroData to determine terminal status");
}
if (currentOperation === initialOperation) {
return false;
}
if (currentResult.statusCode === 202) {
return false;
}
return true;
},
sendFinalRequest: () => Promise.resolve(currentOperation),
poll: async () => {
if (!lastKnownPollingUrl) {
throw new Error("Unable to determine polling url");
}
const pollingArgs = currentOperation.args;
// Make sure we don't send any body to the get request
const { requestBody, ...restSpec } = currentOperation.spec;
const pollingSpec: OperationSpec = {
...restSpec,
httpMethod: "GET",
path: lastKnownPollingUrl
};
const result = await sendOperationFn(pollingArgs, pollingSpec);
// Update latest polling url
lastKnownPollingUrl = result._response[LROSYM]?.location || lastKnownPollingUrl;
// Update lastOperation result
currentOperation = {
args: pollingArgs,
spec: pollingSpec,
result
};
return currentOperation;
}
};
}