Skip to content

feat: add local typings #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@

type keyOfRandom = (
'a' | 'b' | 'c' | 'd' | 'e' |
'f' | 'g' | 'h' | 'i' | 'j' |
'k' | 'l' | 'm' | 'n'
)

export interface id {

/**
* Generates a random UUID
*/
generate(): string

criteria <U extends string>(uuid: U): {
$id: U,
random: Record<keyOfRandom, number>
}
}

type KeyValsAsType<T> = {
[
K in keyof T as T[K] extends string ? (
string extends T[K] ? never : T[K]
) : never
]?: AllowedValues<T>
};

type AllowedValues<Criteria> = (
number | string | boolean |
Array<any> | typeof Function |
Schema<Criteria>
);

type Schema<Criteria> = {
$param: keyof Criteria,
$value?: AllowedValues<Criteria>
$replace?: 'true' | true,
$env?: keyof Criteria
$coerce?: 'number' | 'array' | 'boolean' | 'object'
$splitToken?: string | RegExp
$filter?: keyof Criteria | { $env: keyof NodeJS.ProcessEnv },
$base?: AllowedValues<Criteria>,
$default?: AllowedValues<Criteria>
$id?: string,
$range?: {
limit: number,
value: AllowedValues<Criteria>,
id: string
}[]
$meta?: object | string,
} | KeyValsAsType<Criteria>

type ValueOf<T> = T[keyof T];

type FilterType<C, T extends string> = {
$filter?: C,
} | Record<T, string>

type InferFromObject<T, K extends keyof T = keyof T> = T[K] extends infer C ? C : T[K];

/**
*
*/
type ConfidenceStore<
Criteria,
ReturnType
> = (
{
[K in keyof ReturnType]: ConfidenceStore<
Criteria,
ReturnType[K]
> | Schema<Criteria>
} |
{
[K in keyof Schema<Criteria>]: (
ConfidenceStore<Criteria, ReturnType> |
KeyValsAsType<Criteria>
)
}
);

/**
* Finds the '/' slash path in a type starting from the
* beginning. This type appends the `/` to the root level
* of the types and then calls the `PathImplMid` type for
* the middle paths. The separation allows us to guess
* that `{ a: { b: { c: true }}}` is the path `/a/b/c`
* and not `/a//b//c`.
*/
type PathImplementation<T, K extends keyof T> = (
K extends string ? (
T[K] extends Record<string, any> ? (
T[K] extends ArrayLike<any> ? (
`/${K}` | `/${K}/${PathImplMid<T[K], Exclude<keyof T[K], keyof any[]>>}`
) : (
`/${K}` | `/${K}/${PathImplMid<T[K], keyof T[K]>}`
)
) : `/${K}`
) : never
);

/**
* This generates the mid-path slash commands for `PathImplementation`
*/
type PathImplMid<T, K extends keyof T> = (
K extends string ? (
T[K] extends Record<string, any> ? (
T[K] extends ArrayLike<any> ? (
K | `${K}/${PathImplMid<T[K], Exclude<keyof T[K], keyof any[]>>}`
) : (
K | `${K}/${PathImplMid<T[K], keyof T[K]>}`
)
) : K
) : never
);

// The actual path implementation
type Path<T> = '/' | PathImplementation<T, keyof T>;

type PathValueImp<T, P extends Path<T>> = P extends `/${infer UKey}` ? (
P extends `/${infer LKey}/${infer Rest}` ? (
LKey extends keyof T ? (
Rest extends keyof T[LKey] ? (
T[LKey][Rest]
) : (
`/${Rest}` extends Path<T[LKey]> ? (
PathValueImp<T[LKey], `/${Rest}`>
) : never
)
) : never
) : (
UKey extends keyof T ? T[UKey] : never
)
) : never

type PathValue<T, P extends Path<T>> = P extends '/' ? T : PathValueImp<T, P>

export class Store<Criteria, ReturnType> {

constructor (document: ConfidenceStore<Criteria, ReturnType>);

load(document: ConfidenceStore<Criteria, ReturnType>): void;

get <K extends Path<ReturnType>> (key: K, criteria: Criteria): PathValue<ReturnType, K>

meta <K extends Path<ReturnType> = '/'>(key: K, criteria: Criteria): any;

static validate <T extends ConfidenceStore<any, any>>(node: T): Error | null;

}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "6.0.2",
"repository": "git://github.com/hapipal/confidence",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"keywords": [
"hapi",
"configuration",
Expand All @@ -19,6 +20,7 @@
"devDependencies": {
"@hapi/code": "8.x.x",
"@hapi/lab": "24.x.x",
"@types/node": "^18.11.18",
"coveralls": "3.x.x"
},
"bin": {
Expand Down