Skip to content

Commit 59db50d

Browse files
committed
Simplifies Jsona’s serialize method arguments and return value. Adds simple PropertyMappers (replacement for EntitiesFactory). Adds tests for ModelsSerializer.
1 parent 6223dff commit 59db50d

20 files changed

+1075
-737
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
lib
2-
node_modules
2+
node_modules
3+
npm-debug.log
4+
.idea
5+
typings

package.json

+32-30
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,7 @@
11
{
22
"name": "jsona",
3-
"description": "Framework agnostic library that provide systemized way to work with JSON API spetification v1.0 in your JavaScript / TypeScript code",
3+
"description": "Framework agnostic library that provide systemized way to work with JSON API specification v1.0 in your JavaScript / TypeScript code",
44
"version": "0.2.3",
5-
"main": "lib/index.js",
6-
"typings": "lib/index",
7-
"files": [
8-
"lib/"
9-
],
10-
"devDependencies": {
11-
"typescript": ">=1.6.2",
12-
"typings": "^0.8.1"
13-
},
14-
"scripts": {
15-
"build": "./node_modules/.bin/tsc"
16-
},
17-
"jest": {
18-
"scriptPreprocessor": "<rootDir>/ts-preprocessor.js",
19-
"testFileExtensions": [
20-
"ts",
21-
"js"
22-
],
23-
"moduleFileExtensions": [
24-
"ts",
25-
"js"
26-
]
27-
},
28-
"repository": {
29-
"type": "git",
30-
"url": "https://github.com/olosegres/jsona-js"
31-
},
325
"keywords": [
336
"json api",
347
"json-api",
@@ -37,7 +10,36 @@
3710
"json",
3811
"api"
3912
],
40-
"author": "Sergei Solo",
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/olosegres/jsona-js"
16+
},
17+
"bugs": {
18+
"url": "https://github.com/olosegres/jsona-js/issues"
19+
},
20+
"homepage": "https://github.com/olosegres/jsona-js#readme",
21+
"author": "Sergei Solo <[email protected]>",
4122
"license": "MIT",
42-
"dependencies": {}
23+
"main": "lib/index.js",
24+
"typings": "lib/index",
25+
"files": [
26+
"src",
27+
"lib"
28+
],
29+
"dependencies": {},
30+
"devDependencies": {
31+
"chai": "^4.0.0",
32+
"mocha": "^3.4.2",
33+
"ts-mocha": "^1.0.3",
34+
"typescript": ">=1.6.2",
35+
"typings": "^0.8.1"
36+
},
37+
"scripts": {
38+
"clean": "rm lib/*",
39+
"compile": "./node_modules/.bin/tsc",
40+
"precompile": "npm run clean",
41+
"preversion": "npm run compile && npm test",
42+
"test": "env NODE_ENV=test ts-mocha src src/**/*.test.ts",
43+
"typings": "typings"
44+
}
4345
}

src/Jsona.ts

+59-102
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,85 @@
1-
'use strict';
1+
import {
2+
ISerializePropertiesMapper,
3+
IDeserializePropertiesMapper,
4+
TJsonaDenormalizedIncludeNames,
5+
TJsonaNormalizedIncludeNamesTree,
6+
TJsonaModel,
7+
TJsonApiBody
8+
} from './JsonaTypes';
9+
10+
import {jsonParse} from './utils';
11+
import ModelsSerializer from './builders/ModelsSerializer';
12+
import JsonDeserializer from './builders/JsonDeserializer';
213

314
import {
4-
IJsonaModel,
5-
IJsonaSerializeParams,
6-
IJsonaModelsFactory,
7-
IJsonaIncludeTree,
8-
IJsonaRequestedFields,
9-
IJsonApiBody
10-
} from './JsonaInterfaces';
11-
12-
import JsonBuilder from './helpers/JsonBuilder';
13-
import ModelBuilder from './helpers/ModelBuilder';
14-
import SimpleModelsFactory from './helpers/SimpleModelsFactory';
15-
import isIncludeTree from './helpers/isIncludeTree';
15+
SerializePropertiesMapper,
16+
DeserializePropertiesMapper
17+
} from './simplePropertyMappers';
1618

