refactor(api): to new database files structure
This commit is contained in:
parent
fd5ad33219
commit
2957047219
@ -1,32 +1,22 @@
|
|||||||
import { FastifyInstance, RouteShorthandOptions } from 'fastify';
|
import { FastifyInstance, RouteShorthandOptions } from 'fastify';
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
|
import { parseNewFix } from 'services/compatibility';
|
||||||
|
|
||||||
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/main/data';
|
const databaseUrl =
|
||||||
const commonWordsUrl = `${dataUrl}/common-words.json`;
|
'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/main/database.json';
|
||||||
const fixesUrl = `${dataUrl}/fixes.txt`;
|
const result = await (await fetch(databaseUrl)).json();
|
||||||
const skipsUrl = `${dataUrl}/skips.json`;
|
|
||||||
const tokensUrl = `${dataUrl}/tokens.json`;
|
|
||||||
|
|
||||||
const results = await Promise.all([
|
|
||||||
fetch(commonWordsUrl),
|
|
||||||
fetch(fixesUrl),
|
|
||||||
fetch(skipsUrl),
|
|
||||||
fetch(tokensUrl),
|
|
||||||
]);
|
|
||||||
const skips = await results[2].json();
|
|
||||||
const tokens = await results[3].json();
|
|
||||||
|
|
||||||
reply.send({
|
reply.send({
|
||||||
data: {
|
data: {
|
||||||
classes: tokens.classes,
|
classes: result.tokens.classes,
|
||||||
commonWords: await results[0].json(),
|
commonWords: result.commonWords,
|
||||||
elements: tokens.selectors,
|
elements: result.tokens.selectors,
|
||||||
fixes: (await results[1].text()).split('\n').filter((x) => !!x),
|
fixes: result.fixes.map(parseNewFix),
|
||||||
skips: skips.domains,
|
skips: result.skips.domains,
|
||||||
tags: skips.tags,
|
tags: result.skips.tags,
|
||||||
},
|
},
|
||||||
success: true,
|
success: true,
|
||||||
});
|
});
|
||||||
|
@ -1,28 +1,18 @@
|
|||||||
import { FastifyInstance, RouteShorthandOptions } from 'fastify';
|
import { FastifyInstance, RouteShorthandOptions } from 'fastify';
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
|
import { parseNewFix } from 'services/compatibility';
|
||||||
|
|
||||||
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/main/data';
|
const databaseUrl =
|
||||||
const commonWordsUrl = `${dataUrl}/common-words.json`;
|
'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/main/database.json';
|
||||||
const fixesUrl = `${dataUrl}/fixes.txt`;
|
const result = await (await fetch(databaseUrl)).json();
|
||||||
const skipsUrl = `${dataUrl}/skips.json`;
|
|
||||||
const tokensUrl = `${dataUrl}/tokens.json`;
|
|
||||||
|
|
||||||
const results = await Promise.all([
|
|
||||||
fetch(commonWordsUrl),
|
|
||||||
fetch(fixesUrl),
|
|
||||||
fetch(skipsUrl),
|
|
||||||
fetch(tokensUrl),
|
|
||||||
]);
|
|
||||||
|
|
||||||
reply.send({
|
reply.send({
|
||||||
data: {
|
data: {
|
||||||
commonWords: await results[0].json(),
|
...result,
|
||||||
fixes: (await results[1].text()).split('\n').filter((x) => !!x),
|
fixes: result.fixes.map(parseNewFix),
|
||||||
skips: await results[2].json(),
|
|
||||||
tokens: await results[3].json(),
|
|
||||||
},
|
},
|
||||||
success: true,
|
success: true,
|
||||||
});
|
});
|
||||||
|
@ -82,9 +82,9 @@ export default (server: FastifyInstance, _options: RouteShorthandOptions, done:
|
|||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reply.send({
|
reply.send({
|
||||||
|
data: existingIssue?.html_url,
|
||||||
errors: [error.message],
|
errors: [error.message],
|
||||||
success: false,
|
success: false,
|
||||||
data: existingIssue?.html_url,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
13
packages/api/src/services/compatibility.ts
Normal file
13
packages/api/src/services/compatibility.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
/**
|
||||||
|
* Parse the new fix object into the old string format used by older versions of the extension
|
||||||
|
*/
|
||||||
|
export function parseNewFix(fix: Fix): string {
|
||||||
|
return `${fix.domain}##${fix.selector}##${fix.action}${fix.property ? `##${fix.property}` : ''}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Fix {
|
||||||
|
readonly action: string;
|
||||||
|
readonly domain: string;
|
||||||
|
readonly property?: string;
|
||||||
|
readonly selector: string;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user