@kaspernj/api-maker 1.0.347 → 1.0.348
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.
- package/package.json +1 -1
- package/src/api.mjs +7 -0
- package/src/commands-pool.mjs +12 -12
- package/src/logger.mjs +7 -1
- package/src/session-status-updater.mjs +3 -3
package/package.json
CHANGED
package/src/api.mjs
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import config from "./config.mjs"
|
|
2
2
|
import CustomError from "./custom-error.mjs"
|
|
3
3
|
import FormDataObjectizer from "form-data-objectizer"
|
|
4
|
+
import Logger from "./logger.mjs"
|
|
4
5
|
import qs from "qs"
|
|
5
6
|
import SessionStatusUpdater from "./session-status-updater.mjs"
|
|
6
7
|
|
|
8
|
+
const logger = new Logger({name: "ApiMaker / Api"})
|
|
9
|
+
|
|
10
|
+
// logger.setDebug(true)
|
|
11
|
+
|
|
7
12
|
export default class Api {
|
|
8
13
|
static get(path, pathParams = null) {
|
|
9
14
|
return Api.requestLocal({path, pathParams, method: "GET"})
|
|
@@ -70,6 +75,8 @@ export default class Api {
|
|
|
70
75
|
|
|
71
76
|
const token = await this._token()
|
|
72
77
|
|
|
78
|
+
logger.debug(() => `Got token: ${token}`)
|
|
79
|
+
|
|
73
80
|
if (token) {
|
|
74
81
|
args.headers["X-CSRF-Token"] = token
|
|
75
82
|
}
|
package/src/commands-pool.mjs
CHANGED
|
@@ -14,7 +14,7 @@ import {ValidationErrors} from "./validation-errors.mjs"
|
|
|
14
14
|
const shared = {}
|
|
15
15
|
|
|
16
16
|
export default class ApiMakerCommandsPool {
|
|
17
|
-
static addCommand
|
|
17
|
+
static addCommand(data, args = {}) {
|
|
18
18
|
let pool
|
|
19
19
|
|
|
20
20
|
if (args.instant) {
|
|
@@ -34,17 +34,17 @@ export default class ApiMakerCommandsPool {
|
|
|
34
34
|
return promiseResult
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
static current
|
|
37
|
+
static current() {
|
|
38
38
|
if (!shared.currentApiMakerCommandsPool) shared.currentApiMakerCommandsPool = new ApiMakerCommandsPool()
|
|
39
39
|
|
|
40
40
|
return shared.currentApiMakerCommandsPool
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
static flush
|
|
43
|
+
static flush() {
|
|
44
44
|
ApiMakerCommandsPool.current().flush()
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
constructor
|
|
47
|
+
constructor() {
|
|
48
48
|
this.flushCount = 0
|
|
49
49
|
this.pool = {}
|
|
50
50
|
this.poolData = {}
|
|
@@ -52,7 +52,7 @@ export default class ApiMakerCommandsPool {
|
|
|
52
52
|
this.globalRequestData = {}
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
addCommand
|
|
55
|
+
addCommand(data) {
|
|
56
56
|
return new Promise((resolve, reject) => {
|
|
57
57
|
const id = this.currentId
|
|
58
58
|
this.currentId += 1
|
|
@@ -87,11 +87,11 @@ export default class ApiMakerCommandsPool {
|
|
|
87
87
|
})
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
commandsCount
|
|
90
|
+
commandsCount() {
|
|
91
91
|
return Object.keys(this.pool)
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
async sendRequest
|
|
94
|
+
async sendRequest({commandSubmitData, url}) {
|
|
95
95
|
let response
|
|
96
96
|
|
|
97
97
|
for (let i = 0; i < 3; i++) {
|
|
@@ -113,7 +113,7 @@ export default class ApiMakerCommandsPool {
|
|
|
113
113
|
throw new Error("Couldnt successfully execute request")
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
-
async flush
|
|
116
|
+
async flush() {
|
|
117
117
|
if (this.commandsCount() == 0) {
|
|
118
118
|
return
|
|
119
119
|
}
|
|
@@ -166,7 +166,7 @@ export default class ApiMakerCommandsPool {
|
|
|
166
166
|
}
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
-
handleFailedResponse
|
|
169
|
+
handleFailedResponse(commandData, commandResponseData) {
|
|
170
170
|
let error
|
|
171
171
|
|
|
172
172
|
if (commandResponseData.error_type == "destroy_error") {
|
|
@@ -190,13 +190,13 @@ export default class ApiMakerCommandsPool {
|
|
|
190
190
|
commandData.reject(error)
|
|
191
191
|
}
|
|
192
192
|
|
|
193
|
-
clearTimeout
|
|
193
|
+
clearTimeout() {
|
|
194
194
|
if (this.flushTimeout) {
|
|
195
195
|
clearTimeout(this.flushTimeout)
|
|
196
196
|
}
|
|
197
197
|
}
|
|
198
198
|
|
|
199
|
-
isActive
|
|
199
|
+
isActive() {
|
|
200
200
|
if (this.commandsCount() > 0) {
|
|
201
201
|
return true
|
|
202
202
|
}
|
|
@@ -208,7 +208,7 @@ export default class ApiMakerCommandsPool {
|
|
|
208
208
|
return false
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
-
setFlushTimeout
|
|
211
|
+
setFlushTimeout() {
|
|
212
212
|
this.clearTimeout()
|
|
213
213
|
this.flushTimeout = setTimeout(() => this.flush(), 0)
|
|
214
214
|
}
|
package/src/logger.mjs
CHANGED
|
@@ -37,7 +37,7 @@ export default class ApiMakerSessionStatusUpdater {
|
|
|
37
37
|
|
|
38
38
|
async getCsrfToken() {
|
|
39
39
|
if (this.csrfToken) {
|
|
40
|
-
logger.debug(
|
|
40
|
+
logger.debug(`Get CSRF token from set variable: ${this.csrfToken}`)
|
|
41
41
|
|
|
42
42
|
return this.csrfToken
|
|
43
43
|
}
|
|
@@ -46,7 +46,7 @@ export default class ApiMakerSessionStatusUpdater {
|
|
|
46
46
|
const csrfTokenElement = document.querySelector("meta[name='csrf-token']")
|
|
47
47
|
|
|
48
48
|
if (csrfTokenElement) {
|
|
49
|
-
logger.debug(
|
|
49
|
+
logger.debug(() => `Get CSRF token from meta element: ${csrfTokenElement.getAttribute("content")}`)
|
|
50
50
|
|
|
51
51
|
this.csrfToken = csrfTokenElement.getAttribute("content")
|
|
52
52
|
|
|
@@ -58,7 +58,7 @@ export default class ApiMakerSessionStatusUpdater {
|
|
|
58
58
|
await this.updateSessionStatus()
|
|
59
59
|
|
|
60
60
|
if (this.csrfToken) {
|
|
61
|
-
logger.debug(
|
|
61
|
+
logger.debug(() => `Returning CSRF token after updating session status: ${this.csrfToken}`)
|
|
62
62
|
|
|
63
63
|
return this.csrfToken
|
|
64
64
|
}
|