Skip to main content

CommonJS (CJS)

This playground demonstrates usage of the Lunex HTTP Client with the CommonJS (CJS) module format.

Select an HTTP method below to see example code and run requests live:

    // App.js
    const React = require("react");
    const { useEffect, useState } = React;
    const { default: LunexClient, LunexClientOptions } = require("lunex-http");

    function App() {
      const [data, setData] = useState("");

      useEffect(() => {
        const options = new LunexClientOptions({ timeout: 8000, maxRetries: 2 });
        const client = new LunexClient("https://jsonplaceholder.typicode.com", {}, options);

        async function fetchData() {
          try {
            const result = await client.getAsync("users");
            setData(JSON.stringify(result, null, 2));
          } catch (error) {
            setData(`Error: ${error.message}`);
          }
        }

        fetchData();
      }, []);

      return React.createElement("pre", null, data);
    }

    module.exports = App;