Passer au contenu

SDK JavaScript

Voir en Markdown

Installation

Fenêtre de terminal
npm install @syvel/js
# ou
yarn add @syvel/js

Utilisation basique

import { Syvel } from "@syvel/js";
const syvel = new Syvel({
apiKey: "sv_votre_cle",
});
try {
const result = await syvel.check("yopmail.com");
console.log(result.risk_score); // 100
console.log(result.is_risky); // true
console.log(result.reason); // "disposable"
const emailResult = await syvel.checkEmail("[email protected]");
} catch {
// API indisponible — continuer normalement
}

Validation de formulaire HTML

<form id="signup-form">
<input type="email" id="email" name="email" placeholder="[email protected]" />
<p id="email-error" style="color: red; display: none;"></p>
<button type="submit">S'inscrire</button>
</form>
<script type="module">
import { Syvel } from "https://cdn.jsdelivr.net/npm/@syvel/js";
const syvel = new Syvel({ apiKey: "sv_votre_cle" });
const emailInput = document.getElementById("email");
const emailError = document.getElementById("email-error");
let debounceTimer;
emailInput.addEventListener("input", () => {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(async () => {
const email = emailInput.value;
if (!email.includes("@")) return;
try {
const result = await syvel.checkEmail(email);
if (result?.is_risky) {
emailError.textContent = "Merci d'utiliser une adresse email professionnelle.";
emailError.style.display = "block";
} else {
emailError.style.display = "none";
}
} catch {
emailError.style.display = "none";
}
}, 500);
});
</script>

React Hook

import { useState, useCallback } from "react";
import { Syvel } from "@syvel/js";
const syvel = new Syvel({ apiKey: import.meta.env.VITE_SYVEL_API_KEY });
export function useEmailCheck() {
const [result, setResult] = useState(null);
const [loading, setLoading] = useState(false);
const check = useCallback(async (email: string) => {
if (!email.includes("@")) return;
setLoading(true);
try {
const data = await syvel.checkEmail(email);
setResult(data);
} catch {
setResult(null);
} finally {
setLoading(false);
}
}, []);
return { result, loading, check };
}

Configuration avancée

const syvel = new Syvel({
apiKey: "sv_votre_cle",
timeout: 3000,
silent: true,
});

Ressources

Dernière mise à jour :