import express from "express";
import smart from "fhirclient";
import { fhirclient } from "fhirclient/lib/types";
import session from "express-session";
import dotenv from "dotenv";
dotenv.config();
const app = express();
export class StorageWrapper {
session: any;
constructor(session: any) {
this.session = session;
}
async get(key: string): Promise<any> {
return this.session[key];
}
async set(key: string, value: any): Promise<any> {
this.session[key] = value;
return value;
}
async unset(key: string): Promise<boolean> {
if (Object.prototype.hasOwnProperty.call(this.session, key)) {
delete this.session[key];
return true;
}
return false;
}
}
app.use(express.json());
app.use(
session({
secret: "my-secret-key",
resave: false,
saveUninitialized: true,
cookie: { secure: false },
})
);
app.get("/", async (req, res) => {
const authorizeParams: fhirclient.AuthorizeParams = {
clientId: "whatever",
clientSecret: "my secret",
scope: "user/*.* openid fhirUser offline_access",
};
await smart(req, res, new StorageWrapper(req.session)).authorize(authorizeParams);
});
app.get("/api/integration/fhir/callback", async (req, res) => {
const client = await smart(req, res, new StorageWrapper(req.session)).ready();
const encountersBundle = await client.request("Encounter");
console.log(encountersBundle);
// Throws "Error: Patient is not available"
});
app.listen(3000, () => {
console.log(`Server is running on http://localhost:3000`);
});