-
Notifications
You must be signed in to change notification settings - Fork 9
fixing the issue that linked types are always a HalResource #25
base: master
Are you sure you want to change the base?
Changes from all commits
16aa36a
261eed3
46d5ab7
65594fb
824037c
ec82cf5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| root = true | ||
|
|
||
| [*] | ||
| indent_style = space | ||
| indent_size = 2 | ||
| charset = utf-8 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,6 @@ node_modules | |
| dist | ||
| coverage | ||
| html-report | ||
|
|
||
| *.iml | ||
| .idea/ | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| "url": "http://github.com/deblockt/hal-rest-client/issues" | ||
| }, | ||
| "dependencies": { | ||
| "@types/lodash": "^4.14.92", | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this lib very usefull?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my opinion it is. It adds the aspect of functional programming. It makes the code easier to read and to maintain. Since I'm a Java developer, this is something I miss sadly in TypeScript. I understand that you want to keep your footprint as small as possible and I can definitely rewrite the code to be imperative and if you insist, I'll do. But it'll make a tear run down my cheek. ;) |
||
| "axios": "^0.15.3", | ||
| "reflect-metadata": "^0.1.9", | ||
| "uri-templates": "^0.1.9" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import { DefaultSerializer, IJSONSerializer } from "./hal-json-serializer"; | |
| import { IHalResource, IHalResourceConstructor } from "./hal-resource-interface"; | ||
| import { HalRestClient } from "./hal-rest-client"; | ||
| import { URI } from "./uri"; | ||
| import * as _ from 'lodash'; | ||
|
|
||
| export class HalResource implements IHalResource { | ||
| public readonly links = {}; | ||
|
|
@@ -39,6 +40,13 @@ export class HalResource implements IHalResource { | |
| } | ||
| } | ||
|
|
||
| public fetchArray(params?: object, resource?: IHalResourceConstructor<this>): Promise<this[]> { | ||
| return this.restClient.fetchArray( | ||
| this.uri.fill(params as object), | ||
| resource ? resource.prototype.constructor : this.constructor as IHalResourceConstructor<this>, | ||
| ) as Promise<this[]>; | ||
| } | ||
|
|
||
| /** | ||
| * to clear value use null not undefined | ||
| */ | ||
|
|
@@ -133,11 +141,8 @@ export class HalResource implements IHalResource { | |
|
|
||
| for (const prop of props) { | ||
| const jsonKey = this.tsProptoHalProd(prop) ; | ||
| if (this.props[prop] !== undefined && this.props[prop] !== null && this.props[prop].onInitEnded !== undefined) { | ||
| json[jsonKey] = serializer.parseResource(this.props[prop]); | ||
| } else { | ||
| json[jsonKey] = serializer.parseProp(this.props[prop]); | ||
| } | ||
|
|
||
| json[jsonKey] = this.serializeProperty(prop, serializer); | ||
| } | ||
|
|
||
| for (const link of links) { | ||
|
|
@@ -147,4 +152,23 @@ export class HalResource implements IHalResource { | |
|
|
||
| return json; | ||
| } | ||
|
|
||
| private serializeProperty(prop, serializer: IJSONSerializer, arrayItem?: any) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is a correct place for this method.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just an extension to Line 143 of the previous version corresponds to line 161. I used some helper methods of lodash to improve readability. |
||
| let result = null; | ||
|
|
||
| const property = arrayItem || this.props[prop]; | ||
|
|
||
| if (!_.isEmpty(property) && _.isFunction(property.onInitEnded)) { | ||
| result = serializer.parseResource(property) | ||
| } else if (_.isArray(property)) { | ||
| result = _(property) | ||
| .map(item => this.serializeProperty(prop, serializer, item)) | ||
| .toArray() | ||
| .value() | ||
| } else { | ||
| result = serializer.parseProp(property) | ||
| } | ||
|
|
||
| return result | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import {HalResource} from '../../hal-resource'; | ||
| import {HalProperty} from '../../hal-decorator'; | ||
|
|
||
| export class ArrayResourceItem extends HalResource { | ||
|
|
||
| @HalProperty() | ||
| public id: Number; | ||
|
|
||
| } | ||
|
|
||
| export class ArrayResource extends HalResource { | ||
|
|
||
| @HalProperty('items', ArrayResourceItem) | ||
| public items: ArrayResourceItem[]; | ||
|
|
||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file must no be on the project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can remove it. But I suggest you think about using EditorConfig as is makes collaborating way more easier. I use it in my projects all the time. And I guess it's some kind of common sense.
Have a look here: http://editorconfig.org/