diff --git a/.eslintrc.js b/.eslintrc.js index e770ef2..c050650 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -10,6 +10,8 @@ module.exports = { project: "./tsconfig.json", }, rules: { + "@typescript-eslint/consistent-type-imports": "error", + "@typescript-eslint/no-import-type-side-effects": "error", // Too restrictive, writing ugly code to defend against a very unlikely scenario: https://eslint.org/docs/rules/no-prototype-builtins "no-prototype-builtins": "off", // https://basarat.gitbooks.io/typescript/docs/tips/defaultIsBad.html diff --git a/app.js b/app.js index 83c17ed..5d07e64 100644 --- a/app.js +++ b/app.js @@ -1 +1 @@ -module.exports = require("./dist/app/index.js"); +module.exports = require("./dist/index.js"); diff --git a/package.json b/package.json index 5c2f373..3cf8073 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,10 @@ "repository": "https://github.com/figedi/svc.git", "author": "Felix Kaminski ", "license": "MIT", + "exports": { + "import": "./dist/index.mjs", + "require": "./dist/index.js" + }, "scripts": { "clean": "rm -rf dist && mkdir -p dist", "build": "npm run clean && tsup", diff --git a/src/app/ApplicationBuilder.spec.ts b/src/app/ApplicationBuilder.spec.ts index 6ad6048..9f64310 100644 --- a/src/app/ApplicationBuilder.spec.ts +++ b/src/app/ApplicationBuilder.spec.ts @@ -3,7 +3,7 @@ import { expect } from "chai"; import { assert, stub } from "sinon"; import { ApplicationBuilder } from "./ApplicationBuilder"; -import { Command, Provider, ErrorHandle, ShutdownHandle } from "./types/app"; +import { type Command, type Provider, ErrorHandle, ShutdownHandle } from "./types/app"; import { createStubbedLogger } from "../logger"; describe("ApplicationBuilder", function AppBuilderTest() { diff --git a/src/app/ApplicationBuilder.ts b/src/app/ApplicationBuilder.ts index b5a3de6..0847f39 100644 --- a/src/app/ApplicationBuilder.ts +++ b/src/app/ApplicationBuilder.ts @@ -27,7 +27,7 @@ import type { DynamicObservableTransformFn, DynamicConfigFnArgs, } from "./types"; -import { type RemoteDependencyArgs, IRemoteSource } from "./remoteConfig"; +import type { RemoteDependencyArgs, IRemoteSource } from "./remoteConfig"; import type { DeepMerge } from "./types/base"; import { onExit } from "signal-exit"; @@ -42,7 +42,7 @@ import { toConstantCase, reduceTree, serviceWithPreflightOrShutdown, - TreeNodeTransformerConfig, + type TreeNodeTransformerConfig, } from "./utils"; import { ShutdownHandle, ErrorHandle, REF_SYMBOLS, REF_TYPES, isTransformer } from "./types"; import buildOptions from "minimist-options"; diff --git a/src/app/remoteConfig/RemoteConfig.spec.ts b/src/app/remoteConfig/RemoteConfig.spec.ts index 6fc2d68..ccedc48 100644 --- a/src/app/remoteConfig/RemoteConfig.spec.ts +++ b/src/app/remoteConfig/RemoteConfig.spec.ts @@ -1,4 +1,4 @@ -import { getVersion, ConfigRepository, getRootSchema, SCHEMA_BASE_DIR } from "@figedi/svc-config"; +import { getVersion, type ConfigRepository, getRootSchema, SCHEMA_BASE_DIR } from "@figedi/svc-config"; import { SopsClient } from "@figedi/sops"; import { KmsKeyDecryptor, setupStubbedKms } from "@figedi/sops/kms"; import nock from "nock"; @@ -10,15 +10,15 @@ import type { KeyManagementServiceClient } from "@google-cloud/kms"; import { createStubbedConfigValues, createStubbedResponses, - StubbedResponses, - StubbedConfigValues, + type StubbedResponses, + type StubbedConfigValues, createUpdateStrategyStub, } from "./shared.specFiles"; import { TestApplicationBuilder } from "../TestApplicationBuilder"; import { PollingRemoteSource } from "./remoteSource/PollingRemoteSource"; import { createStubbedLogger } from "../../logger"; import { ApplicationBuilder } from "../ApplicationBuilder"; -import { ReactsOnFn } from "./types"; +import type { ReactsOnFn } from "./types"; import { InvalidConfigWithoutDataError, MaxRetriesWithoutDataError } from "./remoteSource"; import { sleep } from "../utils"; import { assertInTestAppBuilder, assertErrorInTestAppBuilder } from "../shared.specFiles/helpers"; diff --git a/src/app/remoteConfig/RemoteConfigHandler.ts b/src/app/remoteConfig/RemoteConfigHandler.ts index c150b03..276c10f 100644 --- a/src/app/remoteConfig/RemoteConfigHandler.ts +++ b/src/app/remoteConfig/RemoteConfigHandler.ts @@ -1,6 +1,6 @@ -import { Observable, Subscription } from "rxjs"; import { filter, concatMap, pairwise, startWith } from "rxjs/operators"; -import { ServiceWithLifecycleHandlers } from "../types/service"; +import type { Observable, Subscription } from "rxjs"; +import type { ServiceWithLifecycleHandlers } from "../types/service"; export class RemoteConfigHandler implements ServiceWithLifecycleHandlers { private subscription?: Subscription; diff --git a/src/app/remoteConfig/remoteSource/BaseRemoteSource.ts b/src/app/remoteConfig/remoteSource/BaseRemoteSource.ts index d0b7111..2dc5585 100644 --- a/src/app/remoteConfig/remoteSource/BaseRemoteSource.ts +++ b/src/app/remoteConfig/remoteSource/BaseRemoteSource.ts @@ -1,7 +1,7 @@ /* eslint-disable max-classes-per-file */ import type { MeteringRecorder } from "@figedi/metering"; -import { createValidator, SchemaValidationError, SchemaValidator, JSONSchema } from "@figedi/typecop"; -import { Subject, Observable, lastValueFrom } from "rxjs"; +import type { SchemaValidator, JSONSchema } from "@figedi/typecop"; +import { Subject, type Observable, lastValueFrom } from "rxjs"; import { take } from "rxjs/operators"; import { parse } from "semver"; import stringify from "fast-json-stable-stringify"; @@ -48,7 +48,7 @@ export abstract class BaseRemoteSource { try { return this.validator!.validate(this.rootSchema!, data); } catch (e) { - if (e instanceof SchemaValidationError) { + if (e.constructor.name === "SchemaValidationError") { return false; } throw e; @@ -217,7 +217,8 @@ export abstract class BaseRemoteSource { this.initMetrics(); this.trySetRequiredMetrics(); this.stopped = false; - this.validator = createValidator(); + const typecopMod = await import("@figedi/typecop"); + this.validator = typecopMod.createValidator(); this.rootSchema = await this.validator.compile(this.schema, [this.schemaBaseDir]); } diff --git a/src/app/remoteConfig/remoteSource/PollingRemoteSource.ts b/src/app/remoteConfig/remoteSource/PollingRemoteSource.ts index 2869acd..0df7cad 100644 --- a/src/app/remoteConfig/remoteSource/PollingRemoteSource.ts +++ b/src/app/remoteConfig/remoteSource/PollingRemoteSource.ts @@ -1,6 +1,6 @@ import type { MeteringRecorder } from "@figedi/metering"; import type { JSONSchema } from "@figedi/typecop"; -import axios, { AxiosResponse } from "axios"; +import axios, { type AxiosResponse } from "axios"; import { remapTree, sleep } from "../../utils"; import { BaseRemoteSource } from "./BaseRemoteSource"; diff --git a/src/app/remoteConfig/shared.specFiles/fixtures/configValues.ts b/src/app/remoteConfig/shared.specFiles/fixtures/configValues.ts index f7e5b18..e864839 100644 --- a/src/app/remoteConfig/shared.specFiles/fixtures/configValues.ts +++ b/src/app/remoteConfig/shared.specFiles/fixtures/configValues.ts @@ -1,6 +1,6 @@ import { set, cloneDeep } from "lodash"; // eslint-disable-next-line import/no-extraneous-dependencies -import { getFallback, getVersion, SecretsConfiguration } from "@figedi/svc-config"; +import { getFallback, getVersion, type SecretsConfiguration } from "@figedi/svc-config"; // eslint-disable-next-line import/no-extraneous-dependencies import { encryptJson } from "@figedi/sops/test"; diff --git a/src/app/remoteConfig/shared.specFiles/updateStrategyStub.ts b/src/app/remoteConfig/shared.specFiles/updateStrategyStub.ts index 2f40934..a161851 100644 --- a/src/app/remoteConfig/shared.specFiles/updateStrategyStub.ts +++ b/src/app/remoteConfig/shared.specFiles/updateStrategyStub.ts @@ -1,4 +1,4 @@ -import { IReloadingStrategy } from "../types"; +import type { IReloadingStrategy } from "../types"; export const createUpdateStrategyStub = (): IReloadingStrategy => ({ execute: async () => {}, diff --git a/src/app/remoteConfig/types/config.ts b/src/app/remoteConfig/types/config.ts index 11d8c3e..c334a24 100644 --- a/src/app/remoteConfig/types/config.ts +++ b/src/app/remoteConfig/types/config.ts @@ -1,4 +1,4 @@ -import { DynamicOnceTransformFn, DynamicStreamedTransformFn } from "../../types"; +import type { DynamicOnceTransformFn, DynamicStreamedTransformFn } from "../../types"; export type RemoteDependencyArgs = { streamed: DynamicStreamedTransformFn; diff --git a/src/app/shared.specFiles/helpers.ts b/src/app/shared.specFiles/helpers.ts index 9f65d09..f10453b 100644 --- a/src/app/shared.specFiles/helpers.ts +++ b/src/app/shared.specFiles/helpers.ts @@ -1,7 +1,7 @@ // eslint-disable-next-line import/no-extraneous-dependencies import { expect } from "chai"; -import { TestApplicationBuilder } from "../TestApplicationBuilder"; -import { RegisterFnArgs } from "../ApplicationBuilder"; +import type { TestApplicationBuilder } from "../TestApplicationBuilder"; +import type { RegisterFnArgs } from "../ApplicationBuilder"; import { sleep } from "../utils"; export const assertInTestAppBuilder = async ( diff --git a/src/app/utils/service.ts b/src/app/utils/service.ts index 37c48a9..1ec6fd3 100644 --- a/src/app/utils/service.ts +++ b/src/app/utils/service.ts @@ -1,4 +1,4 @@ -import { ServiceWithLifecycleHandlers } from "../types"; +import type { ServiceWithLifecycleHandlers } from "../types"; export const serviceWithPreflightOrShutdown = (svc: any): svc is ServiceWithLifecycleHandlers => "preflight" in svc || "shutdown" in svc; diff --git a/src/k8s/K8sRollingUpdateStrategy.ts b/src/k8s/K8sRollingUpdateStrategy.ts index ac153a5..f1a32c1 100644 --- a/src/k8s/K8sRollingUpdateStrategy.ts +++ b/src/k8s/K8sRollingUpdateStrategy.ts @@ -1,7 +1,7 @@ -import { IReloadingStrategy, IReplicaService } from "../app/remoteConfig"; -import { ServiceWithLifecycleHandlers } from "../app/types"; +import type { IReloadingStrategy, IReplicaService } from "../app/remoteConfig"; +import type { ServiceWithLifecycleHandlers } from "../app/types"; import { sleep } from "../app/utils"; -import { Logger } from "../logger"; +import type { Logger } from "../logger"; /** * Reloading-strategy to gracefully restart a pod with neighbouring replicas. @@ -17,7 +17,10 @@ import { Logger } from "../logger"; export class K8sRollingUpdateStrategy implements IReloadingStrategy, ServiceWithLifecycleHandlers { private static RESTART_SLEEP_TIME_RANGE_MS = 10000; - constructor(private replicaService: IReplicaService, private logger: Logger) {} + constructor( + private replicaService: IReplicaService, + private logger: Logger, + ) {} public preflight(): void { if (!this.replicaService || !this.logger) { diff --git a/src/logger/index.ts b/src/logger/index.ts index 2597139..aa71907 100644 --- a/src/logger/index.ts +++ b/src/logger/index.ts @@ -1,4 +1,4 @@ -import pino, { LoggerOptions as PinoLoggerOptions } from "pino"; +import pino, { type LoggerOptions as PinoLoggerOptions } from "pino"; export interface LoggerBaseProperties { service: string; diff --git a/tsup.config.ts b/tsup.config.ts index 2091e93..d017d73 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -1,7 +1,7 @@ import { defineConfig } from "tsup"; export default defineConfig({ - entry: ["src/index.ts", "src/k8s/index.ts", "src/logger/index.ts", "src/app/index.ts"], + entry: ["src/index.ts", "src/k8s/index.ts", "src/logger/index.ts", "src/remoteConfig/index.ts"], format: ["cjs", "esm"], outDir: "dist", sourcemap: false, @@ -9,5 +9,4 @@ export default defineConfig({ shims: true, clean: true, treeshake: true, - });