Skip to content

Commit

Permalink
🐛 Some typing corrections with @drylian
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashu11-A committed Jul 11, 2024
1 parent 16dc43a commit e2ae3df
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 39 deletions.
16 changes: 7 additions & 9 deletions src/class/pages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { rootPath, page } from '@/index.js'
import { page, rootPath } from '@/index.js'
import { PageProps, PageTypes } from '@/types/page.js'
import chalk from 'chalk'
import 'dotenv/config'
Expand All @@ -9,15 +9,13 @@ import Cache from './cache.js'

const spinner = ora()

export class Page<PageTyper extends PageTypes, Req = any, Loader = any> {
static all: Page<PageTypes, any, any>[] = []
export class Page<PageGeneric extends PageTypes, Req = any, Loader = any> {
static all: Page<PageTypes>[] = []
static find(name: string) { return Page.all.find((page) => page.interaction.name === name) }

interaction: PageProps<PageTyper, Req, Loader>
result?: string

constructor(options: PageProps<PageTyper, Req, Loader>) {
this.interaction = options
constructor(public interaction: PageProps<PageGeneric, Req, Loader>) {
Page.all.push(this)
}

Expand Down Expand Up @@ -80,15 +78,15 @@ export class Page<PageTyper extends PageTypes, Req = any, Loader = any> {
if (page === undefined) throw new Error('404 | Page not found!')

if (
page.interaction.requirements !== undefined && page.interaction.requirements.length > 0 &&
page.interaction.requirements !== undefined && page.interaction.requirements.length > 0 &&
page.interaction.loaders !== undefined && page.interaction.loaders.length > 0

) {
spinner.start()
for (const cache of page.interaction.requirements) {
if (cache.exist()) continue

for (const [index, fn] of Object.entries(page.interaction.loaders)) {
for (const [index, fn] of Object.entries<any>(page.interaction.loaders)) {
spinner.text = `Carregando loaders ${chalk.green(Number(index) + 1)}`
await fn()
}
Expand Down
1 change: 1 addition & 0 deletions src/pages/dns/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { QuestionTypes } from '@/types/questions.js'
new Page({
name: 'dns-delete',
previous: 'dns',
next: 'dns',
type: PageTypes.SubCommand,
loaders: [async () => records.save((await client.dns.records.list({ zone_id: zone.getData().id })).result)],
requirements: [records],
Expand Down
1 change: 1 addition & 0 deletions src/pages/dns/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { QuestionTypes } from '@/types/questions.js'
new Page({
name: 'dns-edit',
previous: 'dns',
next: 'dns',
type: PageTypes.SubCommand,
loaders: [async () => records.save((await client.dns.records.list({ zone_id: zone.getData().id })).result)],
requirements: [records],
Expand Down
1 change: 1 addition & 0 deletions src/pages/dns/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { QuestionTypes } from '@/types/questions.js'
new Page({
name: 'dns-search',
previous: 'dns',
next: 'dns',
type: PageTypes.SubCommand,
loaders: [async () => records.save((await client.dns.records.list({ zone_id: zone.getData().id })).result)],
requirements: [records],
Expand Down
6 changes: 3 additions & 3 deletions src/pages/home/language.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Page } from '@/class/pages.js'
import { Lang } from '@/controller/lang.js'
import { PageTypes } from '@/types/page.js'
import i18next from 'i18next'

new Page({
name: 'language',
loaders: [],
requirements: [],
type: PageTypes.Command,
type: PageTypes.SubCommand,
next: 'home',
previous: 'home',
async run(options) {
await new Lang().selectLanguage()

options.reply(options.interaction.next as string)
options.reply(options.interaction.name as string)
return options
},
})
3 changes: 2 additions & 1 deletion src/pages/home/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ new Page({
loaders: [],
requirements: [],
next: 'home',
type: PageTypes.Command,
previous: 'home',
type: PageTypes.SubCommand,
async run(options) {
const email = await question({
type: QuestionTypes.Input,
Expand Down
5 changes: 2 additions & 3 deletions src/pages/home/logout.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { credentials, Crypt } from '@/class/crypt.js'
import { Page } from '@/class/pages.js'
import { question } from '@/class/questions.js'
import { PageTypes } from '@/types/page.js'
import { QuestionTypes } from '@/types/questions.js'

new Page({
name: 'logout',
loaders: [],
requirements: [credentials],
next: 'home',
type: PageTypes.Command,
type: PageTypes.SubCommand,
previous: 'home',
async run(options) {
const [credentials] = options.interaction.requirements
await new Crypt().delete()
Expand Down
80 changes: 57 additions & 23 deletions src/types/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,17 @@ export enum PageTypes {
}

/**
* Properties specific to each page type.
* Base Page Properties
*
* @export
* @type {object} PageProps
* @template {PageTypes} PageTyper - The type of the page.
* @template Req - The requirements for the page.
* @template Loader - The loader functions for the page.
* @type {PageSchema}
* @template Req
* @template Loader
*
* @property {string} name - The name of the page.
* @property {Req[]} requirements - Requirements that the page needs.
* @property {Loader[]} loaders - This will be executed if a request is not completed.
* @property {PageTypes} type - The type of the page, which depends on the PageTyper.
* @property {string} [next] - The next page in the sequence if the current page type is Command or SubCommand.
* @property {string} [previous] - The previous page in the sequence if the current page type is Option or SubCommand.
* @property {(options: Page<PageTyper, Req, Loader>) => Promise<Page<PageTyper, Req, Loader>>} run - Function to run for this page.
*/
export type PageProps<PageTyper extends PageTypes, Req, Loader> = ({
type PageSchema<Req, Loader> = {
/**
* The name of the page.
* @type {string}
Expand All @@ -47,18 +41,58 @@ export type PageProps<PageTyper extends PageTypes, Req, Loader> = ({
* @type {Loader[]}
*/
loaders: Loader[],

/**
* The type of the page, which depends on the PageTyper.
* @type {PageTypes}
*/
type: PageTyper extends PageTypes.Command
? PageTypes.Command
: PageTyper extends PageTypes.SubCommand
? PageTypes.SubCommand
: PageTyper extends PageTypes.Option
? PageTypes.Option
: never
}


/**
* Page Option Properties
*
* @export
* @type {PageOption}
*
* @property {PageTypes} type - The type of the page, which depends on the PageTyper.
* @property {string} [next] - The next page in the sequence if the current page type is Command or SubCommand.
*/
export type PageOption = {
type: PageTypes.Option,
previous: string,
}

/**
* Page SubCommand Properties
*
* @export
* @type {SubCommand}
*
* @property {PageTypes} type - The type of the page, which depends on the PageTyper.
* @property {string} [next] - The next page in the sequence if the current page type is Command or SubCommand.
* @property {string} [previous] - The previous page in the sequence if the current page type is Option or SubCommand.
*/
export type PageSubCommand = {
type: PageTypes.SubCommand,
previous: string,
next: string
}

/**
* Page Command Properties
*
* @export
* @type {SubCommand}
*
* @property {PageTypes} type - The type of the page, which depends on the PageTyper.
*/
export type PageCommand = {
type:PageTypes.Command,
}

/**
* Properties specific to each page type.
*
* @export
* @property {(options: Page<PageTyper, Req, Loader>) => Promise<Page<PageTyper, Req, Loader>>} run - Function to run for this page.
*/
export type PageProps<PageTyper extends PageTypes, Req, Loader> = (PageSchema<Req, Loader>) & (PageOption | PageSubCommand | PageCommand) & ({

/**
* The next page in the sequence if the current page type is Command or SubCommand.
Expand Down

0 comments on commit e2ae3df

Please sign in to comment.