From b3f56fbc695b601a5ac91eb158d568ce2a7abefe Mon Sep 17 00:00:00 2001 From: Danilo Alonso Date: Thu, 12 Jan 2023 17:34:59 -0500 Subject: [PATCH] feat: add local typings --- lib/index.d.ts | 151 +++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 + 2 files changed, 153 insertions(+) create mode 100644 lib/index.d.ts diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..fcf23f9 --- /dev/null +++ b/lib/index.d.ts @@ -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 (uuid: U): { + $id: U, + random: Record + } +} + +type KeyValsAsType = { + [ + K in keyof T as T[K] extends string ? ( + string extends T[K] ? never : T[K] + ) : never + ]?: AllowedValues +}; + +type AllowedValues = ( + number | string | boolean | + Array | typeof Function | + Schema +); + +type Schema = { + $param: keyof Criteria, + $value?: AllowedValues + $replace?: 'true' | true, + $env?: keyof Criteria + $coerce?: 'number' | 'array' | 'boolean' | 'object' + $splitToken?: string | RegExp + $filter?: keyof Criteria | { $env: keyof NodeJS.ProcessEnv }, + $base?: AllowedValues, + $default?: AllowedValues + $id?: string, + $range?: { + limit: number, + value: AllowedValues, + id: string + }[] + $meta?: object | string, +} | KeyValsAsType + +type ValueOf = T[keyof T]; + +type FilterType = { + $filter?: C, +} | Record + +type InferFromObject = T[K] extends infer C ? C : T[K]; + +/** + * + */ +type ConfidenceStore< + Criteria, + ReturnType +> = ( + { + [K in keyof ReturnType]: ConfidenceStore< + Criteria, + ReturnType[K] + > | Schema + } | + { + [K in keyof Schema]: ( + ConfidenceStore | + KeyValsAsType + ) + } +); + +/** + * 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 = ( + K extends string ? ( + T[K] extends Record ? ( + T[K] extends ArrayLike ? ( + `/${K}` | `/${K}/${PathImplMid>}` + ) : ( + `/${K}` | `/${K}/${PathImplMid}` + ) + ) : `/${K}` + ) : never +); + +/** + * This generates the mid-path slash commands for `PathImplementation` + */ +type PathImplMid = ( + K extends string ? ( + T[K] extends Record ? ( + T[K] extends ArrayLike ? ( + K | `${K}/${PathImplMid>}` + ) : ( + K | `${K}/${PathImplMid}` + ) + ) : K + ) : never +); + +// The actual path implementation +type Path = '/' | PathImplementation; + +type PathValueImp> = 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 ? ( + PathValueImp + ) : never + ) + ) : never + ) : ( + UKey extends keyof T ? T[UKey] : never + ) +) : never + +type PathValue> = P extends '/' ? T : PathValueImp + +export class Store { + + constructor (document: ConfidenceStore); + + load(document: ConfidenceStore): void; + + get > (key: K, criteria: Criteria): PathValue + + meta = '/'>(key: K, criteria: Criteria): any; + + static validate >(node: T): Error | null; + +} diff --git a/package.json b/package.json index cc0e6b5..46ebebc 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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": {