Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
fix: type issue in use() function
Browse files Browse the repository at this point in the history
  • Loading branch information
boywithkeyboard committed Jul 16, 2023
1 parent e5f011a commit 1a24dd6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
21 changes: 14 additions & 7 deletions cheetah.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,24 @@ export class cheetah extends base<cheetah>() {

/* use ---------------------------------------------------------------------- */

use<T extends Collection>(...extensions: Extension[]): this
use<T extends Collection>(
// deno-lint-ignore no-explicit-any
use<C extends Collection>(...extensions: Extension<any>[]): this
use<C extends Collection>(
prefix: `/${string}`,
...extensions: Extension[]
// deno-lint-ignore no-explicit-any
...extensions: Extension<any>[]
): this
use<T extends Collection>(
use<C extends Collection>(
prefix: `/${string}`,
collection: T,
...extensions: Extension[]
collection: C,
// deno-lint-ignore no-explicit-any
...extensions: Extension<any>[]
): this

use<T extends Collection>(...elements: (`/${string}` | T | Extension)[]) {
use<C extends Collection>(
// deno-lint-ignore no-explicit-any
...elements: (`/${string}` | C | Extension<any>)[]
) {
let pre

for (const e of elements) {
Expand Down Expand Up @@ -161,6 +167,7 @@ export class cheetah extends base<cheetah>() {
pre = '*'
}

// @ts-ignore:
this.#extensions.add([pre, e])
}
}
Expand Down
9 changes: 5 additions & 4 deletions extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AppContext, Context } from './mod.ts'
type HasRequired<T> = Partial<T> extends T ? false : true

export type Extension<
Config extends Record<string, unknown> | unknown = unknown,
Config extends Record<string, unknown> | unknown = never,
> = {
__config: Config | undefined
onRequest?: HasRequired<Config> extends true ? ((context: {
Expand All @@ -31,9 +31,10 @@ export type Extension<

type ReturnFunction<
Config extends Record<string, unknown> | unknown = unknown,
> = Config extends unknown ? (() => Extension<Config>)
: HasRequired<Config> extends true ? ((config: Config) => Extension<Config>)
: ((config?: Config) => Extension<Config>)
> = Config extends Record<string, unknown>
? (HasRequired<Config> extends true ? ((config: Config) => Extension<Config>)
: ((config?: Config) => Extension<Config>))
: (() => Extension<Config>)

export function validExtension(ext: Record<string, unknown>) {
const symbol = Object.getOwnPropertySymbols(ext).find((s) =>
Expand Down

0 comments on commit 1a24dd6

Please sign in to comment.