@vllnt/convex-helpers 0.1.0-canary.df58580
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 +92 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +53 -0
- package/dist/index.js.map +1 -0
- package/package.json +75 -0
- package/src/index.test.ts +164 -0
- package/src/index.ts +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 bntvllnt
|
|
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,92 @@
|
|
|
1
|
+
<!-- Badges -->
|
|
2
|
+
[](https://www.npmjs.com/package/@vllnt/convex-helpers)
|
|
3
|
+
[](https://github.com/vllnt/convex-helpers/actions/workflows/ci.yml)
|
|
4
|
+
[](./LICENSE)
|
|
5
|
+
|
|
6
|
+
# @vllnt/convex-helpers
|
|
7
|
+
|
|
8
|
+
Typed host-side helpers for Convex backends — builders, errors, validators, relationships,
|
|
9
|
+
pagination, HTTP, env, and observability.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { asyncMap, pruneNull, nullThrows } from "@vllnt/convex-helpers";
|
|
13
|
+
|
|
14
|
+
const docs = pruneNull(await asyncMap(ids, (id) => ctx.db.get(id)));
|
|
15
|
+
const doc = nullThrows(await ctx.db.get(id), `Document ${id} not found`);
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
A helpers library (not a sandboxed Convex component): pure functions and host-`ctx` glue that run with
|
|
19
|
+
the host's `ctx` — no own tables, no `app.use()` mounting. Stateful concerns (rate limiting,
|
|
20
|
+
idempotency, flags) belong in `@convex-dev/*` or `@vllnt/convex-*` components.
|
|
21
|
+
|
|
22
|
+
See [ROADMAP.md](./ROADMAP.md) for the planned module surface.
|
|
23
|
+
|
|
24
|
+
## Features
|
|
25
|
+
|
|
26
|
+
- `asyncMap` — parallel async map that preserves order
|
|
27
|
+
- `pruneNull` — filter null/undefined from arrays
|
|
28
|
+
- `nullThrows` — non-null assertion with typed `NullDocumentError`
|
|
29
|
+
- [planned] `./builders` — `customQuery`/`customMutation`/`customAction`/`customCtx`
|
|
30
|
+
- [planned] `./errors` — typed `AppError` + HTTP-status map
|
|
31
|
+
- [planned] `./auth` — provider-agnostic `requireIdentity`/`getCurrentSubject`
|
|
32
|
+
- [planned] `./env` — `defineEnv(zodSchema)` cold-start validation
|
|
33
|
+
- [planned] `./tracing` — span emit + `traceparent` propagation via `@vllnt/logger`
|
|
34
|
+
- [planned] `./testing` — fixture factories + `withIdentity`
|
|
35
|
+
- [planned] `./relationships`, `./validators`, `./pagination`, `./rls`, `./triggers`, `./http`
|
|
36
|
+
- [planned] `./react` — optional tree-shakeable front-end hooks layer
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pnpm add @vllnt/convex-helpers
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
`convex` is a peer dependency:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pnpm add convex
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { asyncMap, pruneNull, nullThrows } from "@vllnt/convex-helpers";
|
|
54
|
+
|
|
55
|
+
// Parallel fetch with null filtering
|
|
56
|
+
const docs = pruneNull(await asyncMap(ids, (id) => ctx.db.get(id)));
|
|
57
|
+
|
|
58
|
+
// Non-null assertion
|
|
59
|
+
const doc = nullThrows(await ctx.db.get(id), `Document ${id} not found`);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## API Reference
|
|
63
|
+
|
|
64
|
+
See [docs/API.md](./docs/API.md) for full API reference with signatures and examples.
|
|
65
|
+
|
|
66
|
+
## Testing
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
pnpm test
|
|
70
|
+
pnpm test:coverage # must reach 100%
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Tests use [vitest](https://vitest.dev) with the node environment. 100% coverage is enforced via
|
|
74
|
+
`vitest.config.mts` thresholds.
|
|
75
|
+
|
|
76
|
+
## Contributing
|
|
77
|
+
|
|
78
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md) for development setup, code style, and PR guidelines.
|
|
79
|
+
|
|
80
|
+
## Author
|
|
81
|
+
|
|
82
|
+
Built by [bntvllnt](https://github.com/bntvllnt) · [bntvllnt.com](https://bntvllnt.com) · [X
|
|
83
|
+
@bntvllnt](https://x.com/bntvllnt)
|
|
84
|
+
|
|
85
|
+
Part of the [@vllnt](https://github.com/vllnt) Convex component fleet —
|
|
86
|
+
[vllnt.com](https://vllnt.com)
|
|
87
|
+
|
|
88
|
+
If this is useful, [sponsor the work](https://github.com/sponsors/bntvllnt).
|
|
89
|
+
|
|
90
|
+
## License
|
|
91
|
+
|
|
92
|
+
[MIT](./LICENSE)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @vllnt/convex-helpers
|
|
3
|
+
* Pure host-side helpers for Convex backends.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Applies an async function to every element of an array, returning results in order.
|
|
7
|
+
*
|
|
8
|
+
* @param list - Input array
|
|
9
|
+
* @param fn - Async mapper function
|
|
10
|
+
* @returns Promise resolving to mapped array in original order
|
|
11
|
+
* @example
|
|
12
|
+
* const docs = await asyncMap(ids, (id) => ctx.db.get(id));
|
|
13
|
+
*/
|
|
14
|
+
export declare function asyncMap<T, TResult>(list: T[], fn: (item: T, index: number) => Promise<TResult>): Promise<TResult[]>;
|
|
15
|
+
/**
|
|
16
|
+
* Filters null and undefined values from an array.
|
|
17
|
+
*
|
|
18
|
+
* @param list - Input array potentially containing null/undefined
|
|
19
|
+
* @returns Array with all null and undefined values removed
|
|
20
|
+
* @example
|
|
21
|
+
* const docs = pruneNull(await asyncMap(ids, (id) => ctx.db.get(id)));
|
|
22
|
+
*/
|
|
23
|
+
export declare function pruneNull<T>(list: (null | T | undefined)[]): T[];
|
|
24
|
+
/**
|
|
25
|
+
* Error thrown when a required document is null or undefined.
|
|
26
|
+
*/
|
|
27
|
+
export declare class NullDocumentError extends Error {
|
|
28
|
+
constructor(message?: string);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Asserts a value is non-null and non-undefined, throwing NullDocumentError when absent.
|
|
32
|
+
*
|
|
33
|
+
* @param value - Value to check
|
|
34
|
+
* @param message - Optional custom error message
|
|
35
|
+
* @returns The value, narrowed to exclude null and undefined
|
|
36
|
+
* @throws {NullDocumentError} When value is null or undefined
|
|
37
|
+
* @example
|
|
38
|
+
* const doc = nullThrows(await ctx.db.get(id), `Document ${id} not found`);
|
|
39
|
+
*/
|
|
40
|
+
export declare function nullThrows<T>(value: null | T | undefined, message?: string): T;
|
|
41
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;GAQG;AACH,wBAAsB,QAAQ,CAAC,CAAC,EAAE,OAAO,EACvC,IAAI,EAAE,CAAC,EAAE,EACT,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,GAC/C,OAAO,CAAC,OAAO,EAAE,CAAC,CAEpB;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,IAAI,GAAG,CAAC,GAAG,SAAS,CAAC,EAAE,GAAG,CAAC,EAAE,CAEhE;AAED;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,KAAK;gBAC9B,OAAO,SAAiC;CAIrD;AAED;;;;;;;;;GASG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAC1B,KAAK,EAAE,IAAI,GAAG,CAAC,GAAG,SAAS,EAC3B,OAAO,CAAC,EAAE,MAAM,GACf,CAAC,CAKH"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @vllnt/convex-helpers
|
|
3
|
+
* Pure host-side helpers for Convex backends.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Applies an async function to every element of an array, returning results in order.
|
|
7
|
+
*
|
|
8
|
+
* @param list - Input array
|
|
9
|
+
* @param fn - Async mapper function
|
|
10
|
+
* @returns Promise resolving to mapped array in original order
|
|
11
|
+
* @example
|
|
12
|
+
* const docs = await asyncMap(ids, (id) => ctx.db.get(id));
|
|
13
|
+
*/
|
|
14
|
+
export async function asyncMap(list, fn) {
|
|
15
|
+
return Promise.all(list.map(fn));
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Filters null and undefined values from an array.
|
|
19
|
+
*
|
|
20
|
+
* @param list - Input array potentially containing null/undefined
|
|
21
|
+
* @returns Array with all null and undefined values removed
|
|
22
|
+
* @example
|
|
23
|
+
* const docs = pruneNull(await asyncMap(ids, (id) => ctx.db.get(id)));
|
|
24
|
+
*/
|
|
25
|
+
export function pruneNull(list) {
|
|
26
|
+
return list.filter((item) => item !== null && item !== undefined);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Error thrown when a required document is null or undefined.
|
|
30
|
+
*/
|
|
31
|
+
export class NullDocumentError extends Error {
|
|
32
|
+
constructor(message = "Expected a non-null document") {
|
|
33
|
+
super(message);
|
|
34
|
+
this.name = "NullDocumentError";
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Asserts a value is non-null and non-undefined, throwing NullDocumentError when absent.
|
|
39
|
+
*
|
|
40
|
+
* @param value - Value to check
|
|
41
|
+
* @param message - Optional custom error message
|
|
42
|
+
* @returns The value, narrowed to exclude null and undefined
|
|
43
|
+
* @throws {NullDocumentError} When value is null or undefined
|
|
44
|
+
* @example
|
|
45
|
+
* const doc = nullThrows(await ctx.db.get(id), `Document ${id} not found`);
|
|
46
|
+
*/
|
|
47
|
+
export function nullThrows(value, message) {
|
|
48
|
+
if (value === null || value === undefined) {
|
|
49
|
+
throw new NullDocumentError(message);
|
|
50
|
+
}
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC5B,IAAS,EACT,EAAgD;IAEhD,OAAO,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC;AACnC,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,SAAS,CAAI,IAA8B;IACzD,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,EAAa,EAAE,CAAC,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,SAAS,CAAC,CAAC;AAC/E,CAAC;AAED;;GAEG;AACH,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAC1C,YAAY,OAAO,GAAG,8BAA8B;QAClD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IAClC,CAAC;CACF;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,UAAU,CACxB,KAA2B,EAC3B,OAAgB;IAEhB,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QAC1C,MAAM,IAAI,iBAAiB,CAAC,OAAO,CAAC,CAAC;IACvC,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vllnt/convex-helpers",
|
|
3
|
+
"version": "0.1.0-canary.df58580",
|
|
4
|
+
"description": "Typed host-side helpers for Convex backends — builders, errors, validators, relationships, pagination, HTTP, env, and observability.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"packageManager": "pnpm@9.15.4",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"src"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
"./package.json": "./package.json",
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"module": "./dist/index.js",
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc --project ./tsconfig.build.json",
|
|
24
|
+
"typecheck": "tsc --noEmit",
|
|
25
|
+
"typecheck:ci": "tsc --noEmit --project tsconfig.ci.json",
|
|
26
|
+
"lint": "eslint .",
|
|
27
|
+
"test": "vitest run --passWithNoTests",
|
|
28
|
+
"test:watch": "vitest --typecheck --clearScreen false",
|
|
29
|
+
"test:coverage": "vitest run --coverage --coverage.reporter=text",
|
|
30
|
+
"generate:llms": "node scripts/generate-llms.mjs",
|
|
31
|
+
"preversion": "pnpm install --frozen-lockfile && pnpm build && pnpm test && pnpm typecheck && pnpm generate:llms"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"convex": "^1.36.1"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@vllnt/eslint-config": "^1.0.0",
|
|
38
|
+
"@vllnt/typescript": "^1.0.0",
|
|
39
|
+
"convex": "^1.36.1",
|
|
40
|
+
"eslint": "^9.0.0",
|
|
41
|
+
"prettier": "^3.4.0",
|
|
42
|
+
"typescript": "^5.7.0",
|
|
43
|
+
"vitest": "^3.0.0",
|
|
44
|
+
"@vitest/coverage-v8": "^3.0.0"
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=18"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
},
|
|
52
|
+
"homepage": "https://github.com/vllnt/convex-helpers#readme",
|
|
53
|
+
"bugs": {
|
|
54
|
+
"url": "https://github.com/vllnt/convex-helpers/issues"
|
|
55
|
+
},
|
|
56
|
+
"repository": {
|
|
57
|
+
"type": "git",
|
|
58
|
+
"url": "https://github.com/vllnt/convex-helpers"
|
|
59
|
+
},
|
|
60
|
+
"author": {
|
|
61
|
+
"name": "bntvllnt",
|
|
62
|
+
"url": "https://bntvllnt.com"
|
|
63
|
+
},
|
|
64
|
+
"funding": {
|
|
65
|
+
"type": "github",
|
|
66
|
+
"url": "https://github.com/sponsors/bntvllnt"
|
|
67
|
+
},
|
|
68
|
+
"keywords": [
|
|
69
|
+
"convex",
|
|
70
|
+
"convex-helpers",
|
|
71
|
+
"typescript",
|
|
72
|
+
"backend",
|
|
73
|
+
"utils"
|
|
74
|
+
]
|
|
75
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { asyncMap, NullDocumentError, nullThrows, pruneNull } from "./index.js";
|
|
4
|
+
|
|
5
|
+
describe("asyncMap", () => {
|
|
6
|
+
it("returns empty array for empty input", async () => {
|
|
7
|
+
const result = await asyncMap([], (x: number) => Promise.resolve(x * 2));
|
|
8
|
+
expect(result).toEqual([]);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("maps a single element", async () => {
|
|
12
|
+
const result = await asyncMap([1], (x) => Promise.resolve(x * 2));
|
|
13
|
+
expect(result).toEqual([2]);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("maps multiple elements in order", async () => {
|
|
17
|
+
const result = await asyncMap([1, 2, 3], (x) => Promise.resolve(x * 2));
|
|
18
|
+
expect(result).toEqual([2, 4, 6]);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("passes index to mapper", async () => {
|
|
22
|
+
const result = await asyncMap(
|
|
23
|
+
["a", "b", "c"],
|
|
24
|
+
async (item, index) => `${String(index)}:${item}`,
|
|
25
|
+
);
|
|
26
|
+
expect(result).toEqual(["0:a", "1:b", "2:c"]);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("resolves all promises and returns results in order", async () => {
|
|
30
|
+
const result = await asyncMap([10, 20, 30], (x) => Promise.resolve(x * 3));
|
|
31
|
+
expect(result).toEqual([30, 60, 90]);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe("pruneNull", () => {
|
|
36
|
+
it("returns empty array for empty input", () => {
|
|
37
|
+
expect(pruneNull([])).toEqual([]);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("removes all nulls", () => {
|
|
41
|
+
const nullValue: null = null;
|
|
42
|
+
expect(pruneNull([nullValue, nullValue])).toEqual([]);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("removes all undefineds", () => {
|
|
46
|
+
expect(pruneNull([undefined, undefined])).toEqual([]);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("removes mixed null and undefined", () => {
|
|
50
|
+
const nullValue: null = null;
|
|
51
|
+
expect(pruneNull([nullValue, undefined, nullValue])).toEqual([]);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("keeps all non-null values", () => {
|
|
55
|
+
expect(pruneNull([1, 2, 3])).toEqual([1, 2, 3]);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("filters mixed array of values and nulls", () => {
|
|
59
|
+
const nullValue: null = null;
|
|
60
|
+
expect(pruneNull([1, nullValue, 2, undefined, 3])).toEqual([1, 2, 3]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("works with strings", () => {
|
|
64
|
+
const nullValue: null = null;
|
|
65
|
+
expect(pruneNull(["a", nullValue, "b"])).toEqual(["a", "b"]);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("works with objects", () => {
|
|
69
|
+
const object = { id: 1 };
|
|
70
|
+
const nullValue: null = null;
|
|
71
|
+
expect(pruneNull([object, nullValue])).toEqual([object]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("preserves falsy non-null values (0, false, empty string)", () => {
|
|
75
|
+
const nullValue: null = null;
|
|
76
|
+
expect(pruneNull([0, false, "", nullValue, undefined])).toEqual([
|
|
77
|
+
0,
|
|
78
|
+
false,
|
|
79
|
+
"",
|
|
80
|
+
]);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe("NullDocumentError", () => {
|
|
85
|
+
it("is an instance of Error", () => {
|
|
86
|
+
const error = new NullDocumentError();
|
|
87
|
+
expect(error).toBeInstanceOf(Error);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("has name NullDocumentError", () => {
|
|
91
|
+
const error = new NullDocumentError();
|
|
92
|
+
expect(error.name).toBe("NullDocumentError");
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("uses default message when none provided", () => {
|
|
96
|
+
const error = new NullDocumentError();
|
|
97
|
+
expect(error.message).toBe("Expected a non-null document");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("uses custom message when provided", () => {
|
|
101
|
+
const error = new NullDocumentError("custom message");
|
|
102
|
+
expect(error.message).toBe("custom message");
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe("nullThrows", () => {
|
|
107
|
+
it("returns the value when non-null", () => {
|
|
108
|
+
expect(nullThrows(42)).toBe(42);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("returns the value when truthy string", () => {
|
|
112
|
+
expect(nullThrows("hello")).toBe("hello");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("returns 0 (falsy but non-null)", () => {
|
|
116
|
+
expect(nullThrows(0)).toBe(0);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("returns false (falsy but non-null)", () => {
|
|
120
|
+
expect(nullThrows(false)).toBe(false);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("returns empty string (falsy but non-null)", () => {
|
|
124
|
+
expect(nullThrows("")).toBe("");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("returns objects", () => {
|
|
128
|
+
const object = { id: 1 };
|
|
129
|
+
expect(nullThrows(object)).toBe(object);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("throws NullDocumentError when value is null", () => {
|
|
133
|
+
const nullValue: null = null;
|
|
134
|
+
expect(() => {
|
|
135
|
+
nullThrows(nullValue);
|
|
136
|
+
}).toThrow(NullDocumentError);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("throws NullDocumentError when value is undefined", () => {
|
|
140
|
+
expect(() => {
|
|
141
|
+
nullThrows(undefined);
|
|
142
|
+
}).toThrow(NullDocumentError);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("throws with default message when no message supplied", () => {
|
|
146
|
+
const nullValue: null = null;
|
|
147
|
+
expect(() => {
|
|
148
|
+
nullThrows(nullValue);
|
|
149
|
+
}).toThrow("Expected a non-null document");
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("throws with custom message when supplied", () => {
|
|
153
|
+
const nullValue: null = null;
|
|
154
|
+
expect(() => {
|
|
155
|
+
nullThrows(nullValue, "doc 123 not found");
|
|
156
|
+
}).toThrow("doc 123 not found");
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("throws with custom message for undefined", () => {
|
|
160
|
+
expect(() => {
|
|
161
|
+
nullThrows(undefined, "missing doc");
|
|
162
|
+
}).toThrow("missing doc");
|
|
163
|
+
});
|
|
164
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @vllnt/convex-helpers
|
|
3
|
+
* Pure host-side helpers for Convex backends.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Applies an async function to every element of an array, returning results in order.
|
|
8
|
+
*
|
|
9
|
+
* @param list - Input array
|
|
10
|
+
* @param fn - Async mapper function
|
|
11
|
+
* @returns Promise resolving to mapped array in original order
|
|
12
|
+
* @example
|
|
13
|
+
* const docs = await asyncMap(ids, (id) => ctx.db.get(id));
|
|
14
|
+
*/
|
|
15
|
+
export async function asyncMap<T, TResult>(
|
|
16
|
+
list: T[],
|
|
17
|
+
fn: (item: T, index: number) => Promise<TResult>,
|
|
18
|
+
): Promise<TResult[]> {
|
|
19
|
+
return Promise.all(list.map(fn));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Filters null and undefined values from an array.
|
|
24
|
+
*
|
|
25
|
+
* @param list - Input array potentially containing null/undefined
|
|
26
|
+
* @returns Array with all null and undefined values removed
|
|
27
|
+
* @example
|
|
28
|
+
* const docs = pruneNull(await asyncMap(ids, (id) => ctx.db.get(id)));
|
|
29
|
+
*/
|
|
30
|
+
export function pruneNull<T>(list: (null | T | undefined)[]): T[] {
|
|
31
|
+
return list.filter((item): item is T => item !== null && item !== undefined);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Error thrown when a required document is null or undefined.
|
|
36
|
+
*/
|
|
37
|
+
export class NullDocumentError extends Error {
|
|
38
|
+
constructor(message = "Expected a non-null document") {
|
|
39
|
+
super(message);
|
|
40
|
+
this.name = "NullDocumentError";
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Asserts a value is non-null and non-undefined, throwing NullDocumentError when absent.
|
|
46
|
+
*
|
|
47
|
+
* @param value - Value to check
|
|
48
|
+
* @param message - Optional custom error message
|
|
49
|
+
* @returns The value, narrowed to exclude null and undefined
|
|
50
|
+
* @throws {NullDocumentError} When value is null or undefined
|
|
51
|
+
* @example
|
|
52
|
+
* const doc = nullThrows(await ctx.db.get(id), `Document ${id} not found`);
|
|
53
|
+
*/
|
|
54
|
+
export function nullThrows<T>(
|
|
55
|
+
value: null | T | undefined,
|
|
56
|
+
message?: string,
|
|
57
|
+
): T {
|
|
58
|
+
if (value === null || value === undefined) {
|
|
59
|
+
throw new NullDocumentError(message);
|
|
60
|
+
}
|
|
61
|
+
return value;
|
|
62
|
+
}
|