-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6336daa
Showing
14 changed files
with
3,955 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
coverage | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.