Skip to content

Commit 70c117c

Browse files
committed
feat: Add support for webhooks, roles, space memberships and api keys apis
1 parent d86ecbe commit 70c117c

17 files changed

+1418
-6
lines changed

lib/create-space-api.js

Lines changed: 253 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export default function createSpaceApi ({
3636
const {wrapEntry, wrapEntryCollection} = entities.entry
3737
const {wrapAsset, wrapAssetCollection} = entities.asset
3838
const {wrapLocale, wrapLocaleCollection} = entities.locale
39+
const {wrapWebhook, wrapWebhookCollection} = entities.webhook
40+
const {wrapRole, wrapRoleCollection} = entities.role
41+
const {wrapSpaceMembership, wrapSpaceMembershipCollection} = entities.spaceMembership
42+
const {wrapApiKey, wrapApiKeyCollection} = entities.apiKey
3943

4044
/**
4145
* Space instances.
@@ -352,6 +356,238 @@ export default function createSpaceApi ({
352356
.then((response) => wrapLocale(http, response.data), errorHandler)
353357
}
354358

359+
/**
360+
* Gets a Webhook
361+
* @memberof ContentfulSpaceAPI
362+
* @param {string} id
363+
* @return {Promise<Entities.Webhook>} Promise for a Webhook
364+
* @example
365+
* space.getWebhook('id')
366+
* .then(webhook => console.log(webhook))
367+
*/
368+
function getWebhook (id) {
369+
return http.get('webhook_definitions/' + id)
370+
.then((response) => wrapWebhook(http, response.data), errorHandler)
371+
}
372+
373+
/**
374+
* Gets a collection of Webhooks
375+
* @memberof ContentfulSpaceAPI
376+
* @return {Promise<Entities.WebhookCollection>} Promise for a collection of Webhooks
377+
* @example
378+
* space.getWebhooks()
379+
* .then(webhooks => console.log(webhooks.items))
380+
*/
381+
function getWebhooks () {
382+
return http.get('webhook_definitions')
383+
.then((response) => wrapWebhookCollection(http, response.data), errorHandler)
384+
}
385+
386+
/**
387+
* Creates a Webhook
388+
* @memberof ContentfulSpaceAPI
389+
* @see {Entities.Webhook}
390+
* @param {object} data - Object representation of the Webhook to be created
391+
* @return {Promise<Entities.Webhook>} Promise for the newly created Webhook
392+
* @example
393+
* space.createWebhook({})
394+
* .then(webhook => console.log(webhook))
395+
*/
396+
function createWebhook (data) {
397+
return http.post('webhook_definitions', data)
398+
.then((response) => wrapWebhook(http, response.data), errorHandler)
399+
}
400+
401+
/**
402+
* Creates a Webhook with a specific id
403+
* @memberof ContentfulSpaceAPI
404+
* @see {Entities.Webhook}
405+
* @param {string} id - Webhook ID
406+
* @param {object} data - Object representation of the Webhook to be created
407+
* @return {Promise<Entities.Webhook>} Promise for the newly created Webhook
408+
* @example
409+
* space.createWebhook('id', {})
410+
* .then(webhook => console.log(webhook))
411+
*/
412+
function createWebhookWithId (id, data) {
413+
return http.put('webhook_definitions/' + id, data)
414+
.then((response) => wrapWebhook(http, response.data), errorHandler)
415+
}
416+
417+
/**
418+
* Gets a Space Membership
419+
* @memberof ContentfulSpaceAPI
420+
* @param {string} id
421+
* @return {Promise<Entities.SpaceMembership>} Promise for a Space Membership
422+
* @example
423+
* space.getSpaceMembership('id')
424+
* .then(spaceMembership => console.log(spaceMembership))
425+
*/
426+
function getSpaceMembership (id) {
427+
return http.get('space_memberships/' + id)
428+
.then((response) => wrapSpaceMembership(http, response.data), errorHandler)
429+
}
430+
431+
/**
432+
* Gets a collection of Space Memberships
433+
* @memberof ContentfulSpaceAPI
434+
* @return {Promise<Entities.SpaceMembershipCollection>} Promise for a collection of Space Memberships
435+
* @example
436+
* space.getSpaceMemberships()
437+
* .then(spaceMemberships => console.log(spaceMemberships.items))
438+
*/
439+
function getSpaceMemberships () {
440+
return http.get('space_memberships')
441+
.then((response) => wrapSpaceMembershipCollection(http, response.data), errorHandler)
442+
}
443+
444+
/**
445+
* Creates a Space Membership
446+
* @memberof ContentfulSpaceAPI
447+
* @see {Entities.SpaceMembership}
448+
* @param {object} data - Object representation of the Space Membership to be created
449+
* @return {Promise<Entities.SpaceMembership>} Promise for the newly created Space Membership
450+
* @example
451+
* space.createSpaceMembership({})
452+
* .then(spaceMembership => console.log(spaceMembership))
453+
*/
454+
function createSpaceMembership (data) {
455+
return http.post('space_memberships', data)
456+
.then((response) => wrapSpaceMembership(http, response.data), errorHandler)
457+
}
458+
459+
/**
460+
* Creates a Space Membership with a specific id
461+
* @memberof ContentfulSpaceAPI
462+
* @see {Entities.SpaceMembership}
463+
* @param {string} id - Space Membership ID
464+
* @param {object} data - Object representation of the Space Membership to be created
465+
* @return {Promise<Entities.SpaceMembership>} Promise for the newly created Space Membership
466+
* @example
467+
* space.createSpaceMembership('id', {})
468+
* .then(spaceMembership => console.log(spaceMembership))
469+
*/
470+
function createSpaceMembershipWithId (id, data) {
471+
return http.put('space_memberships/' + id, data)
472+
.then((response) => wrapSpaceMembership(http, response.data), errorHandler)
473+
}
474+
475+
/**
476+
* Gets a Role
477+
* @memberof ContentfulSpaceAPI
478+
* @param {string} id
479+
* @return {Promise<Entities.Role>} Promise for a Role
480+
* @example
481+
* space.getRole('id')
482+
* .then(role => console.log(role))
483+
*/
484+
function getRole (id) {
485+
return http.get('roles/' + id)
486+
.then((response) => wrapRole(http, response.data), errorHandler)
487+
}
488+
489+
/**
490+
* Gets a collection of Roles
491+
* @memberof ContentfulSpaceAPI
492+
* @return {Promise<Entities.RoleCollection>} Promise for a collection of Roles
493+
* @example
494+
* space.getRoles()
495+
* .then(roles => console.log(roles.items))
496+
*/
497+
function getRoles () {
498+
return http.get('roles')
499+
.then((response) => wrapRoleCollection(http, response.data), errorHandler)
500+
}
501+
502+
/**
503+
* Creates a Role
504+
* @memberof ContentfulSpaceAPI
505+
* @see {Entities.Role}
506+
* @param {object} data - Object representation of the Role to be created
507+
* @return {Promise<Entities.Role>} Promise for the newly created Role
508+
* @example
509+
* space.createRole({})
510+
* .then(role => console.log(role))
511+
*/
512+
function createRole (data) {
513+
return http.post('roles', data)
514+
.then((response) => wrapRole(http, response.data), errorHandler)
515+
}
516+
517+
/**
518+
* Creates a Role with a specific id
519+
* @memberof ContentfulSpaceAPI
520+
* @see {Entities.Role}
521+
* @param {string} id - Role ID
522+
* @param {object} data - Object representation of the Role to be created
523+
* @return {Promise<Entities.Role>} Promise for the newly created Role
524+
* @example
525+
* space.createRole('id', {})
526+
* .then(role => console.log(role))
527+
*/
528+
function createRoleWithId (id, data) {
529+
return http.put('roles/' + id, data)
530+
.then((response) => wrapRole(http, response.data), errorHandler)
531+
}
532+
533+
/**
534+
* Gets a Api Key
535+
* @memberof ContentfulSpaceAPI
536+
* @param {string} id
537+
* @return {Promise<Entities.ApiKey>} Promise for a Api Key
538+
* @example
539+
* space.getApiKey('id')
540+
* .then(apiKey => console.log(apiKey))
541+
*/
542+
function getApiKey (id) {
543+
return http.get('api_keys/' + id)
544+
.then((response) => wrapApiKey(http, response.data), errorHandler)
545+
}
546+
547+
/**
548+
* Gets a collection of Api Keys
549+
* @memberof ContentfulSpaceAPI
550+
* @return {Promise<Entities.ApiKeyCollection>} Promise for a collection of Api Keys
551+
* @example
552+
* space.getApiKeys()
553+
* .then(apiKeys => console.log(apiKeys.items))
554+
*/
555+
function getApiKeys () {
556+
return http.get('api_keys')
557+
.then((response) => wrapApiKeyCollection(http, response.data), errorHandler)
558+
}
559+
560+
/**
561+
* Creates a Api Key
562+
* @memberof ContentfulSpaceAPI
563+
* @see {Entities.ApiKey}
564+
* @param {object} data - Object representation of the Api Key to be created
565+
* @return {Promise<Entities.ApiKey>} Promise for the newly created Api Key
566+
* @example
567+
* space.createApiKey({})
568+
* .then(apiKey => console.log(apiKey))
569+
*/
570+
function createApiKey (data) {
571+
return http.post('api_keys', data)
572+
.then((response) => wrapApiKey(http, response.data), errorHandler)
573+
}
574+
575+
/**
576+
* Creates a Api Key with a specific id
577+
* @memberof ContentfulSpaceAPI
578+
* @see {Entities.ApiKey}
579+
* @param {string} id - Api Key ID
580+
* @param {object} data - Object representation of the Api Key to be created
581+
* @return {Promise<Entities.ApiKey>} Promise for the newly created Api Key
582+
* @example
583+
* space.createApiKey('id', {})
584+
* .then(apiKey => console.log(apiKey))
585+
*/
586+
function createApiKeyWithId (id, data) {
587+
return http.put('api_keys/' + id, data)
588+
.then((response) => wrapApiKey(http, response.data), errorHandler)
589+
}
590+
355591
return {
356592
delete: deleteSpace,
357593
update: updateSpace,
@@ -369,6 +605,22 @@ export default function createSpaceApi ({
369605
createAssetWithId: createAssetWithId,
370606
getLocale: getLocale,
371607
getLocales: getLocales,
372-
createLocale: createLocale
608+
createLocale: createLocale,
609+
getWebhook: getWebhook,
610+
getWebhooks: getWebhooks,
611+
createWebhook: createWebhook,
612+
createWebhookWithId: createWebhookWithId,
613+
getRole: getRole,
614+
getRoles: getRoles,
615+
createRole: createRole,
616+
createRoleWithId: createRoleWithId,
617+
getSpaceMembership: getSpaceMembership,
618+
getSpaceMemberships: getSpaceMemberships,
619+
createSpaceMembership: createSpaceMembership,
620+
createSpaceMembershipWithId: createSpaceMembershipWithId,
621+
getApiKey: getApiKey,
622+
getApiKeys: getApiKeys,
623+
createApiKey: createApiKey,
624+
createApiKeyWithId: createApiKeyWithId
373625
}
374626
}

lib/entities/api-key.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* Api Key instances
3+
* @namespace ApiKey
4+
*/
5+
import {cloneDeep} from 'lodash/lang'
6+
import freezeSys from 'contentful-sdk-core/freeze-sys'
7+
import enhanceWithMethods from '../enhance-with-methods'
8+
import mixinToPlainObject from 'contentful-sdk-core/mixins/to-plain-object'
9+
import {
10+
createUpdateEntity,
11+
createDeleteEntity
12+
} from '../instance-actions'
13+
14+
/**
15+
* @memberof ApiKey
16+
* @typedef ApiKey
17+
* @prop {Entities.Sys} sys - System metadata
18+
* @prop {string} name
19+
* @prop {string} description
20+
* @prop {function(): Object} toPlainObject() - Returns this Api Key as a plain JS object
21+
*/
22+
23+
function createApiKeyApi (http) {
24+
return {
25+
26+
/**
27+
* Sends an update to the server with any changes made to the object's properties
28+
* @memberof ApiKey
29+
* @func update
30+
* @return {Promise<ApiKey>} Object returned from the server with updated changes.
31+
* @example
32+
* apiKey.name = 'New name'
33+
* apiKey.update()
34+
* .then(apiKey => console.log(apiKey.name))
35+
*/
36+
update: createUpdateEntity({
37+
http: http,
38+
entityPath: 'api_keys',
39+
wrapperMethod: wrapApiKey
40+
}),
41+
42+
/**
43+
* Deletes this object on the server.
44+
* @memberof ApiKey
45+
* @func delete
46+
* @return {Promise} Promise for the deletion. It contains no data, but the Promise error case should be handled.
47+
* @example
48+
* apiKey.delete()
49+
* .catch(err => console.log(err))
50+
*/
51+
delete: createDeleteEntity({
52+
http: http,
53+
entityPath: 'api_keys'
54+
})
55+
}
56+
}
57+
58+
/**
59+
* @private
60+
* @param {Object} http - HTTP client instance
61+
* @param {Object} data - Raw api key data
62+
* @return {ApiKey} Wrapped api key data
63+
*/
64+
export function wrapApiKey (http, data) {
65+
const apiKey = mixinToPlainObject(cloneDeep(data))
66+
enhanceWithMethods(apiKey, createApiKeyApi(http))
67+
return freezeSys(apiKey)
68+
}
69+
70+
/**
71+
* @memberof Entities
72+
* @typedef ApiKeyCollection
73+
* @prop {number} total
74+
* @prop {number} skip
75+
* @prop {number} limit
76+
* @prop {Array<Entities.ApiKey>} items
77+
* @prop {function(): Object} toPlainObject() - Returns this Api Key collection as a plain JS object
78+
*/
79+
80+
/**
81+
* @private
82+
* @param {Object} http - HTTP client instance
83+
* @param {Object} data - Raw api key collection data
84+
* @return {ApiKeyCollection} Wrapped api key collection data
85+
*/
86+
export function wrapApiKeyCollection (http, data) {
87+
const apiKeys = mixinToPlainObject(cloneDeep(data))
88+
apiKeys.items = apiKeys.items.map((entity) => wrapApiKey(http, entity))
89+
return freezeSys(apiKeys)
90+
}

lib/entities/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@ import * as entry from './entry'
33
import * as asset from './asset'
44
import * as contentType from './content-type'
55
import * as locale from './locale'
6+
import * as webhook from './webhook'
7+
import * as spaceMembership from './space-membership'
8+
import * as role from './role'
9+
import * as apiKey from './api-key'
610

711
export default {
812
space,
913
entry,
1014
asset,
1115
contentType,
12-
locale
16+
locale,
17+
webhook,
18+
spaceMembership,
19+
role,
20+
apiKey
1321
}

0 commit comments

Comments
 (0)