@@ -10,6 +10,23 @@ import {
1010import { getRequestEvent , isServer } from "solid-js/web" ;
1111import { useNavigate , getIntent , getInPreloadFn } from "../routing.js" ;
1212import type { CacheEntry , NarrowResponse } from "../types.js" ;
13+ import type { StandardSchemaV1 } from "@standard-schema/spec" ;
14+
15+ // from https://github.com/standard-schema/standard-schema/#how-do-i-accept-standard-schemas-in-my-library
16+ async function standardValidate < T extends StandardSchemaV1 > (
17+ schema : T ,
18+ input : StandardSchemaV1 . InferInput < T >
19+ ) : Promise < StandardSchemaV1 . InferOutput < T > > {
20+ let result = schema [ "~standard" ] . validate ( input ) ;
21+ if ( result instanceof Promise ) result = await result ;
22+
23+ // if the `issues` field exists, the validation failed
24+ if ( result . issues ) {
25+ throw new Error ( JSON . stringify ( result . issues , null , 2 ) ) ;
26+ }
27+
28+ return result . value ;
29+ }
1330
1431const LocationHeader = "Location" ;
1532const PRELOAD_TIMEOUT = 5000 ;
@@ -70,10 +87,16 @@ export type CachedFunction<T extends (...args: any) => any> = T extends (
7087 }
7188 : never ;
7289
73- export function query < T extends ( ...args : any ) => any > ( fn : T , name : string ) : CachedFunction < T > {
90+ export function query < T extends ( ...args : any ) => any > (
91+ fn : T ,
92+ name : string ,
93+ opts : {
94+ schemas : StandardSchemaV1 [ ] ;
95+ }
96+ ) : CachedFunction < T > {
7497 // prioritize GET for server functions
7598 if ( ( fn as any ) . GET ) fn = ( fn as any ) . GET ;
76- const cachedFn = ( ( ...args : Parameters < T > ) => {
99+ const cachedFn = async ( ...args : Parameters < T > ) => {
77100 const cache = getCache ( ) ;
78101 const intent = getIntent ( ) ;
79102 const inPreloadFn = getInPreloadFn ( ) ;
@@ -133,6 +156,9 @@ export function query<T extends (...args: any) => any>(fn: T, name: string): Cac
133156 res = sharedConfig . load ! ( key ) // hydrating
134157 // @ts -ignore at least until we add a delete method to sharedConfig
135158 delete globalThis . _$HY . r [ key ] ;
159+ } else if ( opts . schemas . length > 0 ) {
160+ let validated = await Promise . all ( opts . schemas . map ( ( schema , i ) => standardValidate ( schema , args [ i ] ) ) ) ;
161+ res = fn ( ...( validated as any ) ) ;
136162 } else res = fn ( ...( args as any ) ) ;
137163
138164 if ( cached ) {
0 commit comments