-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from osher-sade/realities-holder
Realities holder
- Loading branch information
Showing
16 changed files
with
129 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module.exports = { | ||
clearMocks : true, | ||
clearMocks: true, | ||
moduleFileExtensions: [ | ||
"ts", | ||
"js", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/middlewares/middleware-activation-condition/filter-condition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { ResponseMiddlewareParams } from '../middleware'; | ||
|
||
export interface MiddlewareCondition { | ||
shouldBeReturned(params: ResponseMiddlewareParams, isSubEntity?: boolean): boolean; | ||
} |
1 change: 0 additions & 1 deletion
1
src/middlewares/middleware-activation-condition/filter-realities.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
src/middlewares/middleware-activation-condition/filter-soft-delete.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { injectable } from 'inversify'; | ||
import { RealitiesHolder } from './realities-holder'; | ||
|
||
@injectable() | ||
export class DefaultRealitiesHolder extends RealitiesHolder { | ||
getRealities(): Set<number> { | ||
return new Set<number>(); | ||
} | ||
|
||
isRealitySupported(reality: number): boolean { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { UserInputError } from 'apollo-server-koa'; | ||
import { inject, injectable } from 'inversify'; | ||
import { POLARIS_TYPES } from '../inversion-of-control/polaris-types'; | ||
import { RealitiesHolder } from '../main'; | ||
import { PolarisContext } from '../server/polaris-context'; | ||
|
||
@injectable() | ||
export class RealitiesHolderValidator { | ||
constructor(@inject(POLARIS_TYPES.RealitiesHolder) private realitiesHolder: RealitiesHolder) {} | ||
|
||
validateRealitySupport(context: PolarisContext) { | ||
if (context) { | ||
const requestedReality = context.headers.realityId; | ||
if ( | ||
requestedReality != null && | ||
!this.realitiesHolder.isRealitySupported(requestedReality) | ||
) { | ||
throw new UserInputError( | ||
`The requested reality-id ${requestedReality} is not supported!`, | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { injectable } from 'inversify'; | ||
|
||
@injectable() | ||
export abstract class RealitiesHolder { | ||
abstract getRealities(): Set<number>; | ||
|
||
isRealitySupported(reality: number): boolean { | ||
return this.getRealities().has(reality); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { injectable } from 'inversify'; | ||
import { RealitiesHolder } from '../../../src/realities-holder/realities-holder'; | ||
|
||
@injectable() | ||
export class TestRealitiesHolder extends RealitiesHolder { | ||
getRealities(): Set<number> { | ||
return new Set<number>([0, 1, 2, 3, 4, 999]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'reflect-metadata'; | ||
import { PolarisContext, RealitiesHolder } from '../../src/main'; | ||
import { RealitiesHolderValidator } from '../../src/realities-holder/realities-holder-validator'; | ||
|
||
describe('realities-holder-validator tests', () => { | ||
const polarisContextMock: { [T in keyof PolarisContext]: any } = { | ||
headers: { | ||
realityId: jest.fn(), | ||
}, | ||
} as any; | ||
|
||
test('validating supported reality', () => { | ||
const realitiesHolderMock: { [T in keyof RealitiesHolder]: any } = { | ||
isRealitySupported: () => true, | ||
} as any; | ||
|
||
const realitiesHolderValidator = new RealitiesHolderValidator(realitiesHolderMock); | ||
|
||
expect(() => { | ||
realitiesHolderValidator.validateRealitySupport(polarisContextMock); | ||
}).not.toThrow(); | ||
}); | ||
|
||
test('validating unsupported reality', () => { | ||
const realitiesHolderMock: { [T in keyof RealitiesHolder]: any } = { | ||
isRealitySupported: () => false, | ||
} as any; | ||
|
||
const realitiesHolderValidator = new RealitiesHolderValidator(realitiesHolderMock); | ||
|
||
expect(() => { | ||
realitiesHolderValidator.validateRealitySupport(polarisContextMock); | ||
}).toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters