Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve metadata value types #194

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
12 changes: 8 additions & 4 deletions projects/ngx-meta/src/core/src/metadata-json-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ import { Injectable } from '@angular/core'

@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>
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
}
Comment on lines -1 to -3
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was giving issues, as it's a specific type of object. An object that implements an interface without index signature wouldn't comply with this. Despite yet being valid metadata values. So going for a broader definition

export type MetadataValues = object
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>(
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't make sense to specify type here, given the strategy should load some object from route, without caring what the object contains. It's not its responsibility

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',
)