Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
AitorAlgorta committed Feb 9, 2024
1 parent 0be3ca1 commit 1ec3701
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 110 deletions.
156 changes: 92 additions & 64 deletions backend/components/schema-registry-manager/backend/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@

import dotenv from "dotenv";
import express, { Express, Request, Response } from "express";
import { SchemaProvider } from "./types";
import { checkCompatibilityOfNewSchema, createSchema, deleteSchema, getLastMessage, getSchemaInfo, getSchemaVersions, getSchemas, updateSchema } from "./providers/karapace";
import dotenv from 'dotenv';
import express, {Express, Request, Response} from 'express';
import {SchemaProvider} from './types';
import {
checkCompatibilityOfNewSchema,
createSchema,
deleteSchema,
getLastMessage,
getSchemaInfo,
getSchemaVersions,
getSchemas,
updateSchema,
} from './providers/karapace';

dotenv.config();

Expand All @@ -15,22 +23,24 @@ const currentProvider: SchemaProvider = SchemaProvider.karapace;
app.use(bodyParser.json());

app.get('/schemas.provider', (req: Request, res: Response) => {
res.status(200).send(currentProvider);
res.status(200).send(currentProvider);
});

app.get('/schemas.list', (req: Request, res: Response) => {
switch (currentProvider) {
case SchemaProvider.karapace:
getSchemas().then((response: string[]) => {
res.send(response);
}).catch((e: any) => {
res.status(500).send(e);
});
break;
getSchemas()
.then((response: string[]) => {
res.send(response);
})
.catch((e: any) => {
res.status(500).send(e);
});
break;
default:
res.status(404).send('Provider Not Found');
break;
}
}
});

