diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4c6b076 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Tom S + +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. diff --git a/README.md b/README.md index 9bad61a..73eb285 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,18 @@ -# Redis Cache Store and Middleware for Koa +# KOA-CACHIER + +A Koa Middleware backed by a Redis-powered cache store + +## Usage + +```shell +npm i koa-cachier +``` + +```typescript +import { Store, cachier } from "koa-cachier"; + +const store = new Store(); +app.use(cachier(store)); +``` + diff --git a/package-lock.json b/package-lock.json index 811dab7..42401ef 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,19 +1,19 @@ { - "name": "koa-cacher", - "version": "1.0.1", + "name": "koa-cachier", + "version": "1.0.3", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "koa-cacher", - "version": "1.0.1", + "name": "koa-cachier", + "version": "1.0.3", "license": "MIT", "dependencies": { "ioredis": "^5.3.2" }, "devDependencies": { - "@types/koa": "^2.14.0", - "@types/node": "^20.11.19", + "@types/koa": "^2.15.0", + "@types/node": "^20.11.20", "glob": "^10.3.10", "koa": "^2.15.0", "tsx": "^4.7.1", @@ -512,9 +512,9 @@ "dev": true }, "node_modules/@types/koa": { - "version": "2.14.0", - "resolved": "https://registry.npmjs.org/@types/koa/-/koa-2.14.0.tgz", - "integrity": "sha512-DTDUyznHGNHAl+wd1n0z1jxNajduyTh8R53xoewuerdBzGo6Ogj6F2299BFtrexJw4NtgjsI5SMPCmV9gZwGXA==", + "version": "2.15.0", + "resolved": "https://registry.npmjs.org/@types/koa/-/koa-2.15.0.tgz", + "integrity": "sha512-7QFsywoE5URbuVnG3loe03QXuGajrnotr3gQkXcEBShORai23MePfFYdhz90FEtBBpkyIYQbVD+evKtloCgX3g==", "dev": true, "dependencies": { "@types/accepts": "*", @@ -543,9 +543,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "20.11.19", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.19.tgz", - "integrity": "sha512-7xMnVEcZFu0DikYjWOlRq7NTPETrm7teqUT2WkQjrTIkEgUyyGdWsj/Zg8bEJt5TNklzbPD1X3fqfsHw3SpapQ==", + "version": "20.11.20", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.20.tgz", + "integrity": "sha512-7/rR21OS+fq8IyHTgtLkDK949uzsa6n8BkziAKtPVpugIkO6D+/ooXMvzXxDnZrmtXVfjb1bKQafYpb8s89LOg==", "dev": true, "dependencies": { "undici-types": "~5.26.4" diff --git a/package.json b/package.json index bc9900f..2449fb5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "koa-cacher", - "version": "1.0.3", - "description": "A Redis backed cache with a Koa Middleware ", + "name": "koa-cachier", + "version": "1.0.4", + "description": "A Koa Middleware backed by a Redis-powered cache store", "main": "dist/index.js", "type": "module", "scripts": { @@ -22,8 +22,8 @@ "ioredis": "^5.3.2" }, "devDependencies": { - "@types/koa": "^2.14.0", - "@types/node": "^20.11.19", + "@types/koa": "^2.15.0", + "@types/node": "^20.11.20", "glob": "^10.3.10", "koa": "^2.15.0", "tsx": "^4.7.1", diff --git a/src/index.ts b/src/index.ts index 3820561..a0eb9bf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,5 +4,5 @@ * Created by Thomas Sham on 12/12/2023. */ -export { Cacher as Store } from "./components/cacher/index.js"; -export { cacherFactory as cacher, keyBuilder } from "./components/middleware/index.js"; +export { Cacher as Store } from "./lib/cachier/index.js"; +export { cachierFactory as cachier, keyBuilder } from "./lib/middleware/index.js"; diff --git a/src/components/cacher/index.ts b/src/lib/cachier/index.ts similarity index 98% rename from src/components/cacher/index.ts rename to src/lib/cachier/index.ts index 37e719a..2c83d0c 100644 --- a/src/components/cacher/index.ts +++ b/src/lib/cachier/index.ts @@ -7,7 +7,7 @@ export interface CacherOptions extends RedisOptions { const defaults: CacherOptions = { port: 6379, host: "127.0.0.1", - keyPrefix: "koa-cacher:", + keyPrefix: "koa-cachier:", expire: 24 * 60 * 60, }; diff --git a/src/components/middleware/index.ts b/src/lib/middleware/index.ts similarity index 91% rename from src/components/middleware/index.ts rename to src/lib/middleware/index.ts index 541e138..5ab3c09 100644 --- a/src/components/middleware/index.ts +++ b/src/lib/middleware/index.ts @@ -6,7 +6,7 @@ import { Middleware, Request } from "koa"; -import { Cacher, } from "../cacher/index.js"; +import { Cacher, } from "../cachier/index.js"; interface MiddlewareOptions { cache2XXOnly?: boolean; @@ -29,7 +29,7 @@ export function keyBuilder(request: Request, ...args: string[]): string { return key; } -export function cacherFactory(store: Cacher, opts: MiddlewareOptions = defaults, ): Middleware { +export function cachierFactory(store: Cacher, opts: MiddlewareOptions = defaults, ): Middleware { opts = { ...defaults, ...opts }; return async function cacher ( ctx, diff --git a/test/cacher.test.ts b/test/cacher.test.ts index 0fd08a8..a5b1488 100644 --- a/test/cacher.test.ts +++ b/test/cacher.test.ts @@ -60,9 +60,7 @@ describe( }); after(async () => { - await store.delete(testKey1); - await store.delete(testKey2); - await store.delete(testKey3); + await store.delete("", true); store.destroy(); }); } diff --git a/test/middleware.test.ts b/test/middleware.test.ts index 69add15..b596a97 100644 --- a/test/middleware.test.ts +++ b/test/middleware.test.ts @@ -3,7 +3,7 @@ import { strictEqual, } from "node:assert"; import { ParameterizedContext, Request, } from "koa"; -import { Store, cacher, keyBuilder } from "../src/index.js"; +import { Store, cachier, keyBuilder } from "../src/index.js"; let store: Store; @@ -38,7 +38,7 @@ describe( }); it("middleware caches response body", async () => { - const middleware = cacher(store); + const middleware = cachier(store); strictEqual(await store.get(keyBuilder(ctx.request)), null); await middleware( ctx, @@ -55,7 +55,7 @@ describe( ctx.response.status = 200; ctx.response.body = "test-cache-5678"; }); - const middleware = cacher(store); + const middleware = cachier(store); await middleware(ctx, next); strictEqual(next.mock.calls.length, 1); await middleware(ctx, next); @@ -63,7 +63,7 @@ describe( }); it("middleware does not cache empty response body", async () => { - const middleware = cacher(store, { ignoreEmptyBody: true, }); + const middleware = cachier(store, { ignoreEmptyBody: true, }); strictEqual(await store.get(keyBuilder(ctx.request)), null); await middleware( ctx, @@ -76,7 +76,7 @@ describe( }); it("middleware does not cache non-successful (~2XX) response body", async () => { - const middleware = cacher(store, { cache2XXOnly: true, }); + const middleware = cachier(store, { cache2XXOnly: true, }); strictEqual(await store.get(keyBuilder(ctx.request)), null); await middleware( ctx,