Registry / http-networking / swagger-typescript-codegen

swagger-typescript-codegen

JSON →
library3.2.4jsnpmunverified

This package, currently at version 3.2.4, generates TypeScript client code from a Swagger/OpenAPI 2.0 specification file. It leverages Mustache templates for highly customizable code generation, allowing developers to define their own class, method, and type structures. The generated code is built upon `superagent` for HTTP requests, making it adaptable for both Node.js and browser environments when used with bundlers like Browserify or Webpack. The project originated as a fork to streamline specific functionalities and integrate code quality checks via JSHint and code beautification with JS-Beautify. While not indicating a rapid release cadence, its focus on customization and robust template-driven generation makes it suitable for projects requiring tailored API clients.

npm install swagger-typescript-codegen
INSTALL
IMPORT
SIG · SWAGGER-TYPESCRIPT
S
swagger-typescript-codegen
http-networkingjavascriptv3.2.4
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

CodeGen
const CodeGen = require('swagger-typescript-codegen').CodeGen;
import { CodeGen } from 'swagger-typescript-codegen';
The primary API is exposed via CommonJS `require` for `CodeGen.getTypescriptCode` and `CodeGen.getCustomCode`. While an ES module import might be attempted, it is not the documented or typical usage.
getTypescriptCode
CodeGen.getTypescriptCode({ /* options */ });
import { getTypescriptCode } from 'swagger-typescript-codegen';
This is a static method of the `CodeGen` object, not a top-level named export. It must be called on the imported `CodeGen` object.
getCustomCode
CodeGen.getCustomCode({ /* options */ });
import { getCustomCode } from 'swagger-typescript-codegen';
Similar to `getTypescriptCode`, this is a static method of the `CodeGen` object used for custom template-based generation and must be called on the imported `CodeGen` object.

This snippet demonstrates how to synchronously load a Swagger JSON specification from a file, parse it, and then use `swagger-typescript-codegen` to generate a TypeScript client class named 'TestClient', logging the output to the console.

var fs = require("fs"); var CodeGen = require("swagger-typescript-codegen").CodeGen; // Assuming 'swagger/spec.json' exists in the project root // or provide an absolute path. var swaggerFilePath = "swagger/spec.json"; try { var swagger = JSON.parse(fs.readFileSync(swaggerFilePath, "UTF-8")); var tsSourceCode = CodeGen.getTypescriptCode({ className: "TestClient", // Renamed for clarity swagger: swagger, // Example of importing a typings file if needed, adjust path as necessary imports: ["./typings/my-custom-types.d.ts"] }); console.log(tsSourceCode); } catch (error) { console.error(`Failed to generate code: ${error.message}`); if (error.code === 'ENOENT') { console.error(`Ensure '${swaggerFilePath}' exists and is accessible.`); } else if (error instanceof SyntaxError) { console.error(`Swagger file '${swaggerFilePath}' is not valid JSON.`); } }
Debug
Known issues
gotchaThis tool is primarily designed for Swagger 2.0 specifications (OpenAPI 2.0). While it might process some OpenAPI 3.x definitions, full compatibility is not guaranteed, and unexpected behavior or missing features may occur.
fix
Ensure your input specification strictly adheres to Swagger 2.0. If using OpenAPI 3.x, thoroughly test the generated code for correctness or consider alternative tools designed specifically for OpenAPI 3.x.
affects: >=1.0.0
gotchaThe generated TypeScript code relies on `superagent` for HTTP requests. If your project uses a different HTTP client (e.g., Axios, Fetch API), you will need to provide custom templates to integrate your preferred client library, or manually adjust the generated code.
fix
Utilize the `CodeGen.getCustomCode` function with tailored Mustache templates (`class`, `method`, `type`) to generate code that incorporates your desired HTTP client and its API calls.
affects: >=1.0.0
gotchaThe `imports` option in `getTypescriptCode` is strictly for adding TypeScript definition file (`.d.ts`) imports or `/// <reference />` directives for type augmentation, not for importing JavaScript modules that the generated client might depend on.
fix
For actual module dependencies of the generated client (e.g., `superagent` itself if not bundled), you will need to manually ensure they are installed and correctly bundled/imported in your consuming application. The `imports` option specifically handles type references.
affects: >=1.0.0
deprecatedThe primary usage examples and documentation show `require` for importing `CodeGen`. While Node.js supports ESM, the package's direct usage of `require` for its exports suggests a CJS-first design. Attempting ESM imports like `import { CodeGen } from 'swagger-typescript-codegen'` might not work or could require specific Node.js loader configurations.
fix
Stick to `const CodeGen = require('swagger-typescript-codegen').CodeGen;` for consistent and reliable module loading, especially in Node.js environments. If working in a pure ESM context, manual wrapping or careful configuration might be needed.
affects: 3.x
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'info')
The provided `swagger` object is malformed, missing the crucial `info` property, or the file failed to load/parse correctly resulting in an empty or invalid object.
fix
Verify the `swagger` JSON file's content and structure against the Swagger 2.0 specification. Ensure `fs.readFileSync` successfully reads the file and `JSON.parse` does not throw an error before passing the object to `CodeGen`.
TypeError: CodeGen.getTypescriptCode is not a function
Incorrectly importing or accessing the `CodeGen` object, often due to trying a default ESM import (`import CodeGen from '...'`) or an incorrect named import that doesn't resolve the static methods.
fix
Use the documented CommonJS import pattern: `const CodeGen = require('swagger-typescript-codegen').CodeGen;` to ensure `CodeGen` is correctly resolved as an object with the static `getTypescriptCode` method.
Error: ENOENT: no such file or directory, open 'swagger/spec.json'
The specified Swagger specification file (`swagger/spec.json` in the example) does not exist at the given path relative to where the script is being executed.
fix
Double-check the file path provided to `fs.readFileSync`. Ensure the file exists and the path is correct, or use an absolute path for robustness. Consider adding `process.cwd()` to construct the full path.
Upgrade
Version history
3.2.4latest on npm
Audit
Dependencies
superagentrequiredUsed as the underlying HTTP client for generated TypeScript code.
js-beautifyoptionalUsed for beautifying generated code if the `beautify` option is enabled.
Agent activity
8 hits · last 30 days
node
6
OpenAI (training)
1
Resources
swagger-typescript-codegen — npm install swagger-typescript-codegen · libregistry