Giter Site home page Giter Site logo

Comments (6)

aprilandjan avatar aprilandjan commented on July 17, 2024 1

@kettanaito sorry to bother, but I'm having the same doubt here. I read the README file and saw that the presets are imported like:

import { interceptXMLHttpRequest } from 'node-request-interceptor/interceptors/XMLHttpRequest'

But the actual file is located at <node-modules>/node-request-interceptor/lib/interceptors/XMLHttpRequest.

I'm using babel to transpile my ES6+ codes, and I have to use:

import { interceptXMLHttpRequest } from 'node-request-interceptor/lib/interceptors/XMLHttpRequest'

to make it work. So is it a mistake of the README document?

from interceptors.

aprilandjan avatar aprilandjan commented on July 17, 2024 1

Hey, @aprilandjan. Yes, that's a mistake in the README.

The presets are located at:

import withDefaultInterceptors from 'node-request-interceptor/lib/presets/default'
import { interceptClientRequest } from 'node-request-interceptor/lib/interceptors/ClientRequest'
import { interceptXMLHttpRequest } from 'node-request-interceptor/lib/interceptors/XMLHttpRequest'

Please, would you be interested to submit a pull request with the fix to the README?

Thanks for the reply! I just opened #91 for this, please review.

from interceptors.

kettanaito avatar kettanaito commented on July 17, 2024

Hey, @zhaoxiaolei519109457.

You can use it according to the API docs.

const { RequestInterceptor } = require('node-request-interceptor')
const withDefaultInterceptors = require('node-request-interceptor/lib/presets/default')

const interceptor = new RequestInterceptor(withDefaultInterceptors)
interceptor.use((req) => {
  // Provision your interception logic 
})

Let me know if this helps.

Note that node-request-interceptor is a low-level API interception library, which means that you'd have to write a considerable amount of boilerplate to use it for requests mocking. Consider using MSW that leverages node-request-interceptor by wrapping it in a more user-friendly setupServer API. It will work for Electron apps as well.

from interceptors.

Z-SmallRock avatar Z-SmallRock commented on July 17, 2024

Thank you for your reply, but it doesn't seem to be the correct answer. The following is the error reported in electro

[Window Title]
Error

[Main Instruction]
A JavaScript error occurred in the main process

[Content]
Uncaught Exception:
Error: Cannot find module 'node-request-interceptor/presets/default'
Require stack:

  • D:\project\electron-test\main.js
  • D:\project\electron-test\node_modules\electron\dist\resources\default_app.asar\main.js
  • at Module._resolveFilename (internal/modules/cjs/loader.js:972:15)
    at Function.n._resolveFilename (electron/js2c/browser_init.js:249:921)
    at Module._load (internal/modules/cjs/loader.js:848:27)
    at Function.f._load (electron/js2c/asar_bundle.js:5:12694)
    at Module.require (internal/modules/cjs/loader.js:1032:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object. (D:\project\electron-test\main.js:20:33)
    at Module._compile (internal/modules/cjs/loader.js:1152:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1173:10)
    at Module.load (internal/modules/cjs/loader.js:992:32)

from interceptors.

kettanaito avatar kettanaito commented on July 17, 2024

My bad, that's a mistake in the import path.

-node-request-interceptor/presets/default
+node-request-interceptor/lib/presets/default

from interceptors.

kettanaito avatar kettanaito commented on July 17, 2024

Hey, @aprilandjan. Yes, that's a mistake in the README.

The presets are located at:

import withDefaultInterceptors from 'node-request-interceptor/lib/presets/default'
import { interceptClientRequest } from 'node-request-interceptor/lib/interceptors/ClientRequest'
import { interceptXMLHttpRequest } from 'node-request-interceptor/lib/interceptors/XMLHttpRequest'

Please, would you be interested to submit a pull request with the fix to the README?

from interceptors.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.