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

@midwayjs/react

v4.1.0

Published

Midway React API bridge component

Downloads

296

Readme

@midwayjs/react

React bridge contracts for consuming Midway functional API definitions in web applications.

Install

npm i @midwayjs/react @midwayjs/web-bridge
{
  "dependencies": {
    "@midwayjs/react": "^4.0.0-beta.11",
    "@midwayjs/web-bridge": "^4.0.0-beta.11"
  }
}

Recommended Project Layout

src/
  server/
    api/
      user.api.ts
  web/
    main.tsx
    app.tsx

Runtime Boundary

  • React continues using its own router/runtime.
  • @midwayjs/react only provides bridge client helpers.
  • It does not replace route matching.

Server API Definition

// server/api/user.api.ts
import { defineApi } from '@midwayjs/core/functional';

export const userApi = defineApi('/users', api => ({
  getUser: api.get('/:id').handle(async () => {
    return { id: '1', name: 'harry' };
  }),
}));

Web-side Usage

import {
  MidwayApiProvider,
  createClient,
  useMidwayApiOperation,
} from '@midwayjs/react';
import { userApi } from '@/server/api/user.api';

const api = createClient(
  {
    user: userApi,
  },
  {
    basePath: '/api',
  }
);

function UserPage() {
  const callGetUser = useMidwayApiOperation('user.getUser');
  const user = await callGetUser({ params: { id: '1' } });
  return <div>{user.name}</div>;
}

// app root
<MidwayApiProvider client={api}>
  <UserPage />
</MidwayApiProvider>;

Direct-like Pattern (Recommended)

// src/server/api/index.ts
export { userApi } from './user.api';
// src/web/api/client.ts
import { createClient } from '@midwayjs/react';
import { userApi } from '@/server/api';

export const api = createClient(
  {
    user: userApi,
  },
  {
    basePath: '/api',
  }
);

const user = await api.user.getUser({
  params: { id: '123' },
});

Web 侧只依赖 src/server/api 定义,不需要额外维护一份 shared contracts。

Alternative (Operation List)

import { createReactApiClientFromOperations } from '@midwayjs/react';

const api = createReactApiClientFromOperations(manifestOperations, {
  adapter: async ({ operation, input }) => {
    return fetch(operation.fullPath, {
      method: operation.method.toUpperCase(),
      body: JSON.stringify(input),
    }).then(res => res.json());
  },
});

默认 HTTP 调用使用 fetch。如果你要接入 axios 或 tRPC,给 createClient(..., { adapter }) 传入自定义 adapter。

End-to-end Steps

  1. Use defineApi in src/server/api.
  2. Import userApi in web side.
  3. Create client with createClient({ user: userApi }, ...).
  4. Call by namespaced operationId via hook: useMidwayApiOperation('user.getUser').

Dev Workflow (Single Command)

Use Vite with two plugins:

  • @midwayjs/mock/vite devPlugin(...) for embedded Midway HTTP runtime.
  • @midwayjs/web-bridge/vite apiPlugin(...) for browser-side contract transform.
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { devPlugin } from '@midwayjs/mock/vite';
import { apiPlugin } from '@midwayjs/web-bridge/vite';

export default defineConfig({
  plugins: [
    devPlugin({
      appDir: process.cwd(),
      baseDir: 'src/server',
      basePath: '/api',
    }),
    react(),
    apiPlugin({
      root: process.cwd(),
      apiDir: 'src/server/api',
      target: 'both',
    }),
  ],
});

Rspack Usage

import { defineConfig } from '@rspack/cli';
import { createApiRspackRule } from '@midwayjs/web-bridge/rspack';

export default defineConfig({
  module: {
    rules: [
      createApiRspackRule({
        root: process.cwd(),
        apiDir: 'src/server/api',
      }),
    ],
  },
});

Plugin Options

devPlugin (@midwayjs/mock/vite):

  • appDir (required): project root (recommended process.cwd()).
  • baseDir (optional): Midway server source root, default src.
  • basePath (optional): API prefix forwarded to Midway runtime, default /api.

apiPlugin (@midwayjs/web-bridge/vite):

  • root (optional): project root, default process.cwd().
  • apiDir (required): API definition directory (recommended src/server/api).
  • target (optional): transform target, default client.
    • client: transform browser imports only.
    • ssr: transform SSR imports only.
    • both: transform both browser and SSR imports.

Hot Reload Notes

  • API source change (src/server/**/*.ts) triggers Midway app reload in dev.
  • Web-side import of src/server/api is transformed to browser-safe contracts and invalidated on hot update.
  • Dev reload strategy is close old app -> create new app, not in-place module patching.

For heavy connection dependencies (Redis, MQ, long-lived sockets), prefer running backend as an independent process and proxy /api from Vite for a more stable dev loop.