Routes

Utility function to generate routes and their keys.

Each function is named after the route it represents and takes in the necessary parameters for that route.

Code

snippets/typescript/routes.ts
const validateRequiredArguments = (args: { [key: string]: string }) => {
  Object.keys(args).forEach((key) => {
    const value = args[key];


    if (value === undefined || value === null || value === "") {
      throw new Error(`\`${key}\` is required. Received \`${value}\``);
    }


    if (typeof value !== "string") {
      throw new Error(
        `\`${key}\` must be a string. Received \`${JSON.stringify(value)}\``,
      );
    }
  });
};


export const routes = {
  COMPANIES_COMPANY_ID_WORKSPACES_WORKSPACE_ID_USERS_USER_ID: (args: {
    companyId: string;
    workspaceId: string;
    userId: string;
  }) => {
    validateRequiredArguments(args);
    const { companyId, workspaceId, userId } = args;


    return `companies/${companyId}/workspaces/${workspaceId}/users/${userId}`;
  },
};


export const routesKeys = Object.keys(routes).reduce(
  (acc: object, key: string) => ({
    ...acc,
    [key]: key,
  }),
  {},
) as Record<keyof typeof routes, string>;

Examples

snippets/typescript/routes.example.ts
import { routes, routesKeys } from "./routes";


const route = routes.COMPANIES_COMPANY_ID_WORKSPACES_WORKSPACE_ID_USERS_USER_ID(
  {
    companyId: "foo",
    userId: "baz",
    workspaceId: "bar",
  },
);


route; // "companies/foo/workspaces/bar/users/baz"


routesKeys.COMPANIES_COMPANY_ID_WORKSPACES_WORKSPACE_ID_USERS_USER_ID; // "COMPANIES_COMPANY_ID_WORKSPACES_WORKSPACE_ID_USERS_USER_ID"

Tests

snippets/typescript/routes.test.ts
import { describe } from "bun:test";


describe.todo("routes", () => {});

Sources