Skip to content

Commit

Permalink
feat: export TypeScript API from generator package
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasFritzeDev authored Mar 8, 2024
1 parent 7331a54 commit 5ff77f4
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
7 changes: 6 additions & 1 deletion .idea/api-tools.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions packages/generator/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,73 @@
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:

```shell
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:

```typescript
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),
);
```
6 changes: 5 additions & 1 deletion packages/generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
"repository": "https://github.com/mittwald/api-client-js.git",
"license": "MIT",
"exports": {
".": "./bin/cli.js"
".": "./bin/cli.js",
"./js": {
"types": "./dist/types/index.d.ts",
"default": "./dist/esm/index.js"
}
},
"bin": {
"acg": "./bin/cli.js"
Expand Down
7 changes: 7 additions & 0 deletions packages/generator/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from "./loading/UniversalContentLoader.js";
export * from "./loading/UniversalFileLoader.js";
export * from "./openapi/OpenApiSpec.js";
export * from "./generation/model/CodeGenerationModel.js";
export * from "./generation/prepareTypeScriptOutput.js";
export * from "./loading/UniversalFileLoader.js";
export * from "./lib/writeIfChangedAsync.js";

0 comments on commit 5ff77f4

Please sign in to comment.