@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
25 lines (23 loc) • 902 B
JavaScript
import { ACCESS_DENIED_HEADER } from "./route.js";
import { getResponseHeader } from "@tanstack/react-start/server";
import { createMiddleware } from "@tanstack/react-start";
//#region src/route/access-denied-status.ts
/**
* Answers 403 for a page a guard refused by throwing, with the guard's mark.
* TanStack answers any thrown value with 500, which reads as a server error.
*/
function accessDeniedStatusMiddleware() {
return createMiddleware({ type: "request" }).server(async ({ next }) => {
const result = await next();
const reason = getResponseHeader(ACCESS_DENIED_HEADER);
if (result.response.status !== 500 || reason === void 0) return result;
const headers = new Headers(result.response.headers);
headers.set(ACCESS_DENIED_HEADER, reason);
return new Response(result.response.body, {
status: 403,
headers
});
});
}
//#endregion
export { accessDeniedStatusMiddleware };