Skip to content
Open
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
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# source
lib
tests
index.ts

# misc
package-lock.json
Expand Down
1 change: 0 additions & 1 deletion index.d.ts

This file was deleted.

6 changes: 0 additions & 6 deletions index.js

This file was deleted.

1 change: 0 additions & 1 deletion index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion lib/cache.constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MODULE_OPTIONS_TOKEN } from './cache.module-definition';
import { MODULE_OPTIONS_TOKEN } from './cache.module-definition.js';

export const CACHE_MANAGER = 'CACHE_MANAGER';
export const CACHE_KEY_METADATA = 'cache_module:cache_key';
Expand Down
2 changes: 1 addition & 1 deletion lib/cache.module-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ConfigurableModuleBuilder } from '@nestjs/common';
import {
CacheModuleOptions,
CacheOptionsFactory,
} from './interfaces/cache-module.interface';
} from './interfaces/cache-module.interface.js';

export const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } =
new ConfigurableModuleBuilder<CacheModuleOptions>({
Expand Down
8 changes: 4 additions & 4 deletions lib/cache.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { DynamicModule, Module } from '@nestjs/common';
import { createCache } from 'cache-manager';
import { CACHE_MANAGER } from './cache.constants';
import { ConfigurableModuleClass } from './cache.module-definition';
import { createCacheManager } from './cache.providers';
import { CACHE_MANAGER } from './cache.constants.js';
import { ConfigurableModuleClass } from './cache.module-definition.js';
import { createCacheManager } from './cache.providers.js';
import {
CacheModuleAsyncOptions,
CacheModuleOptions,
} from './interfaces/cache-module.interface';
} from './interfaces/cache-module.interface.js';

/**
* This is just the same as the `Cache` interface from `cache-manager` but you can
Expand Down
6 changes: 3 additions & 3 deletions lib/cache.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Provider } from '@nestjs/common';
import { createCache } from 'cache-manager';
import type { Cacheable } from 'cacheable';
import Keyv, { type KeyvStoreAdapter } from 'keyv';
import { CACHE_MANAGER } from './cache.constants';
import { MODULE_OPTIONS_TOKEN } from './cache.module-definition';
import { CacheManagerOptions } from './interfaces/cache-manager.interface';
import { CACHE_MANAGER } from './cache.constants.js';
import { MODULE_OPTIONS_TOKEN } from './cache.module-definition.js';
import { CacheManagerOptions } from './interfaces/cache-manager.interface.js';

function isCacheable(store: any): store is Cacheable {
return (
Expand Down
2 changes: 1 addition & 1 deletion lib/decorators/cache-key.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExecutionContext, SetMetadata } from '@nestjs/common';
import { CACHE_KEY_METADATA } from '../cache.constants';
import { CACHE_KEY_METADATA } from '../cache.constants.js';

export type CacheKeyFactory = (
ctx: ExecutionContext,
Expand Down
2 changes: 1 addition & 1 deletion lib/decorators/cache-ttl.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExecutionContext, SetMetadata } from '@nestjs/common';
import { CACHE_TTL_METADATA } from '../cache.constants';
import { CACHE_TTL_METADATA } from '../cache.constants.js';
export type CacheTTLFactory = (
ctx: ExecutionContext,
) => Promise<number> | number;
Expand Down
4 changes: 2 additions & 2 deletions lib/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './cache-key.decorator';
export * from './cache-ttl.decorator';
export * from './cache-key.decorator.js';
export * from './cache-ttl.decorator.js';
10 changes: 5 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './cache.constants';
export * from './cache.module';
export * from './decorators';
export * from './interceptors';
export * from './interfaces';
export * from './cache.constants.js';
export * from './cache.module.js';
export * from './decorators/index.js';
export * from './interceptors/index.js';
export * from './interfaces/index.js';
19 changes: 14 additions & 5 deletions lib/interceptors/cache.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@ import {
Optional,
StreamableFile,
} from '@nestjs/common';
import { isFunction, isNil } from '@nestjs/common/utils/shared.utils';
import { HttpAdapterHost, Reflector } from '@nestjs/core';
import { Observable, of } from 'rxjs';
import { tap } from 'rxjs/operators';
import {
CACHE_KEY_METADATA,
CACHE_MANAGER,
CACHE_TTL_METADATA,
} from '../cache.constants';
import { CacheKeyFactory, CacheTTLFactory } from '../decorators';
} from '../cache.constants.js';
import { CacheKeyFactory, CacheTTLFactory } from '../decorators/index.js';

const isCacheKeyFactory = (
value: string | CacheKeyFactory | undefined,
): value is CacheKeyFactory => typeof value === 'function';

const isCacheTTLFactory = (
value: number | CacheTTLFactory | null,
): value is CacheTTLFactory => typeof value === 'function';

const isNil = (value: unknown): value is null | undefined => value == null;

/**
* @see [Caching](https://docs.nestjs.com/techniques/caching)
Expand Down Expand Up @@ -57,7 +66,7 @@ export class CacheInterceptor implements NestInterceptor {
if (!isNil(value)) {
return of(value);
}
const ttl = isFunction(ttlValueOrFactory)
const ttl = isCacheTTLFactory(ttlValueOrFactory)
? await ttlValueOrFactory(context)
: ttlValueOrFactory;

Expand Down Expand Up @@ -97,7 +106,7 @@ export class CacheInterceptor implements NestInterceptor {
this.reflector.get(CACHE_KEY_METADATA, context.getHandler()) ?? undefined;

if (!isHttpApp || cacheMetadataOrFactory) {
if (isFunction(cacheMetadataOrFactory)) {
if (isCacheKeyFactory(cacheMetadataOrFactory)) {
const cacheKey = cacheMetadataOrFactory(context);
return cacheKey;
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/interceptors/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './cache.interceptor';
export * from './cache.interceptor.js';
2 changes: 1 addition & 1 deletion lib/interfaces/cache-module.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ConfigurableModuleAsyncOptions, Provider, Type } from '@nestjs/common';
import { CacheManagerOptions } from './cache-manager.interface';
import { CacheManagerOptions } from './cache-manager.interface.js';

export type CacheOptions<
StoreConfig extends Record<any, any> = Record<string, any>,
Expand Down
4 changes: 2 additions & 2 deletions lib/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './cache-manager.interface';
export * from './cache-module.interface';
export * from './cache-manager.interface.js';
export * from './cache-module.interface.js';
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@nestjs/cache-manager",
"version": "3.1.2",
"type": "module",
"description": "Nest - modern, fast, powerful node.js web framework (@cache-manager)",
"author": "Kamil Mysliwiec",
"license": "MIT",
Expand All @@ -18,6 +19,14 @@
"release": "release-it",
"prepare": "husky"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}
},
"devDependencies": {
"@commitlint/cli": "20.5.3",
"@commitlint/config-angular": "20.5.3",
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"compilerOptions": {
"module": "commonjs",
"module": "NodeNext",
"declaration": true,
"removeComments": false,
"noLib": false,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"target": "ES2021",
"moduleResolution": "NodeNext",
"sourceMap": false,
"outDir": "./dist",
"rootDir": "./lib",
Expand Down