Skip to content

Commit

Permalink
Type 'T' does not satisfy the constraint 'IConfiguration' #2065 (#2078)
Browse files Browse the repository at this point in the history
  • Loading branch information
MSNev authored May 18, 2023
1 parent 3f323f8 commit f8546cc
Show file tree
Hide file tree
Showing 14 changed files with 266 additions and 130 deletions.
38 changes: 38 additions & 0 deletions AISKU/Tests/es6-module-type-check/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@microsoft/applicationinsights-test-module-type-check",
"author": "Microsoft Application Insights Team and Contributors",
"version": "3.0.1",
"description": "Microsoft Application Insights ES6 Module and Type check Example",
"homepage": "https://github.com/microsoft/ApplicationInsights-JS#readme",
"keywords": [
"example"
],
"module": "dist-es5/applicationinsights-test-module-type-check.js",
"types": "types/applicationinsights-test-module-type-check.d.ts",
"scripts": {
"clean": "grunt clean",
"build": "tsc --project tsconfig.json --noEmit",
"rebuild": "npm run build",
"test": "",
"mintest": "",
"perftest": "",
"ai-min": "",
"ai-restore": ""
},
"repository": {
"type": "git",
"url": "https://github.com/microsoft/ApplicationInsights-JS/tree/main/AISKU/Tests/es6-module-type-check"
},
"license": "MIT",
"sideEffects": false,
"devDependencies": {
"typescript": "^4.9.3"
},
"peerDependencies": {
"tslib": "*"
},
"dependencies": {
"@microsoft/applicationinsights-common": "3.0.1",
"@microsoft/applicationinsights-web": "3.0.1"
}
}
2 changes: 2 additions & 0 deletions AISKU/Tests/es6-module-type-check/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { ContextTagKeys } from "@microsoft/applicationinsights-common";
import { ITelemetryItem } from "@microsoft/applicationinsights-web";
16 changes: 16 additions & 0 deletions AISKU/Tests/es6-module-type-check/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"lib": ["es2015", "dom"],
"outDir": "./dist",
"strictNullChecks": true,
"experimentalDecorators": true,
"paths": {
"tslib": ["./node_modules/tslib/tslib.es6.js"],
"tsyringe": ["./node_modules/tsyringe/dist/esm2015"]
}
},
"include": ["./src/**/*.ts"]
}

238 changes: 128 additions & 110 deletions common/config/rush/npm-shrinkwrap.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ export class AnalyticsPlugin extends BaseTelemetryPlugin implements IAppInsights
scheduleTimeout(((uri: string) => {
// todo: override start time so that it is not affected by autoRoutePVDelay
_self.trackPageView({ refUri: uri, properties: { duration: 0 } }); // SPA route change loading durations are undefined, so send 0
}).bind(this, _prevUri), _self.autoRoutePVDelay);
}).bind(_self, _prevUri), _self.autoRoutePVDelay);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,62 @@ export class ClickEventTest extends AITestClass {
}
});

this.testCase({
name: "trackPageView properties are correctly assigned (Empty)",
test: () => {
let config = {
coreData: {},
callback: {
pageActionPageTags: () => ({ key2: "value2" })
},
dataTags : {
useDefaultContentNameOrId : true,
metaDataPrefix:"ha-",
customDataPrefix: "data-ha-",
aiBlobAttributeTag: "blob",
parentDataTag: "parent",
dntDataTag: "donotTrack"
}
};
const clickAnalyticsPlugin = new ClickAnalyticsPlugin();
const core = new AppInsightsCore();
const channel = new ChannelPlugin();

core.initialize({
instrumentationKey: "testIkey",
extensionConfig : {
[clickAnalyticsPlugin.identifier] : config
}
} as IConfig & IConfiguration, [clickAnalyticsPlugin, channel]);
this.onDone(() => {
core.unload(false);
});

core.config["extensionConfig"] = core.config["extensionConfig"]? core.config["extensionConfig"] : {};
let extConfig = core.config["extensionConfig"][clickAnalyticsPlugin.identifier]

const traceLogger = new DiagnosticLogger({ loggingLevelConsole: 1 } as any);
const contentHandler = new DomContentHandler(extConfig, traceLogger);
const pageAction = new PageAction(clickAnalyticsPlugin, extConfig, contentHandler, extConfig.callback?.pageActionPageTags, {}, traceLogger );


clickAnalyticsPlugin.trackPageAction()
const element = document.createElement("a");
let spy = this.sandbox.spy(clickAnalyticsPlugin.core, "track");
// clickAnalyticsPlugin.capturePageAction(element, {} as IOverrideValues, {}, false);
pageAction.capturePageAction(element);
Assert.equal(true, spy.called);
let calledEvent: ITelemetryItem = spy.getCall(0).args[0];
Assert.notEqual(-1, calledEvent.data["uri"].indexOf("Tests.html"));
Assert.equal(undefined, calledEvent.data["behavior"]);
Assert.equal(undefined, calledEvent.data["actionType"]);
Assert.equal("[{}]", calledEvent.data["content"]);
Assert.equal(false, isNaN(calledEvent.data["timeToAction"] as number));
Assert.equal("value2", calledEvent.data["properties"]["pageTags"].key2);
}

});

