@my-nestjs-kit/app-config 0.0.0-stage → 0.1.0-dev.20261129020438
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +42 -2
- package/dist/adapters/aws-env.adapter.d.ts +8 -0
- package/dist/adapters/aws-env.adapter.js +26 -0
- package/dist/adapters/host-env-adapter.base.d.ts +21 -0
- package/dist/adapters/host-env-adapter.base.js +39 -0
- package/dist/adapters/localhost-env.adapter.d.ts +8 -0
- package/dist/adapters/localhost-env.adapter.js +24 -0
- package/dist/adapters/railway-env.adapter.d.ts +8 -0
- package/dist/adapters/railway-env.adapter.js +24 -0
- package/dist/adapters/vercel-env.adapter.d.ts +9 -0
- package/dist/adapters/vercel-env.adapter.js +31 -0
- package/dist/app-config.types.d.ts +50 -0
- package/dist/app-config.types.js +26 -0
- package/dist/app-state.d.ts +35 -0
- package/dist/app-state.js +141 -0
- package/dist/app-state.module.d.ts +24 -0
- package/dist/app-state.module.js +48 -0
- package/dist/host-urls.d.ts +22 -0
- package/dist/host-urls.js +43 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +32 -0
- package/package.json +85 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 sabationok
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,43 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @my-nestjs-kit/app-config
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
```sh
|
|
4
|
+
npm i @my-nestjs-kit/app-config joi
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
Where the app runs and in which stage, resolved once at boot from config values.
|
|
8
|
+
|
|
9
|
+
## Pieces
|
|
10
|
+
|
|
11
|
+
- **Host adapters** (`HostEnvAdapter`): one per platform, each knows its own env keys. `isMe(env)` detects it, `resolve(env)` returns `HostEnv` { `provider`, `stageName`, `stage`, `publicDomain?`, `privateDomain?`, `replicaId?` }.
|
|
12
|
+
| Adapter | Detected by | `stageName` from | Domains |
|
|
13
|
+
|---|---|---|---|
|
|
14
|
+
| `RailwayEnvAdapter` | `RAILWAY_ENVIRONMENT_NAME` | `RAILWAY_ENVIRONMENT_NAME` | `RAILWAY_PUBLIC_DOMAIN`, `RAILWAY_PRIVATE_DOMAIN`, `RAILWAY_REPLICA_ID` |
|
|
15
|
+
| `VercelEnvAdapter` | `VERCEL=1` or `VERCEL_ENV` | `VERCEL_ENV` (mapped directly) | `VERCEL_PROJECT_PRODUCTION_URL` ?? `VERCEL_URL`, `VERCEL_URL` |
|
|
16
|
+
| `AwsEnvAdapter` | `AWS_EXECUTION_ENV`, `AWS_LAMBDA_FUNCTION_NAME`, `ECS_CONTAINER_METADATA_URI_V4` | `STAGE` | `PUBLIC_DOMAIN`, `PRIVATE_DOMAIN` ?? public |
|
|
17
|
+
| `LocalhostEnvAdapter` (fallback, last) | always | `STAGE` | `PUBLIC_DOMAIN` ?? `localhost`, `PRIVATE_DOMAIN` ?? public |
|
|
18
|
+
- **Stage** (`StageEnum`): `dev | production | preview`. `stageFromName`, exact match after trim + lowercase: `production` → production; `dev` | `development` → dev; empty → dev (local run); anything else (`prod`, `prod-eu`, `preprod`, `staging`, `pr-42`) → preview. An adapter may override `stageOf` (Vercel does). A valid explicit `STAGE` env overrides the derived stage; an unknown value is ignored.
|
|
19
|
+
- **`AppState`**: `{ host, stage, nodeEnv, isDevStage, isPreviewStage, isProdStage, isDevMode, isProdMode, isTestMode }`. Stage (where it is deployed) and NODE_ENV mode (how Node runs) are separate axes: `isDevMode` / `isProdMode` / `isTestMode` read NODE_ENV only. An unknown NODE_ENV counts as development.
|
|
20
|
+
- **Validation**: `appStateSchema` (Joi) checks the enums, `stage === host.stage`, flag consistency and rejects unknown keys. `validateAppState(state, { providers? })` throws and returns the state frozen. `resolveAppState(env, { adapters? })` = build + validate + freeze, accepting the providers of the given adapters.
|
|
21
|
+
- **URLs** (optional): `buildHostUrl(domain, { port?, globalPrefix?, protocol? })` and `buildHostUrls(host, …)` → `{ publicUrl?, privateUrl? }`. https by default; http plus `port` for loopback and private-network hosts (`localhost`, IPs, `*.internal` such as `*.railway.internal`, `*.local`); a scheme already in the domain is kept; an empty domain gives `undefined`.
|
|
22
|
+
|
|
23
|
+
## Use
|
|
24
|
+
|
|
25
|
+
Without DI, in the app's own config service (one resolution, from `ConfigService`, no `process.env` reads, no `registerAs`):
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
this.app = resolveAppState((key) => config.get<string>(key));
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
With DI:
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
AppStateModule.forRootAsync({
|
|
35
|
+
inject: [ConfigService],
|
|
36
|
+
useFactory: (config: ConfigService) => ({ env: (key) => config.get<string>(key) }),
|
|
37
|
+
});
|
|
38
|
+
// constructor(@InjectAppState() private readonly app: Readonly<AppState>) {}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
A new platform: subclass `HostEnvAdapter` and put it in front of the defaults, `adapters: [new RenderEnvAdapter(), ...DEFAULT_HOST_ADAPTERS]`. Its `provider` can be any string; `resolveAppState` accepts it.
|
|
42
|
+
|
|
43
|
+
Peers: `joi` and `@nestjs/common` (only `AppStateModule` uses it). No other kit package.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type EnvReader, HostProviderEnum } from '../app-config.types';
|
|
2
|
+
import { HostEnvAdapter, type HostEnvFields } from './host-env-adapter.base';
|
|
3
|
+
/** AWS (Lambda / ECS / App Runner): detected by its runtime keys; no standard environment name, so STAGE names it. */
|
|
4
|
+
export declare class AwsEnvAdapter extends HostEnvAdapter {
|
|
5
|
+
readonly provider = HostProviderEnum.AWS;
|
|
6
|
+
isMe(env: EnvReader): boolean;
|
|
7
|
+
protected read(env: EnvReader): HostEnvFields;
|
|
8
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AwsEnvAdapter = void 0;
|
|
4
|
+
const app_config_types_1 = require("../app-config.types");
|
|
5
|
+
const host_env_adapter_base_1 = require("./host-env-adapter.base");
|
|
6
|
+
/** AWS (Lambda / ECS / App Runner): detected by its runtime keys; no standard environment name, so STAGE names it. */
|
|
7
|
+
class AwsEnvAdapter extends host_env_adapter_base_1.HostEnvAdapter {
|
|
8
|
+
constructor() {
|
|
9
|
+
super(...arguments);
|
|
10
|
+
this.provider = app_config_types_1.HostProviderEnum.AWS;
|
|
11
|
+
}
|
|
12
|
+
isMe(env) {
|
|
13
|
+
return !!(env('AWS_EXECUTION_ENV') ||
|
|
14
|
+
env('AWS_LAMBDA_FUNCTION_NAME') ||
|
|
15
|
+
env('ECS_CONTAINER_METADATA_URI_V4'));
|
|
16
|
+
}
|
|
17
|
+
read(env) {
|
|
18
|
+
const publicDomain = env('PUBLIC_DOMAIN');
|
|
19
|
+
return {
|
|
20
|
+
stageName: env('STAGE') ?? '',
|
|
21
|
+
publicDomain,
|
|
22
|
+
privateDomain: env('PRIVATE_DOMAIN') ?? publicDomain,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.AwsEnvAdapter = AwsEnvAdapter;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { EnvReader, HostEnv, HostProvider } from '../app-config.types';
|
|
2
|
+
import { StageEnum } from '../app-config.types';
|
|
3
|
+
/**
|
|
4
|
+
* Default rule, exact match after trim + lowercase: `production` → production; `dev` | `development` → dev;
|
|
5
|
+
* empty → dev (local run); anything else (`prod`, `prod-eu`, `preprod`, `staging`, `pr-42`, ...) → preview.
|
|
6
|
+
*/
|
|
7
|
+
export declare function stageFromName(stageName: string | undefined): StageEnum;
|
|
8
|
+
/** Host fields an adapter reads from the env; `provider` and `stage` are filled in by `resolve`. */
|
|
9
|
+
export type HostEnvFields = Omit<HostEnv, 'provider' | 'stage'>;
|
|
10
|
+
/**
|
|
11
|
+
* One hosting platform. It knows its own env keys: whether it is the active host (`isMe`),
|
|
12
|
+
* which value names the environment (`read` → `stageName`) and how that name maps to a stage (`stageOf`).
|
|
13
|
+
* Stateless, so one instance can serve any number of resolutions.
|
|
14
|
+
*/
|
|
15
|
+
export declare abstract class HostEnvAdapter {
|
|
16
|
+
abstract readonly provider: HostProvider;
|
|
17
|
+
abstract isMe(env: EnvReader): boolean;
|
|
18
|
+
protected abstract read(env: EnvReader): HostEnvFields;
|
|
19
|
+
stageOf(stageName: string): StageEnum;
|
|
20
|
+
resolve(env: EnvReader): HostEnv;
|
|
21
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HostEnvAdapter = exports.stageFromName = void 0;
|
|
4
|
+
const app_config_types_1 = require("../app-config.types");
|
|
5
|
+
const STAGE_NAMES = {
|
|
6
|
+
'': app_config_types_1.StageEnum.DEV,
|
|
7
|
+
dev: app_config_types_1.StageEnum.DEV,
|
|
8
|
+
development: app_config_types_1.StageEnum.DEV,
|
|
9
|
+
production: app_config_types_1.StageEnum.PRODUCTION,
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Default rule, exact match after trim + lowercase: `production` → production; `dev` | `development` → dev;
|
|
13
|
+
* empty → dev (local run); anything else (`prod`, `prod-eu`, `preprod`, `staging`, `pr-42`, ...) → preview.
|
|
14
|
+
*/
|
|
15
|
+
function stageFromName(stageName) {
|
|
16
|
+
return (STAGE_NAMES[(stageName ?? '').trim().toLowerCase()] ?? app_config_types_1.StageEnum.PREVIEW);
|
|
17
|
+
}
|
|
18
|
+
exports.stageFromName = stageFromName;
|
|
19
|
+
/**
|
|
20
|
+
* One hosting platform. It knows its own env keys: whether it is the active host (`isMe`),
|
|
21
|
+
* which value names the environment (`read` → `stageName`) and how that name maps to a stage (`stageOf`).
|
|
22
|
+
* Stateless, so one instance can serve any number of resolutions.
|
|
23
|
+
*/
|
|
24
|
+
class HostEnvAdapter {
|
|
25
|
+
stageOf(stageName) {
|
|
26
|
+
return stageFromName(stageName);
|
|
27
|
+
}
|
|
28
|
+
resolve(env) {
|
|
29
|
+
const fields = this.read(env);
|
|
30
|
+
const stageName = fields.stageName ?? '';
|
|
31
|
+
return {
|
|
32
|
+
provider: this.provider,
|
|
33
|
+
...fields,
|
|
34
|
+
stageName,
|
|
35
|
+
stage: this.stageOf(stageName),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.HostEnvAdapter = HostEnvAdapter;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type EnvReader, HostProviderEnum } from '../app-config.types';
|
|
2
|
+
import { HostEnvAdapter, type HostEnvFields } from './host-env-adapter.base';
|
|
3
|
+
/** Fallback: always matches, so it must stay last in the adapter list. STAGE names the environment. */
|
|
4
|
+
export declare class LocalhostEnvAdapter extends HostEnvAdapter {
|
|
5
|
+
readonly provider = HostProviderEnum.LOCALHOST;
|
|
6
|
+
isMe(): boolean;
|
|
7
|
+
protected read(env: EnvReader): HostEnvFields;
|
|
8
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocalhostEnvAdapter = void 0;
|
|
4
|
+
const app_config_types_1 = require("../app-config.types");
|
|
5
|
+
const host_env_adapter_base_1 = require("./host-env-adapter.base");
|
|
6
|
+
/** Fallback: always matches, so it must stay last in the adapter list. STAGE names the environment. */
|
|
7
|
+
class LocalhostEnvAdapter extends host_env_adapter_base_1.HostEnvAdapter {
|
|
8
|
+
constructor() {
|
|
9
|
+
super(...arguments);
|
|
10
|
+
this.provider = app_config_types_1.HostProviderEnum.LOCALHOST;
|
|
11
|
+
}
|
|
12
|
+
isMe() {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
read(env) {
|
|
16
|
+
const publicDomain = env('PUBLIC_DOMAIN') ?? 'localhost';
|
|
17
|
+
return {
|
|
18
|
+
stageName: env('STAGE') ?? '',
|
|
19
|
+
publicDomain,
|
|
20
|
+
privateDomain: env('PRIVATE_DOMAIN') ?? publicDomain,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.LocalhostEnvAdapter = LocalhostEnvAdapter;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type EnvReader, HostProviderEnum } from '../app-config.types';
|
|
2
|
+
import { HostEnvAdapter, type HostEnvFields } from './host-env-adapter.base';
|
|
3
|
+
/** Railway: RAILWAY_ENVIRONMENT_NAME names the environment; domains and replica come from its injected vars. */
|
|
4
|
+
export declare class RailwayEnvAdapter extends HostEnvAdapter {
|
|
5
|
+
readonly provider = HostProviderEnum.RAILWAY;
|
|
6
|
+
isMe(env: EnvReader): boolean;
|
|
7
|
+
protected read(env: EnvReader): HostEnvFields;
|
|
8
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RailwayEnvAdapter = void 0;
|
|
4
|
+
const app_config_types_1 = require("../app-config.types");
|
|
5
|
+
const host_env_adapter_base_1 = require("./host-env-adapter.base");
|
|
6
|
+
/** Railway: RAILWAY_ENVIRONMENT_NAME names the environment; domains and replica come from its injected vars. */
|
|
7
|
+
class RailwayEnvAdapter extends host_env_adapter_base_1.HostEnvAdapter {
|
|
8
|
+
constructor() {
|
|
9
|
+
super(...arguments);
|
|
10
|
+
this.provider = app_config_types_1.HostProviderEnum.RAILWAY;
|
|
11
|
+
}
|
|
12
|
+
isMe(env) {
|
|
13
|
+
return !!env('RAILWAY_ENVIRONMENT_NAME');
|
|
14
|
+
}
|
|
15
|
+
read(env) {
|
|
16
|
+
return {
|
|
17
|
+
stageName: env('RAILWAY_ENVIRONMENT_NAME') ?? '',
|
|
18
|
+
publicDomain: env('RAILWAY_PUBLIC_DOMAIN'),
|
|
19
|
+
privateDomain: env('RAILWAY_PRIVATE_DOMAIN'),
|
|
20
|
+
replicaId: env('RAILWAY_REPLICA_ID'),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.RailwayEnvAdapter = RailwayEnvAdapter;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type EnvReader, HostProviderEnum, StageEnum } from '../app-config.types';
|
|
2
|
+
import { HostEnvAdapter, type HostEnvFields } from './host-env-adapter.base';
|
|
3
|
+
/** Vercel: VERCEL_ENV is already a stage name (production | preview | development). */
|
|
4
|
+
export declare class VercelEnvAdapter extends HostEnvAdapter {
|
|
5
|
+
readonly provider = HostProviderEnum.VERCEL;
|
|
6
|
+
isMe(env: EnvReader): boolean;
|
|
7
|
+
stageOf(stageName: string): StageEnum;
|
|
8
|
+
protected read(env: EnvReader): HostEnvFields;
|
|
9
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VercelEnvAdapter = void 0;
|
|
4
|
+
const app_config_types_1 = require("../app-config.types");
|
|
5
|
+
const host_env_adapter_base_1 = require("./host-env-adapter.base");
|
|
6
|
+
const VERCEL_STAGES = {
|
|
7
|
+
production: app_config_types_1.StageEnum.PRODUCTION,
|
|
8
|
+
preview: app_config_types_1.StageEnum.PREVIEW,
|
|
9
|
+
development: app_config_types_1.StageEnum.DEV,
|
|
10
|
+
};
|
|
11
|
+
/** Vercel: VERCEL_ENV is already a stage name (production | preview | development). */
|
|
12
|
+
class VercelEnvAdapter extends host_env_adapter_base_1.HostEnvAdapter {
|
|
13
|
+
constructor() {
|
|
14
|
+
super(...arguments);
|
|
15
|
+
this.provider = app_config_types_1.HostProviderEnum.VERCEL;
|
|
16
|
+
}
|
|
17
|
+
isMe(env) {
|
|
18
|
+
return env('VERCEL') === '1' || !!env('VERCEL_ENV');
|
|
19
|
+
}
|
|
20
|
+
stageOf(stageName) {
|
|
21
|
+
return (VERCEL_STAGES[stageName.trim().toLowerCase()] ?? super.stageOf(stageName));
|
|
22
|
+
}
|
|
23
|
+
read(env) {
|
|
24
|
+
return {
|
|
25
|
+
stageName: env('VERCEL_ENV') ?? '',
|
|
26
|
+
publicDomain: env('VERCEL_PROJECT_PRODUCTION_URL') ?? env('VERCEL_URL'),
|
|
27
|
+
privateDomain: env('VERCEL_URL'),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.VercelEnvAdapter = VercelEnvAdapter;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/** Where the app is deployed. Decides what it shows and exposes: dev and preview may show more than production. */
|
|
2
|
+
export declare enum StageEnum {
|
|
3
|
+
DEV = "dev",
|
|
4
|
+
PRODUCTION = "production",
|
|
5
|
+
/** Anything that is neither dev nor production: PR environments, staging, QA. */
|
|
6
|
+
PREVIEW = "preview"
|
|
7
|
+
}
|
|
8
|
+
/** How the Node process runs (NODE_ENV). Independent of the stage: a dev stage can run a production build. */
|
|
9
|
+
export declare enum NodeEnvEnum {
|
|
10
|
+
DEVELOPMENT = "development",
|
|
11
|
+
PRODUCTION = "production",
|
|
12
|
+
TEST = "test"
|
|
13
|
+
}
|
|
14
|
+
/** The hosting platforms the kit ships adapters for. */
|
|
15
|
+
export declare enum HostProviderEnum {
|
|
16
|
+
RAILWAY = "railway",
|
|
17
|
+
VERCEL = "vercel",
|
|
18
|
+
AWS = "aws",
|
|
19
|
+
LOCALHOST = "localhost"
|
|
20
|
+
}
|
|
21
|
+
/** A kit provider, or the name of a platform the app adds with its own adapter. */
|
|
22
|
+
export type HostProvider = HostProviderEnum | (string & {});
|
|
23
|
+
/** Reads one raw env value: `(key) => configService.get(key)` in an app, a plain record in tests. */
|
|
24
|
+
export type EnvReader = (key: string) => string | undefined;
|
|
25
|
+
/** What the active host adapter returns: the host and its stage, already resolved. */
|
|
26
|
+
export interface HostEnv {
|
|
27
|
+
provider: HostProvider;
|
|
28
|
+
/** The host's own environment name (RAILWAY_ENVIRONMENT_NAME, VERCEL_ENV, STAGE, ...). */
|
|
29
|
+
stageName: string;
|
|
30
|
+
stage: StageEnum;
|
|
31
|
+
publicDomain?: string;
|
|
32
|
+
privateDomain?: string;
|
|
33
|
+
replicaId?: string;
|
|
34
|
+
}
|
|
35
|
+
/** The app's state, resolved once at boot: host + stage and the NODE_ENV mode as separate axes. */
|
|
36
|
+
export interface AppState {
|
|
37
|
+
host: HostEnv;
|
|
38
|
+
/** Always equals `host.stage`. */
|
|
39
|
+
stage: StageEnum;
|
|
40
|
+
nodeEnv: NodeEnvEnum;
|
|
41
|
+
isDevStage: boolean;
|
|
42
|
+
isPreviewStage: boolean;
|
|
43
|
+
isProdStage: boolean;
|
|
44
|
+
/** NODE_ENV=development */
|
|
45
|
+
isDevMode: boolean;
|
|
46
|
+
/** NODE_ENV=production */
|
|
47
|
+
isProdMode: boolean;
|
|
48
|
+
/** NODE_ENV=test */
|
|
49
|
+
isTestMode: boolean;
|
|
50
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HostProviderEnum = exports.NodeEnvEnum = exports.StageEnum = void 0;
|
|
4
|
+
/** Where the app is deployed. Decides what it shows and exposes: dev and preview may show more than production. */
|
|
5
|
+
var StageEnum;
|
|
6
|
+
(function (StageEnum) {
|
|
7
|
+
StageEnum["DEV"] = "dev";
|
|
8
|
+
StageEnum["PRODUCTION"] = "production";
|
|
9
|
+
/** Anything that is neither dev nor production: PR environments, staging, QA. */
|
|
10
|
+
StageEnum["PREVIEW"] = "preview";
|
|
11
|
+
})(StageEnum || (exports.StageEnum = StageEnum = {}));
|
|
12
|
+
/** How the Node process runs (NODE_ENV). Independent of the stage: a dev stage can run a production build. */
|
|
13
|
+
var NodeEnvEnum;
|
|
14
|
+
(function (NodeEnvEnum) {
|
|
15
|
+
NodeEnvEnum["DEVELOPMENT"] = "development";
|
|
16
|
+
NodeEnvEnum["PRODUCTION"] = "production";
|
|
17
|
+
NodeEnvEnum["TEST"] = "test";
|
|
18
|
+
})(NodeEnvEnum || (exports.NodeEnvEnum = NodeEnvEnum = {}));
|
|
19
|
+
/** The hosting platforms the kit ships adapters for. */
|
|
20
|
+
var HostProviderEnum;
|
|
21
|
+
(function (HostProviderEnum) {
|
|
22
|
+
HostProviderEnum["RAILWAY"] = "railway";
|
|
23
|
+
HostProviderEnum["VERCEL"] = "vercel";
|
|
24
|
+
HostProviderEnum["AWS"] = "aws";
|
|
25
|
+
HostProviderEnum["LOCALHOST"] = "localhost";
|
|
26
|
+
})(HostProviderEnum || (exports.HostProviderEnum = HostProviderEnum = {}));
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as Joi from 'joi';
|
|
2
|
+
import { type AppState, type EnvReader, type HostEnv, type HostProvider } from './app-config.types';
|
|
3
|
+
import { HostEnvAdapter } from './adapters/host-env-adapter.base';
|
|
4
|
+
/**
|
|
5
|
+
* Checked in order: the first adapter whose `isMe()` is true wins, localhost is the fallback.
|
|
6
|
+
* Add a platform by putting its adapter in front: `[new RenderEnvAdapter(), ...DEFAULT_HOST_ADAPTERS]`.
|
|
7
|
+
*/
|
|
8
|
+
export declare const DEFAULT_HOST_ADAPTERS: readonly HostEnvAdapter[];
|
|
9
|
+
export interface ResolveHostEnvOptions {
|
|
10
|
+
/** Replaces the default list; keep a catch-all (localhost) last. Default `DEFAULT_HOST_ADAPTERS`. */
|
|
11
|
+
adapters?: readonly HostEnvAdapter[];
|
|
12
|
+
}
|
|
13
|
+
export interface AppStateOptions extends ResolveHostEnvOptions {
|
|
14
|
+
}
|
|
15
|
+
export interface ValidateAppStateOptions {
|
|
16
|
+
/** Accepted `host.provider` values. Default: the `HostProviderEnum` values. */
|
|
17
|
+
providers?: readonly HostProvider[];
|
|
18
|
+
}
|
|
19
|
+
export interface ResolveAppStateOptions extends AppStateOptions {
|
|
20
|
+
}
|
|
21
|
+
/** The active host and its stage. An explicit, valid `STAGE` overrides the stage the adapter derived. */
|
|
22
|
+
export declare function resolveHostEnv(env: EnvReader, options?: ResolveHostEnvOptions): HostEnv;
|
|
23
|
+
/** Builds the state without checking it; `resolveAppState` builds, validates and freezes. */
|
|
24
|
+
export declare function buildAppState(env: EnvReader, options?: AppStateOptions): AppState;
|
|
25
|
+
/** Joi schema of `AppState`: enums, `stage === host.stage`, consistent flags, no unknown keys. */
|
|
26
|
+
export declare function buildAppStateSchema(providers?: readonly HostProvider[]): Joi.ObjectSchema<AppState>;
|
|
27
|
+
/** The schema for the kit's own providers. */
|
|
28
|
+
export declare const appStateSchema: Joi.ObjectSchema<AppState>;
|
|
29
|
+
/** Throws on an inconsistent state, so a broken config fails the boot instead of running half-right. */
|
|
30
|
+
export declare function validateAppState(state: AppState, options?: ValidateAppStateOptions): Readonly<AppState>;
|
|
31
|
+
/**
|
|
32
|
+
* Build + validate + freeze in one call. Accepts every kit provider plus those of the given adapters.
|
|
33
|
+
* Call it once at boot, from ConfigService values: `resolveAppState((key) => config.get(key))`.
|
|
34
|
+
*/
|
|
35
|
+
export declare function resolveAppState(env: EnvReader, options?: ResolveAppStateOptions): Readonly<AppState>;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.resolveAppState = exports.validateAppState = exports.appStateSchema = exports.buildAppStateSchema = exports.buildAppState = exports.resolveHostEnv = exports.DEFAULT_HOST_ADAPTERS = void 0;
|
|
27
|
+
const Joi = __importStar(require("joi"));
|
|
28
|
+
const app_config_types_1 = require("./app-config.types");
|
|
29
|
+
const railway_env_adapter_1 = require("./adapters/railway-env.adapter");
|
|
30
|
+
const vercel_env_adapter_1 = require("./adapters/vercel-env.adapter");
|
|
31
|
+
const aws_env_adapter_1 = require("./adapters/aws-env.adapter");
|
|
32
|
+
const localhost_env_adapter_1 = require("./adapters/localhost-env.adapter");
|
|
33
|
+
const STAGES = Object.values(app_config_types_1.StageEnum);
|
|
34
|
+
const NODE_ENVS = Object.values(app_config_types_1.NodeEnvEnum);
|
|
35
|
+
/**
|
|
36
|
+
* Checked in order: the first adapter whose `isMe()` is true wins, localhost is the fallback.
|
|
37
|
+
* Add a platform by putting its adapter in front: `[new RenderEnvAdapter(), ...DEFAULT_HOST_ADAPTERS]`.
|
|
38
|
+
*/
|
|
39
|
+
exports.DEFAULT_HOST_ADAPTERS = Object.freeze([
|
|
40
|
+
new railway_env_adapter_1.RailwayEnvAdapter(),
|
|
41
|
+
new vercel_env_adapter_1.VercelEnvAdapter(),
|
|
42
|
+
new aws_env_adapter_1.AwsEnvAdapter(),
|
|
43
|
+
new localhost_env_adapter_1.LocalhostEnvAdapter(),
|
|
44
|
+
]);
|
|
45
|
+
/** The active host and its stage. An explicit, valid `STAGE` overrides the stage the adapter derived. */
|
|
46
|
+
function resolveHostEnv(env, options = {}) {
|
|
47
|
+
const adapters = options.adapters ?? exports.DEFAULT_HOST_ADAPTERS;
|
|
48
|
+
const adapter = adapters.find((a) => a.isMe(env)) ?? new localhost_env_adapter_1.LocalhostEnvAdapter();
|
|
49
|
+
const host = adapter.resolve(env);
|
|
50
|
+
const explicit = env('STAGE')?.trim();
|
|
51
|
+
return explicit && STAGES.includes(explicit)
|
|
52
|
+
? { ...host, stage: explicit }
|
|
53
|
+
: host;
|
|
54
|
+
}
|
|
55
|
+
exports.resolveHostEnv = resolveHostEnv;
|
|
56
|
+
/** Builds the state without checking it; `resolveAppState` builds, validates and freezes. */
|
|
57
|
+
function buildAppState(env, options = {}) {
|
|
58
|
+
const host = resolveHostEnv(env, options);
|
|
59
|
+
const rawNodeEnv = env('NODE_ENV')?.trim();
|
|
60
|
+
const nodeEnv = rawNodeEnv && NODE_ENVS.includes(rawNodeEnv)
|
|
61
|
+
? rawNodeEnv
|
|
62
|
+
: app_config_types_1.NodeEnvEnum.DEVELOPMENT;
|
|
63
|
+
const { stage } = host;
|
|
64
|
+
return {
|
|
65
|
+
host,
|
|
66
|
+
stage,
|
|
67
|
+
nodeEnv,
|
|
68
|
+
isDevStage: stage === app_config_types_1.StageEnum.DEV,
|
|
69
|
+
isPreviewStage: stage === app_config_types_1.StageEnum.PREVIEW,
|
|
70
|
+
isProdStage: stage === app_config_types_1.StageEnum.PRODUCTION,
|
|
71
|
+
isDevMode: nodeEnv === app_config_types_1.NodeEnvEnum.DEVELOPMENT,
|
|
72
|
+
isProdMode: nodeEnv === app_config_types_1.NodeEnvEnum.PRODUCTION,
|
|
73
|
+
isTestMode: nodeEnv === app_config_types_1.NodeEnvEnum.TEST,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
exports.buildAppState = buildAppState;
|
|
77
|
+
/** A flag that must equal `state[key] === value`, so the shortcuts stay consistent with stage / nodeEnv. */
|
|
78
|
+
const flag = (key, value) => Joi.boolean()
|
|
79
|
+
.required()
|
|
80
|
+
.when(key, {
|
|
81
|
+
is: value,
|
|
82
|
+
then: Joi.valid(true),
|
|
83
|
+
otherwise: Joi.valid(false),
|
|
84
|
+
});
|
|
85
|
+
/** Joi schema of `AppState`: enums, `stage === host.stage`, consistent flags, no unknown keys. */
|
|
86
|
+
function buildAppStateSchema(providers = Object.values(app_config_types_1.HostProviderEnum)) {
|
|
87
|
+
return Joi.object({
|
|
88
|
+
host: Joi.object({
|
|
89
|
+
provider: Joi.string()
|
|
90
|
+
.valid(...providers)
|
|
91
|
+
.required(),
|
|
92
|
+
stageName: Joi.string().allow('').required(),
|
|
93
|
+
stage: Joi.string()
|
|
94
|
+
.valid(...STAGES)
|
|
95
|
+
.required(),
|
|
96
|
+
publicDomain: Joi.string().allow('').optional(),
|
|
97
|
+
privateDomain: Joi.string().allow('').optional(),
|
|
98
|
+
replicaId: Joi.string().allow('').optional(),
|
|
99
|
+
}).required(),
|
|
100
|
+
// must equal host.stage, which is already checked against StageEnum
|
|
101
|
+
stage: Joi.string().valid(Joi.ref('host.stage')).required(),
|
|
102
|
+
nodeEnv: Joi.string()
|
|
103
|
+
.valid(...NODE_ENVS)
|
|
104
|
+
.required(),
|
|
105
|
+
isDevStage: flag('stage', app_config_types_1.StageEnum.DEV),
|
|
106
|
+
isPreviewStage: flag('stage', app_config_types_1.StageEnum.PREVIEW),
|
|
107
|
+
isProdStage: flag('stage', app_config_types_1.StageEnum.PRODUCTION),
|
|
108
|
+
isDevMode: flag('nodeEnv', app_config_types_1.NodeEnvEnum.DEVELOPMENT),
|
|
109
|
+
isProdMode: flag('nodeEnv', app_config_types_1.NodeEnvEnum.PRODUCTION),
|
|
110
|
+
isTestMode: flag('nodeEnv', app_config_types_1.NodeEnvEnum.TEST),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
exports.buildAppStateSchema = buildAppStateSchema;
|
|
114
|
+
/** The schema for the kit's own providers. */
|
|
115
|
+
exports.appStateSchema = buildAppStateSchema();
|
|
116
|
+
/** Throws on an inconsistent state, so a broken config fails the boot instead of running half-right. */
|
|
117
|
+
function validateAppState(state, options = {}) {
|
|
118
|
+
const schema = options.providers
|
|
119
|
+
? buildAppStateSchema(options.providers)
|
|
120
|
+
: exports.appStateSchema;
|
|
121
|
+
const { error, value } = schema.validate(state, {
|
|
122
|
+
abortEarly: false,
|
|
123
|
+
convert: false,
|
|
124
|
+
});
|
|
125
|
+
if (error)
|
|
126
|
+
throw new Error(`Invalid app state: ${error.message}`);
|
|
127
|
+
return Object.freeze({ ...value, host: Object.freeze({ ...value.host }) });
|
|
128
|
+
}
|
|
129
|
+
exports.validateAppState = validateAppState;
|
|
130
|
+
/**
|
|
131
|
+
* Build + validate + freeze in one call. Accepts every kit provider plus those of the given adapters.
|
|
132
|
+
* Call it once at boot, from ConfigService values: `resolveAppState((key) => config.get(key))`.
|
|
133
|
+
*/
|
|
134
|
+
function resolveAppState(env, options = {}) {
|
|
135
|
+
const providers = new Set(Object.values(app_config_types_1.HostProviderEnum));
|
|
136
|
+
(options.adapters ?? []).forEach((a) => providers.add(a.provider));
|
|
137
|
+
return validateAppState(buildAppState(env, options), {
|
|
138
|
+
providers: [...providers],
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
exports.resolveAppState = resolveAppState;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type DynamicModule } from '@nestjs/common';
|
|
2
|
+
import type { EnvReader } from './app-config.types';
|
|
3
|
+
import { type ResolveAppStateOptions } from './app-state';
|
|
4
|
+
/** DI token of the resolved, validated, frozen `AppState`. */
|
|
5
|
+
export declare const APP_STATE: unique symbol;
|
|
6
|
+
export interface AppStateModuleOptions extends ResolveAppStateOptions {
|
|
7
|
+
/** Raw env reader, normally `(key) => configService.get(key)`. */
|
|
8
|
+
env: EnvReader;
|
|
9
|
+
}
|
|
10
|
+
export interface AppStateModuleAsyncOptions {
|
|
11
|
+
imports?: any[];
|
|
12
|
+
inject?: any[];
|
|
13
|
+
useFactory: (...args: any[]) => AppStateModuleOptions | Promise<AppStateModuleOptions>;
|
|
14
|
+
}
|
|
15
|
+
/** `@InjectAppState() private readonly app: Readonly<AppState>` */
|
|
16
|
+
export declare const InjectAppState: () => PropertyDecorator & ParameterDecorator;
|
|
17
|
+
/**
|
|
18
|
+
* Optional DI wrapper: resolves the state once at boot (an invalid state fails the boot) and provides it
|
|
19
|
+
* globally under `APP_STATE`. The pure functions work without it, e.g. in an app's own config service.
|
|
20
|
+
*/
|
|
21
|
+
export declare class AppStateModule {
|
|
22
|
+
static forRoot(options: AppStateModuleOptions): DynamicModule;
|
|
23
|
+
static forRootAsync(options: AppStateModuleAsyncOptions): DynamicModule;
|
|
24
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var AppStateModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.AppStateModule = exports.InjectAppState = exports.APP_STATE = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const app_state_1 = require("./app-state");
|
|
13
|
+
/** DI token of the resolved, validated, frozen `AppState`. */
|
|
14
|
+
exports.APP_STATE = Symbol('APP_STATE');
|
|
15
|
+
/** `@InjectAppState() private readonly app: Readonly<AppState>` */
|
|
16
|
+
const InjectAppState = () => (0, common_1.Inject)(exports.APP_STATE);
|
|
17
|
+
exports.InjectAppState = InjectAppState;
|
|
18
|
+
/**
|
|
19
|
+
* Optional DI wrapper: resolves the state once at boot (an invalid state fails the boot) and provides it
|
|
20
|
+
* globally under `APP_STATE`. The pure functions work without it, e.g. in an app's own config service.
|
|
21
|
+
*/
|
|
22
|
+
let AppStateModule = AppStateModule_1 = class AppStateModule {
|
|
23
|
+
static forRoot(options) {
|
|
24
|
+
return AppStateModule_1.forRootAsync({ useFactory: () => options });
|
|
25
|
+
}
|
|
26
|
+
static forRootAsync(options) {
|
|
27
|
+
return {
|
|
28
|
+
module: AppStateModule_1,
|
|
29
|
+
global: true,
|
|
30
|
+
imports: options.imports ?? [],
|
|
31
|
+
providers: [
|
|
32
|
+
{
|
|
33
|
+
provide: exports.APP_STATE,
|
|
34
|
+
useFactory: async (...args) => {
|
|
35
|
+
const { env, ...rest } = await options.useFactory(...args);
|
|
36
|
+
return (0, app_state_1.resolveAppState)(env, rest);
|
|
37
|
+
},
|
|
38
|
+
inject: options.inject ?? [],
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
exports: [exports.APP_STATE],
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
exports.AppStateModule = AppStateModule;
|
|
46
|
+
exports.AppStateModule = AppStateModule = AppStateModule_1 = __decorate([
|
|
47
|
+
(0, common_1.Module)({})
|
|
48
|
+
], AppStateModule);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { HostEnv } from './app-config.types';
|
|
2
|
+
export interface HostUrlOptions {
|
|
3
|
+
/** Appended to a plain-http host (localhost, `*.internal`, `*.local`, an IP) that has no port of its own. */
|
|
4
|
+
port?: number | string;
|
|
5
|
+
/** `'api'`, `'/api'` or `'/api/'` → `/api`. */
|
|
6
|
+
globalPrefix?: string;
|
|
7
|
+
/** Overrides the protocol picked from the domain. */
|
|
8
|
+
protocol?: 'http' | 'https';
|
|
9
|
+
}
|
|
10
|
+
export interface HostUrls {
|
|
11
|
+
publicUrl?: string;
|
|
12
|
+
privateUrl?: string;
|
|
13
|
+
}
|
|
14
|
+
/** Hosts reached over plain http: loopback, private networking (Railway `*.railway.internal`), mDNS. */
|
|
15
|
+
export declare function isPlainHttpHost(hostname: string): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* `domain` → absolute URL. Keeps a scheme the domain already has; otherwise https, or http for a plain-http host
|
|
18
|
+
* (plus `port`, when the domain has none). Returns undefined for an empty domain.
|
|
19
|
+
*/
|
|
20
|
+
export declare function buildHostUrl(domain: string | undefined, options?: HostUrlOptions): string | undefined;
|
|
21
|
+
/** The public and private URL of the host (erp `publicUrl` / `privateUrl`). */
|
|
22
|
+
export declare function buildHostUrls(host: Pick<HostEnv, 'publicDomain' | 'privateDomain'>, options?: HostUrlOptions): HostUrls;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildHostUrls = exports.buildHostUrl = exports.isPlainHttpHost = void 0;
|
|
4
|
+
const PLAIN_HTTP_HOST = /^(localhost|127(\.\d{1,3}){3}|0\.0\.0\.0|\[::1\]|.+\.(localhost|internal|local))$/i;
|
|
5
|
+
/** Hosts reached over plain http: loopback, private networking (Railway `*.railway.internal`), mDNS. */
|
|
6
|
+
function isPlainHttpHost(hostname) {
|
|
7
|
+
return PLAIN_HTTP_HOST.test(hostname.trim());
|
|
8
|
+
}
|
|
9
|
+
exports.isPlainHttpHost = isPlainHttpHost;
|
|
10
|
+
/**
|
|
11
|
+
* `domain` → absolute URL. Keeps a scheme the domain already has; otherwise https, or http for a plain-http host
|
|
12
|
+
* (plus `port`, when the domain has none). Returns undefined for an empty domain.
|
|
13
|
+
*/
|
|
14
|
+
function buildHostUrl(domain, options = {}) {
|
|
15
|
+
let rest = domain?.trim().replace(/\/+$/, '');
|
|
16
|
+
if (!rest)
|
|
17
|
+
return undefined;
|
|
18
|
+
let protocol = options.protocol;
|
|
19
|
+
const scheme = /^(https?):\/\//i.exec(rest);
|
|
20
|
+
if (scheme) {
|
|
21
|
+
protocol ??= scheme[1].toLowerCase();
|
|
22
|
+
rest = rest.slice(scheme[0].length);
|
|
23
|
+
}
|
|
24
|
+
const [authority, ...path] = rest.split('/');
|
|
25
|
+
const hasPort = /:\d+$/.test(authority);
|
|
26
|
+
const hostname = hasPort ? authority.replace(/:\d+$/, '') : authority;
|
|
27
|
+
const plainHttp = isPlainHttpHost(hostname);
|
|
28
|
+
protocol ??= plainHttp ? 'http' : 'https';
|
|
29
|
+
const port = !hasPort && plainHttp && options.port ? `:${options.port}` : '';
|
|
30
|
+
const prefix = (options.globalPrefix ?? '').trim().replace(/^\/+|\/+$/g, '');
|
|
31
|
+
return [`${protocol}://${authority}${port}`, ...path, prefix]
|
|
32
|
+
.filter(Boolean)
|
|
33
|
+
.join('/');
|
|
34
|
+
}
|
|
35
|
+
exports.buildHostUrl = buildHostUrl;
|
|
36
|
+
/** The public and private URL of the host (erp `publicUrl` / `privateUrl`). */
|
|
37
|
+
function buildHostUrls(host, options = {}) {
|
|
38
|
+
return {
|
|
39
|
+
publicUrl: buildHostUrl(host.publicDomain, options),
|
|
40
|
+
privateUrl: buildHostUrl(host.privateDomain, options),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
exports.buildHostUrls = buildHostUrls;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './app-config.types';
|
|
2
|
+
export * from './app-state';
|
|
3
|
+
export * from './host-urls';
|
|
4
|
+
export * from './app-state.module';
|
|
5
|
+
export { HostEnvAdapter, stageFromName, type HostEnvFields, } from './adapters/host-env-adapter.base';
|
|
6
|
+
export { RailwayEnvAdapter } from './adapters/railway-env.adapter';
|
|
7
|
+
export { VercelEnvAdapter } from './adapters/vercel-env.adapter';
|
|
8
|
+
export { AwsEnvAdapter } from './adapters/aws-env.adapter';
|
|
9
|
+
export { LocalhostEnvAdapter } from './adapters/localhost-env.adapter';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.LocalhostEnvAdapter = exports.AwsEnvAdapter = exports.VercelEnvAdapter = exports.RailwayEnvAdapter = exports.stageFromName = exports.HostEnvAdapter = void 0;
|
|
18
|
+
__exportStar(require("./app-config.types"), exports);
|
|
19
|
+
__exportStar(require("./app-state"), exports);
|
|
20
|
+
__exportStar(require("./host-urls"), exports);
|
|
21
|
+
__exportStar(require("./app-state.module"), exports);
|
|
22
|
+
var host_env_adapter_base_1 = require("./adapters/host-env-adapter.base");
|
|
23
|
+
Object.defineProperty(exports, "HostEnvAdapter", { enumerable: true, get: function () { return host_env_adapter_base_1.HostEnvAdapter; } });
|
|
24
|
+
Object.defineProperty(exports, "stageFromName", { enumerable: true, get: function () { return host_env_adapter_base_1.stageFromName; } });
|
|
25
|
+
var railway_env_adapter_1 = require("./adapters/railway-env.adapter");
|
|
26
|
+
Object.defineProperty(exports, "RailwayEnvAdapter", { enumerable: true, get: function () { return railway_env_adapter_1.RailwayEnvAdapter; } });
|
|
27
|
+
var vercel_env_adapter_1 = require("./adapters/vercel-env.adapter");
|
|
28
|
+
Object.defineProperty(exports, "VercelEnvAdapter", { enumerable: true, get: function () { return vercel_env_adapter_1.VercelEnvAdapter; } });
|
|
29
|
+
var aws_env_adapter_1 = require("./adapters/aws-env.adapter");
|
|
30
|
+
Object.defineProperty(exports, "AwsEnvAdapter", { enumerable: true, get: function () { return aws_env_adapter_1.AwsEnvAdapter; } });
|
|
31
|
+
var localhost_env_adapter_1 = require("./adapters/localhost-env.adapter");
|
|
32
|
+
Object.defineProperty(exports, "LocalhostEnvAdapter", { enumerable: true, get: function () { return localhost_env_adapter_1.LocalhostEnvAdapter; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,88 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@my-nestjs-kit/app-config",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0-dev.20261129020438",
|
|
4
|
+
"description": "App state for NestJS: host-env adapters (Railway, Vercel, AWS, localhost) and the stage + NODE_ENV state, resolved once and Joi-validated.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"nestjs",
|
|
7
|
+
"nest",
|
|
8
|
+
"config",
|
|
9
|
+
"env",
|
|
10
|
+
"stage",
|
|
11
|
+
"railway",
|
|
12
|
+
"vercel"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"homepage": "https://github.com/sabationok/my-nestjs-kit/tree/main/packages/app-config#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/sabationok/my-nestjs-kit/issues"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/sabationok/my-nestjs-kit.git",
|
|
22
|
+
"directory": "packages/app-config"
|
|
23
|
+
},
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=18"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"main": "dist/index.js",
|
|
31
|
+
"types": "dist/index.d.ts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"require": "./dist/index.js",
|
|
36
|
+
"default": "./dist/index.js"
|
|
37
|
+
},
|
|
38
|
+
"./package.json": "./package.json"
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"dist",
|
|
42
|
+
"!dist/**/*.spec.*",
|
|
43
|
+
"!dist/**/*.tsbuildinfo",
|
|
44
|
+
"README.md",
|
|
45
|
+
"LICENSE"
|
|
46
|
+
],
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@nestjs/common": "^10.4.1",
|
|
49
|
+
"joi": "^17.13.0"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@nestjs/common": "10.4.20",
|
|
53
|
+
"@nestjs/core": "10.4.1",
|
|
54
|
+
"@nestjs/testing": "10.4.1",
|
|
55
|
+
"@types/jest": "29.5.12",
|
|
56
|
+
"@types/node": "20.11.28",
|
|
57
|
+
"jest": "29.7.0",
|
|
58
|
+
"joi": "17.13.3",
|
|
59
|
+
"reflect-metadata": "0.2.2",
|
|
60
|
+
"rxjs": "7.8.1",
|
|
61
|
+
"ts-jest": "29.1.2",
|
|
62
|
+
"typescript": "5.4.2"
|
|
63
|
+
},
|
|
64
|
+
"jest": {
|
|
65
|
+
"moduleFileExtensions": [
|
|
66
|
+
"js",
|
|
67
|
+
"json",
|
|
68
|
+
"ts"
|
|
69
|
+
],
|
|
70
|
+
"rootDir": "src",
|
|
71
|
+
"testRegex": ".*\\.spec\\.ts$",
|
|
72
|
+
"transform": {
|
|
73
|
+
"^.+\\.ts$": [
|
|
74
|
+
"ts-jest",
|
|
75
|
+
{
|
|
76
|
+
"tsconfig": "<rootDir>/../tsconfig.spec.json"
|
|
77
|
+
}
|
|
78
|
+
]
|
|
79
|
+
},
|
|
80
|
+
"testEnvironment": "node"
|
|
81
|
+
},
|
|
82
|
+
"scripts": {
|
|
83
|
+
"build": "rm -rf dist && tsc -p tsconfig.json",
|
|
84
|
+
"build:watch": "tsc -p tsconfig.json --watch",
|
|
85
|
+
"typecheck": "tsc -p tsconfig.spec.json",
|
|
86
|
+
"test": "jest"
|
|
87
|
+
}
|
|
6
88
|
}
|