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:
parent
465a08fd3b
commit
81b50b1798
@ -22,7 +22,7 @@ export default (server: FastifyInstance, options: RouteShorthandOptions, done: (
|
|||||||
async (_request, reply) => {
|
async (_request, reply) => {
|
||||||
reply.status(500).send({
|
reply.status(500).send({
|
||||||
success: false,
|
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'],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -5,42 +5,26 @@ export default (server: FastifyInstance, options: RouteShorthandOptions, done: (
|
|||||||
server.get('/data/', async (request, reply) => {
|
server.get('/data/', async (request, reply) => {
|
||||||
try {
|
try {
|
||||||
const dataUrl = 'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/main/data';
|
const dataUrl = 'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/main/data';
|
||||||
const classesUrl = `${dataUrl}/classes.txt`;
|
const commonWordsUrl = `${dataUrl}/common-words.json`;
|
||||||
const elementsUrl = `${dataUrl}/elements.txt`;
|
|
||||||
const fixesUrl = `${dataUrl}/fixes.txt`;
|
const fixesUrl = `${dataUrl}/fixes.txt`;
|
||||||
const skipsUrl = `${dataUrl}/skips.txt`;
|
const skipsUrl = `${dataUrl}/skips.json`;
|
||||||
const tagsUrl = `${dataUrl}/tags.txt`;
|
const tokensUrl = `${dataUrl}/tokens.txt`;
|
||||||
|
|
||||||
const results = await Promise.all([
|
const results = await Promise.all([
|
||||||
fetch(classesUrl),
|
fetch(commonWordsUrl),
|
||||||
fetch(elementsUrl),
|
|
||||||
fetch(fixesUrl),
|
fetch(fixesUrl),
|
||||||
fetch(skipsUrl),
|
fetch(skipsUrl),
|
||||||
fetch(tagsUrl),
|
fetch(tokensUrl),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
reply.send({
|
reply.send({
|
||||||
data: {
|
data: {
|
||||||
classes: (await results[0].text()).split('\n').filter((x) => !!x),
|
classes: (await results[3].json()).classes,
|
||||||
commonWords: [
|
commonWords: await results[0].json(),
|
||||||
'banner',
|
elements: (await results[3].json()).selectors,
|
||||||
'cc',
|
fixes: (await results[1].text()).split('\n').filter((x) => !!x),
|
||||||
'cmp',
|
skips: (await results[2].json()).domains,
|
||||||
'compliance',
|
tags: (await results[2].json()).tags,
|
||||||
'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),
|
|
||||||
},
|
},
|
||||||
success: true,
|
success: true,
|
||||||
});
|
});
|
||||||
|
@ -1,23 +1,32 @@
|
|||||||
import { FastifyInstance, RouteShorthandOptions } from 'fastify';
|
import { FastifyInstance, RouteShorthandOptions } from 'fastify';
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
|
|
||||||
export default (server: FastifyInstance, options: RouteShorthandOptions, done: () => void) => {
|
export default (server: FastifyInstance, _options: RouteShorthandOptions, done: () => void) => {
|
||||||
server.get('/data/', async (request, reply) => {
|
server.get('/data/', async (_request, reply) => {
|
||||||
try {
|
try {
|
||||||
const dataUrl = 'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/v7.0.0/data';
|
const dataUrl = 'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/main/data';
|
||||||
const actionsUrl = `${dataUrl}/actions.json`;
|
const commonWordsUrl = `${dataUrl}/common-words.json`;
|
||||||
|
const fixesUrl = `${dataUrl}/fixes.txt`;
|
||||||
|
const skipsUrl = `${dataUrl}/skips.json`;
|
||||||
const tokensUrl = `${dataUrl}/tokens.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({
|
reply.send({
|
||||||
data: {
|
data: {
|
||||||
actions: await results[0].json(),
|
commonWords: await results[0].json(),
|
||||||
tokens: await results[1].json(),
|
fixes: (await results[1].text()).split('\n').filter((x) => !!x),
|
||||||
|
skips: await results[2].json(),
|
||||||
|
tokens: await results[3].json(),
|
||||||
},
|
},
|
||||||
success: true,
|
success: true,
|
||||||
});
|
});
|
||||||
} catch {
|
} catch (error) {
|
||||||
reply.send({ success: false });
|
reply.send({ success: false });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -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();
|
||||||
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user