@grabjs/superapp-sdk
    Preparing search index...

    Class NetworkModule

    JSBridge module for making network requests via the native bridge.

    Provides access to native network functionality for making HTTP requests. This module is only for MiniApps hosted on the Grab domain to call authenticated Grab API endpoints. It should not be used by external MiniApps (not on Grab domain) or for non-authenticated Grab APIs. This code must run on the Grab SuperApp's WebView to function correctly.

    ES Module:

    import { NetworkModule } from '@grabjs/superapp-sdk';
    const networkModule = new NetworkModule();

    CDN (UMD):

    <script src="https://cdn.jsdelivr.net/npm/@grabjs/superapp-sdk@x.y.z/dist/index.js"></script>
    <script>
    const networkModule = new SuperAppSDK.NetworkModule();
    </script>

    Hierarchy (View Summary)

    Index

    Constructors

    Methods

    Constructors

    Methods

    • Sends a network request via the native bridge.

      Parameters

      • request: {
            body?: unknown;
            endpoint: string;
            headers?: { [key: string]: string };
            method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS";
            query?: { [key: string]: string };
            timeout?: number;
        }

        The network request parameters including endpoint, method, headers, query, body, and timeout. See SendRequest.

      Returns Promise<
          | { status_code: 204 }
          | { error: string; status_code: 400 }
          | { error: string; status_code: 403 }
          | { error: string; status_code: 500 }
          | { error: string; status_code: 501 }
          | { error: string; status_code: 404 }
          | { error: string; status_code: 424 }
          | { error: string; status_code: 426 }
          | { result: { [key: string]: unknown }; status_code: 200 }
          | { error: string; status_code: 401 },
      >

      The network response containing the result data or error information. See SendResponse.

      Simple usage

      import { NetworkModule, isSuccess, isError, hasResult } from '@grabjs/superapp-sdk';

      // Initialize the network module
      const network = new NetworkModule();

      // Send a POST request with headers and body
      const response = await network.send({
      endpoint: 'https://api.example.com/users',
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: { name: 'John', email: 'john@example.com' },
      timeout: 30
      });

      // Handle the response
      if (isSuccess(response) && hasResult(response)) {
      console.log('Success:', response.result);
      } else if (isError(response)) {
      console.error(`Error ${response.status_code}: ${response.error}`);
      } else {
      console.error('Unhandled response');
      }