Skip to content

Commit

Permalink
feat: adds current location to location API
Browse files Browse the repository at this point in the history
  • Loading branch information
ghepting committed Jul 17, 2023
1 parent 1a93550 commit 067b57f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
5 changes: 4 additions & 1 deletion lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
JSONPatchItem,
BaseAppSDK,
KnownAppSDK,
Locations,
} from './types'
import { Channel } from './channel'
import { createAdapter } from './cmaAdapter'
Expand Down Expand Up @@ -62,12 +63,14 @@ function makeSharedAPI(
data: ConnectMessage
): BaseAppSDK<KeyValueMap, KeyValueMap, never> {
const { user, parameters, locales, ids, initialContentTypes } = data
const currentLocation = data.location || locations.LOCATION_ENTRY_FIELD
const currentLocation: Locations[keyof Locations] =
data.location || locations.LOCATION_ENTRY_FIELD

return {
cma: createCMAClient(ids, channel),
cmaAdapter: createAdapter(channel),
location: {
current: currentLocation,
is: (tested: string) => currentLocation === tested,
},
user,
Expand Down
25 changes: 13 additions & 12 deletions lib/types/api.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,20 @@ export interface NotifierAPI {

/* Location API */

export interface Locations {
LOCATION_ENTRY_FIELD: 'entry-field'
LOCATION_ENTRY_FIELD_SIDEBAR: 'entry-field-sidebar'
LOCATION_ENTRY_SIDEBAR: 'entry-sidebar'
LOCATION_DIALOG: 'dialog'
LOCATION_ENTRY_EDITOR: 'entry-editor'
LOCATION_PAGE: 'page'
LOCATION_HOME: 'home'
LOCATION_APP_CONFIG: 'app-config'
}

export interface LocationAPI {
/** Checks the location in which your app is running */
current: Locations[keyof Locations]
is: (type: string) => boolean
}

Expand Down Expand Up @@ -358,20 +370,9 @@ export type AppExtensionSDK = ConfigAppSDK
/** @deprecated consider using {@link KnownAppSDK} */
export type KnownSDK = KnownAppSDK

export interface Locations {
LOCATION_ENTRY_FIELD: 'entry-field'
LOCATION_ENTRY_FIELD_SIDEBAR: 'entry-field-sidebar'
LOCATION_ENTRY_SIDEBAR: 'entry-sidebar'
LOCATION_DIALOG: 'dialog'
LOCATION_ENTRY_EDITOR: 'entry-editor'
LOCATION_PAGE: 'page'
LOCATION_HOME: 'home'
LOCATION_APP_CONFIG: 'app-config'
}

export interface ConnectMessage {
id: string
location: Location[keyof Location]
location: Locations[keyof Locations]
parameters: ParametersAPI<KeyValueMap, KeyValueMap, never>
locales: LocalesAPI
user: UserAPI
Expand Down
5 changes: 3 additions & 2 deletions test/unit/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ function test(expected: string[], location: string | undefined, expectedLocation
expect(api.notifier).to.have.all.keys(['success', 'error', 'warning'])
expect(api.access).to.have.all.keys(['can', 'canEditAppConfig'])

// Test location methods (currently only `is`).
expect(Object.keys(api.location)).to.deep.equal(['is'])
// Test location API methods (`current` and `is`).
expect(Object.keys(api.location)).to.deep.equal(['current', 'is'])
expect(api.location.current).to.equal(expectedLocation)
expect(api.location.is(expectedLocation as string)).to.equal(true)
expect(api.location.is('wat?')).to.equal(false)

Expand Down

0 comments on commit 067b57f

Please sign in to comment.