openai-api-mock 0.1.28 → 0.1.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ import fetch from 'node-fetch';
2
+ import { mockOpenAIResponse, stopMocking } from '../src/index.js';
3
+ import OpenAI from 'openai';
4
+
5
+ const openai = new OpenAI({ apiKey: "OPENAI_API_KEY" });
6
+
7
+ describe('Mock OpenAI Chat with a delay', () => {
8
+
9
+ beforeEach(() => {
10
+ mockOpenAIResponse(true, { latency: 1000, logRequests: true });
11
+ });
12
+
13
+ afterEach(() => {
14
+ stopMocking();
15
+ });
16
+
17
+ test('should mock OpenAI Chat with a delay', async () => {
18
+ const response = await openai.chat.completions.create({
19
+ model: "gpt-3.5-mock",
20
+ messages: [
21
+ { role: 'system', content: "You're an expert chef" },
22
+ { role: 'user', content: "Suggest at least 5 recipes" },
23
+ ]
24
+ });
25
+
26
+ expect(response).toBeDefined();
27
+
28
+ });
29
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openai-api-mock",
3
- "version": "0.1.28",
3
+ "version": "0.1.30",
4
4
  "description": "A Node.js module for mocking OpenAI API responses in a development environment",
5
5
  "repository": {
6
6
  "type": "git",
@@ -39,7 +39,7 @@
39
39
  },
40
40
  "dependencies": {
41
41
  "@faker-js/faker": "^9.0.0",
42
- "nock": "^13.5.4"
42
+ "nock": "^14.0.0"
43
43
  },
44
44
  "devDependencies": {
45
45
  "jest": "^29.7.0",
package/src/index.js CHANGED
@@ -30,12 +30,10 @@ export function mockOpenAIResponse(force = false, options = {}) {
30
30
  return { isActive: false, stopMocking };
31
31
  }
32
32
 
33
- // Add delay if latency is specified
34
- const mockWithDelay = latency > 0 ? nock(OPEN_AI_BASE_URL).delay(latency) : nock(OPEN_AI_BASE_URL);
35
-
36
33
  // Mock chat completions endpoint
37
- const chatScope = mockWithDelay
34
+ const chatScope = nock(OPEN_AI_BASE_URL)
38
35
  .post(CHAT_COMPLETIONS_ENDPOINT)
36
+ .delay(latency) // Add delay to the interceptor
39
37
  .reply(function (uri, requestBody) {
40
38
  if (logRequests) {
41
39
  console.log(`[openai-api-mock] Chat request:`, JSON.stringify(requestBody, null, 2));
@@ -67,8 +65,9 @@ export function mockOpenAIResponse(force = false, options = {}) {
67
65
  });
68
66
 
69
67
  // Mock image generations endpoint
70
- const imageScope = mockWithDelay
68
+ const imageScope = nock(OPEN_AI_BASE_URL)
71
69
  .post(IMAGE_GENERATIONS_ENDPOINT)
70
+ .delay(latency) // Add delay to the interceptor
72
71
  .reply(function (uri, requestBody) {
73
72
  if (logRequests) {
74
73
  console.log(`[openai-api-mock] Image request:`, JSON.stringify(requestBody, null, 2));
package/vite.config.js CHANGED
@@ -30,7 +30,7 @@ export default defineConfig({
30
30
  }
31
31
  }
32
32
  },
33
- target: 'node16',
33
+ target: 'node18',
34
34
  sourcemap: true,
35
35
  minify: false
36
36
  }