Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
aluisiora committed Feb 28, 2018
2 parents 9953616 + 0f20587 commit 3eea709
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "routeros-client",
"version": "0.6.0",
"version": "0.6.1",
"description": "Easy to use abstraction layer over the node-routeros API",
"main": "./dist/index",
"types": "./dist/index",
Expand Down
65 changes: 52 additions & 13 deletions src/RosApiCrud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export abstract class RouterOSAPICrud {

protected snakeCase: boolean;

private needsObjectTranslation: boolean = false;

/**
* Creates a CRUD set of operations and handle
* the raw query to input on the raw API
Expand Down Expand Up @@ -66,7 +68,10 @@ export abstract class RouterOSAPICrud {
* @param ids the id(s) or number(s) to disable
*/
public disable(ids?: Types.Id): Types.SocPromise {
if (ids) this.queryVal.push("=numbers=" + ids);
if (ids) {
ids = this.stringfySearchQuery(ids);
this.queryVal.push("=numbers=" + ids);
}
return this.exec("disable");
}

Expand All @@ -76,7 +81,10 @@ export abstract class RouterOSAPICrud {
* @param ids the id(s) or number(s) to delete
*/
public delete(ids?: Types.Id): Types.SocPromise {
if (ids) this.queryVal.push("=numbers=" + ids);
if (ids) {
ids = this.stringfySearchQuery(ids);
this.queryVal.push("=numbers=" + ids);
}
return this.remove(ids);
}

Expand All @@ -86,7 +94,10 @@ export abstract class RouterOSAPICrud {
* @param ids the id(s) or number(s) to enable
*/
public enable(ids?: Types.Id): Types.SocPromise {
if (ids) this.queryVal.push("=numbers=" + ids);
if (ids) {
ids = this.stringfySearchQuery(ids);
this.queryVal.push("=numbers=" + ids);
}
return this.exec("enable");
}

Expand All @@ -112,8 +123,12 @@ export abstract class RouterOSAPICrud {
*/
public move(from: Types.Id, to?: string | number): Types.SocPromise {
if (!Array.isArray(from)) from = [from];
from = this.stringfySearchQuery(from);
this.queryVal.push("=numbers=" + from);
if (to) this.queryVal.push("=destination=" + to);
if (to) {
to = this.stringfySearchQuery(to);
this.queryVal.push("=destination=" + to);
}
return this.exec("move");
}

Expand All @@ -124,7 +139,10 @@ export abstract class RouterOSAPICrud {
* @param ids optional id(s) of the rules
*/
public update(data: object, ids?: Types.Id): Types.SocPromise {
if (ids) this.queryVal.push("=numbers=" + ids);
if (ids) {
ids = this.stringfySearchQuery(ids);
this.queryVal.push("=numbers=" + ids);
}
this.makeQuery(data);
return this.exec("set");
}
Expand All @@ -136,7 +154,10 @@ export abstract class RouterOSAPICrud {
* @param ids the id(s) of the entries to unset the property(ies)
*/
public unset(properties: string | string[], ids?: Types.Id): Types.SocPromise {
if (ids) this.queryVal.push("=numbers=" + ids);
if (ids) {
ids = this.stringfySearchQuery(ids);
this.queryVal.push("=numbers=" + ids);
}
if (typeof properties === "string") properties = [properties];
const $q: Types.SocPromise[] = [];
const curQueryVal = this.queryVal.slice();
Expand All @@ -156,7 +177,7 @@ export abstract class RouterOSAPICrud {
*/
public remove(ids?: any): Types.SocPromise {
if (ids) {
ids = this.stringfyIdSearchQuery(ids);
ids = this.stringfySearchQuery(ids);
this.queryVal.push("=numbers=" + ids);
}
return this.exec("remove");
Expand Down Expand Up @@ -253,7 +274,7 @@ export abstract class RouterOSAPICrud {
} else if (tmpVal === null) {
tmpVal = "";
} else if (typeof tmpVal === "object") {
tmpVal = this.stringfyIdSearchQuery(tmpVal);
tmpVal = this.stringfySearchQuery(tmpVal);
}

tmpKey = (addQuestionMark ? "?" : "=") + tmpKey;
Expand Down Expand Up @@ -282,8 +303,17 @@ export abstract class RouterOSAPICrud {
});
}

/**
* Translates .id, place-before and number without using internal
* mikrotik id (something like *4A).
*
* This should check if one of those parameters are an object
* and use that object to search the real id of the item.
*
* @param queries query array
*/
protected translateQueryIntoId(queries: string[]): Promise<any> {
if (queries.length === 0) return Promise.resolve(queries);
if (queries.length === 0 || !this.needsObjectTranslation) return Promise.resolve(queries);

const promises = [];
const consultedIndexes = [];
Expand Down Expand Up @@ -312,6 +342,7 @@ export abstract class RouterOSAPICrud {
const consulted = consultedIndexes.shift();
queries[consulted.index] = "=" + consulted.key + "=" + result[".id"];
}
this.needsObjectTranslation = false;
return Promise.resolve(queries);
});
}
Expand Down Expand Up @@ -349,16 +380,24 @@ export abstract class RouterOSAPICrud {
return treatedArr;
}

private stringfyIdSearchQuery(items: any): any {
/**
* Stringify a json formated object to be used later
* for a translation
*
* @param items object items to stringfy
*/
private stringfySearchQuery(items: any): any {
let isArray = true;
const newItems = [];
if (!Array.isArray(items)) {
isArray = false;
items = [items];
}
}
for (const item of items) {
if (typeof item === "object") newItems.push(JSON.stringify(item));
else newItems.push(item);
if (typeof item === "object") {
this.needsObjectTranslation = true;
newItems.push(JSON.stringify(item));
} else newItems.push(item);
}
return isArray ? newItems : newItems.shift();
}
Expand Down

0 comments on commit 3eea709

Please sign in to comment.