|
6 | 6 | * Changes may cause incorrect behavior and will be lost if the code is regenerated. |
7 | 7 | */ |
8 | 8 |
|
| 9 | +// Copyright (c) Microsoft Corporation. |
| 10 | +// Licensed under the MIT license. |
| 11 | + |
| 12 | +import { AbortSignalLike } from "@azure/abort-controller"; |
9 | 13 | import { LongRunningOperation, LroResponse } from "@azure/core-lro"; |
10 | 14 |
|
11 | | -export class LroImpl<T> implements LongRunningOperation<T> { |
12 | | - constructor( |
13 | | - private sendOperationFn: (args: any, spec: any) => Promise<LroResponse<T>>, |
14 | | - private args: Record<string, unknown>, |
15 | | - private spec: { |
16 | | - readonly requestBody?: unknown; |
17 | | - readonly path?: string; |
18 | | - readonly httpMethod: string; |
19 | | - } & Record<string, any>, |
20 | | - public requestPath: string = spec.path!, |
21 | | - public requestMethod: string = spec.httpMethod |
22 | | - ) {} |
23 | | - public async sendInitialRequest(): Promise<LroResponse<T>> { |
24 | | - return this.sendOperationFn(this.args, this.spec); |
25 | | - } |
26 | | - public async sendPollRequest(path: string): Promise<LroResponse<T>> { |
27 | | - const { requestBody, ...restSpec } = this.spec; |
28 | | - return this.sendOperationFn(this.args, { |
29 | | - ...restSpec, |
30 | | - path, |
31 | | - httpMethod: "GET" |
32 | | - }); |
33 | | - } |
| 15 | +export function createLroSpec<T>(inputs: { |
| 16 | + sendOperationFn: (args: any, spec: any) => Promise<LroResponse<T>>; |
| 17 | + args: Record<string, unknown>; |
| 18 | + spec: { |
| 19 | + readonly requestBody?: unknown; |
| 20 | + readonly path?: string; |
| 21 | + readonly httpMethod: string; |
| 22 | + } & Record<string, any>; |
| 23 | +}): LongRunningOperation<T> { |
| 24 | + const { args, spec, sendOperationFn } = inputs; |
| 25 | + return { |
| 26 | + requestMethod: spec.httpMethod, |
| 27 | + requestPath: spec.path!, |
| 28 | + sendInitialRequest: () => sendOperationFn(args, spec), |
| 29 | + sendPollRequest: ( |
| 30 | + path: string, |
| 31 | + options?: { abortSignal?: AbortSignalLike } |
| 32 | + ) => { |
| 33 | + const { requestBody, ...restSpec } = spec; |
| 34 | + return sendOperationFn(args, { |
| 35 | + ...restSpec, |
| 36 | + httpMethod: "GET", |
| 37 | + path, |
| 38 | + abortSignal: options?.abortSignal |
| 39 | + }); |
| 40 | + } |
| 41 | + }; |
34 | 42 | } |
0 commit comments