Skip to Content
Proxy endpoints

Proxy endpoints

JSON

You can easily create a proxy endpoint by fetching data from another server with fetch function that, in its turn, returns Response instance that’s automatically handled by Next.js. The only thing you need to do is to define 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-side the return type is going to be inferred as expected.

import { ProxyRPC } from 'vovk-client'; // ... const { greeting } = await ProxyRPC.getHello();

You can also define response type at the client method if you want to keep the original return type to be Response.

import { ProxyRPC } from 'vovk-client'; // ... const { greeting } = await ProxyRPC.getHello<{ greeting: string }>();

Blob

When return type isn’t changed, the client will automatically resolve the response as Response object. This is useful when you need to fetch a file or a binary data.

import { get } from 'vovk'; export default class ProxyController { @get('proxy') static proxy() { return fetch('https://example.com/example.pdf'); } }
import { ProxyRPC } from 'vovk-client'; // ... const response = await ProxyRPC.proxy(); const buffer = await response.arrayBuffer(); const blob = new Blob([buffer], { type: 'application/pdf' }); // ...
Last updated on