UNPKG

@iotize/cli

Version:
193 lines 8.33 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const typescript_rest_1 = require("typescript-rest"); const device_client_js_1 = require("@iotize/device-client.js"); const service_1 = require("@iotize/device-client.js/device/service"); const device_manager_1 = require("../../provider/device-manager"); const impl_1 = require("@iotize/device-client.js/client/impl"); const impl_2 = require("@iotize/device-client.js/client/impl"); const ENDPOINT = '/device/:deviceId/api'; let TapRestService = class TapRestService { constructor() { } runGetPath(data) { return __awaiter(this, void 0, void 0, function* () { return this._runPath(); }); } runPostPath(data) { return __awaiter(this, void 0, void 0, function* () { return this._runPath(); }); } runPutPath(data) { return __awaiter(this, void 0, void 0, function* () { return this._runPath(); }); } get lwm2mPath() { let request = this.context.request; if (!('0' in request.params)) { throw new typescript_rest_1.Errors.InternalServerError(`Missing lwm2m path in request`); } let lwm2mPath = `/${request.params['0']}`; return lwm2mPath; } getResourceCallFromRequest() { let lwm2mMethod = this.context.request.method.toLowerCase(); return getResourceCallFromPath(lwm2mMethod, this.lwm2mPath); } _runPath() { return __awaiter(this, void 0, void 0, function* () { console.log('===> path: ', this.context.request.path); console.log('===> Params: ', this.context.request.params); console.log('===> Body: ', this.context.request.body); let deviceId = this.context.request.params['deviceId']; let tap = device_manager_1.DeviceManager.get(deviceId); if (!tap.isConnected()) { yield tap.connect(); } let matches = this.getResourceCallFromRequest(); if (matches.length == 0) { throw new typescript_rest_1.Errors.NotFoundError(`Device api path "${this.lwm2mPath}" not found`); } else if (matches.length > 1) { throw new typescript_rest_1.Errors.InternalServerError(`Ambiguous path`); } let callOptions = matches[0]; let body = this.context.request.body; if (body && (Object.keys(body).length !== 0 || body.constructor !== Object)) { console.log('===> Added body to request: ', body); callOptions.body = body; } let response = yield tap.lwm2m.call(callOptions); if (!response.isSuccessful()) { throw new impl_2.ResponseError(response); // throw new Errors.BadRequestError( // new ResponseError(response).message // ) } return response.body(); }); } }; __decorate([ typescript_rest_1.Context, __metadata("design:type", typescript_rest_1.ServiceContext) ], TapRestService.prototype, "context", void 0); __decorate([ typescript_rest_1.Path('/*'), typescript_rest_1.GET, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], TapRestService.prototype, "runGetPath", null); __decorate([ typescript_rest_1.Path('/*'), typescript_rest_1.POST, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], TapRestService.prototype, "runPostPath", null); __decorate([ typescript_rest_1.Path('/*'), typescript_rest_1.PUT, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], TapRestService.prototype, "runPutPath", null); TapRestService = __decorate([ typescript_rest_1.Path(ENDPOINT), __metadata("design:paramtypes", []) ], TapRestService); exports.TapRestService = TapRestService; /** * path: /variable/4/value * model: /variable/{id}/value * out: { * id: 4 * } || false */ function getParamsMapFromPathAndModel(path, model) { let modelParts = model.split('/'); let givenParts = path.split('/'); let params = {}; if (modelParts.length != givenParts.length) { return false; } for (let i = 0; i < modelParts.length; i++) { // TODO test and finish let currentPartParam = impl_1.PathParameter.extractParams(modelParts[i]); if (currentPartParam.length > 0) { params[currentPartParam[0]] = givenParts[i]; } else if (givenParts[i] == modelParts[i]) { continue; } else { return false; } } // return PathParameter.fillAllParams(value.path, parameterValues) return params; } function getResourceCallFromPath(method, path) { let tap = device_client_js_1.Tap.create(); // if (method != "get" && method != "put" && method != "post") { // throw new Error(`Illegal argument method: "${method}"`) // } let serviceNames = Object.keys(tap.service); let matches = []; for (let serviceName of serviceNames) { // console.log(`Sanning service: ${serviceName}`) let service = tap.service[serviceName]; if (service instanceof service_1.AbstractService) { let resources = service .resources; let resourcesArray = Object.keys(resources).map(key => resources[key]); let match = resourcesArray.filter((value) => { // console.log(` - Sanning resource: ${value.path}`) if (value.methodType != method) { // console.log(` - FOUND resource: ${JSON.stringify(value)}`) return false; } let pathParameters = impl_1.PathParameter.extractParams(value.path); if (pathParameters.length > 0) { let map = getParamsMapFromPathAndModel(path, value.path); if (map) { value.pathParameters = map; return true; } else { return false; } } else { return value.path == path; } }); // console.log('Added match: ', match) matches = matches.concat(match); } } return matches; } //# sourceMappingURL=tap-rest.service.js.map