Skip to content
This repository was archived by the owner on Feb 9, 2026. It is now read-only.

Commit ee41ea4

Browse files
author
Francesco
authored
Release v1.0.3 (#7)
* Refactor and upgrade dependencies (#5) * Prepare for release 1.0.3 (#6)
1 parent e06315d commit ee41ea4

File tree

13 files changed

+150
-112
lines changed

13 files changed

+150
-112
lines changed

.eslintrc.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
module.exports = {
2-
root: true,
32
parser: '@typescript-eslint/parser',
3+
parserOptions: {
4+
project: ['./tsconfig.json']
5+
},
46
plugins: [
57
'@typescript-eslint',
68
],
79
extends: [
10+
'airbnb-base',
811
'airbnb-typescript/base',
12+
'plugin:@typescript-eslint/recommended',
13+
'plugin:@typescript-eslint/recommended-requiring-type-checking'
914
],
1015
rules: {
1116
'@typescript-eslint/indent': ['error', 4],

.github/workflows/publish.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Publish Package to npmjs
2+
3+
on:
4+
release:
5+
types: [ created ]
6+
7+
jobs:
8+
publish:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: actions/setup-node@v2
13+
with:
14+
node-version: '16.x'
15+
registry-url: 'https://registry.npmjs.org'
16+
- run: npm install
17+
- run: npm publish
18+
env:
19+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/run-tests.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: run-tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- v1.x
8+
pull_request:
9+
10+
jobs:
11+
test:
12+
runs-on: ubuntu-latest
13+
strategy:
14+
fail-fast: true
15+
matrix:
16+
node: [ '14.x', '16.x' ]
17+
18+
name: Node v${{ matrix.node }}
19+
20+
steps:
21+
- uses: actions/checkout@v2
22+
- uses: actions/setup-node@v2
23+
with:
24+
node-version: ${{ matrix.node }}
25+
- run: npm install
26+
- run: npm run lint
27+
- run: npm run test
28+
- run: npm run build

.npmignore

Lines changed: 0 additions & 8 deletions
This file was deleted.

README.md

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Cache JS
22

3-
[![Build Status](https://travis-ci.com/Pod-Point/cache-js.svg?branch=master)](https://travis-ci.com/Pod-Point/cache-js)
3+
![Build Status](https://github.com/Pod-Point/cache-js/actions/workflows/run-tests.yml/badge.svg)
44

55
A cache service for JS.
66

@@ -41,22 +41,14 @@ This package uses jest. To run the test suites for this project, run the followi
4141
npm run test
4242
```
4343

44-
### Releases
44+
## License
4545

46-
This package is distributed via NPM and published automatically by Travis when creating a tagged commit. This can be done in one of 2 ways:
46+
The MIT License (MIT). Please see [License File](LICENCE) for more information.
4747

48-
#### 1. Pre-merge via the CLI
48+
---
4949

50-
**Before** merging your PR following code review & QA, run the following commands to update the package version and create a new release/tag.
51-
```
52-
npm version <major|minor|patch|prerelease>
53-
git push && git push --tags
54-
```
55-
56-
If you forget to do this before merging, this is not a problem, just create a new branch from master and run these same commands, remembering to open and merge a PR for this branch so that the `package.json` file is updated, alternatively, see option 2.
57-
58-
#### 2. Post-merge via code & GitHub
50+
<img src="https://d3h256n3bzippp.cloudfront.net/pod-point-logo.svg" align="right" />
5951

60-
**After** merging your code changes, create a new branch/PR from master and update the package version in `package.json` to whatever you wish the next release to be.
52+
Travel shouldn't damage the earth 🌍
6153

62-
**Before** merging this PR, create a release via GitHub with a tag version to match this package version number, this will trigger the tagged commit and in turn the automatic deployment to NPM.
54+
Made with ❤️&nbsp;&nbsp;at [Pod Point](https://pod-point.com)

package.json

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pod-point/cache-js",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "A cache service for JS.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",
@@ -20,30 +20,34 @@
2020
"Cache",
2121
"Redis"
2222
],
23+
"files": [
24+
"lib"
25+
],
2326
"scripts": {
24-
"build": "node_modules/.bin/tsc",
25-
"lint": "node_modules/.bin/eslint 'src/**/*.{ts,tsx}'",
27+
"build": "node_modules/.bin/tsc --project tsconfig.build.json",
28+
"lint": "node_modules/.bin/eslint 'src/**/*.ts'",
2629
"lint:fix": "npm run lint -- --fix",
27-
"test": "node_modules/.bin/jest --watch",
30+
"test": "node_modules/.bin/jest",
31+
"test:watch": "node_modules/.bin/jest --watch",
2832
"test:coverage": "node_modules/.bin/jest --coverage --runInBand --ci"
2933
},
3034
"dependencies": {
31-
"redis": "^2.8.0"
35+
"redis": "^3.1.2"
3236
},
3337
"devDependencies": {
34-
"@types/faker": "^4.1.8",
35-
"@types/jest": "^24.0.25",
36-
"@types/node": "^13.1.1",
37-
"@types/redis": "^2.8.14",
38-
"@typescript-eslint/eslint-plugin": "^2.13.0",
39-
"@typescript-eslint/parser": "^2.13.0",
40-
"eslint": "^6.8.0",
41-
"eslint-config-airbnb-typescript": "^6.3.1",
42-
"eslint-plugin-import": "^2.19.1",
43-
"faker": "^4.1.0",
44-
"jest": "^24.9.0",
45-
"ts-jest": "^24.2.0",
46-
"typescript": "^3.7.4"
38+
"@types/faker": "^5.5.9",
39+
"@types/jest": "^27.0.3",
40+
"@types/redis": "^2.8.32",
41+
"@types/node": "^16.11.12",
42+
"@typescript-eslint/eslint-plugin": "^5.6.0",
43+
"@typescript-eslint/parser": "^5.6.0",
44+
"eslint": "^8.4.1",
45+
"eslint-config-airbnb-typescript": "^16.1.0",
46+
"eslint-plugin-import": "^2.25.3",
47+
"faker": "^5.5.3",
48+
"jest": "^27.4.4",
49+
"ts-jest": "^27.1.1",
50+
"typescript": "^4.5.3"
4751
},
4852
"jest": {
4953
"collectCoverageFrom": [

src/__tests__/services/redis/Redis.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import { createClient } from 'redis';
22
import Redis from '../../../services/Redis';
33

4+
/* eslint-disable
5+
@typescript-eslint/no-unsafe-call,
6+
@typescript-eslint/no-unsafe-return
7+
*/
48
const mockSet = jest.fn((key, value, cb) => cb());
59
const mockQuit = jest.fn();
610
const mockDel = jest.fn((key, cb) => cb());
711
const mockExpire = jest.fn((key, time, cb) => cb());
812
const mockExpireAt = jest.fn((key, time, cb) => cb());
913
const mockGet = jest.fn((key, cb) => cb(null, 'someData'));
14+
/* eslint-enable */
1015

1116
jest.mock('redis', () => ({
1217
createClient: jest.fn(() => ({

src/services/Redis.ts

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,56 @@
1-
import { ClientOpts, createClient } from 'redis';
1+
import { ClientOpts, createClient, RedisClient } from 'redis';
22
import { promisify } from 'util';
33
import Expire from '../types/Expire';
44
import Service from '../types/Service';
5-
import RedisService from '../types/redis/Service';
65

7-
class Redis implements Service {
8-
private config: ClientOpts;
6+
interface AsyncRedisClient {
7+
del: (key: string) => Promise<void>;
8+
expire: (key: string, seconds: number) => Promise<void>;
9+
expireAt: (key: string, seconds: number) => Promise<void>;
10+
get: (key: string) => Promise<string>;
11+
quit: () => void
12+
set: (key: string, value: string) => Promise<void>;
13+
}
14+
15+
function getClient(client: RedisClient): AsyncRedisClient {
16+
return {
17+
/* eslint-disable
18+
@typescript-eslint/unbound-method,
19+
@typescript-eslint/no-unsafe-assignment
20+
*/
21+
del: promisify(client.del).bind(client),
22+
expire: promisify(client.expire).bind(client),
23+
expireAt: promisify(client.expireat).bind(client),
24+
get: promisify(client.get).bind(client),
25+
quit: client.quit as () => void,
26+
set: promisify(client.set).bind(client),
27+
/* eslint-enable */
28+
};
29+
}
30+
31+
export default class Redis implements Service {
32+
private readonly config: ClientOpts;
933

10-
private ephemeral: boolean;
34+
private readonly ephemeral: boolean;
1135

12-
private service: RedisService;
36+
private readonly client: AsyncRedisClient;
1337

1438
/**
1539
* Creates a Redis client instance depending on config.
1640
*/
1741
public constructor(config?: ClientOpts, ephemeral?: boolean) {
1842
this.ephemeral = ephemeral;
1943
this.config = config;
20-
21-
if (!this.ephemeral) {
22-
this.service = this.getService(true);
23-
}
44+
this.client = getClient(createClient(config));
2445
}
2546

2647
/**
2748
* Retrieves the keys value from the cache.
2849
*/
2950
public async get(key: string): Promise<string> {
30-
const service = this.getService();
51+
const value = await this.client.get(key);
3152

32-
const value = await service.get(key);
33-
34-
this.quitIfNeeded(service);
53+
this.quitIfNeeded();
3554

3655
return value;
3756
}
@@ -41,61 +60,36 @@ class Redis implements Service {
4160
* optionally setting it to expire at a particular time or in a given number of seconds.
4261
*/
4362
public async put(key: string, value: string, expire?: Expire): Promise<void> {
44-
const service = this.getService();
45-
46-
await service.set(key, value);
63+
await this.client.set(key, value);
4764

4865
if (expire) {
4966
if (expire.at) {
50-
await service.expireAt(key, expire.at);
67+
await this.client.expireAt(key, expire.at);
5168
}
5269

5370
if (expire.in) {
54-
await service.expire(key, expire.in);
71+
await this.client.expire(key, expire.in);
5572
}
5673
}
5774

58-
this.quitIfNeeded(service);
75+
this.quitIfNeeded();
5976
}
6077

6178
/**
6279
* Removes the key/value pair from the cache.
6380
*/
6481
public async remove(key: string): Promise<void> {
65-
const service = this.getService();
82+
await this.client.del(key);
6683

67-
await service.del(key);
68-
69-
this.quitIfNeeded(service);
84+
this.quitIfNeeded();
7085
}
7186

7287
/**
7388
* Quits the redis client if required.
7489
*/
75-
private async quitIfNeeded(service): Promise<void> {
90+
private quitIfNeeded(): void {
7691
if (this.ephemeral) {
77-
service.client.quit();
78-
}
79-
}
80-
81-
/**
82-
* Creates new Redis instance if one does not already exist or if is configured to be ephemeral.
83-
*/
84-
private getService(force: boolean = false): RedisService {
85-
if (this.ephemeral || force) {
86-
const client = createClient(this.config);
87-
this.service = {
88-
client,
89-
del: promisify(client.del).bind(client),
90-
expire: promisify(client.expire).bind(client),
91-
expireAt: promisify(client.expireat).bind(client),
92-
get: promisify(client.get).bind(client),
93-
set: promisify(client.set).bind(client),
94-
};
92+
this.client.quit();
9593
}
96-
97-
return this.service;
9894
}
9995
}
100-
101-
export default Redis;

src/types/Expire.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
interface Expire {
1+
interface ExpireAt {
22
/**
33
* The timestamp when the key/value pair should expire.
44
*/
5-
at?: number;
5+
at: number;
66

7+
in?: never;
8+
}
9+
10+
interface ExpireIn {
711
/**
812
* The number of seconds until the key/value pair should expire.
913
*/
10-
in?: number;
14+
in: number;
15+
16+
at?: never;
1117
}
1218

19+
type Expire = ExpireAt | ExpireIn;
20+
1321
export default Expire;

src/types/Service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
interface Service {
1+
import Expire from './Expire';
2+
3+
export default interface Service {
24
/**
35
* Retrieves the keys value from the cache.
46
*/
@@ -7,12 +9,10 @@ interface Service {
79
/**
810
* Persists the key/value pair in the cache.
911
*/
10-
put(key: string, value: string): Promise<void>;
12+
put(key: string, value: string, expire: Expire): Promise<void>;
1113

1214
/**
1315
* Removes the key/value pair from the cache.
1416
*/
1517
remove(key: string): Promise<void>;
1618
}
17-
18-
export default Service;

0 commit comments

Comments
 (0)