Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
feat: allow async creation of FhirConfig (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
carvantes authored Sep 30, 2021
1 parent ba4cc6f commit 248356f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const OAuthUrl =
? 'https://OAUTH2.com'
: process.env.OAUTH2_DOMAIN_ENDPOINT;

export const fhirConfig: FhirConfig = {
export const getFhirConfig = async (): Promise<FhirConfig> => ({
configVersion: 1.0,
productInfo: {
orgName: 'Organization Name',
Expand Down Expand Up @@ -131,6 +131,6 @@ export const fhirConfig: FhirConfig = {
tenantIdClaimPath: 'custom:tenantId',
}
: undefined,
};
});

export const genericResources = baseResources;
30 changes: 23 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,30 @@

import serverless from 'serverless-http';
import { generateServerlessRouter } from 'fhir-works-on-aws-routing';
import { fhirConfig, genericResources } from './config';
import { getFhirConfig, genericResources } from './config';

const serverlessHandler = serverless(generateServerlessRouter(fhirConfig, genericResources), {
request(request: any, event: any) {
request.user = event.user;
},
});
const ensureAsyncInit = async (initPromise: Promise<any>): Promise<void> => {
try {
await initPromise;
} catch (e) {
console.error('Async initialization failed', e);
// Explicitly exit the process so that next invocation re-runs the init code.
// This prevents Lambda containers from caching a rejected init promise
process.exit(1);
}
};

async function asyncServerless() {
return serverless(generateServerlessRouter(await getFhirConfig(), genericResources), {
request(request: any, event: any) {
request.user = event.user;
},
});
}

const serverlessHandler: Promise<any> = asyncServerless();

export default async (event: any = {}, context: any = {}): Promise<any> => {
return serverlessHandler(event, context);
await ensureAsyncInit(serverlessHandler);
return (await serverlessHandler)(event, context);
};

0 comments on commit 248356f

Please sign in to comment.