1719
class Jsona {
1820

19-
protected modelsFactory: IJsonaModelsFactory;
20-
protected propertiesMapper: IJsonaModel;
21+
public serializePropertiesMapper: ISerializePropertiesMapper;
22+
public deserializePropertiesMapper: IDeserializePropertiesMapper;
2123

22-
/**
23-
* Create a Jsona Service instance
24-
* @param {IJsonaModelsFactory} modelsFactory - The factory, that will provide instance for each of JSON API entities,
25-
* that will be serialized or deserialized
26-
*/
27-
constructor(
28-
modelsFactory: IJsonaModelsFactory = new SimpleModelsFactory()
29-
) {
30-
this.modelsFactory = modelsFactory;
31-
}
24+
constructor({
25+
serializePropertiesMapper = new SerializePropertiesMapper(),
26+
deserializePropertiesMapper = new DeserializePropertiesMapper()
27+
}) {
28+
this.serializePropertiesMapper = serializePropertiesMapper;
29+
this.deserializePropertiesMapper = deserializePropertiesMapper;
30+
}
3231

3332
/**
34-
* Serialize models, and additional data to JSON API compatible object.
35-
* @param {IJsonaModel} params.item - Filled with data model,
36-
that will be used to build a "data" and "include" parts.
37-
* @param {Array<IJsonaModel>} params.collection - Array of filled with data models,
38-
* that will be used to build a "data" and "include" parts.
39-
* @param {Object} params.meta - any JSON-compatible data
40-
* @param {Object} params.error - any JSON-compatible data
41-
* @param {IJsonaIncludeTree} params.requestedIncludes - object must describe what data should be put in "included" part of JSON.
42-
* @param {IJsonaRequestedFields} params.requestedFields - object must describe what attributes of entities should be put
43-
* in "data" and "included" parts of JSON.
44-
* @param {boolean} params.stringify - Stringify results object or not.
45-
* @return {IJsonApiBody}
33+
* serialize
34+
* Creates JSON, compatible with json:api specification from Jsona model(s).
4635
*/
47-
serialize(params: IJsonaSerializeParams): IJsonApiBody {
48-
var item: IJsonaModel = params.item;
49-
var collection: IJsonaModel[] = params.collection;
50-
var meta: Object = params.meta;
51-
var error: Object = params.error;
52-
var requestedIncludes: IJsonaIncludeTree = params.requestedIncludes;
53-
var withAllIncludes: boolean = params.withAllIncludes;
54-
var requestedFields: IJsonaRequestedFields = params.requestedFields;
55-
56-
var jsonBuilder = new JsonBuilder();
57-
58-
!!requestedFields && jsonBuilder.setRequestedFields(requestedFields);
59-
!!requestedIncludes && jsonBuilder.setRequestedIncludesTree(requestedIncludes);
60-
!!withAllIncludes && jsonBuilder.setWithAllIncludes(withAllIncludes);
61-
62-
!!item && jsonBuilder.setItem(item);
63-
!!collection && jsonBuilder.setCollection(collection);
64-
!!meta && jsonBuilder.setMeta(meta);
65-
!!error && jsonBuilder.setError(error);
66-
67-
return jsonBuilder.buildBody();
68-
}
69-
70-
deserialize(body: IJsonApiBody | string) {
71-
var modelBuilder = new ModelBuilder();
72-
73-
if (typeof body === 'string') {
74-
modelBuilder.setJsonParsedObject(this.jsonParse(body));
75-
} else {
76-
modelBuilder.setJsonParsedObject(body);
36+
serialize(
37+
{stuff, includeNames}: {
38+
stuff: TJsonaModel | Array<TJsonaModel>,
39+
includeNames: TJsonaDenormalizedIncludeNames | TJsonaNormalizedIncludeNamesTree
7740
}
78-
79-
modelBuilder.setModelsFactory(this.modelsFactory);
80-
81-
var deserialized = {
82-
hasItem: false,
83-
hasCollection: false,
84-
item: null,
85-
collection: null
86-
};
87-
88-
if (modelBuilder.hasItem()) {
89-
deserialized['hasItem'] = true;
90-
deserialized['item'] = modelBuilder.buildItem();
91-
} else if (modelBuilder.hasCollection()) {
92-
deserialized['hasCollection'] = true;
93-
deserialized['collection'] = modelBuilder.buildCollection();
41+
): TJsonApiBody {
42+
if (!stuff) {
43+
throw new Error('Jsona can not serialize, stuff is not passed');
44+
}
45+
if (!this.serializePropertiesMapper) {
46+
throw new Error('Jsona can not serialize, serializePropertiesMapper is not set');
9447
}
9548

96-
return deserialized;
97-
}
49+
const jsonBuilder = new ModelsSerializer(this.serializePropertiesMapper);
9850

99-
jsonParse(stringified: string): Object {
100-
var parsed;
51+
jsonBuilder.setStuff(stuff);
10152

102-
try {
103-
parsed = JSON.parse(stringified);
104-
} catch (e) {
105-
parsed = {};
53+
if (includeNames) {
54+
jsonBuilder.setIncludeNames(includeNames);
10655
}
10756

108-
return parsed;
57+
return jsonBuilder.build();
10958
}
11059

111-
jsonStringify(object: string): Object {
112-
var stringified;
60+
/**
61+
* deserialize
62+
* Creates Jsona model(s) from JSON, compatible with json:api specification.
63+
*/
64+
deserialize(body: TJsonApiBody | string): TJsonaModel | Array<TJsonaModel> {
65+
if (!body) {
66+
throw new Error('Jsona can not deserialize, body is not passed');
67+
}
68+
if (!this.deserializePropertiesMapper) {
69+
throw new Error('Jsona can not deserialize, deserializePropertiesMapper is not set');
70+
}
71+
72+
const modelBuilder = new JsonDeserializer(this.deserializePropertiesMapper);
11373

114-
try {
115-
stringified = JSON.stringify(object);
116-
} catch (e) {
117-
stringified = '{}';
74+
if (typeof body === 'string') {
75+
modelBuilder.setJsonParsedObject(jsonParse(body));
76+
} else {
77+
modelBuilder.setJsonParsedObject(body);
11878
}
11979

120-
return stringified;
80+
return modelBuilder.build();
12181
}
12282

123-
isIncludeTree(some: any): boolean {
124-
return isIncludeTree(some);
125-
}
12683
}
12784

12885
export default Jsona;

src/JsonaInterfaces.ts

-64
This file was deleted.

0 commit comments

Comments
 (0)