Added the oasstApiClient global var and init

This commit is contained in:
klotske
2023-01-06 13:44:46 +03:00
parent f5b49a952e
commit 3c330dcd13
4 changed files with 15 additions and 8 deletions
@@ -1,4 +1,4 @@
import OasstApiClient from "src/lib/oasst_api_client";
import { OasstApiClient } from "src/lib/oasst_api_client";
describe("Contract test for Oasst API", function () {
// Assumes this is running the mock server.
+12 -1
View File
@@ -1,5 +1,10 @@
import { JWT } from "next-auth/jwt";
declare global {
// eslint-disable-next-line no-var
var oasstApiClient: OasstApiClient | undefined;
}
class OasstError {
message: string;
errorCode: number;
@@ -12,7 +17,7 @@ class OasstError {
}
}
export default class OasstApiClient {
export class OasstApiClient {
constructor(private readonly oasstApiUrl: string, private readonly oasstApiKey: string) {}
private async post(path: string, body: any): Promise<any> {
@@ -85,3 +90,9 @@ export default class OasstApiClient {
});
}
}
export const oasstApiClient =
globalThis.oasstApiClient || new OasstApiClient(process.env.FASTAPI_URL, process.env.FASTAPI_KEY);
if (process.env.NODE_ENV !== "production") {
globalThis.oasstApiClient = oasstApiClient;
}
@@ -1,5 +1,5 @@
import { getToken } from "next-auth/jwt";
import OasstApiClient from "src/lib/oasst_api_client";
import { oasstApiClient } from "src/lib/oasst_api_client";
import prisma from "src/lib/prismadb";
/**
@@ -21,8 +21,6 @@ const handler = async (req, res) => {
return;
}
const oasstApiClient = new OasstApiClient(process.env.FASTAPI_URL, process.env.FASTAPI_KEY);
// Fetch the new task.
const task = await oasstApiClient.fetchTask(task_type, token);
+1 -3
View File
@@ -1,5 +1,5 @@
import { getToken } from "next-auth/jwt";
import OasstApiClient from "src/lib/oasst_api_client";
import { oasstApiClient } from "src/lib/oasst_api_client";
import prisma from "src/lib/prismadb";
/**
@@ -19,8 +19,6 @@ const handler = async (req, res) => {
return;
}
const oasstApiClient = new OasstApiClient(process.env.FASTAPI_URL, process.env.FASTAPI_KEY);
// Parse out the local task ID and the interaction contents.
const { id, content, update_type } = await JSON.parse(req.body);