Skip to content

Commit

Permalink
Merge pull request #58 from ageddesi/v.0.12.0
Browse files Browse the repository at this point in the history
V.0.12.0
  • Loading branch information
ageddesi authored Oct 1, 2022
2 parents a839fe7 + fc484ad commit 0a5eb47
Show file tree
Hide file tree
Showing 19 changed files with 1,599 additions and 410 deletions.
11 changes: 8 additions & 3 deletions app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require('dotenv').config();
import express from 'express';
const express = require('express');
const morgan = require('morgan');
const cors = require('cors');

Expand All @@ -9,13 +9,18 @@ const port = 3000;
// Load Mock Modules
require('./modules/animal/api/animal-routes')(app) // Animals
require('./modules/chat/api/chat-routes')(app) // Chat
require('./modules/colors/api/colors-route')(app); // Colors
require('./modules/countries/api/countries-routes')(app); // Countries
require('./modules/currency/api/currency-routes')(app); // Currencies
require('./modules/emails/api/emails-routes')(app); // Emails
require('./modules/images/api/images-routes')(app); // Images
require('./modules/names/api/names-routes')(app); // Names
require('./modules/products/api/products-routes')(app); // Products
require('./modules/socials/api/socials-routes')(app); // Socials
require('./modules/sports/api/sports-routes')(app); // Sports
require('./modules/users/api/user-routes')(app); // Users
require('./modules/users/api/user-routes')(app); //Users
require('./modules/music/api/music-routes')(app); // Music
require('./modules/quotes/api/qoutes-routes')(app); // Quotes

// Add an healthcheck endpoint
app.get('/status', (req, res) => {
Expand All @@ -41,4 +46,4 @@ app.use(morgan('combined')); // adding morgan to log HTTP requests

app.listen(port, () => {
console.log(`Mock API is running on port ${port}.`);
});
});
9 changes: 9 additions & 0 deletions modules/chat/consts/ChatRandomResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import User from "../../users/consts/User"
import ChatMessage from "./ChatMessage"

type ChatRandomResponse = {
users: User[]
messages: ChatMessage[],
}

export default ChatRandomResponse
10 changes: 7 additions & 3 deletions modules/chat/utils/getRandomChat.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { faker } from "@faker-js/faker";
import getRandomUsers from "../../users/utils/getRandomUsers";
import ChatMessage from "../consts/ChatMessage";
import ChatRandomResponse from "../consts/ChatRandomResponse";

export const getRandomChat = (userCount : number, messageCount: number) : ChatMessage[] => {
export const getRandomChat = (userCount : number, messageCount: number) : ChatRandomResponse => {
const users = getRandomUsers(userCount);

const messages : ChatMessage[] = [];
Expand All @@ -14,5 +15,8 @@ export const getRandomChat = (userCount : number, messageCount: number) : ChatMe
userId: users[faker.datatype.number({ min: 0, max: userCount - 1 })].userId,
})
})
return messages;
}
return {
users,
messages
}
}
63 changes: 63 additions & 0 deletions modules/colors/api/colors-route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Request, Response } from 'express';
import * as core from 'express-serve-static-core';

import { getQtyFromRequest } from '../../../utils/route-utils';
import getRandomColor from '../utils/getRandomColor';
import ColorErrors from '../consts/ColorErrors';

const defaultColorSpace = "rgb";
const defaultColorFormat = "hex";

module.exports = function(app : core.Express){

app.get("/colors/:qty?", (req: Request, res: Response) => {
const qty = getQtyFromRequest(req);

try {
const randomColors = []

for (let i = 0; i < qty; i++) {
randomColors.push(getRandomColor(defaultColorSpace, defaultColorFormat));
}

res.json(randomColors);
} catch (error) {
res.status(500).json({
error: error.message
})
}
})

app.get("/colors/:qty/:colorSpace/:colorFormat", (req: Request, res: Response) => {
const qty = getQtyFromRequest(req);
const colorSpace = req.params.colorSpace || defaultColorSpace;
const colorFormat = req.params.colorFormat || defaultColorFormat;

try {
const randomColors = []

for (let i = 0; i < qty; i++) {
randomColors.push(getRandomColor(colorSpace, colorFormat));
}

res.json(randomColors);
} catch (error) {
if (
error === ColorErrors.InvalidColorSpaceError
|| error === ColorErrors.InvalidFormatError
|| error === ColorErrors.CannotConvertToHexWithAlphaError
) {
res.status(400).json({
error: error.message
})

return;
}

res.status(500).json({
error: error.message
})
}
})

}
9 changes: 9 additions & 0 deletions modules/colors/consts/ColorErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const InvalidColorSpaceError = new Error("Invalid color space");
const InvalidFormatError = new Error("Invalid format");
const CannotConvertToHexWithAlphaError = new Error("Cannot convert to hex with alpha");

export default {
InvalidColorSpaceError,
InvalidFormatError,
CannotConvertToHexWithAlphaError,
};
87 changes: 87 additions & 0 deletions modules/colors/consts/ColorSpaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
interface ColorSpace {
id: string;
getNums: () => number[];
}

