@azure-rest/iot-device-update
Version:
Device Update for IoT Hub is an Azure service that enables customers to publish update for their IoT devices to the cloud, and then deploy that update to their devices (approve updates to groups of devices managed and provisioned in IoT Hub). It leverages
137 lines • 8.5 kB
JavaScript
;
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.isUnexpected = isUnexpected;
const responseMap = {
"GET /deviceUpdate/{instanceId}/updates": ["200"],
"GET /deviceUpdate/{instanceId}/updates:import": ["200", "202"],
"POST /deviceUpdate/{instanceId}/updates:import": ["200", "202"],
"GET /deviceUpdate/{instanceId}/updates/providers/{provider}/names/{name}/versions/{version}": [
"200",
"304",
],
"DELETE /deviceUpdate/{instanceId}/updates/providers/{provider}/names/{name}/versions/{version}": ["202"],
"GET /deviceUpdate/{instanceId}/updates/providers": ["200"],
"GET /deviceUpdate/{instanceId}/updates/providers/{provider}/names": ["200"],
"GET /deviceUpdate/{instanceId}/updates/providers/{provider}/names/{name}/versions": ["200"],
"GET /deviceUpdate/{instanceId}/updates/providers/{provider}/names/{name}/versions/{version}/files": ["200"],
"GET /deviceUpdate/{instanceId}/updates/providers/{provider}/names/{name}/versions/{version}/files/{fileId}": ["200", "304"],
"GET /deviceUpdate/{instanceId}/updates/operations": ["200"],
"GET /deviceUpdate/{instanceId}/updates/operations/{operationId}": ["200", "304"],
"GET /deviceUpdate/{instanceId}/management/deviceClasses": ["200"],
"GET /deviceUpdate/{instanceId}/management/deviceClasses/{deviceClassId}": ["200"],
"PATCH /deviceUpdate/{instanceId}/management/deviceClasses/{deviceClassId}": ["200"],
"DELETE /deviceUpdate/{instanceId}/management/deviceClasses/{deviceClassId}": ["204"],
"GET /deviceUpdate/{instanceId}/management/deviceClasses/{deviceClassId}/installableUpdates": [
"200",
],
"GET /deviceUpdate/{instanceId}/management/devices": ["200"],
"GET /deviceUpdate/{instanceId}/management/devices:import": ["202"],
"POST /deviceUpdate/{instanceId}/management/devices:import": ["202"],
"GET /deviceUpdate/{instanceId}/management/devices/{deviceId}": ["200"],
"GET /deviceUpdate/{instanceId}/management/devices/{deviceId}/modules/{moduleId}": ["200"],
"GET /deviceUpdate/{instanceId}/management/updateCompliance": ["200"],
"GET /deviceUpdate/{instanceId}/management/groups": ["200"],
"GET /deviceUpdate/{instanceId}/management/groups/{groupId}": ["200"],
"DELETE /deviceUpdate/{instanceId}/management/groups/{groupId}": ["204"],
"GET /deviceUpdate/{instanceId}/management/groups/{groupId}/updateCompliance": ["200"],
"GET /deviceUpdate/{instanceId}/management/groups/{groupId}/bestUpdates": ["200"],
"GET /deviceUpdate/{instanceId}/management/groups/{groupId}/deployments": ["200"],
"GET /deviceUpdate/{instanceId}/management/groups/{groupId}/deployments/{deploymentId}": ["200"],
"PUT /deviceUpdate/{instanceId}/management/groups/{groupId}/deployments/{deploymentId}": ["200"],
"DELETE /deviceUpdate/{instanceId}/management/groups/{groupId}/deployments/{deploymentId}": [
"204",
],
"GET /deviceUpdate/{instanceId}/management/groups/{groupId}/deployments/{deploymentId}/status": [
"200",
],
"GET /deviceUpdate/{instanceId}/management/groups/{groupId}/deviceClassSubgroups": ["200"],
"GET /deviceUpdate/{instanceId}/management/groups/{groupId}/deviceClassSubgroups/{deviceClassId}": ["200"],
"DELETE /deviceUpdate/{instanceId}/management/groups/{groupId}/deviceClassSubgroups/{deviceClassId}": ["204"],
"GET /deviceUpdate/{instanceId}/management/groups/{groupId}/deviceClassSubgroups/{deviceClassId}/updateCompliance": ["200"],
"GET /deviceUpdate/{instanceId}/management/groups/{groupId}/deviceClassSubgroups/{deviceClassId}/bestUpdates": ["200"],
"GET /deviceUpdate/{instanceId}/management/groups/{groupId}/deviceClassSubgroups/{deviceClassId}/deployments": ["200"],
"GET /deviceUpdate/{instanceId}/management/groups/{groupId}/deviceClassSubgroups/{deviceClassId}/deployments/{deploymentId}": ["200"],
"DELETE /deviceUpdate/{instanceId}/management/groups/{groupId}/deviceClassSubgroups/{deviceClassId}/deployments/{deploymentId}": ["204"],
"POST /deviceUpdate/{instanceId}/management/groups/{groupId}/deviceClassSubgroups/{deviceClassId}/deployments/{deploymentId}:cancel": ["200"],
"POST /deviceUpdate/{instanceId}/management/groups/{groupId}/deviceClassSubgroups/{deviceClassId}/deployments/{deploymentId}:retry": ["200"],
"GET /deviceUpdate/{instanceId}/management/groups/{groupId}/deviceClassSubgroups/{deviceClassId}/deployments/{deploymentId}/status": ["200"],
"GET /deviceUpdate/{instanceId}/management/groups/{groupId}/deviceClassSubgroups/{deviceClassId}/deployments/{deploymentId}/devicestates": ["200"],
"GET /deviceUpdate/{instanceId}/management/operations/{operationId}": ["200", "304"],
"GET /deviceUpdate/{instanceId}/management/operations": ["200"],
"PUT /deviceUpdate/{instanceId}/management/deviceDiagnostics/logCollections/{operationId}": [
"201",
],
"GET /deviceUpdate/{instanceId}/management/deviceDiagnostics/logCollections/{operationId}": [
"200",
],
"GET /deviceUpdate/{instanceId}/management/deviceDiagnostics/logCollections": ["200"],
"GET /deviceUpdate/{instanceId}/management/deviceDiagnostics/logCollections/{operationId}/detailedStatus": ["200"],
"GET /deviceUpdate/{instanceId}/management/deviceDiagnostics/deviceHealth": ["200"],
};
function isUnexpected(response) {
const lroOriginal = response.headers["x-ms-original-url"];
const url = new URL(lroOriginal !== null && lroOriginal !== void 0 ? lroOriginal : response.request.url);
const method = response.request.method;
let pathDetails = responseMap[`${method} ${url.pathname}`];
if (!pathDetails) {
pathDetails = getParametrizedPathSuccess(method, url.pathname);
}
return !pathDetails.includes(response.status);
}
function getParametrizedPathSuccess(method, path) {
var _a, _b, _c, _d;
const pathParts = path.split("/");
// Traverse list to match the longest candidate
// matchedLen: the length of candidate path
// matchedValue: the matched status code array
let matchedLen = -1, matchedValue = [];
// Iterate the responseMap to find a match
for (const [key, value] of Object.entries(responseMap)) {
// Extracting the path from the map key which is in format
// GET /path/foo
if (!key.startsWith(method)) {
continue;
}
const candidatePath = getPathFromMapKey(key);
// Get each part of the url path
const candidateParts = candidatePath.split("/");
// track if we have found a match to return the values found.
let found = true;
for (let i = candidateParts.length - 1, j = pathParts.length - 1; i >= 1 && j >= 1; i--, j--) {
if (((_a = candidateParts[i]) === null || _a === void 0 ? void 0 : _a.startsWith("{")) && ((_b = candidateParts[i]) === null || _b === void 0 ? void 0 : _b.indexOf("}")) !== -1) {
const start = candidateParts[i].indexOf("}") + 1, end = (_c = candidateParts[i]) === null || _c === void 0 ? void 0 : _c.length;
// If the current part of the candidate is a "template" part
// Try to use the suffix of pattern to match the path
// {guid} ==> $
// {guid}:export ==> :export$
const isMatched = new RegExp(`${(_d = candidateParts[i]) === null || _d === void 0 ? void 0 : _d.slice(start, end)}`).test(pathParts[j] || "");
if (!isMatched) {
found = false;
break;
}
continue;
}
// If the candidate part is not a template and
// the parts don't match mark the candidate as not found
// to move on with the next candidate path.
if (candidateParts[i] !== pathParts[j]) {
found = false;
break;
}
}
// We finished evaluating the current candidate parts
// Update the matched value if and only if we found the longer pattern
if (found && candidatePath.length > matchedLen) {
matchedLen = candidatePath.length;
matchedValue = value;
}
}
return matchedValue;
}
function getPathFromMapKey(mapKey) {
const pathStart = mapKey.indexOf("/");
return mapKey.slice(pathStart);
}
//# sourceMappingURL=isUnexpected.js.map