-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from ageddesi/v.0.12.0
V.0.12.0
- Loading branch information
Showing
19 changed files
with
1,599 additions
and
410 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
|
||
} |
Oops, something went wrong.