Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
swagger-api-hub
Advanced tools
Generate front-end access code for swagger backend services in one click
Produce front-end interface code for OpenAPI-based backend services with a single click. This tool is designed to generate TypeScript code for API clients and request methods from OpenAPI specifications. It can be used as a command line tool or as a Node.js module which is integrated into your build process. It supports both OpenAPI 2.0 and 3.0 specifications.
This tool is a wrapper of the swagger-typescript-api .
Install as global command line tool:
$ npm install -g swagger-api-hub
Install as project-wide dependency:
$ npm install -D swagger-api-hub
It reads the default configuration file ./swagger-api-hub.config.ts
, you can also specify a custom configuration file.
$ swagger-api-hub
$ swagger-api-hub config/services.ts
The configuration file should export an array of ServiceConfig
objects. If you export only one object, it will generate the code directly without prompting.
The config file can be either local path or npm module path, and the file extension can be .ts
, .js
, .json
, or any other module types supported by Node.js.
The file should look like this:
swagger-api-hub.config.ts
import type { ServiceConfig } from 'swagger-api-hub';
const services: ServiceConfig[] = [
{
id: 'iam',
name: 'User Management Service',
url: 'https://api.example.com/iam/swagger/v3',
output: './src/api/iam',
},
{
id: 'asset',
name: 'Asset Management Service',
url: 'https://api.example.com/public-api/asset/swagger/v3',
apiBase: '/public-api',
output: './src/api/asset',
},
];
export default services;
The ServiceConfig
object has the following properties:
id
: [required] A unique identifier for the service.name
: A human-readable name for the service.url
: [required] The URL of the OpenAPI specification file. If you have a local file, you can use input
instead of url
to specify the file path. Or you can even use spec
to provide the OpenAPI specification object directly. Either url, input, or spec is required.apiBase
: The base path of all API endpoints. The service may be hosted on a subpath of the main domain, e.g., /public-api/iam/v3, then the apiBase is /public-api in this case. If the original api path from the OpenAPI specification is acceptable, you don't need this field.crossOrigin
: Whether to use the absolute api path when calling the service. This is useful when the service is hosted on a different domain and you need to set the Access-Control-Allow-Origin
header. Default is false
.output
: The output directory for the generated code. The default is ./src/api/{id}
.httpClientFile
: Change the default path of http-client.ts
file, so you can use your own http client. The default is ./http-client
under each service directory.createApiInstance
: Whether to create an instance of each API class. The instance can only be created with an empty constructor, if you want to set different options for some api classes, you can set this to false
and create the instance manually. Default is true
.import { generate, promptToGenerate } from 'swagger-api-hub';
import type { ServiceConfig } from 'swagger-api-hub';
import services from './swagger-api-hub.config';
// Way1: Generate multiple services with prompts
promptToGenerate(services);
// Way2: Generate a single service directly
generate(services[0]);
FAQs
Generate front-end access code for swagger backend services in one click
The npm package swagger-api-hub receives a total of 1 weekly downloads. As such, swagger-api-hub popularity was classified as not popular.
We found that swagger-api-hub demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.