@trayio/commons 4.36.1-unstable → 4.37.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.
@@ -0,0 +1,16 @@
1
+ import * as E from 'fp-ts/lib/Either';
2
+ import { Codec } from './Codec';
3
+ /**
4
+ * Codec that removes the _tag property from the top level of the object.
5
+ * We use this to expose RequestFailed on APIs as the _tag property is an implementation detail.
6
+ * Note that _tag in nested objects will not be removed. This is necessary to preserve union types.
7
+ */
8
+ export declare class RemoveTopLevelTagCodec implements Codec<unknown> {
9
+ static instance: Codec<unknown>;
10
+ encode(value: unknown): unknown;
11
+ decode(value: unknown): E.Either<Error, unknown>;
12
+ private isObject;
13
+ private removeTopLevelTagIfPresent;
14
+ private removeNullFromObjectIfPresent;
15
+ }
16
+ //# sourceMappingURL=RemoveTopLevelTagCodec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RemoveTopLevelTagCodec.d.ts","sourceRoot":"","sources":["../../src/codec/RemoveTopLevelTagCodec.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,kBAAkB,CAAC;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC;;;;GAIG;AACH,qBAAa,sBAAuB,YAAW,KAAK,CAAC,OAAO,CAAC;IAC5D,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,OAAO,CAAC,CAAgC;IAE/D,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO;IAI/B,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC;IAIhD,OAAO,CAAC,QAAQ;IAIhB,OAAO,CAAC,0BAA0B;IAOlC,OAAO,CAAC,6BAA6B;CAQrC"}
@@ -0,0 +1,59 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.RemoveTopLevelTagCodec = void 0;
27
+ const E = __importStar(require("fp-ts/lib/Either"));
28
+ /**
29
+ * Codec that removes the _tag property from the top level of the object.
30
+ * We use this to expose RequestFailed on APIs as the _tag property is an implementation detail.
31
+ * Note that _tag in nested objects will not be removed. This is necessary to preserve union types.
32
+ */
33
+ class RemoveTopLevelTagCodec {
34
+ static instance = new RemoveTopLevelTagCodec();
35
+ encode(value) {
36
+ return this.removeTopLevelTagIfPresent(value);
37
+ }
38
+ decode(value) {
39
+ return E.right(this.removeTopLevelTagIfPresent(value));
40
+ }
41
+ isObject(value) {
42
+ return typeof value === 'object' && !Array.isArray(value) && value !== null;
43
+ }
44
+ removeTopLevelTagIfPresent(value) {
45
+ if (this.isObject(value)) {
46
+ return this.removeNullFromObjectIfPresent(value);
47
+ }
48
+ return value;
49
+ }
50
+ removeNullFromObjectIfPresent(value) {
51
+ return Object.entries(value).reduce((acc, [key, propertyValue]) => {
52
+ if (key === '_tag') {
53
+ return acc;
54
+ }
55
+ return { ...acc, [key]: propertyValue };
56
+ }, {});
57
+ }
58
+ }
59
+ exports.RemoveTopLevelTagCodec = RemoveTopLevelTagCodec;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=RemoveTopLevelTagCodec.unit.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RemoveTopLevelTagCodec.unit.test.d.ts","sourceRoot":"","sources":["../../src/codec/RemoveTopLevelTagCodec.unit.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const RemoveTopLevelTagCodec_1 = require("./RemoveTopLevelTagCodec");
4
+ const testRemoveTopLevelTagCodec = RemoveTopLevelTagCodec_1.RemoveTopLevelTagCodec.instance;
5
+ describe('RemoveTopLevelTagCodec', () => {
6
+ test('should remove top level tag only', () => {
7
+ const value = {
8
+ _tag: 'SomeTag',
9
+ message: 'blue',
10
+ data: {
11
+ _tag: 'SomeOtherTag',
12
+ value: 'yellow',
13
+ },
14
+ };
15
+ const actualValue = testRemoveTopLevelTagCodec.encode(value);
16
+ const expectedValue = {
17
+ message: 'blue',
18
+ data: {
19
+ _tag: 'SomeOtherTag',
20
+ value: 'yellow',
21
+ },
22
+ };
23
+ expect(actualValue).toEqual(expectedValue);
24
+ });
25
+ test('should succeed if _tag property not present at top level', () => {
26
+ const value = {
27
+ message: 'blue',
28
+ data: {
29
+ _tag: 'SomeOtherTag',
30
+ value: 'yellow',
31
+ },
32
+ };
33
+ const actualValue = testRemoveTopLevelTagCodec.encode(value);
34
+ const expectedValue = {
35
+ message: 'blue',
36
+ data: {
37
+ _tag: 'SomeOtherTag',
38
+ value: 'yellow',
39
+ },
40
+ };
41
+ expect(actualValue).toEqual(expectedValue);
42
+ });
43
+ });
@@ -1 +1 @@
1
- {"version":3,"file":"Error.d.ts","sourceRoot":"","sources":["../../src/error/Error.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,KASvC,CAAC"}
1
+ {"version":3,"file":"Error.d.ts","sourceRoot":"","sources":["../../src/error/Error.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,KAWvC,CAAC"}
@@ -8,6 +8,9 @@ const castError = (error) => {
8
8
  if (error instanceof Error) {
9
9
  return error;
10
10
  }
11
- return new Error('Internal Error');
11
+ if (typeof error === 'object') {
12
+ return new Error(`Internal error: ${JSON.stringify(error)}`);
13
+ }
14
+ return new Error('Unknown internal error');
12
15
  };
13
16
  exports.castError = castError;
@@ -1,7 +1,16 @@
1
- import { TaskEither } from 'fp-ts/TaskEither';
1
+ import * as TE from 'fp-ts/TaskEither';
2
+ import * as TSK from 'fp-ts/Task';
2
3
  import { Either } from 'fp-ts/lib/Either';
3
- export declare const createTaskEitherFromPromise: <E, T>(block: () => Promise<T>, errorHandling: (error: Error) => E) => TaskEither<E, T>;
4
- export declare const createTaskEither: <E, T>(block: () => Promise<Either<E, T>>) => TaskEither<E, T>;
5
- export declare const createTaskEitherFromEither: <E, T>(either: Either<E, T>) => TaskEither<E, T>;
6
- export declare const createTaskEitherFromPromiseWithSimpleError: <T>(block: () => Promise<T>) => TaskEither<Error, T>;
4
+ export interface TaskExtensionsInterface {
5
+ recover: <T>(task: TSK.Task<T>, onError: (error: Error) => T) => TSK.Task<T>;
6
+ }
7
+ export declare const TaskExtensions: TaskExtensionsInterface;
8
+ export interface TaskEitherExtensionsInterface {
9
+ fromPromiseWithSimpleError: <T>(block: () => Promise<T>) => TE.TaskEither<Error, T>;
10
+ }
11
+ export declare const TaskEitherExtensions: TaskEitherExtensionsInterface;
12
+ export declare const createTaskEitherFromPromise: <E, T>(block: () => Promise<T>, errorHandling: (error: Error) => E) => TE.TaskEither<E, T>;
13
+ export declare const createTaskEither: <E, T>(block: () => Promise<Either<E, T>>) => TE.TaskEither<E, T>;
14
+ export declare const createTaskEitherFromEither: <E, T>(either: Either<E, T>) => TE.TaskEither<E, T>;
15
+ export declare const createTaskEitherFromPromiseWithSimpleError: <T>(block: () => Promise<T>) => TE.TaskEither<Error, T>;
7
16
  //# sourceMappingURL=Task.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Task.d.ts","sourceRoot":"","sources":["../../src/task/Task.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAY,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAG1C,eAAO,MAAM,2BAA2B,EAAE,CAAC,CAAC,EAAE,CAAC,EAC9C,KAAK,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACvB,aAAa,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,CAAC,KAC9B,UAAU,CAAC,CAAC,EAAE,CAAC,CACwC,CAAC;AAE7D,eAAO,MAAM,gBAAgB,EAAE,CAAC,CAAC,EAAE,CAAC,EACnC,KAAK,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAC9B,UAAU,CAAC,CAAC,EAAE,CAAC,CAAoB,CAAC;AAGzC,eAAO,MAAM,0BAA0B,EAAE,CAAC,CAAC,EAAE,CAAC,EAC7C,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,KAChB,UAAU,CAAC,CAAC,EAAE,CAAC,CAC4B,CAAC;AAEjD,eAAO,MAAM,0CAA0C,EAAE,CAAC,CAAC,EAC1D,KAAK,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,KACnB,UAAU,CAAC,KAAK,EAAE,CAAC,CACqB,CAAC"}
1
+ {"version":3,"file":"Task.d.ts","sourceRoot":"","sources":["../../src/task/Task.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACvC,OAAO,KAAK,GAAG,MAAM,YAAY,CAAC;AAClC,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAG1C,MAA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/task/Task.js CHANGED
@@ -1,14 +1,53 @@
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
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createTaskEitherFromPromiseWithSimpleError = exports.createTaskEitherFromEither = exports.createTaskEither = exports.createTaskEitherFromPromise = void 0;
4
- const TaskEither_1 = require("fp-ts/TaskEither");
26
+ exports.createTaskEitherFromPromiseWithSimpleError = exports.createTaskEitherFromEither = exports.createTaskEither = exports.createTaskEitherFromPromise = exports.TaskEitherExtensions = exports.TaskExtensions = void 0;
27
+ const TE = __importStar(require("fp-ts/TaskEither"));
5
28
  const Error_1 = require("../error/Error");
6
- const createTaskEitherFromPromise = (block, errorHandling) => (0, TaskEither_1.tryCatch)(block, (error) => errorHandling((0, Error_1.castError)(error)));
29
+ exports.TaskExtensions = {
30
+ recover: (task, onError) => async () => {
31
+ try {
32
+ return await task();
33
+ }
34
+ catch (error) {
35
+ return onError((0, Error_1.castError)(error));
36
+ }
37
+ },
38
+ };
39
+ exports.TaskEitherExtensions = {
40
+ fromPromiseWithSimpleError: (block) => TE.tryCatch(block, (error) => (0, Error_1.castError)(error)),
41
+ };
42
+ // TODO: Remove, use TE.tryCatch
43
+ const createTaskEitherFromPromise = (block, errorHandling) => TE.tryCatch(block, (error) => errorHandling((0, Error_1.castError)(error)));
7
44
  exports.createTaskEitherFromPromise = createTaskEitherFromPromise;
45
+ // TODO: Remove, use TE.tryCatch
8
46
  const createTaskEither = (block) => block;
9
47
  exports.createTaskEither = createTaskEither;
10
48
  // TODO: Remove, we can use TE.fromEither (double check that the other functions are necessary)
11
49
  const createTaskEitherFromEither = (either) => (0, exports.createTaskEither)(() => Promise.resolve(either));
12
50
  exports.createTaskEitherFromEither = createTaskEitherFromEither;
13
- const createTaskEitherFromPromiseWithSimpleError = (block) => (0, TaskEither_1.tryCatch)(block, (error) => (0, Error_1.castError)(error));
51
+ // TODO: Remove, use the method on the interface
52
+ const createTaskEitherFromPromiseWithSimpleError = (block) => TE.tryCatch(block, (error) => (0, Error_1.castError)(error));
14
53
  exports.createTaskEitherFromPromiseWithSimpleError = createTaskEitherFromPromiseWithSimpleError;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/commons",
3
- "version": "4.36.1-unstable",
3
+ "version": "4.37.0",
4
4
  "description": "Extensions to the standard/core libraries and basic features",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"