Skip to content

Commit

Permalink
Worked on linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyDevelopment committed Sep 10, 2024
1 parent f6612ad commit 835372a
Show file tree
Hide file tree
Showing 23 changed files with 168 additions and 68 deletions.
6 changes: 6 additions & 0 deletions libs/ngrx-hateoas/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ module.exports = tseslint.config(
style: "kebab-case",
},
],
"@typescript-eslint/consistent-type-definitions": [
"off"
],
"@typescript-eslint/no-unsafe-function-type": [
"off"
]
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion libs/ngrx-hateoas/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@angular-architects/ngrx-hateoas",
"version": "18.0.0-rc.1",
"version": "18.0.0-rc.2",
"peerDependencies": {
"@angular/common": "^18.0.0",
"@angular/core": "^18.0.0",
Expand Down
5 changes: 2 additions & 3 deletions libs/ngrx-hateoas/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ export interface ResourceSocket {
href: string
}

export type Resource = {
}
export type Resource = Record<string, unknown>;

export type DynamicResourceValue = DynamicResource | Resource | number | string | boolean | DynamicResource[] | Resource[] | number[] | boolean[] | null;

export type DynamicResource = Resource & {
[key: string]: DynamicResourceValue;
[key: string]: DynamicResource | number | string | boolean | DynamicResource[] | number[] | boolean[] | null;
}

export const initialDynamicResource: DynamicResource = {};
4 changes: 2 additions & 2 deletions libs/ngrx-hateoas/src/lib/pipes/get-action.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TestBed } from '@angular/core/testing';
import { GetActionPipe } from './get-action.pipe';
import { HateoasConfig, HateoasService } from '../services/hateoas.service';
import { HateoasService } from '../services/hateoas.service';

