Skip to content

Instantly share code, notes, and snippets.

@sergiodxa
Last active March 15, 2025 21:49
Show Gist options
  • Save sergiodxa/c7fcdfb2e41c6eaa9088baca5a6b10cf to your computer and use it in GitHub Desktop.
Save sergiodxa/c7fcdfb2e41c6eaa9088baca5a6b10cf to your computer and use it in GitHub Desktop.
A crud helper for RRv7 routes.ts file
import { index, prefix, route } from "@react-router/dev/routes";
import { camelize, pluralize, singularize } from "inflected";
function createCrud(base = "./views") {
/**
* Create a CRUD route configuration.
* @param name The name of the resource. It will be pluralized for the path.
* @param options The options for the crud.
* @param options.member Extra routes to add to each member.
* @param options.collection Extra routes to add to the collection.
*
* @example
* export default [
* crud("contacts"),
* crud("users", {
* member: [route("ban", "views/users/ban.ts")],
* collection: [route("search", "views/users/search.ts")],
* })
* ] satisfies RouteConfig;
*/
return function crud(
name: string,
{ member = [], collection = [] }: Options = {},
) {
let plural = pluralize(name);
let camelCase = camelize(singularize(name), false);
return route(
plural,
`${base}/${plural}/layout.tsx`,
{ id: `${name}.layout` },
[
index(`${base}/${plural}/index.tsx`, { id: `${name}.index` }),
route("new", `${base}/${plural}/new.tsx`, { id: `${name}.new` }),
...prefix(`:${camelCase}Id`, [
index(`${base}/${plural}/show.tsx`, { id: `${name}.show` }),
route("edit", `${base}/${plural}/edit.tsx`, { id: `${name}.edit` }),
route("destroy", `${base}/${plural}/destroy.ts`, {
id: `${name}.destroy`,
}),
...member,
]),
...collection,
],
);
};
interface Options {
member?: RouteConfigEntry[];
collection?: RouteConfigEntry[];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment