@azure/app-configuration
Version:
An isomorphic client library for the Azure App Configuration service.
95 lines • 3.97 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { createHttpPoller, } from "@azure/core-lro";
import { createRestError } from "@azure-rest/core-client";
export function getLongRunningPoller(client, processResponseBody, expectedStatuses, options) {
const { restoreFrom, getInitialResponse, apiVersion } = options;
if (!restoreFrom && !getInitialResponse) {
throw new Error("Either restoreFrom or getInitialResponse must be specified");
}
let initialResponse = undefined;
const pollAbortController = new AbortController();
const poller = {
sendInitialRequest: async () => {
if (!getInitialResponse) {
throw new Error("getInitialResponse is required when initializing a new poller");
}
initialResponse = await getInitialResponse();
return getLroResponse(initialResponse, expectedStatuses);
},
sendPollRequest: async (path, pollOptions) => {
// The poll request would both listen to the user provided abort signal and the poller's own abort signal
function abortListener() {
pollAbortController.abort();
}
const abortSignal = pollAbortController.signal;
if (options.abortSignal?.aborted) {
pollAbortController.abort();
}
else if (pollOptions?.abortSignal?.aborted) {
pollAbortController.abort();
}
else if (!abortSignal.aborted) {
options.abortSignal?.addEventListener("abort", abortListener, {
once: true,
});
pollOptions?.abortSignal?.addEventListener("abort", abortListener, {
once: true,
});
}
let response;
try {
const pollingPath = apiVersion ? addApiVersionToUrl(path, apiVersion) : path;
response = await client.pathUnchecked(pollingPath).get({ abortSignal });
}
finally {
options.abortSignal?.removeEventListener("abort", abortListener);
pollOptions?.abortSignal?.removeEventListener("abort", abortListener);
}
return getLroResponse(response, expectedStatuses);
},
};
return createHttpPoller(poller, {
intervalInMs: options?.updateIntervalInMs,
resourceLocationConfig: options?.resourceLocationConfig,
restoreFrom: options?.restoreFrom,
processResult: (result) => {
return processResponseBody(result);
},
});
}
/**
* Converts a Rest Client response to a response that the LRO implementation understands
* @param response - a rest client http response
* @param deserializeFn - deserialize function to convert Rest response to modular output
* @returns - An LRO response that the LRO implementation understands
*/
function getLroResponse(response, expectedStatuses) {
if (!expectedStatuses.includes(response.status)) {
throw createRestError(response);
}
return {
flatResponse: response,
rawResponse: {
...response,
statusCode: Number.parseInt(response.status),
body: response.body,
},
};
}
/**
* Adds the api-version query parameter on a URL if it's not present.
* @param url - the URL to modify
* @param apiVersion - the API version to set
* @returns - the URL with the api-version query parameter set
*/
function addApiVersionToUrl(url, apiVersion) {
// The base URL is only used for parsing and won't appear in the returned URL
const urlObj = new URL(url, "https://microsoft.com");
if (!urlObj.searchParams.get("api-version")) {
// Append one if there is no apiVersion
return `${url}${urlObj.search ? "&" : "?"}api-version=${apiVersion}`;
}
return url;
}
//# sourceMappingURL=pollingHelpers.js.map