diff --git a/package.json b/package.json index 370e55b..1ff5a44 100644 --- a/package.json +++ b/package.json @@ -13,13 +13,16 @@ "default": "./dist/main.cjs" } }, + "imports": { + "@/*": "./src/*" + }, "scripts": { "start": "tsx src/main.ts", "dev": "tsx watch src/main.ts", "test:coverage": "vitest run --coverage", "test": "vitest", "test:ci": "vitest run", - "build": "esbuild src/main.ts --bundle --platform=node --target=node16 --format=cjs --outfile=dist/main.cjs --minify", + "build": "tsc", "format": "prettier -w src", "lint": "tsc --noEmit && eslint ./src && prettier -c src", "lint:fix": "eslint --fix ./src && prettier -w src", diff --git a/src/cache/index.ts b/src/cache/index.ts index 77e55d4..0d13c97 100644 --- a/src/cache/index.ts +++ b/src/cache/index.ts @@ -1 +1 @@ -export * from './cache'; +export * from './cache.js'; diff --git a/src/database/config-rule.service.ts b/src/database/config-rule.service.ts index 5704dcd..edfbce0 100644 --- a/src/database/config-rule.service.ts +++ b/src/database/config-rule.service.ts @@ -1,11 +1,11 @@ -import { logger } from '@/logger'; -import type { SpyConfigRuleEntityAzureTable } from '../entities/spy-config-rule.entity'; -import { AzureTable } from './azure-table'; +import { logger } from '@/logger/index.js'; +import type { SpyConfigRuleEntityAzureTable } from '../entities/spy-config-rule.entity.js'; +import { AzureTable } from './azure-table.js'; import invariant from 'tiny-invariant'; import { TableClient } from '@azure/data-tables'; -import { env } from '@/environment'; +import { env } from '@/environment/index.js'; import { ODataExpression } from 'ts-odata-client'; -import { trimStartAndEndSlash } from '@/utils'; +import { trimStartAndEndSlash } from '@/utils/index.js'; export class SpyConfigRuleService { private oDataExpresion = ODataExpression.forV4(); diff --git a/src/database/index.ts b/src/database/index.ts index 5fd0104..3ddabfb 100644 --- a/src/database/index.ts +++ b/src/database/index.ts @@ -1,2 +1,2 @@ -export * from './azure-table'; -export * from './config-rule.service'; +export * from './azure-table.js'; +export * from './config-rule.service.js'; diff --git a/src/entities/index.ts b/src/entities/index.ts index 787e0bd..516b7d9 100644 --- a/src/entities/index.ts +++ b/src/entities/index.ts @@ -1 +1 @@ -export * from './spy-config-rule.entity'; +export * from './spy-config-rule.entity.js'; diff --git a/src/entities/spy-config-rule.entity.ts b/src/entities/spy-config-rule.entity.ts index be710ab..1e321b2 100644 --- a/src/entities/spy-config-rule.entity.ts +++ b/src/entities/spy-config-rule.entity.ts @@ -1,4 +1,4 @@ -import type { AzureTableEntityBase } from '../database/azure-table'; +import type { AzureTableEntityBase } from '../database/azure-table.js'; export interface SpyConfigRuleEntity { /** diff --git a/src/environment/env.ts b/src/environment/env.ts index 59b2f0e..a2ed987 100644 --- a/src/environment/env.ts +++ b/src/environment/env.ts @@ -1,6 +1,6 @@ import 'dotenv/config'; -import { environmentSchema } from '../environment/env.schema'; -import { extractErorMessage } from '@/utils'; +import { environmentSchema } from '../environment/env.schema.js'; +import { extractErorMessage } from '@/utils/index.js'; import { zodParser } from '@thaitype/record-parser/zod'; /** * Get environment variables diff --git a/src/environment/index.ts b/src/environment/index.ts index c1532d6..7915515 100644 --- a/src/environment/index.ts +++ b/src/environment/index.ts @@ -1 +1 @@ -export * from './env'; +export * from './env.js'; diff --git a/src/logger/index.ts b/src/logger/index.ts index 1ff09ef..3bb73ef 100644 --- a/src/logger/index.ts +++ b/src/logger/index.ts @@ -1 +1 @@ -export * from './logger'; +export * from './logger.js'; diff --git a/src/logger/logger.ts b/src/logger/logger.ts index 097bdbc..23b0794 100644 --- a/src/logger/logger.ts +++ b/src/logger/logger.ts @@ -1,6 +1,6 @@ -import { env } from '@/environment'; -import pino from 'pino'; -import pinoHttp from 'pino-http'; +import { env } from '@/environment/index.js'; +import { pino } from 'pino'; +import { pinoHttp } from 'pino-http'; import fs from 'fs'; const logDir = '.srp/logs'; diff --git a/src/main.ts b/src/main.ts index 66906bf..07ca9f4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,5 +1,5 @@ -import { server } from './server'; -import { extractErorMessage } from './utils/utils'; +import { server } from './server.js'; +import { extractErorMessage } from './utils/utils.js'; server().catch(error => { console.error(extractErorMessage(error)); diff --git a/src/plugins/bootstrap.ts b/src/plugins/bootstrap.ts index 663f091..ca9cb71 100644 --- a/src/plugins/bootstrap.ts +++ b/src/plugins/bootstrap.ts @@ -1,13 +1,13 @@ -import { initSampleRule, spyConfigRuleService } from '../database'; -import { env } from '../environment'; -import { httpLogger, logger, stringLogger } from '../logger'; +import { initSampleRule, spyConfigRuleService } from '../database/index.js'; +import { env } from '../environment/index.js'; +import { httpLogger, logger, stringLogger } from '../logger/index.js'; import type express from 'express'; -import { spyMiddleware } from './spy-middleware'; -import type { SpyConfigRuleEntityAzureTable } from '@/entities'; -import { SpyRule } from './spy-rule'; -import { cache } from '@/cache'; -import type { RuleConfig } from './rule.schema'; -import { ruleConfigSchema } from './rule.schema'; +import { spyMiddleware } from './spy-middleware.js'; +import type { SpyConfigRuleEntityAzureTable } from '@/entities/index.js'; +import { SpyRule } from './spy-rule.js'; +import { cache } from '@/cache/index.js'; +import type { RuleConfig } from './rule.schema.js'; +import { ruleConfigSchema } from './rule.schema.js'; import { DataViewer, DataContainer } from '@thaitype/data-viewer-server'; export async function initRulePlugin() { diff --git a/src/plugins/index.ts b/src/plugins/index.ts index afd4fca..7226190 100644 --- a/src/plugins/index.ts +++ b/src/plugins/index.ts @@ -1,4 +1,4 @@ -export * from './response-transformer'; -export * from './spy-rule-plugin'; -export * from './rule-matcher'; -export * from './bootstrap'; +export * from './response-transformer/index.js'; +export * from './spy-rule-plugin.js'; +export * from './rule-matcher.js'; +export * from './bootstrap.js'; diff --git a/src/plugins/response-transformer/action-expressions/base-action-expression.ts b/src/plugins/response-transformer/action-expressions/base-action-expression.ts index 8b15069..0a69b8f 100644 --- a/src/plugins/response-transformer/action-expressions/base-action-expression.ts +++ b/src/plugins/response-transformer/action-expressions/base-action-expression.ts @@ -1,6 +1,6 @@ -import type { HandleResponseParams } from '@/plugins'; -import type { ExpressionOptions, ExpressionValidateResult } from '../response-transformer-expression'; -import { logger } from '@/logger'; +import type { HandleResponseParams } from '@/plugins/index.js'; +import type { ExpressionOptions, ExpressionValidateResult } from '../response-transformer-expression.js'; +import { logger } from '@/logger/index.js'; export abstract class BaseActionExpression { constructor( diff --git a/src/plugins/response-transformer/action-expressions/index.ts b/src/plugins/response-transformer/action-expressions/index.ts index 8361132..ce0b7df 100644 --- a/src/plugins/response-transformer/action-expressions/index.ts +++ b/src/plugins/response-transformer/action-expressions/index.ts @@ -1 +1 @@ -export * from './replace-status-code'; +export * from './replace-status-code.js'; diff --git a/src/plugins/response-transformer/action-expressions/replace-status-code.ts b/src/plugins/response-transformer/action-expressions/replace-status-code.ts index 90646a4..837bdf4 100644 --- a/src/plugins/response-transformer/action-expressions/replace-status-code.ts +++ b/src/plugins/response-transformer/action-expressions/replace-status-code.ts @@ -1,5 +1,5 @@ -import type { ExpressionValidateResult } from '../response-transformer-expression'; -import { BaseActionExpression } from './base-action-expression'; +import type { ExpressionValidateResult } from '../response-transformer-expression.js'; +import { BaseActionExpression } from './base-action-expression.js'; export class ReplaceStatusCodeActionExpression extends BaseActionExpression { validate(): ExpressionValidateResult { diff --git a/src/plugins/response-transformer/index.ts b/src/plugins/response-transformer/index.ts index 009222f..8b1214f 100644 --- a/src/plugins/response-transformer/index.ts +++ b/src/plugins/response-transformer/index.ts @@ -1 +1 @@ -export * from './response-transformer'; +export * from './response-transformer.js'; diff --git a/src/plugins/response-transformer/response-transformer-expression.ts b/src/plugins/response-transformer/response-transformer-expression.ts index cb90e81..95f9b1c 100644 --- a/src/plugins/response-transformer/response-transformer-expression.ts +++ b/src/plugins/response-transformer/response-transformer-expression.ts @@ -1,6 +1,6 @@ -import type { Rule } from '../rule.schema'; -import type { HandleResponseParams } from '../spy-rule-plugin'; -import { ReplaceStatusCodeActionExpression } from './action-expressions'; +import type { Rule } from '../rule.schema.js'; +import type { HandleResponseParams } from '../spy-rule-plugin.js'; +import { ReplaceStatusCodeActionExpression } from './action-expressions/index.js'; export type ExpressionValidateResult = | { diff --git a/src/plugins/response-transformer/response-transformer.ts b/src/plugins/response-transformer/response-transformer.ts index f8f892a..7ffbae5 100644 --- a/src/plugins/response-transformer/response-transformer.ts +++ b/src/plugins/response-transformer/response-transformer.ts @@ -1,7 +1,7 @@ -import { logger } from '@/logger'; -import type { HandleResponseParams } from '../spy-rule-plugin'; -import { ResponseTransformerExpression } from './response-transformer-expression'; -import type { Rule } from '../rule.schema'; +import { logger } from '@/logger/index.js'; +import type { HandleResponseParams } from '../spy-rule-plugin.js'; +import { ResponseTransformerExpression } from './response-transformer-expression.js'; +import type { Rule } from '../rule.schema.js'; export class ResponseTransformerPlugin { static name = 'response-transformer'; diff --git a/src/plugins/rule-matcher.test.ts b/src/plugins/rule-matcher.test.ts index 7fbca9c..a539ddc 100644 --- a/src/plugins/rule-matcher.test.ts +++ b/src/plugins/rule-matcher.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from 'vitest'; -import { isMatchedRule } from './rule-matcher'; +import { isMatchedRule } from './rule-matcher.js'; import httpMocks from 'node-mocks-http'; const sharedRule = { diff --git a/src/plugins/rule-matcher.ts b/src/plugins/rule-matcher.ts index 3fb9178..2f6a2e7 100644 --- a/src/plugins/rule-matcher.ts +++ b/src/plugins/rule-matcher.ts @@ -1,5 +1,5 @@ import type { IncomingMessage } from 'node:http'; -import { trimStartAndEndSlash } from '@/utils'; +import { trimStartAndEndSlash } from '@/utils/index.js'; export function isMatchedRule( rule: { diff --git a/src/plugins/spy-middleware.ts b/src/plugins/spy-middleware.ts index e9baae3..790309f 100644 --- a/src/plugins/spy-middleware.ts +++ b/src/plugins/spy-middleware.ts @@ -1,8 +1,8 @@ import type { Options as HttpProxyMiddlewareOptions } from 'http-proxy-middleware'; import { createProxyMiddleware, responseInterceptor } from 'http-proxy-middleware'; -import { env } from '@/environment'; -import { stringLogger } from '@/logger'; -import { SpyRulePlugin } from './spy-rule-plugin'; +import { env } from '@/environment/env.js'; +import { stringLogger } from '@/logger/logger.js'; +import { SpyRulePlugin } from './spy-rule-plugin.js'; /** * Configure proxy middleware */ diff --git a/src/plugins/spy-rule-plugin.ts b/src/plugins/spy-rule-plugin.ts index d9aaeec..5860344 100644 --- a/src/plugins/spy-rule-plugin.ts +++ b/src/plugins/spy-rule-plugin.ts @@ -1,8 +1,9 @@ -import { logger } from '@/logger'; +import { logger } from '@/logger/logger.js'; import type { IncomingMessage, ServerResponse } from 'node:http'; -import { ResponseTransformerPlugin } from './response-transformer'; -import { getSpyConfig } from './bootstrap'; -import { isMatchedRule } from './rule-matcher'; +import { ResponseTransformerPlugin } from './response-transformer/response-transformer.js'; +import { getSpyConfig } from './bootstrap.js'; +import { isMatchedRule } from './rule-matcher.js'; +import type { RuleConfig } from './rule.schema.js'; export interface HandleResponseParams { responseBuffer: Buffer; @@ -20,7 +21,7 @@ export class SpyRulePlugin { res.setHeader('Powered-by', 'thaitype/spy-reverse-proxy'); const rule = await getSpyConfig(); - for (const value of Object.values(rule.rules)) { + for (const value of Object.values(rule.rules)) { if (isMatchedRule(value, req)) { if (value.plugin === ResponseTransformerPlugin.name) { const plugin = new ResponseTransformerPlugin(value); diff --git a/src/plugins/spy-rule.test.ts b/src/plugins/spy-rule.test.ts index e16ec8d..1d32ffc 100644 --- a/src/plugins/spy-rule.test.ts +++ b/src/plugins/spy-rule.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from 'vitest'; -import { SpyRule } from './spy-rule'; -import { RuleConfig } from './rule.schema'; +import { SpyRule } from './spy-rule.js'; +import { RuleConfig } from './rule.schema.js'; const sharedRule = { plugin: 'plugin', diff --git a/src/plugins/spy-rule.ts b/src/plugins/spy-rule.ts index 3b56df2..9667657 100644 --- a/src/plugins/spy-rule.ts +++ b/src/plugins/spy-rule.ts @@ -1,8 +1,8 @@ -import type { SpyConfigRuleEntity } from '@/entities'; -import type { Rule, RuleConfig } from './rule.schema'; -import { trimStartAndEndSlash } from '@/utils'; -import { ResponseTransformerPlugin } from '.'; -import { ResponseTransformerExpression } from './response-transformer/response-transformer-expression'; +import type { SpyConfigRuleEntity } from '@/entities/spy-config-rule.entity.js'; +import type { Rule, RuleConfig } from './rule.schema.js'; +import { trimStartAndEndSlash } from '@/utils/index.js'; +import { ResponseTransformerExpression } from './response-transformer/response-transformer-expression.js'; +import { ResponseTransformerPlugin } from './response-transformer/response-transformer.js'; /** * TODO: Hard coded allowed plugins diff --git a/src/server.ts b/src/server.ts index c8629ff..850f30d 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,7 +1,7 @@ import express from 'express'; -import { env } from '@/environment'; -import { logger } from '@/logger'; -import { registerSpyPlugin } from './plugins'; +import { env } from '@/environment/env.js'; +import { logger } from '@/logger/logger.js'; +import { registerSpyPlugin } from './plugins/index.js'; export async function server() { const app = express(); diff --git a/src/utils/index.ts b/src/utils/index.ts index 04bca77..9be8099 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1 +1 @@ -export * from './utils'; +export * from './utils.js'; diff --git a/tsconfig.json b/tsconfig.json index 47d7457..fcb5303 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,9 +12,9 @@ "strict": true, "noUncheckedIndexedAccess": true, // /* If transpiling with TypeScript: */ - // "moduleResolution": "NodeNext", - // "module": "NodeNext", - // "outDir": "dist", + "moduleResolution": "NodeNext", + "module": "NodeNext", + "outDir": "dist", // "sourceMap": true, // /* AND if you're building for a library: */ // "declaration": true, @@ -22,9 +22,9 @@ // "composite": true, // "declarationMap": true, /* If NOT transpiling with TypeScript: */ - "moduleResolution": "Bundler", - "module": "ESNext", - "noEmit": true, + // "moduleResolution": "Bundler", + // "module": "ESNext", + // "noEmit": true, // /* If your code runs in the DOM: */ "lib": ["es2022", "dom", "dom.iterable"], /* If your code doesn't run in the DOM: */