@d2sutils/shopper
v1.5.5
Published
This is a simple utility service that provides a set of utility functions for the shopper service.
Downloads
59
Readme
Utils
https://www.npmjs.com/package/@d2sutils/shopper
Description
This is a simple utility service that provides a set of utility functions for the shopper service.
The utils-npm
package provides a convenient and efficient way to manage and interact with user data in your applications. Built with TypeScript, it offers strong typing and easy integration for projects using Axios for HTTP requests.
Installation
npm install
import {user,notification, api } from "@d2sutils/shopper";
User
import {user} from "@d2sutils/shopper";
- Users: list of users
- List shoppers: Retrieve a list of shoppers based on provided parameters.
- List clients: Retrieve a list of clients based on provided parameters.
- Global Settings: Retrieve global settings for the user service.
- Groups (Filter): Filter groups based on provided parameters.
- Check Client ID: Verify if a user is a client based on their ID.
- Check User ID: Quickly verify the existence of a user by their ID.
- Get User by ID: Retrieve detailed user information by ID.
- Current User Information: Obtain the current user's information based on a provided token.
- Send Notifications: Easily send notifications with customizable parameters including action types, user details, and message content.
List all users
user.users({
filter: {
name: 'Test', // Optional filter by name
},
limit: 10,
}).then(users => {
console.log(users);
})
List shoppers
user.shoppers({
filter: {
name: 'Test', // Optional filter by name
},
limit: 10,
}).then(users => {
console.log(users);
})
List clients
user.clients({
filter: {
name: 'Test', // Optional filter by name
},
limit: 10,
}).then(users => {
console.log(users);
})
Global setting
user.getSettings().then(setting => {
console.log(setting);
})
Groups (filter)
user.groups({filter: {name:1}}).then(setting => {
console.log(setting);
})
Check client id
const userId = 1; // Example user ID
//const userId = [1,2,3]; // Example users ID
user.isClient(userId).then(clients => {
console.log(clients);
})
Check manager id
const userId = 1; // Example user ID
//const userId = [1,2,3]; // Example users ID
user.isManager(userId).then(clients => {
console.log(clients);
})
Checking if a User Exists
const userId = 1; // Example user ID
//const userId = [1,2,3]; // Example users ID
user.checkUserId(userId).then(exists => {
console.log(exists ? 'User exists.' : 'User does not exist.');
});
Getting a User by ID
const userId = 1; // Example user ID
user.getById(userId).then(user => {
console.log(user);
}).catch(error => {
console.error(error);
});
Getting Current User Information
const token = 'your_token_here'; // User's token
user.me(token).then(currentUser => {
console.log(currentUser);
}).catch(error => {
console.error(error);
});
API Utils
import { api } from '@d2sutils/shopper';
API Utils provides a simple way to make HTTP requests to a RESTful API. It supports GET, POST, PUT requests with customizable headers and parameters.
api.get('users', 'list', { age: 25 })
.then(data => console.log('Users:', data))
.catch(err => console.error('Error fetching users:', err));
api.post('users', 'create', { name: 'John', age: 30 })
.then(data => console.log('User created:', data))
.catch(err => console.error('Error creating user:', err));
api.put('users', 'update/123', { name: 'Jane', age: 31 })
.then(data => console.log('User updated:', data))
.catch(err => console.error('Error updating user:', err));
Cache Utils
- Get Cache: Retrieve data from the cache based on a key.
- Set Cache: Store data in the cache based on a key.
- Delete Cache: Remove data from the cache based on a key.
- Clear Cache: Clear all data from the cache.
Format data Utils
- body: Format data to be sent in the body of a request.
import { format } from '@d2sutils/shopper';
Body
const data = format.body(req.body);
Apply filters
import AnyModel from 'any-model';
const filter = req.params.filter; // Example filter
const data = format.applyFilter(AnyModel,filter);
Locations
- All: All locations based on parameters.
- Groups: All groups based on parameters.
import { location } from '@d2sutils/shopper';
All locations
location.all({
filter: {
client_id: 1, // Optional client ID
name: 'Test', // Optional location name
},
limit: 10,
id: 1, // Optional location ID
}).then(response => {
console.log('Locations:', response);
}).catch(error => {
console.error('Failed to get locations:', error);
});
All groups location
location.groups({
limit: 10,
parent_id: 1, // Optional client ID
name: 'Test', // Optional location name
id: 1, // Optional location ID
}).then(response => {
console.log('Locations:', response);
}).catch(error => {
console.error('Failed to get locations:', error);
});
Notification
- Send Notifications: Easily send notifications with customizable parameters including action types, user details, and message content.
import { notification } from '@d2sutils/shopper';
Sending a Notification
notification.send({
action: 'UserRegistration', // Example action
user_id: '12345', // Optional user ID
contact: '[email protected]', // Optional contact detail
communication_id: 'abcde', // Optional communication ID
communication_name: 'WelcomeEmail', // Optional communication name
subject: 'Welcome to Our Service', // Optional email subject
message: 'Thank you for registering.', // Required message content
params: {}, // Optional additional parameters
header: { 'Authorization': 'Bearer your_token_here' } // Optional additional headers
}).then(response => {
console.log('Notification sent successfully:', response);
}).catch(error => {
console.error('Failed to send notification:', error);
});