const testModel = {
_actions: {
Expand All @@ -13,7 +13,7 @@ describe('GetActionPipe', () => {
let getActionPipe: GetActionPipe;

beforeEach(() => {
TestBed.configureTestingModule({ providers: [ GetActionPipe, HateoasService, HateoasConfig ]});
TestBed.configureTestingModule({ providers: [ GetActionPipe, HateoasService ]});
getActionPipe = TestBed.inject(GetActionPipe);
});

Expand Down
4 changes: 2 additions & 2 deletions libs/ngrx-hateoas/src/lib/pipes/get-link.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TestBed } from '@angular/core/testing';
import { HateoasConfig, HateoasService } from '../services/hateoas.service';
import { HateoasService } from '../services/hateoas.service';
import { GetLinkPipe } from './get-link.pipe';

const testModel = {
Expand All @@ -13,7 +13,7 @@ describe('GetLinkPipe', () => {
let getLinkPipe: GetLinkPipe;

beforeEach(() => {
TestBed.configureTestingModule({ providers: [ GetLinkPipe, HateoasService, HateoasConfig ]});
TestBed.configureTestingModule({ providers: [ GetLinkPipe, HateoasService ]});
getLinkPipe = TestBed.inject(GetLinkPipe);
});

Expand Down
4 changes: 2 additions & 2 deletions libs/ngrx-hateoas/src/lib/pipes/get-socket.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TestBed } from '@angular/core/testing';
import { HateoasConfig, HateoasService } from '../services/hateoas.service';
import { HateoasService } from '../services/hateoas.service';
import { GetSocketPipe } from './get-socket.pipe';

const testModel = {
Expand All @@ -13,7 +13,7 @@ describe('GetSocketPipe', () => {
let getSocketPipe: GetSocketPipe;

beforeEach(() => {
TestBed.configureTestingModule({ providers: [ GetSocketPipe, HateoasService, HateoasConfig ]});
TestBed.configureTestingModule({ providers: [ GetSocketPipe, HateoasService ]});
getSocketPipe = TestBed.inject(GetSocketPipe);
});

Expand Down
4 changes: 2 additions & 2 deletions libs/ngrx-hateoas/src/lib/pipes/has-action.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HasActionPipe } from './has-action.pipe';
import { TestBed } from '@angular/core/testing';
import { HateoasConfig, HateoasService } from '../services/hateoas.service';
import { HateoasService } from '../services/hateoas.service';

const testModel = {
_actions: {
Expand All @@ -13,7 +13,7 @@ describe('HasActionPipe', () => {
let hasActionPipe: HasActionPipe

beforeEach(() => {
TestBed.configureTestingModule({ providers: [ HasActionPipe, HateoasService, HateoasConfig ]});
TestBed.configureTestingModule({ providers: [ HasActionPipe, HateoasService ]});
hasActionPipe = TestBed.inject(HasActionPipe);
});

Expand Down
2 changes: 1 addition & 1 deletion libs/ngrx-hateoas/src/lib/pipes/has-action.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class HasActionPipe implements PipeTransform {

private hateoasService = inject(HateoasService);

transform(resource: any, actionName: string): boolean {
transform(resource: unknown, actionName: string): boolean {
return this.hateoasService.getAction(resource, actionName) !== undefined;
}

Expand Down
4 changes: 2 additions & 2 deletions libs/ngrx-hateoas/src/lib/pipes/has-link.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TestBed } from '@angular/core/testing';
import { HateoasConfig, HateoasService } from '../services/hateoas.service';
import { HateoasService } from '../services/hateoas.service';
import { HasLinkPipe } from './has-link.pipe';

const testModel = {
Expand All @@ -13,7 +13,7 @@ describe('HasLinkPipe', () => {
let hasLinkPipe: HasLinkPipe;

beforeEach(() => {
TestBed.configureTestingModule({ providers: [ HasLinkPipe, HateoasService, HateoasConfig ]});
TestBed.configureTestingModule({ providers: [ HasLinkPipe, HateoasService ]});
hasLinkPipe = TestBed.inject(HasLinkPipe);
});

Expand Down
2 changes: 1 addition & 1 deletion libs/ngrx-hateoas/src/lib/pipes/has-link.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class HasLinkPipe implements PipeTransform {

private hateoasService = inject(HateoasService);

transform(resource: any, linkName: string): boolean {
transform(resource: unknown, linkName: string): boolean {
return this.hateoasService.getLink(resource, linkName ) !== undefined;
}

Expand Down
4 changes: 2 additions & 2 deletions libs/ngrx-hateoas/src/lib/pipes/has-socket.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TestBed } from '@angular/core/testing';
import { HateoasConfig, HateoasService } from '../services/hateoas.service';
import { HateoasService } from '../services/hateoas.service';
import { HasSocketPipe } from './has-socket.pipe';

const testModel = {
Expand All @@ -13,7 +13,7 @@ describe('HasSocketPipe', () => {
let hasSocketPipe: HasSocketPipe;

beforeEach(() => {
TestBed.configureTestingModule({ providers: [ HasSocketPipe, HateoasService, HateoasConfig ]});
TestBed.configureTestingModule({ providers: [ HasSocketPipe, HateoasService ]});
hasSocketPipe = TestBed.inject(HasSocketPipe);
});

Expand Down
2 changes: 1 addition & 1 deletion libs/ngrx-hateoas/src/lib/pipes/has-socket.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class HasSocketPipe implements PipeTransform {

private hateoasService = inject(HateoasService);

transform(resource: any, socketName: string): boolean {
transform(resource: unknown, socketName: string): boolean {
return this.hateoasService.getSocket(resource, socketName) !== undefined;
}

Expand Down
91 changes: 91 additions & 0 deletions libs/ngrx-hateoas/src/lib/provide.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { TestBed } from "@angular/core/testing";
import { AntiForgeryOptions, CustomHeadersOptions, HATEOAS_ANTI_FORGERY, HATEOAS_CUSTOM_HEADERS, HATEOAS_LOGIN_REDIRECT, HATEOAS_METADATA_PROVIDER, LoginRedirectOptions, MetadataProvider, provideHateoas, withAntiForgery, withCustomHeaders, withLoginRedirect, withMetadataProvider } from "./provide";
import { DynamicResource, DynamicResourceValue, ResourceAction, ResourceLink, ResourceSocket } from "./models";

describe('provideHateaos', () => {

beforeEach(() => {
TestBed.resetTestingModule();
});

describe('withAntiForgery', () => {

it('registers custom anti forgery options in injection context', () => {

const dummyAntiForgeryOptions: AntiForgeryOptions = {
cookieName: 'foo',
headerName: 'bar'
};

TestBed.configureTestingModule({ providers: [ provideHateoas(withAntiForgery(dummyAntiForgeryOptions)) ]});
const antiForgeryOptions = TestBed.inject(HATEOAS_ANTI_FORGERY);

expect(antiForgeryOptions.cookieName).toBe(dummyAntiForgeryOptions.cookieName);
expect(antiForgeryOptions.headerName).toBe(dummyAntiForgeryOptions.headerName);
});

});

describe('withLoginRedirect', () => {

it('registers custom login redirect options in injection context', () => {

const dummyLoginRedirectOptions: LoginRedirectOptions = {
loginUrl: 'foo',
redirectUrlParamName: 'bar'
};

TestBed.configureTestingModule({ providers: [ provideHateoas(withLoginRedirect(dummyLoginRedirectOptions)) ]});
const loginRedirectOptoins = TestBed.inject(HATEOAS_LOGIN_REDIRECT);

expect(loginRedirectOptoins.loginUrl).toBe(dummyLoginRedirectOptions.loginUrl);
expect(loginRedirectOptoins.redirectUrlParamName).toBe(dummyLoginRedirectOptions.redirectUrlParamName);
});

});

describe('withCustomHeaders', () => {

it('registers custom header options in injection context', () => {

const dummyCustomHeaderOptions: CustomHeadersOptions = {
headers: {
foo: 'bar'
}
};

TestBed.configureTestingModule({ providers: [ provideHateoas(withCustomHeaders(dummyCustomHeaderOptions)) ]});
const customHeaderOptions = TestBed.inject(HATEOAS_CUSTOM_HEADERS);

expect(customHeaderOptions.headers).toBe(dummyCustomHeaderOptions.headers);
});

});

describe('withMedatadaProvider', () => {

it('registers custom metadataprovider in injection context', () => {

const dummyMetadataProvider: MetadataProvider = {
linkLookup(resource, linkName) {
return { href: (resource as any)['myMeta'][`_link_${linkName}`] } satisfies ResourceLink;
},
actionLookup(resource, actionName) {
return { href: (resource as any)['myMeta'][`_action_${actionName}`], method: 'PUT' } satisfies ResourceAction;
},
socketLookup(resource, socketName) {
return { href: (resource as any)['myMeta'][`_socket_${socketName}`], method: 'update' } satisfies ResourceSocket;
}
}

TestBed.configureTestingModule({ providers: [ provideHateoas(withMetadataProvider(dummyMetadataProvider)) ]});
const metadataProvider = TestBed.inject(HATEOAS_METADATA_PROVIDER);

expect(metadataProvider.linkLookup).toBe(dummyMetadataProvider.linkLookup);
expect(metadataProvider.actionLookup).toBe(dummyMetadataProvider.actionLookup);
expect(metadataProvider.socketLookup).toBe(dummyMetadataProvider.socketLookup);
});

});

});
26 changes: 19 additions & 7 deletions libs/ngrx-hateoas/src/lib/provide.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EnvironmentProviders, InjectionToken, makeEnvironmentProviders, Provider } from "@angular/core";
import { HateoasConfig, HateoasService } from "./services/hateoas.service";
import { HateoasService } from "./services/hateoas.service";
import { RequestService } from "./services/request.service";
import { ResourceAction, ResourceLink, ResourceSocket } from "./models";
import { DynamicResource, Resource, ResourceAction, ResourceLink, ResourceSocket } from "./models";

export enum HateoasFeatureKind {
AntiForgery,
Expand Down Expand Up @@ -49,9 +49,24 @@ export interface MetadataProvider {
socketLookup(resource: unknown, socketName: string): ResourceSocket | undefined;
}

function isResource(resource: unknown): resource is DynamicResource {
return typeof resource === 'object' && resource !== null;
}

function isResourceLinkCollection(resourceLinks: unknown): resourceLinks is Record<string, ResourceLink> {
return typeof resourceLinks === 'object' && resourceLinks !== null;
}

function isResourceLink(resourceLink: unknown): resourceLink is ResourceLink {
return typeof resourceLink === 'object' && resourceLink !== null && 'href' in resourceLink;
}

const defaultMetadataProvider: MetadataProvider = {
linkLookup(resource: unknown, linkName: string): ResourceLink | undefined {
return (resource as any)?._links?.[linkName];
if(isResource(resource) && isResourceLinkCollection(resource['_links'] && isResourceLink(resource['_links'][linkName])))
return resource['_links'][linkName];
else
return undefined;
},
actionLookup(resource: unknown, actionName: string): ResourceAction | undefined {
return (resource as any)?._actions?.[actionName];
Expand Down Expand Up @@ -94,7 +109,7 @@ export function withCustomHeaders(options?: CustomHeadersOptions): HateoasFeatur
};
}

export const HATEOAS_METADATA_PROVIDER = new InjectionToken<MetadataProvider>('HATEOAS_METADATA_PROVIDER');
export const HATEOAS_METADATA_PROVIDER = new InjectionToken<MetadataProvider>('HATEOAS_METADATA_PROVIDER', { providedIn: 'root', factory: () => defaultMetadataProvider });

export function withMetadataProvider(provider?: MetadataProvider): HateoasFeature {
return {
Expand All @@ -107,9 +122,6 @@ export function withMetadataProvider(provider?: MetadataProvider): HateoasFeatur

export function provideHateoas(...features: HateoasFeature[]): EnvironmentProviders {
return makeEnvironmentProviders([{
provide: HateoasConfig,
useValue: new HateoasConfig()
}, {
provide: HateoasService,
useClass: HateoasService
}, {
Expand Down
9 changes: 5 additions & 4 deletions libs/ngrx-hateoas/src/lib/services/hateoas.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { TestBed } from '@angular/core/testing';
import { HateoasConfig, HateoasService } from './hateoas.service';
import { HateoasService } from './hateoas.service';
import { ResourceAction, ResourceLink, ResourceSocket } from '../models';
import { HATEOAS_METADATA_PROVIDER, MetadataProvider } from '../provide';

const dummyHateoasConfig: HateoasConfig = {
const dummyHateoasMetadataProvider: MetadataProvider = {
linkLookup(resource, linkName) {
return { href: (resource as any)['myMeta'][`_link_${linkName}`] } satisfies ResourceLink;
},
Expand All @@ -11,7 +12,7 @@ const dummyHateoasConfig: HateoasConfig = {
},
socketLookup(resource, socketName) {
return { href: (resource as any)['myMeta'][`_socket_${socketName}`], method: 'update' } satisfies ResourceSocket;
},
}
}

const testObj = {
Expand All @@ -27,7 +28,7 @@ describe('HateoasService', () => {
let hateoasService: HateoasService;

beforeEach(() => {
TestBed.configureTestingModule({ providers: [ { provide: HateoasConfig, useValue: dummyHateoasConfig }, HateoasService ]});
TestBed.configureTestingModule({ providers: [ { provide: HATEOAS_METADATA_PROVIDER, useValue: dummyHateoasMetadataProvider }, HateoasService ]});
hateoasService = TestBed.inject(HateoasService);
});

Expand Down
15 changes: 2 additions & 13 deletions libs/ngrx-hateoas/src/lib/services/hateoas.service.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
import { Injectable, inject } from "@angular/core";
import { ResourceAction, ResourceLink, ResourceSocket } from "../models";

export class HateoasConfig {
linkLookup(resource: unknown, linkName: string): ResourceLink | undefined {
return (resource as any)?._links?.[linkName];
}
actionLookup(resource: unknown, actionName: string): ResourceAction | undefined {
return (resource as any)?._actions?.[actionName];
}
socketLookup(resource: unknown, socketName: string): ResourceSocket | undefined {
return (resource as any)?._sockets?.[socketName];
}
}
import { HATEOAS_METADATA_PROVIDER } from "../provide";

@Injectable()
export class HateoasService {
private hateoasConfig = inject(HateoasConfig);
private hateoasConfig = inject(HATEOAS_METADATA_PROVIDER);

getLink(resource: unknown, linkName: string): ResourceLink | undefined {
return this.hateoasConfig.linkLookup(resource, linkName);
Expand Down
4 changes: 2 additions & 2 deletions libs/ngrx-hateoas/src/lib/services/request.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('RequestService', () => {

currentLocation = '/angular/route'

let windowsStub = {
const windowsStub = {
location: {
set href(value: string) { currentLocation = value; },
get href() { return currentLocation; }
Expand Down Expand Up @@ -207,7 +207,7 @@ describe('RequestService', () => {
let httpTestingController: HttpTestingController;

beforeEach(() => {
let customHeaderOptions: CustomHeadersOptions = {
const customHeaderOptions: CustomHeadersOptions = {
headers: {
'X-Foo': 'Bar'
}
Expand Down
Loading

0 comments on commit 835372a

Please sign in to comment.