const ColorSpaces: {[key: string]: ColorSpace} = {
RGB: {
id: 'rgb',
getNums: () => {
return [
Math.floor(Math.random() * 256),
Math.floor(Math.random() * 256),
Math.floor(Math.random() * 256),
]
}
},
RGBA: {
id: 'rgba',
getNums: () => {
return [
...ColorSpaces.RGB.getNums(),
Math.round(Math.random() * 100) / 100,
]
}
},

HSL: {
id: 'hsl',
getNums: () => {
return [
Math.floor(Math.random() * 361),
Math.floor(Math.random() * 101),
Math.floor(Math.random() * 101),
]
}
},
HSLA: {
id: 'hsla',
getNums: () => {
return [
...ColorSpaces.HSL.getNums(),
Math.round(Math.random() * 100) / 100,
]
}
},

HSV: {
id: 'hsv',
getNums: () => {
return ColorSpaces.HSL.getNums();
}
},
HSVA: {
id: 'hsva',
getNums: () => {
return [
...ColorSpaces.HSV.getNums(),
Math.round(Math.random() * 100) / 100,
]
}
},

CMYK: {
id: 'cmyk',
getNums: () => {
return [
Math.floor(Math.random() * 101),
Math.floor(Math.random() * 101),
Math.floor(Math.random() * 101),
Math.floor(Math.random() * 101),
]
}
},
CMYKA: {
id: 'cmyka',
getNums: () => {
return [
...ColorSpaces.CMYKA.getNums(),
Math.round(Math.random() * 100) / 100,
]
}
},
}

export type { ColorSpace };

export default ColorSpaces;
29 changes: 29 additions & 0 deletions modules/colors/utils/formatColor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import ColorErrors from '../consts/ColorErrors';
import type { ColorSpace } from '../consts/ColorSpaces';

const formatColor = (format: string, colorRepresentation: ColorSpace) => {
switch (format) {
case "hex":
if (colorRepresentation.id.endsWith('a')) {
throw ColorErrors.CannotConvertToHexWithAlphaError;
}

return colorRepresentation
.getNums()
.map((color) => color.toString(16).padStart(2, "0"))
.join("");
case "decimal":
return JSON.stringify(
colorRepresentation.getNums()
);
case "css":
const colorParams = colorRepresentation.getNums();
const color = colorParams.join(", ");

return `${colorRepresentation.id}( ${color} )`;
default:
throw ColorErrors.InvalidFormatError
}
}

export default formatColor;
15 changes: 15 additions & 0 deletions modules/colors/utils/getRandomColor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import ColorSpaces from '../consts/ColorSpaces';
import formatColor from './formatColor';
import ColorErrors from '../consts/ColorErrors';

const getRandomColor = (colorSpace: string, colorFormat: string) => {
const colorRepresentation = ColorSpaces[colorSpace.toUpperCase()];

if (!colorRepresentation) {
throw ColorErrors.InvalidColorSpaceError;
}

return formatColor(colorFormat, colorRepresentation);
}

export default getRandomColor;
14 changes: 14 additions & 0 deletions modules/emails/api/emails-routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Request, Response } from 'express';
import * as core from 'express-serve-static-core';
import { getRandomEmails } from '../utils/getRandomEmails';

const defaultEmailCount = 10;

module.exports = function(app : core.Express){

// Returns a set of Fake Emails
app.get("/emails", (req: Request, res: Response) => {
res.json(getRandomEmails(defaultEmailCount));
})

}
11 changes: 11 additions & 0 deletions modules/emails/consts/Email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type Email = {
id: string,
from: string,
subject: string,
body: string,
read: boolean,
tags: string[],
date: Date,
}

export default Email
19 changes: 19 additions & 0 deletions modules/emails/utils/getRandomEmails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { faker } from "@faker-js/faker";
import Email from "../consts/Email";

export const getRandomEmails = (emailCount : number) : Email[] => {
const emails : Email[] = [];
Array.from({length: emailCount}).forEach(() => {
emails.push({
id: faker.datatype.uuid(),
from: faker.internet.email(),
subject: faker.lorem.sentence(),
body: faker.lorem.paragraphs(),
read: faker.datatype.boolean(),
tags: [faker.lorem.word(), faker.lorem.word(), faker.lorem.word()],
date: faker.date.recent(faker.datatype.number({ min: 0, max: 30 })),
})
})
return emails;

}
28 changes: 28 additions & 0 deletions modules/music/api/music-routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Request, Response } from 'express';
import * as core from 'express-serve-static-core';
import { faker } from "@faker-js/faker";

interface Music {
id: string
genre: string
song: string
}

module.exports = function(app : core.Express){

app.get("/music/:qty", (req: Request, res: Response) => {
const {params} = req
let data: Music[] = []
for(let i = 0; i <= Number(params.qty); i++) {
data.push({
id: faker.datatype.uuid(),
genre: faker.music.genre(),
song: faker.music.songName()
})
}
res.json({
data,
total: data.length
})
})
}
20 changes: 20 additions & 0 deletions modules/quotes/api/qoutes-routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Request, Response } from 'express';
import * as core from 'express-serve-static-core';
import { getQtyFromRequest } from '../../../utils/route-utils';
import quotes from '../data/quotes';

module.exports = function(app : core.Express){

// Get all quotes
app.get("/quotes/:qty", (req: Request, res: Response) => {
const qty = getQtyFromRequest(req);
res.json(qty >= quotes.length ? quotes : quotes.slice(0, qty));
})

// Get a random quote
app.get("/quotes/random", (req: Request, res: Response) => {
const quote = quotes[Math.floor(Math.random() * quotes.length)];
res.json(quote)
})

}
Loading

0 comments on commit 0a5eb47

Please sign in to comment.