Gathering detailed insights and metrics for @opensource-dev/athena
Gathering detailed insights and metrics for @opensource-dev/athena
Gathering detailed insights and metrics for @opensource-dev/athena
Gathering detailed insights and metrics for @opensource-dev/athena
npm install @opensource-dev/athena
Typescript
Module System
Node Version
NPM Version
TypeScript (93.09%)
JavaScript (6.91%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
12 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Apr 24, 2025
Latest Version
1.0.8
Package Id
@opensource-dev/athena@1.0.8
Unpacked Size
15.32 kB
Size
3.99 kB
File Count
8
NPM Version
11.0.0
Node Version
20.18.2
Published on
Apr 24, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Athena
A Vuex-like state management system for React, providing a simple and flexible way to manage global state, actions, mutations, and getters. This package allows you to define modular state management in React using a structure similar to Vuex, which makes it easy to scale and maintain complex React applications.
You can install athena
via npm or yarn:
1npm install @opensource-dev/athena 2# or 3yarn add @opensource-dev/athena
This package provides the following components:
createGlobalStore
: A function to create a global store that includes multiple modules.useModule
: A custom hook to access state, mutations, actions, and getters from the store.Here’s a guide on how to integrate athena
into your React or React Native application.
Each module will have a structure containing states
, mutations
, actions
, and getters
.
1import { Module } from '@opensource-dev/athena'; 2 3const authModule: Module = () => ({ 4 name: 'auth', 5 6 states: { 7 username: null, 8 loggedIn: false 9 }, 10 11 mutations: { 12 SET_USERNAME(state, { payload }) { 13 state.username = payload 14 }, 15 16 SET_LOGGED_IN(state, { payload }) { 17 state.loggedIn = payload 18 } 19 }, 20 21 actions: { 22 async login({ commit }, params) { 23 try { 24 const response = await apiLogin(params) 25 commit('SET_USERNAME', { payload: response.username }) 26 commit('SET_LOGGED_IN', { payload: true }) 27 return response 28 } catch (error) { 29 return { error: { message: error.message } } 30 } 31 }, 32 33 async logout({ commit }) { 34 commit('SET_USERNAME', { payload: null }) 35 commit('SET_LOGGED_IN', { payload: false }) 36 } 37 }, 38 39 getters: { 40 isLoggedIn(state) { 41 return state.loggedIn 42 } 43 } 44})
name
: The name of the module (e.g., auth
, cart
).states
: The initial state of the module.mutations
: The mutations to modify the state.actions
: The asynchronous actions that dispatch mutations.getters
: The computed values based on the state.Once you have your modules, you can combine them into a global store using createGlobalStore
. You can then wrap your app in the Provider
component that this function returns.
1import { createGlobalStore } from '@opensource-dev/athena' 2import authModule from './modules/authModule' 3import cartModule from './modules/cartModule' // Example of another module 4 5const { Provider } = createGlobalStore([authModule, cartModule]) 6 7const App = () => { 8 return ( 9 <Provider> 10 <YourAppComponents /> 11 </Provider> 12 ) 13}
Provider
: The context provider that makes the global store available to your React components.Once the global store is set up, you can access state, mutations, actions, and getters using the useModule
hook.
1import { useModule } from '@opensource-dev/athena' 2 3const LoginComponent = () => { 4 const { states, actions } = useModule() 5 6 const auth = { 7 ...states('auth', ['username', 'loggedIn']), 8 ...actions('auth', ['login', 'logout']) 9 } 10 11 const handleLogin = async (username: string, password: string) => { 12 const response = await auth.login({ username, password }) 13 if (response.error) { 14 console.error(response.error.message); 15 } 16 } 17 18 return ( 19 <div> 20 {auth.loggedIn ? ( 21 <p>Welcome, {auth.username}!</p> 22 ) : ( 23 <button onClick={() => handleLogin('user', 'password')}>Login</button> 24 )} 25 </div> 26 ) 27}
In the component:
states('moduleName', ['stateName1', 'stateName2'])
: Access the state values from the specified module.actions('moduleName', ['actionName1', 'actionName2'])
: Access the actions from the specified module.mutations('moduleName', ['mutationName1', 'mutationName2'])
: Access the mutations from the specified module.getters('moduleName', ['getterName1', 'getterName2'])
: Access the getters from the specified module.Actions will use the commit
function to update the state through mutations. This is done automatically within the actions and does not require explicit interaction by the user.
Example of how mutations work within actions:
1actions: { 2 async login({ commit }, { username, password }) { 3 try { 4 const response = await apiLogin({ username, password }) 5 commit('SET_USERNAME', { payload: response.username }) 6 commit('SET_LOGGED_IN', { payload: true }) 7 return response 8 } catch (error) { 9 return { error: { message: error.message } } 10 } 11 } 12}
App.tsx
with Multiple Modules:1import React from 'react' 2import { useModule } from '@opensource-dev/athena' 3 4const App = () => { 5 const { states, actions } = useModule() 6 7 const auth = { 8 ...states('auth', ['username', 'loggedIn']), 9 ...actions('auth', ['login', 'logout']) 10 } 11 12 const handleLogin = async () => { 13 await auth.login({ username: 'test', password: 'password' }); 14 }; 15 16 return ( 17 <div> 18 {auth.loggedIn ? ( 19 <p>Welcome, {auth.username}!</p> 20 ) : ( 21 <button onClick={handleLogin}>Login</button> 22 )} 23 </div> 24 ); 25}; 26 27export default App
createGlobalStore(modules: Module[])
modules
: An array of module functions that define the state, actions, mutations, and getters for the store.Provider
component to wrap your app.useModule()
states
, actions
, mutations
, and getters
.
states(moduleName, stateNames)
: Get the specified states from the module.actions(moduleName, actionNames)
: Get the specified actions from the module.mutations(moduleName, mutationNames)
: Get the specified mutations from the module.getters(moduleName, getterNames)
: Get the specified getters from the module.This project is licensed under the MIT License
No vulnerabilities found.
No security vulnerabilities found.