Skip to content
Core function of generating controller code of ruled-router
Branch: master
Clone or download

README.md

Router code generator

Usage

yarn add --dev @jimengio/router-code-generator
import { generateTree } from "@jimengio/router-code-generator";
  • Basic router controller generator
[{ "name": "a", "path": "a" }]
export let genRouter = {
  a: {
    name: "a",
    raw: "a",
    path: () => `/a`,
    go: () => switchPath(`/a`),
  },
};
  • Router with parameter
[
  {
    "name": "a",
    "path": "a/:code"
  }
]
export let genRouter = {
  a_: {
    name: "a",
    raw: "a/:code",
    path: (code: string) => `/a/${code}`,
    go: (code: string) => switchPath(`/a/${code}`),
  },
};
  • Router with query options
[
  {
    "name": "a",
    "path": "a",
    "queries": ["a", "b"]
  }
]
export let genRouter = {
  a: {
    name: "a",
    raw: "a",
    path: <T = { a?: string; b?: string }>(queries?: T) => `/a?${qsStringify(queries)}`,
    go: <T = { a?: string; b?: string }>(queries?: T) => switchPath(`/a?${qsStringify(queries)}`),
  },
};
  • Nested router
[
  {
    "name": "a",
    "path": "a",
    "next": [
      {
        "name": "b",
        "path": "b"
      }
    ]
  }
]
export let genRouter = {
  a: {
    name: "a",
    raw: "a",
    path: () => `/a`,
    go: () => switchPath(`/a`),
    b: {
      name: "b",
      raw: "b",
      path: () => `/a/b`,
      go: () => switchPath(`/a/b`),
    },
  },
};

License

MIT

You can’t perform that action at this time.