@dxos/effect 0.8.4-main.67995b8 → 0.8.4-main.69d29f4
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/dist/lib/browser/chunk-CGS2ULMK.mjs +11 -0
- package/dist/lib/browser/chunk-CGS2ULMK.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +418 -230
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing.mjs +38 -0
- package/dist/lib/browser/testing.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +418 -230
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing.mjs +38 -0
- package/dist/lib/node-esm/testing.mjs.map +7 -0
- package/dist/types/src/RuntimeProvider.d.ts +21 -0
- package/dist/types/src/RuntimeProvider.d.ts.map +1 -0
- package/dist/types/src/ast.d.ts +36 -22
- package/dist/types/src/ast.d.ts.map +1 -1
- package/dist/types/src/atom-kvs.d.ts +19 -0
- package/dist/types/src/atom-kvs.d.ts.map +1 -0
- package/dist/types/src/context.d.ts +2 -1
- package/dist/types/src/context.d.ts.map +1 -1
- package/dist/types/src/dynamic-runtime.d.ts +56 -0
- package/dist/types/src/dynamic-runtime.d.ts.map +1 -0
- package/dist/types/src/dynamic-runtime.test.d.ts +2 -0
- package/dist/types/src/dynamic-runtime.test.d.ts.map +1 -0
- package/dist/types/src/errors.d.ts +35 -1
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +5 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/interrupt.test.d.ts +2 -0
- package/dist/types/src/interrupt.test.d.ts.map +1 -0
- package/dist/types/src/{jsonPath.d.ts → json-path.d.ts} +12 -4
- package/dist/types/src/json-path.d.ts.map +1 -0
- package/dist/types/src/json-path.test.d.ts +2 -0
- package/dist/types/src/json-path.test.d.ts.map +1 -0
- package/dist/types/src/layers.test.d.ts +2 -0
- package/dist/types/src/layers.test.d.ts.map +1 -0
- package/dist/types/src/otel.d.ts +17 -0
- package/dist/types/src/otel.d.ts.map +1 -0
- package/dist/types/src/otel.test.d.ts +2 -0
- package/dist/types/src/otel.test.d.ts.map +1 -0
- package/dist/types/src/resource.d.ts +6 -2
- package/dist/types/src/resource.d.ts.map +1 -1
- package/dist/types/src/testing.d.ts +33 -1
- package/dist/types/src/testing.d.ts.map +1 -1
- package/dist/types/src/url.d.ts +3 -1
- package/dist/types/src/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +35 -11
- package/src/RuntimeProvider.ts +35 -0
- package/src/ast.test.ts +14 -11
- package/src/ast.ts +119 -92
- package/src/atom-kvs.ts +35 -0
- package/src/context.ts +2 -1
- package/src/dynamic-runtime.test.ts +465 -0
- package/src/dynamic-runtime.ts +195 -0
- package/src/errors.test.ts +1 -1
- package/src/errors.ts +90 -22
- package/src/index.ts +5 -3
- package/src/interrupt.test.ts +35 -0
- package/src/{jsonPath.test.ts → json-path.test.ts} +47 -8
- package/src/{jsonPath.ts → json-path.ts} +29 -4
- package/src/layers.test.ts +112 -0
- package/src/otel.test.ts +126 -0
- package/src/otel.ts +45 -0
- package/src/resource.test.ts +5 -4
- package/src/resource.ts +10 -5
- package/src/sanity.test.ts +30 -15
- package/src/testing.ts +53 -1
- package/src/url.test.ts +1 -1
- package/src/url.ts +5 -2
- package/dist/types/src/jsonPath.d.ts.map +0 -1
- package/dist/types/src/jsonPath.test.d.ts +0 -2
- package/dist/types/src/jsonPath.test.d.ts.map +0 -1
package/package.json
CHANGED
|
@@ -1,41 +1,65 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/effect",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.69d29f4",
|
|
4
4
|
"description": "Effect utils.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"author": "info@dxos.org",
|
|
9
13
|
"type": "module",
|
|
10
14
|
"exports": {
|
|
11
15
|
".": {
|
|
16
|
+
"source": "./src/index.ts",
|
|
12
17
|
"types": "./dist/types/src/index.d.ts",
|
|
13
18
|
"browser": "./dist/lib/browser/index.mjs",
|
|
14
|
-
"node": "./dist/lib/node-esm/index.mjs"
|
|
15
|
-
|
|
19
|
+
"node": "./dist/lib/node-esm/index.mjs"
|
|
20
|
+
},
|
|
21
|
+
"./testing": {
|
|
22
|
+
"source": "./src/testing.ts",
|
|
23
|
+
"types": "./dist/types/src/testing.d.ts",
|
|
24
|
+
"browser": "./dist/lib/browser/testing.mjs",
|
|
25
|
+
"node": "./dist/lib/node-esm/testing.mjs"
|
|
16
26
|
}
|
|
17
27
|
},
|
|
18
28
|
"types": "dist/types/src/index.d.ts",
|
|
19
29
|
"typesVersions": {
|
|
20
|
-
"*": {
|
|
30
|
+
"*": {
|
|
31
|
+
"testing": [
|
|
32
|
+
"./dist/types/src/testing.d.ts"
|
|
33
|
+
]
|
|
34
|
+
}
|
|
21
35
|
},
|
|
22
36
|
"files": [
|
|
23
37
|
"dist",
|
|
24
38
|
"src"
|
|
25
39
|
],
|
|
26
40
|
"dependencies": {
|
|
41
|
+
"@effect-atom/atom-react": "^0.4.6",
|
|
42
|
+
"@effect/opentelemetry": "^0.59.2",
|
|
43
|
+
"@effect/platform-browser": "0.73.0",
|
|
44
|
+
"@opentelemetry/api": "^1.9.0",
|
|
27
45
|
"jsonpath-plus": "10.2.0",
|
|
28
|
-
"@dxos/context": "0.8.4-main.
|
|
29
|
-
"@dxos/node-std": "0.8.4-main.
|
|
30
|
-
"@dxos/invariant": "0.8.4-main.
|
|
31
|
-
"@dxos/util": "0.8.4-main.
|
|
46
|
+
"@dxos/context": "0.8.4-main.69d29f4",
|
|
47
|
+
"@dxos/node-std": "0.8.4-main.69d29f4",
|
|
48
|
+
"@dxos/invariant": "0.8.4-main.69d29f4",
|
|
49
|
+
"@dxos/util": "0.8.4-main.69d29f4"
|
|
32
50
|
},
|
|
33
51
|
"devDependencies": {
|
|
34
|
-
"
|
|
35
|
-
"@
|
|
52
|
+
"@opentelemetry/api-logs": "^0.203.0",
|
|
53
|
+
"@opentelemetry/resources": "^2.1.0",
|
|
54
|
+
"@opentelemetry/sdk-logs": "^0.203.0",
|
|
55
|
+
"@opentelemetry/sdk-node": "^0.203.0",
|
|
56
|
+
"@opentelemetry/sdk-trace-node": "^2.1.0",
|
|
57
|
+
"@opentelemetry/semantic-conventions": "^1.37.0",
|
|
58
|
+
"effect": "3.19.11",
|
|
59
|
+
"@dxos/log": "0.8.4-main.69d29f4"
|
|
36
60
|
},
|
|
37
61
|
"peerDependencies": {
|
|
38
|
-
"effect": "
|
|
62
|
+
"effect": "3.19.11"
|
|
39
63
|
},
|
|
40
64
|
"publishConfig": {
|
|
41
65
|
"access": "public"
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
6
|
+
import * as Runtime from 'effect/Runtime';
|
|
7
|
+
|
|
8
|
+
import { runAndForwardErrors, unwrapExit } from './errors';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Provides effect runtime with services to run effects.
|
|
12
|
+
*/
|
|
13
|
+
export type RuntimeProvider<R> = Effect.Effect<Runtime.Runtime<R>>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @returns Runtime provider from the current context.
|
|
17
|
+
*/
|
|
18
|
+
export const currentRuntime = <R = never>() => Effect.runtime<R>().pipe(Effect.map(Effect.succeed));
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Run effect, within runitme, clean errors and fix stack-traces.
|
|
22
|
+
*/
|
|
23
|
+
export const runPromise =
|
|
24
|
+
<R>(provider: RuntimeProvider<R>) =>
|
|
25
|
+
async <A>(effect: Effect.Effect<A, any, R>): Promise<A> => {
|
|
26
|
+
const runtime = await runAndForwardErrors(provider);
|
|
27
|
+
return unwrapExit(await effect.pipe(Runtime.runPromiseExit(runtime)));
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Provide services from runtime provider to effect.
|
|
32
|
+
*/
|
|
33
|
+
export const provide: {
|
|
34
|
+
<R2>(runtime: RuntimeProvider<R2>): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, R2>>;
|
|
35
|
+
} = (runtimeProvider) => (effect) => Effect.flatMap(runtimeProvider, (runtime) => Effect.provide(effect, runtime));
|
package/src/ast.test.ts
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
6
7
|
import { describe, test } from 'vitest';
|
|
7
8
|
|
|
8
9
|
import { invariant } from '@dxos/invariant';
|
|
@@ -12,15 +13,14 @@ import {
|
|
|
12
13
|
findNode,
|
|
13
14
|
findProperty,
|
|
14
15
|
getAnnotation,
|
|
15
|
-
getDiscriminatingProps,
|
|
16
16
|
getDiscriminatedType,
|
|
17
|
-
|
|
17
|
+
getDiscriminatingProps,
|
|
18
|
+
isArrayType,
|
|
19
|
+
isDiscriminatedUnion,
|
|
18
20
|
isOption,
|
|
19
|
-
isSimpleType,
|
|
20
21
|
visit,
|
|
21
|
-
isArrayType,
|
|
22
22
|
} from './ast';
|
|
23
|
-
import { type JsonPath, type JsonProp } from './
|
|
23
|
+
import { type JsonPath, type JsonProp } from './json-path';
|
|
24
24
|
|
|
25
25
|
const ZipCode = Schema.String.pipe(
|
|
26
26
|
Schema.pattern(/^\d{5}$/, {
|
|
@@ -64,10 +64,9 @@ describe('AST', () => {
|
|
|
64
64
|
|
|
65
65
|
const prop = findProperty(TestSchema, 'name' as JsonProp);
|
|
66
66
|
invariant(prop);
|
|
67
|
-
const node = findNode(prop,
|
|
67
|
+
const node = findNode(prop, (node) => node._tag === 'StringKeyword');
|
|
68
68
|
invariant(node);
|
|
69
|
-
|
|
70
|
-
expect(type).to.eq('string');
|
|
69
|
+
expect(node._tag).to.eq('StringKeyword');
|
|
71
70
|
});
|
|
72
71
|
|
|
73
72
|
test('findProperty', ({ expect }) => {
|
|
@@ -136,7 +135,11 @@ describe('AST', () => {
|
|
|
136
135
|
});
|
|
137
136
|
|
|
138
137
|
const props: string[] = [];
|
|
139
|
-
visit(
|
|
138
|
+
visit(
|
|
139
|
+
TestSchema.ast,
|
|
140
|
+
(_, path) => props.push(path.join('.')),
|
|
141
|
+
(node, path, depth) => depth < 3,
|
|
142
|
+
);
|
|
140
143
|
});
|
|
141
144
|
|
|
142
145
|
test('discriminated unions', ({ expect }) => {
|
|
@@ -151,7 +154,7 @@ describe('AST', () => {
|
|
|
151
154
|
expect(isOption(TestUnionSchema.ast)).to.be.false;
|
|
152
155
|
expect(getDiscriminatingProps(TestUnionSchema.ast)).to.deep.eq(['kind']);
|
|
153
156
|
|
|
154
|
-
const node = findNode(TestUnionSchema.ast,
|
|
157
|
+
const node = findNode(TestUnionSchema.ast, isDiscriminatedUnion);
|
|
155
158
|
expect(node).to.eq(TestUnionSchema.ast);
|
|
156
159
|
}
|
|
157
160
|
|
package/src/ast.ts
CHANGED
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
import
|
|
5
|
+
import * as Function from 'effect/Function';
|
|
6
|
+
import * as Option from 'effect/Option';
|
|
7
|
+
import * as Schema from 'effect/Schema';
|
|
8
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
7
9
|
|
|
8
10
|
import { invariant } from '@dxos/invariant';
|
|
9
11
|
import { isNonNullable } from '@dxos/util';
|
|
10
12
|
|
|
11
|
-
import { type JsonPath, type JsonProp } from './
|
|
13
|
+
import { type JsonPath, type JsonProp } from './json-path';
|
|
12
14
|
|
|
13
15
|
//
|
|
14
16
|
// Refs
|
|
@@ -17,67 +19,54 @@ import { type JsonPath, type JsonProp } from './jsonPath';
|
|
|
17
19
|
// https://effect-ts.github.io/effect/schema/SchemaAST.ts.html
|
|
18
20
|
//
|
|
19
21
|
|
|
20
|
-
export type SimpleType = 'object' | 'string' | 'number' | 'boolean' | 'enum' | 'literal';
|
|
21
|
-
|
|
22
22
|
/**
|
|
23
|
-
*
|
|
23
|
+
* Unwraps and collects refinement filters.
|
|
24
24
|
*/
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (SchemaAST.isStringKeyword(node)) {
|
|
36
|
-
return 'string';
|
|
37
|
-
}
|
|
38
|
-
if (SchemaAST.isNumberKeyword(node)) {
|
|
39
|
-
return 'number';
|
|
40
|
-
}
|
|
41
|
-
if (SchemaAST.isBooleanKeyword(node)) {
|
|
42
|
-
return 'boolean';
|
|
25
|
+
const reduceRefinements = (
|
|
26
|
+
type: SchemaAST.AST,
|
|
27
|
+
refinements: SchemaAST.Refinement['filter'][] = [],
|
|
28
|
+
): { type: SchemaAST.AST; refinements: SchemaAST.Refinement['filter'][] } => {
|
|
29
|
+
if (SchemaAST.isRefinement(type)) {
|
|
30
|
+
const annotations = type.annotations;
|
|
31
|
+
const filter = type.filter;
|
|
32
|
+
const nextType = { ...type.from, annotations: { ...type.annotations, ...annotations } } as SchemaAST.AST;
|
|
33
|
+
return reduceRefinements(nextType, [...refinements, filter]);
|
|
43
34
|
}
|
|
44
35
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
36
|
+
return { type, refinements };
|
|
37
|
+
};
|
|
48
38
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Get the base type of a property.
|
|
41
|
+
*
|
|
42
|
+
* Unwraps refinements and optional unions.
|
|
43
|
+
*/
|
|
44
|
+
export const getBaseType = (
|
|
45
|
+
prop: SchemaAST.PropertySignature | SchemaProperty,
|
|
46
|
+
): { type: SchemaAST.AST; refinements: SchemaAST.Refinement['filter'][] } => {
|
|
47
|
+
const encoded = SchemaAST.encodedBoundAST(prop.type);
|
|
48
|
+
// Extract property ast from optional union.
|
|
49
|
+
const unwrapped = prop.isOptional && encoded._tag === 'Union' ? encoded.types[0] : encoded;
|
|
50
|
+
return reduceRefinements(unwrapped);
|
|
52
51
|
};
|
|
53
52
|
|
|
54
|
-
export
|
|
53
|
+
export type SchemaProperty = Pick<SchemaAST.PropertySignature, 'name' | 'type' | 'isOptional' | 'isReadonly'> & {
|
|
54
|
+
/** Can be used to validate the property to the spec of the initial AST. */
|
|
55
|
+
refinements: SchemaAST.Refinement['filter'][];
|
|
56
|
+
};
|
|
55
57
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
case 'boolean': {
|
|
70
|
-
return false;
|
|
71
|
-
}
|
|
72
|
-
case 'object': {
|
|
73
|
-
return {};
|
|
74
|
-
}
|
|
75
|
-
default: {
|
|
76
|
-
throw new Error(`Unsupported type for default value: ${type}`);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
}
|
|
58
|
+
/**
|
|
59
|
+
* Get the property types of an AST.
|
|
60
|
+
*/
|
|
61
|
+
export const getProperties = (ast: SchemaAST.AST): SchemaProperty[] => {
|
|
62
|
+
const properties = SchemaAST.getPropertySignatures(ast);
|
|
63
|
+
return properties.map((prop) => ({
|
|
64
|
+
...getBaseType(prop),
|
|
65
|
+
name: prop.name,
|
|
66
|
+
isOptional: prop.isOptional,
|
|
67
|
+
isReadonly: prop.isReadonly,
|
|
68
|
+
}));
|
|
69
|
+
};
|
|
81
70
|
|
|
82
71
|
//
|
|
83
72
|
// Branded types
|
|
@@ -101,23 +90,14 @@ export type TestFn = (node: SchemaAST.AST, path: Path, depth: number) => VisitRe
|
|
|
101
90
|
|
|
102
91
|
export type VisitorFn = (node: SchemaAST.AST, path: Path, depth: number) => void;
|
|
103
92
|
|
|
104
|
-
const defaultTest: TestFn = isSimpleType;
|
|
105
|
-
|
|
106
93
|
/**
|
|
107
94
|
* Visit leaf nodes.
|
|
108
95
|
* Refs:
|
|
109
96
|
* - https://github.com/syntax-tree/unist-util-visit?tab=readme-ov-file#visitor
|
|
110
97
|
* - https://github.com/syntax-tree/unist-util-is?tab=readme-ov-file#test
|
|
111
98
|
*/
|
|
112
|
-
export const visit: {
|
|
113
|
-
(node
|
|
114
|
-
(node: SchemaAST.AST, test: TestFn, visitor: VisitorFn): void;
|
|
115
|
-
} = (node: SchemaAST.AST, testOrVisitor: TestFn | VisitorFn, visitor?: VisitorFn): void => {
|
|
116
|
-
if (!visitor) {
|
|
117
|
-
visitNode(node, defaultTest, testOrVisitor);
|
|
118
|
-
} else {
|
|
119
|
-
visitNode(node, testOrVisitor as TestFn, visitor);
|
|
120
|
-
}
|
|
99
|
+
export const visit = (node: SchemaAST.AST, testOrVisitor: TestFn | VisitorFn, visitor: VisitorFn): void => {
|
|
100
|
+
visitNode(node, testOrVisitor as TestFn, visitor);
|
|
121
101
|
};
|
|
122
102
|
|
|
123
103
|
const visitNode = (
|
|
@@ -127,15 +107,15 @@ const visitNode = (
|
|
|
127
107
|
path: Path = [],
|
|
128
108
|
depth = 0,
|
|
129
109
|
): VisitResult | undefined => {
|
|
130
|
-
const
|
|
110
|
+
const $result = test?.(node, path, depth);
|
|
131
111
|
const result: VisitResult =
|
|
132
|
-
|
|
112
|
+
$result === undefined
|
|
133
113
|
? VisitResult.CONTINUE
|
|
134
|
-
: typeof
|
|
135
|
-
?
|
|
114
|
+
: typeof $result === 'boolean'
|
|
115
|
+
? $result
|
|
136
116
|
? VisitResult.CONTINUE
|
|
137
117
|
: VisitResult.SKIP
|
|
138
|
-
:
|
|
118
|
+
: $result;
|
|
139
119
|
|
|
140
120
|
if (result === VisitResult.EXIT) {
|
|
141
121
|
return result;
|
|
@@ -204,6 +184,12 @@ export const findNode = (node: SchemaAST.AST, test: (node: SchemaAST.AST) => boo
|
|
|
204
184
|
return child;
|
|
205
185
|
}
|
|
206
186
|
}
|
|
187
|
+
for (const prop of getIndexSignatures(node)) {
|
|
188
|
+
const child = findNode(prop.type, test);
|
|
189
|
+
if (child) {
|
|
190
|
+
return child;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
207
193
|
}
|
|
208
194
|
|
|
209
195
|
// Tuple.
|
|
@@ -218,12 +204,14 @@ export const findNode = (node: SchemaAST.AST, test: (node: SchemaAST.AST) => boo
|
|
|
218
204
|
|
|
219
205
|
// Branching union (e.g., optional, discriminated unions).
|
|
220
206
|
else if (SchemaAST.isUnion(node)) {
|
|
221
|
-
if (
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
207
|
+
if (isLiteralUnion(node)) {
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
for (const type of node.types) {
|
|
212
|
+
const child = findNode(type, test);
|
|
213
|
+
if (child) {
|
|
214
|
+
return child;
|
|
227
215
|
}
|
|
228
216
|
}
|
|
229
217
|
}
|
|
@@ -279,8 +267,8 @@ export const getAnnotation =
|
|
|
279
267
|
<T>(annotationId: symbol, noDefault = true) =>
|
|
280
268
|
(node: SchemaAST.AST): T | undefined => {
|
|
281
269
|
// Title fallback seems to be the identifier.
|
|
282
|
-
const id = pipe(SchemaAST.getIdentifierAnnotation(node), Option.getOrUndefined);
|
|
283
|
-
const value = pipe(SchemaAST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);
|
|
270
|
+
const id = Function.pipe(SchemaAST.getIdentifierAnnotation(node), Option.getOrUndefined);
|
|
271
|
+
const value = Function.pipe(SchemaAST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);
|
|
284
272
|
if (noDefault && (value === defaultAnnotations[node._tag]?.annotations[annotationId] || value === id)) {
|
|
285
273
|
return undefined;
|
|
286
274
|
}
|
|
@@ -326,10 +314,31 @@ export const isOption = (node: SchemaAST.AST): boolean => {
|
|
|
326
314
|
/**
|
|
327
315
|
* Determines if the node is a union of literal types.
|
|
328
316
|
*/
|
|
329
|
-
export const isLiteralUnion = (node: SchemaAST.AST):
|
|
317
|
+
export const isLiteralUnion = (node: SchemaAST.AST): node is SchemaAST.Union<SchemaAST.Literal> => {
|
|
330
318
|
return SchemaAST.isUnion(node) && node.types.every(SchemaAST.isLiteral);
|
|
331
319
|
};
|
|
332
320
|
|
|
321
|
+
/**
|
|
322
|
+
* Determines if the node is an array type.
|
|
323
|
+
*/
|
|
324
|
+
export const isArrayType = (node: SchemaAST.AST): node is SchemaAST.TupleType => {
|
|
325
|
+
return SchemaAST.isTupleType(node) && node.elements.length === 0 && node.rest.length === 1;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Get the type of the array elements.
|
|
330
|
+
*/
|
|
331
|
+
export const getArrayElementType = (node: SchemaAST.AST): SchemaAST.AST | undefined => {
|
|
332
|
+
return isArrayType(node) ? node.rest.at(0)?.type : undefined;
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Determines if the node is a tuple type.
|
|
337
|
+
*/
|
|
338
|
+
export const isTupleType = (node: SchemaAST.AST): boolean => {
|
|
339
|
+
return SchemaAST.isTupleType(node) && node.elements.length > 0;
|
|
340
|
+
};
|
|
341
|
+
|
|
333
342
|
/**
|
|
334
343
|
* Determines if the node is a discriminated union.
|
|
335
344
|
*/
|
|
@@ -409,6 +418,20 @@ export const getDiscriminatedType = (
|
|
|
409
418
|
return schema.ast;
|
|
410
419
|
};
|
|
411
420
|
|
|
421
|
+
/**
|
|
422
|
+
* Determines if the node is a nested object type.
|
|
423
|
+
*/
|
|
424
|
+
export const isNestedType = (node: SchemaAST.AST): boolean => {
|
|
425
|
+
return (
|
|
426
|
+
SchemaAST.isDeclaration(node) ||
|
|
427
|
+
SchemaAST.isObjectKeyword(node) ||
|
|
428
|
+
SchemaAST.isTypeLiteral(node) ||
|
|
429
|
+
// TODO(wittjosiah): Tuples are actually arrays.
|
|
430
|
+
isTupleType(node) ||
|
|
431
|
+
isDiscriminatedUnion(node)
|
|
432
|
+
);
|
|
433
|
+
};
|
|
434
|
+
|
|
412
435
|
/**
|
|
413
436
|
* Maps AST nodes.
|
|
414
437
|
* The user is responsible for recursively calling {@link mapAst} on the SchemaAST.
|
|
@@ -432,6 +455,7 @@ export const mapAst = (
|
|
|
432
455
|
),
|
|
433
456
|
),
|
|
434
457
|
ast.indexSignatures,
|
|
458
|
+
ast.annotations,
|
|
435
459
|
);
|
|
436
460
|
}
|
|
437
461
|
case 'Union': {
|
|
@@ -456,15 +480,18 @@ export const mapAst = (
|
|
|
456
480
|
}
|
|
457
481
|
};
|
|
458
482
|
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
483
|
+
const getIndexSignatures = (ast: SchemaAST.AST): Array<SchemaAST.IndexSignature> => {
|
|
484
|
+
const annotation = SchemaAST.getSurrogateAnnotation(ast);
|
|
485
|
+
if (Option.isSome(annotation)) {
|
|
486
|
+
return getIndexSignatures(annotation.value);
|
|
487
|
+
}
|
|
488
|
+
switch (ast._tag) {
|
|
489
|
+
case 'TypeLiteral':
|
|
490
|
+
return ast.indexSignatures.slice();
|
|
491
|
+
case 'Suspend':
|
|
492
|
+
return getIndexSignatures(ast.f());
|
|
493
|
+
case 'Refinement':
|
|
494
|
+
return getIndexSignatures(ast.from);
|
|
495
|
+
}
|
|
496
|
+
return [];
|
|
470
497
|
};
|
package/src/atom-kvs.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as BrowserKeyValueStore from '@effect/platform-browser/BrowserKeyValueStore';
|
|
6
|
+
import { Atom } from '@effect-atom/atom-react';
|
|
7
|
+
import type * as Schema from 'effect/Schema';
|
|
8
|
+
|
|
9
|
+
// TODO(wittjosiah): This is currently provided for convenience but maybe should be removed.
|
|
10
|
+
const defaultRuntime = Atom.runtime(BrowserKeyValueStore.layerLocalStorage);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Creates a KVS-backed atom for structured settings using Atom.kvs.
|
|
14
|
+
* The entire object is stored as a single localStorage key with JSON serialization.
|
|
15
|
+
*
|
|
16
|
+
* @param options.key - The localStorage key to store the value under.
|
|
17
|
+
* @param options.schema - Effect Schema for the value type.
|
|
18
|
+
* @param options.defaultValue - Function returning the default value.
|
|
19
|
+
* @param options.runtime - Optional custom Atom runtime (defaults to localStorage).
|
|
20
|
+
* @returns A writable atom that persists to localStorage.
|
|
21
|
+
*/
|
|
22
|
+
export const createKvsStore = <T extends Record<string, any>>(options: {
|
|
23
|
+
key: string;
|
|
24
|
+
schema: Schema.Schema<T>;
|
|
25
|
+
defaultValue: () => T;
|
|
26
|
+
runtime?: ReturnType<typeof Atom.runtime>;
|
|
27
|
+
}): Atom.Writable<T> => {
|
|
28
|
+
const runtime = options.runtime ?? defaultRuntime;
|
|
29
|
+
return Atom.kvs({
|
|
30
|
+
runtime,
|
|
31
|
+
key: options.key,
|
|
32
|
+
schema: options.schema,
|
|
33
|
+
defaultValue: options.defaultValue,
|
|
34
|
+
}).pipe(Atom.keepAlive);
|
|
35
|
+
};
|