@@ -6,63 +6,65 @@ import { RequestSchema, NotificationSchema, ResultSchema } from "../types.js";
66/*
77Test that custom request/notification/result schemas can be used with the Server class.
88*/
9- const GetWeatherRequestSchema = RequestSchema . extend ( {
10- method : z . literal ( "weather/get" ) ,
11- params : z . object ( {
12- city : z . string ( ) ,
13- } ) ,
14- } ) ;
9+ test ( "should typecheck" , ( ) => {
10+ const GetWeatherRequestSchema = RequestSchema . extend ( {
11+ method : z . literal ( "weather/get" ) ,
12+ params : z . object ( {
13+ city : z . string ( ) ,
14+ } ) ,
15+ } ) ;
1516
16- const GetForecastRequestSchema = RequestSchema . extend ( {
17- method : z . literal ( "weather/forecast" ) ,
18- params : z . object ( {
19- city : z . string ( ) ,
20- days : z . number ( ) ,
21- } ) ,
22- } ) ;
17+ const GetForecastRequestSchema = RequestSchema . extend ( {
18+ method : z . literal ( "weather/forecast" ) ,
19+ params : z . object ( {
20+ city : z . string ( ) ,
21+ days : z . number ( ) ,
22+ } ) ,
23+ } ) ;
2324
24- const WeatherForecastNotificationSchema = NotificationSchema . extend ( {
25- method : z . literal ( "weather/alert" ) ,
26- params : z . object ( {
27- severity : z . enum ( [ "warning" , "watch" ] ) ,
28- message : z . string ( ) ,
29- } ) ,
30- } ) ;
25+ const WeatherForecastNotificationSchema = NotificationSchema . extend ( {
26+ method : z . literal ( "weather/alert" ) ,
27+ params : z . object ( {
28+ severity : z . enum ( [ "warning" , "watch" ] ) ,
29+ message : z . string ( ) ,
30+ } ) ,
31+ } ) ;
3132
32- const WeatherRequestSchema = GetWeatherRequestSchema . or (
33- GetForecastRequestSchema ,
34- ) ;
35- const WeatherNotificationSchema = WeatherForecastNotificationSchema ;
36- const WeatherResultSchema = ResultSchema . extend ( {
37- temperature : z . number ( ) ,
38- conditions : z . string ( ) ,
39- } ) ;
33+ const WeatherRequestSchema = GetWeatherRequestSchema . or (
34+ GetForecastRequestSchema ,
35+ ) ;
36+ const WeatherNotificationSchema = WeatherForecastNotificationSchema ;
37+ const WeatherResultSchema = ResultSchema . extend ( {
38+ temperature : z . number ( ) ,
39+ conditions : z . string ( ) ,
40+ } ) ;
4041
41- type WeatherRequest = z . infer < typeof WeatherRequestSchema > ;
42- type WeatherNotification = z . infer < typeof WeatherNotificationSchema > ;
43- type WeatherResult = z . infer < typeof WeatherResultSchema > ;
42+ type WeatherRequest = z . infer < typeof WeatherRequestSchema > ;
43+ type WeatherNotification = z . infer < typeof WeatherNotificationSchema > ;
44+ type WeatherResult = z . infer < typeof WeatherResultSchema > ;
4445
45- // Create a typed Server for weather data
46- const weatherServer = new Server <
47- WeatherRequest ,
48- WeatherNotification ,
49- WeatherResult
50- > ( {
51- name : "WeatherServer" ,
52- version : "1.0.0" ,
53- } ) ;
46+ // Create a typed Server for weather data
47+ const weatherServer = new Server <
48+ WeatherRequest ,
49+ WeatherNotification ,
50+ WeatherResult
51+ > ( {
52+ name : "WeatherServer" ,
53+ version : "1.0.0" ,
54+ } ) ;
5455
55- // Typecheck that only valid weather requests/notifications/results are allowed
56- weatherServer . setRequestHandler ( GetWeatherRequestSchema , ( request ) => {
57- return {
58- temperature : 72 ,
59- conditions : "sunny" ,
60- } ;
61- } ) ;
56+ // Typecheck that only valid weather requests/notifications/results are allowed
57+ weatherServer . setRequestHandler ( GetWeatherRequestSchema , ( request ) => {
58+ return {
59+ temperature : 72 ,
60+ conditions : "sunny" ,
61+ } ;
62+ } ) ;
6263
63- weatherServer . setNotificationHandler (
64- WeatherForecastNotificationSchema ,
65- ( notification ) => {
66- console . log ( `Weather alert: ${ notification . params . message } ` ) ;
67- } ,
68- ) ;
64+ weatherServer . setNotificationHandler (
65+ WeatherForecastNotificationSchema ,
66+ ( notification ) => {
67+ console . log ( `Weather alert: ${ notification . params . message } ` ) ;
68+ } ,
69+ ) ;
70+ } ) ;
0 commit comments