feat(api): add temp v3 endpoints to test
This commit is contained in:
parent
e9fb829c0a
commit
031913ee6e
@ -5,6 +5,8 @@ import v1EntriesRoutes from 'routes/v1/entries';
|
|||||||
import v1ReportRoutes from 'routes/v1/report';
|
import v1ReportRoutes from 'routes/v1/report';
|
||||||
import v2DataRoutes from 'routes/v2/data';
|
import v2DataRoutes from 'routes/v2/data';
|
||||||
import v2ReportRoutes from 'routes/v2/report';
|
import v2ReportRoutes from 'routes/v2/report';
|
||||||
|
import v3DataRoutes from 'routes/v3/data';
|
||||||
|
import v3ReportRoutes from 'routes/v3/report';
|
||||||
import environment from 'services/environment';
|
import environment from 'services/environment';
|
||||||
|
|
||||||
const server = fastify({ logger: true });
|
const server = fastify({ logger: true });
|
||||||
@ -27,6 +29,8 @@ server.register(v1EntriesRoutes, { prefix: '/rest/v1' });
|
|||||||
server.register(v1ReportRoutes, { prefix: '/rest/v1' });
|
server.register(v1ReportRoutes, { prefix: '/rest/v1' });
|
||||||
server.register(v2DataRoutes, { prefix: '/rest/v2' });
|
server.register(v2DataRoutes, { prefix: '/rest/v2' });
|
||||||
server.register(v2ReportRoutes, { 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) => {
|
server.listen({ host: '0.0.0.0', port: environment.port }, (error, address) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
26
packages/api/src/routes/v3/data.ts
Normal file
26
packages/api/src/routes/v3/data.ts
Normal 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();
|
||||||
|
};
|
76
packages/api/src/routes/v3/report.ts
Normal file
76
packages/api/src/routes/v3/report.ts
Normal 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();
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user