Proxy Endpoints
JSON
You can create a proxy endpoint by fetching data from another server with fetch. It returns a Response, which Next.js handles automatically. To ensure correct client-side inference, force a different return type.
import { get } from 'vovk';
export default class ProxyController {
@get('greeting')
static getHello() {
return fetch('https://vovk.dev/api/hello/greeting.json') as unknown as { greeting: string };
}
}See this example in action .
On the client, the return type is inferred as expected.
import { ProxyRPC } from 'vovk-client';
// ...
const { greeting } = await ProxyRPC.getHello();Alternatively, you can define the response type at the client method and keep the server return type as Response.
import { ProxyRPC } from 'vovk-client';
// ...
const { greeting } = await ProxyRPC.getHello<{ greeting: string }>();Blob
When you keep the original Response type, the client resolves the result as a Response object. This is useful for files or binary data.
import { get } from 'vovk';
export default class ProxyController {
@get('pdf-proxy')
static getPdf() {
return fetch('https://example.com/example.pdf');
}
}import { ProxyRPC } from 'vovk-client';
// ...
const response = await ProxyRPC.getPdf();
const buffer = await response.arrayBuffer();
const blob = new Blob([buffer], { type: 'application/pdf' });
// ...Last updated on