1+ 'use strict' ;
2+
3+ Object . defineProperty ( exports , "__esModule" , {
4+ value : true
5+ } ) ;
6+
7+ var _typeof = typeof Symbol === "function" && typeof Symbol . iterator === "symbol" ? function ( obj ) { return typeof obj ; } : function ( obj ) { return obj && typeof Symbol === "function" && obj . constructor === Symbol && obj !== Symbol . prototype ? "symbol" : typeof obj ; } ;
8+
9+ var _createClass = function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ( ) ;
10+
11+ var _helpers = require ( '../Support/helpers' ) ;
12+
13+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
14+
15+ var Repository = function ( ) {
16+ function Repository ( definition ) {
17+ _classCallCheck ( this , Repository ) ;
18+
19+ this . $sort = [ ] ;
20+ this . $search = [ ] ;
21+ this . $match = [ ] ;
22+ this . $related = [ ] ;
23+ this . $actions = [ ] ;
24+
25+ if ( typeof definition === 'string' ) {
26+ return this . uriKey = definition ;
27+ }
28+
29+ if ( ( typeof definition === 'undefined' ? 'undefined' : _typeof ( definition ) ) === 'object' ) {
30+ if ( ! definition . hasOwnProperty ( 'uriKey' ) ) {
31+ throw new Error ( 'Invalid repository definition.' ) ;
32+ }
33+
34+ return this . setUriKey ( definition . uriKey ) . setName ( definition . name ) . setSorts ( definition . sort ) . setMatches ( definition . match ) . setRelated ( definition . related ) . setSearcheables ( definition . searchables ) . setActions ( definition . actions ) ;
35+ }
36+ }
37+
38+ _createClass ( Repository , [ {
39+ key : 'setUriKey' ,
40+ value : function setUriKey ( uriKey ) {
41+ this . uriKey = uriKey ;
42+
43+ return this ;
44+ }
45+ } , {
46+ key : 'setName' ,
47+ value : function setName ( name ) {
48+ this . name = name ;
49+
50+ return this ;
51+ }
52+ } , {
53+ key : 'setConfig' ,
54+ value : function setConfig ( config ) {
55+ this . config = config ;
56+
57+ return this ;
58+ }
59+ } , {
60+ key : 'setRequest' ,
61+ value : function setRequest ( request ) {
62+ this . request = request ;
63+
64+ return this ;
65+ }
66+ } , {
67+ key : 'sorts' ,
68+ value : function sorts ( ) {
69+ return this . $sort || [ ] ;
70+ }
71+ } , {
72+ key : 'matches' ,
73+ value : function matches ( ) {
74+ return this . $match || [ ] ;
75+ }
76+ } , {
77+ key : 'searchables' ,
78+ value : function searchables ( ) {
79+ return this . $search || [ ] ;
80+ }
81+ } , {
82+ key : 'related' ,
83+ value : function related ( ) {
84+ var key = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : null ;
85+
86+ return this . $related || [ ] ;
87+ }
88+ } , {
89+ key : 'setSorts' ,
90+ value : function setSorts ( sort ) {
91+ this . $sort = sort ;
92+
93+ return this ;
94+ }
95+ } , {
96+ key : 'setMatches' ,
97+ value : function setMatches ( match ) {
98+ this . $match = match ;
99+
100+ return this ;
101+ }
102+ } , {
103+ key : 'setSearcheables' ,
104+ value : function setSearcheables ( search ) {
105+ this . $search = search ;
106+
107+ return this ;
108+ }
109+ } , {
110+ key : 'setActions' ,
111+ value : function setActions ( actions ) {
112+ this . $actions = actions ;
113+
114+ return this ;
115+ }
116+ } , {
117+ key : 'setRelated' ,
118+ value : function setRelated ( related ) {
119+ this . $related = related ;
120+
121+ return this ;
122+ }
123+ } , {
124+ key : 'uri' ,
125+ value : function uri ( ) {
126+ var suffix = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : null ;
127+
128+ return this . config . uri ( suffix ? this . uriKey + '/' + suffix : this . uriKey ) ;
129+ }
130+ } , {
131+ key : 'get' ,
132+ value : function get ( ) {
133+ var query = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : { } ;
134+
135+ return Restify . request ( ) . get ( this . uri ( ) , {
136+ params : query
137+ } ) ;
138+ }
139+ } , {
140+ key : 'show' ,
141+ value : function show ( key ) {
142+ var query = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : { } ;
143+
144+ return Restify . request ( ) . get ( this . uri ( key ) , {
145+ params : query
146+ } ) ;
147+ }
148+ } , {
149+ key : 'store' ,
150+ value : function store ( data ) {
151+ return Restify . request ( ) . post ( this . uri ( ) , data ) ;
152+ }
153+ } , {
154+ key : 'update' ,
155+ value : function update ( key , data ) {
156+ return Restify . request ( ) . post ( this . uri ( key ) , data ) ;
157+ }
158+ } , {
159+ key : 'delete' ,
160+ value : function _delete ( key ) {
161+ return Restify . request ( ) . delete ( this . uri ( key ) ) ;
162+ }
163+ } , {
164+ key : 'itemAction' ,
165+ value : function itemAction ( id , key , data ) {
166+ return Restify . request ( ) . post ( this . uri ( id + '/actions?action=' + key ) , data ) ;
167+ }
168+ } , {
169+ key : 'action' ,
170+ value : function action ( key , data ) {
171+ var action = this . getAction ( key ) ;
172+
173+ if ( ! action ) {
174+ throw new Error ( 'Action ' + key + ' is not defined.' ) ;
175+ }
176+
177+ return Restify . request ( ) . post ( this . uri ( 'actions?action=' + key ) , data ) ;
178+ }
179+ } , {
180+ key : 'getAction' ,
181+ value : function getAction ( key ) {
182+ if ( key ) {
183+ return ( 0 , _helpers . collect ) ( this . $actions ) . firstWhere ( 'uriKey' , key ) ;
184+ }
185+
186+ return this . $actions ;
187+ }
188+ } ] , [ {
189+ key : 'make' ,
190+ value : function make ( item ) {
191+ return new this ( item ) ;
192+ }
193+ } ] ) ;
194+
195+ return Repository ;
196+ } ( ) ;
197+
198+ exports . default = Repository ;
0 commit comments