Socket
Socket
Sign inDemoInstall

@mittwald/api-code-generator

Package Overview
Dependencies
385
Maintainers
2
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @mittwald/api-code-generator

Common code base used by `@mittwald/api-client-*` package.


Version published
Maintainers
2
Created

Changelog

Source

4.14.1 (2024-03-11)

Note: Version bump only for package root

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

Readme

Source

Common code base used by @mittwald/api-client-* package.

CLI

Use the CLI commands provided by this package to build your API client and validate the OpenAPI-Spec:

acg validate spec/openapi.json
acg generate --name MittwaldAPIV2 spec/openapi.json src/generated --optionalHeader x-access-token

TypeScript API

If you need to generate the client in code, you can use the TypeScript API demonstrated in this template:

import * as path from "path";
import { UniversalContentLoader } from "@mittwald/api-code-generator/js";
import { UniversalFileLoader } from "@mittwald/api-code-generator/js";
import { OpenApiSpec } from "@mittwald/api-code-generator/js";
import { CodeGenerationModel } from "@mittwald/api-code-generator/js";
import { prepareTypeScriptOutput } from "@mittwald/api-code-generator/js";
import { writeIfChangedAsync } from "@mittwald/api-code-generator/js";

const name = "MittwaldAPIV2";
const inout = `${process.cwd()}/spec/openapi.json`;
const output = `${process.cwd()}/src/generated`;

// Loading OpenAPI spec
const loader = new UniversalContentLoader(new UniversalFileLoader(input));
const openApiDoc = await loader.load();

// Parsing OpenAPI spec
const spec = await OpenApiSpec.parse(openApiDoc, {
  skipValidation: flags.skipValidation,
});

// Building transformation model
const model = CodeGenerationModel.fromDoc(name, spec.document);

// Generating descriptors
const descriptorsFileContent = model.paths.compileDescriptors();
await writeIfChangedAsync(
  path.join(output, "descriptors.ts"),
  await prepareTypeScriptOutput(descriptorsFileContent),
);

// Generating types
const typesFileContent = await model.compileTypes({
  rootNamespace: flags.name,
  optionalHeaders: ["x-access-token"],
});
await writeIfChangedAsync(
  path.join(output, "types.ts"),
  await prepareTypeScriptOutput(typesFileContent),
);

// Generating client
const clientFileContent = model.paths.compileClient();
await writeIfChangedAsync(
  path.join(output, "client.ts"),
  await prepareTypeScriptOutput(clientFileContent),
);

// if needed: Generating React client
const reactClientFileContent = model.paths.compileReactClient();
await writeIfChangedAsync(
  path.join(output, "client-react.ts"),
  await prepareTypeScriptOutput(reactClientFileContent),
);

FAQs

Last updated on 11 Mar 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc