Allowing Unmatched Requests in OpenAI Mock API #2574
-
Hey Hope that you are doing well I'm currently working on an OpenAI mock API module using Here's my current implementation: const OPEN_AI_BASE_URL = 'https://api.openai.com';
const CHAT_COMPLETIONS_ENDPOINT = '/v1/chat/completions';
const IMAGE_GENERATIONS_ENDPOINT = '/v1/images/generations';
function mockOpenAIResponse(force = false) {
var env = process.env.NODE_ENV || 'development';
// Intercept the HTTP call and return the mock response
if (env === 'development' || force) {
nock(OPEN_AI_BASE_URL)
.post(CHAT_COMPLETIONS_ENDPOINT)
.reply(function (uri, requestBody) {
return [200, getChatResponce(requestBody)];
});
nock(OPEN_AI_BASE_URL)
.post(IMAGE_GENERATIONS_ENDPOINT)
.reply(function (uri, requestBody) {
return [200, getImageResponce(requestBody)];
});
// Mocking only the chat completion endpoint, not blocking other requests
nock.emitter.on('no match', function (req) {
nock.enableNetConnect(req);
});
}
} I've tried different approaches like using nock.enableNetConnect() after defining mocks, but it doesn't seem to work. How can I modify my code to allow external API requests to pass through and avoid ERR_NOCK_NO_MATCH? Any help or guidance would be greatly appreciated! Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You're taking the wrong approach with your "no match" listener. By that point the net-connect check has already happened. nock.emitter.on('no match', function (req) {
nock.enableNetConnect(req);
}); Try: nock.enableNetConnect(host => host !== "api.openai.com"); This is essentially telling Nock to allow real network connections for any host other than OpenAIs. |
Beta Was this translation helpful? Give feedback.
You're taking the wrong approach with your "no match" listener. By that point the net-connect check has already happened.
Instead of:
Try:
This is essentially telling Nock to allow real network connections for any host other than OpenAIs.