Skip to content

Commit

Permalink
feat: expose cma in the sdk [EXT-3599] (#1536)
Browse files Browse the repository at this point in the history
* feat(cma): expose cma in the sdk [EXT-3599]

* fix api spec

* change plainclient to cma client

* add test

* changes to correct typing

* move contentful-management from peer to direct dependency

* fix: use correct plain client type

* fix: import correct type

* chore: cleanup

* revert: client types

* chore: remove contentful-management from dev dependencies

---------

Co-authored-by: Mitch Goudy <mgoudy91@gmail.com>
Co-authored-by: Yuri Mazursky <colomolo@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 22, 2023
1 parent c378eaf commit 942c5e0
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 86 deletions.
4 changes: 3 additions & 1 deletion lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from './types'
import { Channel } from './channel'
import { createAdapter } from './cmaAdapter'
import { createCMAClient } from './cma'

const DEFAULT_API_PRODUCERS = [
makeSharedAPI,
Expand Down Expand Up @@ -60,9 +61,10 @@ function makeSharedAPI(channel: Channel, data: ConnectMessage): BaseAppSDK {
const currentLocation = data.location || locations.LOCATION_ENTRY_FIELD

return {
cma: createCMAClient(ids, channel),
cmaAdapter: createAdapter(channel),
location: {
is: (tested) => currentLocation === tested,
is: (tested: string) => currentLocation === tested,
},
user,
parameters,
Expand Down
19 changes: 19 additions & 0 deletions lib/cma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createClient } from 'contentful-management'
import { createAdapter } from './cmaAdapter'
import { CMAClient } from './types/cmaClient.types'
import type { IdsAPI } from './types/api.types'
import type { Channel } from './channel'

export function createCMAClient(ids: IdsAPI, channel: Channel): CMAClient {
return createClient(
{ apiAdapter: createAdapter(channel) },
{
type: 'plain',
defaults: {
environmentId: ids.environmentAlias ?? ids.environment,
spaceId: ids.space,
organizationId: ids.organization,
},
}
)
}
3 changes: 3 additions & 0 deletions lib/types/api.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { AppConfigAPI } from './app.types'
import { NavigatorAPI } from './navigator.types'
import { EntryFieldInfo, FieldInfo } from './field.types'
import { Adapter, KeyValueMap } from 'contentful-management/types'
import { CMAClient } from './cmaClient.types'

/* User API */

Expand Down Expand Up @@ -236,6 +237,8 @@ export interface BaseAppSDK {
ids: Omit<IdsAPI, EntryScopedIds>
/** Adapter to be injected in contentful-management client */
cmaAdapter: Adapter
/** A CMA Client initialized with default params */
cma: CMAClient
}

export type EditorAppSDK = Omit<BaseAppSDK, 'ids'> &
Expand Down
Loading

0 comments on commit 942c5e0

Please sign in to comment.