import { injectable } from "inversify"; import BaseService from "../../service"; import { IService, IResourceMapper, IFindParams } from "../../interfaces"; import BookmarkList, { IBookmarkList, IBookmarkListResource, IBookmarkListResponse } from "../bookmarkList"; import BookmarkListMapper from "./mapper"; export interface IBookmarkListService extends IService { findByUserId(userId: string, options?: { include: string[] }): Promise; createList(userId: string, attributes: IBookmarkList): Promise; deleteList(userId: string, listId: string): Promise; updateList(userId: string, list: IBookmarkList): Promise; } @injectable() export default class BookmarkListService extends BaseService implements IBookmarkListService { resource: string = "bookmark-list"; mapper: IResourceMapper = new BookmarkListMapper(); userId: string; /** * @example http://stable.web.op-api-gateway.qa.lonelyplanet.com/users/jcreamer898/bookmark-lists */ public url({ userId }) { return `${this.host}/users/${userId}/bookmark-lists`; } /** * Find a user's bookmark lists by Id. * By default includes entries and the target's of the entries. * Also adds the `nocache=true` query param * @param userId User Id * @param options * @example * * ```typescript * * client.bookmarkList.findByUserId("1234"}); * ``` */ public async findByUserId(userId: string, options: IFindParams = { include: ["entries", "entries.target"] }) { this.userId = userId; const resources = await this.http.fetch( `${this.url({ userId })}${this.qs(options)}&nocache=true`, { headers: this.headers(), } ); return this.mapper.map(resources); } /** * Create a Bookmark List from a user * @param userId User Id * @param attributes * @example * * ```typescript * * client.bookmarkList.createList("1234", { * name: "My List", * source: "dotcom", * visibility: "public" * }); * ``` */ public async createList(userId: string, attributes: IBookmarkList) { const response = await this.http.fetch(`${this.url({ userId })}`, { method: "POST", body: { data: this.mapper.toResource(attributes), }, headers: this.headers(), }); return this.mapper.map(response); } /** * Delete a bookmark list for a user * @param userId User Id * @example * * ```typescript * * client.bookmarkList.createList("1234", { * name: "My List", * source: "dotcom", * visibility: "public" * }); * ``` */ public async deleteList(userId: string, listId: string) { await this.http.fetch(`${this.url({ userId })}/${listId}`, { method: "DELETE", body: { data: {}, }, headers: this.headers(), }); return; } /** * Update a list * @param userId User Id * @param list List updates * @example * * ```typescript * * client.bookmarkList.updateList("1234", { * name: "My List", * source: "dotcom", * visibility: "public" * }); * ``` */ public async updateList(userId: string, list: IBookmarkList) { const resource = await this.http.fetch( `${this.url({ userId })}/${list.id}`, { method: "PATCH", body: { data: this.mapper.toResource(list), }, headers: this.headers(), }); return this.mapper.map(resource); } }