Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2.2.0 #56

Merged
merged 5 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module.exports = {
env: {
es2021: true,
node: true,
jest: true,
},
root: true,
settings: {
"import/resolver": {
typescript: {},
},
},
extends: [
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
],
parser: "@typescript-eslint/parser",
parserOptions: {
project: "tsconfig.json",
tsconfigRootDir: __dirname,
sourceType: "module",
},
plugins: ["@typescript-eslint/eslint-plugin"],
ignorePatterns: [".eslintrc.js"],
rules: {
"import/prefer-default-export": "off",
"@typescript-eslint/interface-name-prefix": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-explicit-any": "off",
"prettier/prettier": ["error", { singleQuote: false }],
},
};
38 changes: 0 additions & 38 deletions .eslintrc.json

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
id: metadata
uses: dependabot/fetch-metadata@v1
with:
github-token: '${{ secrets.GITHUB_TOKEN }}'
github-token: "${{ secrets.GITHUB_TOKEN }}"

- name: Approve a PR
run: gh pr review --approve "$PR_URL"
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'
node-version: "18.x"
registry-url: "https://registry.npmjs.org"
- run: npm ci
- run: npm test
- run: npm run build
Expand Down
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"singleQuote": true,
"singleQuote": false,
"trailingComma": "es5",
"semi": true
}
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# run all CI checks
all: build lint test

ci:
npm ci

Expand All @@ -7,6 +10,9 @@ test:
cover:
npm test -- --coverage --coverageProvider=v8

lint:
npx eslint .

format:
npx prettier --write .

Expand All @@ -16,4 +22,4 @@ fix: format
build:
npm run build

PHONY: test
PHONY: test
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ npm i fastify-lcache

```ts
// your app
import fastify from 'fastify';
import lcache from 'fastify-lcache';
import fastify from "fastify";
import lcache from "fastify-lcache";

const app = fastify();
const address = '0.0.0.0';
const address = "0.0.0.0";
const port = 4000;

app.register(lcache, {
Expand All @@ -27,8 +27,8 @@ app.register(lcache, {

app.after(() => {
// add your routes
app.get('/ping', async (req, reply) => {
reply.send('pong');
app.get("/ping", async (req, reply) => {
reply.send("pong");
});
});

Expand All @@ -37,7 +37,7 @@ app.listen(port, address);

```ts
// client wants data from your app
const url = 'http://0.0.0.0:4000/ping';
const url = "http://0.0.0.0:4000/ping";
// first request will return origin data from route '/ping'
// and put result to the cache
axios.get(url);
Expand Down Expand Up @@ -70,12 +70,12 @@ axios.get(url);
<p><b>app.lcache</b> available inside your app</p>

```ts
interface IStorage {
interface ILightCache {
// Get cached data
get(key: string): any;
get<T>(key: string): T;

// Set data to cache
set(key: string, value: any): void;
set<T>(key: string, value: T): void;

// Check if data exists in cache
has(key: string): boolean;
Expand Down
46 changes: 0 additions & 46 deletions __tests__/helpers/index.ts

This file was deleted.

69 changes: 69 additions & 0 deletions __tests__/helpers/lcache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import fastify from "fastify";
import lcache from "../../lib";
import type { ICacheOptions } from "../../lib/types/lcache";

/** Injected spy functions in the tested endpoints */
export const spies = {
getPing: jest.fn(),
postPing: jest.fn(),
deletePing: jest.fn(),
getJSON: jest.fn(),
postPost: jest.fn(),
getDate: jest.fn(),
putPut: jest.fn(),
postApiAdmin: jest.fn(),
};

export const getApp = (options: Partial<ICacheOptions> = {}) => {
const app = fastify();
app.register(lcache, options);

app.after(() => {
app.get("/ping", async (_req, reply) => {
spies.getPing();
reply.send("pong");
});

app.post("/ping", async (_req, reply) => {
spies.postPing();
reply.send("pong");
});

app.delete("/ping", async (_req, reply) => {
spies.deletePing();
reply.send("pong");
});

app.get("/json", async (_req, reply) => {
spies.getJSON();
reply.send({ hello: "world" });
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
app.post("/post", async (req: any, reply) => {
spies.postPost();
reply.status(201);
reply.send(req.body.data);
});

app.get("/date", async (_req, reply) => {
spies.getDate();
await new Promise((resolve) => {
setTimeout(() => resolve(reply.send(Date.now())), Math.random() * 100);
});
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
app.put("/put", async (req: any, reply) => {
spies.putPut();
reply.status(201).send(req.body.data);
});

app.post("/api/admin", async (req, reply) => {
spies.postApiAdmin();
reply.send("secret");
});
});

return app;
};
Loading
Loading