nest-unstorage 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +67 -0
- package/dist/index.cjs +116 -0
- package/dist/index.d.cts +48 -0
- package/package.json +89 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Tankosin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# nest-unstorage
|
|
2
|
+
|
|
3
|
+
[![npm version][npm-version-src]][npm-version-href]
|
|
4
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
5
|
+
[![License][license-src]][license-href]
|
|
6
|
+
|
|
7
|
+
Type-safe [unstorage](https://unstorage.unjs.io/) integration for [NestJS](https://nestjs.com/) with an injectable, driver-based key-value storage.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
pnpm add nest-unstorage unstorage
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { Module } from '@nestjs/common'
|
|
19
|
+
import { StorageModule } from 'nest-unstorage'
|
|
20
|
+
|
|
21
|
+
@Module({
|
|
22
|
+
imports: [
|
|
23
|
+
StorageModule.forRoot({
|
|
24
|
+
driver: { driver: 'redis', url: process.env.REDIS_URL },
|
|
25
|
+
}),
|
|
26
|
+
],
|
|
27
|
+
})
|
|
28
|
+
export class AppModule {}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import type { Storage } from 'unstorage'
|
|
33
|
+
import { Injectable } from '@nestjs/common'
|
|
34
|
+
import { InjectStorage } from 'nest-unstorage'
|
|
35
|
+
|
|
36
|
+
@Injectable()
|
|
37
|
+
export class TokenService {
|
|
38
|
+
constructor(@InjectStorage() private readonly storage: Storage) {}
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Built-in drivers load on demand; install their peer deps (`ioredis` for `redis`).
|
|
43
|
+
|
|
44
|
+
### Async
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
StorageModule.forRootAsync({
|
|
48
|
+
imports: [ConfigModule],
|
|
49
|
+
inject: [ConfigService],
|
|
50
|
+
useFactory: (config: ConfigService) => ({
|
|
51
|
+
driver: { driver: 'redis', url: config.getOrThrow('REDIS_URL') },
|
|
52
|
+
}),
|
|
53
|
+
})
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## License
|
|
57
|
+
|
|
58
|
+
[MIT](./LICENSE) License © [Tankosin](https://github.com/tankosin)
|
|
59
|
+
|
|
60
|
+
<!-- Badges -->
|
|
61
|
+
|
|
62
|
+
[npm-version-src]: https://npmx.dev/api/registry/badge/version/nest-unstorage
|
|
63
|
+
[npm-version-href]: https://npmx.dev/package/nest-unstorage
|
|
64
|
+
[npm-downloads-src]: https://npmx.dev/api/registry/badge/downloads/nest-unstorage
|
|
65
|
+
[npm-downloads-href]: https://npmx.dev/package/nest-unstorage
|
|
66
|
+
[license-src]: https://img.shields.io/npm/l/nest-unstorage?style=flat&colorA=080f12&colorB=1fa669
|
|
67
|
+
[license-href]: ./LICENSE
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _nestjs_common = require("@nestjs/common");
|
|
3
|
+
let unstorage = require("unstorage");
|
|
4
|
+
//#region src/storage.constants.ts
|
|
5
|
+
/** Injection token for the root unstorage `Storage` instance. */
|
|
6
|
+
const STORAGE = "nest-unstorage:storage";
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/storage.decorators.ts
|
|
9
|
+
/** Property/parameter decorator that injects the {@link STORAGE} instance. */
|
|
10
|
+
function InjectStorage() {
|
|
11
|
+
return (0, _nestjs_common.Inject)(STORAGE);
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/storage.module-definition.ts
|
|
15
|
+
const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN, OPTIONS_TYPE, ASYNC_OPTIONS_TYPE } = new _nestjs_common.ConfigurableModuleBuilder({ moduleName: "Storage" }).setClassMethodName("forRoot").setExtras({ isGlobal: true }, (definition, extras) => ({
|
|
16
|
+
...definition,
|
|
17
|
+
global: extras.isGlobal
|
|
18
|
+
})).build();
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/storage.factory.ts
|
|
21
|
+
function isMountConfig(config) {
|
|
22
|
+
return typeof config.driver === "string";
|
|
23
|
+
}
|
|
24
|
+
async function resolveDriver(config) {
|
|
25
|
+
if (!isMountConfig(config)) return config;
|
|
26
|
+
const { driver, ...options } = config;
|
|
27
|
+
const specifier = unstorage.builtinDrivers[driver] ?? driver;
|
|
28
|
+
let module;
|
|
29
|
+
try {
|
|
30
|
+
module = await import(specifier);
|
|
31
|
+
} catch (cause) {
|
|
32
|
+
throw new Error(`[nest-unstorage] Failed to load driver "${driver}". Install its peer dependency if it is a built-in driver.`, { cause });
|
|
33
|
+
}
|
|
34
|
+
const factory = module.default ?? module;
|
|
35
|
+
if (typeof factory !== "function") throw new TypeError(`[nest-unstorage] Driver "${driver}" did not resolve to a factory function.`);
|
|
36
|
+
return factory(options);
|
|
37
|
+
}
|
|
38
|
+
async function createStorageInstance(options) {
|
|
39
|
+
const mounts = Object.entries(options.mounts ?? {});
|
|
40
|
+
const [rootDriver, mountDrivers] = await Promise.all([options.driver ? resolveDriver(options.driver) : Promise.resolve(void 0), Promise.all(mounts.map(async ([, config]) => resolveDriver(config)))]);
|
|
41
|
+
const storage = (0, unstorage.createStorage)({ driver: rootDriver });
|
|
42
|
+
mounts.forEach(([base], index) => storage.mount(base, mountDrivers[index]));
|
|
43
|
+
return storage;
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region \0@oxc-project+runtime@0.135.0/helpers/esm/decorateMetadata.js
|
|
47
|
+
function __decorateMetadata(k, v) {
|
|
48
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
49
|
+
}
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region \0@oxc-project+runtime@0.135.0/helpers/esm/decorateParam.js
|
|
52
|
+
function __decorateParam(paramIndex, decorator) {
|
|
53
|
+
return function(target, key) {
|
|
54
|
+
decorator(target, key, paramIndex);
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region \0@oxc-project+runtime@0.135.0/helpers/esm/decorate.js
|
|
59
|
+
function __decorate(decorators, target, key, desc) {
|
|
60
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
61
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
62
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
63
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
64
|
+
}
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/storage.providers.ts
|
|
67
|
+
const storageProvider = {
|
|
68
|
+
provide: STORAGE,
|
|
69
|
+
useFactory: createStorageInstance,
|
|
70
|
+
inject: [MODULE_OPTIONS_TOKEN]
|
|
71
|
+
};
|
|
72
|
+
let StorageDisposer = class StorageDisposer {
|
|
73
|
+
storage;
|
|
74
|
+
constructor(storage) {
|
|
75
|
+
this.storage = storage;
|
|
76
|
+
}
|
|
77
|
+
async onApplicationShutdown() {
|
|
78
|
+
await this.storage.dispose();
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
StorageDisposer = __decorate([
|
|
82
|
+
(0, _nestjs_common.Injectable)(),
|
|
83
|
+
__decorateParam(0, (0, _nestjs_common.Inject)(STORAGE)),
|
|
84
|
+
__decorateMetadata("design:paramtypes", [Object])
|
|
85
|
+
], StorageDisposer);
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/storage.module.ts
|
|
88
|
+
let StorageModule = class StorageModule extends ConfigurableModuleClass {
|
|
89
|
+
static forRoot(options) {
|
|
90
|
+
return this.withStorage(super.forRoot(options));
|
|
91
|
+
}
|
|
92
|
+
static forRootAsync(options) {
|
|
93
|
+
return this.withStorage(super.forRootAsync(options));
|
|
94
|
+
}
|
|
95
|
+
static withStorage(definition) {
|
|
96
|
+
return {
|
|
97
|
+
...definition,
|
|
98
|
+
providers: [
|
|
99
|
+
...definition.providers ?? [],
|
|
100
|
+
storageProvider,
|
|
101
|
+
StorageDisposer
|
|
102
|
+
],
|
|
103
|
+
exports: [...definition.exports ?? [], STORAGE]
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
StorageModule = __decorate([(0, _nestjs_common.Module)({})], StorageModule);
|
|
108
|
+
//#endregion
|
|
109
|
+
exports.InjectStorage = InjectStorage;
|
|
110
|
+
exports.STORAGE = STORAGE;
|
|
111
|
+
Object.defineProperty(exports, "StorageModule", {
|
|
112
|
+
enumerable: true,
|
|
113
|
+
get: function() {
|
|
114
|
+
return StorageModule;
|
|
115
|
+
}
|
|
116
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { DynamicModule } from "@nestjs/common";
|
|
2
|
+
import { BuiltinDriverName, BuiltinDriverOptions, Driver } from "unstorage";
|
|
3
|
+
|
|
4
|
+
//#region src/storage.constants.d.ts
|
|
5
|
+
/** Injection token for the root unstorage `Storage` instance. */
|
|
6
|
+
declare const STORAGE = "nest-unstorage:storage";
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/storage.decorators.d.ts
|
|
9
|
+
/** Property/parameter decorator that injects the {@link STORAGE} instance. */
|
|
10
|
+
declare function InjectStorage(): PropertyDecorator & ParameterDecorator;
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/storage.types.d.ts
|
|
13
|
+
type OptionsFor<Name extends BuiltinDriverName> = Name extends keyof BuiltinDriverOptions ? BuiltinDriverOptions[Name] : unknown;
|
|
14
|
+
/**
|
|
15
|
+
* A built-in driver referenced by name with its options inlined and type-checked
|
|
16
|
+
* (e.g. `{ driver: 'fs', base: './data' }`).
|
|
17
|
+
*/
|
|
18
|
+
type BuiltinMountConfig = { [Name in BuiltinDriverName]: {
|
|
19
|
+
driver: Name;
|
|
20
|
+
} & OptionsFor<Name> }[BuiltinDriverName];
|
|
21
|
+
/**
|
|
22
|
+
* A mount's driver: a built-in referenced by name ({@link BuiltinMountConfig}) or a
|
|
23
|
+
* `Driver` instance (e.g. `memoryDriver()`).
|
|
24
|
+
*/
|
|
25
|
+
type StorageDriverConfig = Driver | BuiltinMountConfig;
|
|
26
|
+
/** Options for {@link StorageModule.forRoot}. */
|
|
27
|
+
interface StorageModuleOptions {
|
|
28
|
+
/** Root driver for unprefixed keys. Defaults to an in-memory driver. */
|
|
29
|
+
driver?: StorageDriverConfig;
|
|
30
|
+
/** Mountpoints keyed by base, each routed to its own driver. */
|
|
31
|
+
mounts?: Record<string, StorageDriverConfig>;
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/storage.module-definition.d.ts
|
|
35
|
+
interface StorageModuleExtraOptions {
|
|
36
|
+
/** Register the module globally. Defaults to `true`. */
|
|
37
|
+
isGlobal?: boolean;
|
|
38
|
+
}
|
|
39
|
+
declare const ConfigurableModuleClass: import("@nestjs/common").ConfigurableModuleCls<StorageModuleOptions, "forRoot", "create", StorageModuleExtraOptions>, MODULE_OPTIONS_TOKEN: string | symbol, OPTIONS_TYPE: StorageModuleOptions & Partial<StorageModuleExtraOptions>, ASYNC_OPTIONS_TYPE: import("@nestjs/common").ConfigurableModuleAsyncOptions<StorageModuleOptions, "create"> & Partial<StorageModuleExtraOptions>;
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/storage.module.d.ts
|
|
42
|
+
declare class StorageModule extends ConfigurableModuleClass {
|
|
43
|
+
static forRoot(options: typeof OPTIONS_TYPE): DynamicModule;
|
|
44
|
+
static forRootAsync(options: typeof ASYNC_OPTIONS_TYPE): DynamicModule;
|
|
45
|
+
private static withStorage;
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
export { type BuiltinMountConfig, InjectStorage, STORAGE, type StorageDriverConfig, StorageModule, type StorageModuleOptions };
|
package/package.json
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nest-unstorage",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"description": "Type-safe unstorage integration for NestJS with an injectable, driver-based key-value storage.",
|
|
6
|
+
"author": "Tankosin <tankosinn@gmail.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://github.com/tankosin/nest-unstorage#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/tankosin/nest-unstorage.git"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/tankosin/nest-unstorage/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"nestjs",
|
|
18
|
+
"nest",
|
|
19
|
+
"unstorage",
|
|
20
|
+
"unjs",
|
|
21
|
+
"nitro",
|
|
22
|
+
"storage",
|
|
23
|
+
"key-value",
|
|
24
|
+
"kv",
|
|
25
|
+
"cache",
|
|
26
|
+
"driver",
|
|
27
|
+
"dynamic-module"
|
|
28
|
+
],
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./dist/index.d.cts",
|
|
33
|
+
"default": "./dist/index.cjs"
|
|
34
|
+
},
|
|
35
|
+
"./package.json": "./package.json"
|
|
36
|
+
},
|
|
37
|
+
"main": "./dist/index.cjs",
|
|
38
|
+
"types": "./dist/index.d.cts",
|
|
39
|
+
"files": [
|
|
40
|
+
"dist"
|
|
41
|
+
],
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=18.12.0"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@nestjs/common": "^10.0.0 || ^11.0.0",
|
|
47
|
+
"reflect-metadata": "^0.1.13 || ^0.2.0",
|
|
48
|
+
"unstorage": "^1.10.0"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@antfu/eslint-config": "9.0.0",
|
|
52
|
+
"@nestjs/common": "11.1.26",
|
|
53
|
+
"@nestjs/core": "11.1.26",
|
|
54
|
+
"@nestjs/testing": "11.1.26",
|
|
55
|
+
"@types/node": "25.9.3",
|
|
56
|
+
"@vitest/coverage-v8": "4.1.8",
|
|
57
|
+
"bumpp": "11.1.0",
|
|
58
|
+
"eslint": "10.4.1",
|
|
59
|
+
"installed-check": "10.0.1",
|
|
60
|
+
"lint-staged": "17.0.7",
|
|
61
|
+
"reflect-metadata": "0.2.2",
|
|
62
|
+
"rxjs": "7.8.2",
|
|
63
|
+
"simple-git-hooks": "2.13.1",
|
|
64
|
+
"tsdown": "0.22.2",
|
|
65
|
+
"tsx": "4.22.4",
|
|
66
|
+
"typescript": "6.0.3",
|
|
67
|
+
"unstorage": "1.17.5",
|
|
68
|
+
"vitest": "4.1.8"
|
|
69
|
+
},
|
|
70
|
+
"simple-git-hooks": {
|
|
71
|
+
"pre-commit": "pnpm lint-staged"
|
|
72
|
+
},
|
|
73
|
+
"lint-staged": {
|
|
74
|
+
"*": "eslint --cache --fix"
|
|
75
|
+
},
|
|
76
|
+
"scripts": {
|
|
77
|
+
"build": "tsdown",
|
|
78
|
+
"dev": "tsdown --watch",
|
|
79
|
+
"lint": "eslint --cache .",
|
|
80
|
+
"lint:fix": "eslint --cache --fix .",
|
|
81
|
+
"test": "vitest run",
|
|
82
|
+
"test:cov": "vitest run --coverage",
|
|
83
|
+
"test:e2e": "vitest run --project e2e",
|
|
84
|
+
"test:unit": "vitest run --project unit",
|
|
85
|
+
"typecheck": "tsc --noEmit",
|
|
86
|
+
"publish:ci": "tsx scripts/publish.ts",
|
|
87
|
+
"release": "bumpp"
|
|
88
|
+
}
|
|
89
|
+
}
|