@azure/maps-common
Version:
An internal shared package for Azure Maps TypeScript SDK
90 lines (84 loc) • 3.29 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
const API_KEY_HEADER_NAME = "subscription-key";
/**
* The programmatic identifier of the mapsAzureKeyCredentialPolicy.
*/
const azureMapsKeyCredentialPolicyName = "mapsAzureKeyCredentialPolicy";
/**
* Create an HTTP pipeline policy to authenticate a request
* using an `AzureKeyCredential` for Azure Maps
*/
function createAzureMapsKeyCredentialPolicy(azureKeyCredential) {
return {
name: azureMapsKeyCredentialPolicyName,
async sendRequest(request, next) {
if (!request.headers.has(API_KEY_HEADER_NAME)) {
request.headers.set(API_KEY_HEADER_NAME, azureKeyCredential.key);
}
return next(request);
},
};
}
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
const MAPS_CLIENT_ID_HEADER_NAME = "x-ms-client-id";
/**
* The programmatic identifier of the mapsTokenCredentialPolicy.
*/
const mapsClientIdPolicyName = "mapsClientIdPolicy";
/**
* Create an HTTP pipeline policy to add x-ms-client-id header
* for `TokenCredential` based authentication for Azure Maps
*/
function createMapsClientIdPolicy(mapsClientId) {
return {
name: mapsClientIdPolicyName,
async sendRequest(request, next) {
if (!request.headers.has(MAPS_CLIENT_ID_HEADER_NAME)) {
request.headers.set(MAPS_CLIENT_ID_HEADER_NAME, mapsClientId);
}
return next(request);
},
};
}
/**
* Extract several fields of the response to the rawResponse
*
* @param getResponse - A async function that actually call the backend API.
* @param options - The options for the getResponse callback
* @returns A promise for the API call.
*/
async function getRawResponse(getResponse, options) {
const { onResponse } = options || {};
let rawResponse;
const flatResponse = await getResponse(Object.assign(Object.assign({}, options), { onResponse: (response, flatResponseParam) => {
rawResponse = response;
onResponse === null || onResponse === void 0 ? void 0 : onResponse(response, flatResponseParam);
} }));
return {
flatResponse,
rawResponse: {
statusCode: rawResponse.status,
headers: rawResponse.headers.toJSON(),
body: rawResponse.parsedBody,
},
};
}
/**
* Helper function to create a method that can be passed to sendPollRequest in createHttpPoller.
*
* @param settings - The settings of the poll request, including client, options and the spec
* @returns A callback that accept the path as input and return the promise of Lro response.
*/
function createSendPollRequest(settings) {
const { client, options, spec } = settings;
return async (path) => getRawResponse((paramOptions) => client.sendOperationRequest({ options: paramOptions }, Object.assign({ path }, spec)), options);
}
exports.createAzureMapsKeyCredentialPolicy = createAzureMapsKeyCredentialPolicy;
exports.createMapsClientIdPolicy = createMapsClientIdPolicy;
exports.createSendPollRequest = createSendPollRequest;
exports.getRawResponse = getRawResponse;
//# sourceMappingURL=index.js.map