API Functions

getUserProfile(userId)

Retrieves the profile information for a given user ID.

Parameters:

  • userId: string | number - The unique identifier of the user.

Returns:

  • A Promise that resolves with an object containing user profile data.
  • The object has the following structure: { id: string, username: string, email: string, creationDate: string }.

Throws:

  • An Error if the userId is invalid or the user is not found.

Example:

async function displayUser(id) {
    try {
        const profile = await getUserProfile(id);
        console.log(`User: ${profile.username}`);
        console.log(`Email: ${profile.email}`);
    } catch (error) {
        console.error(`Failed to fetch user profile: ${error.message}`);
    }
}

displayUser('12345');

createItem(itemData)

Creates a new item with the provided data.

Parameters:

  • itemData: object - An object containing the details of the item to be created. Expected properties: { name: string, description: string, price: number }.

Returns:

  • A Promise that resolves with the newly created item object, including its generated ID.
  • The object has the following structure: { id: string, name: string, description: string, price: number, createdAt: string }.

Example:

const newItem = {
    name: 'Gadget X',
    description: 'A revolutionary new gadget.',
    price: 99.99
};

createItem(newItem)
    .then(created => console.log('Item created successfully:', created))
    .catch(error => console.error('Error creating item:', error));

deleteResource(resourceId, options)

Deletes a resource identified by its ID. Optional parameters can control the deletion behavior.

Parameters:

  • resourceId: string - The ID of the resource to delete.
  • options: object (optional) - An object with deletion options. Supported keys:
    • force: boolean - If true, performs a hard delete. Defaults to false (soft delete).
    • auditLog: boolean - If true, logs the deletion. Defaults to true.

Returns:

  • A Promise that resolves when the deletion is complete.
  • Returns true on successful deletion, false otherwise.

Example:

deleteResource('res-9876', { force: true, auditLog: false })
    .then(success => {
        if (success) {
            console.log('Resource deleted forcefully.');
        } else {
            console.log('Failed to delete resource.');
        }
    });