UMD Build (CDN and Local)
If your project does not use a bundler or support ES modules, you can use the UMD build of lunex-http
directly in the browser via a CDN or by hosting the script locally.
Using CDN
Include the UMD build script in your HTML using one of these CDNs:
jsDelivr CDN
<script src="https://cdn.jsdelivr.net/npm/lunex-http/dist/umd/lunex-client.umd.js"></script>
unpkg CDN
<script src="https://unpkg.com/lunex-http/dist/umd/lunex-client.umd.js"></script>
Using Locally Hosted UMD Build
- Install the package:
npm install lunex-http
- Copy the UMD bundle to your public directory:
cp node_modules/lunex-http/dist/umd/lunex-client.umd.js ./public/umd/
- Include the script in your HTML:
<script src="umd/lunex-client.umd.js"></script>
Usage Example
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/lunex-http/dist/umd/lunex-client.umd.js"></script>
</head>
<body>
<div id="output">Loading...</div>
<script>
const output = document.getElementById('output');
const LunexClient = window.LunexClient.default;
const LunexClientOptions = window.LunexClient.LunexClientOptions;
const options = new LunexClientOptions({
timeout: 8000,
maxRetries: 2
});
const client = new LunexClient('https://api.example.com', {}, options);
async function fetchUsers() {
try {
const users = await client.getAsync('users');
output.innerHTML = ``;
} catch (error) {
output.textContent = 'Error fetching users: ' + error.message;
}
}
fetchUsers();
</script>
</body>
</html>
Note on Local Usage
If you host the UMD bundle locally, simply update the <script>
tag’s src
attribute to point to your local file path, for example:
<script src="umd/lunex-client.umd.js"></script>
Notes
- The UMD build exposes LunexClient as a global variable on window.
- This approach is suitable for legacy projects or simple static sites without build tools.
- For production applications, bundling or native ESM imports are recommended for better performance and caching.