@trayio/commons 5.34.0 → 5.35.0-unstable.fd7158a
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/base64/Base64.d.ts +5 -10
- package/dist/base64/Base64.d.ts.map +1 -1
- package/dist/base64/Base64.js +8 -40
- package/dist/base64/Base64.unit.test.js +4 -27
- package/dist/buffer/BufferExtensions.d.ts +4 -4
- package/dist/buffer/BufferExtensions.d.ts.map +1 -1
- package/dist/buffer/BufferExtensions.js +25 -38
- package/dist/buffer/BufferExtensions.unit.test.js +22 -45
- package/dist/callsite/CallSite.js +17 -7
- package/dist/codec/CamelCasePropertyNameCodec.d.ts +2 -2
- package/dist/codec/CamelCasePropertyNameCodec.d.ts.map +1 -1
- package/dist/codec/CamelCasePropertyNameCodec.js +2 -25
- package/dist/codec/Codec.abstract.test.d.ts +2 -2
- package/dist/codec/Codec.abstract.test.d.ts.map +1 -1
- package/dist/codec/Codec.abstract.test.js +5 -31
- package/dist/codec/Codec.d.ts +2 -2
- package/dist/codec/Codec.d.ts.map +1 -1
- package/dist/codec/CompositeCodec.d.ts +2 -2
- package/dist/codec/CompositeCodec.d.ts.map +1 -1
- package/dist/codec/CompositeCodec.js +2 -26
- package/dist/codec/PartialRedactionCodec.d.ts +2 -2
- package/dist/codec/PartialRedactionCodec.d.ts.map +1 -1
- package/dist/codec/PartialRedactionCodec.js +2 -25
- package/dist/codec/RemoveNullValuesCodec.d.ts +2 -2
- package/dist/codec/RemoveNullValuesCodec.d.ts.map +1 -1
- package/dist/codec/RemoveNullValuesCodec.js +2 -25
- package/dist/codec/RemoveTopLevelTagCodec.d.ts +2 -2
- package/dist/codec/RemoveTopLevelTagCodec.d.ts.map +1 -1
- package/dist/codec/RemoveTopLevelTagCodec.js +2 -25
- package/dist/codec/SensitiveRemoveTagAndRedactCodec.d.ts +2 -2
- package/dist/codec/SensitiveRemoveTagAndRedactCodec.d.ts.map +1 -1
- package/dist/codec/SensitiveRemoveTagAndRedactCodec.js +2 -25
- package/dist/codec/SensitiveRemoveTagAndRedactCodec.unit.test.js +15 -38
- package/dist/codec/SnakeCasePropertyNameCodec.d.ts +2 -2
- package/dist/codec/SnakeCasePropertyNameCodec.d.ts.map +1 -1
- package/dist/codec/SnakeCasePropertyNameCodec.js +2 -25
- package/dist/codec/TypeCodec.d.ts +24 -8
- package/dist/codec/TypeCodec.d.ts.map +1 -1
- package/dist/codec/TypeCodec.js +40 -48
- package/dist/codec/TypeCodec.unit.test.js +12 -38
- package/dist/dynamicschema/DynamicSchema.d.ts +6 -12
- package/dist/dynamicschema/DynamicSchema.d.ts.map +1 -1
- package/dist/dynamicschema/DynamicSchema.js +18 -41
- package/dist/dynamicschema/DynamicSchema.unit.test.js +51 -75
- package/dist/dynamictype/DynamicType.d.ts +17 -20
- package/dist/dynamictype/DynamicType.d.ts.map +1 -1
- package/dist/dynamictype/DynamicType.js +48 -35
- package/dist/effect/EffectExtensions.d.ts +29 -0
- package/dist/effect/EffectExtensions.d.ts.map +1 -0
- package/dist/effect/EffectExtensions.js +30 -0
- package/dist/effect/EffectExtensions.unit.test.d.ts +2 -0
- package/dist/effect/EffectExtensions.unit.test.d.ts.map +1 -0
- package/dist/effect/EffectExtensions.unit.test.js +59 -0
- package/dist/effect/Interop.d.ts +53 -0
- package/dist/effect/Interop.d.ts.map +1 -0
- package/dist/effect/Interop.js +56 -0
- package/dist/effect/Interop.unit.test.d.ts +2 -0
- package/dist/effect/Interop.unit.test.d.ts.map +1 -0
- package/dist/effect/Interop.unit.test.js +134 -0
- package/dist/effect/SchemaExtensions.d.ts +11 -0
- package/dist/effect/SchemaExtensions.d.ts.map +1 -0
- package/dist/effect/SchemaExtensions.js +7 -0
- package/dist/effect/SchemaExtensions.unit.test.d.ts +2 -0
- package/dist/effect/SchemaExtensions.unit.test.d.ts.map +1 -0
- package/dist/effect/SchemaExtensions.unit.test.js +29 -0
- package/dist/file/FakeFileStorage.d.ts +8 -8
- package/dist/file/FakeFileStorage.d.ts.map +1 -1
- package/dist/file/FakeFileStorage.js +16 -40
- package/dist/file/File.d.ts +10 -14
- package/dist/file/File.d.ts.map +1 -1
- package/dist/file/File.js +31 -24
- package/dist/file/FileStorage.abstract.test.d.ts.map +1 -1
- package/dist/file/FileStorage.abstract.test.js +18 -41
- package/dist/file/NodeFsFileStorage.d.ts +9 -11
- package/dist/file/NodeFsFileStorage.d.ts.map +1 -1
- package/dist/file/NodeFsFileStorage.integration.test.js +21 -45
- package/dist/file/NodeFsFileStorage.js +37 -29
- package/dist/http/Http.d.ts +11 -13
- package/dist/http/Http.d.ts.map +1 -1
- package/dist/http/Http.js +30 -22
- package/dist/http/Http.unit.test.js +3 -26
- package/dist/http/HttpClient.abstract.test.d.ts.map +1 -1
- package/dist/http/HttpClient.abstract.test.js +104 -108
- package/dist/http/HttpClient.d.ts +2 -2
- package/dist/http/HttpClient.d.ts.map +1 -1
- package/dist/http/HttpController.d.ts +2 -3
- package/dist/http/HttpController.d.ts.map +1 -1
- package/dist/http/HttpSchemas.d.ts +13 -0
- package/dist/http/HttpSchemas.d.ts.map +1 -0
- package/dist/http/HttpSchemas.js +23 -0
- package/dist/http/MultipartHttpRequestBody.integration.test.js +31 -20
- package/dist/http/MultipartHttpRequestBody.unit.test.js +70 -53
- package/dist/record/Record.d.ts +3 -3
- package/dist/record/Record.d.ts.map +1 -1
- package/dist/record/Record.js +9 -30
- package/dist/result/Result.d.ts +3 -3
- package/dist/result/Result.d.ts.map +1 -1
- package/dist/result/Result.js +9 -26
- package/dist/schema/DateFromUnixTime.d.ts +4 -0
- package/dist/schema/DateFromUnixTime.d.ts.map +1 -0
- package/dist/schema/DateFromUnixTime.js +10 -0
- package/dist/schema/DateFromUnixTime.unit.test.d.ts +2 -0
- package/dist/schema/DateFromUnixTime.unit.test.d.ts.map +1 -0
- package/dist/schema/DateFromUnixTime.unit.test.js +11 -0
- package/dist/schema/JsonSchemaIntrospector.js +17 -7
- package/dist/sensitive/Sensitive.d.ts +7 -3
- package/dist/sensitive/Sensitive.d.ts.map +1 -1
- package/dist/sensitive/Sensitive.js +23 -39
- package/dist/sensitive/Sensitive.unit.test.d.ts +4 -4
- package/dist/sensitive/Sensitive.unit.test.d.ts.map +1 -1
- package/dist/sensitive/Sensitive.unit.test.js +20 -46
- package/dist/serialization/FormUrlEncodedSerialization.d.ts +2 -2
- package/dist/serialization/FormUrlEncodedSerialization.d.ts.map +1 -1
- package/dist/serialization/FormUrlEncodedSerialization.js +5 -28
- package/dist/serialization/JsonSerialization.d.ts +2 -2
- package/dist/serialization/JsonSerialization.d.ts.map +1 -1
- package/dist/serialization/JsonSerialization.js +5 -28
- package/dist/serialization/Serialization.abstract.test.d.ts.map +1 -1
- package/dist/serialization/Serialization.abstract.test.js +6 -32
- package/dist/serialization/Serialization.d.ts +2 -2
- package/dist/serialization/Serialization.d.ts.map +1 -1
- package/dist/try/Try.d.ts +2 -2
- package/dist/try/Try.d.ts.map +1 -1
- package/dist/try/Try.js +2 -32
- package/dist/uuid/Uuid.d.ts +11 -10
- package/dist/uuid/Uuid.d.ts.map +1 -1
- package/dist/uuid/Uuid.js +13 -39
- package/dist/uuid/Uuid.unit.test.js +4 -27
- package/dist/zod/SchemaParser.abstract.test.d.ts.map +1 -1
- package/dist/zod/SchemaParser.abstract.test.js +6 -31
- package/dist/zod/SchemaParser.d.ts +2 -2
- package/dist/zod/SchemaParser.d.ts.map +1 -1
- package/dist/zod/ZodSchemaParser.d.ts +3 -4
- package/dist/zod/ZodSchemaParser.d.ts.map +1 -1
- package/dist/zod/ZodSchemaParser.js +4 -28
- package/dist/zod/ZodSchemaParser.unit.test.js +2 -25
- package/package.json +3 -8
- package/dist/http/HttpTypeDescriptors.d.ts +0 -11
- package/dist/http/HttpTypeDescriptors.d.ts.map +0 -1
- package/dist/http/HttpTypeDescriptors.js +0 -59
- package/dist/task/Task.d.ts +0 -19
- package/dist/task/Task.d.ts.map +0 -1
- package/dist/task/Task.js +0 -74
- package/dist/task/Task.unit.test.d.ts +0 -2
- package/dist/task/Task.unit.test.d.ts.map +0 -1
- package/dist/task/Task.unit.test.js +0 -46
|
@@ -1,18 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import * as O from 'fp-ts/Option';
|
|
3
|
-
import * as t from 'io-ts';
|
|
1
|
+
import { Either, Option, Schema } from 'effect';
|
|
4
2
|
import { DynamicObject } from '../dynamictype/DynamicType';
|
|
5
|
-
declare const
|
|
6
|
-
export type DynamicSchema =
|
|
7
|
-
export interface DynamicSchemaBrand {
|
|
8
|
-
readonly DynamicSchema: unique symbol;
|
|
9
|
-
}
|
|
3
|
+
export declare const dynamicSchemaSchema: Schema.brand<Schema.filter<Schema.Schema<DynamicObject, DynamicObject, never>>, "DynamicSchema">;
|
|
4
|
+
export type DynamicSchema = typeof dynamicSchemaSchema.Type;
|
|
10
5
|
export interface DynamicSchemaInterface {
|
|
11
|
-
fromDynamicObject: (schema: DynamicObject, strict?:
|
|
12
|
-
validate: (schema: DynamicSchema | DynamicObject, value: DynamicObject, strict?:
|
|
13
|
-
fromValue: (value: DynamicObject, schemaUrl:
|
|
6
|
+
fromDynamicObject: (schema: DynamicObject, strict?: Option.Option<boolean>) => Either.Either<DynamicSchema, Error>;
|
|
7
|
+
validate: (schema: DynamicSchema | DynamicObject, value: DynamicObject, strict?: Option.Option<boolean>, coerce?: boolean) => Either.Either<DynamicObject, Error>;
|
|
8
|
+
fromValue: (value: DynamicObject, schemaUrl: Option.Option<string>) => DynamicObject;
|
|
14
9
|
isValidJsonSchema: (schema: DynamicObject) => boolean;
|
|
15
10
|
}
|
|
16
11
|
export declare const DynamicSchema: DynamicSchemaInterface;
|
|
17
|
-
export {};
|
|
18
12
|
//# sourceMappingURL=DynamicSchema.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DynamicSchema.d.ts","sourceRoot":"","sources":["../../src/dynamicschema/DynamicSchema.ts"],"names":[],"mappings":"AAEA,OAAO,
|
|
1
|
+
{"version":3,"file":"DynamicSchema.d.ts","sourceRoot":"","sources":["../../src/dynamicschema/DynamicSchema.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAQ,MAAM,QAAQ,CAAC;AAEtD,OAAO,EAAE,aAAa,EAAuB,MAAM,4BAA4B,CAAC;AA+BhF,eAAO,MAAM,mBAAmB,kGAOc,CAAC;AAE/C,MAAM,MAAM,aAAa,GAAG,OAAO,mBAAmB,CAAC,IAAI,CAAC;AA8F5D,MAAM,WAAW,sBAAsB;IACtC,iBAAiB,EAAE,CAClB,MAAM,EAAE,aAAa,EACrB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,KAC3B,MAAM,CAAC,MAAM,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC;IACzC,QAAQ,EAAE,CACT,MAAM,EAAE,aAAa,GAAG,aAAa,EACrC,KAAK,EAAE,aAAa,EACpB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,EAC/B,MAAM,CAAC,EAAE,OAAO,KACZ,MAAM,CAAC,MAAM,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC;IACzC,SAAS,EAAE,CACV,KAAK,EAAE,aAAa,EACpB,SAAS,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,KAC5B,aAAa,CAAC;IACnB,iBAAiB,EAAE,CAAC,MAAM,EAAE,aAAa,KAAK,OAAO,CAAC;CACtD;AAED,eAAO,MAAM,aAAa,EAAE,sBA6C3B,CAAC"}
|
|
@@ -1,38 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
4
|
};
|
|
28
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.DynamicSchema = void 0;
|
|
6
|
+
exports.DynamicSchema = exports.dynamicSchemaSchema = void 0;
|
|
30
7
|
const ajv_draft_04_1 = __importDefault(require("ajv-draft-04"));
|
|
31
8
|
const ajv_formats_1 = __importDefault(require("ajv-formats"));
|
|
32
|
-
const
|
|
33
|
-
const O = __importStar(require("fp-ts/Option"));
|
|
34
|
-
const t = __importStar(require("io-ts"));
|
|
35
|
-
const function_1 = require("fp-ts/lib/function");
|
|
9
|
+
const effect_1 = require("effect");
|
|
36
10
|
const deep_copy_ts_1 = require("deep-copy-ts");
|
|
37
11
|
const DynamicType_1 = require("../dynamictype/DynamicType");
|
|
38
12
|
const TypeCodec_1 = require("../codec/TypeCodec");
|
|
@@ -60,10 +34,14 @@ const isDynamicSchema = (o) => {
|
|
|
60
34
|
return false;
|
|
61
35
|
}
|
|
62
36
|
};
|
|
63
|
-
|
|
64
|
-
|
|
37
|
+
exports.dynamicSchemaSchema = DynamicType_1.dynamicObjectSchema
|
|
38
|
+
.pipe(effect_1.Schema.filter(isDynamicSchema, {
|
|
39
|
+
message: () => 'Not a valid JSON schema',
|
|
40
|
+
}), effect_1.Schema.brand('DynamicSchema'))
|
|
41
|
+
.annotations({ identifier: 'DynamicSchema' });
|
|
42
|
+
const dynamicSchemaTypeCodec = TypeCodec_1.TypeCodec.fromSchema(exports.dynamicSchemaSchema);
|
|
65
43
|
const preprocessSchema = (schema, strict) => {
|
|
66
|
-
const hasStrict =
|
|
44
|
+
const hasStrict = effect_1.Option.isSome(strict);
|
|
67
45
|
delete schema.$schema;
|
|
68
46
|
if (schema.oneOf || schema.anyOf || schema.allOf) {
|
|
69
47
|
const schemas = (schema.oneOf ||
|
|
@@ -127,18 +105,17 @@ const createSchema = (value) => {
|
|
|
127
105
|
return {};
|
|
128
106
|
};
|
|
129
107
|
const createSchemaWithSchemaUrl = (schema, schemaUrl) => {
|
|
130
|
-
const hasSchemaUrl = O.isSome(schemaUrl);
|
|
131
108
|
const schemaObj = createSchema(schema);
|
|
132
|
-
schemaObj[schemaProperty] =
|
|
109
|
+
schemaObj[schemaProperty] = effect_1.Option.getOrElse(schemaUrl, () => draft04SchemaUrl);
|
|
133
110
|
return schemaObj;
|
|
134
111
|
};
|
|
135
112
|
exports.DynamicSchema = {
|
|
136
|
-
fromDynamicObject: (schema, strict =
|
|
113
|
+
fromDynamicObject: (schema, strict = effect_1.Option.none()) => {
|
|
137
114
|
const copiedSchema = (0, deep_copy_ts_1.deepCopy)(schema);
|
|
138
115
|
preprocessSchema(copiedSchema, strict);
|
|
139
|
-
return
|
|
116
|
+
return effect_1.Either.mapLeft(dynamicSchemaTypeCodec.decode(copiedSchema), (e) => new Error(`Failed to create DynamicSchema: ${e}`));
|
|
140
117
|
},
|
|
141
|
-
validate: (schema, value, strict =
|
|
118
|
+
validate: (schema, value, strict = effect_1.Option.none(), coerce = false) => {
|
|
142
119
|
const copiedSchema = (0, deep_copy_ts_1.deepCopy)(schema);
|
|
143
120
|
preprocessSchema(copiedSchema, strict);
|
|
144
121
|
const copiedValue = (0, deep_copy_ts_1.deepCopy)(value);
|
|
@@ -148,16 +125,16 @@ exports.DynamicSchema = {
|
|
|
148
125
|
}
|
|
149
126
|
return ajv.compile(copiedSchema);
|
|
150
127
|
});
|
|
151
|
-
return
|
|
128
|
+
return effect_1.Either.flatMap(validateE, (validate) => {
|
|
152
129
|
if (validate(copiedValue)) {
|
|
153
|
-
return
|
|
130
|
+
return effect_1.Either.right(copiedValue);
|
|
154
131
|
}
|
|
155
132
|
const errorText = ajv
|
|
156
133
|
.errorsText(validate.errors)
|
|
157
134
|
.replace(/data\//g, '');
|
|
158
|
-
return
|
|
159
|
-
})
|
|
135
|
+
return effect_1.Either.left(new Error(errorText));
|
|
136
|
+
});
|
|
160
137
|
},
|
|
161
|
-
fromValue: (value, schemaUrl =
|
|
138
|
+
fromValue: (value, schemaUrl = effect_1.Option.none()) => createSchemaWithSchemaUrl(value, schemaUrl),
|
|
162
139
|
isValidJsonSchema: (schema) => isDynamicSchema(schema),
|
|
163
140
|
};
|
|
@@ -1,30 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
const
|
|
27
|
-
const O = __importStar(require("fp-ts/Option"));
|
|
3
|
+
const effect_1 = require("effect");
|
|
28
4
|
const DynamicSchema_1 = require("./DynamicSchema");
|
|
29
5
|
describe('DynamicSchema', () => {
|
|
30
6
|
let validSchema;
|
|
@@ -166,15 +142,15 @@ describe('DynamicSchema', () => {
|
|
|
166
142
|
describe('fromDynamicObject', () => {
|
|
167
143
|
it('should return a Right DynamicSchema when given a valid schema object', () => {
|
|
168
144
|
const result = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
|
|
169
|
-
expect(
|
|
170
|
-
if (
|
|
145
|
+
expect(effect_1.Either.isRight(result)).toBe(true);
|
|
146
|
+
if (effect_1.Either.isRight(result)) {
|
|
171
147
|
expect(result.right).toEqual(validSchema);
|
|
172
148
|
}
|
|
173
149
|
});
|
|
174
150
|
it('should return a Left Error when given an invalid schema object', () => {
|
|
175
151
|
const result = DynamicSchema_1.DynamicSchema.fromDynamicObject(invalidSchema);
|
|
176
|
-
expect(
|
|
177
|
-
if (
|
|
152
|
+
expect(effect_1.Either.isLeft(result)).toBe(true);
|
|
153
|
+
if (effect_1.Either.isLeft(result)) {
|
|
178
154
|
expect(result.left).toBeInstanceOf(Error);
|
|
179
155
|
expect(result.left.message).toContain('Failed to create DynamicSchema:');
|
|
180
156
|
}
|
|
@@ -205,8 +181,8 @@ describe('DynamicSchema', () => {
|
|
|
205
181
|
},
|
|
206
182
|
advanced: [],
|
|
207
183
|
};
|
|
208
|
-
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema,
|
|
209
|
-
expect(actualSchemaE).toEqual(
|
|
184
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, effect_1.Option.none());
|
|
185
|
+
expect(actualSchemaE).toEqual(effect_1.Either.right(expectedSchema));
|
|
210
186
|
});
|
|
211
187
|
it('should correctly remove empty required arrays and not add additionalProperties when strict is not set', () => {
|
|
212
188
|
const schema = {
|
|
@@ -234,8 +210,8 @@ describe('DynamicSchema', () => {
|
|
|
234
210
|
},
|
|
235
211
|
advanced: [],
|
|
236
212
|
};
|
|
237
|
-
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema,
|
|
238
|
-
expect(actualSchemaE).toEqual(
|
|
213
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, effect_1.Option.none());
|
|
214
|
+
expect(actualSchemaE).toEqual(effect_1.Either.right(expectedSchema));
|
|
239
215
|
});
|
|
240
216
|
it('should correctly keep populated required arrays and not add additionalProperties when strict is not set', () => {
|
|
241
217
|
const schema = {
|
|
@@ -264,8 +240,8 @@ describe('DynamicSchema', () => {
|
|
|
264
240
|
required: ['age', 'name'],
|
|
265
241
|
advanced: [],
|
|
266
242
|
};
|
|
267
|
-
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema,
|
|
268
|
-
expect(actualSchemaE).toEqual(
|
|
243
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, effect_1.Option.none());
|
|
244
|
+
expect(actualSchemaE).toEqual(effect_1.Either.right(expectedSchema));
|
|
269
245
|
});
|
|
270
246
|
it('should correctly remove empty required and add additionalProperties=true to object schema when strict is set', () => {
|
|
271
247
|
const schema = {
|
|
@@ -294,8 +270,8 @@ describe('DynamicSchema', () => {
|
|
|
294
270
|
advanced: [],
|
|
295
271
|
additionalProperties: true,
|
|
296
272
|
};
|
|
297
|
-
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema,
|
|
298
|
-
expect(actualSchemaE).toEqual(
|
|
273
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, effect_1.Option.some(false));
|
|
274
|
+
expect(actualSchemaE).toEqual(effect_1.Either.right(expectedSchema));
|
|
299
275
|
});
|
|
300
276
|
it('should correctly add additionalProperties=false to object schema', () => {
|
|
301
277
|
const schema = validSchema;
|
|
@@ -335,8 +311,8 @@ describe('DynamicSchema', () => {
|
|
|
335
311
|
advanced: [],
|
|
336
312
|
additionalProperties: false,
|
|
337
313
|
};
|
|
338
|
-
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema,
|
|
339
|
-
expect(actualSchemaE).toEqual(
|
|
314
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, effect_1.Option.some(true));
|
|
315
|
+
expect(actualSchemaE).toEqual(effect_1.Either.right(expectedSchema));
|
|
340
316
|
});
|
|
341
317
|
it('should correctly add additionalProperties=true to complex object schema', () => {
|
|
342
318
|
const schema = {
|
|
@@ -402,15 +378,15 @@ describe('DynamicSchema', () => {
|
|
|
402
378
|
},
|
|
403
379
|
additionalProperties: true,
|
|
404
380
|
};
|
|
405
|
-
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema,
|
|
406
|
-
expect(actualSchemaE).toEqual(
|
|
381
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, effect_1.Option.some(false));
|
|
382
|
+
expect(actualSchemaE).toEqual(effect_1.Either.right(expectedSchema));
|
|
407
383
|
});
|
|
408
384
|
it('should skip mutation if there is no object type schema', () => {
|
|
409
385
|
const schema = {
|
|
410
386
|
type: 'string',
|
|
411
387
|
};
|
|
412
|
-
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema,
|
|
413
|
-
expect(actualSchemaE).toEqual(
|
|
388
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, effect_1.Option.some(true));
|
|
389
|
+
expect(actualSchemaE).toEqual(effect_1.Either.right(schema));
|
|
414
390
|
});
|
|
415
391
|
it('should correctly add additionalProperties=true to oneOf schemas', () => {
|
|
416
392
|
const schema = {
|
|
@@ -457,8 +433,8 @@ describe('DynamicSchema', () => {
|
|
|
457
433
|
},
|
|
458
434
|
],
|
|
459
435
|
};
|
|
460
|
-
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema,
|
|
461
|
-
expect(actualSchemaE).toEqual(
|
|
436
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, effect_1.Option.some(false));
|
|
437
|
+
expect(actualSchemaE).toEqual(effect_1.Either.right(expectedSchema));
|
|
462
438
|
});
|
|
463
439
|
it('should correctly add additionalProperties=true to anyOf schemas', () => {
|
|
464
440
|
const schema = {
|
|
@@ -505,8 +481,8 @@ describe('DynamicSchema', () => {
|
|
|
505
481
|
},
|
|
506
482
|
],
|
|
507
483
|
};
|
|
508
|
-
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema,
|
|
509
|
-
expect(actualSchemaE).toEqual(
|
|
484
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, effect_1.Option.some(false));
|
|
485
|
+
expect(actualSchemaE).toEqual(effect_1.Either.right(expectedSchema));
|
|
510
486
|
});
|
|
511
487
|
it('should correctly add additionalProperties=true to allOf schemas', () => {
|
|
512
488
|
const schema = {
|
|
@@ -553,67 +529,67 @@ describe('DynamicSchema', () => {
|
|
|
553
529
|
},
|
|
554
530
|
],
|
|
555
531
|
};
|
|
556
|
-
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema,
|
|
557
|
-
expect(actualSchemaE).toEqual(
|
|
532
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, effect_1.Option.some(false));
|
|
533
|
+
expect(actualSchemaE).toEqual(effect_1.Either.right(expectedSchema));
|
|
558
534
|
});
|
|
559
535
|
});
|
|
560
536
|
describe('validate', () => {
|
|
561
537
|
it('should return a Right with the value when the value matches the schema', () => {
|
|
562
538
|
const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
|
|
563
|
-
if (
|
|
564
|
-
const result = DynamicSchema_1.DynamicSchema.validate(schema.right, value,
|
|
565
|
-
expect(
|
|
566
|
-
if (
|
|
539
|
+
if (effect_1.Either.isRight(schema)) {
|
|
540
|
+
const result = DynamicSchema_1.DynamicSchema.validate(schema.right, value, effect_1.Option.none());
|
|
541
|
+
expect(effect_1.Either.isRight(result)).toBe(true);
|
|
542
|
+
if (effect_1.Either.isRight(result)) {
|
|
567
543
|
expect(result.right).toEqual(value);
|
|
568
544
|
}
|
|
569
545
|
}
|
|
570
546
|
});
|
|
571
547
|
it('should return a Right with the value when strict is false and value matches the schema', () => {
|
|
572
548
|
const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
|
|
573
|
-
if (
|
|
574
|
-
const result = DynamicSchema_1.DynamicSchema.validate(schema.right, value,
|
|
575
|
-
expect(
|
|
576
|
-
if (
|
|
549
|
+
if (effect_1.Either.isRight(schema)) {
|
|
550
|
+
const result = DynamicSchema_1.DynamicSchema.validate(schema.right, value, effect_1.Option.some(true));
|
|
551
|
+
expect(effect_1.Either.isRight(result)).toBe(true);
|
|
552
|
+
if (effect_1.Either.isRight(result)) {
|
|
577
553
|
expect(result.right).toEqual(value);
|
|
578
554
|
}
|
|
579
555
|
}
|
|
580
556
|
});
|
|
581
557
|
it('should return a Right with non extra values when value matches the schema but has extra fields', () => {
|
|
582
558
|
const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
|
|
583
|
-
if (
|
|
584
|
-
const result = DynamicSchema_1.DynamicSchema.validate(schema.right, valueWithExtraFields,
|
|
585
|
-
expect(
|
|
586
|
-
if (
|
|
559
|
+
if (effect_1.Either.isRight(schema)) {
|
|
560
|
+
const result = DynamicSchema_1.DynamicSchema.validate(schema.right, valueWithExtraFields, effect_1.Option.some(true));
|
|
561
|
+
expect(effect_1.Either.isRight(result)).toBe(true);
|
|
562
|
+
if (effect_1.Either.isRight(result)) {
|
|
587
563
|
expect(result.right).toEqual(value);
|
|
588
564
|
}
|
|
589
565
|
}
|
|
590
566
|
});
|
|
591
567
|
it('should return a Right with extra values when value matches the schema but has extra fields and strict is false', () => {
|
|
592
568
|
const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
|
|
593
|
-
if (
|
|
594
|
-
const result = DynamicSchema_1.DynamicSchema.validate(schema.right, valueWithExtraFields,
|
|
595
|
-
expect(
|
|
596
|
-
if (
|
|
569
|
+
if (effect_1.Either.isRight(schema)) {
|
|
570
|
+
const result = DynamicSchema_1.DynamicSchema.validate(schema.right, valueWithExtraFields, effect_1.Option.some(false));
|
|
571
|
+
expect(effect_1.Either.isRight(result)).toBe(true);
|
|
572
|
+
if (effect_1.Either.isRight(result)) {
|
|
597
573
|
expect(result.right).toEqual(valueWithExtraFields);
|
|
598
574
|
}
|
|
599
575
|
}
|
|
600
576
|
});
|
|
601
577
|
it('should return a Right with when value matches the schema but has coerceable types', () => {
|
|
602
578
|
const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
|
|
603
|
-
if (
|
|
604
|
-
const result = DynamicSchema_1.DynamicSchema.validate(schema.right, valueWithCoerceableTypes,
|
|
605
|
-
expect(
|
|
606
|
-
if (
|
|
579
|
+
if (effect_1.Either.isRight(schema)) {
|
|
580
|
+
const result = DynamicSchema_1.DynamicSchema.validate(schema.right, valueWithCoerceableTypes, effect_1.Option.some(true), true);
|
|
581
|
+
expect(effect_1.Either.isRight(result)).toBe(true);
|
|
582
|
+
if (effect_1.Either.isRight(result)) {
|
|
607
583
|
expect(result.right).toEqual(value);
|
|
608
584
|
}
|
|
609
585
|
}
|
|
610
586
|
});
|
|
611
587
|
it('should return a Left Error when the value does not match the schema', () => {
|
|
612
588
|
const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
|
|
613
|
-
if (
|
|
614
|
-
const result = DynamicSchema_1.DynamicSchema.validate(schema.right, invalidValue,
|
|
615
|
-
expect(
|
|
616
|
-
if (
|
|
589
|
+
if (effect_1.Either.isRight(schema)) {
|
|
590
|
+
const result = DynamicSchema_1.DynamicSchema.validate(schema.right, invalidValue, effect_1.Option.none());
|
|
591
|
+
expect(effect_1.Either.isLeft(result)).toBe(true);
|
|
592
|
+
if (effect_1.Either.isLeft(result)) {
|
|
617
593
|
expect(result.left).toBeInstanceOf(Error);
|
|
618
594
|
}
|
|
619
595
|
}
|
|
@@ -621,12 +597,12 @@ describe('DynamicSchema', () => {
|
|
|
621
597
|
});
|
|
622
598
|
describe('fromValue', () => {
|
|
623
599
|
it('should return a Right with the schema when given a valid value object', () => {
|
|
624
|
-
const result = DynamicSchema_1.DynamicSchema.fromValue(value,
|
|
600
|
+
const result = DynamicSchema_1.DynamicSchema.fromValue(value, effect_1.Option.none());
|
|
625
601
|
expect(result).toEqual(validSchemaWithoutExtraFields);
|
|
626
602
|
});
|
|
627
603
|
it('should return a Right with the schema when given a valid value object and a schema url', () => {
|
|
628
604
|
const schemaUrl = 'http://json-schema.org/draft-04/schema#';
|
|
629
|
-
const result = DynamicSchema_1.DynamicSchema.fromValue(value,
|
|
605
|
+
const result = DynamicSchema_1.DynamicSchema.fromValue(value, effect_1.Option.some(schemaUrl));
|
|
630
606
|
expect(result.$schema).toEqual(schemaUrl);
|
|
631
607
|
});
|
|
632
608
|
});
|
|
@@ -1,36 +1,33 @@
|
|
|
1
|
-
import
|
|
2
|
-
import * as E from 'fp-ts/Either';
|
|
3
|
-
import * as O from 'fp-ts/Option';
|
|
4
|
-
import * as t from 'io-ts';
|
|
1
|
+
import { Effect, Either, Option, Schema } from 'effect';
|
|
5
2
|
import { Codec } from '../codec/Codec';
|
|
6
3
|
export type DynamicType = string | number | boolean | null | DynamicObject | DynamicArray;
|
|
7
4
|
export type DynamicObject = {
|
|
8
5
|
[x: string]: DynamicType;
|
|
9
6
|
};
|
|
10
7
|
export type DynamicArray = Array<DynamicType>;
|
|
8
|
+
export declare const dynamicTypeSchema: Schema.Schema<DynamicType>;
|
|
9
|
+
export declare const dynamicObjectSchema: Schema.Schema<DynamicObject>;
|
|
10
|
+
export declare const dynamicArraySchema: Schema.Schema<DynamicArray>;
|
|
11
11
|
export interface DynamicObjectInterface {
|
|
12
|
-
fromBase64String: (base64Str: string) =>
|
|
12
|
+
fromBase64String: (base64Str: string) => Either.Either<DynamicObject, Error>;
|
|
13
13
|
removeNullValues: (value: DynamicObject) => DynamicObject;
|
|
14
14
|
isDynamicObject: (value: unknown) => value is DynamicObject;
|
|
15
|
-
getTypedProperty: <T>(dynamicObject: DynamicObject, name: string, codec: Codec<T>) =>
|
|
16
|
-
getObjectProperty: (dynamicObject: DynamicObject, name: string) =>
|
|
17
|
-
getStringProperty: (dynamicObject: DynamicObject, name: string) =>
|
|
18
|
-
getNumberProperty: (dynamicObject: DynamicObject, name: string) =>
|
|
19
|
-
getBooleanProperty: (dynamicObject: DynamicObject, name: string) =>
|
|
15
|
+
getTypedProperty: <T>(dynamicObject: DynamicObject, name: string, codec: Codec<T>) => Option.Option<T>;
|
|
16
|
+
getObjectProperty: (dynamicObject: DynamicObject, name: string) => Option.Option<DynamicObject>;
|
|
17
|
+
getStringProperty: (dynamicObject: DynamicObject, name: string) => Option.Option<string>;
|
|
18
|
+
getNumberProperty: (dynamicObject: DynamicObject, name: string) => Option.Option<number>;
|
|
19
|
+
getBooleanProperty: (dynamicObject: DynamicObject, name: string) => Option.Option<boolean>;
|
|
20
20
|
}
|
|
21
21
|
export declare const DynamicObject: DynamicObjectInterface;
|
|
22
22
|
export interface DynamicTypeInterface {
|
|
23
|
-
readFromFile: (path: string) =>
|
|
24
|
-
writeToFile: (path: string, value: DynamicType) =>
|
|
25
|
-
writeToFileAsync: (path: string, value: DynamicType) =>
|
|
26
|
-
safeCast: <T>(dynamicType: DynamicType, codec: Codec<T>) =>
|
|
27
|
-
getStringProperty: (dynamicObject: DynamicObject, name: string) =>
|
|
28
|
-
getNumberProperty: (dynamicObject: DynamicObject, name: string) =>
|
|
29
|
-
getBooleanProperty: (dynamicObject: DynamicObject, name: string) =>
|
|
23
|
+
readFromFile: (path: string) => Either.Either<DynamicType, Error>;
|
|
24
|
+
writeToFile: (path: string, value: DynamicType) => Either.Either<undefined, Error>;
|
|
25
|
+
writeToFileAsync: (path: string, value: DynamicType) => Effect.Effect<undefined, Error>;
|
|
26
|
+
safeCast: <T>(dynamicType: DynamicType, codec: Codec<T>) => Either.Either<T, Error>;
|
|
27
|
+
getStringProperty: (dynamicObject: DynamicObject, name: string) => Option.Option<string>;
|
|
28
|
+
getNumberProperty: (dynamicObject: DynamicObject, name: string) => Option.Option<number>;
|
|
29
|
+
getBooleanProperty: (dynamicObject: DynamicObject, name: string) => Option.Option<boolean>;
|
|
30
30
|
}
|
|
31
31
|
export declare const DynamicType: DynamicTypeInterface;
|
|
32
|
-
export declare const dynamicTypeDescriptor: t.Type<DynamicType>;
|
|
33
|
-
export declare const dynamicObjectTypeDescriptor: t.Type<DynamicObject>;
|
|
34
|
-
export declare const dynamicArrayTypeDescriptor: t.Type<DynamicArray>;
|
|
35
32
|
export declare const castUndefinedPropertyToNull: (obj: Record<string, any>) => DynamicType;
|
|
36
33
|
//# sourceMappingURL=DynamicType.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DynamicType.d.ts","sourceRoot":"","sources":["../../src/dynamictype/DynamicType.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"DynamicType.d.ts","sourceRoot":"","sources":["../../src/dynamictype/DynamicType.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MA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6BzB,CAAC;AAEF,eAAO,MAAM,2BAA2B,GACvC,KAAK,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KACtB,WAUF,CAAC"}
|
|
@@ -15,58 +15,71 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
25
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.castUndefinedPropertyToNull = exports.
|
|
27
|
-
const TE = __importStar(require("fp-ts/TaskEither"));
|
|
36
|
+
exports.castUndefinedPropertyToNull = exports.DynamicType = exports.DynamicObject = exports.dynamicArraySchema = exports.dynamicObjectSchema = exports.dynamicTypeSchema = void 0;
|
|
28
37
|
const fs = __importStar(require("fs"));
|
|
29
38
|
const fse = __importStar(require("fs-extra"));
|
|
30
|
-
const
|
|
31
|
-
const O = __importStar(require("fp-ts/Option"));
|
|
32
|
-
const function_1 = require("fp-ts/function");
|
|
33
|
-
const t = __importStar(require("io-ts"));
|
|
39
|
+
const effect_1 = require("effect");
|
|
34
40
|
const Base64_1 = require("../base64/Base64");
|
|
35
41
|
const JsonSerialization_1 = require("../serialization/JsonSerialization");
|
|
36
42
|
const Try_1 = require("../try/Try");
|
|
37
43
|
const RemoveNullValuesCodec_1 = require("../codec/RemoveNullValuesCodec");
|
|
38
44
|
const TypeCodec_1 = require("../codec/TypeCodec");
|
|
45
|
+
const Error_1 = require("../error/Error");
|
|
46
|
+
exports.dynamicTypeSchema = effect_1.Schema.Union(effect_1.Schema.String, effect_1.Schema.Number, effect_1.Schema.Boolean, effect_1.Schema.Null, effect_1.Schema.suspend(() => exports.dynamicObjectSchema), effect_1.Schema.suspend(() => exports.dynamicArraySchema)).annotations({ identifier: 'DynamicType' });
|
|
47
|
+
exports.dynamicObjectSchema = effect_1.Schema.mutable(effect_1.Schema.Record({
|
|
48
|
+
key: effect_1.Schema.String,
|
|
49
|
+
value: effect_1.Schema.suspend(() => exports.dynamicTypeSchema),
|
|
50
|
+
})).annotations({ identifier: 'DynamicObject' });
|
|
51
|
+
exports.dynamicArraySchema = effect_1.Schema.mutable(effect_1.Schema.Array(effect_1.Schema.suspend(() => exports.dynamicTypeSchema))).annotations({ identifier: 'DynamicArray' });
|
|
39
52
|
const serializer = new JsonSerialization_1.JsonSerialization();
|
|
40
53
|
const testRemoveNullValuesCodec = RemoveNullValuesCodec_1.RemoveNullValuesCodec.instance;
|
|
54
|
+
const isDynamicObject = effect_1.Schema.is(exports.dynamicObjectSchema);
|
|
55
|
+
const propertyOfType = (typeName) => (dynamicObject, name) => {
|
|
56
|
+
const property = dynamicObject[name];
|
|
57
|
+
return typeof property === typeName
|
|
58
|
+
? effect_1.Option.some(property)
|
|
59
|
+
: effect_1.Option.none();
|
|
60
|
+
};
|
|
41
61
|
exports.DynamicObject = {
|
|
42
|
-
fromBase64String: (base64Str) => (
|
|
62
|
+
fromBase64String: (base64Str) => effect_1.Either.map(effect_1.Either.flatMap(effect_1.Either.map(Base64_1.Base64.fromBase64String(base64Str), (base64) => Base64_1.Base64.decodeBinary(base64)), (decoded) => serializer.deserialize(decoded)), (decoded) => decoded),
|
|
43
63
|
removeNullValues: (value) => testRemoveNullValuesCodec.encode(value),
|
|
44
|
-
isDynamicObject: (value) =>
|
|
45
|
-
getTypedProperty: (dynamicObject, name, codec) =>
|
|
46
|
-
getObjectProperty: (dynamicObject, name) => exports.DynamicObject.getTypedProperty(dynamicObject, name, TypeCodec_1.TypeCodec.
|
|
47
|
-
getStringProperty: (
|
|
48
|
-
getNumberProperty: (
|
|
49
|
-
getBooleanProperty: (
|
|
64
|
+
isDynamicObject: (value) => isDynamicObject(value),
|
|
65
|
+
getTypedProperty: (dynamicObject, name, codec) => effect_1.Option.flatMap(effect_1.Option.fromNullable(dynamicObject[name]), (property) => effect_1.Option.getRight(exports.DynamicType.safeCast(property, codec))),
|
|
66
|
+
getObjectProperty: (dynamicObject, name) => exports.DynamicObject.getTypedProperty(dynamicObject, name, TypeCodec_1.TypeCodec.fromSchema(exports.dynamicObjectSchema)),
|
|
67
|
+
getStringProperty: propertyOfType('string'),
|
|
68
|
+
getNumberProperty: propertyOfType('number'),
|
|
69
|
+
getBooleanProperty: propertyOfType('boolean'),
|
|
50
70
|
};
|
|
51
71
|
exports.DynamicType = {
|
|
52
72
|
readFromFile: (path) => (0, Try_1.tryToEither)(() => JSON.parse(fs.readFileSync(path, 'utf8'))),
|
|
53
|
-
writeToFile: (path, value) =>
|
|
54
|
-
writeToFileAsync: (path, value) =>
|
|
73
|
+
writeToFile: (path, value) => effect_1.Either.map((0, Try_1.tryToEither)(() => fs.writeFileSync(path, JSON.stringify(value))), () => undefined),
|
|
74
|
+
writeToFileAsync: (path, value) => effect_1.Effect.map(effect_1.Effect.tryPromise({
|
|
75
|
+
try: () => fse.writeFile(path, JSON.stringify(value)),
|
|
76
|
+
catch: Error_1.castError,
|
|
77
|
+
}), () => undefined),
|
|
55
78
|
safeCast: (dynamicType, codec) => codec.decode(dynamicType),
|
|
56
|
-
getStringProperty: (
|
|
57
|
-
getNumberProperty: (
|
|
58
|
-
getBooleanProperty: (
|
|
79
|
+
getStringProperty: propertyOfType('string'),
|
|
80
|
+
getNumberProperty: propertyOfType('number'),
|
|
81
|
+
getBooleanProperty: propertyOfType('boolean'),
|
|
59
82
|
};
|
|
60
|
-
exports.dynamicTypeDescriptor = t.recursion('DynamicType', () => t.union([
|
|
61
|
-
t.string,
|
|
62
|
-
t.number,
|
|
63
|
-
t.boolean,
|
|
64
|
-
t.null,
|
|
65
|
-
exports.dynamicObjectTypeDescriptor,
|
|
66
|
-
exports.dynamicArrayTypeDescriptor,
|
|
67
|
-
]));
|
|
68
|
-
exports.dynamicObjectTypeDescriptor = t.recursion('DynamicObject', () => t.record(t.string, exports.dynamicTypeDescriptor));
|
|
69
|
-
exports.dynamicArrayTypeDescriptor = t.recursion('DynamicArray', () => t.array(exports.dynamicTypeDescriptor));
|
|
70
83
|
const castUndefinedPropertyToNull = (obj) => {
|
|
71
84
|
if (obj === undefined) {
|
|
72
85
|
return null;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Effect, Either } from 'effect';
|
|
2
|
+
/**
|
|
3
|
+
* Small helpers on top of Effect for the patterns the codebase used with fp-ts Task / TaskEither.
|
|
4
|
+
* Everything here is plain Effect; nothing requires a runtime layer.
|
|
5
|
+
*/
|
|
6
|
+
export interface EffectExtensionsInterface {
|
|
7
|
+
/** Wraps a promise-returning block; any rejection becomes an `Error` (strings and objects are coerced). */
|
|
8
|
+
tryPromiseWithSimpleError: <T>(block: () => Promise<T>) => Effect.Effect<T, Error>;
|
|
9
|
+
/** Wraps a synchronous block; anything thrown becomes an `Error`. */
|
|
10
|
+
tryWithSimpleError: <T>(block: () => T) => Effect.Effect<T, Error>;
|
|
11
|
+
/**
|
|
12
|
+
* Runs the effect. A typed failure rejects with the failure value itself and a defect rejects with the
|
|
13
|
+
* thrown value, so callers see the same errors they would have seen from the equivalent promise code.
|
|
14
|
+
*/
|
|
15
|
+
runToPromise: <T, E>(effect: Effect.Effect<T, E>) => Promise<T>;
|
|
16
|
+
/** Recovers from both typed failures and defects with a plain value. */
|
|
17
|
+
recover: <T, E>(effect: Effect.Effect<T, E>, onError: (error: Error) => T) => Effect.Effect<T>;
|
|
18
|
+
/**
|
|
19
|
+
* Runs `first`, then feeds each result to `next`: a Right continues the loop with the returned effect,
|
|
20
|
+
* a Left ends it with the final result.
|
|
21
|
+
*
|
|
22
|
+
* Left means "stop", not "error": the loop reuses `Either` as a two-way control-flow signal, the same
|
|
23
|
+
* convention as `Effect.loop`/`Effect.iterate`'s predecessors in fp-ts (`chainRec`). Failures travel in the
|
|
24
|
+
* effect's error channel as usual.
|
|
25
|
+
*/
|
|
26
|
+
loop: <Interstitial, Final, E>(first: Effect.Effect<Interstitial, E>, next: (result: Interstitial) => Either.Either<Effect.Effect<Interstitial, E>, Final>) => Effect.Effect<Final, E>;
|
|
27
|
+
}
|
|
28
|
+
export declare const EffectExtensions: EffectExtensionsInterface;
|
|
29
|
+
//# sourceMappingURL=EffectExtensions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EffectExtensions.d.ts","sourceRoot":"","sources":["../../src/effect/EffectExtensions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,MAAM,EAAE,MAAM,EAAQ,MAAM,QAAQ,CAAC;AAGrD;;;GAGG;AACH,MAAM,WAAW,yBAAyB;IACzC,2GAA2G;IAC3G,yBAAyB,EAAE,CAAC,CAAC,EAC5B,KAAK,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,KACnB,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;IAC7B,qEAAqE;IACrE,kBAAkB,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;IACnE;;;OAGG;IACH,YAA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