mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-25 14:28:51 +08:00
e516e0db23
- [x] separates modules into `fundamental`, `core`, `plugins`
- [x] optional modules with `@OptionalModule` decorator to install modules with requirements met(`requires`, `if`)
- [x] `module.contributesTo` defines optional features that will be enabled if module registered
- [x] `AFFiNE.plugins.use('payment', {})` to enable a optional/plugin module
- [x] `PaymentModule` is the first plugin module
- [x] GraphQLSchema will not be generated for non-included modules
- [x] Frontend can use `ServerConfigType` query to detect which features are enabled
- [x] override existing provider globally
59 lines
1.3 KiB
TypeScript
59 lines
1.3 KiB
TypeScript
import { DynamicModule, FactoryProvider } from '@nestjs/common';
|
|
import { merge } from 'lodash-es';
|
|
|
|
import { ApplyType } from '../utils/types';
|
|
import { AFFiNEConfig } from './def';
|
|
|
|
/**
|
|
* @example
|
|
*
|
|
* import { Config } from '@affine/server'
|
|
*
|
|
* class TestConfig {
|
|
* constructor(private readonly config: Config) {}
|
|
* test() {
|
|
* return this.config.env
|
|
* }
|
|
* }
|
|
*/
|
|
export class Config extends ApplyType<AFFiNEConfig>() {}
|
|
|
|
function createConfigProvider(
|
|
override?: DeepPartial<Config>
|
|
): FactoryProvider<Config> {
|
|
return {
|
|
provide: Config,
|
|
useFactory: () => {
|
|
const wrapper = new Config();
|
|
const config = merge({}, globalThis.AFFiNE, override);
|
|
|
|
const proxy: Config = new Proxy(wrapper, {
|
|
get: (_target, property: keyof Config) => {
|
|
const desc = Object.getOwnPropertyDescriptor(
|
|
globalThis.AFFiNE,
|
|
property
|
|
);
|
|
if (desc?.get) {
|
|
return desc.get.call(proxy);
|
|
}
|
|
return config[property];
|
|
},
|
|
});
|
|
return proxy;
|
|
},
|
|
};
|
|
}
|
|
|
|
export class ConfigModule {
|
|
static forRoot = (override?: DeepPartial<Config>): DynamicModule => {
|
|
const provider = createConfigProvider(override);
|
|
|
|
return {
|
|
global: true,
|
|
module: ConfigModule,
|
|
providers: [provider],
|
|
exports: [provider],
|
|
};
|
|
};
|
|
}
|