serverless-cron-jobs
v1.0.5
Published
A library to schedule cron jobs and fetch data from APIs using Node.js
Downloads
73
Maintainers
Readme
Serverless Cron Jobs Library
A simple library to schedule cron jobs and fetch data from APIs using Node.js. This library allows you to define cron jobs with custom schedules and handle errors gracefully.
Installation
To use this library in your project, install it via npm or yarn:
npm install serverless-cron-jobs
OR
yarn add serverless-cron-jobs
Importing the Library
import { scheduleJob, fetchDataFromApi } from 'serverless-cron-jobs';
Scheduling a Cron Job
Use the scheduleJob function to schedule a cron job. The function takes the following parameters: cronExpression (string): The cron expression that defines the schedule. job (JobFunction): A function that will be executed at the scheduled times. This function takes an optional URL parameter. url (string, optional): A URL to be passed to the job function.
Example
import { scheduleJob, fetchDataFromApi } from 'serverless-cron-jobs';
// URL of the API to fetch data from
const apiUrl = 'https://api.example.com/data'; // Replace with your API URL
// Schedule a cron job to fetch data from the API every day at midnight
scheduleJob('0 0 * * *', fetchDataFromApi, apiUrl);
fetchDataFromApi
Fetches data from a specified URL and logs the data or errors.
Parameters
url
(string): The URL from which to fetch data.timeout
(number, optional): The timeout for the request in milliseconds (default is 5000).
Returns
- A promise that resolves when the data is successfully retrieved and parsed, or rejects if an error occurs.
Example
import { fetchDataFromApi } from 'serverless-cron-jobs';
const apiUrl = 'https://api.example.com/data';
fetchDataFromApi(apiUrl)
.then(() => {
console.log('Data fetched and processed successfully');
})
.catch((error) => {
console.error('Error fetching data:', error);
});
Key Sections Explained:
- Installation: Instructions on how to add the library to a project.
- Usage: Shows how to use the
scheduleJob
andfetchDataFromApi
functions with code examples. - API Documentation: Detailed information about the functions available in the library.
- Error Handling: Information on how the library handles errors.
- Testing: Instructions for running tests using Jest.
- License: Details about the licensing of the library.
Feel free to adjust the content to better fit your library's specific details or your personal preferences!