-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
93 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { UserInputConfig } from 'c12' | ||
import { Configuration } from '@nailyjs/config' | ||
import { Service } from '@nailyjs/ioc' | ||
|
||
@Service(Configuration) | ||
export class CustomConfigurationService implements Configuration { | ||
configure(defaultConfiguration: UserInputConfig): UserInputConfig | Promise<UserInputConfig> { | ||
return defaultConfiguration | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import { NodeBootstrap } from '@nailyjs/backend/node-adapter' | ||
import { Configuration } from '@nailyjs/config' | ||
import { ConfigurationPlugin } from '@nailyjs/config' | ||
import './custom-configuration.service' | ||
import './test.controller' | ||
import './test.filter' | ||
|
||
new NodeBootstrap() | ||
.use(Configuration()) | ||
.use(ConfigurationPlugin()) | ||
.then(bootstrap => bootstrap.run(3000)) | ||
.then(() => console.log(`Backend started on port http://localhost:3000`)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { ResolvedConfig, UserInputConfig } from 'c12' | ||
import { Container, Injectable } from '@nailyjs/ioc' | ||
// eslint-disable-next-line ts/consistent-type-imports | ||
import { JexlExecutor } from '@nailyjs/jexl' | ||
import { loadConfig } from 'c12' | ||
import { Configuration as ConfigurationSymbol } from './plugin-protocol' | ||
|
||
@Injectable() | ||
export class ConfigProvider { | ||
constructor(private readonly jexlExecutor: JexlExecutor) {} | ||
|
||
private getDefaultConfiguration(): UserInputConfig { | ||
return { | ||
name: 'naily', | ||
} | ||
} | ||
|
||
private c12InstanceCache: ResolvedConfig | undefined | ||
private async getC12Instance(config: UserInputConfig = this.getDefaultConfiguration()): ReturnType<typeof loadConfig> { | ||
if (this.c12InstanceCache) return this.c12InstanceCache | ||
const c12Instance = await loadConfig(config || this.getDefaultConfiguration()) | ||
this.c12InstanceCache = c12Instance | ||
return c12Instance | ||
} | ||
|
||
private getCustomConfigurationTarget(bootstrap: Container): ConfigurationSymbol | undefined { | ||
const customTarget = bootstrap.getInjectableTargetByToken(ConfigurationSymbol) | ||
if (!customTarget) return undefined | ||
return customTarget.getOrCreateInstance() | ||
} | ||
|
||
async readConfiguration(bootstrap: Container = new Container()): ReturnType<typeof loadConfig> { | ||
const customTargetInstance = this.getCustomConfigurationTarget(bootstrap) | ||
if (!customTargetInstance) return await this.getC12Instance() | ||
|
||
if (!customTargetInstance.configure || typeof customTargetInstance.configure !== 'function') | ||
return await this.getC12Instance() | ||
const configuration = await customTargetInstance.configure(this.getDefaultConfiguration()) | ||
return await this.getC12Instance(configuration) | ||
} | ||
|
||
async evaluateExpression<El extends string>(el: El): Promise<any> { | ||
return this.jexlExecutor.evalSync(el, (await this.readConfiguration()).config) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from './config-provider' | ||
export * from './decorators' | ||
export * from './plugin' | ||
export * from './plugin-protocol' | ||
export * from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { UserInputConfig } from 'c12' | ||
|
||
export const Configuration = '__naily_config_custom_configuration__' | ||
export interface Configuration { | ||
configure(defaultConfiguration: UserInputConfig): UserInputConfig | Promise<UserInputConfig> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters