Skip to content

Commit a545a49

Browse files
committed
feat(types): toJSON() on List now return Json[].
1 parent fba7d3a commit a545a49

File tree

9 files changed

+15
-75
lines changed

9 files changed

+15
-75
lines changed

src/services/AxiosProvider.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
import axios, { AxiosError, Method } from 'axios';
2-
import {
3-
HttpStatus,
4-
HttpVerb,
5-
isRestResult,
6-
Request,
7-
RequestOptions,
8-
RequestProvider,
9-
Response,
10-
toResponse,
11-
} from '../http';
2+
import { HttpStatus, HttpVerb, isRestResult, Request, RequestOptions, RequestProvider, Response, toResponse } from '../http';
123
import { isDefined, toResult, Uri } from '../types';
134
import { choose } from '../utils';
145

src/types/List.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class List<T> extends Array<T> {
1313

1414
last = (p?: (value: T, index: number, array: T[]) => unknown, params?: unknown): T => (p ? this.filter(p, params).last() : this[this.length - 1]);
1515

16-
toJSON = (): List<Json> => this.map(i => json.parse(i));
16+
toJSON = (): Json[] => this.reduce((a, i) => { a.push(json.parse(i)); return a; }, new Array<Json>());
1717

1818
map = <U>(f: (value: T, index: number, array: T[]) => U, params?: unknown): List<U> => super.map(f, params) as List<U>;
1919

src/validation/Contraints.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
1-
import {
2-
Func,
3-
inFuture,
4-
inPast,
5-
isDefined,
6-
isFunction,
7-
isIn,
8-
isNotEmpty,
9-
isString,
10-
list,
11-
List,
12-
meta,
13-
Results,
14-
Text,
15-
} from '../types';
1+
import { Func, inFuture, inPast, isDefined, isFunction, isIn, isNotEmpty, isString, list, List, meta, Results, Text } from '../types';
162
import { validate, Validator } from './Validate';
173

184
export type Constraint = Func<boolean | Results, any>;

src/validation/When.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
import {
2-
Constructor,
3-
ErrorOrigin,
4-
Exception,
5-
Get,
6-
isDefined,
7-
isEmpty,
8-
isIn,
9-
ofGet,
10-
Predicate,
11-
Results,
12-
toArray,
13-
} from '../types';
1+
import { Constructor, ErrorOrigin, Exception, Get, isDefined, isEmpty, isIn, ofGet, Predicate, Results, toArray } from '../types';
142
import { validate } from './Validate';
153
import { reject, resolve } from '../utils';
164

test/data/CollectionGateway.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CollectionGateway, Exception, resolve, toJson } from '../../src';
1+
import { CollectionGateway, Exception, resolve, Json, toJson, toList } from '../../src';
22
import { Dev } from '../ref';
33
import '@thisisagile/easy-test';
44

@@ -7,7 +7,7 @@ describe('CollectionGateway', () => {
77
let gateway: CollectionGateway;
88

99
beforeEach(() => {
10-
gateway = new CollectionGateway(resolve(Dev.All.toJSON()));
10+
gateway = new CollectionGateway(resolve(toList<Json>(Dev.All.toJSON())));
1111
});
1212

1313
test('byId with known id returns product', () => {

test/types/Is.test.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
1-
import {
2-
Entity,
3-
isArray,
4-
isBoolean,
5-
isDefined,
6-
isEmpty,
7-
isEmptyObject,
8-
isInstance,
9-
isNotEmpty,
10-
isNumber,
11-
isObject,
12-
isPrimitive,
13-
isString,
14-
} from '../../src';
1+
import { Entity, isArray, isBoolean, isDefined, isEmpty, isEmptyObject, isInstance, isNotEmpty, isNumber, isObject, isPrimitive, isString } from '../../src';
152
import { Dev } from '../ref';
163

174
describe('isDefined', () => {

test/types/List.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ describe('List', () => {
9393

9494
test('toJSON', () => {
9595
const json = list(Dev.Sander, Dev.Wouter).toJSON();
96+
expect(json).not.toBeInstanceOf(List);
97+
expect(json).toBeInstanceOf(Array);
9698
expect(JSON.stringify(json)).toBe(JSON.stringify([Dev.Sander.toJSON(), Dev.Wouter.toJSON()]));
9799
const j = list(Scope.Auth, Scope.Basic, Scope.Admin).toJSON();
98100
expect(JSON.stringify(j)).toBe(JSON.stringify([Scope.Auth.toJSON(), Scope.Basic.toJSON(), Scope.Admin.toJSON()]));

test/validation/Constraints.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,27 @@ describe('Constraints', () => {
5858
});
5959
});
6060

61-
const is42 = (message?: Text): PropertyDecorator => constraint(v => v === 42, message ?? 'Property {property} should have value \'42\' instead of \'{actual}\'.');
61+
const is42 = (message?: Text): PropertyDecorator => constraint(v => v === 42, message ?? "Property {property} should have value '42' instead of '{actual}'.");
6262

6363
class Person extends Struct {
6464
@valid() readonly age: Age = new Age(this.state.age);
6565
@is42() readonly realAge: number = this.state.age;
6666
}
6767

6868
describe('Custom constraints', () => {
69-
70-
test('test age', () => {
69+
test('age', () => {
7170
expect(isValue(new Age(42))).toBeTruthy();
7271
expect(validate(new Age(42))).toBeValid();
7372
expect(validate(new Age(142))).not.toBeValid();
7473
});
7574

76-
test('test person', () => {
75+
test('person', () => {
7776
expect(validate(new Person({ age: 42 }))).toBeValid();
7877
expect(validate(new Person({ age: 142 }))).not.toBeValid();
7978
});
8079

81-
test('test custom validation', () => {
80+
test('custom validation', () => {
8281
const res = validate(new Person({ age: 142 }));
8382
expect(res.results).toHaveLength(2);
8483
});
85-
});
84+
});

test/validation/Validate.test.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
1-
import {
2-
asList,
3-
Entity,
4-
Enum,
5-
includes,
6-
List,
7-
required,
8-
rule,
9-
Struct,
10-
toList,
11-
valid,
12-
validate,
13-
Value,
14-
} from '../../src';
1+
import { asList, Entity, Enum, includes, List, required, rule, Struct, toList, valid, validate, Value } from '../../src';
152
import '@thisisagile/easy-test';
163
import { Dev } from '../ref';
174

0 commit comments

Comments
 (0)