@form-engine-ts/storage-localstorage 1.0.0
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 +23 -0
- package/dist/index.cjs +129 -0
- package/dist/index.d.cts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +104 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 nitta-a
|
|
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,23 @@
|
|
|
1
|
+
# @form-engine-ts/storage-localstorage
|
|
2
|
+
|
|
3
|
+
Browser `localStorage` implementation of the form-engine-ts storage contract with prefix isolation and injectable storage.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @form-engine-ts/core @form-engine-ts/storage-localstorage
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createLocalStorageAdapter } from "@form-engine-ts/storage-localstorage";
|
|
15
|
+
|
|
16
|
+
const storage = createLocalStorageAdapter("my-app:");
|
|
17
|
+
await storage.saveSchema(schema);
|
|
18
|
+
await storage.saveSubmission(submission);
|
|
19
|
+
|
|
20
|
+
const responses = await storage.listSubmissions(schema.id, schema.version);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Outside a browser, pass a compatible `StorageLike` object as the second argument.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createLocalStorageAdapter: () => createLocalStorageAdapter
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var import_core = require("@form-engine-ts/core");
|
|
27
|
+
function isRecord(value) {
|
|
28
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
29
|
+
}
|
|
30
|
+
function cloneJson(value) {
|
|
31
|
+
return JSON.parse(JSON.stringify(value));
|
|
32
|
+
}
|
|
33
|
+
function parseJson(value, key) {
|
|
34
|
+
try {
|
|
35
|
+
return JSON.parse(value);
|
|
36
|
+
} catch (cause) {
|
|
37
|
+
throw new Error(`Stored JSON at "${key}" is invalid.`, { cause });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function parseSchema(value, key) {
|
|
41
|
+
const parsed = parseJson(value, key);
|
|
42
|
+
try {
|
|
43
|
+
(0, import_core.assertValidFormSchema)(parsed);
|
|
44
|
+
} catch (cause) {
|
|
45
|
+
throw new Error(`Stored schema at "${key}" is invalid.`, { cause });
|
|
46
|
+
}
|
|
47
|
+
return parsed;
|
|
48
|
+
}
|
|
49
|
+
function parseSubmission(value, key) {
|
|
50
|
+
const parsed = parseJson(value, key);
|
|
51
|
+
if (!isRecord(parsed) || typeof parsed.id !== "string" || typeof parsed.formId !== "string" || !Number.isInteger(parsed.formVersion) || typeof parsed.locale !== "string" || typeof parsed.submittedAt !== "string" || !isRecord(parsed.values)) {
|
|
52
|
+
throw new Error(`Stored submission at "${key}" is invalid.`);
|
|
53
|
+
}
|
|
54
|
+
return parsed;
|
|
55
|
+
}
|
|
56
|
+
function encoded(value) {
|
|
57
|
+
return encodeURIComponent(value);
|
|
58
|
+
}
|
|
59
|
+
function createLocalStorageAdapter(storagePrefix = "pf_", injectedStorage) {
|
|
60
|
+
const storage = injectedStorage ?? (typeof globalThis.localStorage === "undefined" ? void 0 : globalThis.localStorage);
|
|
61
|
+
if (storage === void 0) {
|
|
62
|
+
throw new Error("LocalStorage is unavailable. Pass a StorageLike implementation when running outside a browser.");
|
|
63
|
+
}
|
|
64
|
+
const schemaPrefix = `${storagePrefix}schema:`;
|
|
65
|
+
const submissionPrefix = `${storagePrefix}submission:`;
|
|
66
|
+
const schemaKey = (formId, formVersion) => `${schemaPrefix}${encoded(formId)}:${formVersion}`;
|
|
67
|
+
const submissionKey = (id) => `${submissionPrefix}${encoded(id)}`;
|
|
68
|
+
const prefixedKeys = (prefix) => {
|
|
69
|
+
const keys = [];
|
|
70
|
+
for (let index = 0; index < storage.length; index += 1) {
|
|
71
|
+
const key = storage.key(index);
|
|
72
|
+
if (key?.startsWith(prefix)) keys.push(key);
|
|
73
|
+
}
|
|
74
|
+
return keys;
|
|
75
|
+
};
|
|
76
|
+
return {
|
|
77
|
+
async saveSchema(schema) {
|
|
78
|
+
(0, import_core.assertValidFormSchema)(schema);
|
|
79
|
+
storage.setItem(schemaKey(schema.id, schema.version), JSON.stringify(schema));
|
|
80
|
+
},
|
|
81
|
+
async getSchema(formId, formVersion) {
|
|
82
|
+
const key = schemaKey(formId, formVersion);
|
|
83
|
+
const value = storage.getItem(key);
|
|
84
|
+
return value === null ? null : cloneJson(parseSchema(value, key));
|
|
85
|
+
},
|
|
86
|
+
async listSchemas() {
|
|
87
|
+
return prefixedKeys(schemaPrefix).map((key) => {
|
|
88
|
+
const value = storage.getItem(key);
|
|
89
|
+
if (value === null) throw new Error(`Stored schema at "${key}" disappeared during reading.`);
|
|
90
|
+
return parseSchema(value, key);
|
|
91
|
+
}).sort((left, right) => left.id.localeCompare(right.id) || left.version - right.version).map(cloneJson);
|
|
92
|
+
},
|
|
93
|
+
async deleteSchema(formId, formVersion) {
|
|
94
|
+
storage.removeItem(schemaKey(formId, formVersion));
|
|
95
|
+
},
|
|
96
|
+
async saveSubmission(submission) {
|
|
97
|
+
const key = submissionKey(submission.id);
|
|
98
|
+
if (storage.getItem(key) !== null) throw new Error(`A submission with ID "${submission.id}" already exists.`);
|
|
99
|
+
storage.setItem(key, JSON.stringify(submission));
|
|
100
|
+
},
|
|
101
|
+
async listSubmissions(formId, formVersion) {
|
|
102
|
+
return prefixedKeys(submissionPrefix).map((key) => {
|
|
103
|
+
const value = storage.getItem(key);
|
|
104
|
+
if (value === null) throw new Error(`Stored submission at "${key}" disappeared during reading.`);
|
|
105
|
+
return parseSubmission(value, key);
|
|
106
|
+
}).filter(
|
|
107
|
+
(submission) => submission.formId === formId && (formVersion === void 0 || submission.formVersion === formVersion)
|
|
108
|
+
).sort((left, right) => left.submittedAt.localeCompare(right.submittedAt)).map(cloneJson);
|
|
109
|
+
},
|
|
110
|
+
async deleteSubmission(submissionId) {
|
|
111
|
+
storage.removeItem(submissionKey(submissionId));
|
|
112
|
+
},
|
|
113
|
+
async clearResponses(formId) {
|
|
114
|
+
const matchingKeys = prefixedKeys(submissionPrefix).filter((key) => {
|
|
115
|
+
const value = storage.getItem(key);
|
|
116
|
+
if (value === null) throw new Error(`Stored submission at "${key}" disappeared during reading.`);
|
|
117
|
+
return parseSubmission(value, key).formId === formId;
|
|
118
|
+
});
|
|
119
|
+
for (const key of matchingKeys) storage.removeItem(key);
|
|
120
|
+
},
|
|
121
|
+
async clear() {
|
|
122
|
+
for (const key of prefixedKeys(storagePrefix)) storage.removeItem(key);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
127
|
+
0 && (module.exports = {
|
|
128
|
+
createLocalStorageAdapter
|
|
129
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { FormStorageAdapter } from '@form-engine-ts/core';
|
|
2
|
+
|
|
3
|
+
interface StorageLike {
|
|
4
|
+
readonly length: number;
|
|
5
|
+
getItem(key: string): string | null;
|
|
6
|
+
setItem(key: string, value: string): void;
|
|
7
|
+
removeItem(key: string): void;
|
|
8
|
+
key(index: number): string | null;
|
|
9
|
+
}
|
|
10
|
+
declare function createLocalStorageAdapter(storagePrefix?: string, injectedStorage?: StorageLike): FormStorageAdapter;
|
|
11
|
+
|
|
12
|
+
export { type StorageLike, createLocalStorageAdapter };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { FormStorageAdapter } from '@form-engine-ts/core';
|
|
2
|
+
|
|
3
|
+
interface StorageLike {
|
|
4
|
+
readonly length: number;
|
|
5
|
+
getItem(key: string): string | null;
|
|
6
|
+
setItem(key: string, value: string): void;
|
|
7
|
+
removeItem(key: string): void;
|
|
8
|
+
key(index: number): string | null;
|
|
9
|
+
}
|
|
10
|
+
declare function createLocalStorageAdapter(storagePrefix?: string, injectedStorage?: StorageLike): FormStorageAdapter;
|
|
11
|
+
|
|
12
|
+
export { type StorageLike, createLocalStorageAdapter };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { assertValidFormSchema } from "@form-engine-ts/core";
|
|
3
|
+
function isRecord(value) {
|
|
4
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5
|
+
}
|
|
6
|
+
function cloneJson(value) {
|
|
7
|
+
return JSON.parse(JSON.stringify(value));
|
|
8
|
+
}
|
|
9
|
+
function parseJson(value, key) {
|
|
10
|
+
try {
|
|
11
|
+
return JSON.parse(value);
|
|
12
|
+
} catch (cause) {
|
|
13
|
+
throw new Error(`Stored JSON at "${key}" is invalid.`, { cause });
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function parseSchema(value, key) {
|
|
17
|
+
const parsed = parseJson(value, key);
|
|
18
|
+
try {
|
|
19
|
+
assertValidFormSchema(parsed);
|
|
20
|
+
} catch (cause) {
|
|
21
|
+
throw new Error(`Stored schema at "${key}" is invalid.`, { cause });
|
|
22
|
+
}
|
|
23
|
+
return parsed;
|
|
24
|
+
}
|
|
25
|
+
function parseSubmission(value, key) {
|
|
26
|
+
const parsed = parseJson(value, key);
|
|
27
|
+
if (!isRecord(parsed) || typeof parsed.id !== "string" || typeof parsed.formId !== "string" || !Number.isInteger(parsed.formVersion) || typeof parsed.locale !== "string" || typeof parsed.submittedAt !== "string" || !isRecord(parsed.values)) {
|
|
28
|
+
throw new Error(`Stored submission at "${key}" is invalid.`);
|
|
29
|
+
}
|
|
30
|
+
return parsed;
|
|
31
|
+
}
|
|
32
|
+
function encoded(value) {
|
|
33
|
+
return encodeURIComponent(value);
|
|
34
|
+
}
|
|
35
|
+
function createLocalStorageAdapter(storagePrefix = "pf_", injectedStorage) {
|
|
36
|
+
const storage = injectedStorage ?? (typeof globalThis.localStorage === "undefined" ? void 0 : globalThis.localStorage);
|
|
37
|
+
if (storage === void 0) {
|
|
38
|
+
throw new Error("LocalStorage is unavailable. Pass a StorageLike implementation when running outside a browser.");
|
|
39
|
+
}
|
|
40
|
+
const schemaPrefix = `${storagePrefix}schema:`;
|
|
41
|
+
const submissionPrefix = `${storagePrefix}submission:`;
|
|
42
|
+
const schemaKey = (formId, formVersion) => `${schemaPrefix}${encoded(formId)}:${formVersion}`;
|
|
43
|
+
const submissionKey = (id) => `${submissionPrefix}${encoded(id)}`;
|
|
44
|
+
const prefixedKeys = (prefix) => {
|
|
45
|
+
const keys = [];
|
|
46
|
+
for (let index = 0; index < storage.length; index += 1) {
|
|
47
|
+
const key = storage.key(index);
|
|
48
|
+
if (key?.startsWith(prefix)) keys.push(key);
|
|
49
|
+
}
|
|
50
|
+
return keys;
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
async saveSchema(schema) {
|
|
54
|
+
assertValidFormSchema(schema);
|
|
55
|
+
storage.setItem(schemaKey(schema.id, schema.version), JSON.stringify(schema));
|
|
56
|
+
},
|
|
57
|
+
async getSchema(formId, formVersion) {
|
|
58
|
+
const key = schemaKey(formId, formVersion);
|
|
59
|
+
const value = storage.getItem(key);
|
|
60
|
+
return value === null ? null : cloneJson(parseSchema(value, key));
|
|
61
|
+
},
|
|
62
|
+
async listSchemas() {
|
|
63
|
+
return prefixedKeys(schemaPrefix).map((key) => {
|
|
64
|
+
const value = storage.getItem(key);
|
|
65
|
+
if (value === null) throw new Error(`Stored schema at "${key}" disappeared during reading.`);
|
|
66
|
+
return parseSchema(value, key);
|
|
67
|
+
}).sort((left, right) => left.id.localeCompare(right.id) || left.version - right.version).map(cloneJson);
|
|
68
|
+
},
|
|
69
|
+
async deleteSchema(formId, formVersion) {
|
|
70
|
+
storage.removeItem(schemaKey(formId, formVersion));
|
|
71
|
+
},
|
|
72
|
+
async saveSubmission(submission) {
|
|
73
|
+
const key = submissionKey(submission.id);
|
|
74
|
+
if (storage.getItem(key) !== null) throw new Error(`A submission with ID "${submission.id}" already exists.`);
|
|
75
|
+
storage.setItem(key, JSON.stringify(submission));
|
|
76
|
+
},
|
|
77
|
+
async listSubmissions(formId, formVersion) {
|
|
78
|
+
return prefixedKeys(submissionPrefix).map((key) => {
|
|
79
|
+
const value = storage.getItem(key);
|
|
80
|
+
if (value === null) throw new Error(`Stored submission at "${key}" disappeared during reading.`);
|
|
81
|
+
return parseSubmission(value, key);
|
|
82
|
+
}).filter(
|
|
83
|
+
(submission) => submission.formId === formId && (formVersion === void 0 || submission.formVersion === formVersion)
|
|
84
|
+
).sort((left, right) => left.submittedAt.localeCompare(right.submittedAt)).map(cloneJson);
|
|
85
|
+
},
|
|
86
|
+
async deleteSubmission(submissionId) {
|
|
87
|
+
storage.removeItem(submissionKey(submissionId));
|
|
88
|
+
},
|
|
89
|
+
async clearResponses(formId) {
|
|
90
|
+
const matchingKeys = prefixedKeys(submissionPrefix).filter((key) => {
|
|
91
|
+
const value = storage.getItem(key);
|
|
92
|
+
if (value === null) throw new Error(`Stored submission at "${key}" disappeared during reading.`);
|
|
93
|
+
return parseSubmission(value, key).formId === formId;
|
|
94
|
+
});
|
|
95
|
+
for (const key of matchingKeys) storage.removeItem(key);
|
|
96
|
+
},
|
|
97
|
+
async clear() {
|
|
98
|
+
for (const key of prefixedKeys(storagePrefix)) storage.removeItem(key);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
export {
|
|
103
|
+
createLocalStorageAdapter
|
|
104
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@form-engine-ts/storage-localstorage",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"type": "module",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"README.md",
|
|
12
|
+
"LICENSE"
|
|
13
|
+
],
|
|
14
|
+
"main": "./dist/index.cjs",
|
|
15
|
+
"module": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js",
|
|
21
|
+
"require": "./dist/index.cjs"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/nitta-a/form-engine-ts.git",
|
|
28
|
+
"directory": "packages/storage-localstorage"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/nitta-a/form-engine-ts/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/nitta-a/form-engine-ts#readme",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"form",
|
|
36
|
+
"survey",
|
|
37
|
+
"storage",
|
|
38
|
+
"localstorage",
|
|
39
|
+
"typescript"
|
|
40
|
+
],
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@form-engine-ts/core": "1.0.0"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --clean --external @form-engine-ts/core",
|
|
46
|
+
"check": "biome check . && tsc --noEmit",
|
|
47
|
+
"test": "vitest run --globals",
|
|
48
|
+
"typecheck": "tsc --noEmit"
|
|
49
|
+
}
|
|
50
|
+
}
|