From f00ec540686c2b2c1453cc68fe9e1eebcc89560e Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Thu, 4 Jun 2020 15:34:13 -0700 Subject: [PATCH 1/2] feat: add limit and offset to membership lists --- src/endpoints/Memberships.js | 26 +++++++++++++++++++++----- tests/endpoints/Memberships.test.js | 4 ++-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/endpoints/Memberships.js b/src/endpoints/Memberships.js index db32423b..5ae73e86 100644 --- a/src/endpoints/Memberships.js +++ b/src/endpoints/Memberships.js @@ -1,4 +1,6 @@ // @flow +import querystring from "querystring"; +import { pick } from "lodash"; import type { Membership, OrganizationDescriptor, @@ -10,6 +12,16 @@ import type { import Endpoint from "../endpoints/Endpoint"; import { wrap } from "../util/helpers"; +type MembershipsListOptions = { + ...RequestOptions, + limit?: number, + offset?: number +}; + +const headers = { + "Abstract-Api-Version": "20" +}; + export default class Memberships extends Endpoint { name = "memberships"; @@ -38,24 +50,28 @@ export default class Memberships extends Endpoint { list( descriptor: OrganizationDescriptor | ProjectDescriptor, - requestOptions: RequestOptions = {} + requestOptions: MembershipsListOptions = {} ) { + const { limit, offset, ...restOptions } = requestOptions; + const options = pick(requestOptions, ["limit", "offset"]); + const query = querystring.stringify(options); + return this.configureRequest>("list", { api: async () => { let url = ""; if (descriptor.organizationId) { - url = `organizations/${descriptor.organizationId}/memberships`; + url = `organizations/${descriptor.organizationId}/memberships?${query}`; } if (descriptor.projectId) { - url = `projects/${descriptor.projectId}/memberships`; + url = `projects/${descriptor.projectId}/memberships?${query}`; } - const response = await this.apiRequest(url); + const response = await this.apiRequest(url, { headers }); return wrap(response.data, response); }, - requestOptions + restOptions }); } } 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" From 321d94c5049f4dab31aaa30018e8316c6c3e7a94 Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Fri, 5 Jun 2020 13:51:16 -0700 Subject: [PATCH 2/2] add membership filtering --- src/endpoints/Memberships.js | 46 +++++++++++++++++++++++++++++++----- src/types.js | 5 ++++ 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/endpoints/Memberships.js b/src/endpoints/Memberships.js index 5ae73e86..d9e6d781 100644 --- a/src/endpoints/Memberships.js +++ b/src/endpoints/Memberships.js @@ -7,7 +7,10 @@ import type { OrganizationMembershipDescriptor, ProjectDescriptor, ProjectMembershipDescriptor, - RequestOptions + RequestOptions, + OrganizationRole, + SubscriptionRole, + ProjectRoleFilter } from "../types"; import Endpoint from "../endpoints/Endpoint"; import { wrap } from "../util/helpers"; @@ -15,7 +18,11 @@ import { wrap } from "../util/helpers"; type MembershipsListOptions = { ...RequestOptions, limit?: number, - offset?: number + offset?: number, + organizationRole?: OrganizationRole, + subscriptionRole?: SubscriptionRole, + projectRole?: ProjectRoleFilter, + query?: string }; const headers = { @@ -52,20 +59,47 @@ export default class Memberships extends Endpoint { descriptor: OrganizationDescriptor | ProjectDescriptor, requestOptions: MembershipsListOptions = {} ) { - const { limit, offset, ...restOptions } = requestOptions; + const { + limit, + offset, + organizationRole, + projectRole, + subscriptionRole, + query, + ...restOptions + } = requestOptions; const options = pick(requestOptions, ["limit", "offset"]); - const query = querystring.stringify(options); return this.configureRequest>("list", { api: async () => { let url = ""; if (descriptor.organizationId) { - url = `organizations/${descriptor.organizationId}/memberships?${query}`; + 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?${query}`; + if (projectRole) { + options.role = projectRole; + } + if (query) { + options.search = query; + } + url = `projects/${ + descriptor.projectId + }/memberships?${querystring.stringify(options)}`; } const response = await this.apiRequest(url, { headers }); 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,