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

@ahamove/js-function

v1.2.1

Published

[![NPM Version](https://img.shields.io/npm/v/%40ahamove%2Fjs-function)](https://www.npmjs.com/package/@ahamove/js-function) ![NPM Unpacked Size](https://img.shields.io/npm/unpacked-size/%40ahamove%2Fjs-function) ![NPM Downloads](https://img.shields.io/npm

Downloads

283

Readme

@ahamove/js-function

NPM Version NPM Unpacked Size NPM Downloads

The @ahamove/js-function library facilitates seamless communication between web and mobile applications through event-based interactions. Designed specifically for use within the Ahamove app, this Node.js library enables the web application to connect with mobile functionality by defining and handling events on both platforms. This integration supports dynamic interactions, allowing web components to trigger mobile-side actions and respond to mobile events, enhancing the in-app experience by bridging mobile and web capabilities.

Installation

You can install @ahamove/js-function using npm or dynamically load it from a URL.

1. NPM Installation

To install via npm, run:

npm install @ahamove/js-function

2. Dynamic Loading with loadJS

Alternatively, you can use loadJS from @ahm/common-helpers to dynamically load the library as an IIFE bundle:

import { loadJS } from "@ahm/common-helpers";

loadJS(
  "https://vncdn.ahamove.com/public/js-function/{{VERSION}}/index.js"
)
  .then(() => {
    console.log("JSFunction library loaded successfully");
    // Access the library with window.JSFunction
    window.JSFunction.call({
      name: "openPage",
      body: "add_ahamove_deep_link_here",
    });
  })
  .catch((error) => {
    console.error("Failed to load JSFunction library:", error);
  });

Usage

The library supports both module-based and global usage:

  • If installed via npm, you can import JSFunction and use it directly.
  • If loaded dynamically, JSFunction will be available globally as window.JSFunction.

Importing the Library (NPM)

import { JSFunction } from "@ahamove/js-function";

or import the default export:

import JSFunction from "@ahamove/js-function";

Sending Events

Use the call method to push events to the mobile app. Here are a few examples:

// Open a new order page
JSFunction.call({
  name: "openPage",
  body: "add_ahamove_deep_link_here",
});

// Close the WebView
JSFunction.call({
  name: "close",
});

// Navigate back
JSFunction.call({
  name: "back",
});

Handling Callback with call

You can handle responses from the mobile app directly using the callback parameter in the call function:

JSFunction.call({ name: "getToken" }, (messageEvent) => {
  console.log("Token received:", messageEvent.data);
});

Using window.JSFunction in TypeScript

When loading @ahamove/js-function dynamically via CDN, JSFunction will be available globally as window.JSFunction. To avoid TypeScript errors like Property 'JSFunction' does not exist on type 'Window', you need to declare window.JSFunction in the global scope.

Declare window.JSFunction in TypeScript

Add the following global declaration to your TypeScript file to define window.JSFunction:

import type { JSFunctionType } from "@ahamove/js-function";

declare global {
  interface Window {
    JSFunction: JSFunctionType;
  }
}

This declaration will allow you to access window.JSFunction without TypeScript errors:

window.JSFunction.call({
  name: 'openPage',
  body: 'add_ahamove_deep_link_here',
});

Optional: In case you want to handle the message event yourself

Alternatively, if you prefer managing the response with a message event listener, here’s an example in React:

import React, { useEffect, useState } from "react";
import { JSFunction } from "@ahamove/js-function";

function App() {
  const [callbackData, setCallbackData] = useState(null);

  useEffect(() => {
    const messageHandler = (messageEvent) => {
      console.log("Message received:", messageEvent);
      setCallbackData(JSON.stringify(messageEvent.data));
      if (messageEvent.data?.webinapp) {
        console.log("Event data contains webInApp:", messageEvent.data);
      } else {
        console.warn(
          "Event data does not contain webInApp:",
          messageEvent.data
        );
      }
      window.removeEventListener("message", messageHandler);
    };

    window.addEventListener("message", messageHandler);

    JSFunction.call({ name: "getToken" });

    return () => window.removeEventListener("message", messageHandler);
  }, []);

  return <div>Callback Data: {callbackData}</div>;
}

export default App;

Optional: Enable Logging

For debugging purposes, you can enable logging:

JSFunction.enableLogger(true);

This will log event information to the console.

API Reference

JSFunction.call(data: { name: WebInAppEvent, title?: string, body?: object }, callback?: (event: MessageEvent) => void)

Send a WebView event to the Ahamove mobile application.

  • data: An object containing event details.
    • name: The event name, one of the WebInAppEvent types.
    • title: Optional title for events involving UI updates.
    • body: Optional data for event-specific parameters.
  • callback: Optional callback function to handle responses from the mobile app.

JSFunction.enableLogger(enable: boolean)

Enable or disable logging to the console for debugging purposes.


Here's an updated README.md that includes instructions for using @ahamove/js-function in TypeScript, both as an npm module and as a global variable accessed through window.JSFunction. Additionally, it includes steps for declaring window.JSFunction with TypeScript to avoid type errors.