@@ -6,30 +6,25 @@ export async function handleApi(request, url, host, env, state) {
66
77 // Toggle global maintenance mode
88 if ( url . pathname === '/worker/api/toggle-maintenance/global' && request . method === 'POST' ) {
9- const newState = { ...state , isGlobalMaintenance : ! state . isGlobalMaintenance } ;
10- await env . MAINTENANCE_KV . put ( 'MAINTENANCE_STATE' , JSON . stringify ( newState ) ) ;
9+ await env . MAINTENANCE_KV . put ( 'MAINTENANCE_GLOBAL' , state . isGlobalMaintenance ? 'false' : 'true' ) ;
1110 return new Response ( 'Maintenance globale mise à jour' ) ;
1211 }
1312
1413 // Add a subdomain to maintenance list
1514 if ( url . pathname === '/worker/api/maintenance/subdomain/add' && request . method === 'POST' ) {
1615 const { subdomain } = await request . json ( ) ;
17- if ( typeof subdomain === 'string' && subdomain . trim ( ) && ! state . subdomainsMaintenance . includes ( subdomain ) ) {
16+ if ( ! state . subdomainsMaintenance . includes ( subdomain ) ) {
1817 state . subdomainsMaintenance . push ( subdomain ) ;
19- const newState = { ...state , subdomainsMaintenance : state . subdomainsMaintenance } ;
20- await env . MAINTENANCE_KV . put ( 'MAINTENANCE_STATE' , JSON . stringify ( newState ) ) ;
18+ await env . MAINTENANCE_KV . put ( 'MAINTENANCE_SUBDOMAINS' , JSON . stringify ( state . subdomainsMaintenance ) ) ;
2119 }
2220 return new Response ( 'Sous-domaine ajouté' ) ;
2321 }
2422
2523 // Remove a subdomain from maintenance list
2624 if ( url . pathname === '/worker/api/maintenance/subdomain/remove' && request . method === 'POST' ) {
2725 const { subdomain } = await request . json ( ) ;
28- if ( typeof subdomain === 'string' && subdomain . trim ( ) ) {
29- const newList = state . subdomainsMaintenance . filter ( d => d !== subdomain ) ;
30- const newState = { ...state , subdomainsMaintenance : newList } ;
31- await env . MAINTENANCE_KV . put ( 'MAINTENANCE_STATE' , JSON . stringify ( newState ) ) ;
32- }
26+ const newList = state . subdomainsMaintenance . filter ( d => d !== subdomain ) ;
27+ await env . MAINTENANCE_KV . put ( 'MAINTENANCE_SUBDOMAINS' , JSON . stringify ( newList ) ) ;
3328 return new Response ( 'Sous-domaine retiré' ) ;
3429 }
3530
@@ -47,31 +42,28 @@ export async function handleApi(request, url, host, env, state) {
4742 // Add a subdomain to the banner list
4843 if ( url . pathname === '/worker/api/banner/subdomains/add' && request . method === 'POST' ) {
4944 const { subdomain } = await request . json ( ) ;
50- if ( typeof subdomain === 'string' && subdomain . trim ( ) && ! state . bannerSubdomains . includes ( subdomain ) ) {
45+ if ( typeof subdomain !== 'string' ) return new Response ( 'Format attendu: { subdomain: "..." }' , { status : 400 } ) ;
46+ if ( ! state . bannerSubdomains . includes ( subdomain ) ) {
5147 state . bannerSubdomains . push ( subdomain ) ;
52- const newState = { ...state , bannerSubdomains : state . bannerSubdomains } ;
53- await env . MAINTENANCE_KV . put ( 'MAINTENANCE_STATE' , JSON . stringify ( newState ) ) ;
48+ await env . MAINTENANCE_KV . put ( 'BANNER_SUBDOMAINS' , JSON . stringify ( state . bannerSubdomains ) ) ;
5449 }
5550 return new Response ( 'Sous-domaine ajouté au bandeau' ) ;
5651 }
5752
5853 // Remove a subdomain from the banner list
5954 if ( url . pathname === '/worker/api/banner/subdomains/remove' && request . method === 'POST' ) {
6055 const { subdomain } = await request . json ( ) ;
61- if ( typeof subdomain === 'string' && subdomain . trim ( ) ) {
62- const newList = state . bannerSubdomains . filter ( d => d !== subdomain ) ;
63- const newState = { ...state , bannerSubdomains : newList } ;
64- await env . MAINTENANCE_KV . put ( 'MAINTENANCE_STATE' , JSON . stringify ( newState ) ) ;
65- }
56+ if ( typeof subdomain !== 'string' ) return new Response ( 'Format attendu: { subdomain: "..." }' , { status : 400 } ) ;
57+ const newList = state . bannerSubdomains . filter ( d => d !== subdomain ) ;
58+ await env . MAINTENANCE_KV . put ( 'BANNER_SUBDOMAINS' , JSON . stringify ( newList ) ) ;
6659 return new Response ( 'Sous-domaine retiré du bandeau' ) ;
6760 }
6861
6962 // Set the banner message
7063 if ( url . pathname === '/worker/api/banner/message' && request . method === 'POST' ) {
7164 const { message } = await request . json ( ) ;
7265 if ( typeof message === 'string' ) {
73- const newState = { ...state , bannerMessage : message } ;
74- await env . MAINTENANCE_KV . put ( 'MAINTENANCE_STATE' , JSON . stringify ( newState ) ) ;
66+ await env . MAINTENANCE_KV . put ( 'BANNER_MESSAGE' , message ) ;
7567 return new Response ( 'Message du bandeau mis à jour' ) ;
7668 } else {
7769 return new Response ( 'Format attendu: { message: "..." }' , { status : 400 } ) ;
0 commit comments