bxo 0.0.5-dev.17 → 0.0.5-dev.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.ts +95 -15
- package/package.json +1 -1
package/index.ts
CHANGED
@@ -447,20 +447,61 @@ export default class BXO {
|
|
447
447
|
const formData = await request.formData();
|
448
448
|
body = Object.fromEntries(formData.entries());
|
449
449
|
} else {
|
450
|
-
|
450
|
+
// Try to parse as JSON if it looks like JSON, otherwise treat as text
|
451
|
+
const textBody = await request.text();
|
452
|
+
try {
|
453
|
+
// Check if the text looks like JSON
|
454
|
+
if (textBody.trim().startsWith('{') || textBody.trim().startsWith('[')) {
|
455
|
+
body = JSON.parse(textBody);
|
456
|
+
} else {
|
457
|
+
body = textBody;
|
458
|
+
}
|
459
|
+
} catch {
|
460
|
+
body = textBody;
|
461
|
+
}
|
451
462
|
}
|
452
463
|
}
|
453
464
|
|
454
|
-
// Create context
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
465
|
+
// Create context with validation
|
466
|
+
let ctx: Context;
|
467
|
+
try {
|
468
|
+
// Validate each part separately to get better error messages
|
469
|
+
const validatedParams = route.config?.params ? this.validateData(route.config.params, params) : params;
|
470
|
+
const validatedQuery = route.config?.query ? this.validateData(route.config.query, query) : query;
|
471
|
+
const validatedBody = route.config?.body ? this.validateData(route.config.body, body) : body;
|
472
|
+
const validatedHeaders = route.config?.headers ? this.validateData(route.config.headers, headers) : headers;
|
473
|
+
|
474
|
+
ctx = {
|
475
|
+
params: validatedParams,
|
476
|
+
query: validatedQuery,
|
477
|
+
body: validatedBody,
|
478
|
+
headers: validatedHeaders,
|
479
|
+
path: pathname,
|
480
|
+
request,
|
481
|
+
set: {}
|
482
|
+
};
|
483
|
+
} catch (validationError) {
|
484
|
+
// Validation failed - return error response
|
485
|
+
const errorMessage = validationError instanceof Error ? validationError.message : 'Validation failed';
|
486
|
+
|
487
|
+
// Extract detailed validation errors from Zod
|
488
|
+
let validationDetails = undefined;
|
489
|
+
if (validationError instanceof Error) {
|
490
|
+
if ('errors' in validationError && Array.isArray(validationError.errors)) {
|
491
|
+
validationDetails = validationError.errors;
|
492
|
+
} else if ('issues' in validationError && Array.isArray(validationError.issues)) {
|
493
|
+
validationDetails = validationError.issues;
|
494
|
+
}
|
495
|
+
}
|
496
|
+
|
497
|
+
return new Response(JSON.stringify({
|
498
|
+
error: `Validation error: ${errorMessage}`,
|
499
|
+
details: validationDetails
|
500
|
+
}), {
|
501
|
+
status: 400,
|
502
|
+
headers: { 'Content-Type': 'application/json' }
|
503
|
+
});
|
504
|
+
}
|
464
505
|
|
465
506
|
try {
|
466
507
|
// Run global onRequest hook
|
@@ -493,11 +534,26 @@ export default class BXO {
|
|
493
534
|
// Validate response against schema if provided
|
494
535
|
if (route.config?.response && !(response instanceof Response)) {
|
495
536
|
try {
|
537
|
+
console.log('response', response);
|
496
538
|
response = this.validateData(route.config.response, response);
|
497
539
|
} catch (validationError) {
|
498
540
|
// Response validation failed
|
499
541
|
const errorMessage = validationError instanceof Error ? validationError.message : 'Response validation failed';
|
500
|
-
|
542
|
+
|
543
|
+
// Extract detailed validation errors from Zod
|
544
|
+
let validationDetails = undefined;
|
545
|
+
if (validationError instanceof Error) {
|
546
|
+
if ('errors' in validationError && Array.isArray(validationError.errors)) {
|
547
|
+
validationDetails = validationError.errors;
|
548
|
+
} else if ('issues' in validationError && Array.isArray(validationError.issues)) {
|
549
|
+
validationDetails = validationError.issues;
|
550
|
+
}
|
551
|
+
}
|
552
|
+
|
553
|
+
return new Response(JSON.stringify({
|
554
|
+
error: `Response validation error: ${errorMessage}`,
|
555
|
+
details: validationDetails
|
556
|
+
}), {
|
501
557
|
status: 500,
|
502
558
|
headers: { 'Content-Type': 'application/json' }
|
503
559
|
});
|
@@ -629,6 +685,11 @@ export default class BXO {
|
|
629
685
|
}
|
630
686
|
});
|
631
687
|
|
688
|
+
// Verify server was created successfully
|
689
|
+
if (!this.server) {
|
690
|
+
throw new Error('Failed to create server instance');
|
691
|
+
}
|
692
|
+
|
632
693
|
this.isRunning = true;
|
633
694
|
this.serverPort = port;
|
634
695
|
this.serverHostname = hostname;
|
@@ -666,10 +727,22 @@ export default class BXO {
|
|
666
727
|
}
|
667
728
|
|
668
729
|
if (this.server) {
|
669
|
-
|
670
|
-
|
730
|
+
try {
|
731
|
+
// Try to stop the server gracefully
|
732
|
+
if (typeof this.server.stop === 'function') {
|
733
|
+
this.server.stop();
|
734
|
+
} else {
|
735
|
+
console.warn('⚠️ Server stop method not available');
|
736
|
+
}
|
737
|
+
} catch (stopError) {
|
738
|
+
console.error('❌ Error calling server.stop():', stopError);
|
739
|
+
}
|
740
|
+
|
741
|
+
// Clear the server reference
|
742
|
+
this.server = undefined;
|
671
743
|
}
|
672
744
|
|
745
|
+
// Reset state regardless of server.stop() success
|
673
746
|
this.isRunning = false;
|
674
747
|
this.serverPort = undefined;
|
675
748
|
this.serverHostname = undefined;
|
@@ -679,8 +752,15 @@ export default class BXO {
|
|
679
752
|
await this.hooks.onAfterStop(this);
|
680
753
|
}
|
681
754
|
|
755
|
+
console.log('✅ Server stopped successfully');
|
756
|
+
|
682
757
|
} catch (error) {
|
683
758
|
console.error('❌ Error stopping server:', error);
|
759
|
+
// Even if there's an error, reset the state
|
760
|
+
this.isRunning = false;
|
761
|
+
this.server = undefined;
|
762
|
+
this.serverPort = undefined;
|
763
|
+
this.serverHostname = undefined;
|
684
764
|
throw error;
|
685
765
|
}
|
686
766
|
}
|
@@ -694,7 +774,7 @@ export default class BXO {
|
|
694
774
|
|
695
775
|
// Server status
|
696
776
|
isServerRunning(): boolean {
|
697
|
-
return this.isRunning;
|
777
|
+
return this.isRunning && this.server !== undefined;
|
698
778
|
}
|
699
779
|
|
700
780
|
getServerInfo(): { running: boolean } {
|