-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
87 lines (79 loc) · 2.29 KB
/
index.js
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
import { correct, Grammarly } from "@stewartmcgown/grammarly-api";
import express from "express";
import cors from "cors";
const app = express();
app.use(cors());
const getRequiredDetailsFromGrammarly = (response) => {
const alerts = response.alerts.map((alert) => {
const {
title,
minicardTitle,
result,
details,
explanation,
todo,
text,
cardLayout: { group },
} = alert;
return {
title: cleanOutput(title),
minicardTitle: cleanOutput(minicardTitle),
result: cleanOutput(result),
details: cleanOutput(details),
explanation: cleanOutput(explanation),
todo: cleanOutput(todo),
text: cleanOutput(text),
group: cleanOutput(group),
};
});
const { score, outcomeScores, generalScore } = response.result;
const { corrected } = response;
return {
alerts,
score: { score, outcomeScores, generalScore },
corrected,
};
};
const cleanOutput = (inputQuery) => {
const newLineRegEx = /\r?\n|\r/;
const htmlRegEx = /(<([^>]+)>)/gi;
const removeSpace = /\u00a0/g;
const removeWhiteSpace = /\s+/g;
const removeExtra = (query) =>
query
.replace(newLineRegEx, " ")
.replace(htmlRegEx, " ")
.replace(removeSpace, " ")
.replace(removeWhiteSpace, " ")
.trim();
return inputQuery ? removeExtra(inputQuery) : inputQuery;
};
app.get("/api/v1/check", async function (req, res) {
try {
const grammarly = new Grammarly();
// const grammarly = new Grammarly({
// auth: {
// grauth:
// "AABJH9lc281M9dT-n67og-TL61sgtqFMAUHLDRowzlgmjw0k8hg4Z12Z-D1Telx26ZoNwkog1V0YwB2j",
// "csrf-token": "AABJH1NwM0PyotrfdTxHxwUZRiZZzXrEbl/aBg",
// },
// });
const { text } = req.query;
if (text.length > 0) {
const results = await grammarly.analyse(text).then(correct);
const finalResults = getRequiredDetailsFromGrammarly(results);
res.status(200).send(finalResults);
} else {
res.status(200).send([]);
}
} catch (error) {
res.status(404).send("Error! Something is really wrong.");
}
});
app.get("/", (req, res) =>
res.status(200).send("Welcome to Jaynil's Grammar Checker!")
);
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Grammar Checker Running on ${port}`);
});