1.0.1 • Published 8 months ago

@lacspace/api v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

@lacspace/api

A TypeScript-based npm package to streamline API configuration and requests for any REST API. Set up your API once and call any endpoint effortlessly.

Features

  • Interactive CLI: Configure API_URL, API_KEY, and timestamp with a single command.
  • Universal Endpoints: Works with any API endpoint (e.g., /products, /login, /categories).
  • Secure Storage: Saves config in lacspace-api-config.json to keep sensitive data safe.
  • Type-Safe: Built with TypeScript for robust development.
  • HTTP Methods: Supports GET, POST, PUT, DELETE via axios.
  • Easy Updates: Modify API settings anytime via CLI.

Installation

Install the package:

npm install @lacspace/api

For TypeScript projects, ensure TypeScript is set up:

npm init -y
npm install typescript --save-dev
npx tsc --init

Setup
Configure your API settings:

npx lacspace-api-config

You'll be prompted for:

API_URL (required): Base URL of your API (e.g., https://api.lacspace.com/api or https://api.grocista.com/api).
API_KEY (optional): API key for authentication (e.g., 99f74459341f35151e1092d0bf6a18ab5986d449687e28380c90cccaLacSpacE).
Timestamp (optional): Custom timestamp or defaults to Date.now().
This generates a lacspace-api-config.json file in your project root:

{
  "API_URL": "https://api.lacspace.com/api",
  "API_KEY": "99f74459341f35151e1092d0bf6a18ab5986d449687e28380c90cccaLacSpacE",
  "timestamp": "1623456789000"
}


Usage
Import LacspaceAPI and make requests to any endpoint.

Example 1: Fetch Products
Retrieve a list of products:


import LacspaceAPI from '@lacspace/api';

LacspaceAPI.get('/products')
  .then(products => console.log('Products:', products))
  .catch(err => console.error('Error:', err));

Example 2: User Login
Authenticate a user:

import LacspaceAPI from '@lacspace/api';

LacspaceAPI.post('/login', { username: 'john_doe', password: 'secure123' })
  .then(response => console.log('Login response:', response))
  .catch(err => console.error('Login error:', err));
  

Example 3: Update a Product
Update an existing product:
import LacspaceAPI from '@lacspace/api';

LacspaceAPI.put('/products/42', { name: 'Super Widget', price: 19.99 })
  .then(updated => console.log('Updated product:', updated))
  .catch(err => console.error('Update error:', err));



Example 4: Delete a Category
Remove a category:

import LacspaceAPI from '@lacspace/api';

LacspaceAPI.delete('/categories/7')
  .then(() => console.log('Category deleted'))
  .catch(err => console.error('Delete error:', err));

Update Configuration
To modify API settings (e.g., change API_URL or remove API_KEY):

npx lacspace-api-config

This re-runs the CLI prompt and updates lacspace-api-config.json. Example updated config:

{
  "API_URL": "https://api.grocista.com/api",
  "API_KEY": null,
  "timestamp": null
}