1.3.0 • Published 5 months ago

mager v1.3.0

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

mager

Mager is my personal collection of my repetitive code that gives me hassle


Table of Contents


Installation

npm

npm i mager

yarn

yarn add mager

API

axiosGroup

axiosGroup( arrayOfRequest:array ) run axios request in Promise.All

import axios from 'axios'
import { axiosGroup } from 'mager'

const getProfile = async () => {
	return axios.request({
		method: 'GET',
		url: '/api/profile/me'
	})
}
const getNotifications = async () => {
	return axios.request({
		method: 'GET',
		url: '/api/notifications'
	})
}

const [responseGetProfile, responseGetNotifications] = await axiosGroup([getProfile(), getNotifications()])

if (responseGetProfile.status === 200) {
	console.log(responseGetProfile.response)
} else {
	console.log(responseGetProfile.error.response)
}
if (responseGetNotifications.status === 200) {
	console.log(responseGetNotifications.response)
} else {
	console.log(responseGetNotifications.error.response)
}

routeGuard

routeGuard( allowed:array, redirectTo:string, returnValue:object ) function handler to validate some rules in array

import { routeGuard } from 'mager/next'

export const getServerSideProps = async ({ req, query, ..._other }) => {
	const accessToken = req.session?.auth?.access_token
	const userRole = req.session?.auth?.role
	const isLoggedIn = !!accessToken
	const isSuperAdmin = userRole === 'super-admin'
	const validator = [isLoggedIn, isSuperAdmin]
	// if the validator contains a false value, routeGuard will redirect to /login (the value of the second param), otherwise routeGuard will pass the return value based on the value of the third param
	return routeGuard(validator, '/login', {
		props: { query }
	})
}
1.3.0

5 months ago

1.2.0

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago

1.1.0

9 years ago