Skip to content

Commit

Permalink
refactor: add routing & general metadata entry points (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlj95 authored Nov 22, 2023
1 parent fc55f26 commit a791e4f
Show file tree
Hide file tree
Showing 59 changed files with 129 additions and 95 deletions.
1 change: 1 addition & 0 deletions projects/ngx-metadata/general-metadata/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './public-api'
6 changes: 6 additions & 0 deletions projects/ngx-metadata/general-metadata/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "../../../node_modules/ng-packagr/ng-package.schema.json",
"lib": {
"entryFile": "public-api.ts"
}
}
7 changes: 7 additions & 0 deletions projects/ngx-metadata/general-metadata/public-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export { GeneralMetadataModule as NgxMetadataGeneralModule } from './src/general-metadata.module'
export * from './src/routing/default-general-metadata-route-data'
export { GeneralMetadataRouteDataService as _GeneralMetadataRouteDataService } from './src/routing/general-metadata-route-data.service'
export * from './src/general-metadata.service'
export { GeneralMetadataAppliersService as _GeneralMetadataAppliersService } from './src/general-metadata-appliers.service'
export * from './src/general-metadata'
export * from './src/general-metadata-image'
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Inject, ModuleWithProviders, NgModule, Optional } from '@angular/core'
import { GeneralMetadataService } from './general-metadata.service'
import { HtmlLangAttributeService } from './html-lang-attribute/html-lang-attribute.service'
import { DefaultGeneralMetadataRouteStrategy } from './routing/default-general-metadata-route-strategy'
import { MetadataRouteStrategy } from '../routing/metadata-route-strategy'
import { _MetadataRouteStrategy } from 'ngx-metadata/routing'
import { GeneralMetadataAppliersService } from './general-metadata-appliers.service'
import { GeneralMetadataApplierService } from './general-metadata-applier.service'
import { GeneralMetadataRouteDataService } from './routing/general-metadata-route-data.service'
Expand Down Expand Up @@ -48,7 +48,7 @@ export class GeneralMetadataModule {
useClass: DefaultGeneralMetadataRouteStrategy,
},
{
provide: MetadataRouteStrategy,
provide: _MetadataRouteStrategy,
useExisting: GeneralMetadataRouteStrategy,
multi: true,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { MAIN_KEY } from '../../routing/current-route-data-key-path-metadata-strategy'
import { _MAIN_KEY } from 'ngx-metadata/routing'
import { KEY } from './default-general-metadata-route-strategy'
import { GeneralMetadata } from '../general-metadata'

export interface DefaultGeneralMetadataRouteData {
[MAIN_KEY]: {
[_MAIN_KEY]: {
[KEY]: GeneralMetadata
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GeneralMetadata } from '../general-metadata'
import { GeneralMetadataService } from '../general-metadata.service'
import { GeneralMetadataRouteStrategy } from './general-metadata-route-strategy'
import { ActivatedRouteSnapshot } from '@angular/router'
import { CurrentRouteDataKeyPathMetadataStrategy } from '../../routing/current-route-data-key-path-metadata-strategy'
import { _CurrentRouteDataKeyPathMetadataStrategy } from 'ngx-metadata/routing'

export const KEY = 'general'

Expand All @@ -12,7 +12,7 @@ export class DefaultGeneralMetadataRouteStrategy
implements GeneralMetadataRouteStrategy
{
constructor(
private readonly currentRouteDataKeyPathMetadataStrategy: CurrentRouteDataKeyPathMetadataStrategy,
private readonly currentRouteDataKeyPathMetadataStrategy: _CurrentRouteDataKeyPathMetadataStrategy,
private readonly generalMetadataService: GeneralMetadataService,
) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { MockProvider } from 'ng-mocks'
import { GeneralMetadata } from '../general-metadata'
import { GENERAL_METADATA_DEFAULTS_TOKEN } from '../general-metadata-defaults-token'
import { Provider } from '@angular/core'
import { MetadataRouteStrategy } from '../../routing/metadata-route-strategy'
import { _MetadataRouteStrategy } from 'ngx-metadata/routing'
import { ActivatedRouteSnapshot } from '@angular/router'
import { DefaultGeneralMetadataRouteStrategy } from './default-general-metadata-route-strategy'
import { GeneralMetadataRouteStrategy } from './general-metadata-route-strategy'
Expand All @@ -27,7 +27,7 @@ describe('GeneralMetadataRouteDataService', () => {
sut = makeSut()
generalMetadataRouteStrategy = TestBed.inject(
GeneralMetadataRouteStrategy,
) as jasmine.SpyObj<MetadataRouteStrategy<GeneralMetadata>>
) as jasmine.SpyObj<_MetadataRouteStrategy<GeneralMetadata>>
})

describe('when no data has been resolved for route', () => {
Expand Down Expand Up @@ -63,7 +63,7 @@ describe('GeneralMetadataRouteDataService', () => {
sut = makeSut({ defaults })
generalMetadataRouteStrategy = TestBed.inject(
GeneralMetadataRouteStrategy,
) as jasmine.SpyObj<MetadataRouteStrategy<GeneralMetadata>>
) as jasmine.SpyObj<_MetadataRouteStrategy<GeneralMetadata>>
defaultsService = TestBed.inject(
_DefaultsService,
) as jasmine.SpyObj<_DefaultsService>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Inject, Injectable, Optional } from '@angular/core'
import { MetadataRouteStrategy } from '../../routing/metadata-route-strategy'
import { _MetadataRouteStrategy } from 'ngx-metadata/routing'
import { GeneralMetadata } from '../general-metadata'
import { ActivatedRouteSnapshot } from '@angular/router'
import { GENERAL_METADATA_DEFAULTS_TOKEN } from '../general-metadata-defaults-token'
Expand All @@ -8,7 +8,7 @@ import { _DefaultsService } from 'ngx-metadata/common'

@Injectable()
export class GeneralMetadataRouteDataService
implements Pick<MetadataRouteStrategy<GeneralMetadata>, 'resolve'>
implements Pick<_MetadataRouteStrategy<GeneralMetadata>, 'resolve'>
{
constructor(
private readonly generalMetadataRouteStrategy: GeneralMetadataRouteStrategy,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { _MetadataRouteStrategy } from 'ngx-metadata/routing'
import { GeneralMetadata } from '../general-metadata'

export abstract class GeneralMetadataRouteStrategy extends _MetadataRouteStrategy<GeneralMetadata> {}
2 changes: 0 additions & 2 deletions projects/ngx-metadata/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
* Public API Surface of ngx-metadata
*/

export * from './src/lib/general-metadata'
export * from './src/lib/open-graph'
export * from './src/lib/open-graph-profile'
export * from './src/lib/routing'
export * from './src/lib/twitter-card'
1 change: 1 addition & 0 deletions projects/ngx-metadata/routing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './public-api'
6 changes: 6 additions & 0 deletions projects/ngx-metadata/routing/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "../../../node_modules/ng-packagr/ng-package.schema.json",
"lib": {
"entryFile": "public-api.ts"
}
}
7 changes: 7 additions & 0 deletions projects/ngx-metadata/routing/public-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export { RoutingModule as NgxMetadataRoutingModule } from './src/routing.module'
export { MetadataRouteStrategy as _MetadataRouteStrategy } from './src/metadata-route-strategy'
export { CurrentRouteDataKeyPathMetadataStrategy as _CurrentRouteDataKeyPathMetadataStrategy } from './src/current-route-data-key-path-metadata-strategy'
export {
MAIN_KEY as _MAIN_KEY,
KEY_PATH_SEPARATOR as _KEY_PATH_SEPARATOR,
} from './src/current-route-data-key-path-metadata-strategy'
5 changes: 0 additions & 5 deletions projects/ngx-metadata/src/lib/general-metadata/index.ts

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Optional,
} from '@angular/core'
import { OpenGraphProfileApplierService } from './open-graph-profile-applier.service'
import { MetadataRouteStrategy } from '../routing/metadata-route-strategy'
import { _MetadataRouteStrategy } from 'ngx-metadata/routing'
import { OpenGraphProfileRouteStrategy } from './routing/open-graph-profile-route-strategy'
import { OpenGraphProfile } from './open-graph-profile'
import { OPEN_GRAPH_PROFILE_DEFAULTS_TOKEN } from './open-graph-profile-defaults-token'
Expand Down Expand Up @@ -49,7 +49,7 @@ export class OpenGraphProfileModule {
useClass: DefaultOpenGraphProfileRouteStrategy,
},
{
provide: MetadataRouteStrategy,
provide: _MetadataRouteStrategy,
useExisting: OpenGraphProfileRouteStrategy,
multi: true,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { OpenGraphProfile } from '../open-graph-profile'
import { MAIN_KEY } from '../../routing/current-route-data-key-path-metadata-strategy'
import { _MAIN_KEY } from 'ngx-metadata/routing'
import { KEY as OPEN_GRAPH_KEY } from '../../open-graph/routing/default-open-graph-route-strategy'
import { KEY } from './default-open-graph-profile-route-strategy'

export interface DefaultOpenGraphProfileRouteData {
[MAIN_KEY]: {
[_MAIN_KEY]: {
[OPEN_GRAPH_KEY]: {
[KEY]: OpenGraphProfile
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import { Injectable } from '@angular/core'
import { OpenGraphProfile } from '../open-graph-profile'
import { ActivatedRouteSnapshot } from '@angular/router'
import { OpenGraphProfileRouteStrategy } from './open-graph-profile-route-strategy'
import {
CurrentRouteDataKeyPathMetadataStrategy,
KEY_PATH_SEPARATOR,
} from '../../routing/current-route-data-key-path-metadata-strategy'
import { OpenGraphProfileService } from '../open-graph-profile.service'
import { KEY as OPEN_GRAPH_KEY } from '../../open-graph/routing/default-open-graph-route-strategy'
import {
_CurrentRouteDataKeyPathMetadataStrategy,
_KEY_PATH_SEPARATOR,
} from 'ngx-metadata/routing'

export const KEY = 'profile'
export const KEY_PATH = `${OPEN_GRAPH_KEY}${KEY_PATH_SEPARATOR}${KEY}`
export const KEY_PATH = `${OPEN_GRAPH_KEY}${_KEY_PATH_SEPARATOR}${KEY}`

@Injectable()
export class DefaultOpenGraphProfileRouteStrategy
implements OpenGraphProfileRouteStrategy
{
constructor(
private readonly currentRouteDataKeyPathMetadataStrategy: CurrentRouteDataKeyPathMetadataStrategy,
private readonly currentRouteDataKeyPathMetadataStrategy: _CurrentRouteDataKeyPathMetadataStrategy,
private readonly openGraphProfileService: OpenGraphProfileService,
) {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MetadataRouteStrategy } from '../../routing/metadata-route-strategy'
import { _MetadataRouteStrategy } from 'ngx-metadata/routing'
import { OpenGraphProfile } from '../open-graph-profile'

export abstract class OpenGraphProfileRouteStrategy extends MetadataRouteStrategy<OpenGraphProfile> {}
export abstract class OpenGraphProfileRouteStrategy extends _MetadataRouteStrategy<OpenGraphProfile> {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { TestBed } from '@angular/core/testing'

import { OpenGraphGeneralMetadataListenerService } from './open-graph-general-metadata-listener.service'
import { EventEmitter, Provider } from '@angular/core'
import { GeneralMetadata, GeneralMetadataImage } from '../general-metadata'
import {
_GeneralMetadataAppliersService,
GeneralMetadata,
GeneralMetadataImage,
} from 'ngx-metadata/general-metadata'
import { MockProvider } from 'ng-mocks'
import { GeneralMetadataAppliersService } from '../general-metadata/general-metadata-appliers.service'
import { OpenGraphAppliersService } from './open-graph-appliers.service'
import { enableAutoSpy } from 'ngx-metadata/__tests__/enable-auto-spy'

Expand Down Expand Up @@ -150,7 +153,7 @@ function makeSut(

if (!opts.generalMetadataUnavailable) {
providers.push(
MockProvider(GeneralMetadataAppliersService, {
MockProvider(_GeneralMetadataAppliersService, {
changes$: opts.changes$ ?? new EventEmitter(),
}),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Injectable, OnDestroy, Optional } from '@angular/core'
import { Subscription } from 'rxjs'
import { GeneralMetadataAppliersService } from '../general-metadata/general-metadata-appliers.service'
import { GeneralMetadata } from '../general-metadata'
import {
_GeneralMetadataAppliersService,
GeneralMetadata,
} from 'ngx-metadata/general-metadata'
import { OpenGraphAppliersService } from './open-graph-appliers.service'

@Injectable()
Expand All @@ -10,7 +12,7 @@ export class OpenGraphGeneralMetadataListenerService implements OnDestroy {

constructor(
@Optional()
private readonly generalMetadataAppliersService: GeneralMetadataAppliersService | null,
private readonly generalMetadataAppliersService: _GeneralMetadataAppliersService | null,
private readonly appliers: OpenGraphAppliersService,
) {}

Expand Down
4 changes: 2 additions & 2 deletions projects/ngx-metadata/src/lib/open-graph/open-graph.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Inject, ModuleWithProviders, NgModule, Optional } from '@angular/core'
import { OpenGraphService } from './open-graph.service'
import { OpenGraphApplierService } from './open-graph-applier.service'
import { OpenGraphAppliersService } from './open-graph-appliers.service'
import { MetadataRouteStrategy } from '../routing/metadata-route-strategy'
import { _MetadataRouteStrategy } from 'ngx-metadata/routing'
import { DefaultOpenGraphRouteStrategy } from './routing/default-open-graph-route-strategy'
import { OpenGraphGeneralMetadataListenerService } from './open-graph-general-metadata-listener.service'
import { OpenGraphRouteStrategy } from './routing/open-graph-route-strategy'
Expand Down Expand Up @@ -48,7 +48,7 @@ export class OpenGraphModule {
useClass: DefaultOpenGraphRouteStrategy,
},
{
provide: MetadataRouteStrategy,
provide: _MetadataRouteStrategy,
useExisting: OpenGraphRouteStrategy,
multi: true,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { OpenGraph } from '../open-graph'
import { MAIN_KEY } from '../../routing/current-route-data-key-path-metadata-strategy'
import { _MAIN_KEY } from 'ngx-metadata/routing'
import { KEY } from './default-open-graph-route-strategy'

export interface DefaultOpenGraphRouteData {
[MAIN_KEY]: {
[_MAIN_KEY]: {
[KEY]: OpenGraph
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { MockProvider, MockService } from 'ng-mocks'
import { OpenGraph } from '../open-graph'
import { OpenGraphType } from '../open-graph-type'
import { ActivatedRouteSnapshot } from '@angular/router'
import { GeneralMetadataRouteDataService } from '../../general-metadata/routing/general-metadata-route-data.service'
import {
_GeneralMetadataRouteDataService,
GeneralMetadata,
} from 'ngx-metadata/general-metadata'
import { Provider } from '@angular/core'
import { GeneralMetadata } from '../../general-metadata'
import { _DefaultsService } from 'ngx-metadata/common'
import { CurrentRouteDataKeyPathMetadataStrategy } from '../../routing/current-route-data-key-path-metadata-strategy'
import { _CurrentRouteDataKeyPathMetadataStrategy } from 'ngx-metadata/routing'
import { OpenGraphService } from '../open-graph.service'
import { enableAutoSpy } from 'ngx-metadata/__tests__/enable-auto-spy'

Expand All @@ -28,8 +30,8 @@ describe('DefaultOpenGraphRouteStrategy', () => {
it('should resolve route data using proper key path', () => {
const sut = makeSut()
const currentRouteDataKeyPathMetadataStrategy = TestBed.inject(
CurrentRouteDataKeyPathMetadataStrategy,
) as jasmine.SpyObj<CurrentRouteDataKeyPathMetadataStrategy>
_CurrentRouteDataKeyPathMetadataStrategy,
) as jasmine.SpyObj<_CurrentRouteDataKeyPathMetadataStrategy>
currentRouteDataKeyPathMetadataStrategy.resolve.and.returnValue(
dummyMetadata,
)
Expand All @@ -45,8 +47,8 @@ describe('DefaultOpenGraphRouteStrategy', () => {
it('should return metadata from route using simple strategy', () => {
const sut = makeSut()
const currentRouteDataKeyPathMetadataStrategy = TestBed.inject(
CurrentRouteDataKeyPathMetadataStrategy,
) as jasmine.SpyObj<CurrentRouteDataKeyPathMetadataStrategy>
_CurrentRouteDataKeyPathMetadataStrategy,
) as jasmine.SpyObj<_CurrentRouteDataKeyPathMetadataStrategy>
currentRouteDataKeyPathMetadataStrategy.resolve.and.returnValue(
dummyMetadata,
)
Expand Down Expand Up @@ -77,19 +79,19 @@ describe('DefaultOpenGraphRouteStrategy', () => {
applicationName: 'general metadata application name',
}
let sut: DefaultOpenGraphRouteStrategy
let currentRouteDataKeyPathMetadataStrategy: jasmine.SpyObj<CurrentRouteDataKeyPathMetadataStrategy>
let currentRouteDataKeyPathMetadataStrategy: jasmine.SpyObj<_CurrentRouteDataKeyPathMetadataStrategy>

beforeEach(() => {
sut = makeSut({ generalMetadata: true })
const generalMetadataRouteDataService = TestBed.inject(
GeneralMetadataRouteDataService,
) as jasmine.SpyObj<GeneralMetadataRouteDataService>
_GeneralMetadataRouteDataService,
) as jasmine.SpyObj<_GeneralMetadataRouteDataService>
generalMetadataRouteDataService.resolve.and.returnValue(
compatibleGeneralMetadata,
)
currentRouteDataKeyPathMetadataStrategy = TestBed.inject(
CurrentRouteDataKeyPathMetadataStrategy,
) as jasmine.SpyObj<CurrentRouteDataKeyPathMetadataStrategy>
_CurrentRouteDataKeyPathMetadataStrategy,
) as jasmine.SpyObj<_CurrentRouteDataKeyPathMetadataStrategy>
})

describe('when no open graph data can be resolved', () => {
Expand Down Expand Up @@ -171,13 +173,13 @@ describe('DefaultOpenGraphRouteStrategy', () => {
function makeSut(opts: { generalMetadata?: boolean } = {}) {
const providers: Provider[] = [
DefaultOpenGraphRouteStrategy,
MockProvider(CurrentRouteDataKeyPathMetadataStrategy),
MockProvider(_CurrentRouteDataKeyPathMetadataStrategy),
_DefaultsService,
MockProvider(OpenGraphService),
]

if (opts.generalMetadata) {
providers.push(MockProvider(GeneralMetadataRouteDataService))
providers.push(MockProvider(_GeneralMetadataRouteDataService))
}

TestBed.configureTestingModule({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { Injectable, Optional } from '@angular/core'
import { OpenGraph } from '../open-graph'
import { ActivatedRouteSnapshot } from '@angular/router'
import { GeneralMetadataRouteDataService } from '../../general-metadata/routing/general-metadata-route-data.service'
import { _DefaultsService } from 'ngx-metadata/common'
import { OpenGraphRouteStrategy } from './open-graph-route-strategy'
import { CurrentRouteDataKeyPathMetadataStrategy } from '../../routing/current-route-data-key-path-metadata-strategy'
import { _CurrentRouteDataKeyPathMetadataStrategy } from 'ngx-metadata/routing'
import { OpenGraphService } from '../open-graph.service'
import { _GeneralMetadataRouteDataService } from 'ngx-metadata/general-metadata'

export const KEY = 'openGraph'
export const KEY_PATH = `${KEY}`

@Injectable()
export class DefaultOpenGraphRouteStrategy implements OpenGraphRouteStrategy {
constructor(
private readonly currentRouteDataKeyPathMetadataStrategy: CurrentRouteDataKeyPathMetadataStrategy,
private readonly currentRouteDataKeyPathMetadataStrategy: _CurrentRouteDataKeyPathMetadataStrategy,
@Optional()
private readonly generalMetadataRouteData: GeneralMetadataRouteDataService | null,
private readonly generalMetadataRouteData: _GeneralMetadataRouteDataService | null,
private readonly defaultsService: _DefaultsService,
private readonly openGraphService: OpenGraphService,
) {}
Expand Down
Loading

0 comments on commit a791e4f

Please sign in to comment.