Incarnate
Runtime Dependency Lifecycle Management for JavaScript.
Install
npm i -S incarnate
API Docs
http://incarnate.resist.design
Usage Example
import Incarnate from 'incarnate';
const inc = new Incarnate({
subMap: {
state: {
subMap: {
user: {
factory: () => ({
authToken: undefined
})
}
}
},
services: {
shared: {
user: 'state.user'
},
subMap: {
user: true,
login: {
factory: () => {
return async (username, password) => {
const fakeToken = `${username}:${password}`;
return Buffer.from(fakeToken).toString('base64');
};
}
},
accounts: {
dependencies: {
user: 'user'
},
factory: ({dependencies: {user: {authToken = ''} = {}} = {}} = {}) => {
return async () => {
if (!authToken) {
throw new Error('The accounts service requires an authorization token but none was supplied.');
}
console.log('Getting accounts with headers:', {
Authorization: `Bearer: ${authToken}`
});
return [
{name: 'Account 1'},
{name: 'Account 2'},
{name: 'Account 3'},
{name: 'Account 4'}
];
};
}
}
}
},
actions: {
shared: {
user: 'state.user',
loginService: 'services.login'
},
subMap: {
user: true,
loginService: true,
login: {
dependencies: {
loginService: 'loginService'
},
setters: {
setUser: 'user'
},
factory: ({dependencies: {loginService} = {}, setters: {setUser} = {}} = {}) => {
return async ({username, password} = {}) => {
const authToken = await loginService(username, password);
setUser({
authToken
});
return true;
};
}
}
}
}
}
});
export default async function app() {
const loginAction = inc.getResolvedPath('actions.login');
const loginResult = await loginAction({
username: 'TestUser',
password: 'StopTryingToReadThis'
});
const accountsService = inc.getResolvedPath('services.accounts');
const accounts = await accountsService();
console.log('These are the accounts:', accounts);
}
app();
License
MIT