feat(api): update v3 endpoints to fit new files, return pull request url when reporting and improve v1 deprecated route error message

This commit is contained in:
wanhose 2024-04-03 18:51:04 +02:00
parent 465a08fd3b
commit 81b50b1798
4 changed files with 111 additions and 37 deletions

View File

@ -22,7 +22,7 @@ export default (server: FastifyInstance, options: RouteShorthandOptions, done: (
async (_request, reply) => {
reply.status(500).send({
success: false,
errors: ['This API route is no longer supported in Mozilla Firefox'],
errors: ['This API route is no longer supported. Please upgrade to the latest version'],
});
}
);

View File

@ -5,42 +5,26 @@ export default (server: FastifyInstance, options: RouteShorthandOptions, done: (
server.get('/data/', async (request, reply) => {
try {
const dataUrl = 'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/main/data';
const classesUrl = `${dataUrl}/classes.txt`;
const elementsUrl = `${dataUrl}/elements.txt`;
const commonWordsUrl = `${dataUrl}/common-words.json`;
const fixesUrl = `${dataUrl}/fixes.txt`;
const skipsUrl = `${dataUrl}/skips.txt`;
const tagsUrl = `${dataUrl}/tags.txt`;
const skipsUrl = `${dataUrl}/skips.json`;
const tokensUrl = `${dataUrl}/tokens.txt`;
const results = await Promise.all([
fetch(classesUrl),
fetch(elementsUrl),
fetch(commonWordsUrl),
fetch(fixesUrl),
fetch(skipsUrl),
fetch(tagsUrl),
fetch(tokensUrl),
]);
reply.send({
data: {
classes: (await results[0].text()).split('\n').filter((x) => !!x),
commonWords: [
'banner',
'cc',
'cmp',
'compliance',
'consent',
'cookie',
'dialog',
'disclaimer',
'gdpr',
'law',
'policy',
'popup',
'privacy',
],
elements: (await results[1].text()).split('\n').filter((x) => !!x),
fixes: (await results[2].text()).split('\n').filter((x) => !!x),
skips: (await results[3].text()).split('\n').filter((x) => !!x),
tags: (await results[4].text()).split('\n').filter((x) => !!x),
classes: (await results[3].json()).classes,
commonWords: await results[0].json(),
elements: (await results[3].json()).selectors,
fixes: (await results[1].text()).split('\n').filter((x) => !!x),
skips: (await results[2].json()).domains,
tags: (await results[2].json()).tags,
},
success: true,
});

View File

@ -1,23 +1,32 @@
import { FastifyInstance, RouteShorthandOptions } from 'fastify';
import fetch from 'node-fetch';
export default (server: FastifyInstance, options: RouteShorthandOptions, done: () => void) => {
server.get('/data/', async (request, reply) => {
export default (server: FastifyInstance, _options: RouteShorthandOptions, done: () => void) => {
server.get('/data/', async (_request, reply) => {
try {
const dataUrl = 'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/v7.0.0/data';
const actionsUrl = `${dataUrl}/actions.json`;
const dataUrl = 'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/main/data';
const commonWordsUrl = `${dataUrl}/common-words.json`;
const fixesUrl = `${dataUrl}/fixes.txt`;
const skipsUrl = `${dataUrl}/skips.json`;
const tokensUrl = `${dataUrl}/tokens.json`;
const results = await Promise.all([fetch(actionsUrl), fetch(tokensUrl)]);
const results = await Promise.all([
fetch(commonWordsUrl),
fetch(fixesUrl),
fetch(skipsUrl),
fetch(tokensUrl),
]);
reply.send({
data: {
actions: await results[0].json(),
tokens: await results[1].json(),
commonWords: await results[0].json(),
fixes: (await results[1].text()).split('\n').filter((x) => !!x),
skips: await results[2].json(),
tokens: await results[3].json(),
},
success: true,
});
} catch {
} catch (error) {
reply.send({ success: false });
}
});

View File

@ -1 +1,82 @@
export { default as default } from 'routes/v2/report';
import { FastifyInstance, RouteShorthandOptions } from 'fastify';
import environment from 'services/environment';
import { octokit } from 'services/octokit';
interface PostReportBody {
readonly reason?: string;
readonly url: string;
readonly userAgent?: string;
readonly version: string;
}
export default (server: FastifyInstance, _options: RouteShorthandOptions, done: () => void) => {
server.post<{ Body: PostReportBody }>(
'/report/',
{
schema: {
body: {
properties: {
reason: {
type: 'string',
},
url: {
type: 'string',
},
userAgent: {
type: 'string',
},
version: {
type: 'string',
},
},
required: ['url', 'version'],
type: 'object',
},
},
},
async (request, reply) => {
try {
const issues = await octokit.request('GET /repos/{owner}/{repo}/issues', {
owner: environment.github.owner,
repo: environment.github.repo,
});
const url = new URL(request.body.url).hostname
.split('.')
.slice(-3)
.join('.')
.replace('www.', '');
if (issues.data.some((issue) => issue.title.includes(url))) {
throw new Error('Issue already exists');
}
const response = await octokit.request('POST /repos/{owner}/{repo}/issues', {
assignees: [environment.github.owner],
body: [
'## Specifications',
`- <b>Reason:</b> ${request.body.reason ?? '-'}`,
`- <b>URL:</b> ${request.body.url}`,
`- <b>User-Agent:</b> ${request.body.userAgent ?? '-'}`,
`- <b>Version:</b> ${request.body.version}`,
].join('\n'),
labels: ['bug'],
owner: environment.github.owner,
repo: environment.github.repo,
title: url,
});
reply.send({
data: response.data.html_url,
success: true,
});
} catch (error) {
reply.send({
errors: [error.message],
success: false,
});
}
}
);
done();
};