-
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.
feat: allow feature APIs in core module + deprecate options (#830)
- Loading branch information
Showing
10 changed files
with
243 additions
and
111 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Provider } from '@angular/core' | ||
|
||
/** | ||
* Inspired from Angular router | ||
* | ||
* https://github.com/angular/angular/blob/17.0.7/packages/router/src/provide_router.ts#L80-L96 | ||
* @internal | ||
*/ | ||
export const enum __CoreFeatureKind { | ||
Defaults, | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export interface __CoreFeature<FeatureKind extends __CoreFeatureKind> { | ||
_kind: FeatureKind | ||
_providers: Provider[] | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const __coreFeature = <FeatureKind extends __CoreFeatureKind>( | ||
kind: FeatureKind, | ||
providers: Provider[], | ||
): __CoreFeature<FeatureKind> => ({ | ||
_kind: kind, | ||
_providers: providers, | ||
}) | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const isCoreFeature = ( | ||
anObject: object, | ||
): anObject is __CoreFeature<__CoreFeatureKind> => | ||
('_providers' satisfies keyof __CoreFeature<__CoreFeatureKind>) in anObject | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export type __CoreFeatures = ReadonlyArray<__CoreFeature<__CoreFeatureKind>> | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const __providersFromCoreFeatures = ( | ||
features: __CoreFeatures, | ||
): ReadonlyArray<Provider> => features.map((f) => f._providers) |
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,6 +1,9 @@ | ||
import { InjectionToken } from '@angular/core' | ||
import { inject, InjectionToken } from '@angular/core' | ||
import { MetadataValues } from './metadata-values' | ||
|
||
export const DEFAULTS_TOKEN = new InjectionToken<MetadataValues>( | ||
ngDevMode ? 'NgxMeta Metadata defaults' : 'NgxMetaDefs', | ||
) | ||
|
||
export const injectDefaults = (): MetadataValues | null => | ||
inject(DEFAULTS_TOKEN, { optional: true }) |
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 |
---|---|---|
@@ -1,45 +1,112 @@ | ||
import { ModuleWithProviders, NgModule } from '@angular/core' | ||
import { MetadataValues } from './metadata-values' | ||
import { withNgxMetaDefaults } from './provide-ngx-meta-core' | ||
import { CORE_PROVIDERS } from './core-providers' | ||
import { withNgxMetaDefaults } from './with-ngx-meta-defaults' | ||
import { | ||
__CoreFeature, | ||
__CoreFeatureKind, | ||
__CoreFeatures, | ||
__providersFromCoreFeatures, | ||
isCoreFeature, | ||
} from './core-feature' | ||
|
||
/** | ||
* Adds core providers of `ngx-meta` to the application. | ||
* Must use {@link NgxMetaCoreModule.forRoot} method. | ||
* Provides `ngx-meta`'s core library services. | ||
* | ||
* For standalone apps, use {@link provideNgxMetaCore} instead | ||
* Use {@link NgxMetaCoreModule.(forRoot:1)} method. Importing the module alone does nothing. | ||
* | ||
* For standalone apps, use {@link provideNgxMetaCore} instead. | ||
* | ||
* @public | ||
*/ | ||
@NgModule() | ||
export class NgxMetaCoreModule { | ||
/** | ||
* Provides the core library services | ||
* Provides `ngx-meta`'s core library services. | ||
* | ||
* Accepts optional features configuration. See examples for more info. | ||
* | ||
* Allows specifying some default metadata values | ||
* Previous configuration of features with an options object has been deprecated. | ||
* See {@link NgxMetaCoreModule.(forRoot:2)} for more information and how to migrate | ||
* | ||
* @example | ||
* Default metadata values can be set up. | ||
* | ||
* ```typescript | ||
* NgxMetaCoreModule.forRoot(withNgxMetaDefaults({title: 'Default title'}) | ||
* ``` | ||
* | ||
* @see {@link withNgxMetaDefaults} | ||
* @see {@link https://ngx-meta.dev/guides/defaults/} | ||
* | ||
* @param features - Features to configure the core module with | ||
*/ | ||
static forRoot( | ||
...features: __CoreFeatures | ||
): ModuleWithProviders<NgxMetaCoreModule> | ||
|
||
/** | ||
* Deprecated way of configuring the core module features. | ||
* | ||
* This way of configuring options doesn't allow tree shaking unneeded features. | ||
* So usage is discouraged and deprecated. | ||
* See deprecation notice for the tree-shaking friendly alternative | ||
* | ||
* Checkout the method signature examples for an example on how to migrate to the recommended way | ||
* | ||
* @deprecated Use {@link NgxMetaCoreModule.(forRoot:1)} with feature APIs as arguments instead. | ||
* | ||
* You can set some defaults using the `options` argument | ||
* @example | ||
* ```typescript | ||
* NgxMetaCoreModule.forRoot({defaults: {title: 'Default title'}}) | ||
* ``` | ||
* | ||
* @param options - Allows providing some default metadata values using `defaults` | ||
* should be migrated to | ||
* | ||
* ```typescript | ||
* NgxMetaCoreModule.forRoot(withNgxMetaDefaults({title: 'Default title'})) | ||
* ``` | ||
*/ | ||
// noinspection JSDeprecatedSymbols | ||
static forRoot( | ||
options: { | ||
defaults?: MetadataValues | ||
} = {}, | ||
options: NgxMetaCoreModuleForRootOptions, | ||
): ModuleWithProviders<NgxMetaCoreModule> | ||
|
||
// noinspection JSDeprecatedSymbols | ||
static forRoot( | ||
optionsOrFeature: | ||
| NgxMetaCoreModuleForRootOptions | ||
| __CoreFeature<__CoreFeatureKind> = {}, | ||
...features: __CoreFeatures | ||
): ModuleWithProviders<NgxMetaCoreModule> { | ||
const optionFeaturesOrFirstFeature = isCoreFeature(optionsOrFeature) | ||
? [optionsOrFeature] | ||
: optionsOrFeature.defaults | ||
? [withNgxMetaDefaults(optionsOrFeature.defaults)] | ||
: [] | ||
return { | ||
ngModule: NgxMetaCoreModule, | ||
providers: [ | ||
...CORE_PROVIDERS, | ||
...(options.defaults !== undefined | ||
? withNgxMetaDefaults(options.defaults)._providers | ||
: []), | ||
...__providersFromCoreFeatures([ | ||
...optionFeaturesOrFirstFeature, | ||
...features, | ||
]), | ||
], | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Configuration options for {@link NgxMetaCoreModule.(forRoot:2)} | ||
* | ||
* @deprecated Use {@link NgxMetaCoreModule.(forRoot:1)} with feature APIs as arguments instead. | ||
* See {@link NgxMetaCoreModule.(forRoot:2)} for a migration example | ||
* @public | ||
*/ | ||
export interface NgxMetaCoreModuleForRootOptions { | ||
/** | ||
* See {@link withNgxMetaDefaults} | ||
*/ | ||
defaults?: MetadataValues | ||
} |
Oops, something went wrong.