1.0.8 • Published 2 years ago
api-key-service v1.0.8
Api Key Service
api-key-service provides a class called ApiKeyService that allows you to interact with an Api key service. It offers the following functionality:
- Generate Api Key: You can generate a new Api key with arbitrary properties and values.
- Update Api Key: You can update an existing Api key by modifying its properties.
- Validate Api Key: You can validate an Api key by checking the values of its properties.
- Get all Api Keys: You can retrieve all Api keys stored in the service.
- Delete Api Key: You can delete an Api key from the service.
Installation
First, install the api-key-service package using npm:
$ npm install api-key-serviceAfter you have it installed you need to get an api key from the Api key service
Below are examples that demonstrate the usage of the ApiKeyService class
const { ApiKeyService } = require("api-key-service");
async function someFunction() {
  const apiKeyService = new ApiKeyService("your-api-key");
  // Generate an Api Key
  const initialProperties = {
    enabled: false,
    isAdmin: true,
    ip: "127.0.0.1",
    five: 5
  };
  const key = await apiKeyService.generateApiKey(initialProperties);
  const { apiKey } = key;
  console.log("Generated Api Key:", key);
  // Update an Api Key
  const updatedProperties = {
    enabled: true,
    isAdmin: false,
    ip: "localhost",
    five: 5
  };
  const updatedKey = await apiKeyService.updateApiKey(apiKey, updatedProperties);
  console.log("Updated Api Key:", updatedKey);
  // Validate an Api Key
  const propertiesToValidate = {isAdmin: true, ip: "127.0.0.1", five: 4};
  const validationResult = await apiKeyService.validate(apiKey, propertiesToValidate);
  console.log("Validation Result:", validationResult);
  // Get all Api Keys
  const allKeys = await apiKeyService.getApiKeys();
  console.log("All Api Keys:", allKeys);
  // Delete an Api Key
  const deletedKey = await apiKeyService.deleteApiKey(apiKey);
  console.log("Deleted Api Key:", deletedKey);
}Example output
Please note that the enabled property is set to true by default unless specified otherwise. If the enabled property is explicitly set to false, the apiKeyService.validate method will always return { valid: false }
Generated Api Key: {
  enabled: false,
  isAdmin: true,
  ip: '127.0.0.1',
  five: 5,
  apiKey: 'someRandomKey'
}
Updated Api Key: {
  enabled: true,
  isAdmin: false,
  ip: 'localhost',
  five: 5,
  apiKey: 'someRandomKey'
}
Validation Result: { valid: false, invalid: { isAdmin: false, ip: 'localhost', five: 5 } }
All Api Keys: [
  {
    enabled: true,
    isAdmin: false,
    ip: 'localhost',
    five: 5,
    apiKey: 'someRandomKey'
  }
]
Deleted Api Key: {
  enabled: true,
  isAdmin: false,
  ip: 'localhost',
  five: 5,
  apiKey: 'someRandomKey'
}Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.