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

feat: add limit and offset to membership lists #278

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
62 changes: 56 additions & 6 deletions src/endpoints/Memberships.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
// @flow
import querystring from "querystring";
import { pick } from "lodash";
import type {
Membership,
OrganizationDescriptor,
OrganizationMembershipDescriptor,
ProjectDescriptor,
ProjectMembershipDescriptor,
RequestOptions
RequestOptions,
OrganizationRole,
SubscriptionRole,
ProjectRoleFilter
} from "../types";
import Endpoint from "../endpoints/Endpoint";
import { wrap } from "../util/helpers";

type MembershipsListOptions = {
...RequestOptions,
limit?: number,
offset?: number,
organizationRole?: OrganizationRole,
subscriptionRole?: SubscriptionRole,
projectRole?: ProjectRoleFilter,
query?: string
};

const headers = {
"Abstract-Api-Version": "20"
};

export default class Memberships extends Endpoint {
name = "memberships";

Expand Down Expand Up @@ -38,24 +57,55 @@ export default class Memberships extends Endpoint {

list(
descriptor: OrganizationDescriptor | ProjectDescriptor,
requestOptions: RequestOptions = {}
requestOptions: MembershipsListOptions = {}
) {
const {
limit,
offset,
organizationRole,
projectRole,
subscriptionRole,
query,
...restOptions
} = requestOptions;
const options = pick(requestOptions, ["limit", "offset"]);

return this.configureRequest<Promise<Membership[]>>("list", {
api: async () => {
let url = "";

if (descriptor.organizationId) {
url = `organizations/${descriptor.organizationId}/memberships`;
if (organizationRole) {
options.role = organizationRole;
}
if (subscriptionRole) {
options.subscription_role = subscriptionRole;
}
if (query) {
options.search = query;
}
url = `organizations/${
descriptor.organizationId
}/memberships?${querystring.stringify(options)}`;
console.log(url);
}

if (descriptor.projectId) {
url = `projects/${descriptor.projectId}/memberships`;
if (projectRole) {
options.role = projectRole;
}
if (query) {
options.search = query;
}
url = `projects/${
descriptor.projectId
}/memberships?${querystring.stringify(options)}`;
}

const response = await this.apiRequest(url);
const response = await this.apiRequest(url, { headers });
return wrap(response.data, response);
},
requestOptions
restOptions
});
}
}
5 changes: 5 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,11 @@ export type OrganizationMembershipDescriptor = {|
organizationId: string
|};

export type OrganizationRole = "member" | "owner" | "guest";
export type SubscriptionRole = "viewer" | "contributor";
export type ProjectMembershipRole = "member" | "admin";
export type ProjectRoleFilter = ProjectMembershipRole | "guest";

export type Membership = {
createdAt: string,
organizationId: string,
Expand Down
4 changes: 2 additions & 2 deletions tests/endpoints/Memberships.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe("memberships", () => {

describe("list", () => {
test("api - project", async () => {
mockAPI("/projects/project-id/memberships", {
mockAPI("/projects/project-id/memberships?", {
data: [
{
userId: "user-id"
Expand All @@ -62,7 +62,7 @@ describe("memberships", () => {
});

test("api - organization", async () => {
mockAPI("/organizations/org-id/memberships", {
mockAPI("/organizations/org-id/memberships?", {
data: [
{
userId: "user-id"
Expand Down