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 fetch from 'node-fetch';
|
||||
import { parseNewFix } from 'services/compatibility';
|
||||
|
||||
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/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(commonWordsUrl),
|
||||
fetch(fixesUrl),
|
||||
fetch(skipsUrl),
|
||||
fetch(tokensUrl),
|
||||
]);
|
||||
const skips = await results[2].json();
|
||||
const tokens = await results[3].json();
|
||||
const databaseUrl =
|
||||
'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/main/database.json';
|
||||
const result = await (await fetch(databaseUrl)).json();
|
||||
|
||||
reply.send({
|
||||
data: {
|
||||
classes: tokens.classes,
|
||||
commonWords: await results[0].json(),
|
||||
elements: tokens.selectors,
|
||||
fixes: (await results[1].text()).split('\n').filter((x) => !!x),
|
||||
skips: skips.domains,
|
||||
tags: skips.tags,
|
||||
classes: result.tokens.classes,
|
||||
commonWords: result.commonWords,
|
||||
elements: result.tokens.selectors,
|
||||
fixes: result.fixes.map(parseNewFix),
|
||||
skips: result.skips.domains,
|
||||
tags: result.skips.tags,
|
||||
},
|
||||
success: true,
|
||||
});
|
||||
|
@ -1,28 +1,18 @@
|
||||
import { FastifyInstance, RouteShorthandOptions } from 'fastify';
|
||||
import fetch from 'node-fetch';
|
||||
import { parseNewFix } from 'services/compatibility';
|
||||
|
||||
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/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(commonWordsUrl),
|
||||
fetch(fixesUrl),
|
||||
fetch(skipsUrl),
|
||||
fetch(tokensUrl),
|
||||
]);
|
||||
const databaseUrl =
|
||||
'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/main/database.json';
|
||||
const result = await (await fetch(databaseUrl)).json();
|
||||
|
||||
reply.send({
|
||||
data: {
|
||||
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(),
|
||||
...result,
|
||||
fixes: result.fixes.map(parseNewFix),
|
||||
},
|
||||
success: true,
|
||||
});
|
||||
|
@ -82,9 +82,9 @@ export default (server: FastifyInstance, _options: RouteShorthandOptions, done:
|
||||
});
|
||||
} catch (error) {
|
||||
reply.send({
|
||||
data: existingIssue?.html_url,
|
||||
errors: [error.message],
|
||||
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