UNPKG

node-hue-api

Version:
113 lines (112 loc) 4.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.resourceLinksApi = void 0; const hue_bridge_model_1 = require("@peter-murray/hue-bridge-model"); const util_1 = require("../../../util"); const ResourceLinkPlaceholder_1 = require("../../placeholders/ResourceLinkPlaceholder"); const ApiEndpoint_1 = require("./ApiEndpoint"); const ApiError_1 = require("../../../ApiError"); const instanceChecks = hue_bridge_model_1.model.instanceChecks; const RESOURCELINK_PLACEHOLDER = new ResourceLinkPlaceholder_1.ResourceLinkPlaceholder(); const resourceLinksApi = { getAll: new ApiEndpoint_1.ApiEndpoint() .get() .uri('/<username>/resourcelinks') .acceptJson() .pureJson() .postProcess(buildResourceLinkResults), getResourceLink: new ApiEndpoint_1.ApiEndpoint() .get() .uri('/<username>/resourcelinks/<id>') .placeholder(RESOURCELINK_PLACEHOLDER) .acceptJson() .pureJson() .postProcess(buildResourceLink), createResourceLink: new ApiEndpoint_1.ApiEndpoint() .post() .uri('/<username>/resourcelinks') .payload(createResourceLinkPayload) .acceptJson() .pureJson() .postProcess(buildCreateResourceLinkResult), updateResourceLink: new ApiEndpoint_1.ApiEndpoint() .put() .uri('/<username>/resourcelinks/<id>') .placeholder(RESOURCELINK_PLACEHOLDER) .payload(buildResourceLinkUpdatePayload) .acceptJson() .pureJson() .postProcess(util_1.extractUpdatedAttributes), deleteResourceLink: new ApiEndpoint_1.ApiEndpoint() .delete() .uri('/<username>/resourcelinks/<id>') .placeholder(RESOURCELINK_PLACEHOLDER) .acceptJson() .pureJson() .postProcess(util_1.wasSuccessful) }; exports.resourceLinksApi = resourceLinksApi; function buildResourceLinkResults(data) { const resourceLinks = []; if (data) { Object.keys(data).forEach(id => { resourceLinks.push(hue_bridge_model_1.model.createFromBridge('resourcelink', id, data[id])); }); } return resourceLinks; } function buildResourceLink(data, requestParameters) { if (data) { const id = RESOURCELINK_PLACEHOLDER.getValue(requestParameters); return hue_bridge_model_1.model.createFromBridge('resourcelink', id, data); } return null; } function buildCreateResourceLinkResult(result) { const hueErrors = (0, util_1.parseErrors)(result); //TODO not sure if this still gets called as the request handles some of this if (hueErrors) { throw new ApiError_1.ApiError(`Error creating resourcelink: ${hueErrors[0].description}`, hueErrors[0]); } return { id: Number(result[0].success.id) }; } function buildResourceLinkUpdatePayload(parameters) { const resourceLink = parameters.resourceLink; if (!resourceLink) { throw new ApiError_1.ApiError('No ResourceLink provided'); } else if (!instanceChecks.isResourceLinkInstance(resourceLink)) { throw new ApiError_1.ApiError('Must provide a valid ResourceLink object'); } const body = buildResourceLinkBody(resourceLink); // Cannot change the owner, type or recycle values delete body.type; delete body.recycle; delete body.owner; return { type: 'application/json', body: body }; } function createResourceLinkPayload(parameters) { const resourceLink = parameters.resourceLink; if (!resourceLink) { throw new ApiError_1.ApiError('No ResourceLink provided'); } else if (!instanceChecks.isResourceLinkInstance(resourceLink)) { throw new ApiError_1.ApiError('Must provide a valid ResourceLink object'); } const body = buildResourceLinkBody(resourceLink); if (!body.links || body.links.length === 0) { throw new ApiError_1.ApiError('You must provide a ResourceLink with some links defined'); } return { type: 'application/json', body: body }; } function buildResourceLinkBody(resourceLink) { const data = resourceLink.getHuePayload(); // Cannot have an ID in the create payload delete data.id; return data; }