Skip to main content

Vite Vanilla JavaScript Simple App

This example demonstrates how to use the lunex-http library in a modern Vanilla JavaScript app scaffolded with Vite. It shows a minimal setup that runs in the browser with ES module support and leverages Vite’s fast development server.

How to run this example

  • Run npm install (to install dependencies including lunex-http).
  • Run npm run dev to start the Vite dev server.
  • Open http://localhost:5173 (or the port shown in your terminal) in a modern browser.
Note

Vite automatically supports ES modules and hot module replacement, making it ideal for frontend development with libraries like lunex-http.

What this example shows

  • How to import lunex-http directly in an ES module.
  • How to instantiate LunexClient with a base API URL.
  • How to make asynchronous requests with getAsync.
  • Basic DOM manipulation to show a simple greeting.
  • Console output of the fetched API response.

Notes

  • Replace 'https://api.example.com/' in main.js with your actual API endpoint.
  • Vite requires Node.js and npm/yarn installed on your system.
  • This example assumes a simple user endpoint at users for demonstration.

Source Code

index.html

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Lunex HTTP</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>

src/main.js

import './style.css'
import LunexClient from 'lunex-http'

document.querySelector('#app').innerHTML = `Hello World`

const client = new LunexClient('https://api.example.com/')
console.log('LunexClient created')

async function fetchUsers() {
try {
let response = await client.getAsync('users')
console.log(response)
} catch (error) {
console.error('Error fetching users:', error)
}
}

fetchUsers()
:root {
font-family: system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;

color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;

font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}

body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}

h1 {
font-size: 3.2em;
line-height: 1.1;
}

#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vanilla:hover {
filter: drop-shadow(0 0 2em #f7df1eaa);
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}

button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}

@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}