UNPKG

45.2 kBJavaScriptView Raw
1import head from 'lodash/head';
2import keys from 'lodash/keys';
3import camelCase from 'lodash/camelCase';
4import concat from 'lodash/concat';
5import get from 'lodash/get';
6import isArray from 'lodash/isArray';
7import isEmpty from 'lodash/isEmpty';
8import kebabCase from 'lodash/kebabCase';
9import pullAll from 'lodash/pullAll';
10import snakeCase from 'lodash/snakeCase';
11import uniq from 'lodash/uniq';
12import falcorPathParser from 'falcor-path-syntax';
13import map from 'lodash/map';
14import isNil from 'lodash/isNil';
15import { stringify } from 'querystring';
16import filter from 'lodash/filter';
17import forEach from 'lodash/forEach';
18import isError from 'lodash/isError';
19import isFunction from 'lodash/isFunction';
20import last from 'lodash/last';
21import nth from 'lodash/nth';
22import some from 'lodash/some';
23
24var name = 'audioCategory';
25var pluralName = 'audioCategories';
26
27var defaultProperties = ['id', 'name'];
28
29var audioCategory = Object.freeze({
30 name: name,
31 pluralName: pluralName,
32 defaultProperties: defaultProperties
33});
34
35var name$1 = 'audioTrack';
36var pluralName$1 = 'audioTracks';
37
38var defaultProperties$1 = ['id', 'name', 'author', 'duration', 'src'];
39
40var audioTrack = Object.freeze({
41 name: name$1,
42 pluralName: pluralName$1,
43 defaultProperties: defaultProperties$1
44});
45
46var name$2 = 'champion';
47var pluralName$2 = 'champions';
48
49var defaultProperties$2 = ['id'];
50
51var champion = Object.freeze({
52 name: name$2,
53 pluralName: pluralName$2,
54 defaultProperties: defaultProperties$2
55});
56
57var name$3 = 'cohort';
58var pluralName$3 = 'cohorts';
59
60var defaultProperties$3 = ['id', 'name', 'description'];
61
62var cohort = Object.freeze({
63 name: name$3,
64 pluralName: pluralName$3,
65 defaultProperties: defaultProperties$3
66});
67
68var DEFAULT_FROM_INDEX = 0;
69var DEFAULT_TO_INDEX = 19;
70var MY_PATH_KEYWORD = 'self';
71
72function buildPaging(options) {
73 return {
74 from: get(options, 'paging.from') || DEFAULT_FROM_INDEX,
75 to: get(options, 'paging.to') || DEFAULT_TO_INDEX
76 };
77}
78
79function buildProperties(defaults, options) {
80 var properties = concat([], options.properties || defaults);
81
82 var toAnd = options.andProperties;
83 if (isArray(toAnd) && !isEmpty(toAnd)) {
84 properties = concat(properties, toAnd);
85 }
86
87 var toNot = options.notProperties;
88 if (isArray(toNot) && !isEmpty(toNot)) {
89 properties = pullAll(properties, toNot);
90 }
91
92 properties = uniq(properties);
93
94 return properties;
95}
96
97function entityNameToFilename(name) {
98 return kebabCase(name) + '.js';
99}
100
101function entityToAPIEnvelopeKey(entity) {
102 return snakeCase(entity.name);
103}
104
105function entityToFalcorListLeaf(entity) {
106 return snakeCase(entity.name) + '_list';
107}
108
109function entityToFalcorMembershipsListLeaf(entity) {
110 return 'membership_' + entityToFalcorListLeaf(entity);
111}
112
113function entityToFalcorMyListLeaf(entity) {
114 return 'my_' + entityToFalcorListLeaf(entity);
115}
116
117function entityToFalcorRootLeaf(entity) {
118 return snakeCase(entity.pluralName);
119}
120
121function falcorListLeafToEntityName(listLeaf) {
122 return camelCase(listLeaf.replace(/_list$/, ''));
123}
124
125function falcorRootLeafToEntityName(rootLeaf) {
126 return camelCase(rootLeaf);
127}
128
129function optionsHasQuery(options) {
130 return keys(options.query).length > 0;
131}
132
133function typedEntityToFalcorListPath(entity, options) {
134 return [snakeCase(options.type), snakeCase(entity.name) + '_list'];
135}
136
137var name$4 = 'comment';
138var pluralName$4 = 'comments';
139
140var defaultProperties$4 = ['id', 'body', 'markup_tokens', 'post', 'owner.id', 'owner.first_name', 'owner.last_name', 'owner.name', 'owner.avatar.src', 'created_at'];
141
142function invalidateAfterCreate() {
143 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
144
145 var parentEntityName = head(keys(options.parent));
146 var parentEntity = entities[parentEntityName];
147 var parentId = options.parent[parentEntityName].id;
148
149 var rootLeaf = entityToFalcorRootLeaf(parentEntity);
150
151 var paths = [[rootLeaf, parentId, 'comment_list'], [rootLeaf, parentId, 'comment_count']];
152
153 return paths;
154}
155
156// TODO: Update Falcor comment routes to follow standard entity
157// *_list and length conventions and then remove this override
158function invalidateAfterDestroy() {
159 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
160
161 var rootEntityLeaf = entityToFalcorRootLeaf({ pluralName: pluralName$4 });
162 var entityId = options.id;
163
164 var parentEntityName = head(keys(options.parent));
165 var parentEntity = entities[parentEntityName];
166 var rootParentLeaf = entityToFalcorRootLeaf(parentEntity);
167 var parentId = options.parent[parentEntityName].id;
168
169 var paths = [[rootEntityLeaf, entityId], [rootParentLeaf, parentId, 'comment_list'], [rootParentLeaf, parentId, 'comment_count']];
170
171 return paths;
172}
173
174var comment = Object.freeze({
175 name: name$4,
176 pluralName: pluralName$4,
177 defaultProperties: defaultProperties$4,
178 invalidateAfterCreate: invalidateAfterCreate,
179 invalidateAfterDestroy: invalidateAfterDestroy
180});
181
182var name$5 = 'contentListItem';
183var pluralName$5 = 'contentListItems';
184
185var defaultProperties$5 = ['id', 'content_id', 'content_type', 'new', 'position'];
186
187var contentListItem = Object.freeze({
188 name: name$5,
189 pluralName: pluralName$5,
190 defaultProperties: defaultProperties$5
191});
192
193var name$6 = 'directMessage';
194var pluralName$6 = 'directMessages';
195
196var defaultProperties$6 = ['id', 'name'];
197
198var directMessage = Object.freeze({
199 name: name$6,
200 pluralName: pluralName$6,
201 defaultProperties: defaultProperties$6
202});
203
204var name$8 = 'post';
205var pluralName$8 = 'posts';
206
207var defaultProperties$7 = ['id', 'body', 'artifact_id'];
208
209var post = Object.freeze({
210 name: name$8,
211 pluralName: pluralName$8,
212 defaultProperties: defaultProperties$7
213});
214
215var name$7 = 'feedPost';
216var pluralName$7 = 'feedPosts';
217function getMany() {
218 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
219
220 var _buildPaging = buildPaging(options),
221 from = _buildPaging.from,
222 to = _buildPaging.to;
223
224 var properties = buildProperties(defaultProperties$7, options);
225 var listLeaf = entityToFalcorListLeaf({ name: name$7 });
226
227 var paths = map(properties, function (property) {
228 var propertyPath = falcorPathParser(property);
229 var path = concat(['self', listLeaf, { from: from, to: to }], propertyPath);
230
231 return path;
232 });
233
234 return paths;
235}
236
237function extractMany() {
238 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
239 var jsonGraph = arguments[1];
240
241 var listLeaf = entityToFalcorListLeaf({ name: name$7 });
242 var postList = jsonGraph.json.self[listLeaf];
243
244 var posts = map(postList, function (post) {
245 return post;
246 });
247
248 return posts;
249}
250
251var feedPost = Object.freeze({
252 name: name$7,
253 pluralName: pluralName$7,
254 defaultProperties: defaultProperties$7,
255 getMany: getMany,
256 extractMany: extractMany
257});
258
259var name$9 = 'growthAction';
260var pluralName$9 = 'growthActions';
261
262var defaultProperties$8 = ['id', 'agent.id', 'agent.name', 'growee.id', 'growee.name', 'relationship.id', 'relationship.owner.id', 'growth_relationship_id', 'state', 'title', 'due_at', 'trigger_prompt_at', 'completed_at', 'progress_current', 'progress_total'];
263
264var growthAction = Object.freeze({
265 name: name$9,
266 pluralName: pluralName$9,
267 defaultProperties: defaultProperties$8
268});
269
270var name$10 = 'growthRelationship';
271var pluralName$10 = 'growthRelationships';
272
273var defaultProperties$9 = ['id', 'owner_type', 'owner.id', 'owner.name', 'growee.id', 'growee.name', 'agent.id', 'agent.name'];
274
275var growthRelationship = Object.freeze({
276 name: name$10,
277 pluralName: pluralName$10,
278 defaultProperties: defaultProperties$9
279});
280
281var name$11 = 'growthAgentPrompt';
282var pluralName$11 = 'growthAgentPrompts';
283
284var defaultProperties$10 = ['id', 'title', 'state', 'type', 'display_type', 'status', 'action.id', 'action.due_at', 'action.state', 'action.trigger_prompt_at', 'action.progress_total', 'action.progress_current', 'action.growee.id', 'action.growee.name', 'action.growee.email', 'action.growee.avatar.id', 'action.growee.avatar.src', 'action.agent.id', 'action.agent.name', 'action.relationship.id', 'action.relationship.owner.id'];
285
286var growthAgentPrompt = Object.freeze({
287 name: name$11,
288 pluralName: pluralName$11,
289 defaultProperties: defaultProperties$10
290});
291
292var name$12 = 'growthAgentResponse';
293var pluralName$12 = 'growthAgentResponses';
294
295var defaultProperties$11 = ['id', 'message'];
296
297var growthAgentResponse = Object.freeze({
298 name: name$12,
299 pluralName: pluralName$12,
300 defaultProperties: defaultProperties$11
301});
302
303var name$13 = 'license';
304var pluralName$13 = 'licenses';
305
306var defaultProperties$12 = ['id', 'updated_at'];
307
308var license = Object.freeze({
309 name: name$13,
310 pluralName: pluralName$13,
311 defaultProperties: defaultProperties$12
312});
313
314var name$14 = 'licensedCohort';
315var pluralName$14 = 'licensedCohorts';
316
317var defaultProperties$13 = ['id', 'updated_at'];
318
319var licensedCohort = Object.freeze({
320 name: name$14,
321 pluralName: pluralName$14,
322 defaultProperties: defaultProperties$13
323});
324
325var name$15 = 'licensedContentItem';
326var pluralName$15 = 'licensedContentItems';
327
328var defaultProperties$14 = ['id', 'updated_at'];
329
330var licensedContentItem = Object.freeze({
331 name: name$15,
332 pluralName: pluralName$15,
333 defaultProperties: defaultProperties$14
334});
335
336var name$16 = 'moment';
337var pluralName$16 = 'moments';
338
339var defaultProperties$15 = ['id', 'description', 'title', 'owner_type', 'owner.name', 'owner.avatar.src', 'owner.icon.src', 'cover_image_media.id', 'cover_image_media.src', 'background_music.src', 'background_music.duration', 'background_music.author'];
340
341var moment = Object.freeze({
342 name: name$16,
343 pluralName: pluralName$16,
344 defaultProperties: defaultProperties$15
345});
346
347var name$17 = 'myGrowthAgentPrompt';
348var pluralName$17 = 'myGrowthAgentPrompts';
349
350var defaultProperties$16 = ['id', 'title', 'state', 'type', 'display_type', 'status', 'action.id', 'action.due_at', 'action.state', 'action.trigger_prompt_at', 'action.progress_total', 'action.progress_current', 'action.growee.id', 'action.growee.name', 'action.growee.email', 'action.growee.avatar.id', 'action.growee.avatar.src', 'action.agent.id', 'action.agent.name', 'action.relationship.id', 'action.relationship.owner.id'];
351
352function getMany$1() {
353 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
354
355 var _buildPaging = buildPaging(options),
356 from = _buildPaging.from,
357 to = _buildPaging.to;
358
359 var properties = buildProperties(defaultProperties$16, options);
360 var listLeaf = entityToFalcorListLeaf({ name: name$17 });
361
362 var paths = map(properties, function (property) {
363 var propertyPath = falcorPathParser(property);
364 var path = concat(['self', listLeaf, { from: from, to: to }], propertyPath);
365
366 return path;
367 });
368
369 return paths;
370}
371
372function extractMany$1() {
373 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
374 var jsonGraph = arguments[1];
375
376 var listLeaf = entityToFalcorListLeaf({ name: name$17 });
377 var viewList = jsonGraph.json.self[listLeaf];
378
379 var views = map(viewList, function (view) {
380 return view;
381 });
382
383 return views;
384}
385
386var myGrowthAgentPrompt = Object.freeze({
387 name: name$17,
388 pluralName: pluralName$17,
389 defaultProperties: defaultProperties$16,
390 getMany: getMany$1,
391 extractMany: extractMany$1
392});
393
394var name$19 = 'review';
395var pluralName$19 = 'reviews';
396
397var defaultProperties$17 = ['id', 'rating'];
398
399var review = Object.freeze({
400 name: name$19,
401 pluralName: pluralName$19,
402 defaultProperties: defaultProperties$17
403});
404
405var name$20 = 'tree';
406var pluralName$20 = 'trees';
407
408var treeEntity = Object.freeze({
409 name: name$20,
410 pluralName: pluralName$20
411});
412
413/*
414 * This entity is temporary while we figure out how to handle "my" entities
415 * in a standard entity way. We expect to delete it once we have settled on a
416 * standard "my" approach.
417 */
418
419var name$18 = 'myTreeReview';
420var pluralName$18 = 'myTreeReviews';
421function getOne() {
422 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
423 var tree = options.tree;
424
425
426 if (isNil(tree) || isNil(tree.id)) {
427 return new Error("options.tree.id is required when getting the current user's tree review");
428 }
429
430 var properties = buildProperties(defaultProperties$17, options);
431
432 var paths = properties.map(function (property) {
433 var rootLeaf = entityToFalcorRootLeaf(treeEntity);
434 var path = falcorPathParser(property);
435
436 return concat([rootLeaf, tree.id, 'my_review'], path);
437 });
438
439 return paths;
440}
441
442function extractOne() {
443 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
444 var jsonGraph = arguments[1];
445 var tree = options.tree;
446
447
448 if (isNil(tree) || isNil(tree.id)) {
449 return new Error("options.tree.id is required when getting the current user's tree review");
450 }
451
452 var rootLeaf = entityToFalcorRootLeaf(treeEntity);
453
454 return jsonGraph.json[rootLeaf][tree.id].my_review;
455}
456
457var myTreeReview = Object.freeze({
458 name: name$18,
459 pluralName: pluralName$18,
460 defaultProperties: defaultProperties$17,
461 getOne: getOne,
462 extractOne: extractOne
463});
464
465var name$21 = 'note';
466var pluralName$21 = 'notes';
467
468var defaultProperties$18 = ['id', 'body'];
469
470var note = Object.freeze({
471 name: name$21,
472 pluralName: pluralName$21,
473 defaultProperties: defaultProperties$18
474});
475
476var name$22 = 'progressItem';
477var pluralName$22 = 'progressItems';
478
479var defaultProperties$19 = ['id', 'action', 'title', 'description', 'completed', 'hidden', 'assigner_type', 'linkable_type', 'updated_at', 'date_due'];
480
481var progressItem = Object.freeze({
482 name: name$22,
483 pluralName: pluralName$22,
484 defaultProperties: defaultProperties$19
485});
486
487var name$23 = 'space';
488var pluralName$23 = 'spaces';
489
490var defaultProperties$20 = ['id', 'name', 'topic', 'champion_id'];
491
492var space = Object.freeze({
493 name: name$23,
494 pluralName: pluralName$23,
495 defaultProperties: defaultProperties$20
496});
497
498var name$24 = 'timeline';
499var pluralName$24 = 'timelines';
500
501var timeline = Object.freeze({
502 name: name$24,
503 pluralName: pluralName$24
504});
505
506var name$25 = 'toDoItem';
507var pluralName$25 = 'toDoItems';
508
509var defaultProperties$21 = ['id', 'title', 'description', 'action', 'date_due'];
510
511var toDoItem = Object.freeze({
512 name: name$25,
513 pluralName: pluralName$25,
514 defaultProperties: defaultProperties$21
515});
516
517var name$26 = 'toDoList';
518var pluralName$26 = 'toDoLists';
519
520var defaultProperties$22 = ['id', 'name', 'created_at', 'updated_at'];
521
522var toDoList = Object.freeze({
523 name: name$26,
524 pluralName: pluralName$26,
525 defaultProperties: defaultProperties$22
526});
527
528var name$27 = 'topic';
529var pluralName$27 = 'topics';
530
531var defaultProperties$23 = ['id', 'name'];
532
533var topic = Object.freeze({
534 name: name$27,
535 pluralName: pluralName$27,
536 defaultProperties: defaultProperties$23
537});
538
539var name$28 = 'tourTipView';
540var pluralName$28 = 'tourTipViews';
541
542var defaultProperties$24 = ['id', 'state'];
543
544function getMany$2() {
545 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
546
547 var _buildPaging = buildPaging(options),
548 from = _buildPaging.from,
549 to = _buildPaging.to;
550
551 var properties = buildProperties(defaultProperties$24, options);
552 var listLeaf = entityToFalcorListLeaf({ name: name$28 });
553
554 var paths = map(properties, function (property) {
555 var propertyPath = falcorPathParser(property);
556 var path = concat(['self', listLeaf, { from: from, to: to }], propertyPath);
557
558 return path;
559 });
560
561 return paths;
562}
563
564function extractMany$2() {
565 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
566 var jsonGraph = arguments[1];
567
568 var listLeaf = entityToFalcorListLeaf({ name: name$28 });
569 var viewList = jsonGraph.json.self[listLeaf];
570
571 var views = map(viewList, function (view) {
572 return view;
573 });
574
575 return views;
576}
577
578var tourTipView = Object.freeze({
579 name: name$28,
580 pluralName: pluralName$28,
581 defaultProperties: defaultProperties$24,
582 getMany: getMany$2,
583 extractMany: extractMany$2
584});
585
586var name$29 = 'user';
587var pluralName$29 = 'users';
588
589var defaultProperties$25 = ['id', 'username', 'name'];
590
591var user = Object.freeze({
592 name: name$29,
593 pluralName: pluralName$29,
594 defaultProperties: defaultProperties$25
595});
596
597var name$30 = 'userToDoList';
598var pluralName$30 = 'userToDoLists';
599
600var defaultProperties$26 = ['id', 'name'];
601
602var userToDoList = Object.freeze({
603 name: name$30,
604 pluralName: pluralName$30,
605 defaultProperties: defaultProperties$26
606});
607
608var name$31 = 'view';
609var pluralName$31 = 'views';
610
611var defaultProperties$27 = ['id', 'viewable_id', 'viewable_type'];
612
613var view = Object.freeze({
614 name: name$31,
615 pluralName: pluralName$31,
616 defaultProperties: defaultProperties$27
617});
618
619
620
621var entities = Object.freeze({
622 audioCategory: audioCategory,
623 audioCategories: audioCategory,
624 audioTrack: audioTrack,
625 audioTracks: audioTrack,
626 champion: champion,
627 champions: champion,
628 cohort: cohort,
629 cohorts: cohort,
630 comment: comment,
631 comments: comment,
632 contentListItem: contentListItem,
633 contentListItems: contentListItem,
634 directMessage: directMessage,
635 directMessages: directMessage,
636 feedPost: feedPost,
637 feedPosts: feedPost,
638 growthAction: growthAction,
639 growthActions: growthAction,
640 growthRelationship: growthRelationship,
641 growthRelationships: growthRelationship,
642 growthAgentPrompt: growthAgentPrompt,
643 growthAgentPrompts: growthAgentPrompt,
644 growthAgentResponse: growthAgentResponse,
645 growthAgentResponses: growthAgentResponse,
646 license: license,
647 licenses: license,
648 licensedCohort: licensedCohort,
649 licensedCohorts: licensedCohort,
650 licensedContentItem: licensedContentItem,
651 licensedContentItems: licensedContentItem,
652 moment: moment,
653 moments: moment,
654 myGrowthAgentPrompt: myGrowthAgentPrompt,
655 myGrowthAgentPrompts: myGrowthAgentPrompt,
656 myTreeReview: myTreeReview,
657 myTreeReviews: myTreeReview,
658 note: note,
659 notes: note,
660 post: post,
661 posts: post,
662 progressItem: progressItem,
663 progressItems: progressItem,
664 review: review,
665 reviews: review,
666 space: space,
667 spaces: space,
668 timeline: timeline,
669 timelines: timeline,
670 toDoItem: toDoItem,
671 toDoItems: toDoItem,
672 toDoList: toDoList,
673 toDoLists: toDoList,
674 topic: topic,
675 topics: topic,
676 tourTipView: tourTipView,
677 tourTipViews: tourTipView,
678 tree: treeEntity,
679 trees: treeEntity,
680 user: user,
681 users: user,
682 userToDoList: userToDoList,
683 userToDoLists: userToDoList,
684 view: view,
685 views: view
686});
687
688var entitySrc = 'entities';
689
690function entityIsUndefinedError(entityName) {
691 var filename = entityNameToFilename(entityName);
692 return new Error('The ' + entityName + ' entity is undefined. Ensure ' + entitySrc + '/' + filename + ' exists and is exported by ' + entitySrc + '/_catalog.js');
693}
694
695function entityMustExportError(entityName, property) {
696 var filename = entityNameToFilename(entityName);
697 return new Error(entitySrc + '/' + filename + ' must export ' + property);
698}
699
700function optionsMustHaveError(property) {
701 return new Error('options.' + property + ' is required');
702}
703
704function entityExports(entity, property) {
705 return !isNil(entity[property]);
706}
707
708
709
710function optionsHas(options, property) {
711 return !isNil(get(options, property));
712}
713
714function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
715
716function getOne$1(entityName, entity) {
717 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
718
719 if (!entityExports(entity, 'pluralName')) {
720 return entityMustExportError(entityName, 'pluralName');
721 }
722
723 if (!entityExports(entity, 'defaultProperties')) {
724 return entityMustExportError(entityName, 'defaultProperties');
725 }
726
727 if (!optionsHas(options, 'id')) {
728 return optionsMustHaveError('id');
729 }
730
731 var properties = buildProperties(entity.defaultProperties, options);
732 var rootLeaf = entityToFalcorRootLeaf(entity);
733
734 var paths = map(properties, function (property) {
735 var propertyPath = falcorPathParser(property);
736 var path = concat([rootLeaf, options.id], propertyPath);
737
738 return path;
739 });
740
741 return paths;
742}
743
744function extractOne$1(entity) {
745 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
746 var jsonGraph = arguments[2];
747
748 var rootLeaf = entityToFalcorRootLeaf(entity);
749 var extractedEntity = jsonGraph.json[rootLeaf][options.id];
750
751 return extractedEntity;
752}
753
754function getMany$3(entityName, entity) {
755 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
756
757 if (!entityExports(entity, 'name')) {
758 return entityMustExportError(entityName, 'name');
759 }
760
761 if (!entityExports(entity, 'pluralName')) {
762 return entityMustExportError(entityName, 'pluralName');
763 }
764
765 if (!entityExports(entity, 'defaultProperties')) {
766 return entityMustExportError(entityName, 'defaultProperties');
767 }
768
769 var _buildPaging = buildPaging(options),
770 from = _buildPaging.from,
771 to = _buildPaging.to;
772
773 var properties = buildProperties(entity.defaultProperties, options);
774
775 var parentEntity = void 0,
776 parentOptions = void 0;
777 if (!isNil(options.parent)) {
778 var parentEntityName = head(keys(options.parent));
779 parentEntity = entities[parentEntityName];
780 parentOptions = options.parent[parentEntityName];
781 }
782
783 var basePath = [];
784
785 if (parentEntity) {
786 var rootLeaf = entityToFalcorRootLeaf(parentEntity);
787 basePath = concat(basePath, [rootLeaf, parentOptions.id]);
788 }
789
790 var listLeaf = !isNil(options.type) ? typedEntityToFalcorListPath(entity, options) : entityToFalcorListLeaf(entity);
791
792 basePath = concat(basePath, listLeaf);
793
794 if (optionsHasQuery(options)) {
795 var qs = stringify(options.query);
796 basePath = concat(basePath, qs);
797 }
798
799 basePath = concat(basePath, { from: from, to: to });
800
801 var paths = map(properties, function (property) {
802 var propertyPath = falcorPathParser(property);
803 var path = concat(basePath, propertyPath);
804
805 return path;
806 });
807
808 return paths;
809}
810
811function extractMany$3(entity) {
812 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
813 var jsonGraph = arguments[2];
814
815 var parentEntity = void 0,
816 parentOptions = void 0;
817 if (!isNil(options.parent)) {
818 var parentEntityName = head(keys(options.parent));
819 parentEntity = entities[parentEntityName];
820 parentOptions = options.parent[parentEntityName];
821 }
822
823 var leafPath = !isNil(options.type) ? typedEntityToFalcorListPath(entity, options) : entityToFalcorListLeaf(entity);
824
825 var listPath = [];
826 if (parentEntity) {
827 var parentRootLeaf = entityToFalcorRootLeaf(parentEntity);
828 listPath = concat(listPath, 'json', parentRootLeaf, parentOptions.id, leafPath);
829 } else {
830 listPath = concat(listPath, 'json', leafPath);
831 }
832
833 if (optionsHasQuery(options)) {
834 var qs = stringify(options.query);
835 listPath = concat(listPath, qs);
836 }
837
838 var entityList = get(jsonGraph, listPath);
839 var extractedEntities = map(entityList, function (entity) {
840 return entity;
841 });
842
843 return extractedEntities;
844}
845
846function getMy(entityName, entity) {
847 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
848
849 if (!entityExports(entity, 'name')) {
850 return entityMustExportError(entityName, 'name');
851 }
852
853 if (!entityExports(entity, 'defaultProperties')) {
854 return entityMustExportError(entityName, 'defaultProperties');
855 }
856
857 var _buildPaging2 = buildPaging(options),
858 from = _buildPaging2.from,
859 to = _buildPaging2.to;
860
861 var properties = buildProperties(entity.defaultProperties, options);
862 var listLeaf = entityToFalcorMyListLeaf(entity);
863 var basePath = [MY_PATH_KEYWORD, listLeaf];
864
865 if (optionsHasQuery(options)) {
866 var qs = stringify(options.query);
867 basePath = concat(basePath, qs);
868 }
869
870 basePath = concat(basePath, { from: from, to: to });
871
872 var paths = map(properties, function (property) {
873 var propertyPath = falcorPathParser(property);
874 var path = concat(basePath, propertyPath);
875
876 return path;
877 });
878
879 return paths;
880}
881
882function extractMy(entity) {
883 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
884 var jsonGraph = arguments[2];
885
886 var listLeaf = entityToFalcorMyListLeaf(entity);
887 var entityList = jsonGraph.json[MY_PATH_KEYWORD][listLeaf];
888
889 if (optionsHasQuery(options)) {
890 var qs = stringify(options.query);
891 entityList = entityList[qs];
892 }
893
894 var extractedEntities = map(entityList, function (entity) {
895 return entity;
896 });
897
898 return extractedEntities;
899}
900
901function getMembership(entityName, entity) {
902 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
903
904 if (!entityExports(entity, 'name')) {
905 return entityMustExportError(entityName, 'name');
906 }
907
908 if (!entityExports(entity, 'defaultProperties')) {
909 return entityMustExportError(entityName, 'defaultProperties');
910 }
911
912 var _buildPaging3 = buildPaging(options),
913 from = _buildPaging3.from,
914 to = _buildPaging3.to;
915
916 var properties = buildProperties(entity.defaultProperties, options);
917 var listLeaf = entityToFalcorMembershipsListLeaf(entity);
918 var basePath = [MY_PATH_KEYWORD, listLeaf];
919
920 if (optionsHasQuery(options)) {
921 var qs = stringify(options.query);
922 basePath = concat(basePath, qs);
923 }
924
925 basePath = concat(basePath, { from: from, to: to });
926
927 var paths = map(properties, function (property) {
928 var propertyPath = falcorPathParser(property);
929 var path = concat(basePath, propertyPath);
930
931 return path;
932 });
933
934 return paths;
935}
936
937function extractMemberships(entity) {
938 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
939 var jsonGraph = arguments[2];
940
941 var listLeaf = entityToFalcorMembershipsListLeaf(entity);
942 var entityList = jsonGraph.json[MY_PATH_KEYWORD][listLeaf];
943
944 if (optionsHasQuery(options)) {
945 var qs = stringify(options.query);
946 entityList = entityList[qs];
947 }
948
949 var extractedEntities = map(entityList, function (entity) {
950 return entity;
951 });
952
953 return extractedEntities;
954}
955
956function create(entityName, entity) {
957 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
958
959 if (!entityExports(entity, 'name')) {
960 return entityMustExportError(entityName, 'name');
961 }
962
963 if (!entityExports(entity, 'pluralName')) {
964 return entityMustExportError(entityName, 'pluralName');
965 }
966
967 if (!optionsHas(options, entityName)) {
968 return optionsMustHaveError(entityName);
969 }
970
971 var parentEntity = void 0,
972 parentOptions = void 0;
973 if (!isNil(options.parent)) {
974 var parentEntityName = head(keys(options.parent));
975 parentEntity = entities[parentEntityName];
976 parentOptions = options.parent[parentEntityName];
977
978 if (isNil(parentEntity)) {
979 return entityIsUndefinedError(parentEntityName);
980 }
981
982 if (!entityExports(parentEntity, 'pluralName')) {
983 return entityMustExportError(parentEntityName, 'pluralName');
984 }
985
986 if (!optionsHas(parentOptions, 'id')) {
987 return optionsMustHaveError('parent.' + parentEntityName + '.id');
988 }
989 }
990
991 var path = void 0;
992 if (parentEntity) {
993 var rootLeaf = entityToFalcorRootLeaf(parentEntity);
994 var listLeaf = entityToFalcorListLeaf(entity);
995
996 path = [rootLeaf, parentOptions.id, listLeaf, 'add'];
997 } else {
998 var _rootLeaf = entityToFalcorRootLeaf(entity);
999
1000 path = [_rootLeaf, 'create'];
1001 }
1002
1003 var envelopeKey = entityToAPIEnvelopeKey(entity);
1004 var args = [_defineProperty({}, envelopeKey, options[entity.name])];
1005
1006 return { path: path, args: args };
1007}
1008
1009function extractCreated(entity) {
1010 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1011 var jsonGraph = arguments[2];
1012
1013 var rootLeaf = entityToFalcorRootLeaf(entity);
1014 var entities = jsonGraph.json[rootLeaf];
1015 var first = head(keys(entities));
1016 var extractedEntity = entities[first];
1017
1018 return extractedEntity;
1019}
1020
1021function invalidateAfterCreate$1(entity) {
1022 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1023
1024 if (!isNil(options.parent)) {
1025 var parentEntityName = head(keys(options.parent));
1026 var parentEntity = entities[parentEntityName];
1027 var parentId = options.parent[parentEntityName].id;
1028
1029 var rootLeaf = entityToFalcorRootLeaf(parentEntity);
1030 var listLeaf = entityToFalcorListLeaf(entity);
1031
1032 var paths = [[rootLeaf, parentId, listLeaf], [rootLeaf, parentId, listLeaf, 'length']];
1033
1034 return paths;
1035 }
1036}
1037
1038function update(entityName, entity) {
1039 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
1040
1041 if (!entityExports(entity, 'pluralName')) {
1042 return entityMustExportError(entityName, 'pluralName');
1043 }
1044
1045 if (!optionsHas(options, 'id')) {
1046 return optionsMustHaveError('id');
1047 }
1048
1049 if (keys(options).length === 1) {
1050 return new Error('options must include at least one property to update');
1051 }
1052
1053 var properties = filter(keys(options), function (key) {
1054 return key !== 'id';
1055 });
1056 var rootLeaf = entityToFalcorRootLeaf(entity);
1057
1058 var paths = map(properties, function (property) {
1059 var path = {
1060 path: [rootLeaf, options.id, property],
1061 value: options[property]
1062 };
1063
1064 return path;
1065 });
1066
1067 return paths;
1068}
1069
1070function extractUpdated(entity) {
1071 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1072 var jsonGraph = arguments[2];
1073
1074 var rootLeaf = entityToFalcorRootLeaf(entity);
1075 var updatedProperties = jsonGraph.json[rootLeaf][options.id];
1076
1077 return updatedProperties;
1078}
1079
1080function destroy(entityName, entity) {
1081 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
1082
1083 if (!entityExports(entity, 'pluralName')) {
1084 return entityMustExportError(entityName, 'pluralName');
1085 }
1086
1087 if (!optionsHas(options, 'id')) {
1088 return optionsMustHaveError('id');
1089 }
1090
1091 var rootLeaf = entityToFalcorRootLeaf(entity);
1092 var destroyPath = [rootLeaf, options.id, 'delete'];
1093
1094 return destroyPath;
1095}
1096
1097function invalidateAfterDestroy$1(entity) {
1098 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1099
1100 var rootEntityLeaf = entityToFalcorRootLeaf(entity);
1101 var entityId = options.id;
1102
1103 var paths = [[rootEntityLeaf, entityId]];
1104
1105 if (!isNil(options.parent)) {
1106 var parentEntityName = head(keys(options.parent));
1107 var parentEntity = entities[parentEntityName];
1108 var parentId = options.parent[parentEntityName].id;
1109
1110 var rootParentLeaf = entityToFalcorRootLeaf(parentEntity);
1111 var listLeaf = entityToFalcorListLeaf(entity);
1112
1113 paths.push([rootParentLeaf, parentId, listLeaf], [rootParentLeaf, parentId, listLeaf, 'length']);
1114 }
1115
1116 return paths;
1117}
1118
1119var _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; }; }();
1120
1121function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
1122
1123function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
1124
1125var SuperGloo = function () {
1126 function SuperGloo() {
1127 var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
1128
1129 _classCallCheck(this, SuperGloo);
1130
1131 var requiredProperties = ['falcorModel'];
1132
1133 if (some(requiredProperties, function (_) {
1134 return isNil(get(config, _));
1135 })) {
1136 throw new Error('One of the following required config parameters is undefined: ' + requiredProperties.join(', '));
1137 }
1138
1139 this.falcor = config.falcorModel;
1140 }
1141
1142 /*
1143 * Standard interfaces
1144 */
1145
1146 _createClass(SuperGloo, [{
1147 key: 'setModel',
1148 value: function setModel(model) {
1149 if (isNil(model)) {
1150 throw new Error('When setting the model, the model cannot be null or undefined');
1151 }
1152
1153 this.falcor = model;
1154 }
1155 }, {
1156 key: 'getOne',
1157 value: function getOne$$1(entityName) {
1158 var _falcor;
1159
1160 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1161
1162 var entity = entities[entityName];
1163
1164 if (isNil(entity)) {
1165 return Promise.reject(entityIsUndefinedError(entityName));
1166 }
1167
1168 var paths = isFunction(entity.getOne) ? entity.getOne(options) : getOne$1(entityName, entity, options);
1169
1170 if (isError(paths)) {
1171 return Promise.reject(paths);
1172 }
1173
1174 return (_falcor = this.falcor).get.apply(_falcor, _toConsumableArray(paths)).then(function (jsonGraph) {
1175 var extractedEntity = void 0;
1176 if (isNil(get(jsonGraph, 'json'))) {
1177 extractedEntity = null;
1178 } else {
1179 extractedEntity = isFunction(entity.extractOne) ? entity.extractOne(options, jsonGraph) : extractOne$1(entity, options, jsonGraph);
1180 }
1181
1182 return extractedEntity;
1183 }).catch(function (response) {
1184 var message = buildGetOneErrorMessage(response);
1185 return Promise.reject(new Error(message));
1186 });
1187 }
1188 }, {
1189 key: 'getMany',
1190 value: function getMany$$1(entityName) {
1191 var _falcor2;
1192
1193 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1194
1195 var entity = entities[entityName];
1196
1197 if (isNil(entity)) {
1198 return Promise.reject(entityIsUndefinedError(entityName));
1199 }
1200
1201 var paths = isFunction(entity.getMany) ? entity.getMany(options) : getMany$3(entityName, entity, options);
1202
1203 if (isError(paths)) {
1204 return Promise.reject(paths);
1205 }
1206
1207 return (_falcor2 = this.falcor).get.apply(_falcor2, _toConsumableArray(paths)).then(function (jsonGraph) {
1208 var extractedEntities = void 0;
1209 if (isNil(get(jsonGraph, 'json'))) {
1210 extractedEntities = null;
1211 } else {
1212 extractedEntities = isFunction(entity.extractMany) ? entity.extractMany(options, jsonGraph) : extractMany$3(entity, options, jsonGraph);
1213 }
1214
1215 return extractedEntities;
1216 }).catch(function (response) {
1217 var message = buildGetManyErrorMessage(response);
1218 return Promise.reject(new Error(message));
1219 });
1220 }
1221 }, {
1222 key: 'getMy',
1223 value: function getMy$$1(entityName) {
1224 var _falcor3;
1225
1226 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1227
1228 var entity = entities[entityName];
1229
1230 if (isNil(entity)) {
1231 return Promise.reject(entityIsUndefinedError(entityName));
1232 }
1233
1234 var paths = isFunction(entity.getMy) ? entity.getMy(options) : getMy(entityName, entity, options);
1235
1236 if (isError(paths)) {
1237 return Promise.reject(paths);
1238 }
1239
1240 return (_falcor3 = this.falcor).get.apply(_falcor3, _toConsumableArray(paths)).then(function (jsonGraph) {
1241 var extractedEntities = void 0;
1242 if (isNil(get(jsonGraph, 'json'))) {
1243 extractedEntities = null;
1244 } else {
1245 extractedEntities = isFunction(entity.extractMy) ? entity.extractMy(options, jsonGraph) : extractMy(entity, options, jsonGraph);
1246 }
1247
1248 return extractedEntities;
1249 }).catch(function (response) {
1250 var message = buildGetMyErrorMessage(response);
1251 return Promise.reject(new Error(message));
1252 });
1253 }
1254 }, {
1255 key: 'getMembership',
1256 value: function getMembership$$1(entityName) {
1257 var _falcor4;
1258
1259 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1260
1261 var entity = entities[entityName];
1262
1263 if (isNil(entity)) {
1264 return Promise.reject(entityIsUndefinedError(entityName));
1265 }
1266
1267 var paths = isFunction(entity.getMembership) ? entity.getMembership(options) : getMembership(entityName, entity, options);
1268
1269 if (isError(paths)) {
1270 return Promise.reject(paths);
1271 }
1272
1273 return (_falcor4 = this.falcor).get.apply(_falcor4, _toConsumableArray(paths)).then(function (jsonGraph) {
1274 var extractedEntities = void 0;
1275 if (isNil(get(jsonGraph, 'json'))) {
1276 extractedEntities = null;
1277 } else {
1278 extractedEntities = isFunction(entity.extractMemberships) ? entity.extractMemberships(options, jsonGraph) : extractMemberships(entity, options, jsonGraph);
1279 }
1280
1281 return extractedEntities;
1282 }).catch(function (response) {
1283 var message = buildGetMembershipsErrorMessage(response);
1284 return Promise.reject(new Error(message));
1285 });
1286 }
1287 }, {
1288 key: 'create',
1289 value: function create$$1(entityName) {
1290 var _this = this;
1291
1292 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1293
1294 var entity = entities[entityName];
1295
1296 if (isNil(entity)) {
1297 return Promise.reject(entityIsUndefinedError(entityName));
1298 }
1299
1300 var callOptions = isFunction(entity.create) ? entity.create(options) : create(entityName, entity, options);
1301
1302 if (isError(callOptions)) {
1303 return Promise.reject(callOptions);
1304 }
1305
1306 var path = callOptions.path,
1307 args = callOptions.args;
1308
1309
1310 return this.falcor.call(path, args).then(function (jsonGraph) {
1311 var extractedEntity = void 0;
1312 if (isNil(get(jsonGraph, 'json'))) {
1313 extractedEntity = null;
1314 } else {
1315 extractedEntity = isFunction(entity.extractCreated) ? entity.extractCreated(options, jsonGraph) : extractCreated(entity, options, jsonGraph);
1316 }
1317
1318 var pathsToInvalidate = isFunction(entity.invalidateAfterCreate) ? entity.invalidateAfterCreate(options) : invalidateAfterCreate$1(entity, options);
1319
1320 if (!isEmpty(pathsToInvalidate)) {
1321 forEach(pathsToInvalidate, function (path) {
1322 _this.falcor.invalidate(path);
1323 });
1324 }
1325
1326 return extractedEntity;
1327 }).catch(function (response) {
1328 var message = buildCreateErrorMessage(response);
1329 return Promise.reject(new Error(message));
1330 });
1331 }
1332 }, {
1333 key: 'update',
1334 value: function update$$1(entityName) {
1335 var _falcor5;
1336
1337 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1338
1339 var entity = entities[entityName];
1340
1341 if (isNil(entity)) {
1342 return Promise.reject(entityIsUndefinedError(entityName));
1343 }
1344
1345 var paths = isFunction(entity.update) ? entity.update(options) : update(entityName, entity, options);
1346
1347 if (isError(paths)) {
1348 return Promise.reject(paths);
1349 }
1350
1351 return (_falcor5 = this.falcor).set.apply(_falcor5, _toConsumableArray(paths)).then(function (jsonGraph) {
1352 var updatedProperties = void 0;
1353 if (isNil(get(jsonGraph, 'json'))) {
1354 updatedProperties = null;
1355 } else {
1356 updatedProperties = isFunction(entity.extractUpdated) ? entity.extractUpdated(options, jsonGraph) : extractUpdated(entity, options, jsonGraph);
1357 }
1358
1359 return updatedProperties;
1360 }).catch(function (response) {
1361 var message = buildUpdateErrorMessage(response);
1362 return Promise.reject(new Error(message));
1363 });
1364 }
1365 }, {
1366 key: 'destroy',
1367 value: function destroy$$1(entityName) {
1368 var _this2 = this;
1369
1370 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1371
1372 var entity = entities[entityName];
1373
1374 if (isNil(entity)) {
1375 return Promise.reject(entityIsUndefinedError(entityName));
1376 }
1377
1378 var destroyPath = isFunction(entity.destroy) ? entity.destroy(options) : destroy(entityName, entity, options);
1379
1380 if (isError(destroyPath)) {
1381 return Promise.reject(destroyPath);
1382 }
1383
1384 return this.falcor.call(destroyPath).then(function () {
1385 var pathsToInvalidate = isFunction(entity.invalidateAfterDestroy) ? entity.invalidateAfterDestroy(options) : invalidateAfterDestroy$1(entity, options);
1386
1387 if (!isEmpty(pathsToInvalidate)) {
1388 forEach(pathsToInvalidate, function (path) {
1389 _this2.falcor.invalidate(path);
1390 });
1391 }
1392
1393 return 'Entity destroyed';
1394 }).catch(function (response) {
1395 var message = buildDestroyErrorMessage(response);
1396 return Promise.reject(new Error(message));
1397 });
1398 }
1399 }]);
1400
1401 return SuperGloo;
1402}();
1403
1404var codeToMessageMap = {
1405 400: 'request is malformed',
1406 401: 'user is not authorized',
1407 403: 'user is forbidden',
1408 404: 'entity does not exist',
1409 422: 'entity is not valid',
1410 default: 'unspecified error encountered'
1411};
1412
1413function buildGetOneErrorMessage(response) {
1414 if (response instanceof Error) {
1415 return response.message;
1416 }
1417
1418 var firstPath = response[0];
1419 var entity = camelCase(firstPath.path[0]);
1420 var entityId = firstPath.path[1];
1421 var originalMessage = firstPath.value.message || firstPath.value;
1422
1423 var match = originalMessage.match(/\b[4,5][0-9][0-9]\b/);
1424 var code = !isNil(match) && !isNil(codeToMessageMap[match[0]]) ? match[0] : 'default';
1425
1426 var improvedMessage = codeToMessageMap[code];
1427
1428 return 'Could not get ' + entity + ' ' + entityId + ': ' + improvedMessage + ' (' + originalMessage + ')';
1429}
1430
1431function buildGetManyErrorMessage(response) {
1432 if (response instanceof Error) {
1433 return response.message;
1434 }
1435
1436 var firstPath = response[0];
1437 var entity = camelCase(firstPath.path[0]);
1438 var originalMessage = firstPath.value.message || firstPath.value;
1439
1440 var match = originalMessage.match(/\b[4,5][0-9][0-9]\b/);
1441 var code = !isNil(match) && !isNil(codeToMessageMap[match[0]]) ? match[0] : 'default';
1442
1443 var improvedMessage = codeToMessageMap[code];
1444
1445 return 'Could not get many ' + entity + ': ' + improvedMessage + ' (' + originalMessage + ')';
1446}
1447
1448function buildGetMyErrorMessage(response) {
1449 if (response instanceof Error) {
1450 return response.message;
1451 }
1452
1453 var firstPath = response[0];
1454 var entity = camelCase(firstPath.path[0]);
1455 var originalMessage = firstPath.value.message || firstPath.value;
1456
1457 var match = originalMessage.match(/\b[4,5][0-9][0-9]\b/);
1458 var code = !isNil(match) && !isNil(codeToMessageMap[match[0]]) ? match[0] : 'default';
1459
1460 var improvedMessage = codeToMessageMap[code];
1461
1462 return 'Could not get my ' + entity + ': ' + improvedMessage + ' (' + originalMessage + ')';
1463}
1464
1465function buildGetMembershipsErrorMessage(response) {
1466 if (response instanceof Error) {
1467 return response.message;
1468 }
1469
1470 var firstPath = response[0];
1471 var entity = camelCase(firstPath.path[0]);
1472 var originalMessage = firstPath.value.message || firstPath.value;
1473
1474 var match = originalMessage.match(/\b[4,5][0-9][0-9]\b/);
1475 var code = !isNil(match) && !isNil(codeToMessageMap[match[0]]) ? match[0] : 'default';
1476
1477 var improvedMessage = codeToMessageMap[code];
1478
1479 return 'Could not get ' + entity + ' memberships: ' + improvedMessage + ' (' + originalMessage + ')';
1480}
1481
1482function buildCreateErrorMessage(response) {
1483 if (response instanceof Error) {
1484 return response.message;
1485 }
1486
1487 var firstError = head(response);
1488
1489 var entityName = void 0;
1490 if (last(firstError.path) === 'add') {
1491 var listLeaf = nth(firstError.path, -2);
1492 entityName = falcorListLeafToEntityName(listLeaf);
1493 } else {
1494 var rootLeaf = head(firstError.path);
1495 entityName = falcorRootLeafToEntityName(rootLeaf);
1496 }
1497
1498 var originalMessage = firstError.value.message || firstError.value;
1499
1500 var match = originalMessage.match(/\b[4,5][0-9][0-9]\b/);
1501 var code = !isNil(match) && !isNil(codeToMessageMap[match[0]]) ? match[0] : 'default';
1502
1503 var improvedMessage = codeToMessageMap[code];
1504
1505 return 'Could not create a new ' + entityName + ': ' + improvedMessage + ' (' + originalMessage + ')';
1506}
1507
1508function buildUpdateErrorMessage(response) {
1509 if (response instanceof Error) {
1510 return response.message;
1511 }
1512
1513 var firstPath = response[0];
1514 var entity = camelCase(firstPath.path[0]);
1515 var entityId = firstPath.path[1];
1516 var originalMessage = firstPath.value.message || firstPath.value;
1517
1518 var match = originalMessage.match(/\b[4,5][0-9][0-9]\b/);
1519 var code = !isNil(match) && !isNil(codeToMessageMap[match[0]]) ? match[0] : 'default';
1520
1521 var improvedMessage = codeToMessageMap[code];
1522
1523 return 'Could not update ' + entity + ' ' + entityId + ': ' + improvedMessage + ' (' + originalMessage + ')';
1524}
1525
1526function buildDestroyErrorMessage(response) {
1527 if (response instanceof Error) {
1528 return response.message;
1529 }
1530
1531 var firstPath = response[0];
1532 var entity = camelCase(firstPath.path[0]);
1533 var entityId = firstPath.path[1];
1534 var originalMessage = firstPath.value.message || firstPath.value;
1535
1536 var match = originalMessage.match(/\b[4,5][0-9][0-9]\b/);
1537 var code = !isNil(match) && !isNil(codeToMessageMap[match[0]]) ? match[0] : 'default';
1538
1539 var improvedMessage = codeToMessageMap[code];
1540
1541 return 'Could not destroy ' + entity + ' ' + entityId + ': ' + improvedMessage + ' (' + originalMessage + ')';
1542}
1543
1544export default SuperGloo;