@63klabs/cache-data 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,186 @@
1
+ /*
2
+ * =============================================================================
3
+ * Endpoint request class. DAO/Template
4
+ * -----------------------------------------------------------------------------
5
+ *
6
+ * Barebones API request to an endpoint. Can also be used as a template to
7
+ * create additional DAO object classes.
8
+ *
9
+ * The class can be used as a template and modified to provide additional
10
+ * logic, query, filtering, and manipulation before/after data is sent/received
11
+ * via the APIRequest class.
12
+ *
13
+ * The class itself is not exposed, instead various functions can be used
14
+ * to access the class. For exmaple, getDataDirectFromURI(connection, data)
15
+ *
16
+ * The connection parameter is used to pass connection information to the
17
+ * API (host, path, query, etc).
18
+ *
19
+ * The data parameter is optional and can be left off. However, it can be used
20
+ * to pass additional information to the class to perform before/after logic.
21
+ *
22
+ * @example
23
+ * // access function that utilizes the class
24
+ * const getDataDirectFromURI = async (connection, data = null) => {
25
+ * return (new Endpoint(connection).get());
26
+ * };
27
+ */
28
+
29
+ /*
30
+ * -----------------------------------------------------------------------------
31
+ * Object definitions
32
+ * -----------------------------------------------------------------------------
33
+ */
34
+
35
+ /**
36
+ * @typedef ConnectionObject
37
+ * @property {Object} connection
38
+ * @property {string} connection.method
39
+ * @property {string} connection.uri
40
+ * @property {string} connection.protocol http or https
41
+ * @property {string} connection.host
42
+ * @property {string} connection.path
43
+ * @property {string} connection.body
44
+ * @property {object} connection.parameters
45
+ * @property {object} connection.headers
46
+ * @property {object} connection.options
47
+ * @property {number} connection.options.timeout
48
+ * @property {string} connection.note
49
+ */
50
+
51
+ /*
52
+ * -----------------------------------------------------------------------------
53
+ */
54
+
55
+ "use strict";
56
+
57
+ const tools = require("./tools/index.js");
58
+
59
+ /**
60
+ *
61
+ * @param {ConnectionObject} connection An object with details about the connection (method, uri, host, etc)
62
+ * @param {*} data Additional data to perform a query for the request, or transformation of the response within the DAO object. This data is not directly sent to the endpoint. It is used within the DAO object to transform the request and/or response. Any data sent to the endpoint should be in the connection or handled within the DAO
63
+ * @returns {object} The response
64
+ */
65
+ const getDataDirectFromURI = async (connection, data = null) => {
66
+ return (new Endpoint(connection).get());
67
+ };
68
+
69
+ /**
70
+ * A bare bones request to an endpoint. Can be used as a template to
71
+ * create more elaboarate requests.
72
+ */
73
+ class Endpoint {
74
+
75
+ /**
76
+ *
77
+ * @param {ConnectionObject} connection An object with connection data
78
+ */
79
+ constructor(connection) {
80
+
81
+ this.response = null;
82
+
83
+ this.request = {
84
+ method: this._setRequestSetting(connection, "method", "GET"),
85
+ uri: this._setRequestSetting(connection, "uri", ""),
86
+ protocol: this._setRequestSetting(connection, "protocol", "https"),
87
+ host: this._setRequestSetting(connection, "host", ""),
88
+ path: this._setRequestSetting(connection, "path", ""),
89
+ body: this._setRequestSetting(connection, "body", null),
90
+ note: this._setRequestSetting(connection, "note", "Get data from endpoint"),
91
+ parameters: this._setRequestSetting(connection, "parameters", null),
92
+ headers: this._setRequestSetting(connection, "headers", null),
93
+ options: this._setRequestSetting(connection, "options", null)
94
+ };
95
+ };
96
+
97
+ /**
98
+ * Takes the connection object, checks for the key provided and if the key
99
+ * exists it returns its value. Otherwise it returns the default value.
100
+ * @param {ConnectionObject} connection The connection object to check for the existence of a key
101
+ * @param {string} key The key to check for and return the value from connection
102
+ * @param {*} defaultValue The value to use if the key is not found in the connection object
103
+ * @returns {*} Either the value of the key if found in the connection object, or the default value
104
+ */
105
+ _setRequestSetting(connection, key, defaultValue) {
106
+ if (!(key in connection)) {
107
+ connection[key] = defaultValue;
108
+ }
109
+
110
+ return connection[key];
111
+ };
112
+
113
+ /**
114
+ * This is the function used by the accessor method after the constructor
115
+ * is called.
116
+ *
117
+ * As a template, it can be modified to perform additional checks,
118
+ * operations, etc before or after sending the call.
119
+ *
120
+ * @example
121
+ * // access function that utilizes the class
122
+ * const getDataDirectFromURI = async (connection, data = null) => {
123
+ * return (new Endpoint(connection).get());
124
+ * };
125
+ * @returns {object} Response data from the completed request
126
+ */
127
+ async get() {
128
+
129
+ if (this.response === null) {
130
+
131
+ // send the call
132
+ try {
133
+
134
+ tools.DebugAndLog.debug("Sending call", this.request);
135
+ this.response = await this._call();
136
+
137
+ // if it is not JSON we don't convert
138
+ try {
139
+
140
+ let body = null;
141
+
142
+ if ( this.response.body !== "" && this.response.body !== null ) {
143
+ body = JSON.parse(this.response.body);
144
+ }
145
+
146
+ this.response.body = body;
147
+
148
+ } catch (error) {
149
+ tools.DebugAndLog.debug("This isn't JSON so we'll keep as text and do nothing. This isn't a true error.");
150
+ }
151
+
152
+ } catch (error) {
153
+ tools.DebugAndLog.error(`Error in call to remote endpoint (${this.request.note}): ${error.message}`, error.stack);
154
+ }
155
+
156
+ }
157
+
158
+ return this.response;
159
+ }
160
+
161
+ /**
162
+ * An internal function that actually makes the call to APIRequest class
163
+ * @returns {object} Response data from the completed request
164
+ */
165
+ async _call() {
166
+
167
+ var response = null;
168
+
169
+ try {
170
+ var apiRequest = new tools.APIRequest(this.request);
171
+ response = await apiRequest.send();
172
+
173
+ } catch (error) {
174
+ tools.DebugAndLog.error(`Error in call (${this.request.note}): ${error.message}`, error.stack);
175
+ response = tools.APIRequest.responseFormat(false, 500, "Error in call()");
176
+ }
177
+
178
+ return response;
179
+
180
+ };
181
+
182
+ };
183
+
184
+ module.exports = {
185
+ getDataDirectFromURI
186
+ };