-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
93 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { show, flatMorph } from "@arktype/util"; | ||
import { isObject, mergeIfUndefined } from "./generics"; | ||
import { NamespacesInput } from "./input"; | ||
import { AbiTypeScope, Scope } from "./scope"; | ||
import { resolveNamespaceWithShorthands, validateNamespaceWithShorthands } from "./namespaceWithShorthands"; | ||
|
||
export type validateNamespacesWithShorthands<namespaces, scope extends Scope = AbiTypeScope> = { | ||
[label in keyof namespaces]: validateNamespaceWithShorthands<namespaces[label], scope>; | ||
}; | ||
|
||
export function validateNamespacesWithShorthands<scope extends Scope = AbiTypeScope>( | ||
namespaces: unknown, | ||
scope: scope, | ||
): asserts namespaces is NamespacesInput { | ||
if (!isObject(namespaces)) { | ||
throw new Error(`Expected namespaces, received ${JSON.stringify(namespaces)}`); | ||
} | ||
for (const namespace of Object.values(namespaces)) { | ||
validateNamespaceWithShorthands(namespace, scope); | ||
} | ||
} | ||
|
||
export type resolveNamespacesWithShorthands<namespaces, scope extends Scope = AbiTypeScope> = { | ||
readonly [label in keyof namespaces]: resolveNamespaceWithShorthands< | ||
mergeIfUndefined<namespaces[label], { label: label }>, | ||
scope | ||
>; | ||
}; | ||
|
||
export function resolveNamespacesWithShorthands<namespaces extends NamespacesInput, scope extends Scope = AbiTypeScope>( | ||
namespaces: namespaces, | ||
scope: scope, | ||
): show<resolveNamespacesWithShorthands<namespaces, scope>> { | ||
if (!isObject(namespaces)) { | ||
throw new Error(`Expected namespaces config, received ${JSON.stringify(namespaces)}`); | ||
} | ||
|
||
return flatMorph(namespaces as NamespacesInput, (label, namespace) => [ | ||
label, | ||
resolveNamespaceWithShorthands(mergeIfUndefined(namespace, { label }), scope), | ||
]) as never; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters