Skip to content

Commit 763eeff

Browse files
authored
feat(organizations): implement new organizations endpoint
1 parent 40426d1 commit 763eeff

File tree

8 files changed

+98
-7
lines changed

8 files changed

+98
-7
lines changed

lib/create-contentful-api.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import entities from './entities'
66
* @property {function(id: string): Promise<Space>} getSpace - Gets a space with the spcified id
77
* @property {function(): Promise<SpaceCollection>} getSpaces - Gets a collection of spaces
88
* @property {function(data: {name: string}): Promise<Space>} createSpace - Creates a space
9+
* @property {function(): Promise<OrganizationCollection>} getOrganizations - Gets a collection of Organizations
910
*/
1011

1112
/**
@@ -17,8 +18,9 @@ import entities from './entities'
1718
* @prop {Function} shouldLinksResolve - Link resolver preconfigured with global setting
1819
* @return {ClientAPI}
1920
*/
20-
export default function createSpaceApi ({ http }) {
21+
export default function createClientApi ({ http }) {
2122
const {wrapSpace, wrapSpaceCollection} = entities.space
23+
const {wrapOrganizationCollection} = entities.organization
2224

2325
/**
2426
* Gets all spaces
@@ -64,9 +66,26 @@ export default function createSpaceApi ({ http }) {
6466
.then((response) => wrapSpace(http, response.data), errorHandler)
6567
}
6668

69+
/**
70+
* Gets a collection of Organizations
71+
* @memberof ClientAPI
72+
* @return {Promise<OrganizationCollection>} Promise for a collection of Organizations
73+
* @example
74+
* space.getOrganizations()
75+
* .then(result => console.log(result.items))
76+
*/
77+
function getOrganizations () {
78+
const baseURL = http.defaults.baseURL.replace('/spaces/', '/organizations/')
79+
return http.get('', {
80+
baseURL
81+
})
82+
.then((response) => wrapOrganizationCollection(http, response.data), errorHandler)
83+
}
84+
6785
return {
6886
getSpaces: getSpaces,
6987
getSpace: getSpace,
70-
createSpace: createSpace
88+
createSpace: createSpace,
89+
getOrganizations: getOrganizations
7190
}
7291
}

lib/entities/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as spaceMembership from './space-membership'
99
import * as role from './role'
1010
import * as apiKey from './api-key'
1111
import * as upload from './upload'
12+
import * as organization from './organization'
1213

1314
export default {
1415
space,
@@ -21,5 +22,6 @@ export default {
2122
spaceMembership,
2223
role,
2324
apiKey,
24-
upload
25+
upload,
26+
organization
2527
}

lib/entities/organization.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import cloneDeep from 'lodash/cloneDeep'
2+
import { freezeSys, toPlainObject } from 'contentful-sdk-core'
3+
4+
/**
5+
* This method normalizes each organization in a collection.
6+
* @private
7+
* @param {Object} http - HTTP client instance
8+
* @param {Object} data - Raw organization collection data
9+
* @return {OrganizationCollection} Normalized organization collection data
10+
*/
11+
export function wrapOrganizationCollection (http, data) {
12+
const organizations = toPlainObject(cloneDeep(data))
13+
return freezeSys(organizations)
14+
}

test/integration/integration-tests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ test('Gets spaces', (t) => {
3232
})
3333
})
3434

35+
test('Gets organizations', (t) => {
36+
t.plan(1)
37+
return client.getOrganizations()
38+
.then((response) => {
39+
t.ok(response.items.length >= 1, 'user must belong to at least one organization')
40+
})
41+
})
42+
3543
test('Gets space', (t) => {
3644
t.plan(2)
3745
return client.getSpace('cfexampleapi')

test/unit/create-contentful-api-test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import test from 'blue-tape'
22
import { Promise } from 'es6-promise'
33

4-
import {spaceMock, setupEntitiesMock} from './mocks/entities'
4+
import {spaceMock, setupEntitiesMock, organizationMock} from './mocks/entities'
55
import setupHttpMock from './mocks/http'
66
import createContentfulApi, {__RewireAPI__ as createContentfulApiRewireApi} from '../../lib/create-contentful-api'
77
import {makeGetEntityTest, makeGetCollectionTest, makeEntityMethodFailingTest} from './test-creators/static-entity-methods'
@@ -49,6 +49,20 @@ test('API call getSpace fails', (t) => {
4949
})
5050
})
5151

52+
test('API call getOrganizations', (t) => {
53+
makeGetCollectionTest(t, setup, teardown, {
54+
entityType: 'organization',
55+
mockToReturn: organizationMock,
56+
methodToTest: 'getOrganizations'
57+
})
58+
})
59+
60+
test('API call getOrganizations fails', (t) => {
61+
makeEntityMethodFailingTest(t, setup, teardown, {
62+
methodToTest: 'getOrganizations'
63+
})
64+
})
65+
5266
test('API call createSpace', (t) => {
5367
t.plan(3)
5468
const data = {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import test from 'tape'
2+
import { cloneMock } from '../mocks/entities'
3+
import setupHttpMock from '../mocks/http'
4+
import { wrapOrganizationCollection } from '../../../lib/entities/organization'
5+
import {
6+
entityCollectionWrappedTest
7+
} from '../test-creators/instance-entity-methods'
8+
9+
function setup (promise) {
10+
return {
11+
httpMock: setupHttpMock(promise),
12+
entityMock: cloneMock('organization')
13+
}
14+
}
15+
16+
test('Organization collection is wrapped', (t) => {
17+
entityCollectionWrappedTest(t, setup, {
18+
wrapperMethod: wrapOrganizationCollection
19+
})
20+
})

test/unit/mocks/entities.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ const apiKeyMock = {
150150
})
151151
}
152152

153+
const organizationMock = {
154+
sys: assign(cloneDeep(sysMock), {
155+
type: 'Organization'
156+
})
157+
}
158+
153159
const errorMock = {
154160
config: {
155161
url: 'requesturl',
@@ -175,7 +181,8 @@ const mocks = {
175181
role: roleMock,
176182
apiKey: apiKeyMock,
177183
error: errorMock,
178-
upload: uploadMock
184+
upload: uploadMock,
185+
organization: organizationMock
179186
}
180187

181188
function cloneMock (name) {
@@ -234,6 +241,9 @@ function setupEntitiesMock (rewiredModuleApi) {
234241
},
235242
upload: {
236243
wrapUpload: sinon.stub()
244+
},
245+
organization: {
246+
wrapOrganizationCollection: sinon.stub()
237247
}
238248
}
239249
rewiredModuleApi.__Rewire__('entities', entitiesMock)
@@ -259,5 +269,6 @@ export {
259269
cloneMock,
260270
mockCollection,
261271
setupEntitiesMock,
262-
uploadMock
272+
uploadMock,
273+
organizationMock
263274
}

test/unit/mocks/http.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ export default function setupHttpMock (promise = Promise.resolve({data: {}})) {
66
get: sinon.stub().returns(promise),
77
post: sinon.stub().returns(promise),
88
put: sinon.stub().returns(promise),
9-
delete: sinon.stub().returns(promise)
9+
delete: sinon.stub().returns(promise),
10+
defaults: {
11+
baseURL: 'https://api.contentful.com/spaces/'
12+
}
1013
}
1114
}

0 commit comments

Comments
 (0)