Skip to main content

Getting Started with Apinni

The apinni package enables type-safe API generation for TypeScript projects, integrating with decorators like @ApinniController, @ApinniEndpoint, @ApinniDomain, and @ApinniDisabled. This guide covers the prerequisites, installation, setup, and running of apinni in your project, along with configuration options and troubleshooting tips.

What is Apinni?

Apinni is a powerful tool for generating type definitions from TypeScript controllers and endpoints, ensuring type safety and streamlined API development. It’s ideal for projects requiring robust API documentation and client-side type integration.

Prerequisites

Before installing apinni, ensure your project meets the following requirements:

RequirementVersion/Description
Node.js>= 18 (LTS recommended for stability)
TypeScript>= 4.5 (with a valid tsconfig.json)
Package Managernpm, Yarn, or pnpm
Project SetupA TypeScript project with decorators enabled (experimentalDecorators and emitDecoratorMetadata in tsconfig.json)

Configuring tsconfig.json

Ensure your tsconfig.json includes the following settings to support Apinni’s decorator-based workflow:

tsconfig.json
{
"compilerOptions": {
...
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"outDir": "./dist",
"rootDir": "./src",
...
}
}
warning

Apinni relies on TypeScript decorators. Omitting experimentalDecorators or emitDecoratorMetadata in tsconfig.json can cause type generation to fail.

Installation

Install apinni as a development dependency using your preferred package manager.

npm install @apinni/client-ts --save-dev
tip

Installing apinni as a dev dependency (--save-dev, -D) is recommended, as it’s typically used during development for type generation.

Setup

To integrate apinni into your project, add a script to your package.json to run the type generation command.

Step 1: Add apinni Script

Modify your package.json to include a apinni script:

package.json
{
"scripts": {
"apinni": "apinni"
}
}

Step 2: Create a Controller

Create a basic controller using Apinni decorators to test the setup. For example:

src/controllers/user.controller.ts
import { ApinniController, ApinniEndpoint } from '@apinni/client-ts';

@ApinniController({ path: '/api/users' })
class UserController {
@ApinniEndpoint({ path: '/:id', method: 'GET' })
getUserById() {
return { id: '123', name: 'Alice' };
}
}

Step 3: Run Apinni

Run the apinni generate command to generate type definitions:

npm run apinni generate

This generates type definition files (e.g., api-types.d.ts) in your project’s output directory, based on your controllers and endpoints.

export type GetApiUsersByIdResponse = {
id: string;
name: string;
};

export type BuildApi<T extends IApi> = T;

export type Api = {
['/api/users/:id']: {
GET: {
request: never;
responses: {
200: GetApiUsersByIdResponse;
};
};
};
}
example

Output: After running apinni generate, a file like api-types.d.ts will be generated, containing type definitions for the /api/users/:id endpoint and other utility types.

Configuration Options

Create a apinni.config.cjs file in your project root:

module.exports = {
outputPath: "./types",
plugins: [], // Your plugins here
}
PropertyTypeDescription
outputPathstringDirectory for generated type files (e.g., ./types).
includestring[]Glob patterns for files to process (e.g., ["src/controllers/**/*.ts"]).
pluginsPluginTypes[]List plugins which will affect generation logic.
Try It Out

Experiment with a sample Apinni project in a live TypeScript environment: CodeSandbox Link.

Troubleshooting

Common Issues

IssueSolution
No types generatedEnsure tsconfig.json has experimentalDecorators and emitDecoratorMetadata enabled. Check that controllers use @ApinniController.
Types generated in wrong directoryVerify the outputPath in your config file.
Decorator errorsConfirm @apinni/client-ts is installed and imported correctly (import { ApinniController } from '@apinni/client-ts').
Endpoints missing or incorrectEnsure you haven't used named imports (import { ApinniController as Controller } from '@apinni/client-ts')

Debugging Tips

  • Run apinni with --verbose to see detailed logs:
  • Check the output directory for generated .d.ts files.
  • Verify that your tsconfig.json includes the correct include and exclude patterns.
warning

Ensure your TypeScript project is compiled with tsc or a compatible build tool before running apinni, as it relies on TypeScript metadata.

Next Steps

info

For advanced use cases, check the API Reference or contact the Apinni community.