Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accept generic options in Users.getFavorites() #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/modules/Users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import type {
SearchUsersOrder,
Timezones,
DefaultImageSize,
PostProperties
PostProperties,
GetFavoritesOptions
} from "../types";
import FormHelper from "../util/FormHelper";
import { APIError } from "../util/RequestHandler";
Expand Down Expand Up @@ -197,14 +198,22 @@ export default class Users {
/**
* Get the favorites of a user
*
* @param {number} [id] - the id of the user to get favorites for, or none to get the authenticated user, if provided
* @param {object} [options]
* @param {number} [options.id] - the id of the user to get favorites for, or none to get the authenticated user, if provided
* @param {number} [options.page] - page of results to get
* @param {number} [options.limit] - limit the maximum amount of results returned
*/
async getFavorites(id?: number) {
if (!id) {
async getFavorites(options?: GetFavoritesOptions) {
options = options ?? {};
if (!options.id) {
const auth = this.main.request.authCheck.call(this, "Users#getFavorites", false);
if (!auth) throw new Error("calling Users#getFavorites without an id requires authentication");
}
const res = await this.main.request.get<{ posts: Array<PostProperties>; }>(`/favorites.json${!id ? "" : `?user_id=${id}`}`);
const qs = new FormHelper();
if (typeof options.id !== "undefined") qs.add("user_id", options.id);
if (typeof options.page !== "undefined") qs.add("page", options.page);
if (typeof options.limit === "number" || typeof options.limit === "string") qs.add("limit", options.limit);
const res = await this.main.request.get<{ posts: Array<PostProperties>; }>(`/favorites.json?${qs.build()}`);
return res!.posts.map(info => new Post(this.main, info));
}

Expand Down
8 changes: 6 additions & 2 deletions src/structures/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import type {
SearchUserFeedbackOptions,
CreateUserFeedbackOptions,
UserFeedbackProperties,
FeedbackCategories
FeedbackCategories,
GenericSearchOptions
} from "../types";

export default class User implements UserProperties {
Expand Down Expand Up @@ -76,9 +77,12 @@ export default class User implements UserProperties {
/**
* Get this users favorites
*
* @param {object} [options]
* @param {number} [options.page] - page of results to get
* @param {number} [options.limit] - limit the maximum amount of results returned
* @returns {Promise<Array<Post>>}
*/
async getFavorites() { return this.main.users.getFavorites(this.id); }
async getFavorites(options?: GenericSearchOptions) { return this.main.users.getFavorites({ id: this.id, ...options }); }

async asAuthenticatedUser() {
this.main.request.authCheck("User#asAuthenticatedUser");
Expand Down
4 changes: 4 additions & 0 deletions src/types/users.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,7 @@ export interface EditSelfUserOptions {
disable_responsive_mode?: boolean;
custom_css_style?: string;
}

export interface GetFavoritesOptions extends GenericSearchOptions {
id?: number;
}
16 changes: 14 additions & 2 deletions test/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,20 @@ describe("Users", function() {
// @TODO Get Upload Limit Test
it.skip("get upload limit", async function() {});

// @TODO Get Favorites Test
it.skip("get favorites", async function() {});
it("get favorites", async function() {
const limit = 5;
const favorites = await E6Client.users.getFavorites({ id: 323290, limit });
expect(favorites.length).to.not.equal(0, "get favorites returned zero results");
expect(favorites.length).to.lte(limit, "get favorites not respecting limit");
});

it("get favorites from user object", async function() {
const limit = 5;
const user = await E6Client.users.get(323290);
const favorites = await user?.getFavorites({ limit }) || [];
expect(favorites.length).to.not.equal(0, "get favorites from user returned zero results");
expect(favorites.length).to.lte(limit, "get favorites not respecting limit");
});

// @TODO Add Favorite Test
it.skip("add favorite", async function() {});
Expand Down