Bugfix storybook now works with live API data or with built tests

This commit is contained in:
2026-06-17 16:06:53 -07:00
parent f944c3aac6
commit 345152b548
5 changed files with 50 additions and 16 deletions
+1
View File
@@ -0,0 +1 @@
VITE_API_BASE_URL=
+20 -3
View File
@@ -1,6 +1,6 @@
/** @type { import('@storybook/react-vite').StorybookConfig } */ /** @type { import('@storybook/react-vite').StorybookConfig } */
import { mergeConfig } from 'vite';
const config = { const config = {
"stories": [ "stories": [
"../src/**/*.mdx", "../src/**/*.mdx",
@@ -12,6 +12,23 @@ const config = {
"@storybook/addon-a11y", "@storybook/addon-a11y",
"@storybook/addon-docs" "@storybook/addon-docs"
], ],
"framework": "@storybook/react-vite" "framework": "@storybook/react-vite",
// --- ADDED THE PROXY INJECTION HERE ---
async viteFinal(config) {
return mergeConfig(config, {
server: {
proxy: {
'/api': {
target: 'http://vdap.org:8082',
changeOrigin: true,
secure: false,
},
},
},
});
},
// --------------------------------------
}; };
export default config; export default config;
@@ -35,7 +35,7 @@ export const Populated = {
msw: { msw: {
handlers: [ handlers: [
// Intercept the exact URL your component is trying to fetch // Intercept the exact URL your component is trying to fetch
http.get('*/api/v2/dvar_vaa', () => { http.get('*/api/v2/comm_coor_vaa', () => {
// Return a 200 OK with the mock JSON // Return a 200 OK with the mock JSON
return HttpResponse.json(mockData); return HttpResponse.json(mockData);
}), }),
@@ -49,7 +49,7 @@ export const ServerCrash = {
parameters: { parameters: {
msw: { msw: {
handlers: [ handlers: [
http.get('*/api/v2/dvar_vaa', () => { http.get('*/api/v2/comm_coor_vaa', () => {
// Return a 500 Error // Return a 500 Error
return new HttpResponse(null, { status: 500 }); return new HttpResponse(null, { status: 500 });
}), }),
@@ -63,7 +63,7 @@ export const EmptyData = {
parameters: { parameters: {
msw: { msw: {
handlers: [ handlers: [
http.get('*/api/v2/dvar_vaa', () => { http.get('*/api/v2/comm_coor_vaa', () => {
return HttpResponse.json([]); return HttpResponse.json([]);
}), }),
], ],
+8 -5
View File
@@ -11,10 +11,13 @@ export default function useWidgetApi(endpoint) {
setError(null); setError(null);
try { try {
const baseUrl = import.meta.env.VITE_API_BASE_URL || 'http://web-api:8000'; // CHANGED: Fallback is now an empty string.
// Strip leading slashes to prevent double slashes in the URL const baseUrl = import.meta.env.VITE_API_BASE_URL || '';
const cleanEndpoint = endpoint.startsWith('/') ? endpoint.slice(1) : endpoint;
const url = `${baseUrl}/${cleanEndpoint}`; // CHANGED: Ensure the endpoint always starts with a slash
// so it resolves perfectly as a relative path to the root domain.
const cleanEndpoint = endpoint.startsWith('/') ? endpoint : `/${endpoint}`;
const url = `${baseUrl}${cleanEndpoint}`;
const response = await fetch(url); const response = await fetch(url);
@@ -37,7 +40,7 @@ export default function useWidgetApi(endpoint) {
if (endpoint) { if (endpoint) {
fetchData(); fetchData();
} }
}, [endpoint]); // Re-fetch if the endpoint changes }, [endpoint]);
return { data, isLoading, error }; return { data, isLoading, error };
} }
+13
View File
@@ -13,6 +13,19 @@ const dirname = typeof __dirname !== 'undefined' ? __dirname : path.dirname(file
// More info at: https://storybook.js.org/docs/next/writing-tests/integrations/vitest-addon // More info at: https://storybook.js.org/docs/next/writing-tests/integrations/vitest-addon
export default defineConfig({ export default defineConfig({
plugins: [react(), jsconfigPaths()], plugins: [react(), jsconfigPaths()],
// --- ADDED THE SERVER PROXY HERE ---
server: {
proxy: {
'/api': {
target: 'http://vdap.org:8082', // The remote internet API
changeOrigin: true, // Tricks the remote server into accepting the request
secure: false, // Set to true later when you upgrade to HTTPS
},
},
},
// -----------------------------------
test: { test: {
projects: [{ projects: [{
extends: true, extends: true,