Helper to create basic SPWA using React, Baobab and Fetchery written in typescript
And baobab-router
This section is a quick overview of the steps to setup an application.
Application data is stored in a Baobab tree.
import oiler from 'oiler';
// init state
oiler.state.set({ foo: 'bar' });
Services are declared using Fetchery to query APIs.
import oiler, { Fetchery } from 'oiler';
oiler.addClient(
'api', // service name
'https://foo.bar', // service base url
{
// Fetchery defaults
contentType: Fetchery.CONTENT_TYPE.JSON,
},
{
// API routes
'foo.bar': { route: '/foo/bar', method: Fetchery.METHOD.POST },
}
);
A simple example can be found in ./dev/services/api.ts.
Actions are async functions that handle application logic (call services, update state).
foo.js
export const foo = async (oiler, data) => {
oiler.state.set(['foo'], data);
};
bar.js
export const bar = async (oiler, data) => {
oiler.state.set(['bar'], data);
};
actions.js
import oiler from 'oiler';
import { foo } from './foo';
import { bar } from './bar';
oiler.addAction('foo', foo);
oiler.addAction('bar', bar);
A simple example can be found in ./dev/actions.
import oiler from 'oiler';
const FooBar = function (props) {
return <div>Foo bar</div>;
};
// Define route and state (pages only)
FooBar.route = '/foo/bar/:id';
FooBar.state = { id: ':id' };
// Actions to execute on page/modal display
FooBar.dependencies = [{ action: ['foo'] }, { action: ['bar'] }];
// Set header/footer display (pages only)
FooBar.header = true;
FooBar.footer = false;
// Register page to app
oiler.addPage('foo.bar', FooBar);
A simple example can be found in ./dev/pages and ./dev/modals.
It is possible to set some header and footer components to be displayed before and after pages.
import oiler from 'oiler';
oiler.Header = function () {
return <div>Header</div>;
};
oiler.Footer = function () {
return <div>Footer</div>;
};
It is possible to set wrapper around pages and modals components to handle specific layout and logic.
import oiler from 'oiler';
oiler.ModalWrapper = function ({ children }) {
return <div className="modal">{children}</div>;
};
Open registered page or modal
import oiler, { CONTAINERS } from 'oiler';
// open a page with uuid
oiler.open({
id: 'foo.bar',
container: CONTAINERS.PAGE,
uuid: 'some-id',
});
// open modal with metadata
oiler.open({
id: 'foo.bar',
container: CONTAINERS.MODAL,
metadata: { foo: 'bar' },
});
This method reload page or modal (triggers actions dependencies).
import oiler, { CONTAINERS } from 'oiler';
// refresh page
oiler.refresh(CONTAINERS.PAGE);
// refresh modal
oiler.refresh(CONTAINERS.MODAL);
Once pages and modals are registered, application needs to be initialized to create the router and render.
import oiler from 'oiler';
// will render application in #container
// and use page ['foo', 'bar'] as default page
oiler.start('container', ['foo', 'bar']);
This method enable access to pages defined with authenticated=true
.
This method restrict access to pages defined with authenticated=false
.
Oiler uses node-polyglot
to handle I18n.
See dev's "About" page and dev's locales as a simple example.
Used to register an available language in the application by providing a name and a URL to a JSON file containing texts.
import oiler from 'oiler';
oiler.addLocale('en', '/locales/en.json');
oiler.addLocale('fr', 'https://foo.bar/locales/fr.json');
To load locale on start, locale name must be passed as the third parameter of oiler.start
.
Load locale file from URL and re-render.
import oiler from 'oiler';
await oiler.setLocale('fr');
Returns text for the provided key, all parameters are passed to polyglot.t
for interpolation (see node-polyglot
for more details).
import oiler from 'oiler';
// In a page component
const FooBar = function ({ oiler }) {
// use text from locale
return <div>{oiler.text('foo.bar')}</div>;
};
Emit when oiler.start
is called.
Emit with page/modal parameters when oiler.open
is called.
Emit with page/modal parameters when oiler.refresh
is called.
Emit when oiler.login
is called.
Emit when oiler.logout
is called.
Run npm run dev
to start dev server in watch mode on port 8080
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to run npm run lint
.