swork-when
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

swork-when

npm travis ci coverage download Greenkeeper badgeJoin the chat at https://gitter.im/swork-chat/community

swork-when is a swork middleware designed to create logical paths conditional upon the incoming request. This allows for incremental branching strategies to reduce redundant conditional checks and overall simplified middleware. It is built with TypeScript and async methods.

License

MIT

Installation

npm install swork-when

yarn add swork-when

Example

// sw.ts
import { Swork } from "swork";
import { when } from "swork-when";
import { buildApiSwork } from "./apiSwork";
import { buildNotApiSwork } from "./notApiSwork";
 
// Define root app
export const app = new Swork();
const api = buildApiSwork();
const notApi = buildNotApiSwork();
 
// Use api swork app if condition is met
app.use(when((context: FetchContext) => {
    return context.request.headers.get("Accept") === "application/json";
}, api));
 
// If not an api call
app.use(notApi);
 
app.listen();

In the above example, the service worker conditionally re-routes the logic flow to the api swork app whenever the condition is true. Whenever the condition is false, the logic passes over the api logic flow and onto the next middleware.

Methods

when

when(predicate: (context: FetchContext) => Promise<boolean> | boolean, appSwork)Swork

Create a conditional branch in the app workflow. If predicate returns true the provided swork app will execute. If predicate returns false, the provided swork app is ignored and execution continues.

Notes

when can be used by nested swork applications and is encouraged when deep branching strategies are required.

Contact

If you are using swork or any of its related middlewares, please let me know on gitter. I am always looking for feedback or additional middleware ideas.

Dependencies (1)

Dev Dependencies (9)

Package Sidebar

Install

npm i swork-when

Weekly Downloads

6

Version

1.0.0

License

MIT

Unpacked Size

10.8 kB

Total Files

9

Last publish

Collaborators

  • justin-arch-sw