Here’s an example of how you can structure your router tasks and dependencies, following your instructions. This includes the definitions for a typical set of API routes, with support for standard HTTP methods (POST, GET, DELETE, PUT) and utilizing a parameter (id) in the route path.
Dependencies for the Router Tasks
{
"name": "api_tasks",
"schema": {
"type": "object",
"code": {
"prompt": {
"task_name": "CRUD Operations for API",
"description": "Define API routes for basic CRUD operations with task management using Express.",
"dependencies": [
{
"route": "GET /api/tasks",
"description": "Fetch all tasks",
"method": "GET",
"params": null,
"status": "pending"
},
{
"route": "GET /api/tasks/:id",
"description": "Fetch a single task by its ID",
"method": "GET",
"params": {
"id": "Task ID"
},
"status": "pending"
},
{
"route": "POST /api/tasks",
"description": "Create a new task",
"method": "POST",
"params": {
"body": "Task data (e.g., name, description)"
},
"status": "pending"
},
{
"route": "PUT /api/tasks/:id",
"description": "Update an existing task by its ID",
"method": "PUT",
"params": {
"id": "Task ID",
"body": "Updated task data"
},
"status": "pending"
},
{
"route": "DELETE /api/tasks/:id",
"description": "Delete a task by its ID",
"method": "DELETE",
"params": {
"id": "Task ID"
},
"status": "pending"
}
],
"status": "pending",
"isImport": false
}
}
}
}
Example Router Implementation (Express.js)
Here’s a basic implementation of the routes using Express.js:
const express = require('express');
const router = express.Router();
// Sample database array
let tasks = [];
// GET all tasks
router.get('/api/tasks', async (req, res) => {
res.status(200).json(tasks);
});
// GET task by ID
router.get('/api/tasks/:id', async (req, res) => {
const task = tasks.find(t => t.id === req.params.id);
if (!task) return res.status(404).send('Task not found');
res.status(200).json(task);
});
// POST create a new task
router.post('/api/tasks', async (req, res) => {
const newTask = { id: Date.now().toString(), ...req.body };
tasks.push(newTask);
res.status(201).json(newTask);
});
// PUT update a task by ID
router.put('/api/tasks/:id', async (req, res) => {
const index = tasks.findIndex(t => t.id === req.params.id);
if (index === -1) return res.status(404).send('Task not found');
tasks[index] = { ...tasks[index], ...req.body };
res.status(200).json(tasks[index]);
});
// DELETE a task by ID
router.delete('/api/tasks/:id', async (req, res) => {
const index = tasks.findIndex(t => t.id === req.params.id);
if (index === -1) return res.status(404).send('Task not found');
tasks.splice(index, 1);
res.status(204).send(); // No content response
});
module.exports = router;
Summary
This code provides a comprehensive set of tasks for handling CRUD operations through an API. Each task outlines its method, route, and the parameters it requires. The example router illustrates how these tasks could be implemented in an Express application.