Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-lee-collins committed Jan 9, 2020
0 parents commit 6336daa
Show file tree
Hide file tree
Showing 14 changed files with 3,955 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
coverage
dist
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
node_js:
- 10
after_script:
- "jest --config jestconfig.json --coverage --coverageReporters=text-lcov | coveralls"
29 changes: 29 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Unit Tests",
"port": 9230,
"runtimeArgs": [
"--inspect-brk=9230",
"${workspaceRoot}/node_modules/jest/bin/jest.js",
"--runInBand",
// "--watch"
],
"args": [
"-c", "jestconfig.json"
],
"runtimeExecutable": null,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"env": {
"BABEL_ENV": "testing"
}
}
]
}
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"files.exclude": {
"dist": true,
"node_modules": true
},
"editor.fontLigatures": true,
"editor.fontFamily": "Fira Code",
"cSpell.words": [
"swork"
]
}
23 changes: 23 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

(The MIT License)

Copyright (c) 2019 Justin Collins

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 changes: 8 additions & 0 deletions jestconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"transform": {
"^.+\\.(t|j)sx?$": "ts-jest"
},
"testRegex": "/tests/.*tests\\.ts",
"coveragePathIgnorePatterns": ["/node_modules/", "<rootDir>/tests/mock-helper\\.ts"],
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
}
60 changes: 60 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "swork-when",
"version": "1.0.0",
"description": "Conditional middleware for the swork framework.",
"main": "dist/index.js",
"scripts": {
"test": "jest --config jestconfig.json --coverage --all",
"lint": "tslint -p tsconfig.json",
"build": "tsc",
"prepare": "yarn build && yarn lint",
"prepublishOnly": "yarn test"
},
"license": "MIT",
"dependencies": {
"swork": "^1.0.0"
},
"jest": {
"testEnvironment": "node"
},
"devDependencies": {
"@types/jest": "^24.0.17",
"@types/service-worker-mock": "^2.0.0",
"coveralls": "^3.0.6",
"jest": "^24.8.0",
"service-worker-mock": "^2.0.3",
"ts-jest": "^24.0.0",
"ts-loader": "^5.3.3",
"tslint": "^5.18.0",
"typescript": "^3.3.3333"
},
"keywords": [
"service",
"worker",
"serviceworker",
"middleware",
"fetch",
"pipeline",
"FetchEvent",
"offline",
"activate",
"install",
"PWA",
"progressive",
"web",
"app",
"application",
"router"
],
"icon": "",
"files": [
"dist/**/*",
"README.md",
"LICENSE"
],
"repository": {
"type": "git",
"url": "git+https://github.com/justin-lee-collins/swork-if"
},
"homepage": "https://github.com/justin-lee-collins/swork-if"
}
60 changes: 60 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# swork-when

