fix: more types for API

This commit is contained in:
Andras Bacsai
2022-07-14 12:47:26 +00:00
parent ce31146a9c
commit 90597389c9
40 changed files with 722 additions and 316 deletions

View File

@@ -1,13 +1,15 @@
import axios from "axios";
import cuid from "cuid";
import crypto from "crypto";
import type { FastifyReply, FastifyRequest } from "fastify";
import { encrypt, errorHandler, getAPIUrl, getUIUrl, isDev, prisma } from "../../../lib/common";
import { encrypt, errorHandler, getUIUrl, isDev, prisma } from "../../../lib/common";
import { checkContainer, removeContainer } from "../../../lib/docker";
import { scheduler } from "../../../lib/scheduler";
import { getApplicationFromDB, getApplicationFromDBWebhook } from "../../api/v1/applications/handlers";
import { getApplicationFromDBWebhook } from "../../api/v1/applications/handlers";
export async function installGithub(request: FastifyRequest, reply: FastifyReply): Promise<any> {
import type { FastifyReply, FastifyRequest } from "fastify";
import type { GitHubEvents, InstallGithub } from "./types";
export async function installGithub(request: FastifyRequest<InstallGithub>, reply: FastifyReply): Promise<any> {
try {
const { gitSourceId, installation_id } = request.query;
const source = await prisma.gitSource.findUnique({
@@ -63,7 +65,7 @@ export async function configureGitHubApp(request, reply) {
return errorHandler({ status, message })
}
}
export async function gitHubEvents(request: FastifyRequest, reply: FastifyReply): Promise<any> {
export async function gitHubEvents(request: FastifyRequest<GitHubEvents>): Promise<any> {
try {
const buildId = cuid();
const allowedGithubEvents = ['push', 'pull_request'];

View File

@@ -1,10 +1,12 @@
import { FastifyPluginAsync } from 'fastify';
import { configureGitHubApp, gitHubEvents, installGithub } from './handlers';
const root: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
import type { GitHubEvents, InstallGithub } from './types';
const root: FastifyPluginAsync = async (fastify): Promise<void> => {
fastify.get('/', async (request, reply) => configureGitHubApp(request, reply));
fastify.get('/install', async (request, reply) => installGithub(request, reply));
fastify.post('/events', async (request, reply) => gitHubEvents(request, reply));
fastify.get<InstallGithub>('/install', async (request, reply) => installGithub(request, reply));
fastify.post<GitHubEvents>('/events', async (request, reply) => gitHubEvents(request));
};
export default root;

View File

@@ -0,0 +1,20 @@
export interface InstallGithub {
Querystring: {
gitSourceId: string,
installation_id: string
}
}
export interface GitHubEvents {
Body: {
number: string,
action: string,
repository: string,
ref: string,
pull_request: {
head: {
ref: string,
repo: string
}
}
}
}

View File

@@ -2,16 +2,18 @@ import axios from "axios";
import cuid from "cuid";
import crypto from "crypto";
import type { FastifyReply, FastifyRequest } from "fastify";
import { encrypt, errorHandler, getAPIUrl, isDev, listSettings, prisma } from "../../../lib/common";
import { errorHandler, getAPIUrl, isDev, listSettings, prisma } from "../../../lib/common";
import { checkContainer, removeContainer } from "../../../lib/docker";
import { scheduler } from "../../../lib/scheduler";
import { getApplicationFromDB, getApplicationFromDBWebhook } from "../../api/v1/applications/handlers";
export async function configureGitLabApp(request: FastifyRequest, reply: FastifyReply) {
import type { ConfigureGitLabApp, GitLabEvents } from "./types";
export async function configureGitLabApp(request: FastifyRequest<ConfigureGitLabApp>, reply: FastifyReply) {
try {
const { code, state } = request.query;
const { fqdn } = await listSettings();
const { gitSource: { gitlabApp: { appId, appSecret }, htmlUrl } } = await getApplicationFromDB(state, undefined);
const { gitSource: { gitlabApp: { appId, appSecret }, htmlUrl } }: any = await getApplicationFromDB(state, undefined);
let domain = `http://${request.hostname}`;
if (fqdn) domain = fqdn;
@@ -36,12 +38,13 @@ export async function configureGitLabApp(request: FastifyRequest, reply: Fastify
return errorHandler({ status, message })
}
}
export async function gitLabEvents(request: FastifyRequest, reply: FastifyReply) {
export async function gitLabEvents(request: FastifyRequest<GitLabEvents>) {
const { object_kind: objectKind, ref, project_id } = request.body
try {
const buildId = cuid();
const allowedActions = ['opened', 'reopen', 'close', 'open', 'update'];
const { object_kind: objectKind, ref, project_id } = request.body
const webhookToken = request.headers['x-gitlab-token'];
if (!webhookToken) {
throw { status: 500, message: 'Invalid webhookToken.' }

View File

@@ -1,9 +1,11 @@
import { FastifyPluginAsync } from 'fastify';
import { configureGitLabApp, gitLabEvents } from './handlers';
const root: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
fastify.get('/', async (request, reply) => configureGitLabApp(request, reply));
fastify.post('/events', async (request, reply) => gitLabEvents(request, reply));
import type { ConfigureGitLabApp, GitLabEvents } from './types';
const root: FastifyPluginAsync = async (fastify): Promise<void> => {
fastify.get<ConfigureGitLabApp>('/', async (request, reply) => configureGitLabApp(request, reply));
fastify.post<GitLabEvents>('/events', async (request) => gitLabEvents(request));
};
export default root;

View File

@@ -0,0 +1,24 @@
export interface ConfigureGitLabApp {
Querystring: {
code: string,
state: string
}
}
export interface GitLabEvents {
Body: {
object_attributes: {
work_in_progress: string
isDraft: string
action: string
source_branch: string
target_branch: string
iid: string
},
project: {
id: string
},
object_kind: string,
ref: string,
project_id: string
}
}

View File

@@ -1,6 +1,7 @@
import { FastifyRequest } from "fastify";
import { asyncExecShell, errorHandler, getDomain, isDev, listServicesWithIncludes, prisma, supportedServiceTypesAndVersions } from "../../../lib/common";
import { getEngine } from "../../../lib/docker";
import { TraefikOtherConfiguration } from "./types";
function configureMiddleware(
{ id, container, port, domain, nakedDomain, isHttps, isWWW, isDualCerts, scriptName, type },
@@ -362,7 +363,7 @@ export async function traefikConfiguration(request, reply) {
}
}
export async function traefikOtherConfiguration(request: FastifyRequest, reply) {
export async function traefikOtherConfiguration(request: FastifyRequest<TraefikOtherConfiguration>) {
try {
const { id } = request.query
if (id) {

View File

@@ -1,9 +1,10 @@
import { FastifyPluginAsync } from 'fastify';
import { traefikConfiguration, traefikOtherConfiguration } from './handlers';
import { TraefikOtherConfiguration } from './types';
const root: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
const root: FastifyPluginAsync = async (fastify): Promise<void> => {
fastify.get('/main.json', async (request, reply) => traefikConfiguration(request, reply));
fastify.get('/other.json', async (request, reply) => traefikOtherConfiguration(request, reply));
fastify.get<TraefikOtherConfiguration>('/other.json', async (request, reply) => traefikOtherConfiguration(request));
};
export default root;

View File

@@ -0,0 +1,9 @@
export interface TraefikOtherConfiguration {
Querystring: {
id: string,
privatePort: number,
publicPort: number,
type: string,
address: string
}
}