@orpc/server
Version:
<div align="center"> <image align="center" src="https://orpc.dev/logo.webp" width=280 alt="oRPC logo" /> </div>
40 lines (37 loc) • 1.5 kB
JavaScript
import { ORPCError, fallbackContractConfig } from '@orpc/contract';
const STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL = Symbol("STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT");
class StrictGetMethodPlugin {
error;
/**
* make sure execute before batch plugin to get real method
*/
order = 7e6;
constructor(options = {}) {
this.error = options.error ?? new ORPCError("METHOD_NOT_SUPPORTED");
}
init(options) {
options.rootInterceptors ??= [];
options.clientInterceptors ??= [];
options.rootInterceptors.unshift((options2) => {
const isGetMethod = options2.request.method === "GET";
return options2.next({
...options2,
context: {
...options2.context,
[STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL]: isGetMethod
}
});
});
options.clientInterceptors.unshift((options2) => {
if (typeof options2.context[STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL] !== "boolean") {
throw new TypeError("[StrictGetMethodPlugin] strict GET method context has been corrupted or modified by another plugin or interceptor");
}
const procedureMethod = fallbackContractConfig("defaultMethod", options2.procedure["~orpc"].route.method);
if (options2.context[STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL] && procedureMethod !== "GET") {
throw this.error;
}
return options2.next();
});
}
}
export { StrictGetMethodPlugin as S };