Skip to content

Commit

Permalink
refactor(types): rework all types (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdanialraza authored Jan 17, 2024
1 parent 9f00e02 commit e180573
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/data/quotes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Quote } from "../types/index.js"
import type { QuoteCreation } from "../types/quote.js"

/**
* Initial quotes data
*/
export const quotes: Quote[] = [
export const quotes: QuoteCreation[] = [
{
author: "Steve Jobs",
categories: ["Work"],
Expand Down
15 changes: 15 additions & 0 deletions src/types/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
/**
* Defines the structure of a validation error
*
* @typeParam Expected - The expected value type
* @typeParam Received - The received value type
*/
export type ValidationError<Expected = string, Received = unknown> = {
/**
* The expected type
*/
expected: Expected
/**
* The error message
*/
message: string
/**
* The received value
*/
received: Received
}
70 changes: 51 additions & 19 deletions src/types/quote.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,59 @@
export type Category =
| "Art"
| "Fashion"
| "Funny"
| "Inspirational"
| "Leadership"
| "Life"
| "Love"
| "Motivational"
| "Music"
| "People"
| "Sports"
| "Success"
| "Wisdom"
| "Work"
import type { Quote as PrismaQuote } from "@prisma/client"

export type Quote = {
/**
* Defines the structure of a quote before it is added to the database
*/
export type QuoteCreation = {
/**
* The author of the quote
*/
author?: string
categories: Category[]
/**
* The categories the quote belongs in
*/
categories: string[]
/**
* The submitter of the quote
*/
submitter: string
/**
* The text of the quote
*/
text: string
/**
* Whether the quote is verified or not
*/
verified: boolean
}

export type QuoteWithStringCategories = Omit<Quote, "categories"> & { categories: string }
/**
* Defines the structure of a quote after it is added to the database
*/
export type DatabaseQuote = Omit<QuoteCreation, "categories"> & {
/**
* The categories the quote belongs in
*/
categories: PrismaQuote["categories"]
/**
* The date the quote was submitted
*/
createdAt: PrismaQuote["createdAt"]
/**
* The Id of the quote
*/
id: PrismaQuote["id"]
/**
* The date the quote was last updated
*/
updatedAt: PrismaQuote["updatedAt"]
}

/**
* Defines the structure of a quote with categories as an array
*/
export type QuoteWithCategoriesArray = Omit<DatabaseQuote, "categories"> & Pick<QuoteCreation, "categories">

export type QuoteUnion = Quote | QuoteWithStringCategories
/**
* Defined a union of a quote before and after it is added to the database
*/
export type QuoteUnion = DatabaseQuote | QuoteCreation
7 changes: 3 additions & 4 deletions src/util/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Quote as DatabaseQuote } from "@prisma/client"
import Filter from "bad-words"
import type { Category, QuoteUnion, ValidationError } from "../types/index.js"
import type { DatabaseQuote, QuoteUnion, QuoteWithCategoriesArray, ValidationError } from "../types/index.js"

/**
* Converts the properties of a quote for the database
Expand All @@ -20,10 +19,10 @@ export function convertPropertiesForDatabase(quote: QuoteUnion) {
*
* @param quote - The quote to convert the properties of
*/
export function convertPropertiesFromDatabase(quote: DatabaseQuote) {
export function convertPropertiesFromDatabase(quote: DatabaseQuote): QuoteWithCategoriesArray {
return {
...quote,
categories: typeof quote.categories === "string" ? (quote.categories.split(", ") as Category[]) : quote.categories,
categories: typeof quote.categories === "string" ? quote.categories.split(", ") : quote.categories,
}
}

Expand Down

0 comments on commit e180573

Please sign in to comment.