Skip to content

Commit

Permalink
Output API warnings in the console (#119)
Browse files Browse the repository at this point in the history
* Output API warnings in the console

* Bump patch version

* Add missing import

* Don't use ES2020 features

ESLint does not like them
  • Loading branch information
arutkowski00 authored Aug 16, 2023
1 parent 889bdac commit 5e6aaaf
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "monday-sdk-js",
"version": "0.4.8",
"version": "0.4.9",
"private": false,
"repository": "https://github.com/mondaycom/monday-sdk-js",
"main": "src/index.js",
Expand Down
14 changes: 6 additions & 8 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { MONDAY_OAUTH_URL } = require("./constants.js");
const { convertToArrayIfNeeded } = require("./helpers");
const { initScrollHelperIfNeeded } = require("./helpers/ui-helpers");
const { initBackgroundTracking } = require("./services/background-tracking-service");
const { logWarnings } = require("./helpers/monday-api-helpers");

const EMPTY_ARRAY = [];

Expand Down Expand Up @@ -65,17 +66,14 @@ class MondayClientSdk {
const token = options.token || this._apiToken;
const apiVersion = options.apiVersion || this._apiVersion;

let responsePromise;
if (token) {
return mondayApiClient.execute(params, token, { apiVersion });
responsePromise = mondayApiClient.execute(params, token, { apiVersion });
} else {
return new Promise((resolve, reject) => {
this._localApi("api", { params, apiVersion })
.then(result => {
resolve(result.data);
})
.catch(err => reject(err));
});
responsePromise = this._localApi("api", { params, apiVersion }).then(result => result.data);
}

return responsePromise.then(logWarnings);
}

listen(typeOrTypes, callback, params) {
Expand Down
43 changes: 43 additions & 0 deletions src/helpers/monday-api-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const logWarnings = res => {
const warnings = res && res.extensions && res.extensions.warnings;
if (!warnings || !Array.isArray(warnings)) return res;

warnings.forEach(warning => {
if (!warning || !warning.message) return;

try {
const locations =
warning.locations && warning.locations.map(loc => `line ${loc.line}, column ${loc.column}`).join("; ");
const path = warning.path && warning.path.join(" → ");

let message = warning.message;

// remove the dot at the end of the message
message = message.replace(/\.$/, "");
// start the message with lower case letter
message = message.charAt(0).toLowerCase() + message.slice(1);

const messageParts = [
"[monday API]",
`${path}:`,
message,
locations && `@ ${locations}`,
warning.extensions ? ["\n\nAdditional details:", warning.extensions] : undefined
]
.flat()
.filter(Boolean);

console.warn(...messageParts);
} catch (e) {
if (warning) {
console.warn("[monday API] Warning:", warning);
}
}
});

return res;
};

module.exports = {
logWarnings
};
5 changes: 3 additions & 2 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { logWarnings } = require("./helpers/monday-api-helpers");
const mondayApiClient = require("./monday-api-client");
const { oauthToken } = require("./services/oauth-service.js");

Expand All @@ -21,14 +22,14 @@ class MondayServerSdk {
this._apiVersion = apiVersion;
}

async api(query, options = {}) {
api(query, options = {}) {
const params = { query, variables: options.variables };
const token = options.token || this._token;
const apiVersion = options.apiVersion || this._apiVersion;

if (!token) throw new Error(TOKEN_MISSING_ERROR);

return await mondayApiClient.execute(params, token, { apiVersion });
return mondayApiClient.execute(params, token, { apiVersion }).then(logWarnings);
}

oauthToken(code, clientId, clientSecret) {
Expand Down

0 comments on commit 5e6aaaf

Please sign in to comment.