Skip to content

Commit

Permalink
Merge pull request #40 from starspot/decorators-tweaks
Browse files Browse the repository at this point in the history
Decorators tweaks
  • Loading branch information
praxxis committed Nov 28, 2016
2 parents d429f97 + 7109dca commit 506b34b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/starspot-json-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { default as ResourceController, after } from "./resource-controller";
export { default as Resource, attribute, writable, readOnly, attributes, writableAttributes, hasOne } from "./resource";
export { default as Resource, attribute, writable, updatable, creatable, readOnly, attributes, writableAttributes, updatableAttributes, creatableAttributes, hasOne } from "./resource";
export { default as Serializer } from "./serializer";
export { default as JSONAPI } from "./json-api";
export { JSONAPIError } from "./exceptions";
export { JSONAPIError } from "./exceptions";
14 changes: 10 additions & 4 deletions packages/starspot-json-api/src/resource/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@ export function creatable(resource: Resource<any>, attribute: string) {
fieldFor(resource, attribute, AttributeField).creatable = true;
}

export function readOnly(resource: Resource<any> | any, attribute?: string | any): any {
export interface ReadOnlyOptions {
ignoreWrites?: boolean;
}

export function readOnly(resource: Resource<any>, attribute: string): void;
export function readOnly(options: ReadOnlyOptions): PropertyDecorator;
export function readOnly(resource: Resource<any> | ReadOnlyOptions, attribute?: string): void | PropertyDecorator {
if (arguments.length === 1) {
let options: any = resource;
let options: ReadOnlyOptions = resource;

return function (resource: Resource<any>, attribute: string) {
let field = fieldFor(resource, attribute, AttributeField);
field.writable = false;
field.ignoreUpdateErrors = options && options.ignoreWrites;
}
};
} else {
let field = fieldFor(resource, attribute, AttributeField);
field.writable = false;
Expand Down Expand Up @@ -69,4 +75,4 @@ export function hasOne(relationship: string) {
let desc = fieldFor(resourceConstructor, relationship, RelationshipField);
desc.type = "hasOne";
};
}
}

0 comments on commit 506b34b

Please sign in to comment.