npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

electron-nest-core

v1.0.8

Published

Electron-nest IPC 装饰器,HTTP 服务支持,数据库支持

Readme

Electron Nest Core

一个用于 Electron 应用的装饰器和 HTTP 服务支持库,让你能像 Nest.js 一样使用装饰器来处理 IPC 通信和 HTTP 请求。

特性

  • 支持 IPC 通信的装饰器
  • 支持 HTTP 服务
  • 支持 DTO 验证
  • 支持 CORS 跨域
  • TypeScript 支持

安装

npm install electron-nest-core --save

依赖

本库需要以下依赖:

npm install class-transformer class-validator reflect-metadata --save

基本使用

初始化 HTTP 服务

import { app } from 'electron';
import { initializeHttpServer } from 'electron-nest-core';

app.whenReady().then(() => {
  // 初始化 HTTP 服务
  initializeHttpServer({
    enable: true,
    port: 3000,
    baseRoutePrefix: '/api'
  });
});

创建 IPC 控制器

import { IpcController, IpcHandler } from 'electron-nest-core';
import { IsString, IsNotEmpty } from 'class-validator';

// 定义 DTO
class UserDTO {
  @IsString()
  @IsNotEmpty()
  name: string;
}

// 定义控制器
@IpcController({
  prefix: 'user',
  enableHttp: true
})
class UserController {
  @IpcHandler('create', UserDTO)
  async createUser(event, userData: UserDTO) {
    return {
      success: true,
      message: '用户创建成功',
      data: userData
    };
  }
}

// 实例化控制器
const userController = new UserController();

客户端调用方式

通过 IPC 调用

// 在渲染进程中
const result = await window.electron.ipcRenderer.invoke('user/create', {
  name: '张三'
});

通过 HTTP 调用

// 使用 fetch API
const response = await fetch('http://localhost:3000/api/user/create', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    name: '张三'
  })
});
const result = await response.json();

高级用法

配置 CORS

import { initializeHttpServer, getHttpServer } from 'electron-nest-core';

// 方法 1: 初始化时配置
initializeHttpServer({
  cors: {
    enable: true,
    options: {
      origin: ['http://localhost:8080'],
      methods: ['GET', 'POST'],
      credentials: true
    }
  }
});

// 方法 2: 获取实例后配置
const httpServer = getHttpServer();
httpServer.setCorsOptions({
  origin: '*',
  methods: ['GET', 'POST']
});

// 方法 3: 禁用 CORS
httpServer.enableCors(false);

添加自定义中间件

import { getHttpServer } from 'electron-nest-core';

const httpServer = getHttpServer();
httpServer.use((req, res, next) => {
  console.log(`[${req.method}] ${req.url}`);
  next();
});

API 参考

IpcController 装饰器

@IpcController(options: string | IpcControllerOptions)

参数:

  • options: 字符串前缀或控制器选项对象
    • prefix: 路由前缀
    • enableHttp: 是否启用 HTTP 支持

IpcHandler 装饰器

@IpcHandler(path: string, dtoClass?: any)

参数:

  • path: 处理器路径
  • dtoClass: 可选的 DTO 类用于验证

HTTP 服务

initializeHttpServer(options?: Partial<HttpServerOptions>)

参数:

  • options: HTTP 服务配置选项
    • enable: 是否启用 HTTP 服务
    • port: 端口号
    • baseRoutePrefix: 路由前缀
    • cors: CORS 配置
getHttpServer(): HttpServerService

返回 HTTP 服务实例,可用于添加自定义中间件或路由。

许可证

MIT