Skip to content

Commit

Permalink
Merge pull request #3 from trevorpfiz/elektrikspark/ttp-38-convert-to…
Browse files Browse the repository at this point in the history
…-openapi-zod-client-typed-openapi

ttp 38 convert to openapi zod client typed openapi
  • Loading branch information
trevorpfiz authored Nov 30, 2023
2 parents 0080263 + 71755eb commit 7f3245b
Show file tree
Hide file tree
Showing 11 changed files with 14,743 additions and 1,596 deletions.
15 changes: 2 additions & 13 deletions apps/nextjs/src/app/(landing)/_components/all-patients.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,11 @@ export function AllPatients() {
{patients.length > 0 ? (
<ul>
{patients.map((patient) => {
const firstName = patient?.name?.[0] as
| {
use: string;
period: { start: string; end: string };
family: string;
given: string[];
}
| undefined;

return (
<li key={patient?.id}>
<p>ID: {patient?.id}</p>
<p>
Name: {firstName?.family ? `${firstName.family}, ` : ""}
{firstName?.given.join(" ")}
</p>
<p>Given Name: {patient?.name?.[0]?.given}</p>
<p>Family Name: {patient?.name?.[0]?.family}</p>
{/* Include more patient details as needed */}
</li>
);
Expand Down
9 changes: 2 additions & 7 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,23 @@
"clean": "rm -rf .turbo node_modules",
"lint": "eslint .",
"format": "prettier --check . --ignore-path ../../.gitignore",
"typecheck": "tsc --noEmit"
"typecheck": "tsc --noEmit",
"client:generate": "pnpm dlx typed-openapi ./src/canvas/openapi.yaml --output ./src/canvas/canvas-client.ts --runtime zod"
},
"dependencies": {
"@acme/auth": "workspace:^",
"@acme/db": "workspace:^",
"@trpc/client": "^10.43.6",
"@trpc/server": "^10.43.6",
"openapi-typescript-codegen": "^0.25.0",
"superjson": "^2.2.1",
"zod": "^3.22.4"
},
"devDependencies": {
"@acme/eslint-config": "workspace:^",
"@acme/prettier-config": "workspace:^",
"@acme/tsconfig": "workspace:^",
"@types/fhir": "^0.0.40",
"eslint": "^8.54.0",
"openapi-typescript": "^6.7.1",
"openapi-zod-client": "1.13.1",
"prettier": "^3.1.0",
"ts-to-zod": "^3.4.0",
"typed-openapi": "^0.3.0",
"typescript": "^5.3.2"
},
"eslintConfig": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// canvasApi.ts
import { TRPCError } from "@trpc/server";

import { env } from "./env.mjs";
import { env } from "../env.mjs";
import { createApiClient } from "./canvas-client";

interface TokenResponse {
access_token: string;
Expand All @@ -14,7 +12,9 @@ let tokenExpires: Date | null = null;
const baseUrl = env.FUMAGE_BASE_URL.replace("fumage-", "");
const clientId = env.CANVAS_API_CLIENT_ID;
const clientSecret = env.CANVAS_API_CLIENT_SECRET;
const TOKEN_EXPIRATION_BUFFER = 300; // Buffer time in seconds, e.g., 5 minutes

// TODO - use post_GetAnOauthToken?
async function getNewAuthToken(): Promise<string> {
const payload = new URLSearchParams({
grant_type: "client_credentials",
Expand All @@ -39,33 +39,30 @@ async function getNewAuthToken(): Promise<string> {
return token;
}

async function ensureValidToken(): Promise<string> {
if (!token || !tokenExpires || tokenExpires <= new Date()) {
export async function ensureValidToken(): Promise<string> {
if (
!token ||
!tokenExpires ||
new Date() >=
new Date(tokenExpires.getTime() - TOKEN_EXPIRATION_BUFFER * 1000)
) {
return getNewAuthToken();
}
return token;
}

async function makeCanvasRequest<T>(path: string): Promise<T> {
const validToken = await ensureValidToken(); // TODO - is not updating ctx?
const response = await fetch(`${env.FUMAGE_BASE_URL}${path}`, {
headers: { Authorization: `Bearer ${validToken}` },
});
export const api = createApiClient(async (method, url, params) => {
const canvasToken = await ensureValidToken();
const headers = { Authorization: `Bearer ${canvasToken}` };
const options: RequestInit = { method, headers };

if (response.status === 401) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const newToken = await getNewAuthToken();
return makeCanvasRequest(path); // Retry the request with a new token
if (params) {
if (method === "post" || method === "put") {
options.body = JSON.stringify(params);
} else if (method === "get") {
// Handle GET request params if needed
}
}

if (!response.ok) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Failed to fetch patient: ${response.statusText}`,
});
}

return response.json() as Promise<T>;
}

export { ensureValidToken, makeCanvasRequest };
return fetch(url, options).then((res) => res.json());
}, env.FUMAGE_BASE_URL);
Loading

0 comments on commit 7f3245b

Please sign in to comment.