diff --git a/src/endpoints/Memberships.js b/src/endpoints/Memberships.js index db32423b..d9e6d781 100644 --- a/src/endpoints/Memberships.js +++ b/src/endpoints/Memberships.js @@ -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"; @@ -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>("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 }); } } diff --git a/src/types.js b/src/types.js index 44358834..a9d72dbf 100644 --- a/src/types.js +++ b/src/types.js @@ -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, diff --git a/tests/endpoints/Memberships.test.js b/tests/endpoints/Memberships.test.js index 71a02308..8dc480ea 100644 --- a/tests/endpoints/Memberships.test.js +++ b/tests/endpoints/Memberships.test.js @@ -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" @@ -62,7 +62,7 @@ describe("memberships", () => { }); test("api - organization", async () => { - mockAPI("/organizations/org-id/memberships", { + mockAPI("/organizations/org-id/memberships?", { data: [ { userId: "user-id"