@@ -32,36 +32,45 @@ type OptionalFunction<T> = T | (() => Awaitable<T>)
3232 * ```
3333 * @see https://nuxt.com/docs/getting-started/testing#registerendpoint
3434 */
35- export function registerEndpoint (
36- url : string ,
37- options :
38- | EventHandler
39- | {
40- handler : EventHandler
41- method : HTTPMethod
42- } ,
43- ) {
35+ const endpointRegistry : Record < string , Array < { handler : EventHandler , method ?: HTTPMethod } > > = { }
36+ export function registerEndpoint ( url : string , options : EventHandler | { handler : EventHandler , method : HTTPMethod } ) {
4437 // @ts -expect-error private property
4538 const app : App = window . __app
4639
47- if ( ! app ) return
40+ if ( ! app ) {
41+ throw new Error ( 'registerEndpoint() can only be used in a `@nuxt/test-utils` runtime environment' )
42+ }
4843
49- const config
50- = typeof options === 'function'
51- ? {
52- handler : options ,
53- method : undefined ,
54- }
55- : options
56-
57- app . use ( '/_' + url , defineEventHandler ( config . handler ) , {
58- match ( _ , event ) {
59- return config . method ? event ?. method === config . method : true
60- } ,
61- } )
44+ const config = typeof options === 'function' ? { handler : options , method : undefined } : options
45+ config . handler = defineEventHandler ( config . handler )
6246
6347 // @ts -expect-error private property
64- window . __registry . add ( url )
48+ const hasBeenRegistered : boolean = window . __registry . has ( url )
49+
50+ endpointRegistry [ url ] ||= [ ]
51+ endpointRegistry [ url ] . push ( config )
52+
53+ if ( ! hasBeenRegistered ) {
54+ // @ts -expect-error private property
55+ window . __registry . add ( url )
56+
57+ app . use ( '/_' + url , defineEventHandler ( ( event ) => {
58+ const latestHandler = [ ...endpointRegistry [ url ] ] . reverse ( ) . find ( config => config . method ? event . method === config . method : true )
59+ return latestHandler ?. handler ( event )
60+ } ) , {
61+ match ( _ , event ) {
62+ return endpointRegistry [ url ] ?. some ( config => config . method ? event ?. method === config . method : true )
63+ } ,
64+ } )
65+ }
66+
67+ return ( ) => {
68+ endpointRegistry [ url ] . splice ( endpointRegistry [ url ] . indexOf ( config ) , 1 )
69+ if ( endpointRegistry [ url ] . length === 0 ) {
70+ // @ts -expect-error private property
71+ window . __registry . delete ( url )
72+ }
73+ }
6574}
6675
6776/**
0 commit comments