Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
build: move api docs data to nodejs runtime route
Brad Barbinf3b06eee55c7
diff --git a/apps/carbon/app/hooks/useSwaggerDocs.tsx b/apps/carbon/app/hooks/useSwaggerDocs.tsxnew file mode 100644index 0000000000..d98a11ae30--- /dev/null+++ b/apps/carbon/app/hooks/useSwaggerDocs.tsx@@ -0,0 +1,20 @@+import { useMount } from "@carbon/react";+import { useFetcher } from "@remix-run/react";+import { path } from "~/utils/path";++type SwaggerDocsSchema = {+ paths: Record<string, any>;+ definitions: Record<string, any>;+};++export const useSwaggerDocs = () => {+ const docsFetcher = useFetcher<SwaggerDocsSchema>();++ useMount(() => {+ docsFetcher.load(path.to.api.docs);+ });++ const swaggerDocs = docsFetcher.data;++ return swaggerDocs;+};diff --git a/apps/carbon/app/modules/api/ui/TableDocs.tsx b/apps/carbon/app/modules/api/ui/TableDocs.tsxindex ce9c1a3f6a..ca0ea6a312 100644--- a/apps/carbon/app/modules/api/ui/TableDocs.tsx+++ b/apps/carbon/app/modules/api/ui/TableDocs.tsx@@ -1,9 +1,10 @@import { getBrowserEnv } from "@carbon/auth";-import swaggerDocsSchema from "@carbon/database/swagger-docs-schema";import { cn } from "@carbon/react";import { LuTable2 } from "react-icons/lu";+import { useSwaggerDocs } from "~/hooks/useSwaggerDocs";import type { ValidLang } from "~/modules/api";import { CodeSnippet, Snippets } from "~/modules/api";+import { snakeToCamel } from "~/utils/string";const { SUPABASE_ANON_PUBLIC } = getBrowserEnv();@@ -11,25 +12,47 @@ type TableDocsProps = {endpoint: string;selectedLang: ValidLang;resourceId: string;- resources: Record<- string,- { id: string; displayName: string; camelCase: string }- >;};-const TableDocs = ({- endpoint,- selectedLang,- resourceId,- resources,-}: TableDocsProps) => {- const { paths, definitions } = swaggerDocsSchema;- // @ts-ignore+const functionPath = "rpc/";+const TableDocs = ({ endpoint, selectedLang, resourceId }: TableDocsProps) => {+ const swaggerDocsSchema = useSwaggerDocs();+ const { resources } = Object.entries(swaggerDocsSchema?.paths || {}).reduce<{+ resources: Record<+ string,+ { id: string; displayName: string; camelCase: string }+ >;+ }>(+ (a, [name]) => {+ const trimmedName = name.slice(1);+ const id = trimmedName.replace(functionPath, "");++ const displayName = id.replace(/_/g, " ");+ const camelCase = snakeToCamel(id);+ const enriched = { id, displayName, camelCase };++ if (!trimmedName.length) {+ return a;+ }++ return {+ resources: {+ ...a.resources,+ ...(!trimmedName.includes(functionPath)+ ? {+ [id]: enriched,+ }+ : {}),+ },+ };+ },+ { resources: {} }+ );- const resourcePaths = paths[`/${resourceId}`];+ const resourcePaths = swaggerDocsSchema?.paths?.[`/${resourceId}`];+ const resourceDefinition = swaggerDocsSchema?.definitions?.[resourceId];// @ts-ignore- const resourceDefinition = definitions?.[resourceId];- const resourceMeta = resources[resourceId];+ const resourceMeta = resources?.[resourceId];const realtimeEnabled = true; // TODO: realtime is not available for a lot of tables (unless we enable it)const methods = Object.keys(resourcePaths ?? {}).map((x) => x.toUpperCase());@@ -41,7 +64,7 @@ const TableDocs = ({}));- if (!paths || !definitions) return null;+ if (!swaggerDocsSchema?.paths || !swaggerDocsSchema?.definitions) return null;return (<>diff --git a/apps/carbon/app/routes/api+/docs.ts b/apps/carbon/app/routes/api+/docs.tsnew file mode 100644index 0000000000..e3c4b89fd6--- /dev/null+++ b/apps/carbon/app/routes/api+/docs.ts@@ -0,0 +1,31 @@+import { requirePermissions } from "@carbon/auth/auth.server";+import swaggerDocsSchema from "@carbon/database/swagger-docs-schema";+import type { ClientLoaderFunctionArgs } from "@remix-run/react";+import type { LoaderFunctionArgs, SerializeFrom } from "@vercel/remix";+import { json } from "@vercel/remix";+import { docsQuery } from "~/utils/react-query";++export const config = {+ runtime: "nodejs",+};++export async function loader({ request }: LoaderFunctionArgs) {+ await requirePermissions(request, {});++ return json(swaggerDocsSchema);+}++export async function clientLoader({ serverLoader }: ClientLoaderFunctionArgs) {+ const queryKey = docsQuery().queryKey;+ const data =+ window?.clientCache?.getQueryData<SerializeFrom<typeof loader>>(queryKey);++ if (!data) {+ const serverData = await serverLoader<typeof loader>();+ window?.clientCache?.setQueryData(queryKey, serverData);+ return serverData;+ }++ return data;+}+clientLoader.hydrate = true;diff --git a/apps/carbon/app/routes/x+/api+/$lang.rpc.$id.tsx b/apps/carbon/app/routes/x+/api+/$lang.rpc.$id.tsxindex 1bda8fb41e..2d12454057 100644--- a/apps/carbon/app/routes/x+/api+/$lang.rpc.$id.tsx+++ b/apps/carbon/app/routes/x+/api+/$lang.rpc.$id.tsx@@ -1,40 +1,38 @@-import swaggerDocsSchema from "@carbon/database/swagger-docs-schema";+import { useSwaggerDocs } from "~/hooks/useSwaggerDocs";import { useSelectedLang } from "~/modules/api";import { snakeToCamel } from "~/utils/string";-export const config = {- runtime: "nodejs",-};-const functionPath = "rpc/";-const { rpcs } = Object.entries(swaggerDocsSchema.paths || {}).reduce(- (a, [name]) => {- const trimmedName = name.slice(1);- const id = trimmedName.replace(functionPath, "");- const displayName = id.replace(/_/g, " ");- const camelCase = snakeToCamel(id);- const enriched = { id, displayName, camelCase };+export default function Route() {+ const swaggerDocsSchema = useSwaggerDocs();+ //+ const { rpcs } = Object.entries(swaggerDocsSchema?.paths || {}).reduce(+ (a, [name]) => {+ const trimmedName = name.slice(1);+ const id = trimmedName.replace(functionPath, "");- if (!trimmedName.length) {- return a;- }+ const displayName = id.replace(/_/g, " ");+ const camelCase = snakeToCamel(id);+ const enriched = { id, displayName, camelCase };- return {- rpcs: {- ...a.rpcs,- ...(trimmedName.includes(functionPath)- ? {- [id]: enriched,- }- : {}),- },- };- },- { rpcs: {} }-);+ if (!trimmedName.length) {+ return a;+ }-export default function Route() {+ return {+ rpcs: {+ ...a.rpcs,+ ...(trimmedName.includes(functionPath)+ ? {+ [id]: enriched,+ }+ : {}),+ },+ };+ },+ { rpcs: {} }+ );const selectedLang = useSelectedLang();return null;diff --git a/apps/carbon/app/routes/x+/api+/$lang.table.$id.tsx b/apps/carbon/app/routes/x+/api+/$lang.table.$id.tsxindex 8b625e8f2a..4184279b42 100644--- a/apps/carbon/app/routes/x+/api+/$lang.table.$id.tsx+++ b/apps/carbon/app/routes/x+/api+/$lang.table.$id.tsx@@ -1,8 +1,6 @@import { getBrowserEnv } from "@carbon/auth";-import swaggerDocsSchema from "@carbon/database/swagger-docs-schema";import { useParams } from "@remix-run/react";import { TableDocs, useSelectedLang } from "~/modules/api";-import { snakeToCamel } from "~/utils/string";const { SUPABASE_API_URL } = getBrowserEnv();@@ -10,34 +8,6 @@ export const config = {runtime: "nodejs",};-const functionPath = "rpc/";-const { resources } = Object.entries(swaggerDocsSchema.paths || {}).reduce(- (a, [name]) => {- const trimmedName = name.slice(1);- const id = trimmedName.replace(functionPath, "");-- const displayName = id.replace(/_/g, " ");- const camelCase = snakeToCamel(id);- const enriched = { id, displayName, camelCase };-- if (!trimmedName.length) {- return a;- }-- return {- resources: {- ...a.resources,- ...(!trimmedName.includes(functionPath)- ? {- [id]: enriched,- }- : {}),- },- };- },- { resources: {} }-);-export default function Route() {const selectedLang = useSelectedLang();const { id } = useParams();@@ -48,7 +18,6 @@ export default function Route() {endpoint={SUPABASE_API_URL}selectedLang={selectedLang}resourceId={id}- resources={resources}/>);}diff --git a/apps/carbon/app/routes/x+/api+/_layout.tsx b/apps/carbon/app/routes/x+/api+/_layout.tsxindex 91f3dabcd3..b5e98c4b3d 100644--- a/apps/carbon/app/routes/x+/api+/_layout.tsx+++ b/apps/carbon/app/routes/x+/api+/_layout.tsx@@ -1,10 +1,10 @@-import swaggerDocsSchema from "@carbon/database/swagger-docs-schema";import { Button } from "@carbon/react";import { Outlet, useLocation, useNavigate } from "@remix-run/react";import type { MetaFunction } from "@vercel/remix";import { LuEye, LuTable2 } from "react-icons/lu";import { GroupedContentSidebar } from "~/components/Layout";import { CollapsibleSidebarProvider } from "~/components/Layout/Navigation";+import { useSwaggerDocs } from "~/hooks/useSwaggerDocs";import type { ValidLang } from "~/modules/api";import { useSelectedLang } from "~/modules/api";import DocsStyle from "~/styles/docs.css?url";@@ -97,6 +97,7 @@ const tableBlacklist = new Set([]);function useApiDocsMenu(): RouteGroup[] {+ const swaggerDocsSchema = useSwaggerDocs();const selectedLang = useSelectedLang();const result: RouteGroup[] = [{@@ -110,10 +111,11 @@ function useApiDocsMenu(): RouteGroup[] {},];- const tables = Object.keys(swaggerDocsSchema.definitions).sort();+ const tables = Object.keys(swaggerDocsSchema?.definitions ?? {}).sort();const isTable = (table: string): boolean => {- const tableKey = `/${table}` as keyof typeof swaggerDocsSchema.paths;- return !!Object.keys(swaggerDocsSchema.paths[tableKey] ?? {})?.some(+ if (!swaggerDocsSchema?.paths) return false;+ const tableKey = `/${table}`;+ return Object.keys(swaggerDocsSchema.paths[tableKey] ?? {}).some((x) => x.toUpperCase() === "POST");};diff --git a/apps/carbon/app/routes/x+/supplier-quote+/$id.$lineId.details.tsx b/apps/carbon/app/routes/x+/supplier-quote+/$id.$lineId.details.tsxindex 63692b6fb9..7308c74bc7 100644--- a/apps/carbon/app/routes/x+/supplier-quote+/$id.$lineId.details.tsx+++ b/apps/carbon/app/routes/x+/supplier-quote+/$id.$lineId.details.tsx@@ -28,10 +28,6 @@ import type { MethodItemType } from "~/modules/shared";import { setCustomFields } from "~/utils/form";import { path } from "~/utils/path";-export const config = {- runtime: "nodejs",-};-export const loader = async ({ request, params }: LoaderFunctionArgs) => {const { companyId } = await requirePermissions(request, {view: "purchasing",diff --git a/apps/carbon/app/utils/path.ts b/apps/carbon/app/utils/path.tsindex 242d15b8f2..5e1da0e6e7 100644--- a/apps/carbon/app/utils/path.ts+++ b/apps/carbon/app/utils/path.ts@@ -28,6 +28,7 @@ export const path = {departments: `${api}/people/departments`,digitalQuote: (id: string) =>generatePath(`${api}/sales/digital-quote/${id}`),+ docs: `${api}/docs`,employeeTypes: `${api}/users/employee-types`,emptyPermissions: `${api}/users/empty-permissions`,generateCsvColumns: (table: string) =>diff --git a/apps/carbon/app/utils/react-query.ts b/apps/carbon/app/utils/react-query.tsindex 93356ae93c..5ddbf161ee 100644--- a/apps/carbon/app/utils/react-query.ts+++ b/apps/carbon/app/utils/react-query.ts@@ -38,6 +38,11 @@ export const customerLocationsQuery = (customerId: string) => ({staleTime: RefreshRate.Low,});+export const docsQuery = () => ({+ queryKey: ["docs"],+ staleTime: RefreshRate.Never,+});+export const locationsQuery = (companyId: string | null) => ({queryKey: ["locations", companyId ?? "null"],staleTime: RefreshRate.Low,