Skip to content

Commit

Permalink
Upgrade pkgs
Browse files Browse the repository at this point in the history
Fix deprecation warning for using fastify request.routerPath
Use CI workflow in publish file
  • Loading branch information
denbon05 committed Dec 18, 2023
1 parent a8748aa commit 7887c64
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 69 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
name: Node CI
name: CI

on:
workflow_call:
pull_request:
branches:
- main
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
run-checks:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest]
# only LTS versions
node-version: [16.x, 18.x, 20.x]

Expand All @@ -28,11 +27,12 @@ jobs:
- run: npm run build
- run: npm run lint
- run: npm test
- name: Check fastify v3 compatibility
run: |
npm i -D fastify@3
npm run build
npm test
# TODO uncomment with Fastify v5 dependency
# - name: Check fastify v4 compatibility
# run: |
# npm i -D fastify@4
# npm run build
# npm test

- name: Test & publish code coverage
if: ${{ github.ref == 'refs/heads/main' || github.actor == 'dependabot[bot]' }}
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
types: [created]

jobs:
ci:
uses: denbon05/fastify-lcache/.github/workflows/ci.yml@main

publish:
runs-on: ubuntu-latest

Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
[![Test Coverage](https://api.codeclimate.com/v1/badges/6dfec3501aa3eb441bab/test_coverage)](https://codeclimate.com/github/denbon05/fastify-lcache/test_coverage)

<p>fastify-lcache plugin for <a href="https://www.fastify.io/" target="_blank">Fastify</a> for memorize
data on first request and use next time until <a href="https://en.wikipedia.org/wiki/Time_to_live" target="_blank">ttl</a> expires</p>

<p>Supports Fastify version since ^3</p>
data on first request and use it next time until <a href="https://en.wikipedia.org/wiki/Time_to_live" target="_blank">ttl</a> expires</p>

```bash
npm i fastify-lcache
Expand Down Expand Up @@ -49,7 +47,7 @@ axios.get(url);
```

<dl>
<dt><b>IMPORTANT</b></dt>
<dt><b>⚠️ IMPORTANT</b></dt>
<dd><i>Restarting your app resets the cache</i></dd>
</dl>

Expand All @@ -72,9 +70,6 @@ axios.get(url);
<p><b>app.lcache</b> available inside your app</p>

```ts
// you can specify payload data type:
// app.lcache.get<{ name: string }>('person')

interface CachedResponse<T> {
payload: T;
headers?: { [key: string]: string | number | string[] };
Expand All @@ -98,3 +93,10 @@ interface IStorage {
destroy(): void;
}
```

### Fastify version compatibility

| Fastify | lcache |
| :-----: | :----: |
| 3-4.9 | 1-1.2 |
| ^4.10 | 2.x |
8 changes: 3 additions & 5 deletions lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable import/prefer-default-export */
import type { FastifyRequest } from 'fastify';
import type {
RequestMethod,
Expand All @@ -12,7 +11,7 @@ export const formatOptions = (opts: ICacheOptions): ICachePluginOptions => ({
...opts,
methodsToCache: new Set(opts.methodsToCache),
statusesToCache: new Set(opts.statusesToCache),
excludeRoutes: new Set(opts.excludeRoutes?.map((r) => r.trim())),
excludeRoutes: new Set(opts.excludeRoutes?.map((route) => route.trim())),
ttl: getMilliseconds(opts.ttlInMinutes),
});

Expand All @@ -22,12 +21,11 @@ export const shouldBeCached = (
statusCode: number
): boolean => {
const { methodsToCache, statusesToCache, excludeRoutes } = opts;
// TODO use routeOptions.url - test compatibility
const { routerPath, method } = request;
const { method, routeOptions } = request;

return (
methodsToCache.has(method as RequestMethod) &&
statusesToCache.has(statusCode) &&
!excludeRoutes.has(routerPath)
!excludeRoutes.has(routeOptions.url)
);
};
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const cache: FastifyPluginCallback<ICacheOptions> = (

const lcache = fp(cache, {
name: '@fastify/lcache',
fastify: '>=4.10',
});

/**
Expand Down
Loading

0 comments on commit 7887c64

Please sign in to comment.