This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable $docref based on the compiled IGs (#83)
- Loading branch information
Showing
11 changed files
with
320 additions
and
58 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
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
68 changes: 68 additions & 0 deletions
68
src/operationDefinitions/OperationDefinitionRegistry.test.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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
import { Router } from 'express'; | ||
import ConfigHandler from '../configHandler'; | ||
import { OperationDefinitionRegistry } from './OperationDefinitionRegistry'; | ||
import { OperationDefinitionImplementation } from './types'; | ||
import ResourceHandler from '../router/handlers/resourceHandler'; | ||
|
||
const fakeRouter = (jest.fn() as unknown) as Router; | ||
const fakeOperation: OperationDefinitionImplementation = { | ||
canonicalUrl: 'https://fwoa.com/operation/fakeOperation', | ||
httpVerbs: ['GET'], | ||
path: '/Patient/fakeOperation', | ||
targetResourceType: 'Patient', | ||
requestInformation: { | ||
operation: 'read', | ||
resourceType: 'Patient', | ||
}, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
buildRouter: (resourceHandler: ResourceHandler) => fakeRouter, | ||
}; | ||
describe('OperationDefinitionRegistry', () => { | ||
test('getAllRouters', () => { | ||
const configHandlerMock = { | ||
getResourceHandler: jest.fn().mockReturnValue({}), | ||
}; | ||
|
||
const operationDefinitionRegistry = new OperationDefinitionRegistry( | ||
(configHandlerMock as unknown) as ConfigHandler, | ||
[fakeOperation], | ||
); | ||
|
||
expect(operationDefinitionRegistry.getAllRouters()).toHaveLength(1); | ||
expect(operationDefinitionRegistry.getAllRouters()[0]).toBe(fakeRouter); | ||
}); | ||
|
||
test('getOperation', () => { | ||
const configHandlerMock = { | ||
getResourceHandler: jest.fn().mockReturnValue({}), | ||
}; | ||
|
||
const operationDefinitionRegistry = new OperationDefinitionRegistry( | ||
(configHandlerMock as unknown) as ConfigHandler, | ||
[fakeOperation], | ||
); | ||
|
||
expect(operationDefinitionRegistry.getOperation('PATCH', '/Patient/fakeOperation')).toBeUndefined(); | ||
expect(operationDefinitionRegistry.getOperation('GET', '/Patient/someOtherOperation')).toBeUndefined(); | ||
|
||
expect(operationDefinitionRegistry.getOperation('GET', '/Patient/fakeOperation')).toBe(fakeOperation); | ||
}); | ||
|
||
test('ResourceHandler not available', () => { | ||
const configHandlerMock = { | ||
getResourceHandler: jest.fn().mockReturnValue(undefined), | ||
}; | ||
|
||
expect( | ||
() => new OperationDefinitionRegistry((configHandlerMock as unknown) as ConfigHandler, [fakeOperation]), | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"Failed to initialize operation https://fwoa.com/operation/fakeOperation. Is your FhirConfig correct?"`, | ||
); | ||
}); | ||
}); |
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,38 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
import { Router } from 'express'; | ||
import { OperationDefinitionImplementation } from './types'; | ||
import ConfigHandler from '../configHandler'; | ||
|
||
export class OperationDefinitionRegistry { | ||
private readonly operations: OperationDefinitionImplementation[]; | ||
|
||
private readonly routers: Router[]; | ||
|
||
constructor(configHandler: ConfigHandler, operations: OperationDefinitionImplementation[]) { | ||
this.operations = operations; | ||
|
||
this.routers = operations.map(operation => { | ||
const resourceHandler = configHandler.getResourceHandler(operation.targetResourceType); | ||
if (!resourceHandler) { | ||
throw new Error( | ||
`Failed to initialize operation ${operation.canonicalUrl}. Is your FhirConfig correct?`, | ||
); | ||
} | ||
console.log(`Enabling operation ${operation.canonicalUrl} at ${operation.path}`); | ||
return operation.buildRouter(resourceHandler); | ||
}); | ||
} | ||
|
||
getOperation(method: string, path: string): OperationDefinitionImplementation | undefined { | ||
return this.operations.find(o => o.path === path && o.httpVerbs.includes(method)); | ||
} | ||
|
||
getAllRouters(): Router[] { | ||
return this.routers; | ||
} | ||
} |
Oops, something went wrong.