Skip to content

Commit

Permalink
feat: adds optional types for parameters [EXT-4381] (#1538)
Browse files Browse the repository at this point in the history
* feat: adds optional types for installation, instance, and invocation parameters

---------

Co-authored-by: Andi Pätzold <[email protected]>
  • Loading branch information
ghepting and andipaetzold authored Jun 22, 2023
1 parent 198c8fe commit 7cbad71
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 23 deletions.
6 changes: 5 additions & 1 deletion lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { Channel } from './channel'
import { createAdapter } from './cmaAdapter'
import { createCMAClient } from './cma'
import { KeyValueMap } from 'contentful-management/types'

const DEFAULT_API_PRODUCERS = [
makeSharedAPI,
Expand Down Expand Up @@ -56,7 +57,10 @@ export default function createAPI(
}, {}) as any
}

function makeSharedAPI(channel: Channel, data: ConnectMessage): BaseAppSDK {
function makeSharedAPI(
channel: Channel,
data: ConnectMessage
): BaseAppSDK<KeyValueMap, KeyValueMap, never> {
const { user, parameters, locales, ids, initialContentTypes } = data
const currentLocation = data.location || locations.LOCATION_ENTRY_FIELD

Expand Down
77 changes: 55 additions & 22 deletions lib/types/api.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ export interface LocationAPI {

/* Parameters API */

export interface ParametersAPI {
installation: KeyValueMap
instance: KeyValueMap
invocation?: SerializedJSONValue
export interface ParametersAPI<
InstallationParameters extends KeyValueMap,
InstanceParameters extends KeyValueMap,
InvocationParameters extends SerializedJSONValue
> {
installation: InstallationParameters
instance: InstanceParameters
invocation: InvocationParameters
}

/* IDs */
Expand Down Expand Up @@ -212,7 +216,11 @@ export interface AccessAPI {

type EntryScopedIds = 'field' | 'entry' | 'contentType'

export interface BaseAppSDK {
export interface BaseAppSDK<
InstallationParameters extends KeyValueMap = KeyValueMap,
InstanceParameters extends KeyValueMap = KeyValueMap,
InvocationParameters extends SerializedJSONValue = SerializedJSONValue
> {
/** @deprecated since version 4.0.0 consider using the CMA instead
* See https://www.contentful.com/developers/docs/extensibility/app-framework/sdk/#using-the-contentful-management-library for more details
*/
Expand All @@ -228,7 +236,7 @@ export interface BaseAppSDK {
/** Methods for displaying notifications. */
notifier: NotifierAPI
/** Exposes app configuration parameters */
parameters: ParametersAPI
parameters: ParametersAPI<InstallationParameters, InstanceParameters, InvocationParameters>
/** Exposes method to identify app's location */
location: LocationAPI
/** Exposes methods for checking user's access level */
Expand All @@ -241,21 +249,30 @@ export interface BaseAppSDK {
cma: CMAClient
}

export type EditorAppSDK = Omit<BaseAppSDK, 'ids'> &
export type EditorAppSDK<
InstallationParameters extends KeyValueMap = KeyValueMap,
InstanceParameters extends KeyValueMap = KeyValueMap
> = Omit<BaseAppSDK<InstallationParameters, InstanceParameters, never>, 'ids'> &
SharedEditorSDK & {
/** A set of IDs for the app */
ids: Omit<IdsAPI, 'field'>
}

export type SidebarAppSDK = Omit<BaseAppSDK, 'ids'> &
export type SidebarAppSDK<
InstallationParameters extends KeyValueMap = KeyValueMap,
InstanceParameters extends KeyValueMap = KeyValueMap
> = Omit<BaseAppSDK<InstallationParameters, InstanceParameters, never>, 'ids'> &
SharedEditorSDK & {
/** A set of IDs for the app */
ids: Omit<IdsAPI, 'field'>
/** Methods to update the size of the iframe the app is contained within. */
window: WindowAPI
}

export type FieldAppSDK = BaseAppSDK &
export type FieldAppSDK<
InstallationParameters extends KeyValueMap = KeyValueMap,
InstanceParameters extends KeyValueMap = KeyValueMap
> = BaseAppSDK<InstallationParameters, InstanceParameters, never> &
SharedEditorSDK & {
/** A set of IDs for the app */
ids: IdsAPI
Expand All @@ -265,7 +282,10 @@ export type FieldAppSDK = BaseAppSDK &
window: WindowAPI
}

export type DialogAppSDK = Omit<BaseAppSDK, 'ids'> & {
export type DialogAppSDK<
InstallationParameters extends KeyValueMap = KeyValueMap,
InvocationParameters extends SerializedJSONValue = SerializedJSONValue
> = Omit<BaseAppSDK<InstallationParameters, never, InvocationParameters>, 'ids'> & {
/** A set of IDs for the app */
ids: Omit<IdsAPI, EntryScopedIds>
/** Closes the dialog and resolves openCurrentApp promise with data */
Expand All @@ -274,29 +294,42 @@ export type DialogAppSDK = Omit<BaseAppSDK, 'ids'> & {
window: WindowAPI
}

export type PageAppSDK = Omit<BaseAppSDK, 'ids'> & {
export type PageAppSDK<InstallationParameters extends KeyValueMap = KeyValueMap> = Omit<
BaseAppSDK<InstallationParameters, never, { path: string }>,
'ids'
> & {
/** A set of IDs actual for the app */
ids: Omit<IdsAPI, EntryScopedIds>
}

export type HomeAppSDK = Omit<BaseAppSDK, 'ids'> & {
export type HomeAppSDK<InstallationParameters extends KeyValueMap = KeyValueMap> = Omit<
BaseAppSDK<InstallationParameters, never, never>,
'ids'
> & {
ids: Omit<IdsAPI, EntryScopedIds>
}

export type ConfigAppSDK = Omit<BaseAppSDK, 'ids'> & {
export type ConfigAppSDK<InstallationParameters extends KeyValueMap = KeyValueMap> = Omit<
BaseAppSDK<InstallationParameters, never, never>,
'ids'
> & {
/** A set of IDs actual for the app */
ids: Omit<IdsAPI, EntryScopedIds | 'extension' | 'app'> & { app: string }
app: AppConfigAPI
}

export type KnownAppSDK =
| FieldAppSDK
| SidebarAppSDK
| DialogAppSDK
| EditorAppSDK
| PageAppSDK
| ConfigAppSDK
| HomeAppSDK
export type KnownAppSDK<
InstallationParameters extends KeyValueMap = KeyValueMap,
InstanceParameters extends KeyValueMap = KeyValueMap,
InvocationParameters extends SerializedJSONValue = SerializedJSONValue
> =
| FieldAppSDK<InstallationParameters, InstanceParameters>
| SidebarAppSDK<InstallationParameters, InstanceParameters>
| DialogAppSDK<InstallationParameters, InvocationParameters>
| EditorAppSDK<InstallationParameters, InstanceParameters>
| PageAppSDK<InstallationParameters>
| ConfigAppSDK<InstallationParameters>
| HomeAppSDK<InstallationParameters>

/** @deprecated consider using {@link BaseAppSDK} */
export type BaseExtensionSDK = BaseAppSDK
Expand Down Expand Up @@ -339,7 +372,7 @@ export interface Locations {
export interface ConnectMessage {
id: string
location: Location[keyof Location]
parameters: ParametersAPI
parameters: ParametersAPI<KeyValueMap, KeyValueMap, never>
locales: LocalesAPI
user: UserAPI
initialContentTypes: ContentType[]
Expand Down

0 comments on commit 7cbad71

Please sign in to comment.