@cosystem/storage 0.0.2

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Coaction
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.
@@ -0,0 +1,23 @@
1
+ import { App, Plugin, StateChangeEvent } from "@cosystem/core";
2
+
3
+ //#region src/index.d.ts
4
+ interface StorageLike {
5
+ getItem(key: string): string | null | Promise<string | null>;
6
+ setItem(key: string, value: string): void | Promise<void>;
7
+ removeItem?(key: string): void | Promise<void>;
8
+ }
9
+ interface StoragePluginOptions<TState = unknown> {
10
+ readonly key: string;
11
+ readonly storage: StorageLike;
12
+ readonly serialize?: (state: TState) => string;
13
+ readonly deserialize?: (value: string) => TState;
14
+ readonly shouldPersist?: (event: StateChangeEvent) => boolean;
15
+ }
16
+ interface StoragePlugin extends Plugin {
17
+ clear(): Promise<void>;
18
+ persist(app: App): Promise<void>;
19
+ }
20
+ declare function createStoragePlugin<TState = unknown>(options: StoragePluginOptions<TState>): StoragePlugin;
21
+ //#endregion
22
+ export { StorageLike, StoragePlugin, StoragePluginOptions, createStoragePlugin };
23
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";;;UAEiB,WAAA;EACf,OAAA,CAAQ,GAAA,2BAA8B,OAAA;EACtC,OAAA,CAAQ,GAAA,UAAa,KAAA,kBAAuB,OAAA;EAC5C,UAAA,EAAY,GAAA,kBAAqB,OAAA;AAAA;AAAA,UAGlB,oBAAA;EAAA,SACN,GAAA;EAAA,SACA,OAAA,EAAS,WAAA;EAAA,SACT,SAAA,IAAa,KAAA,EAAO,MAAA;EAAA,SACpB,WAAA,IAAe,KAAA,aAAkB,MAAA;EAAA,SACjC,aAAA,IAAiB,KAAA,EAAO,gBAAA;AAAA;AAAA,UAGlB,aAAA,SAAsB,MAAA;EACrC,KAAA,IAAS,OAAA;EACT,OAAA,CAAQ,GAAA,EAAK,GAAA,GAAM,OAAA;AAAA;AAAA,iBAGL,mBAAA,mBACd,OAAA,EAAS,oBAAA,CAAqB,MAAA,IAC7B,aAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,27 @@
1
+ //#region src/index.ts
2
+ function createStoragePlugin(options) {
3
+ const serialize = options.serialize ?? JSON.stringify;
4
+ const deserialize = options.deserialize ?? JSON.parse;
5
+ return {
6
+ name: "cosystem:storage",
7
+ async clear() {
8
+ await options.storage.removeItem?.(options.key);
9
+ },
10
+ async onStateChange(event) {
11
+ if (options.shouldPersist !== void 0 && !options.shouldPersist(event)) return;
12
+ await options.storage.setItem(options.key, serialize(event.state));
13
+ },
14
+ async persist(app) {
15
+ await options.storage.setItem(options.key, serialize(app.store.getPureState()));
16
+ },
17
+ async setup(app) {
18
+ const stored = await options.storage.getItem(options.key);
19
+ if (stored === null) return;
20
+ app.store.setState(deserialize(stored));
21
+ }
22
+ };
23
+ }
24
+ //#endregion
25
+ export { createStoragePlugin };
26
+
27
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { App, Plugin, StateChangeEvent } from \"@cosystem/core\";\n\nexport interface StorageLike {\n getItem(key: string): string | null | Promise<string | null>;\n setItem(key: string, value: string): void | Promise<void>;\n removeItem?(key: string): void | Promise<void>;\n}\n\nexport interface StoragePluginOptions<TState = unknown> {\n readonly key: string;\n readonly storage: StorageLike;\n readonly serialize?: (state: TState) => string;\n readonly deserialize?: (value: string) => TState;\n readonly shouldPersist?: (event: StateChangeEvent) => boolean;\n}\n\nexport interface StoragePlugin extends Plugin {\n clear(): Promise<void>;\n persist(app: App): Promise<void>;\n}\n\nexport function createStoragePlugin<TState = unknown>(\n options: StoragePluginOptions<TState>,\n): StoragePlugin {\n const serialize = options.serialize ?? JSON.stringify;\n const deserialize = options.deserialize ?? JSON.parse;\n\n return {\n name: \"cosystem:storage\",\n async clear() {\n await options.storage.removeItem?.(options.key);\n },\n async onStateChange(event) {\n if (options.shouldPersist !== undefined && !options.shouldPersist(event)) {\n return;\n }\n\n await options.storage.setItem(options.key, serialize(event.state as TState));\n },\n async persist(app) {\n await options.storage.setItem(options.key, serialize(app.store.getPureState() as TState));\n },\n async setup(app) {\n const stored = await options.storage.getItem(options.key);\n\n if (stored === null) {\n return;\n }\n\n app.store.setState(deserialize(stored) as never);\n },\n };\n}\n"],"mappings":";AAqBA,SAAgB,oBACd,SACe;CACf,MAAM,YAAY,QAAQ,aAAa,KAAK;CAC5C,MAAM,cAAc,QAAQ,eAAe,KAAK;CAEhD,OAAO;EACL,MAAM;EACN,MAAM,QAAQ;GACZ,MAAM,QAAQ,QAAQ,aAAa,QAAQ,GAAG;EAChD;EACA,MAAM,cAAc,OAAO;GACzB,IAAI,QAAQ,kBAAkB,KAAA,KAAa,CAAC,QAAQ,cAAc,KAAK,GACrE;GAGF,MAAM,QAAQ,QAAQ,QAAQ,QAAQ,KAAK,UAAU,MAAM,KAAe,CAAC;EAC7E;EACA,MAAM,QAAQ,KAAK;GACjB,MAAM,QAAQ,QAAQ,QAAQ,QAAQ,KAAK,UAAU,IAAI,MAAM,aAAa,CAAW,CAAC;EAC1F;EACA,MAAM,MAAM,KAAK;GACf,MAAM,SAAS,MAAM,QAAQ,QAAQ,QAAQ,QAAQ,GAAG;GAExD,IAAI,WAAW,MACb;GAGF,IAAI,MAAM,SAAS,YAAY,MAAM,CAAU;EACjD;CACF;AACF"}
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@cosystem/storage",
3
+ "version": "0.0.2",
4
+ "description": "Persistence plugin for CoSystem.",
5
+ "license": "MIT",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "type": "module",
10
+ "sideEffects": false,
11
+ "module": "./dist/index.js",
12
+ "types": "./dist/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js"
17
+ }
18
+ },
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "dependencies": {
23
+ "@cosystem/core": "0.0.2"
24
+ },
25
+ "devDependencies": {
26
+ "tsdown": "0.22.3",
27
+ "typescript": "6.0.3",
28
+ "vitest": "4.1.9",
29
+ "@cosystem/tsconfig": "0.0.2"
30
+ },
31
+ "scripts": {
32
+ "build": "tsdown",
33
+ "clean": "rm -rf coverage dist .turbo",
34
+ "dev": "tsdown --watch",
35
+ "test": "cd ../.. && vitest run --project @cosystem/storage",
36
+ "test:coverage": "cd ../.. && vitest run --coverage --project @cosystem/storage",
37
+ "typecheck": "tsc -p tsconfig.json --noEmit"
38
+ }
39
+ }