Skip to content

Commit

Permalink
Refine JSDoc / TypeScript types for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
dobesv committed Oct 7, 2022
1 parent 6dad612 commit 1c26a40
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 16 deletions.
20 changes: 10 additions & 10 deletions packages/analytics-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function analytics(config = {}) {
// if (SERVER) {
// console.log('INIT SERVER')
// }

/* Parse plugins array */
const parsedOptions = (config.plugins || []).reduce((acc, plugin) => {
if (isFunction(plugin)) {
Expand Down Expand Up @@ -125,7 +125,7 @@ function analytics(config = {}) {
middlewares: [],
events: []
})

/* Storage by default is set to global & is not persisted */
const storage = (config.storage) ? config.storage : {
getItem: get,
Expand Down Expand Up @@ -160,7 +160,7 @@ function analytics(config = {}) {
// throw new Error(`${ERROR_URL}3`)
throw new Error('Abort disabled inListener')
}

// Parse URL parameters
const params = paramsParse()
// Initialize visitor information
Expand All @@ -177,8 +177,8 @@ function analytics(config = {}) {
}

/**
* Async Management methods for plugins.
*
* Async Management methods for plugins.
*
* This is also where [custom methods](https://bit.ly/329vFXy) are loaded into the instance.
* @typedef {Object} Plugins
* @property {EnablePlugin} enable - Set storage value
Expand Down Expand Up @@ -275,7 +275,7 @@ function analytics(config = {}) {
// Merge in custom plugin methods
...parsedOptions.methods
}

let readyCalled = false
/**
* Analytic instance returned from initialization
Expand All @@ -289,7 +289,7 @@ function analytics(config = {}) {
* @property {On} on - Fire callback on analytics lifecycle events.
* @property {Once} once - Fire callback on analytics lifecycle events once.
* @property {GetState} getState - Get data about user, activity, or context.
* @property {Storage} storage - storage methods
* @property {AnalyticsStorage} storage - storage methods
* @property {Plugins} plugins - plugin methods
*/
const instance = {
Expand Down Expand Up @@ -733,7 +733,7 @@ function analytics(config = {}) {
/**
* Storage utilities for persisting data.
* These methods will allow you to save data in localStorage, cookies, or to the window.
* @typedef {Object} Storage
* @typedef {Object} AnalyticsStorage
* @property {GetItem} getItem - Get value from storage
* @property {SetItem} setItem - Set storage value
* @property {RemoveItem} removeItem - Remove storage value
Expand Down Expand Up @@ -887,7 +887,7 @@ function analytics(config = {}) {
}
return acc
}, {})

const initialState = {
context: initialConfig,
user: visitorInfo,
Expand Down Expand Up @@ -941,7 +941,7 @@ function analytics(config = {}) {

const enabledPlugins = pluginKeys.filter((name) => parsedOptions.pluginEnabled[name])
const disabledPlugins = pluginKeys.filter((name) => !parsedOptions.pluginEnabled[name])

/* Register analytic plugins */
store.dispatch({
type: EVENTS.registerPlugins,
Expand Down
2 changes: 1 addition & 1 deletion packages/analytics-core/src/modules/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function currentUrl(search) {
}

/**
* Page data for overides
* Page data for overrides
* @typedef {object} PageData
* @property {string} [title] - Page title
* @property {string} [url] - Page url
Expand Down
1 change: 1 addition & 0 deletions packages/analytics-core/src/modules/track.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import EVENTS from '../events'
import serialize from '../utils/serialize'


// Track State
const initialState = {
last: {},
Expand Down
68 changes: 63 additions & 5 deletions packages/analytics-core/src/pluginTypeDef.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,70 @@
/**
* @callback PluginTrackFunction
* @param {Object} arg
* @param {Object} arg.config config from the plugin spec
* @param {AnalyticsInstance} arg.instance analytics instance
* @param {Object} arg.payload event data
* @param {string} arg.payload.event event name passed to track
* @param {Object.<string,Object>} arg.payload.properties event properties passed to track
* @return {void}
*/

/**
* @callback PluginPageFunction
* @param {Object} arg
* @param {Object} arg.config config from the plugin spec
* @param {AnalyticsInstance} arg.instance analytics instance
* @param {Object} arg.payload
* @param {string} arg.payload.event
* @param {PageData} arg.payload.properties
* @return {void}
*/

/**
* @callback PluginIdentifyFunction
* @param {Object} arg
* @param {Object} arg.config config from the plugin spec
* @param {AnalyticsInstance} arg.instance analytics instance
* @param {Object} arg.payload
* @param {string} arg.payload.userId
* @param {Object.<string,Object>} arg.payload.traits
* @return {void}
*/

/**
* @callback PluginInitializeFunction
* @param {Object} arg
* @param {Object} arg.config config from the plugin spec
* @param {AnalyticsInstance} arg.instance analytics instance
* @return boolean
*/

/**
* @callback PluginLoadedFunction
* @param {Object} arg
* @param {Object} arg.config config from the plugin spec
* @param {AnalyticsInstance} arg.instance analytics instance
* @param {Object} arg.payload
* @return boolean
*/

/**
* @callback PluginReadyFunction
* @param {Object} arg
* @param {Object} arg.config config from the plugin spec
* @param {AnalyticsInstance} arg.instance analytics instance
* @return void
*/

/**
* @typedef {Object} AnalyticsPlugin
* @property {string} name - Name of plugin
* @property {Object} [EVENTS] - exposed events of plugin
* @property {Object} [config] - Configuration of plugin
* @property {function} [initialize] - Load analytics scripts method
* @property {function} [page] - Page visit tracking method
* @property {function} [track] - Custom event tracking method
* @property {function} [identify] - User identify method
* @property {function} [loaded] - Function to determine if analytics script loaded
* @property {function} [ready] - Fire function when plugin ready
* @property {PluginPageFunction} [page] - Page visit tracking method
* @property {PluginTrackFunction} [track] - Custom event tracking method
* @property {PluginIdentifyFunction} [identify] - User identify method
* @property {PluginLoadedFunction} [loaded] - Function to determine if analytics script loaded
* @property {PluginReadyFunction} [ready] - Fire function when plugin ready
*/

0 comments on commit 1c26a40

Please sign in to comment.