[![npm](https://img.shields.io/npm/v/swork-when)](https://www.npmjs.com/package/swork-when) [![travis ci](https://travis-ci.org/justin-lee-collins/swork-when.svg?branch=master)](https://travis-ci.org/justin-lee-collins/swork-when.svg?branch=master) [![coverage](https://img.shields.io/coveralls/github/justin-lee-collins/swork-when)](https://img.shields.io/coveralls/github/justin-lee-collins/swork-when) [![download](https://img.shields.io/npm/dw/swork-when)](https://img.shields.io/npm/dw/swork-when) [![Greenkeeper badge](https://badges.greenkeeper.io/justin-lee-collins/swork-when.svg)](https://greenkeeper.io/)[![Join the chat at https://gitter.im/swork-chat/community](https://badges.gitter.im/swork-chat/community.svg)](https://gitter.im/swork-chat/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

swork-when is a [swork](https://www.npmjs.com/package/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**

```ts
// 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**

```ts
when(predicate: (context: FetchContext) => Promise<boolean> | boolean, app: Swork): 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](https://www.npmjs.com/package/swork) or any of its related middlewares, please let me know on [gitter](https://gitter.im/swork-chat/community). I am always looking for feedback or additional middleware ideas.
16 changes: 16 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { FetchContext, Swork } from "swork";
import { Middleware, RequestDelegate } from "swork/dist/swork";

export function when(predicate: (context: FetchContext) => Promise<boolean> | boolean, app: Swork): Middleware {

// tslint:disable-next-line:no-string-literal
const whenApp = app["build"]() as RequestDelegate;

return async (context: FetchContext, next: () => Promise<void>) => {
if (await predicate(context)) {
await whenApp(context);
} else {
await next();
}
};
}
91 changes: 91 additions & 0 deletions tests/index.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { FetchContext, Swork } from "swork";
import { RequestDelegate } from "swork/dist/swork";
import { when } from "./../src/index";
import { getFetchEvent, mockInit } from "./mock-helper";

declare var global: any;

function build(instance: Swork): RequestDelegate {
// tslint:disable-next-line:no-string-literal
return instance["build"]();
}

describe("when tests", () => {
let instance: Swork;
let whenInstance: Swork;
let context: FetchContext;

beforeEach(() => {
mockInit();

instance = new Swork();
whenInstance = new Swork();

context = new FetchContext(getFetchEvent("http://www.example.com"));
});

test("when goes into provided app when true", async (done) => {
let whenAppCalled = false;
let mainAppStopped = true;

whenInstance.use(() => {
whenAppCalled = true;
});

instance.use(when((_: FetchContext) => true, whenInstance));

instance.use(() => {
mainAppStopped = false;
});

const delegate = build(instance);

await delegate(context);

expect(whenAppCalled).toBeTruthy();
expect(mainAppStopped).toBeTruthy();

done();
});

test("when does not go into app when false", async (done) => {
let whenAppCalled = false;
let mainAppStopped = true;

whenInstance.use(() => {
whenAppCalled = true;
});

instance.use(when((_: FetchContext) => false, whenInstance));

instance.use(() => {
mainAppStopped = false;
});

const delegate = build(instance);

await delegate(context);

expect(whenAppCalled).toBeFalsy();
expect(mainAppStopped).toBeFalsy();

done();
});

test("when passes a valid FetchContext to the predicate", async (done) => {
let passedContext: FetchContext | null = null;

instance.use(when((c: FetchContext) => {
passedContext = c;
return true;
}, whenInstance));

const delegate = build(instance);

await delegate(context);

expect(passedContext).toBe(context);

done();
});
});
29 changes: 29 additions & 0 deletions tests/mock-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as makeServiceWorkerEnv from "service-worker-mock";
// @ts-ignore
import * as FetchEvent from "service-worker-mock/models/FetchEvent";
// @ts-ignore
import * as Request from "service-worker-mock/models/Request";
// @ts-ignore
import * as Response from "service-worker-mock/models/Response";

declare var global: any;

export function mockInit() {
Object.assign(global, makeServiceWorkerEnv(), {
fetch: (request: RequestInfo): Promise<Response> => {
const response = new Response("", {
status: 200,
statusText: "ok",
});
response.url = typeof(request) === "string" ? request : request.url;
return Promise.resolve(response);
},
});
jest.resetModules();
}

export function getFetchEvent(url: string, method?: string) {
return new FetchEvent("FetchEvent", {
request: new Request(url, { method: method || "GET"}),
});
}
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"strict": true,
"lib": ["es6", "webworker"],
},
"include": [
"src"
],
"exclude": [
"node_modules",
]
}
12 changes: 12 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": ["tslint:recommended"],
"rules": {
"variable-name": false,
"callable-types": false,
"interface-over-type-literal": false,
"max-line-length": [true, 150],
"max-classes-per-file": false,
"no-trailing-whitespace": false,
"no-console": false
}
}
Loading

0 comments on commit 6336daa

Please sign in to comment.