TypeScript SDKServe
Vite
Run hypequery next to a Vite frontend and proxy API requests in development.
This guide assumes you already have:
- a Vite + React app
- an
api/queries.tsfile exportingapi - a running hypequery server process
If not, start with Quick Start.
Proxy API requests in development
Update vite.config.ts:
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
server: {
proxy: {
'/api': {
target: 'http://localhost:4000',
changeOrigin: true,
},
},
},
});Generate typed React hooks
Generate the route manifest, so the hooks know each endpoint's method and path:
npm install @hypequery/react @tanstack/react-query
npm install -D @hypequery/cli
npx hypequery generate:manifest api/queries.ts --output src/lib/hypequery-manifest.jsonCreate src/lib/hypequery.ts:
import { createHooks } from '@hypequery/react';
import type { InferApiType } from '@hypequery/serve';
import type { api } from '../../api/queries';
import manifest from './hypequery-manifest.json';
type Api = InferApiType<typeof api>;
export const { useQuery, useMutation } = createHooks<Api>({
baseUrl: '/api/analytics',
manifest,
});Both server imports are import type, so no server code reaches the Vite bundle. Serve mounts routes under
/api/analytics by default, which the /api proxy above forwards. Re-run generate:manifest when you add or
re-route a query. Projects created by hypequery init also export ApiDefinition, the same
InferApiType<typeof api> type.
Wrap the app with QueryClientProvider
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import App from './App';
const queryClient = new QueryClient();
createRoot(document.getElementById('root')!).render(
<StrictMode>
<QueryClientProvider client={queryClient}>
<App />
</QueryClientProvider>
</StrictMode>
);Use Node.js or Fetch Runtime Integration for the server process that hosts api.handler.