-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathABObjectApiCore.js
More file actions
128 lines (102 loc) · 3.34 KB
/
ABObjectApiCore.js
File metadata and controls
128 lines (102 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const ABObject = require("../platform/ABObject");
const ABModelApi = require("../platform/ABModelApi");
module.exports = class ABObjectApiCore extends ABObject {
constructor(attributes, AB) {
super(attributes, AB);
this.isAPI = true;
this.fromValues(attributes);
}
///
/// Instance Methods
///
/// ABApplication data methods
fromValues(attributes) {
super.fromValues(attributes);
this.readonly = parseInt(attributes.readonly) || 0;
this.apiType = attributes.apiType || "Read";
this.request = attributes.request ?? {};
this.request.headers = attributes.request?.headers ?? [];
this.response = attributes.response ?? {};
this.response.fields = attributes.response?.fields ?? [];
this.isFetched = attributes.isFetched ?? false;
}
/**
* @method toObj()
*
* properly compile the current state of this ABObjectQuery instance
* into the values needed for saving to the DB.
*
* @return {json}
*/
toObj() {
const result = super.toObj();
result.isAPI = this.isAPI;
result.readonly = this.readonly;
result.apiType = this.apiType;
result.request = this.request ?? {};
result.request.headers = this.request?.headers ?? [];
result.response = this.response ?? {};
result.response.fields = this.response?.fields ?? [];
result.isFetched = this.isFetched;
return result;
}
/**
* @method model
* return a Model object that will allow you to interact with the data for
* this ABObjectQuery.
*/
model() {
var model = new ABModelApi(this);
// default the context of this model's operations to this object
model.contextKey(this.constructor.contextKey());
model.contextValues({ id: this.id }); // the datacollection.id
return model;
}
/**
* @function getPagingValues()
*
* @return {Object} - {
* start: "Property name of the API for start index",
* limit: "Property name of the API for limit return the item number"
* }
*/
getPagingValues({ skip, limit }) {
const result = {};
const pagingSettings = this.request?.paging ?? {};
if (pagingSettings.start && skip != null) {
result[pagingSettings.start] = skip;
}
if (pagingSettings.limit && limit != null) {
result[pagingSettings.limit] = limit;
}
return result;
}
dataFromKey(data) {
let result = [];
if (!Array.isArray(data)) data = [data];
data.forEach((item) => {
// Clone item
let itemResult = { ...item };
// Pull data from `Data key` of the API object
// FORMAT: "Property.Name.Value"
(this.response.dataKey ?? "").split(".").forEach((key) => {
if (key == "" || key == null) return;
itemResult = itemResult?.[key];
});
if (Array.isArray(itemResult)) {
result = result.concat(itemResult);
} else if (itemResult) {
result.push(itemResult);
}
});
return result;
}
get headers() {
const headers = {};
(this.request.headers ?? []).forEach((header) => {
if (header?.value == null) return;
headers[header.key] = header.value;
});
return headers;
}
};