bkper-js 1.10.0 → 1.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -418,6 +418,12 @@ export declare class Bkper {
418
418
  * @returns The retrieved list of Books
419
419
  */
420
420
  static getBooks(): Promise<Book[]>;
421
+ /**
422
+ * Gets all [[Collections]] the user has access to.
423
+ *
424
+ * @returns The retrieved list of Collections
425
+ */
426
+ static getCollections(): Promise<Collection[]>;
421
427
  /**
422
428
  * Gets all [[Apps]] available for the user.
423
429
  *
@@ -821,10 +827,64 @@ export declare class Collection {
821
827
  * @returns The name of this Collection
822
828
  */
823
829
  getName(): string | undefined;
830
+ /**
831
+ * Sets the name of the Collection.
832
+ *
833
+ * @returns This Collection, for chainning.
834
+ */
835
+ setName(name: string): Collection;
836
+ /**
837
+ * Gets the username of the owner of this Collection
838
+ *
839
+ * @returns The Collection's owner username
840
+ */
841
+ getOwnerUsername(): string | undefined;
842
+ /**
843
+ * Gets the user permission for this Collection
844
+ *
845
+ * @returns The permission for the current user
846
+ */
847
+ getPermission(): Permission | undefined;
824
848
  /**
825
849
  * @returns All Books of this collection.
826
850
  */
827
851
  getBooks(): Book[];
852
+ /**
853
+ * Adds Books to this Collection.
854
+ *
855
+ * @returns The added Book objects
856
+ */
857
+ addBooks(books: Book[]): Promise<Book[]>;
858
+ /**
859
+ * Removes Books from this Collection.
860
+ *
861
+ * @returns The removed Book objects
862
+ */
863
+ removeBooks(books: Book[]): Promise<Book[]>;
864
+ /**
865
+ * Gets the last update date of this Collection
866
+ *
867
+ * @returns The Collection's last update timestamp, in milliseconds
868
+ */
869
+ getUpdatedAt(): string | undefined;
870
+ /**
871
+ * Performs create new Collection.
872
+ *
873
+ * @returns The created Collection object
874
+ */
875
+ create(): Promise<Collection>;
876
+ /**
877
+ * Performs update Collection, applying pending changes.
878
+ *
879
+ * @returns The updated Collection object
880
+ */
881
+ update(): Promise<Collection>;
882
+ /**
883
+ * Performs delete Collection.
884
+ *
885
+ * @returns The list of Books the user has access to that were affected by the deletion of this Collection
886
+ */
887
+ remove(): Promise<Book[]>;
828
888
  }
829
889
 
830
890
  /**
@@ -11,11 +11,13 @@ import { Book } from "./Book.js";
11
11
  import { App } from "./App.js";
12
12
  import * as AppService from '../service/app-service.js';
13
13
  import * as BookService from '../service/book-service.js';
14
+ import * as CollectionService from '../service/collection-service.js';
14
15
  import * as UserService from '../service/user-service.js';
15
16
  import * as TemplateService from '../service/template-service.js';
16
17
  import { HttpApiRequest } from '../service/http-api-request.js';
17
18
  import { User } from "./User.js";
18
19
  import { Template } from "./Template.js";
20
+ import { Collection } from "./Collection.js";
19
21
  /**
20
22
  * This is the main entry point of the [bkper-js](https://www.npmjs.com/package/bkper-js) library.
21
23
  *
@@ -75,6 +77,17 @@ export class Bkper {
75
77
  return books.map(book => new Book(book));
76
78
  });
77
79
  }
80
+ /**
81
+ * Gets all [[Collections]] the user has access to.
82
+ *
83
+ * @returns The retrieved list of Collections
84
+ */
85
+ static getCollections() {
86
+ return __awaiter(this, void 0, void 0, function* () {
87
+ let collections = yield CollectionService.loadCollections();
88
+ return collections.map(collection => new Collection(collection));
89
+ });
90
+ }
78
91
  /**
79
92
  * Gets all [[Apps]] available for the user.
80
93
  *
@@ -1,4 +1,14 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
1
10
  import { Book } from "./Book.js";
11
+ import * as CollectionService from '../service/collection-service.js';
2
12
  /**
3
13
  * This class defines a Collection of [[Books]].
4
14
  *
@@ -26,6 +36,31 @@ export class Collection {
26
36
  getName() {
27
37
  return this.payload.name;
28
38
  }
39
+ /**
40
+ * Sets the name of the Collection.
41
+ *
42
+ * @returns This Collection, for chainning.
43
+ */
44
+ setName(name) {
45
+ this.payload.name = name;
46
+ return this;
47
+ }
48
+ /**
49
+ * Gets the username of the owner of this Collection
50
+ *
51
+ * @returns The Collection's owner username
52
+ */
53
+ getOwnerUsername() {
54
+ return this.payload.ownerUsername;
55
+ }
56
+ /**
57
+ * Gets the user permission for this Collection
58
+ *
59
+ * @returns The permission for the current user
60
+ */
61
+ getPermission() {
62
+ return this.payload.permission ? this.payload.permission : undefined;
63
+ }
29
64
  /**
30
65
  * @returns All Books of this collection.
31
66
  */
