Skip to content

Commit

Permalink
refactor: renamed to koa-cachier
Browse files Browse the repository at this point in the history
test: updated cachier store test to ensure proper cleanup
chore(dep): updated dependencies
chore(ver): bumped version
  • Loading branch information
UnKnoWn-Consortium committed Feb 26, 2024
1 parent 23d53ff commit b3bd39b
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 31 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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));
```


24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions test/cacher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
Expand Down
10 changes: 5 additions & 5 deletions test/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -38,7 +38,7 @@ describe(
});

it("middleware caches response body", async () => {
const middleware = cacher(store);
const middleware = cachier(store);
strictEqual(await store.get(keyBuilder(<Request>ctx.request)), null);
await middleware(
<ParameterizedContext>ctx,
Expand All @@ -55,15 +55,15 @@ describe(
ctx.response.status = 200;
ctx.response.body = "test-cache-5678";
});
const middleware = cacher(store);
const middleware = cachier(store);
await middleware(<ParameterizedContext>ctx, next);
strictEqual(next.mock.calls.length, 1);
await middleware(<ParameterizedContext>ctx, next);
strictEqual(next.mock.calls.length, 1);
});

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(<Request>ctx.request)), null);
await middleware(
<ParameterizedContext>ctx,
Expand All @@ -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(<Request>ctx.request)), null);
await middleware(
<ParameterizedContext>ctx,
Expand Down

0 comments on commit b3bd39b

Please sign in to comment.