UNPKG

verdantly

Version:

Node.js client for accessing plant species and variety data from the Verdantly API

76 lines 2.52 kB
// src/client/index.ts var VerdantlyClient = class { constructor(config) { if (!config.apiKey) { throw new Error("API Key is required to initialize VerdantlyClient."); } this.apiKey = config.apiKey; this.baseUrl = config.baseUrl || "https://verdantly-gardening-api.p.rapidapi.com"; } async getPlantVarietyCategories() { return this.apiRequest("/v1/plants/varieties/categories"); } async getTypesByCategory(category) { return this.apiRequest(`/v1/plants/varieties/types/${category}`); } async getSubtypesByType(type) { return this.apiRequest(`/v1/plants/varieties/subtypes/${type}`); } async searchPlantVarietiesByName(query, page = 1) { return this.apiRequest(`/v1/plants/varieties/name?q=${encodeURIComponent(query)}&page=${page}`); } async searchPlantVarietiesByFilter(filters, page = 1) { const queryParams = new URLSearchParams({ ...Object.entries(filters).reduce( (acc, [key, value]) => { if (value !== void 0 && value !== null) acc[key] = String(value); return acc; }, {} ), page: page.toString() }).toString(); return this.apiRequest(`/v1/plants/varieties/filter?${queryParams}`); } async searchPlantSpeciesByName(query, page = 1) { return this.apiRequest(`/v1/plants/species/name?q=${encodeURIComponent(query)}&page=${page}`); } async searchPlantSpeciesByFilter(filters, page = 1) { const queryParams = new URLSearchParams({ ...Object.entries(filters).reduce( (acc, [key, value]) => { if (value !== void 0 && value !== null) acc[key] = String(value); return acc; }, {} ), page: page.toString() }).toString(); return this.apiRequest(`/v1/plants/species/filter?${queryParams}`); } async apiRequest(endpoint, method = "GET", body) { const url = `${this.baseUrl}${endpoint}`; const headers = { "Content-Type": "application/json", "x-rapidapi-key": this.apiKey, "x-rapidapi-host": "verdantly-gardening-api.p.rapidapi.com" }; const response = await fetch(url, { method, headers, ...method === "POST" ? { body: JSON.stringify(body) } : {} }); if (!response.ok) { const message = await response.text(); throw new Error(`Request failed: ${response.status} ${response.statusText} ${message}`); } return response.json(); } }; export { VerdantlyClient }; //# sourceMappingURL=index.js.map