Skip to content

Commit

Permalink
chore: refactor passing openai api key (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
birme authored Dec 5, 2024
1 parent 9c5a64d commit 50aba39
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
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

0 comments on commit 50aba39

Please sign in to comment.