Skip to content

Commit a517813

Browse files
committed
[WIP] Additional types
1 parent 4b632bb commit a517813

23 files changed

+832
-102
lines changed

lib/entityCoordinates.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface EntityCoordinatesSpec {
1111
}
1212

1313
/** Represents entity coordinates for a software component */
14-
declare class EntityCoordinates {
14+
export declare class EntityCoordinates {
1515
/** The type of the entity (e.g., 'npm', 'maven', 'git') */
1616
type?: string
1717

lib/entityCoordinates.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation and others. Licensed under the MIT license.
22
// SPDX-License-Identifier: MIT
33

4-
/** @typedef {import('./entityCoordinates')} EntityCoordinates */
54
/** @typedef {import('./entityCoordinates').EntityCoordinatesSpec} EntityCoordinatesSpec */
65

76
/** Property flag for namespace normalization */
@@ -42,11 +41,6 @@ class EntityCoordinates {
4241
*
4342
* @param {EntityCoordinatesSpec | EntityCoordinates | null | undefined} spec - The specification object or existing
4443
* EntityCoordinates instance
45-
* @param {string} [spec.type] - The type of the entity
46-
* @param {string} [spec.provider] - The provider of the entity
47-
* @param {string} [spec.namespace] - The namespace of the entity
48-
* @param {string} [spec.name] - The name of the entity
49-
* @param {string} [spec.revision] - The revision/version of the entity
5044
* @returns {EntityCoordinates | null} New EntityCoordinates instance or null if spec is falsy
5145
*/
5246
static fromObject(spec) {
@@ -78,7 +72,7 @@ class EntityCoordinates {
7872
static fromUrn(urn) {
7973
if (!urn) return null
8074
// eslint-disable-next-line no-unused-vars
81-
const [scheme, type, provider, namespace, name, revToken, revision] = urn.split(':')
75+
const [, type, provider, namespace, name, , revision] = urn.split(':')
8276
return new EntityCoordinates(type, provider, namespace, name, revision)
8377
}
8478

lib/mockInsights.d.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Corporation and others. Licensed under the MIT license.
2+
// SPDX-License-Identifier: MIT
3+
4+
import { TelemetryClient, Contracts } from 'applicationinsights'
5+
6+
/** Mock implementation of Application Insights client for testing and development */
7+
declare class MockInsights {
8+
/** The underlying Application Insights client, if any */
9+
client: TelemetryClient | null
10+
11+
/**
12+
* Creates a new MockInsights instance
13+
*
14+
* @param client - Optional Application Insights client to wrap
15+
*/
16+
constructor(client?: TelemetryClient | null)
17+
18+
/**
19+
* Sets up the mock insights client or configures Application Insights
20+
*
21+
* @param key - Application Insights instrumentation key or 'mock' for testing
22+
* @param echo - Whether to echo telemetry to both mock and real client
23+
*/
24+
static setup(key?: string | null, echo?: boolean): void
25+
26+
/**
27+
* Tracks an exception
28+
*
29+
* @param exceptionTelemetry - The exception telemetry data
30+
*/
31+
trackException(exceptionTelemetry: Contracts.ExceptionTelemetry): void
32+
33+
/**
34+
* Tracks a trace message
35+
*
36+
* @param traceTelemetry - The trace telemetry data
37+
*/
38+
trackTrace(traceTelemetry: Contracts.TraceTelemetry): void
39+
}
40+
41+
export = MockInsights

lib/mockInsights.js

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,79 @@
33

44
const appInsights = require('applicationinsights')
55

6+
/**
7+
* @typedef {import('applicationinsights').TelemetryClient} TelemetryClient
8+
*
9+
* @typedef {import('applicationinsights').Contracts.ExceptionTelemetry} ExceptionTelemetry
10+
*
11+
* @typedef {import('applicationinsights').Contracts.TraceTelemetry} TraceTelemetry
12+
*/
13+
14+
/**
15+
* Mock implementation of Application Insights client for testing and development. This class provides a lightweight
16+
* alternative to the full Application Insights client, allowing for console-based logging of telemetry data during
17+
* development and testing.
18+
*/
619
class MockInsights {
20+
/**
21+
* Creates a new MockInsights instance
22+
*
23+
* @param {TelemetryClient | null} [client=null] - Optional Application Insights client to wrap. Default is `null`
24+
*/
725
constructor(client = null) {
26+
/**
27+
* The underlying Application Insights client, if any
28+
*
29+
* @type {TelemetryClient | null}
30+
*/
831
this.client = client
932
}
1033

34+
/**
35+
* Sets up the mock insights client or configures Application Insights. This method configures the global Application
36+
* Insights client based on the provided key.
37+
*
38+
* @param {string | null} [key=null] - Application Insights instrumentation key or 'mock' for testing. Default is
39+
* `null`
40+
* @param {boolean} [echo=false] - Whether to echo telemetry to both mock and real client. Default is `false`
41+
* @returns {void}
42+
*/
1143
static setup(key = null, echo = false) {
1244
// exit if we we are already setup
1345
if (appInsights.defaultClient instanceof MockInsights) return
14-
if (!key || key === 'mock') appInsights.defaultClient = new MockInsights()
46+
if (!key || key === 'mock') appInsights.defaultClient = /** @type {any} */ (new MockInsights())
1547
else {
1648
appInsights.setup(key).setAutoCollectPerformance(false).setAutoCollectDependencies(true).start()
17-
if (echo) appInsights.defaultClient = new MockInsights(appInsights.defaultClient)
49+
if (echo) appInsights.defaultClient = /** @type {any} */ (new MockInsights(appInsights.defaultClient))
1850
}
1951
}
2052

53+
/**
54+
* Tracks an exception by logging it to the console and optionally forwarding to Application Insights.
55+
*
56+
* @param {ExceptionTelemetry} exceptionTelemetry - The exception telemetry data
57+
* @returns {void}
58+
*/
2159
trackException(exceptionTelemetry) {
2260
console.log('Exception: ')
2361
console.dir(exceptionTelemetry.exception)
24-
if (this.client) this.client.trackException(exceptionTelemetry)
62+
if (this.client) this.client.trackException(/** @type {ExceptionTelemetry} */ (exceptionTelemetry))
2563
}
2664

65+
/**
66+
* Tracks a trace message by logging it to the console and optionally forwarding to Application Insights. The trace is
67+
* formatted with severity level indicators for easy console reading.
68+
*
69+
* @param {TraceTelemetry} traceTelemetry - The trace telemetry data
70+
* @returns {void}
71+
*/
2772
trackTrace(traceTelemetry) {
2873
// const severities = ['Verbose', 'Info', 'Warning', 'Error', 'Critical'];
2974
const severities = ['V', 'I', 'W', 'E', 'C']
3075
const hasProperties = traceTelemetry.properties && Object.keys(traceTelemetry.properties).length > 0
3176
const propertyString = hasProperties ? `${JSON.stringify(traceTelemetry.properties)}` : ''
3277
console.log(`[${severities[traceTelemetry.severity]}] ${traceTelemetry.message}${propertyString}`)
33-
if (this.client) this.client.trackTrace(traceTelemetry)
78+
if (this.client) this.client.trackTrace(/** @type {TraceTelemetry} */ (traceTelemetry))
3479
}
3580
}
3681
module.exports = MockInsights

package-lock.json

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,17 @@
7676
},
7777
"devDependencies": {
7878
"@tsconfig/node18": "18.2.4",
79+
"@tsconfig/strictest": "2.0.5",
7980
"@types/chai": "4.3.3",
8081
"@types/deep-equal-in-any-order": "1.0.4",
8182
"@types/express": "4.17.17",
8283
"@types/lodash": "4.17.16",
84+
"@types/memory-cache": "0.2.6",
8385
"@types/mocha": "8.2.3",
8486
"@types/node": "18.15.11",
87+
"@types/pako": "1.0.1",
8588
"@types/sinon": "5.0.7",
89+
"@types/winston": "2.3.9",
8690
"chai": "^4.1.2",
8791
"chai-as-promised": "^7.1.1",
8892
"deep-equal-in-any-order": "^1.0.10",

providers/caching/index.d.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Logger } from '../../providers/logging'
2+
3+
/** Base configuration options common to all cache implementations */
4+
export interface BaseCacheOptions {
5+
/** Optional logger instance for logging cache operations */
6+
logger?: Logger
7+
/** Default time-to-live in seconds for cached items */
8+
defaultTtlSeconds?: number
9+
}
10+
11+
/** Generic cache interface that all cache implementations should follow */
12+
export interface CacheInterface {
13+
/**
14+
* Initializes the cache
15+
*
16+
* @returns Promise that resolves when initialization is complete
17+
*/
18+
initialize(): Promise<void>
19+
20+
/**
21+
* Cleanup method called when cache is no longer needed
22+
*
23+
* @returns Promise that resolves when cleanup is complete
24+
*/
25+
done(): Promise<void>
26+
27+
/**
28+
* Retrieves an item from the cache
29+
*
30+
* @param item - The key of the item to retrieve
31+
* @returns The cached value or null if not found or expired
32+
*/
33+
get(item: string): Promise<any> | any
34+
35+
/**
36+
* Stores an item in the cache
37+
*
38+
* @param item - The key to store the value under
39+
* @param value - The value to cache
40+
* @param ttlSeconds - Time-to-live in seconds (optional)
41+
*/
42+
set(item: string, value: any, ttlSeconds?: number): Promise<void> | void
43+
44+
/**
45+
* Removes an item from the cache
46+
*
47+
* @param item - The key of the item to remove
48+
*/
49+
delete(item: string): Promise<void> | void
50+
}

providers/caching/memory.d.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) Amazon.com, Inc. or its affiliates and others. Licensed under the MIT license.
2+
// SPDX-License-Identifier: MIT
3+
4+
import { BaseCacheOptions, CacheInterface } from '.'
5+
6+
/** Configuration options for MemoryCache */
7+
export interface MemoryCacheOptions extends BaseCacheOptions {}
8+
9+
/** In-memory cache implementation */
10+
declare class MemoryCache implements CacheInterface {
11+
/** The underlying memory cache instance */
12+
private cache: any
13+
14+
/** Default TTL in seconds for cached items */
15+
private defaultTtlSeconds: number
16+
17+
/**
18+
* Creates a new MemoryCache instance
19+
*
20+
* @param options - Configuration options for the cache
21+
*/
22+
constructor(options: MemoryCacheOptions)
23+
24+
/**
25+
* Initializes the cache (async for interface compatibility)
26+
*
27+
* @returns Promise that resolves when initialization is complete
28+
*/
29+
initialize(): Promise<void>
30+
31+
/**
32+
* Cleanup method called when cache is no longer needed
33+
*
34+
* @returns Promise that resolves when cleanup is complete
35+
*/
36+
done(): Promise<void>
37+
38+
/**
39+
* Retrieves an item from the cache
40+
*
41+
* @param item - The key of the item to retrieve
42+
* @returns The cached value or null if not found or expired
43+
*/
44+
get(item: string): any
45+
46+
/**
47+
* Stores an item in the cache
48+
*
49+
* @param item - The key to store the value under
50+
* @param value - The value to cache
51+
* @param ttlSeconds - Time-to-live in seconds (optional, uses default if not provided)
52+
*/
53+
set(item: string, value: any, ttlSeconds?: number): void
54+
55+
/**
56+
* Removes an item from the cache
57+
*
58+
* @param item - The key of the item to remove
59+
*/
60+
delete(item: string): void
61+
}
62+
63+
/**
64+
* Factory function to create a new MemoryCache instance
65+
*
66+
* @param options - Configuration options for the cache
67+
* @returns A new MemoryCache instance
68+
*/
69+
declare function createMemoryCache(options?: MemoryCacheOptions): MemoryCache
70+
71+
export default createMemoryCache
72+
export { MemoryCache }

0 commit comments

Comments
 (0)