Skip to content

Commit

Permalink
fix: esm build with tsc
Browse files Browse the repository at this point in the history
  • Loading branch information
mildronize committed Feb 9, 2024
1 parent ee73888 commit 99e9900
Show file tree
Hide file tree
Showing 28 changed files with 78 additions and 74 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/cache/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './cache';
export * from './cache.js';
10 changes: 5 additions & 5 deletions src/database/config-rule.service.ts
Original file line number Diff line number Diff line change
@@ -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<SpyConfigRuleEntityAzureTable>();
Expand Down
4 changes: 2 additions & 2 deletions src/database/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './azure-table';
export * from './config-rule.service';
export * from './azure-table.js';
export * from './config-rule.service.js';
2 changes: 1 addition & 1 deletion src/entities/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './spy-config-rule.entity';
export * from './spy-config-rule.entity.js';
2 changes: 1 addition & 1 deletion src/entities/spy-config-rule.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AzureTableEntityBase } from '../database/azure-table';
import type { AzureTableEntityBase } from '../database/azure-table.js';

export interface SpyConfigRuleEntity {
/**
Expand Down
4 changes: 2 additions & 2 deletions src/environment/env.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/environment/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './env';
export * from './env.js';
2 changes: 1 addition & 1 deletion src/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './logger';
export * from './logger.js';
6 changes: 3 additions & 3 deletions src/logger/logger.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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));
Expand Down
18 changes: 9 additions & 9 deletions src/plugins/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './replace-status-code';
export * from './replace-status-code.js';
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/response-transformer/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './response-transformer';
export * from './response-transformer.js';
Original file line number Diff line number Diff line change
@@ -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 =
| {
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/response-transformer/response-transformer.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/rule-matcher.test.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/rule-matcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IncomingMessage } from 'node:http';
import { trimStartAndEndSlash } from '@/utils';
import { trimStartAndEndSlash } from '@/utils/index.js';

export function isMatchedRule(
rule: {
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/spy-middleware.ts
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down
11 changes: 6 additions & 5 deletions src/plugins/spy-rule-plugin.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<RuleConfig['rules'][number]>(rule.rules)) {
if (isMatchedRule(value, req)) {
if (value.plugin === ResponseTransformerPlugin.name) {
const plugin = new ResponseTransformerPlugin(value);
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/spy-rule.test.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/spy-rule.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './utils';
export * from './utils.js';
12 changes: 6 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
"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,
// /* AND if you're building for a library in a monorepo: */
// "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: */
Expand Down

0 comments on commit 99e9900

Please sign in to comment.