this.testCase({
name: "PageAction properties are correctly assigned (Empty)",
test: () => {
Expand Down
5 changes: 5 additions & 0 deletions rush.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@
"packageName": "@microsoft/applicationinsights-example-shared-worker",
"projectFolder": "examples/shared-worker",
"shouldPublish": false
},
{
"packageName": "@microsoft/applicationinsights-test-module-type-check",
"projectFolder": "AISKU/Tests/es6-module-type-check",
"shouldPublish": false
}
]
}
2 changes: 1 addition & 1 deletion shared/AppInsightsCommon/src/ThrottleMgr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class ThrottleMgr {
// Make sure the root config is dynamic as it may be the global config
config = createDynamicConfig(config as any || {}, null, _logger).cfg;

let unloadHook = onConfigChange((config), () => {
let unloadHook = onConfigChange(config, () => {
_canUseLocalStorage = utlCanUseLocalStorage();

let configMgr = config || {};
Expand Down
10 changes: 5 additions & 5 deletions shared/AppInsightsCore/src/Config/DynamicConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ function _createAndUseHandler<T>(state: _IDynamicConfigHandlerState<T>, configHa
* @param inPlace - Should the passed config be converted in-place or a new proxy returned
* @returns The existing dynamic handler or a new instance with the provided config values
*/
function _createDynamicHandler<T extends IConfiguration>(logger: IDiagnosticLogger, target: T, inPlace: boolean) : IDynamicConfigHandler<T> {
let dynamicHandler = getDynamicConfigHandler(target);
function _createDynamicHandler<T = IConfiguration>(logger: IDiagnosticLogger, target: T, inPlace: boolean) : IDynamicConfigHandler<T> {
let dynamicHandler = getDynamicConfigHandler<T, T>(target);
if (dynamicHandler) {
// The passed config is already dynamic so return it's tracker
return dynamicHandler;
Expand Down Expand Up @@ -170,7 +170,7 @@ function _logInvalidAccess(logger: IDiagnosticLogger, message: string) {
* @param inPlace - Should the config be converted in-place into a dynamic config or a new instance returned, defaults to true
* @returns The dynamic config handler for the config (whether new or existing)
*/
export function createDynamicConfig<T extends IConfiguration>(config: T, defaultConfig?: IConfigDefaults<T>, logger?: IDiagnosticLogger, inPlace?: boolean): IDynamicConfigHandler<T> {
export function createDynamicConfig<T = IConfiguration>(config: T, defaultConfig?: IConfigDefaults<T>, logger?: IDiagnosticLogger, inPlace?: boolean): IDynamicConfigHandler<T> {
let dynamicHandler = _createDynamicHandler<T>(logger, config || {} as T, inPlace);

if (defaultConfig) {
Expand All @@ -189,9 +189,9 @@ export function createDynamicConfig<T extends IConfiguration>(config: T, default
* @returns A watcher handler instance that can be used to remove itself when being unloaded
* @throws TypeError if the provided config is not a dynamic config instance
*/
export function onConfigChange<T>(config: T, configHandler: WatcherFunction<T>, logger?: IDiagnosticLogger): IWatcherHandler<T> {
export function onConfigChange<T = IConfiguration>(config: T, configHandler: WatcherFunction<T>, logger?: IDiagnosticLogger): IWatcherHandler<T> {
let handler: IDynamicConfigHandler<T> = config[CFG_HANDLER_LINK] || config;
if (handler.cfg && (handler.cfg === config || handler.cfg[CFG_HANDLER_LINK] === handler)) {
if (handler.cfg && (handler.cfg === (config as any) || handler.cfg[CFG_HANDLER_LINK] === handler)) {
return handler.watch(configHandler);
}

Expand Down
5 changes: 3 additions & 2 deletions shared/AppInsightsCore/src/Config/DynamicSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

import { isArray, isPlainObject, objForEachKey, symbolFor, throwTypeError } from "@nevware21/ts-utils";
import { IConfiguration } from "../JavaScriptSDK.Interfaces/IConfiguration";
import { IDynamicConfigHandler } from "./IDynamicConfigHandler";
import { _IDynamicConfigHandlerState } from "./_IDynamicConfigHandlerState";

Expand Down Expand Up @@ -57,10 +58,10 @@ export function _cfgDeepCopy<T>(source: T): T {
* @param value
* @returns
*/
export function getDynamicConfigHandler<T>(value: T | IDynamicConfigHandler<T>): IDynamicConfigHandler<T> | null {
export function getDynamicConfigHandler<T = IConfiguration, V = IConfiguration>(value: V | IDynamicConfigHandler<T>): IDynamicConfigHandler<T> | null {
if (value) {
let handler: IDynamicConfigHandler<T> = value[CFG_HANDLER_LINK] || value;
if (handler.cfg && (handler.cfg === value || handler.cfg[CFG_HANDLER_LINK] === handler)) {
if (handler.cfg && (handler.cfg === (value as any) || handler.cfg[CFG_HANDLER_LINK] === handler)) {
return handler;
}
}
Expand Down
6 changes: 3 additions & 3 deletions shared/AppInsightsCore/src/Config/IDynamicConfigHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IWatcherHandler, WatcherFunction } from "./IDynamicWatcher";
/**
* This interface identifies the config which can track changes
*/
export interface IDynamicConfigHandler<T extends IConfiguration> {
export interface IDynamicConfigHandler<T = IConfiguration> {
/**
* Unique Id for this config handler
*/
Expand All @@ -21,7 +21,7 @@ export interface IDynamicConfigHandler<T extends IConfiguration> {
cfg: T;

/**
* The logger instance to use to loger any issues
* The logger instance to use to logger any issues
*/
logger: IDiagnosticLogger,

Expand Down Expand Up @@ -101,7 +101,7 @@ export interface IDynamicConfigHandler<T extends IConfiguration> {
* @internal
* @ignore
*/
export interface _IInternalDynamicConfigHandler<T extends IConfiguration> extends IDynamicConfigHandler<T> {
export interface _IInternalDynamicConfigHandler<T = IConfiguration> extends IDynamicConfigHandler<T> {
/**
* @ignore
* Internal function to explicitly block watching for any config updates
Expand Down
3 changes: 2 additions & 1 deletion shared/AppInsightsCore/src/Config/IDynamicPropertyHandler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { IConfiguration } from "../JavaScriptSDK.Interfaces/IConfiguration";
import { IWatcherHandler } from "./IDynamicWatcher";

export interface IDynamicPropertyHandler<T> {
export interface IDynamicPropertyHandler<T = IConfiguration> {
/**
* Identifies the name of the field that is handled by this handler
*/
Expand Down
10 changes: 5 additions & 5 deletions shared/AppInsightsCore/src/Config/IDynamicWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IUnloadHook } from "../JavaScriptSDK.Interfaces/IUnloadHook";
import { IConfigDefaults } from "./IConfigDefaults";
import { IDynamicPropertyHandler } from "./IDynamicPropertyHandler";

export interface IWatchDetails<T extends IConfiguration> {
export interface IWatchDetails<T = IConfiguration> {
/**
* The current config object
*/
Expand Down Expand Up @@ -44,19 +44,19 @@ export interface IWatchDetails<T extends IConfiguration> {
rdOnly: <C, V = any>(target: C, name: string) => V;
}

export type WatcherFunction<T extends IConfiguration> = (details: IWatchDetails<T>) => void;
export type WatcherFunction<T = IConfiguration> = (details: IWatchDetails<T>) => void;

/**
* @internal
*/
export interface _WatcherChangeDetails<T extends IConfiguration> {
export interface _WatcherChangeDetails<T = IConfiguration> {
d: _IDynamicDetail<T>;
}

/**
* @internal
*/
export interface _IDynamicDetail<T extends IConfiguration> extends IDynamicPropertyHandler<T> {
export interface _IDynamicDetail<T = IConfiguration> extends IDynamicPropertyHandler<T> {

/**
* Add the watcher for monitoring changes
Expand All @@ -69,7 +69,7 @@ export interface _IDynamicDetail<T extends IConfiguration> extends IDynamicPrope
clr: (handler: IWatcherHandler<T>) => void;
}

export interface IWatcherHandler<T extends IConfiguration> extends IUnloadHook {
export interface IWatcherHandler<T = IConfiguration> extends IUnloadHook {
fn: WatcherFunction<T>;
rm: () => void;
}
3 changes: 1 addition & 2 deletions tools/release-tools/setVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ const setPackageJsonRelease = () => {
if (theFilename.indexOf("/package.json") !== -1) {
let srcFolder = theFilename.replace("/package.json", "/**/*.ts", "/**/*.tsx", "/**/*.html");
console.log(" - Checking source files: " + srcFolder);
const tsFiles = globby.sync(srcFolder);
const tsFiles = globby.sync([srcFolder, "!**/node_modules/**"]);
tsFiles.map(sourceFile => {
// Don't update node_modules
if (shouldProcess(sourceFile)) {
Expand Down Expand Up @@ -543,7 +543,6 @@ if (parseArgs()) {
const newContent = JSON.stringify(theVersion, null, 4) + "\n";
fs.writeFileSync(process.cwd() + "/version.json", newContent);
}

} else {
console.error("Failed to identify the new version number");
}
Expand Down

0 comments on commit f8546cc

Please sign in to comment.