Skip to content

citrizon/nakadachi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nakadachi is a minimal Javascript Web Network Hooking Library. You can use this library to hook into Fetch and the old-school XHR (XMLHttpRequest) calls. You can check the examples below.

Usage

For basic logging, you can use:

// Assuming you've imported Nakadachi.
// Run the hook command
Nakadachi.hook();

// After hooking, add a middleware.
Nakadachi.addMiddleware( interceptor => {
    // Log the requests only
    if ( interceptor.type === 'request' )
        console.log( interceptor.request );
} );

// Make any Fetch or XMLHttpRequest calls here.

For intercepting requests, you can use:

// Assuming you've imported Nakadachi and hooked it.
Nakadachi.addMiddleware( interceptor => {
    // Log the requests only
    if ( interceptor.type === 'request'
        && interceptor.request.url.endsWith( '/debug' ) )
        interceptor.intercept(
            new Nakadachi.HTTPCode( '200', 'OK' ),
            { /* Leaving headers empty */ },
            JSON.stringify( {
                isDebug: true
            } )
        );
} );

// Make any Fetch or XMLHttpRequest calls here.

Details

/* Hooks onto the network calls */
Nakadachi.Hook()

/* Middleware Management */
Nakadachi.addMiddleware( mw: Function )
Nakadachi.removeMiddleware( mw: Function )

/* Helper classes */
new Nakadachi.HTTPCode( code: Number, status: String )

/* Internals */
InterceptableRequest.type: String
InterceptableRequest.request: Request
InterceptableRequest.intercept( code: HTTPCode, headers: Object, data: Any )

About

a Javascript Web Network Hooking Library.

Resources

License

Stars

Watchers

Forks

Packages

No packages published