8000 Fix #10 - Before and After serialize / deserialize hooks · Wikodit/js-data-jsonapi-light@30701bf · GitHub
[go: up one dir, main page]

Skip to content

Commit 30701bf

Browse files
committed
Fix #10 - Before and After serialize / deserialize hooks
1 parent 5f60bbe commit 30701bf

9 files changed

+260
-39
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![NPM][npmB]][npmL] [![CircleCI build][circleciMB]][circleciML] [![CircleCI build develop][circleciB]][circleciL] [![Codecov][codecovB]][codecovL] [![Issues][issuesB]][issuesL]
44

55
[npmB]: https://img.shields.io/npm/v/js-data-jsonapi-light.svg?style=flat
6-
[npmL]: https://www.npmjs.org/package/js-data-json-light
6+
[npmL]: https://www.npmjs.org/package/js-data-jsonapi-light
77
[issuesB]: https://img.shields.io/github/issues/Wikodit/js-data-jsonapi-light.svg
88
[issuesL]: https://github.com/Wikodit/js-data-jsonapi-light/issues
99
[circleciB]: https://img.shields.io/circleci/project/Wikodit/js-data-jsonapi-light/develop.svg?style=flat&label=develop

dist/js-data-jsonapi-light.js

Lines changed: 43 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js-data-jsonapi-light.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Adapter } from 'js-data-adapter';
77
export class JsonApiAdapter extends HttpAdapter{
88
adapter: Adapter;
99
private store: DataStore;
10+
private options: any;
1011

1112
constructor(options?:any) {
1213
options = utils.deepMixIn({
@@ -19,32 +20,39 @@ export class JsonApiAdapter extends HttpAdapter{
1920
throw new Error('JsonApiAdapter needs to be given a store option.')
2021
}
2122

22-
if (options.serialize) {
23-
options.beforeSerialize = options.serialize
24-
}
25-
26-
if (options.deserialize) {
27-
options.afterDeserialize = options.deserialize
28-
}
29-
3023
var selfWrapper:any = {};
3124

3225
options.serialize = function(wrapper:any):any{
33-
return function(){
34-
return wrapper.self.jsonApiSerialize.apply(wrapper.self, arguments);
26+
return function(mapper:any, data:any, opts:any){
27+
let
28+
beforeSerialize = opts.beforeSerialize || mapper.beforeSerialize || wrapper.self.options.beforeSerialize,
29+
afterSerialize = opts.afterSerialize || mapper.afterSerialize || wrapper.self.options.afterSerialize;
30+
31+
if (beforeSerialize) data = beforeSerialize.call(wrapper.self, mapper, data, opts);
32+
data = wrapper.self.jsonApiSerialize.call(wrapper.self, mapper, data, opts);
33+
if (afterSerialize) data = afterSerialize.call(wrapper.self, mapper, data, opts);
34+
return data;
3535
}
3636
}(selfWrapper);
3737

3838
options.deserialize = function(wrapper:any):any{
39-
return function(){
40-
return wrapper.self.jsonApiDeserialize.apply(wrapper.self, arguments);
39+
return function(mapper:any, res:any, opts:any){
40+
let
41+
beforeDeserialize = opts.beforeDeserialize || mapper.beforeDeserialize || wrapper.self.options.beforeDeserialize,
42+
afterDeserialize = opts.afterDeserialize || mapper.afterDeserialize || wrapper.self.options.afterDeserialize;
43+
44+
if (beforeDeserialize) res = beforeDeserialize.call(wrapper.self, mapper, res, opts);
45+
res = wrapper.self.jsonApiDeserialize.call(wrapper.self, mapper, res, opts);
46+
if (afterDeserialize) res = afterDeserialize.call(wrapper.self, mapper, res, opts);
47+
return res;
4148
}
4249
}(selfWrapper);
4350

4451
super(options);
4552

4653
selfWrapper.self = this;
4754
this.store = options.store;
55+
this.options = options;
4856
}
4957

5058
private warn(...args:any[]) {
@@ -218,16 +226,30 @@ export class JsonApiAdapter extends HttpAdapter{
218226
return response;
219227
}}
220228

229+
private handleBeforeLifecycle (opts?:any): Promise<void> {
230+
if(opts && (opts.serialize || opts.deserialize)) {
231+
return Promise.reject(new Error('You can not use deserialize and serialize options with this adapter, you should instead provide an afterSerialize, a beforeSerialize, an afterDeserialize or a beforeDeserialize.'))
232+
}
233+
234+
return Promise.resolve();
235+
}
236+
221237
public find(mapper: Mapper, id: string | number, opts?: any): Promise<any> {
222-
return super.find(mapper, id, opts).then(this.handleResponse(opts))
238+
return this.handleBeforeLifecycle(opts).then(() => {
239+
return HttpAdapter.prototype.find.call(this, mapper, id, opts);
240+
}).then(this.handleResponse(opts));
223241
}
224242

225243
public findAll(mapper: Mapper, query?: any, opts?: any): Promise<any> {
226-
return super.findAll(mapper, query, opts).then(this.handleResponse(opts))
244+
return this.handleBeforeLifecycle(opts).then(() => {
245+
return HttpAdapter.prototype.findAll.call(this, mapper, query, opts);
246+
}).then(this.handleResponse(opts));
227247
}
228248

229249
public create(mapper: Mapper, props: any, opts?: any): Promise<any> {
230-
return super.create(mapper, props, opts).then(this.handleResponse(opts))
250+
return this.handleBeforeLifecycle(opts).then(() => {
251+
return HttpAdapter.prototype.create.call(this, mapper, props, opts);
252+
}).then(this.handleResponse(opts))
231253
}
232254

233255
public createMany(mapper: Mapper, props: any, opts?: any): Promise<any> {
@@ -238,7 +260,9 @@ export class JsonApiAdapter extends HttpAdapter{
238260
// Ensure id is properly set
239261
props[mapper.idAttribute] = id;
240262

241-
return super.update(mapper, id, props, opts).then(this.handleResponse(opts))
263+
return this.handleBeforeLifecycle(opts).then(() => {
264+
return HttpAdapter.prototype.update.call(this, mapper, id, props, opts)
265+
}).then(this.handleResponse(opts))
242266
}
243267

244268
public updateAll(mapper: Mapper, props: any, query: any, opts?: any): Promise<any> {
@@ -250,7 +274,9 @@ export class JsonApiAdapter extends HttpAdapter{
250274
}
251275

252276
public destroy(mapper: Mapper, id: string | number, opts?: any): Promise<any> {
253-
return super.destroy(mapper, id, opts).then(this.handleResponse(opts))
277+
return this.handleBeforeLifecycle(opts).then(() => {
278+
return HttpAdapter.prototype.destroy.call(this, mapper, id, opts);
279+
}).then(this.handleResponse(opts))
254280
}
255281

256282
public destroyAll(mapper: Mapper, query: any, opts?: any): Promise<any> {

test/unit/crud/create.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import * as sinon from 'sinon';
22
import { expect } from 'chai';
33
import { store } from '../../ds';
44
import * as Resources from '../../resources';
5-
import { respondJson } from './lib';
5+
import { respondJson } from '../lib';
66

77
describe('CREATE', () => {
88
let requests:Array<any> = [];
9-
let server:any = null
9+
let server:any = null;
1010

1111
beforeEach(() => {
1212
server = sinon.fakeServer.create();

test/unit/crud/destroy.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as sinon from 'sinon';
22
import { expect } from 'chai';
33
import { store } from '../../ds';
44
import * as Resources from '../../resources';
5-
import { respondJson } from './lib';
5+
import { respondJson } from '../lib';
66

77
describe('DESTROY', () => {
88
let requests:Array<any> = [];

test/unit/crud/update.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as sinon from 'sinon';
22
import { expect } from 'chai';
33
import { store } from '../../ds';
44
import * as Resources from '../../resources';
5-
import { respondJson } from './lib';
5+
import { respondJson } from '../lib';
66

77
describe('UPDATE', () => {
88
let requests:Array<any> = [];
@@ -116,7 +116,7 @@ describe('UPDATE', () => {
116116
expect(reqPut.body.data.relationships.author).to.deep.equal({
117117
data: { type: "User", id: UPDATE_PARAMS.authorId }
118118
});
119-
119+
120120
expect(data).to.be.an('object')
121121
expect(data.id).to.equal(ID)
122122
expect(data.title).to.equal(UPDATE_PARAMS.title)
File renamed without changes.

0 commit comments

Comments
 (0)
0