11import z from "zod" ;
2- import { createHandler , argument } from "../../router" ;
3- import { GlobalConfigKey } from "../../middleware" ;
2+ import { createHandler , argument , GlobalConfigAccessorKey } from "../../router" ;
43import { JsonRendererKey } from "../../tui" ;
54import { globalConfigSchema , type GlobalConfig } from "../../globalConfig" ;
65
@@ -24,7 +23,7 @@ export const createConfigHandler = () =>
2423 argument ( "value" , "value to set for the key" , z . string ( ) . optional ( ) ) ,
2524 ] ,
2625 handle : async ( ctx , _flags , args ) => {
27- const globalConfigAccessor = ctx . require ( GlobalConfigKey ) ;
26+ const globalConfigAccessor = ctx . require ( GlobalConfigAccessorKey ) ;
2827 const jsonRenderer = ctx . require ( JsonRendererKey ) ;
2928
3029 const globalConfig = await globalConfigAccessor . get ( ) ;
@@ -47,20 +46,21 @@ export const createConfigHandler = () =>
4746 const updatedConfig = await globalConfigAccessor . set ( rawUpdatedConfig ) ;
4847 const updatedScopedConfig = getAtPath ( updatedConfig , args . key ) ;
4948
50- if ( updatedScopedConfig === undefined && ! isValidPath ( updatedConfig , args . key ) )
51- throw new TypeError ( `invalid key ${ args . key } for global config` ) ;
5249 jsonRenderer . renderJson ( updatedScopedConfig ) ;
5350 } ,
5451 } ) ;
5552
53+ /** Type guard that narrows `value` to a plain object record. */
5654function isRecord ( value : unknown ) : value is Record < string , unknown > {
5755 return typeof value === "object" && value !== null && ! Array . isArray ( value ) ;
5856}
5957
58+ /** Retrieves a nested value from `obj` using dot-notation (e.g. `"telemetry.enabled"`). */
6059function getAtPath ( obj : object , path : string ) : unknown {
6160 return path . split ( "." ) . reduce < unknown > ( ( acc , key ) => ( isRecord ( acc ) ? acc [ key ] : undefined ) , obj ) ;
6261}
6362
63+ /** Returns a shallow-cloned object with `value` set at the given dot-notation `path`. */
6464function setAtPath (
6565 obj : Record < string , unknown > ,
6666 path : string ,
@@ -80,6 +80,7 @@ function setAtPath(
8080 } ;
8181}
8282
83+ /** Checks whether `path` corresponds to a key recognized by {@link globalConfigSchema}. */
8384function isValidPath ( config : GlobalConfig , path : string ) : boolean {
8485 const parseAttempt = globalConfigSchema . safeParse ( setAtPath ( config , path , "placeholder" ) ) ;
8586 // if parse fails --> zod validated the key so it must be known.
0 commit comments