openai-api-mock 0.1.24 → 0.1.26

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.
@@ -12,7 +12,7 @@ jobs:
12
12
 
13
13
  strategy:
14
14
  matrix:
15
- node-version: [16.x, 18.x, 20.x]
15
+ node-version: [18.x, 20.x]
16
16
 
17
17
  steps:
18
18
  - uses: actions/checkout@v4
@@ -26,5 +26,8 @@ jobs:
26
26
  - name: Install dependencies
27
27
  run: npm ci
28
28
 
29
- - name: Run tests
30
- run: npm test
29
+ - name: Build package
30
+ run: npm run build
31
+
32
+ - name: Run tests
33
+ run: npm test
@@ -23,8 +23,15 @@ npm install -D openai-api-mock
23
23
  ```
24
24
 
25
25
  ## Usage
26
+
27
+ The module supports both ESM and CommonJS imports:
28
+
26
29
  ```js
30
+ // ESM
27
31
  import { mockOpenAIResponse } from 'openai-api-mock';
32
+
33
+ // CommonJS
34
+ const { mockOpenAIResponse } = require('openai-api-mock');
28
35
  ```
29
36
 
30
37
  Then, call the mockOpenAIResponse function to set up the mock response:
@@ -61,7 +68,7 @@ console.log(mock.isActive);
61
68
  // Stop all mocks
62
69
  mock.stopMocking();
63
70
 
64
- // Add custom endpoint mock
71
+ // Add custom endpoint mock (uses api.openai.com as base url)
65
72
  mock.addCustomEndpoint('POST', '/v1/custom', (uri, body) => {
66
73
  return [200, { custom: 'response' }];
67
74
  });
@@ -0,0 +1,47 @@
1
+ import fetch from 'node-fetch';
2
+ import { mockOpenAIResponse, stopMocking } from '../src/index.js';
3
+
4
+ describe('Mock Custom OpenAI endpoint', () => {
5
+ let mock;
6
+ beforeEach(() => {
7
+ mock = mockOpenAIResponse(true, { logRequests: true });
8
+ });
9
+
10
+ afterEach(() => {
11
+ stopMocking();
12
+ });
13
+
14
+ test('should mock custom endpoint', async () => {
15
+ mock.addCustomEndpoint('POST', '/v1/custom/endpoint', () => {
16
+ return [200, { message: 'Custom endpoint response' }];
17
+ });
18
+ const response = await fetch('https://api.openai.com/v1/custom/endpoint'
19
+ , {
20
+ method: 'POST',
21
+ headers: {
22
+ 'Content-Type': 'application/json',
23
+ },
24
+ }
25
+ );
26
+
27
+ const data = await response.json();
28
+ expect(data).toEqual({ message: 'Custom endpoint response' });
29
+ });
30
+
31
+ test('should mock custom endpoint with error', async () => {
32
+ mock.addCustomEndpoint('POST', '/v1/custom/endpoint', () => {
33
+ return [400, { error: { message: 'Invalid request' } }];
34
+ });
35
+ const response = await fetch('https://api.openai.com/v1/custom/endpoint'
36
+ , {
37
+ method: 'POST',
38
+ headers: {
39
+ 'Content-Type': 'application/json',
40
+ },
41
+ }
42
+ );
43
+
44
+ const data = await response.json();
45
+ expect(data).toEqual({ error: { message: 'Invalid request' } });
46
+ });
47
+ });
@@ -1,5 +1,5 @@
1
1
  import fetch from 'node-fetch';
2
- import { mockOpenAIResponse, stopMocking } from './index.js'
2
+ import { mockOpenAIResponse, stopMocking } from '../src/index.js';
3
3
  import OpenAI from 'openai';
4
4
 
5
5
  const openai = new OpenAI({ apiKey: "OPENAI_API_KEY" });