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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@webviewkit/bridge

v4.0.0

Published

A bridge library for WebView and Native communication

Downloads

3

Readme

@webviewkit/bridge

@webviewkit/bridge is a TypeScript library that facilitates communication between WebView and native platforms. It provides a flexible, version-aware bridge implementation for seamless interaction in hybrid mobile applications.

Installation

npm install @webviewkit/bridge
# or
yarn add @webviewkit/bridge
# or
pnpm add @webviewkit/bridge

Features

  • Type-safe communication between WebView and native platforms
  • Intelligent version-aware API handling
  • Support for iOS, Android, and React Native
  • Customizable error handling
  • Flexible request and response handlers
  • Automatic selection of the most appropriate handler based on semantic versioning

Usage

Here's a basic example of how to use @webviewkit/bridge:

import { createBridge } from "@webviewkit/bridge";

// Define request types
interface UserProfileRequestTypes extends IRequestTypes {
  getUserProfile: {
    default: {
      params: { userId: string };
      result: { id: string; name: string };
    };
    "1.0.0": {
      params: { userId: string };
      result: { id: string; name: string };
    };
    "2.0.0": {
      params: { userId: string; includeEmail: boolean };
      result: { id: string; name: string; email?: string };
    };
  };
  updateUserProfile: {
    default: {
      params: { userId: string; name: string };
      result: { success: boolean };
    };
    "1.0.0": {
      params: { userId: string; name: string };
      result: { success: boolean };
    };
    "2.0.0": {
      params: { userId: string; name: string; email?: string };
      result: { success: boolean; updatedAt: string };
    };
  };
}

// Define event types
interface UserEventTypes extends IEventTypes {
  onUserStatusChange: {
    default: { userId: string; status: "online" | "offline" };
    "1.0.0": { userId: string; status: "online" | "offline" };
    "2.0.0": {
      userId: string;
      status: "online" | "offline" | "away";
      lastSeen?: number;
    };
  };
}

// Create the bridge instance
const bridge = createBridge<UserProfileRequestTypes, UserEventTypes>({
  version: "2.0.0",
  bridges: {
    Android: {
      postMessage: (message: string) => {
        // Call Android native code
      },
    },
    iOS: {
      postMessage: (message: string) => {
        // Call iOS native code
      },
    },
    ReactNative: {
      postMessage: (message: string) => {
        // Call React Native native code
      },
    },
  },
  errorHandlers: {
    default: (error: Error) => {
      console.error("Bridge error:", error);
      return error;
    },
  },
  defaultTimeout: 5000,
});

// Make a request
async function getUserProfile(userId: string) {
  try {
    const [result, error] = await bridge.request("getUserProfile", [
      { version: "2.0.0", params: { userId, includeEmail: true } },
      { version: "1.0.0", params: { userId } },
      { version: "default", params: { userId } },
    ]);

    if (error) {
      console.error("Error fetching user profile:", error);
      return;
    }

    if (result) {
      console.log("User profile:", result);

      if (result.version === "2.0.0" && result.result.email) {
        console.log("User email:", result.result.email);
      }
    }
  } catch (error) {
    console.error("Unexpected error:", error);
  }
}

// Add a response listener
bridge.on("onUserStatusChange", (response) => {
  console.log("User status changed:", response);
  if (response.version === "2.0.0" && response.data.status === "away") {
    console.log("User last seen:", response.data.lastSeen);
  }
});

// Usage examples
getUserProfile("user123");

Version Handling

The bridge intelligently selects the most appropriate handler based on the current version:

  • If an exact version match is found, that handler is used.
  • If no exact match is found, it uses the handler from the highest version that's lower than the current version.
  • If no suitable version is found, it falls back to the 'default' handler.

For example, with handlers defined for versions "1.0.0", "1.6.1", and "2.0.1":

  • Version "1.2.1" will use the "1.0.0" handler
  • Version "1.6.1" will use the "1.6.1" handler
  • Version "2.0.3" will use the "2.0.1" handler
  • Version "0.6.1" will use the "default" handler

API

createBridge

A function that creates a new Bridge instance with version-aware handlers.

function createBridge<T extends IRequestTypes, E extends IEventTypes>(
  config: BridgeConfig
): Bridge<T, E>;

Bridge<T extends IRequestTypes, E extends IEventTypes>

The main class for handling bridge operations.

Methods

  • request<M extends keyof T>(methodName: M, requests: VersionedRequest<T, M>[]): Promise<[VersionedResponse<T, M> | null, Error | null]>

    Sends a request to the native platform, automatically selecting the appropriate version handler.

  • on<K extends keyof E>(eventName: K, handler: (response: EventResponse<E, K, keyof E[K] & string>) => void): void

    Adds a listener for a specific event type.

  • off<K extends keyof E>(eventName: K): void

    Removes a previously added listener.

Types

  • SemverVersion: Represents a semantic version string or "default".
  • IRequestTypes: Represents the interface for defining request types.
  • IEventTypes: Represents the interface for defining event types.
  • VersionedRequest<T extends IRequestTypes, M extends keyof T>: Represents a versioned request.
  • VersionedResponse<T extends IRequestTypes, M extends keyof T>: Represents a versioned response.
  • EventResponse<E extends IEventTypes, K extends keyof E>: Represents an event response.
  • BridgeConfig: Represents the configuration object for creating a Bridge instance.
    interface BridgeConfig {
      version: SemverVersion;
      bridges: Bridges;
      errorHandlers: ErrorHandlers;
      defaultTimeout?: number;
    }

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.