@@ -40,5 +75,78 @@ export class Collection {
40
75
  }
41
76
  return books;
42
77
  }
78
+ /**
79
+ * Adds Books to this Collection.
80
+ *
81
+ * @returns The added Book objects
82
+ */
83
+ addBooks(books) {
84
+ return __awaiter(this, void 0, void 0, function* () {
85
+ const collectionId = this.getId();
86
+ if (collectionId && books.length > 0) {
87
+ const bookList = { items: books.map(b => b.json()) };
88
+ let addedBooks = yield CollectionService.addBooksToCollection(collectionId, bookList);
89
+ return addedBooks.map(book => new Book(book));
90
+ }
91
+ return [];
92
+ });
93
+ }
94
+ /**
95
+ * Removes Books from this Collection.
96
+ *
97
+ * @returns The removed Book objects
98
+ */
99
+ removeBooks(books) {
100
+ return __awaiter(this, void 0, void 0, function* () {
101
+ const collectionId = this.getId();
102
+ if (collectionId && books.length > 0) {
103
+ const bookList = { items: books.map(b => b.json()) };
104
+ let removedBooks = yield CollectionService.removeBooksFromCollection(collectionId, bookList);
105
+ return removedBooks.map(book => new Book(book));
106
+ }
107
+ return [];
108
+ });
109
+ }
110
+ /**
111
+ * Gets the last update date of this Collection
112
+ *
113
+ * @returns The Collection's last update timestamp, in milliseconds
114
+ */
115
+ getUpdatedAt() {
116
+ return this.payload.updatedAt;
117
+ }
118
+ /**
119
+ * Performs create new Collection.
120
+ *
121
+ * @returns The created Collection object
122
+ */
123
+ create() {
124
+ return __awaiter(this, void 0, void 0, function* () {
125
+ this.payload = yield CollectionService.createCollection(this.payload);
126
+ return this;
127
+ });
128
+ }
129
+ /**
130
+ * Performs update Collection, applying pending changes.
131
+ *
132
+ * @returns The updated Collection object
133
+ */
134
+ update() {
135
+ return __awaiter(this, void 0, void 0, function* () {
136
+ this.payload = yield CollectionService.updateCollection(this.payload);
137
+ return this;
138
+ });
139
+ }
140
+ /**
141
+ * Performs delete Collection.
142
+ *
143
+ * @returns The list of Books the user has access to that were affected by the deletion of this Collection
144
+ */
145
+ remove() {
146
+ return __awaiter(this, void 0, void 0, function* () {
147
+ let books = yield CollectionService.deleteCollection(this.payload);
148
+ return books.map(book => new Book(book));
149
+ });
150
+ }
43
151
  }
44
152
  //# sourceMappingURL=Collection.js.map
@@ -0,0 +1,51 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { HttpApiV5Request } from "./http-api-request.js";
11
+ export function loadCollections() {
12
+ return __awaiter(this, void 0, void 0, function* () {
13
+ var _a;
14
+ let response = yield new HttpApiV5Request('collections').fetch();
15
+ return ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
16
+ });
17
+ }
18
+ export function createCollection(payload) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ let response = yield new HttpApiV5Request('collections').setMethod('POST').setPayload(payload).fetch();
21
+ return response.data;
22
+ });
23
+ }
24
+ export function updateCollection(payload) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ let response = yield new HttpApiV5Request('collections').setMethod('PUT').setPayload(payload).fetch();
27
+ return response.data;
28
+ });
29
+ }
30
+ export function deleteCollection(payload) {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ var _a;
33
+ let response = yield new HttpApiV5Request(`collections/${payload.id}`).setMethod('DELETE').fetch();
34
+ return ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
35
+ });
36
+ }
37
+ export function addBooksToCollection(collectionId, payload) {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ var _a;
40
+ let response = yield new HttpApiV5Request(`collections/${collectionId}/books/add`).setMethod('PATCH').setPayload(payload).fetch();
41
+ return ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
42
+ });
43
+ }
44
+ export function removeBooksFromCollection(collectionId, payload) {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ var _a;
47
+ let response = yield new HttpApiV5Request(`collections/${collectionId}/books/remove`).setMethod('PATCH').setPayload(payload).fetch();
48
+ return ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.items) || [];
49
+ });
50
+ }
51
+ //# sourceMappingURL=collection-service.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bkper-js",
3
- "version": "1.10.0",
3
+ "version": "1.12.0",
4
4
  "description": "Javascript client for Bkper REST API",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",