Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor passing openai api key #5

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "White-label AI chat service ready to be embedded on your site",
"scripts": {
"test": "OPENAI_API_KEY=dummy jest",
"test": "jest",
"prepare": "husky install",
"lint": "eslint .",
"pretty": "prettier --check --ignore-unknown .",
Expand Down
2 changes: 1 addition & 1 deletion src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import api from './api';

describe('api', () => {
it('responds with hello, world!', async () => {
const server = api({ title: 'my awesome service' });
const server = api({ title: 'my awesome service', openAiApiKey: 'dummy' });
const response = await server.inject({
method: 'GET',
url: '/api'
Expand Down
4 changes: 2 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Static, Type } from '@sinclair/typebox';
import { FastifyPluginCallback } from 'fastify';
import apiService from './api_service';

const openAiApiKey = process.env.OPENAI_API_KEY as string;
const HelloWorld = Type.String({
description: 'The magical words!'
});
Expand Down Expand Up @@ -40,6 +39,7 @@ const healthcheck: FastifyPluginCallback<HealthcheckOptions> = (

export interface ApiOptions {
title: string;
openAiApiKey: string;
}

export default (opts: ApiOptions) => {
Expand Down Expand Up @@ -69,7 +69,7 @@ export default (opts: ApiOptions) => {

api.register(apiService, {
prefix: '/api/v1',
openAiApiKey: openAiApiKey as string
openAiApiKey: opts.openAiApiKey
});

return api;
Expand Down
9 changes: 8 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import fastifyStatic from '@fastify/static';
import path from 'path';
import api from './api';

const server = api({ title: 'White Label AI chat service' });
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
if (!OPENAI_API_KEY) {
throw new Error('OPENAI_API_KEY is required');
}
const server = api({
title: 'White Label AI chat service',
openAiApiKey: OPENAI_API_KEY
});

server.register(fastifyStatic, {
root: path.join(__dirname, '../out'),
Expand Down
Loading