app.get('/schemas.versions', (req: Request, res: Response) => {
Expand All @@ -41,16 +51,18 @@ app.get('/schemas.versions', (req: Request, res: Response) => {

switch (currentProvider) {
case SchemaProvider.karapace:
getSchemaVersions(req.query.topicName as string).then((response: any) => {
res.status(200).send(response);
}).catch((e: any) => {
res.status(500).send(e);
});
break;
getSchemaVersions(req.query.topicName as string)
.then((response: any) => {
res.status(200).send(response);
})
.catch((e: any) => {
res.status(500).send(e);
});
break;
default:
res.status(404).send('Provider Not Found');
break;
}
}
});

app.get('/schemas.info', (req: Request, res: Response) => {
Expand All @@ -66,16 +78,18 @@ app.get('/schemas.info', (req: Request, res: Response) => {

switch (currentProvider) {
case SchemaProvider.karapace:
getSchemaInfo(req.query.topicName as string, version).then((response: any) => {
res.status(200).send(response);
}).catch((e: any) => {
res.status(500).send(e);
});
break;
getSchemaInfo(req.query.topicName as string, version)
.then((response: any) => {
res.status(200).send(response);
})
.catch((e: any) => {
res.status(500).send(e);
});
break;
default:
res.status(404).send('Provider Not Found');
break;
}
}
});

app.post('/schemas.update', (req: Request, res: Response) => {
Expand All @@ -90,16 +104,18 @@ app.post('/schemas.update', (req: Request, res: Response) => {

switch (currentProvider) {
case SchemaProvider.karapace:
updateSchema(req.query.topicName as string, req.body.schema as string).then((response: any) => {
res.status(200).send(response);
}).catch((e: any) => {
res.status(500).send(e);
});
break;
updateSchema(req.query.topicName as string, req.body.schema as string)
.then((response: any) => {
res.status(200).send(response);
})
.catch((e: any) => {
res.status(500).send(e);
});
break;
default:
res.status(404).send('Provider Not Found');
break;
}
}
});

app.post('/schemas.create', (req: Request, res: Response) => {
Expand All @@ -114,16 +130,18 @@ app.post('/schemas.create', (req: Request, res: Response) => {

switch (currentProvider) {
case SchemaProvider.karapace:
createSchema(req.query.topicName as string, req.body.schema as string).then((response: any) => {
res.status(200).send(response);
}).catch((e: any) => {
res.status(500).send(e);
});
break;
createSchema(req.query.topicName as string, req.body.schema as string)
.then((response: any) => {
res.status(200).send(response);
})
.catch((e: any) => {
res.status(500).send(e);
});
break;
default:
res.status(404).send('Provider Not Found');
break;
}
}
});

app.post('/schemas.compatibility', (req: Request, res: Response) => {
Expand All @@ -142,16 +160,22 @@ app.post('/schemas.compatibility', (req: Request, res: Response) => {

switch (currentProvider) {
case SchemaProvider.karapace:
checkCompatibilityOfNewSchema(req.query.topicName as string, req.body.schema as string, req.query.version as string).then((response: any) => {
res.status(200).send(response);
}).catch((e: any) => {
res.status(500).send(e);
});
break;
checkCompatibilityOfNewSchema(
req.query.topicName as string,
req.body.schema as string,
req.query.version as string
)
.then((response: any) => {
res.status(200).send(response);
})
.catch((e: any) => {
res.status(500).send(e);
});
break;
default:
res.status(404).send('Provider Not Found');
break;
}
}
});

app.post('/schemas.delete', (req: Request, res: Response) => {
Expand All @@ -162,16 +186,18 @@ app.post('/schemas.delete', (req: Request, res: Response) => {

switch (currentProvider) {
case SchemaProvider.karapace:
deleteSchema(req.query.topicName as string).then((response: any) => {
res.status(200).send(response);
}).catch((e: any) => {
res.status(500).send(e);
});
break;
deleteSchema(req.query.topicName as string)
.then((response: any) => {
res.status(200).send(response);
})
.catch((e: any) => {
res.status(500).send(e);
});
break;
default:
res.status(404).send('Provider Not Found');
break;
}
}
});

app.get('/schemas.lastMessage', (req: Request, res: Response) => {
Expand All @@ -182,17 +208,19 @@ app.get('/schemas.lastMessage', (req: Request, res: Response) => {

switch (currentProvider) {
case SchemaProvider.karapace:
getLastMessage(req.query.topicName as string).then((response: any) => {
console.log(response);
res.status(200).send(response);
}).catch((e: any) => {
res.status(500).send(e);
});
break;
getLastMessage(req.query.topicName as string)
.then((response: any) => {
console.log(response);
res.status(200).send(response);
})
.catch((e: any) => {
res.status(500).send(e);
});
break;
default:
res.status(404).send('Provider Not Found');
break;
}
}
});

async function main() {
Expand All @@ -201,4 +229,4 @@ async function main() {
});
}

main().catch(console.error);
main().catch(console.error);
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
export async function getSchemas() {
return getData('subjects').then(response => {
return getData('subjects').then(response => {
return response;
});
};
}

export async function getSchemaVersions(topicName: string) {
return getData(`subjects/${topicName}/versions`).then(response => {
if (response.error_code && response.error_code.toString().includes('404') && !topicName.includes('-value')) {
return Promise.reject('404 Not Found');
}
return response;
return response;
});
};
}

export async function getSchemaInfo(topicName: string, version: string) {
export async function getSchemaInfo(topicName: string, version: string) {
return getData(`subjects/${topicName}/versions/${version}`).then(response => {
if (response.error_code && response.error_code.toString().includes('404') && !topicName.includes('-value')) {
return Promise.reject('404 Not Found');
}
return response;
});
};
}

export async function updateSchema(topicName: string, schema: string) {
const body = {
schema: JSON.stringify({...JSON.parse(schema)}),
Expand All @@ -34,8 +34,8 @@ export async function updateSchema(topicName: string, schema: string) {
if (response.message) return Promise.reject(response.message);
return Promise.reject('Unknown Error');
});
};
}

export async function createSchema(topicName: string, schema: string) {
const body = {
schema: JSON.stringify({...JSON.parse(schema)}),
Expand All @@ -49,8 +49,8 @@ export async function createSchema(topicName: string, schema: string) {
.catch(e => {
return Promise.reject(e);
});
};
}

export async function checkCompatibilityOfNewSchema(topicName: string, schema: string, version: string) {
const body = {
schema: JSON.stringify({...JSON.parse(schema)}),
Expand All @@ -72,17 +72,17 @@ export async function checkCompatibilityOfNewSchema(topicName: string, schema: s
.catch(e => {
return Promise.reject(e);
});
};
}

export async function deleteSchema(topicName: string) {
return deleteData(`subjects/${topicName}`).then(response => {
if (response.error_code && response.error_code.toString().includes('404') && !topicName.includes('-value')) {
return Promise.reject('404 Not Found');
}
return response;
});
};
}

export async function getLastMessage(topicName: string) {
const body = {
ksql: `PRINT '${topicName}' FROM BEGINNING LIMIT 1;`,
Expand All @@ -92,30 +92,30 @@ export async function getLastMessage(topicName: string) {
console.log(response);
return response;
});
};
async function getData(url: string) {
const response = await fetch(process.env.URL + '/' + url, {
method: 'GET',
});
return response.json();
}
async function deleteData(url: string) {
const response = await fetch(process.env.URL + '/' + url, {
method: 'DELETE',
});
return response.json();
}
async function postData(url: string, body: any) {
const response = await fetch(process.env.URL + '/' + url, {
method: 'POST',
headers: {
'Content-Type': 'application/vnd.schemaregistry.v1+json',
},
body: JSON.stringify(body),
});
return response.json();
}
}

async function getData(url: string) {
const response = await fetch(process.env.URL + '/' + url, {
method: 'GET',
});
return response.json();
}

async function deleteData(url: string) {
const response = await fetch(process.env.URL + '/' + url, {
method: 'DELETE',
});
return response.json();
}

async function postData(url: string, body: any) {
const response = await fetch(process.env.URL + '/' + url, {
method: 'POST',
headers: {
'Content-Type': 'application/vnd.schemaregistry.v1+json',
},
body: JSON.stringify(body),
});

return response.json();
}
Loading

0 comments on commit 1ec3701

Please sign in to comment.