CORS Handler Plugin
Use CORSHandlerPlugin to configure CORS Policy for your API.
Basic
import { class CORSHandlerPlugin<T extends Context>Configures the [CORS Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
for your API, including preflight requests.CORSHandlerPlugin } from '@orpc/server/plugins'
const const handler: RPCHandler<{
headers?: IncomingHttpHeaders;
} & object>
handler = new new RPCHandler<{
headers?: IncomingHttpHeaders;
} & object>(router: Router<{
headers?: IncomingHttpHeaders;
} & object>, options?: NoInfer<RPCHandlerOptions<{
headers?: IncomingHttpHeaders;
} & object>>): RPCHandler<{
headers?: IncomingHttpHeaders;
} & object>
Serves an oRPC router over the RPC protocol using the Fetch API
(Request/Response), supported by modern runtimes like Deno, Bun,
Cloudflare Workers, and browsers.RPCHandler(const router: {
planet: {
list: ImplementedProcedure<{
headers?: IncomingHttpHeaders;
} & object, object, ZodObject<{
limit: ZodOptional<ZodNumber>;
cursor: ZodDefault<ZodNumber>;
}, $strip>, ZodArray<ZodObject<{
id: ZodNumber;
name: ZodString;
description: ZodOptional<ZodString>;
}, $strip>>, object>;
find: ImplementedProcedure<{
headers?: IncomingHttpHeaders;
} & object, object, ZodObject<{
id: ZodNumber;
}, $strip>, ZodObject<...>, object>;
create: ImplementedProcedure<...>;
};
}
router, {
FetchHandlerOptions<{ headers?: IncomingHttpHeaders; } & object>.plugins?: FetchHandlerPlugin<{
headers?: IncomingHttpHeaders;
} & object>[] | undefined
plugins: [
new new CORSHandlerPlugin<{
headers?: IncomingHttpHeaders;
} & object>(options?: CORSHandlerPluginOptions<{
headers?: IncomingHttpHeaders;
} & object>): CORSHandlerPlugin<{
headers?: IncomingHttpHeaders;
} & object>
Configures the [CORS Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
for your API, including preflight requests.CORSHandlerPlugin({
CORSHandlerPluginOptions<{ headers?: IncomingHttpHeaders; } & object>.origin?: Value<Promisable<string | readonly string[] | null | undefined>, [origin: string, options: StandardHandlerRoutingInterceptorOptions<{
headers?: IncomingHttpHeaders;
} & object>]>
Configures the `Access-Control-Allow-Origin` header.
Can be a string, an array of allowed origins, or a function that returns the allowed origin(s).origin: (origin: stringorigin, options: StandardHandlerRoutingInterceptorOptions<{
headers?: IncomingHttpHeaders;
} & object>
options) => origin: stringorigin,
CORSHandlerPluginOptions<T extends Context>.allowMethods?: readonly string[] | undefinedConfigures the `Access-Control-Allow-Methods` header for preflight requests.allowMethods: ['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH'],
// ...
}),
],
})
Learn More
For implementation details, see the source code.