feat(api): add temp v3 endpoints to test

This commit is contained in:
wanhose 2023-01-18 19:55:54 +01:00
parent e9fb829c0a
commit 031913ee6e
3 changed files with 106 additions and 0 deletions

View File

@ -5,6 +5,8 @@ import v1EntriesRoutes from 'routes/v1/entries';
import v1ReportRoutes from 'routes/v1/report';
import v2DataRoutes from 'routes/v2/data';
import v2ReportRoutes from 'routes/v2/report';
import v3DataRoutes from 'routes/v3/data';
import v3ReportRoutes from 'routes/v3/report';
import environment from 'services/environment';
const server = fastify({ logger: true });
@ -27,6 +29,8 @@ server.register(v1EntriesRoutes, { prefix: '/rest/v1' });
server.register(v1ReportRoutes, { prefix: '/rest/v1' });
server.register(v2DataRoutes, { prefix: '/rest/v2' });
server.register(v2ReportRoutes, { prefix: '/rest/v2' });
server.register(v3DataRoutes, { prefix: '/rest/v3' });
server.register(v3ReportRoutes, { prefix: '/rest/v3' });
server.listen({ host: '0.0.0.0', port: environment.port }, (error, address) => {
if (error) {

View File

@ -0,0 +1,26 @@
import { FastifyInstance, RouteShorthandOptions } from 'fastify';
import fetch from 'node-fetch';
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 tokensUrl = `${dataUrl}/tokens.json`;
const results = await Promise.all([fetch(actionsUrl), fetch(tokensUrl)]);
reply.send({
data: {
actions: await results[0].json(),
tokens: await results[1].json(),
},
success: true,
});
} catch {
reply.send({ success: false });
}
});
done();
};

View File

@ -0,0 +1,76 @@
import { FastifyInstance, RouteShorthandOptions } from 'fastify';
import environment from 'services/environment';
import { octokit } from 'services/octokit';
type PostReportBody = {
reason?: string;
url: string;
userAgent?: string;
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();
}
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({ success: true });
} catch (error) {
reply.send({ errors: [error.message], success: false });
}
}
);
done();
};