@polarityio/integration-types 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Polarity IO
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # @polarityio/integration-types
2
+
3
+ TypeScript type definitions for building [Polarity](https://www.polarity.io) Integrations.
4
+
5
+ This package provides **types only** (no runtime code, no dependencies) so integrations can get full type safety without pulling in the runtime dependencies of `polarity-integration-utils`.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install --save-dev @polarityio/integration-types
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```typescript
16
+ import type {
17
+ Entity,
18
+ DoLookupUserOptions,
19
+ DoLookupResult,
20
+ IntegrationContext,
21
+ Integration
22
+ } from '@polarityio/integration-types';
23
+ ```
24
+
25
+ ## What's Included
26
+
27
+ | Module | Types |
28
+ |--------|-------|
29
+ | **Entity** | `Entity`, `EntityType`, `EntityTypeIdentifier`, `StandardEntityType`, `Channel`, `EntityPosition`, `RequestContext`, `OnDemandRequestContext`, `ScreenChangeRequestContext` |
30
+ | **Results** | `LookupResult<TDetails>`, `DoLookupResult<TDetails>` |
31
+ | **User Options** | `DoLookupUserOptions`, `ValidateOptionsUserOption`, `ValidateOptionsUserOptions`, `SelectTypeOptionValue`, `PossibleUserOptionValue`, `ValidationError` |
32
+ | **Context** | `IntegrationContext`, `PolarityCache`, `GlobalCache`, `IntegrationCache`, `UserCache`, `CacheOptions` |
33
+ | **Logging** | `Logger` |
34
+ | **Contract** | `Integration` |
35
+ | **Polling** | `Poll`, `PollSpec`, `IntervalPollSpec`, `CronPollSpec`, `PollFunction` |
36
+
37
+ ## Notes
38
+
39
+ - This package exports **type definitions only** — no JavaScript is emitted.
40
+ - To create `IntegrationError` instances at runtime, use `polarity-integration-utils`.
41
+ - Types are kept in sync with the canonical definitions in `polarity-integration-utils`.
42
+
43
+ ## License
44
+
45
+ MIT
@@ -0,0 +1,59 @@
1
+ import type { Logger } from './logger';
2
+ /**
3
+ * Cache operation options.
4
+ */
5
+ export interface CacheOptions {
6
+ /** Time-to-live in seconds. If not specified, uses default TTL. */
7
+ ttl?: number;
8
+ }
9
+ /**
10
+ * Global cache operations — shared across all integrations and users.
11
+ * Use for system-wide statistics, feature flags, or shared configuration.
12
+ */
13
+ export interface GlobalCache {
14
+ get(key: string): Promise<unknown>;
15
+ set(key: string, value: unknown, options?: CacheOptions): Promise<void>;
16
+ delete(key: string): Promise<void>;
17
+ }
18
+ /**
19
+ * Integration-scoped cache operations — shared across all users of a specific integration.
20
+ * Use for API responses, configuration, or data that's the same for all users.
21
+ */
22
+ export interface IntegrationCache {
23
+ get(key: string): Promise<unknown>;
24
+ set(key: string, value: unknown, options?: CacheOptions): Promise<void>;
25
+ delete(key: string): Promise<void>;
26
+ }
27
+ /**
28
+ * User-scoped cache operations — specific to individual users.
29
+ * Use for user preferences, recent activity, or personalized data.
30
+ */
31
+ export interface UserCache {
32
+ get(key: string): Promise<unknown>;
33
+ set(key: string, value: unknown, options?: CacheOptions): Promise<void>;
34
+ delete(key: string): Promise<void>;
35
+ }
36
+ /**
37
+ * Main cache interface providing hierarchical caching with three scopes.
38
+ *
39
+ * - **Global**: System-wide data shared across all integrations
40
+ * - **Integration**: Data shared among all users of a specific integration
41
+ * - **User**: User-specific data within an integration context
42
+ */
43
+ export interface PolarityCache {
44
+ global: GlobalCache;
45
+ integration: IntegrationCache;
46
+ user: UserCache;
47
+ }
48
+ /**
49
+ * Integration context provided to integration functions (`doLookup`, `onMessage`,
50
+ * `onDetails`, `validateOptions`).
51
+ */
52
+ export interface IntegrationContext {
53
+ cache: PolarityCache;
54
+ integrationId: string;
55
+ userId: number;
56
+ logger: Logger;
57
+ startPolling: (pollName: string) => void;
58
+ stopPolling: (pollName: string) => void;
59
+ }
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Represents a channel associated with a Polarity Entity.
3
+ */
4
+ export interface Channel {
5
+ channel_name: string;
6
+ id: number;
7
+ }
8
+ /**
9
+ * List of supported standard entity type values.
10
+ */
11
+ export type StandardEntityType = 'IPv4' | 'IPv4CIDR' | 'IPv6' | 'MAC' | 'MD5' | 'SHA1' | 'SHA256' | 'cve' | 'domain' | 'email' | 'string' | 'url';
12
+ /**
13
+ * Entity types including custom types.
14
+ *
15
+ * Used in the `types` array where specific custom type identifiers (e.g., `custom.myType`) are allowed.
16
+ */
17
+ export type EntityType = StandardEntityType | 'IP' | 'hash' | `custom.${string}`;
18
+ /**
19
+ * The value used in the `type` property of an Entity.
20
+ *
21
+ * For IPv4/IPv6 entities this is `'IPv4'` or `'IPv6'`. For any hash type this
22
+ * is `'hash'`. For custom types this is `'custom'`.
23
+ */
24
+ export type EntityTypeIdentifier = StandardEntityType | 'custom';
25
+ /**
26
+ * Screen position of an entity detected in Stream or Highlight mode.
27
+ */
28
+ export interface EntityPosition {
29
+ height: number;
30
+ width: number;
31
+ x: number;
32
+ y: number;
33
+ }
34
+ /**
35
+ * Request context for an **OnDemand** lookup.
36
+ *
37
+ * OnDemand mode is triggered when a user explicitly searches for an entity
38
+ * (e.g., via the search bar). The `requestType` is always `'OnDemand'`.
39
+ */
40
+ export interface OnDemandRequestContext {
41
+ requestType: 'OnDemand';
42
+ isUserInitiated: boolean;
43
+ }
44
+ /**
45
+ * Request context for a **Stream** or **Highlight** mode lookup.
46
+ *
47
+ * Both modes use `requestType: 'ScreenChange'` and include screen position
48
+ * data, the originating process name, and window title.
49
+ *
50
+ * - **Stream mode** — Entities are detected automatically from screen content
51
+ * without user interaction. `isUserInitiated` is `false`.
52
+ * - **Highlight mode** — Entities are detected from screen content in response
53
+ * to a user-initiated action (e.g., selecting text). `isUserInitiated` is `true`.
54
+ */
55
+ export interface ScreenChangeRequestContext {
56
+ requestType: 'ScreenChange';
57
+ isUserInitiated: boolean;
58
+ positions: EntityPosition[];
59
+ processName: string;
60
+ windowTitle: string;
61
+ }
62
+ /**
63
+ * Union of all request context types.
64
+ *
65
+ * Polarity entities are looked up in one of three modes:
66
+ *
67
+ * | Mode | `requestType` | `isUserInitiated` |
68
+ * |---------------|------------------|-------------------|
69
+ * | **OnDemand** | `'OnDemand'` | `true` |
70
+ * | **Stream** | `'ScreenChange'` | `false` |
71
+ * | **Highlight** | `'ScreenChange'` | `true` |
72
+ *
73
+ * Use `requestContext.requestType` and `requestContext.isUserInitiated` to
74
+ * distinguish between the three modes at runtime.
75
+ */
76
+ export type RequestContext = OnDemandRequestContext | ScreenChangeRequestContext;
77
+ /**
78
+ * Represents a Polarity Entity object which is passed to an integration's
79
+ * doLookup method.
80
+ */
81
+ export interface Entity {
82
+ value: string;
83
+ rawValue: string;
84
+ types: EntityType[];
85
+ type: EntityTypeIdentifier;
86
+ requestContext: RequestContext;
87
+ longitude: number;
88
+ latitude: number;
89
+ IPLong: number;
90
+ isURL: boolean;
91
+ isSHA512: boolean;
92
+ isSHA256: boolean;
93
+ isSHA1: boolean;
94
+ isPrivateIP: boolean;
95
+ isMD5: boolean;
96
+ isIPv6: boolean;
97
+ isIPv4: boolean;
98
+ isIP: boolean;
99
+ isHex: boolean;
100
+ isHash: boolean;
101
+ isHTMLTag: boolean;
102
+ isEmail: boolean;
103
+ isDomain: boolean;
104
+ hashType: 'MD5' | 'SHA1' | 'SHA256' | '';
105
+ displayValue: string;
106
+ channels: Channel[];
107
+ IPType: 'IPv4' | 'IPv6' | 'IPv4CIDR' | '';
108
+ }
@@ -0,0 +1,7 @@
1
+ export type { Channel, StandardEntityType, EntityType, EntityTypeIdentifier, EntityPosition, OnDemandRequestContext, ScreenChangeRequestContext, RequestContext, Entity } from './entity';
2
+ export type { SelectTypeOptionValue, PossibleUserOptionValue, DoLookupUserOptions, ValidateOptionsUserOption, ValidateOptionsUserOptions, ValidationError } from './user-options';
3
+ export type { LookupResult, DoLookupResult } from './result';
4
+ export type { CacheOptions, GlobalCache, IntegrationCache, UserCache, PolarityCache, IntegrationContext } from './context';
5
+ export type { Logger } from './logger';
6
+ export type { Integration } from './integration';
7
+ export type { PollFunction, IntervalPollSpec, CronPollSpec, PollSpec, Poll } from './polling';
@@ -0,0 +1,45 @@
1
+ import type { Entity } from './entity';
2
+ import type { DoLookupUserOptions, ValidateOptionsUserOptions, ValidationError } from './user-options';
3
+ import type { DoLookupResult, LookupResult } from './result';
4
+ import type { IntegrationContext } from './context';
5
+ import type { Logger } from './logger';
6
+ import type { Poll } from './polling';
7
+ /**
8
+ * Main interface defining the contract for a Polarity Integration.
9
+ */
10
+ export interface Integration {
11
+ /**
12
+ * Called once when the integration is first loaded. Use this to initialize
13
+ * connections, validate configuration, or perform other one-time setup.
14
+ */
15
+ startup: (logger: Logger) => Promise<unknown>;
16
+ /**
17
+ * Called for each batch of entities that match the integration's entity types.
18
+ * Return lookup results, or null/void for a miss.
19
+ *
20
+ * @throws IntegrationError (from `polarity-integration-utils`) on failure.
21
+ */
22
+ doLookup: (entities: Entity[], options: DoLookupUserOptions, context: IntegrationContext) => Promise<DoLookupResult | null | void>;
23
+ /**
24
+ * Optional handler for messages sent from the integration's overlay window.
25
+ *
26
+ * @throws IntegrationError (from `polarity-integration-utils`) on failure.
27
+ */
28
+ onMessage?: (payload: unknown, options: DoLookupUserOptions, context: IntegrationContext) => Promise<unknown>;
29
+ /**
30
+ * Optional handler called when a user expands the details block for a result.
31
+ *
32
+ * @throws IntegrationError (from `polarity-integration-utils`) on failure.
33
+ */
34
+ onDetails?: (lookupObject: LookupResult, options: DoLookupUserOptions, context: IntegrationContext) => Promise<unknown>;
35
+ /**
36
+ * Called to validate the user's option configuration. Return an array of
37
+ * validation errors, or an empty array if options are valid.
38
+ */
39
+ validateOptions: (options: ValidateOptionsUserOptions, context: IntegrationContext) => ValidationError[];
40
+ /**
41
+ * Optional record of named poll specifications. Each key is the poll name
42
+ * (used with `context.startPolling()` / `context.stopPolling()`).
43
+ */
44
+ poll?: Poll;
45
+ }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Bunyan-compatible logger interface used by Polarity integrations.
3
+ *
4
+ * Each log level method supports multiple call signatures:
5
+ * - No arguments: returns `true` if the level is enabled
6
+ * - `Error` as first argument: logs the error with stack trace
7
+ * - `Object` as first argument: merges fields into the log record
8
+ * - String/format as first argument: logs a formatted message
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * // Check if level is enabled
13
+ * if (logger.trace()) { ... }
14
+ *
15
+ * // Log with structured fields
16
+ * logger.info({ entityValue: '8.8.8.8' }, 'Looking up entity');
17
+ *
18
+ * // Log an error
19
+ * logger.error(err, 'Request failed');
20
+ *
21
+ * // Create a child logger with bound fields
22
+ * const child = logger.child({ module: 'api-client' });
23
+ * ```
24
+ */
25
+ export interface Logger {
26
+ /** Create a child logger with additional bound fields. */
27
+ child(options: object, simple?: boolean): Logger;
28
+ /** Returns true if the `trace` level is enabled. */
29
+ trace(): boolean;
30
+ /** Log an Error at the `trace` level. */
31
+ trace(error: Error, ...params: unknown[]): void;
32
+ /** Log with merged fields at the `trace` level. */
33
+ trace(obj: object, ...params: unknown[]): void;
34
+ /** Log a formatted message at the `trace` level. */
35
+ trace(format: string, ...params: unknown[]): void;
36
+ /** Returns true if the `debug` level is enabled. */
37
+ debug(): boolean;
38
+ /** Log an Error at the `debug` level. */
39
+ debug(error: Error, ...params: unknown[]): void;
40
+ /** Log with merged fields at the `debug` level. */
41
+ debug(obj: object, ...params: unknown[]): void;
42
+ /** Log a formatted message at the `debug` level. */
43
+ debug(format: string, ...params: unknown[]): void;
44
+ /** Returns true if the `info` level is enabled. */
45
+ info(): boolean;
46
+ /** Log an Error at the `info` level. */
47
+ info(error: Error, ...params: unknown[]): void;
48
+ /** Log with merged fields at the `info` level. */
49
+ info(obj: object, ...params: unknown[]): void;
50
+ /** Log a formatted message at the `info` level. */
51
+ info(format: string, ...params: unknown[]): void;
52
+ /** Returns true if the `warn` level is enabled. */
53
+ warn(): boolean;
54
+ /** Log an Error at the `warn` level. */
55
+ warn(error: Error, ...params: unknown[]): void;
56
+ /** Log with merged fields at the `warn` level. */
57
+ warn(obj: object, ...params: unknown[]): void;
58
+ /** Log a formatted message at the `warn` level. */
59
+ warn(format: string, ...params: unknown[]): void;
60
+ /** Returns true if the `error` level is enabled. */
61
+ error(): boolean;
62
+ /** Log an Error at the `error` level. */
63
+ error(error: Error, ...params: unknown[]): void;
64
+ /** Log with merged fields at the `error` level. */
65
+ error(obj: object, ...params: unknown[]): void;
66
+ /** Log a formatted message at the `error` level. */
67
+ error(format: string, ...params: unknown[]): void;
68
+ /** Returns true if the `fatal` level is enabled. */
69
+ fatal(): boolean;
70
+ /** Log an Error at the `fatal` level. */
71
+ fatal(error: Error, ...params: unknown[]): void;
72
+ /** Log with merged fields at the `fatal` level. */
73
+ fatal(obj: object, ...params: unknown[]): void;
74
+ /** Log a formatted message at the `fatal` level. */
75
+ fatal(format: string, ...params: unknown[]): void;
76
+ }
@@ -0,0 +1,69 @@
1
+ import type { DoLookupUserOptions } from './user-options';
2
+ import type { IntegrationContext } from './context';
3
+ /**
4
+ * A function that is called on a polling schedule.
5
+ *
6
+ * Receives admin-only (global) user options and the integration context.
7
+ */
8
+ export type PollFunction = (options: DoLookupUserOptions, context: IntegrationContext) => Promise<void>;
9
+ interface BasePollSpec {
10
+ /** The function to call on each poll cycle. */
11
+ function: PollFunction;
12
+ /**
13
+ * Whether to start polling automatically when the integration loads.
14
+ * If `false`, polling must be started manually via `context.startPolling()`.
15
+ */
16
+ autostart?: boolean;
17
+ /**
18
+ * Strategy to use when the poll function throws an error.
19
+ *
20
+ * - `'restart'` — Retry the function with linear backoff until it succeeds,
21
+ * then resume the normal schedule.
22
+ * - `'shutdown'` — Stop the integration and report an error to the frontend.
23
+ */
24
+ failureStrategy?: 'restart' | 'shutdown';
25
+ }
26
+ /**
27
+ * A poll specification that runs on a fixed interval.
28
+ */
29
+ export interface IntervalPollSpec extends BasePollSpec {
30
+ /** Polling interval in milliseconds. */
31
+ interval: number;
32
+ cron?: never;
33
+ }
34
+ /**
35
+ * A poll specification that runs on a cron schedule.
36
+ */
37
+ export interface CronPollSpec extends BasePollSpec {
38
+ /** A cron expression defining the polling schedule (e.g., `"0 9 * * *"`). */
39
+ cron: string;
40
+ interval?: never;
41
+ }
42
+ /**
43
+ * A single poll specification — either interval-based or cron-based.
44
+ */
45
+ export type PollSpec = IntervalPollSpec | CronPollSpec;
46
+ /**
47
+ * A record of named poll specifications exported by an integration.
48
+ *
49
+ * Each key is the poll name (used with `context.startPolling()` / `context.stopPolling()`),
50
+ * and the value defines the schedule and function to call.
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * const poll: Poll = {
55
+ * refreshTokens: {
56
+ * function: doRefreshTokens,
57
+ * interval: 10000,
58
+ * autostart: true
59
+ * },
60
+ * uploadReport: {
61
+ * function: doUploadReport,
62
+ * cron: "0 9 * * *",
63
+ * failureStrategy: "restart"
64
+ * }
65
+ * };
66
+ * ```
67
+ */
68
+ export type Poll = Record<string, PollSpec>;
69
+ export {};
@@ -0,0 +1,25 @@
1
+ import type { Entity } from './entity';
2
+ /**
3
+ * A single lookup result returned by an integration's `doLookup` method.
4
+ *
5
+ * @typeParam TDetails - The type of the `data.details` object. Defaults to `unknown`.
6
+ */
7
+ export interface LookupResult<TDetails = unknown> {
8
+ entity: Entity;
9
+ displayValue?: string;
10
+ /**
11
+ * If set to `true`, this result will not be cached in the Integration cache.
12
+ * Defaults to `false`.
13
+ */
14
+ isVolatile?: boolean;
15
+ data: {
16
+ summary: string[];
17
+ details: TDetails;
18
+ } | null;
19
+ }
20
+ /**
21
+ * The array of results returned by an integration's `doLookup` method.
22
+ *
23
+ * @typeParam TDetails - The type of each result's `data.details` object.
24
+ */
25
+ export type DoLookupResult<TDetails = unknown> = LookupResult<TDetails>[];
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Represents a dropdown option value used in user configuration.
3
+ */
4
+ export interface SelectTypeOptionValue {
5
+ display: string;
6
+ value: string;
7
+ }
8
+ /**
9
+ * Possible values for a user option.
10
+ */
11
+ export type PossibleUserOptionValue = string | number | boolean | SelectTypeOptionValue | SelectTypeOptionValue[];
12
+ /**
13
+ * User options object passed into the integration's `doLookup` method.
14
+ *
15
+ * Keys correspond to the `key` values defined in the integration's `config.json`.
16
+ *
17
+ * @example
18
+ * If your integration has a user option with a `key` of `apiKey`, the options
19
+ * object passed into `doLookup` would look like:
20
+ * ```json
21
+ * { "apiKey": "XXXXXXXXXX" }
22
+ * ```
23
+ */
24
+ export type DoLookupUserOptions = Record<string, PossibleUserOptionValue>;
25
+ /**
26
+ * A single user option as passed to the `validateOptions` method.
27
+ */
28
+ export interface ValidateOptionsUserOption {
29
+ key: string;
30
+ value: PossibleUserOptionValue;
31
+ integration_id: string;
32
+ user_can_edit: boolean;
33
+ admin_only: boolean;
34
+ }
35
+ /**
36
+ * User options object passed into the integration's `validateOptions` method.
37
+ */
38
+ export type ValidateOptionsUserOptions = Record<string, ValidateOptionsUserOption>;
39
+ /**
40
+ * Represents a validation error returned from the `validateOptions` method.
41
+ */
42
+ export interface ValidationError {
43
+ key: string;
44
+ message: string;
45
+ }
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@polarityio/integration-types",
3
+ "version": "1.0.1",
4
+ "description": "TypeScript type definitions for Polarity Integrations",
5
+ "types": "./dist/index.d.ts",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts"
9
+ }
10
+ },
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "type": "module",
15
+ "sideEffects": false,
16
+ "scripts": {
17
+ "build": "tsc -p tsconfig.build.json",
18
+ "clean": "rm -rf dist",
19
+ "lint": "eslint src/",
20
+ "lint:fix": "eslint src/ --fix",
21
+ "format:check": "prettier --check src/",
22
+ "format": "prettier --write src/",
23
+ "prepublishOnly": "npm run clean && npm run build"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://github.com/polarityio/integration-types"
28
+ },
29
+ "author": "Polarity IO <support@polarity.io>",
30
+ "license": "MIT",
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/polarityio/integration-types/issues"
36
+ },
37
+ "homepage": "https://github.com/polarityio/integration-types#readme",
38
+ "devDependencies": {
39
+ "@eslint/js": "^10.0.1",
40
+ "eslint": "^10.2.0",
41
+ "eslint-config-prettier": "^10.1.8",
42
+ "eslint-plugin-prettier": "^5.5.5",
43
+ "prettier": "^3.8.2",
44
+ "typescript": "^5.9.3",
45
+ "typescript-eslint": "^8.58.1"
46
+ }
47
+ }