Skip to content

Commit

Permalink
fix: types for plugins to be env specific (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpagtakhan committed Jun 16, 2023
1 parent 301ba1e commit 8584efe
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
22 changes: 21 additions & 1 deletion packages/analytics-types/src/client/node-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AmplitudeReturn } from '../amplitude-promise';
import { NodeOptions } from '../config';
import { NodeConfig, NodeOptions } from '../config';
import { CoreClient } from './core-client';
import { Plugin } from '../plugin';

export interface NodeClient extends CoreClient {
/**
Expand All @@ -12,4 +13,23 @@ export interface NodeClient extends CoreClient {
* ```
*/
init(apiKey: string, options?: NodeOptions): AmplitudeReturn<void>;

/**
* Adds a new plugin.
*
* ```typescript
* const plugin = {
* name: 'my-plugin',
* type: 'enrichment',
* async setup(config: NodeConfig, amplitude: NodeClient) {
* return;
* },
* async execute(event: Event) {
* return event;
* },
* };
* amplitude.add(plugin);
* ```
*/
add(plugin: Plugin<NodeClient, NodeConfig>): AmplitudeReturn<void>;
}
41 changes: 40 additions & 1 deletion packages/analytics-types/src/client/web-client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { AmplitudeReturn } from '../amplitude-promise';
import { BrowserOptions, ReactNativeOptions } from '../config';
import { BrowserConfig, BrowserOptions, ReactNativeConfig, ReactNativeOptions } from '../config';
import { TransportType } from '../transport';
import { CoreClient } from './core-client';
import { Plugin } from '../plugin';

interface Client extends CoreClient {
/**
Expand Down Expand Up @@ -116,6 +117,25 @@ export interface BrowserClient extends Client {
* ```
*/
setTransport(transport: TransportType): void;

/**
* Adds a new plugin.
*
* ```typescript
* const plugin = {
* name: 'my-plugin',
* type: 'enrichment',
* async setup(config: BrowserConfig, amplitude: BrowserClient) {
* return;
* },
* async execute(event: Event) {
* return event;
* },
* };
* amplitude.add(plugin);
* ```
*/
add(plugin: Plugin<BrowserClient, BrowserConfig>): AmplitudeReturn<void>;
}

export interface ReactNativeClient extends Client {
Expand All @@ -128,4 +148,23 @@ export interface ReactNativeClient extends Client {
* ```
*/
init(apiKey: string, userId?: string, options?: ReactNativeOptions): AmplitudeReturn<void>;

/**
* Adds a new plugin.
*
* ```typescript
* const plugin = {
* name: 'my-plugin',
* type: 'enrichment',
* async setup(config: ReactNativeConfig, amplitude: ReactNativeClient) {
* return;
* },
* async execute(event: Event) {
* return event;
* },
* };
* amplitude.add(plugin);
* ```
*/
add(plugin: Plugin<ReactNativeClient, ReactNativeConfig>): AmplitudeReturn<void>;
}
14 changes: 7 additions & 7 deletions packages/analytics-types/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ export enum PluginType {
DESTINATION = 'destination',
}

export interface BeforePlugin<T = CoreClient> {
export interface BeforePlugin<T = CoreClient, U = Config> {
name: string;
type: PluginType.BEFORE;
setup(config: Config, client?: T): Promise<void>;
setup(config: U, client?: T): Promise<void>;
execute(context: Event): Promise<Event | null>;
}

export interface EnrichmentPlugin<T = CoreClient> {
export interface EnrichmentPlugin<T = CoreClient, U = Config> {
name: string;
type: PluginType.ENRICHMENT;
setup(config: Config, client?: T): Promise<void>;
setup(config: U, client?: T): Promise<void>;
execute(context: Event): Promise<Event | null>;
}

export interface DestinationPlugin<T = CoreClient> {
export interface DestinationPlugin<T = CoreClient, U = Config> {
name: string;
type: PluginType.DESTINATION;
setup(config: Config, client?: T): Promise<void>;
setup(config: U, client?: T): Promise<void>;
execute(context: Event): Promise<Result>;
flush?(): Promise<void>;
}

export type Plugin = BeforePlugin | EnrichmentPlugin | DestinationPlugin;
export type Plugin<T = CoreClient, U = Config> = BeforePlugin<T, U> | EnrichmentPlugin<T, U> | DestinationPlugin<T, U>;

0 comments on commit 8584efe

Please sign in to comment.