Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
/*
* MIT License
*
* Copyright (c) 2023 FoxifyJS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

/*
* For a detailed explanation regarding each configuration property and type check, visit:
* https://jestjs.io/docs/configuration
Expand Down Expand Up @@ -34,7 +59,7 @@ export default {
],

// Indicates which provider should be used to instrument code for coverage
// coverageProvider: "babel",
coverageProvider: "v8",

// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
Expand Down Expand Up @@ -188,7 +213,7 @@ export default {
"^.+\\.tsx?$": [
"ts-jest",
{
tsconfig: "<rootDir>/tsconfig.json",
tsconfig : "<rootDir>/tsconfig.json",
isolatedModules: true,
},
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"typedoc": "^0.24.8",
"typescript": "^5.1.3"
"typescript": "^5.1.6"
},
"workspaces": [
"benchmarks/*",
Expand Down
14 changes: 11 additions & 3 deletions packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"node": ">=16"
},
"imports": {
"#src/config": {
"#src/config-content": {
"import": "./src/config.esm.ts",
"default": "./src/config.cjs.ts"
},
Expand All @@ -61,7 +61,15 @@
"@types/node": ">=16"
},
"dependencies": {
"joi": "^17.9.2"
"etag": "^1.8.1",
"joi": "^17.9.2",
"proxy-addr": "^2.0.7",
"qs": "^6.11.2"
},
"devDependencies": {
"@types/etag": "^1.8.1",
"@types/proxy-addr": "^2.0.0",
"@types/qs": "^6.9.7"
},
"publishConfig": {
"access": "public",
Expand All @@ -82,7 +90,7 @@
"./package.json": "./package.json"
},
"imports": {
"#src/config": {
"#src/config-content": {
"import": {
"types": "./.build/esm/config.esm.d.ts",
"default": "./.build/esm/config.esm.js"
Expand Down
4 changes: 2 additions & 2 deletions packages/config/src/config.cjs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CONFIG_FILEPATH, ConfigI } from "#src/constants";
import { CONFIG_FILEPATH } from "#src/constants/index";

/* ------------------------- Read the config file ------------------------- */

Expand All @@ -14,6 +14,6 @@ try {
resolved = {};
}

const content: ConfigI = resolved.default ?? {};
const content: any = resolved.default ?? null;

export default content;
7 changes: 3 additions & 4 deletions packages/config/src/config.esm.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { CONFIG_FILEPATH, ConfigI } from "#src/constants";
import { CONFIG_FILEPATH } from "#src/constants/index";

/* ------------------------- Read the config file ------------------------- */

// TODO: Just to avoid the CommonJS build issue
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error,@typescript-eslint/ban-ts-comment
// @ts-ignore
const content: ConfigI = await import(CONFIG_FILEPATH)
const content: any = await import(CONFIG_FILEPATH)
.then(resolved => resolved.default)
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
.catch(() => ({}) as never);
.catch(() => null);

export default content;
183 changes: 183 additions & 0 deletions packages/config/src/config/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*
* MIT License
*
* Copyright (c) 2023 FoxifyJS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

import { cpus } from "node:os";
import Joi from "joi";
import content from "#src/config-content";
import { ENV } from "#src/constants/index";
import { Node, type Schema } from "#src/utils/index";
import { JsonConfig, type JsonConfigI } from "./json.config.js";
import { JsonpConfig, type JsonpConfigI } from "./jsonp.config.js";
import { ProxyConfig, type ProxyConfigI } from "./proxy.config.js";
import { QueryConfig, type QueryConfigI } from "./query.config.js";
import { RouterConfig, type RouterConfigI } from "./router.config.js";
import { ServerConfig, type ServerConfigI } from "./server.config.js";
import { SubdomainConfig, type SubdomainConfigI } from "./subdomain.config.js";
import { ViewConfig, type ViewConfigI } from "./view.config.js";

export interface ConfigI {

/**
* Node.js environment.
* @default process.env.NODE_ENV ?? "development"
*/
env?: ENV;

/**
* JSON config.
*/
readonly json?: JsonConfigI;

/**
* JSONP config.
*/
readonly jsonp?: JsonpConfigI;

/**
* Proxy config.
*/
readonly proxy?: ProxyConfigI;

/**
* Request query string config.
*/
readonly query?: QueryConfigI;

/**
* Router config.
*/
readonly router?: RouterConfigI;

/**
* Server config.
*/
readonly server?: ServerConfigI;

/**
* Subdomain config.
*/
readonly subdomain?: SubdomainConfigI;

/**
* View config.
*/
readonly view?: ViewConfigI;

/**
* Number of Node.js cluster workers to be created.
* In case of `1` Node.js cluster workers won't be used.
* @default 1
*/
workers?: number;

/**
* Indicates whether the "X-Powered-By" header should be present or not.
* @default true
*/
xPoweredBy?: boolean;
}

export class Config extends Node {

public static SCHEMA: Schema<Config> = {
env: Joi.string().valid(...Object.values(ENV))
.default(process.env.NODE_ENV as ENV | undefined ?? ENV.DEVELOPMENT),
workers: Joi.number().integer()
.min(1)
.max(cpus().length)
.default(1),
xPoweredBy: Joi.boolean().default(true),
};

/**
* Node.js environment.
* @default process.env.NODE_ENV ?? "development"
*/
public env: ENV;

/**
* JSON config.
*/
public json = new JsonConfig;

/**
* JSONP config.
*/
public jsonp = new JsonpConfig;

/**
* Proxy config.
*/
public proxy = new ProxyConfig;

/**
* Request query string config.
*/
public query = new QueryConfig;

/**
* Router config.
*/
public router = new RouterConfig;

/**
* Server config.
*/
public server = new ServerConfig;

/**
* Subdomain config.
*/
public subdomain = new SubdomainConfig;

/**
* View config.
*/
public view = new ViewConfig;

/**
* Number of Node.js cluster workers to be created.
* In case of `1` Node.js cluster workers won't be used.
* @default 1
*/
public workers: number;

/**
* Indicates whether the "X-Powered-By" header should be present or not.
* @default true
*/
public xPoweredBy: boolean;

public constructor(config: Partial<Config> = content ?? {}) {
super();

const { env, workers, xPoweredBy } = config as Required<Config>;

this.env = env;
this.workers = workers;
this.xPoweredBy = xPoweredBy;
}

}
32 changes: 32 additions & 0 deletions packages/config/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* MIT License
*
* Copyright (c) 2023 FoxifyJS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

import { Config, ConfigI } from "./config.js";

export { type ViewRendererCallbackT, type ViewRendererT } from "./view.config.js";

export type { Config, ConfigI };

export const config = new Config;
Loading