From 5f35752ee97aa82d1d79edad6e0c14e82371fcd3 Mon Sep 17 00:00:00 2001 From: David LJ Date: Thu, 18 Jan 2024 00:20:10 +0100 Subject: [PATCH] feat: improve metadata value types --- .../meta-set-by-route-and-service.component.ts | 6 ++---- .../meta-set-by-route-and-service.component.ts | 6 ++---- .../meta-set-by-route-and-service.component.ts | 6 ++---- projects/ngx-meta/src/core/src/core.module.ts | 2 +- .../src/core/src/metadata-json-resolver.spec.ts | 2 +- .../src/core/src/metadata-json-resolver.ts | 16 ++++++++++------ .../src/core/src/metadata-resolver.spec.ts | 2 +- .../ngx-meta/src/core/src/metadata-resolver.ts | 4 ++-- .../ngx-meta/src/core/src/metadata-values.ts | 4 +--- .../ngx-meta/src/core/src/metadata.service.ts | 4 ++-- projects/ngx-meta/src/core/src/provide-core.ts | 2 +- .../src/core/src/route-metadata-values.spec.ts | 3 +-- .../src/routing/src/metadata-route-data.ts | 6 +++--- .../src/routing/src/metadata-route-strategy.ts | 12 +++++++----- .../src/routing/src/router-listener.service.ts | 3 ++- 15 files changed, 38 insertions(+), 40 deletions(-) diff --git a/projects/ngx-meta/e2e/a15/src/app/meta-set-by-route-and-service/meta-set-by-route-and-service.component.ts b/projects/ngx-meta/e2e/a15/src/app/meta-set-by-route-and-service/meta-set-by-route-and-service.component.ts index c490c39d0..080bf7516 100644 --- a/projects/ngx-meta/e2e/a15/src/app/meta-set-by-route-and-service/meta-set-by-route-and-service.component.ts +++ b/projects/ngx-meta/e2e/a15/src/app/meta-set-by-route-and-service/meta-set-by-route-and-service.component.ts @@ -1,5 +1,5 @@ import { Component, OnInit } from '@angular/core' -import { MetadataService, MetadataValues } from '@davidlj95/ngx-meta/core' +import { MetadataService } from '@davidlj95/ngx-meta/core' import { ActivatedRoute } from '@angular/router' import { OPEN_GRAPH_TYPE_BOOK, @@ -42,8 +42,6 @@ export class MetaSetByRouteAndServiceComponent implements OnInit { } ngOnInit(): void { - this.metadataService.set( - this.overriddenMetadata as unknown as MetadataValues, - ) + this.metadataService.set(this.overriddenMetadata) } } diff --git a/projects/ngx-meta/e2e/a16/src/app/meta-set-by-route-and-service/meta-set-by-route-and-service.component.ts b/projects/ngx-meta/e2e/a16/src/app/meta-set-by-route-and-service/meta-set-by-route-and-service.component.ts index c490c39d0..080bf7516 100644 --- a/projects/ngx-meta/e2e/a16/src/app/meta-set-by-route-and-service/meta-set-by-route-and-service.component.ts +++ b/projects/ngx-meta/e2e/a16/src/app/meta-set-by-route-and-service/meta-set-by-route-and-service.component.ts @@ -1,5 +1,5 @@ import { Component, OnInit } from '@angular/core' -import { MetadataService, MetadataValues } from '@davidlj95/ngx-meta/core' +import { MetadataService } from '@davidlj95/ngx-meta/core' import { ActivatedRoute } from '@angular/router' import { OPEN_GRAPH_TYPE_BOOK, @@ -42,8 +42,6 @@ export class MetaSetByRouteAndServiceComponent implements OnInit { } ngOnInit(): void { - this.metadataService.set( - this.overriddenMetadata as unknown as MetadataValues, - ) + this.metadataService.set(this.overriddenMetadata) } } diff --git a/projects/ngx-meta/e2e/a17/src/app/meta-set-by-route-and-service/meta-set-by-route-and-service.component.ts b/projects/ngx-meta/e2e/a17/src/app/meta-set-by-route-and-service/meta-set-by-route-and-service.component.ts index 5fff9594a..43f72143e 100644 --- a/projects/ngx-meta/e2e/a17/src/app/meta-set-by-route-and-service/meta-set-by-route-and-service.component.ts +++ b/projects/ngx-meta/e2e/a17/src/app/meta-set-by-route-and-service/meta-set-by-route-and-service.component.ts @@ -1,5 +1,5 @@ import { Component, OnInit } from '@angular/core' -import { MetadataService, MetadataValues } from '@davidlj95/ngx-meta/core' +import { MetadataService } from '@davidlj95/ngx-meta/core' import { JsonPipe } from '@angular/common' import { ActivatedRoute } from '@angular/router' import { @@ -45,8 +45,6 @@ export class MetaSetByRouteAndServiceComponent implements OnInit { } ngOnInit(): void { - this.metadataService.set( - this.overriddenMetadata as unknown as MetadataValues, - ) + this.metadataService.set(this.overriddenMetadata) } } diff --git a/projects/ngx-meta/src/core/src/core.module.ts b/projects/ngx-meta/src/core/src/core.module.ts index de0110f79..d2de54173 100644 --- a/projects/ngx-meta/src/core/src/core.module.ts +++ b/projects/ngx-meta/src/core/src/core.module.ts @@ -1,6 +1,6 @@ import { ModuleWithProviders, NgModule } from '@angular/core' -import { MetadataValues } from './metadata-values' import { CORE_PROVIDERS, withDefaults } from './provide-core' +import { MetadataValues } from './metadata-values' @NgModule() export class CoreModule { diff --git a/projects/ngx-meta/src/core/src/metadata-json-resolver.spec.ts b/projects/ngx-meta/src/core/src/metadata-json-resolver.spec.ts index 708496575..abf00ed06 100644 --- a/projects/ngx-meta/src/core/src/metadata-json-resolver.spec.ts +++ b/projects/ngx-meta/src/core/src/metadata-json-resolver.spec.ts @@ -37,7 +37,7 @@ describe('MetadataJsonResolver', () => { }) }) describe('and global value exists', () => { - const valuesWithGlobal = { [global]: value, ...values } + const valuesWithGlobal = { [global]: value, ...(values as object) } it('should return global value', () => { expect(sut.get(metadataWithGlobal, valuesWithGlobal)).toEqual(value) diff --git a/projects/ngx-meta/src/core/src/metadata-json-resolver.ts b/projects/ngx-meta/src/core/src/metadata-json-resolver.ts index f115e1a93..e67f3397f 100644 --- a/projects/ngx-meta/src/core/src/metadata-json-resolver.ts +++ b/projects/ngx-meta/src/core/src/metadata-json-resolver.ts @@ -1,26 +1,28 @@ -import { Metadata } from './metadata' -import { MetadataValues } from './metadata-values' import { MaybeUndefined } from './maybe-undefined' import { isObject } from './is-object' import { Injectable } from '@angular/core' +import { Metadata } from './metadata' +import { MetadataValues } from './metadata-values' @Injectable({ providedIn: 'root' }) export class MetadataJsonResolver { - get(definition: Metadata, values?: MetadataValues): T | undefined { + get(metadata: Metadata, values?: MetadataValues): T | undefined { if (values === undefined) { return } - const keys = [...definition.jsonPath] + const keys = [...metadata.jsonPath] let value: unknown = values for (const key of keys) { if (value === undefined || value === null) { break } - value = (value as MetadataValues)[key] + value = (value as IndexedObject)[key] } const globalValue = - definition.global !== undefined ? values[definition.global] : undefined + metadata.global !== undefined + ? (values as IndexedObject)[metadata.global] + : undefined if (value !== undefined && !globalValue) { return value as MaybeUndefined } @@ -33,3 +35,5 @@ export class MetadataJsonResolver { return globalValue as MaybeUndefined } } + +type IndexedObject = Record diff --git a/projects/ngx-meta/src/core/src/metadata-resolver.spec.ts b/projects/ngx-meta/src/core/src/metadata-resolver.spec.ts index 0af94216a..db3d344f9 100644 --- a/projects/ngx-meta/src/core/src/metadata-resolver.spec.ts +++ b/projects/ngx-meta/src/core/src/metadata-resolver.spec.ts @@ -6,11 +6,11 @@ import { enableAutoSpy } from '../../__tests__/enable-auto-spy' import { MetadataJsonResolver } from './metadata-json-resolver' import { RouteMetadataValues } from './route-metadata-values' import { Metadata } from './metadata' -import { MetadataValues } from './metadata-values' import { MaybeUndefined } from './maybe-undefined' import { makeGlobalMetadata } from './make-global-metadata' import { Provider } from '@angular/core' import { DEFAULTS_TOKEN } from './defaults-token' +import { MetadataValues } from './metadata-values' describe('MetadataResolver', () => { enableAutoSpy() diff --git a/projects/ngx-meta/src/core/src/metadata-resolver.ts b/projects/ngx-meta/src/core/src/metadata-resolver.ts index d67c12ffe..8cc497c17 100644 --- a/projects/ngx-meta/src/core/src/metadata-resolver.ts +++ b/projects/ngx-meta/src/core/src/metadata-resolver.ts @@ -1,11 +1,11 @@ import { Inject, Injectable, Optional } from '@angular/core' import { MetadataJsonResolver } from './metadata-json-resolver' -import { MetadataValues } from './metadata-values' import { RouteMetadataValues } from './route-metadata-values' import { DEFAULTS_TOKEN } from './defaults-token' import { isObject } from './is-object' import { Metadata } from './metadata' import { MaybeUndefined } from './maybe-undefined' +import { MetadataValues } from './metadata-values' @Injectable({ providedIn: 'root' }) export class MetadataResolver { @@ -14,7 +14,7 @@ export class MetadataResolver { private readonly routeMetadataValues: RouteMetadataValues, @Optional() @Inject(DEFAULTS_TOKEN) - private readonly defaults: MetadataValues | null, + private readonly defaults: unknown | null, ) {} get(metadata: Metadata, values: MetadataValues): T | undefined { diff --git a/projects/ngx-meta/src/core/src/metadata-values.ts b/projects/ngx-meta/src/core/src/metadata-values.ts index 83543cc9f..ed0728c48 100644 --- a/projects/ngx-meta/src/core/src/metadata-values.ts +++ b/projects/ngx-meta/src/core/src/metadata-values.ts @@ -1,3 +1 @@ -export interface MetadataValues { - [key: string]: unknown -} +export type MetadataValues = object diff --git a/projects/ngx-meta/src/core/src/metadata.service.ts b/projects/ngx-meta/src/core/src/metadata.service.ts index 8e690aa8e..d3d4bc145 100644 --- a/projects/ngx-meta/src/core/src/metadata.service.ts +++ b/projects/ngx-meta/src/core/src/metadata.service.ts @@ -1,8 +1,8 @@ import { Injectable } from '@angular/core' import { MetadataResolver } from './metadata-resolver' -import { MetadataValues } from './metadata-values' import { RouteMetadataValues } from './route-metadata-values' import { MetadataRegistry } from './metadata-registry' +import { MetadataValues } from './metadata-values' @Injectable({ providedIn: 'root' }) export class MetadataService { @@ -12,7 +12,7 @@ export class MetadataService { private readonly routeValues: RouteMetadataValues, ) {} - public set(values: MetadataValues = {}): void { + public set(values: M = {} as M): void { const allMetadata = this.registry.getAll() for (const metadata of allMetadata) { metadata.set(this.resolver.get(metadata.metadata, values)) diff --git a/projects/ngx-meta/src/core/src/provide-core.ts b/projects/ngx-meta/src/core/src/provide-core.ts index d0f1ea0e5..513621059 100644 --- a/projects/ngx-meta/src/core/src/provide-core.ts +++ b/projects/ngx-meta/src/core/src/provide-core.ts @@ -3,9 +3,9 @@ import { makeEnvironmentProviders, Provider, } from '@angular/core' -import { MetadataValues } from './metadata-values' import { DEFAULTS_TOKEN } from './defaults-token' import { HEAD_ELEMENT_UPSERT_OR_REMOVE_PROVIDER } from './head-element-upsert-or-remove' +import { MetadataValues } from './metadata-values' export const CORE_PROVIDERS = [HEAD_ELEMENT_UPSERT_OR_REMOVE_PROVIDER] diff --git a/projects/ngx-meta/src/core/src/route-metadata-values.spec.ts b/projects/ngx-meta/src/core/src/route-metadata-values.spec.ts index 2d5a54cd8..44ec8ab6f 100644 --- a/projects/ngx-meta/src/core/src/route-metadata-values.spec.ts +++ b/projects/ngx-meta/src/core/src/route-metadata-values.spec.ts @@ -4,13 +4,12 @@ import { RouteMetadataValues } from './route-metadata-values' import { enableAutoSpy } from '../../__tests__/enable-auto-spy' import { MockProviders } from 'ng-mocks' import { Router } from '@angular/router' -import { MetadataValues } from './metadata-values' describe('RouteMetadataValues', () => { enableAutoSpy() let sut: RouteMetadataValues let router: jasmine.SpyObj - const dummyValues: MetadataValues = { foo: 'bar' } + const dummyValues = { foo: 'bar' } const url = '/set-url' beforeEach(() => { diff --git a/projects/ngx-meta/src/routing/src/metadata-route-data.ts b/projects/ngx-meta/src/routing/src/metadata-route-data.ts index 6ec38f9cf..6d1bbb459 100644 --- a/projects/ngx-meta/src/routing/src/metadata-route-data.ts +++ b/projects/ngx-meta/src/routing/src/metadata-route-data.ts @@ -1,5 +1,5 @@ -import { GlobalMetadata, MetadataValues } from '@davidlj95/ngx-meta/core' +import { MetadataValues } from '@davidlj95/ngx-meta/core' -export interface MetadataRouteData { - meta: GlobalMetadata & MetadataValues +export interface MetadataRouteData { + meta: M } diff --git a/projects/ngx-meta/src/routing/src/metadata-route-strategy.ts b/projects/ngx-meta/src/routing/src/metadata-route-strategy.ts index 3448d6603..9ec2205b0 100644 --- a/projects/ngx-meta/src/routing/src/metadata-route-strategy.ts +++ b/projects/ngx-meta/src/routing/src/metadata-route-strategy.ts @@ -1,10 +1,12 @@ import { InjectionToken } from '@angular/core' import { ActivatedRouteSnapshot } from '@angular/router' +import { MetadataValues } from '@davidlj95/ngx-meta/core' -export type MetadataRouteStrategy = ( +export type MetadataRouteStrategy = ( activatedRouteSnapshot: ActivatedRouteSnapshot, -) => T | undefined +) => MetadataValues | undefined -export const METADATA_ROUTE_STRATEGY = new InjectionToken( - ngDevMode ? 'NgxMeta Metadata Route Strategy' : 'NgxMetaMRS', -) +export const METADATA_ROUTE_STRATEGY = + new InjectionToken( + ngDevMode ? 'NgxMeta Metadata Route Strategy' : 'NgxMetaMRS', + ) diff --git a/projects/ngx-meta/src/routing/src/router-listener.service.ts b/projects/ngx-meta/src/routing/src/router-listener.service.ts index b818045ee..90c362f58 100644 --- a/projects/ngx-meta/src/routing/src/router-listener.service.ts +++ b/projects/ngx-meta/src/routing/src/router-listener.service.ts @@ -49,7 +49,8 @@ export class RouterListenerService implements OnDestroy { } return } - this.metadataService.set(this.strategy(this.activatedRoute.snapshot)) + const data = this.strategy(this.activatedRoute.snapshot) + this.metadataService.set(data ?? {}) }, }) }