From 0c47a64c45d8ea6e5b6acf7bfb4145a0ace0f412 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Fri, 1 May 2026 10:01:27 +0200 Subject: [PATCH 1/2] chore: migrate to esm --- index.d.ts | 2 +- index.js | 7 +------ index.ts | 2 +- lib/cache.constants.ts | 2 +- lib/cache.module-definition.ts | 2 +- lib/cache.module.ts | 8 ++++---- lib/cache.providers.ts | 6 +++--- lib/decorators/cache-key.decorator.ts | 2 +- lib/decorators/cache-ttl.decorator.ts | 2 +- lib/decorators/index.ts | 4 ++-- lib/index.ts | 10 +++++----- lib/interceptors/cache.interceptor.ts | 19 ++++++++++++++----- lib/interceptors/index.ts | 2 +- lib/interfaces/cache-module.interface.ts | 2 +- lib/interfaces/index.ts | 4 ++-- package.json | 9 +++++++++ tsconfig.json | 3 ++- 17 files changed, 50 insertions(+), 36 deletions(-) diff --git a/index.d.ts b/index.d.ts index 5703fb5f..2b8395cd 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1 +1 @@ -export * from './dist'; +export * from './dist/index.js'; diff --git a/index.js b/index.js index 9368dd39..2b8395cd 100644 --- a/index.js +++ b/index.js @@ -1,6 +1 @@ -"use strict"; -function __export(m) { - for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; -} -exports.__esModule = true; -__export(require("./dist")); +export * from './dist/index.js'; diff --git a/index.ts b/index.ts index 5703fb5f..2b8395cd 100644 --- a/index.ts +++ b/index.ts @@ -1 +1 @@ -export * from './dist'; +export * from './dist/index.js'; diff --git a/lib/cache.constants.ts b/lib/cache.constants.ts index aa1e752d..7b4a46cb 100644 --- a/lib/cache.constants.ts +++ b/lib/cache.constants.ts @@ -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'; diff --git a/lib/cache.module-definition.ts b/lib/cache.module-definition.ts index 24805e5d..bd20570a 100644 --- a/lib/cache.module-definition.ts +++ b/lib/cache.module-definition.ts @@ -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({ diff --git a/lib/cache.module.ts b/lib/cache.module.ts index 6386b52b..11f32f47 100644 --- a/lib/cache.module.ts +++ b/lib/cache.module.ts @@ -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 diff --git a/lib/cache.providers.ts b/lib/cache.providers.ts index 0cdb80de..514cb880 100644 --- a/lib/cache.providers.ts +++ b/lib/cache.providers.ts @@ -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 ( diff --git a/lib/decorators/cache-key.decorator.ts b/lib/decorators/cache-key.decorator.ts index 4d9fdba0..1e76d699 100644 --- a/lib/decorators/cache-key.decorator.ts +++ b/lib/decorators/cache-key.decorator.ts @@ -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, diff --git a/lib/decorators/cache-ttl.decorator.ts b/lib/decorators/cache-ttl.decorator.ts index 202c1d90..6930f401 100644 --- a/lib/decorators/cache-ttl.decorator.ts +++ b/lib/decorators/cache-ttl.decorator.ts @@ -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; diff --git a/lib/decorators/index.ts b/lib/decorators/index.ts index 9d60a27f..675b0052 100644 --- a/lib/decorators/index.ts +++ b/lib/decorators/index.ts @@ -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'; diff --git a/lib/index.ts b/lib/index.ts index 1a5dfc37..c8ed480c 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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'; diff --git a/lib/interceptors/cache.interceptor.ts b/lib/interceptors/cache.interceptor.ts index 9bddf788..21b7b33a 100644 --- a/lib/interceptors/cache.interceptor.ts +++ b/lib/interceptors/cache.interceptor.ts @@ -8,7 +8,6 @@ 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'; @@ -16,8 +15,18 @@ 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) @@ -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; @@ -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 { diff --git a/lib/interceptors/index.ts b/lib/interceptors/index.ts index 474887b1..a0f915a2 100644 --- a/lib/interceptors/index.ts +++ b/lib/interceptors/index.ts @@ -1 +1 @@ -export * from './cache.interceptor'; +export * from './cache.interceptor.js'; diff --git a/lib/interfaces/cache-module.interface.ts b/lib/interfaces/cache-module.interface.ts index 6c62cccf..87ac0c7c 100644 --- a/lib/interfaces/cache-module.interface.ts +++ b/lib/interfaces/cache-module.interface.ts @@ -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 = Record, diff --git a/lib/interfaces/index.ts b/lib/interfaces/index.ts index 54aa4c3c..9456f6a4 100644 --- a/lib/interfaces/index.ts +++ b/lib/interfaces/index.ts @@ -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'; diff --git a/package.json b/package.json index 5b40e062..1948c635 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/tsconfig.json b/tsconfig.json index 5b758377..2e34c6c7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "module": "commonjs", + "module": "NodeNext", "declaration": true, "removeComments": false, "noLib": false, @@ -8,6 +8,7 @@ "esModuleInterop": true, "experimentalDecorators": true, "target": "ES2021", + "moduleResolution": "NodeNext", "sourceMap": false, "outDir": "./dist", "rootDir": "./lib", From 297c1def4bbb5c2032bf6d4cd361eb8def1eb031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Fri, 1 May 2026 10:04:16 +0200 Subject: [PATCH 2/2] chore: remove unecessary index file --- .npmignore | 1 - index.d.ts | 1 - index.js | 1 - index.ts | 1 - 4 files changed, 4 deletions(-) delete mode 100644 index.d.ts delete mode 100644 index.js delete mode 100644 index.ts diff --git a/.npmignore b/.npmignore index 194a72d3..a3419b06 100644 --- a/.npmignore +++ b/.npmignore @@ -1,7 +1,6 @@ # source lib tests -index.ts # misc package-lock.json diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 2b8395cd..00000000 --- a/index.d.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './dist/index.js'; diff --git a/index.js b/index.js deleted file mode 100644 index 2b8395cd..00000000 --- a/index.js +++ /dev/null @@ -1 +0,0 @@ -export * from './dist/index.js'; diff --git a/index.ts b/index.ts deleted file mode 100644 index 2b8395cd..00000000 --- a/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './dist/index.js';