Skip to content

Commit

Permalink
feat: improve metadata value types
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlj95 committed Jan 18, 2024
1 parent 42eae60 commit 5f35752
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion projects/ngx-meta/src/core/src/core.module.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 10 additions & 6 deletions projects/ngx-meta/src/core/src/metadata-json-resolver.ts
Original file line number Diff line number Diff line change
@@ -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<T>(definition: Metadata, values?: MetadataValues): T | undefined {
get<T>(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<T>
}
Expand All @@ -33,3 +35,5 @@ export class MetadataJsonResolver {
return globalValue as MaybeUndefined<T>
}
}

type IndexedObject = Record<string, unknown>
2 changes: 1 addition & 1 deletion projects/ngx-meta/src/core/src/metadata-resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions projects/ngx-meta/src/core/src/metadata-resolver.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<T>(metadata: Metadata, values: MetadataValues): T | undefined {
Expand Down
4 changes: 1 addition & 3 deletions projects/ngx-meta/src/core/src/metadata-values.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export interface MetadataValues {
[key: string]: unknown
}
export type MetadataValues = object
4 changes: 2 additions & 2 deletions projects/ngx-meta/src/core/src/metadata.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -12,7 +12,7 @@ export class MetadataService {
private readonly routeValues: RouteMetadataValues,
) {}

public set(values: MetadataValues = {}): void {
public set<M extends MetadataValues>(values: M = {} as M): void {
const allMetadata = this.registry.getAll()
for (const metadata of allMetadata) {
metadata.set(this.resolver.get(metadata.metadata, values))
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-meta/src/core/src/provide-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
3 changes: 1 addition & 2 deletions projects/ngx-meta/src/core/src/route-metadata-values.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Router>
const dummyValues: MetadataValues = { foo: 'bar' }
const dummyValues = { foo: 'bar' }
const url = '/set-url'

beforeEach(() => {
Expand Down
6 changes: 3 additions & 3 deletions projects/ngx-meta/src/routing/src/metadata-route-data.ts
Original file line number Diff line number Diff line change
@@ -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<M = MetadataValues> {
meta: M
}
12 changes: 7 additions & 5 deletions projects/ngx-meta/src/routing/src/metadata-route-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { InjectionToken } from '@angular/core'
import { ActivatedRouteSnapshot } from '@angular/router'
import { MetadataValues } from '@davidlj95/ngx-meta/core'

export type MetadataRouteStrategy = <T>(
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<MetadataRouteStrategy>(
ngDevMode ? 'NgxMeta Metadata Route Strategy' : 'NgxMetaMRS',
)
3 changes: 2 additions & 1 deletion projects/ngx-meta/src/routing/src/router-listener.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? {})
},
})
}
Expand Down

0 comments on commit 5f35752

Please sign in to comment.