From ee23f4ee36570d386e8e502e7db3a8d26dca648c Mon Sep 17 00:00:00 2001 From: Anton Date: Fri, 21 Jun 2024 10:10:27 +0300 Subject: [PATCH] feat: updated to type: module at package.json --- src/example/login-and-listen.ts | 2 +- src/index.ts | 4 ++-- src/lib/asterisk-ami-adapter.ts | 24 ++++++++++-------------- src/lib/index.ts | 1 + src/test/unit/index.ts | 2 +- src/types/index.ts | 2 +- tsconfig.json | 14 +++++++------- 7 files changed, 23 insertions(+), 26 deletions(-) create mode 100644 src/lib/index.ts diff --git a/src/example/login-and-listen.ts b/src/example/login-and-listen.ts index 250139b..ef2bca6 100644 --- a/src/example/login-and-listen.ts +++ b/src/example/login-and-listen.ts @@ -2,7 +2,7 @@ import { setTimeout as setTimeoutPromise } from "node:timers/promises"; -import { AsteriskAmiAdapter, Types } from ".."; +import { AsteriskAmiAdapter, Types } from "../index.js"; const config = { AMI_HOST: process.env.AMI_HOST || "localhost", diff --git a/src/index.ts b/src/index.ts index 6d57885..a122b3f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ -export * as Types from "./types"; -export { AsteriskAmiAdapter } from "./lib/asterisk-ami-adapter"; +export * as Types from "./types/index.js"; +export { AsteriskAmiAdapter } from "./lib/index.js"; diff --git a/src/lib/asterisk-ami-adapter.ts b/src/lib/asterisk-ami-adapter.ts index 1d2e0d4..1fc1f14 100644 --- a/src/lib/asterisk-ami-adapter.ts +++ b/src/lib/asterisk-ami-adapter.ts @@ -4,7 +4,7 @@ import fs from "node:fs"; import net from "node:net"; import { setTimeout as setTimeoutPromise } from "node:timers/promises"; -import * as Types from "../types"; +import * as Types from "../types/index.js"; export class AsteriskAmiAdapter extends EventEmitter { #NEW_LINE = "\r\n"; @@ -74,11 +74,12 @@ export class AsteriskAmiAdapter extends EventEmitter { } } - const logMethods: { [k: string]: "error" | "log" | "warn"; } = { + const logMethods = { + ERROR: "error", INFO: "log", WARN: "warn", - }; + } as const; const method = logMethods[level]; @@ -118,7 +119,7 @@ export class AsteriskAmiAdapter extends EventEmitter { return str + this.#NEW_LINE; } - #getFormatedUuid = crypto.randomUUID; + #getFormattedUuid = crypto.randomUUID; #processData(data: string) { if (data.substring(0, 21) === "Asterisk Call Manager") { @@ -148,18 +149,13 @@ export class AsteriskAmiAdapter extends EventEmitter { const event: Types.TAmiEvent = preparedEvents.reduce((p: Types.TAmiMessageOut, c: string) => { const [key, value]: string[] = c.split(": ", 2); - if (!key) return p; + if (!key || !value) return p; if (key === "Variable") { - if (!p.Variable) { - p.Variable = {}; - } + const [k, v] = value.split("="); - if (value) { - const [k, v] = value.split("="); - - p.Variable[k] = v; - } + if (!p.Variable) p.Variable = {}; + if (k && v) p.Variable[k] = v; } else { p[key] = value; } @@ -174,7 +170,7 @@ export class AsteriskAmiAdapter extends EventEmitter { } #send(data: Types.TAmiMessageIn & Types.TAmiMessageToSocket, callback?: Types.TCallBack) { - if (!data.ActionID) data.ActionID = this.#getFormatedUuid(); + if (!data.ActionID) data.ActionID = this.#getFormattedUuid(); const actionID = data.ActionID; diff --git a/src/lib/index.ts b/src/lib/index.ts new file mode 100644 index 0000000..f8f017d --- /dev/null +++ b/src/lib/index.ts @@ -0,0 +1 @@ +export * from "./asterisk-ami-adapter.js"; diff --git a/src/test/unit/index.ts b/src/test/unit/index.ts index 2113ca1..da0a695 100644 --- a/src/test/unit/index.ts +++ b/src/test/unit/index.ts @@ -1,7 +1,7 @@ import assert from "node:assert"; import test from "node:test"; -import { AsteriskAmiAdapter } from "../.."; +import { AsteriskAmiAdapter } from "../../index.js"; const amiAdapter = new AsteriskAmiAdapter({ encoding: "ascii", diff --git a/src/types/index.ts b/src/types/index.ts index 6b57160..e9db7b2 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1 +1 @@ -export * from "./common"; +export * from "./common.js"; diff --git a/tsconfig.json b/tsconfig.json index 8297c5c..fb00675 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,8 +11,8 @@ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ /* Language and Environment */ - "target": "es2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ - // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + "target": "esnext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "lib": ["esnext"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "jsx": "preserve", /* Specify what JSX code is generated. */ // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ @@ -24,9 +24,9 @@ // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ /* Modules */ - "module": "commonjs", /* Specify what module code is generated. */ + "module": "esnext", /* Specify what module code is generated. */ "rootDir": "./src", /* Specify the root folder within your source files. */ - // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ + "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ @@ -67,7 +67,7 @@ // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ - // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ + "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */ // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ @@ -81,14 +81,14 @@ // "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */ // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ // "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */ - // "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */ + "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */ // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ // "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */ // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */ // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ - // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ + "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */ // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */