@compas/code-gen
TypeScript icon, indicating that this package has built-in type declarations

0.15.6 • Public • Published

@compas/code-gen

Install size CI status badge Codecov status


Code generators for routers, validators, SQL queries, API clients and more. Take a look at the documentation.

As a server

mainFn(import.meta, main);

async function main() {
  const app = new App();

  const T = new TypeCreator("post");
  const R = T.router("/post");

  app.add(
    new TypeCreator("database")
      .object("post")
      .keys({
        title: T.string().searchable(),
        body: T.string(),
      })
      .enableQueries({ withDates: true }),

    T.crud("/post").entity(T.reference("database", "post")).routes({
      listRoute: true,
      singleRoute: true,
      createRoute: true,
      updateRoute: true,
      deleteRoute: true,
    }),
  );

  await app.generate({
    outputDirectory: "./src/generated/application",
    dumpStructure: true,
    dumpApiStructure: true,
    dumpPostgres: true,
    enabledGenerators: ["validator", "router", "sql", "apiClient"],
  });
}

As a client

mainFn(import.meta, main);

async function main() {
  const app = new App({ verbose: true });

  const fromRemote = await loadApiStructureFromRemote(
    Axios,
    "https://some.compas.powered.backend",
  );

  app.extend(fromRemote);

  await app.generate({
    outputDirectory: "./src/generated",
    isBrowser: true,
    enabledGenerators: ["type", "apiClient", "reactQuery"],
  });

  await spawn("yarn", ["format"]);
}

Package Sidebar

Install

npm i @compas/code-gen

Homepage

compasjs.com

Weekly Downloads

1,131

Version

0.15.6

License

MIT

Unpacked Size

1.45 MB

Total Files

202

Last publish

Collaborators

  • dirkdev98