@@ -7,6 +7,7 @@ import chalk from 'chalk';
7
7
import { Command } from 'commander' ;
8
8
import ora from 'ora' ;
9
9
import { exampleTool } from './tools/example.js' ;
10
+ import { configureFetchTool , fetchExampleTool } from './tools/fetch-example.js' ;
10
11
11
12
/**
12
13
* Create and configure the CLI command structure
@@ -49,5 +50,89 @@ export function createCLI() {
49
50
}
50
51
} ) ;
51
52
53
+ // Fetch example tool command
54
+ program
55
+ . command ( 'fetch-example <url>' )
56
+ . description ( 'Demonstrate configurable fetch patterns with different backends and caching' )
57
+ . option (
58
+ '-b, --backend <backend>' ,
59
+ 'Fetch backend to use (built-in, cache-memory, cache-disk)' ,
60
+ )
61
+ . option ( '--no-cache' , 'Bypass cache for this request' )
62
+ . option ( '-u, --user-agent <agent>' , 'Custom User-Agent header for this request' )
63
+ . action (
64
+ async (
65
+ url : string ,
66
+ options : { backend ?: string ; cache ?: boolean ; userAgent ?: string } ,
67
+ ) => {
68
+ const spinner = ora ( 'Fetching data...' ) . start ( ) ;
69
+ try {
70
+ const args : any = { url } ;
71
+ if ( options . backend ) args . backend = options . backend ;
72
+ if ( options . cache === false ) args . no_cache = true ;
73
+ if ( options . userAgent ) args . user_agent = options . userAgent ;
74
+
75
+ const result = await fetchExampleTool ( args ) ;
76
+
77
+ if ( result . isError ) {
78
+ spinner . fail ( chalk . red ( 'Error fetching data' ) ) ;
79
+ console . error ( chalk . red ( result . content [ 0 ] . text ) ) ;
80
+ process . exit ( 1 ) ;
81
+ } else {
82
+ spinner . succeed ( chalk . green ( 'Fetch completed!' ) ) ;
83
+ console . log ( result . content [ 0 ] . text ) ;
84
+ }
85
+ } catch ( error ) {
86
+ spinner . fail ( chalk . red ( 'Error fetching data' ) ) ;
87
+ console . error ( error ) ;
88
+ process . exit ( 1 ) ;
89
+ }
90
+ } ,
91
+ ) ;
92
+
93
+ // Configure fetch tool command
94
+ program
95
+ . command ( 'configure-fetch' )
96
+ . description ( 'Configure the global fetch instance settings and caching behavior' )
97
+ . option ( '-b, --backend <backend>' , 'Default fetch backend to use' )
98
+ . option ( '-t, --cache-ttl <ms>' , 'Cache TTL in milliseconds' , Number . parseInt )
99
+ . option ( '-d, --cache-dir <dir>' , 'Directory for disk caching' )
100
+ . option ( '-u, --user-agent <agent>' , 'Default User-Agent header' )
101
+ . option ( '--clear-cache' , 'Clear all caches' )
102
+ . action (
103
+ async ( options : {
104
+ backend ?: string ;
105
+ cacheTtl ?: number ;
106
+ cacheDir ?: string ;
107
+ userAgent ?: string ;
108
+ clearCache ?: boolean ;
109
+ } ) => {
110
+ const spinner = ora ( 'Updating fetch configuration...' ) . start ( ) ;
111
+ try {
112
+ const args : any = { } ;
113
+ if ( options . backend ) args . backend = options . backend ;
114
+ if ( options . cacheTtl ) args . cache_ttl = options . cacheTtl ;
115
+ if ( options . cacheDir ) args . cache_dir = options . cacheDir ;
116
+ if ( options . userAgent ) args . user_agent = options . userAgent ;
117
+ if ( options . clearCache ) args . clear_cache = true ;
118
+
119
+ const result = await configureFetchTool ( args ) ;
120
+
121
+ if ( result . isError ) {
122
+ spinner . fail ( chalk . red ( 'Error updating configuration' ) ) ;
123
+ console . error ( chalk . red ( result . content [ 0 ] . text ) ) ;
124
+ process . exit ( 1 ) ;
125
+ } else {
126
+ spinner . succeed ( chalk . green ( 'Configuration updated!' ) ) ;
127
+ console . log ( result . content [ 0 ] . text ) ;
128
+ }
129
+ } catch ( error ) {
130
+ spinner . fail ( chalk . red ( 'Error updating configuration' ) ) ;
131
+ console . error ( error ) ;
132
+ process . exit ( 1 ) ;
133
+ }
134
+ } ,
135
+ ) ;
136
+
52
137
return program ;
53
138
}
0 commit comments