-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial release
- Loading branch information
Showing
70 changed files
with
13,875 additions
and
2,499 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
src/**/__tests__/** | ||
node_modules | ||
.eslintrc.js | ||
jest.config.js | ||
webpack.config.js | ||
webpack.dev.js | ||
webpack.prod.js | ||
webpack.*.js | ||
test/ | ||
bin/ | ||
test.config.js | ||
nyc.config.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.idea/ | ||
/docs/ | ||
/test/ | ||
.editorconfig | ||
.eslintignore | ||
.gitignore | ||
test.config.js | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License | ||
|
||
Copyright (c) 2019 Izumi Hoshino <rindo.hinase@gmail.com> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# xspy | ||
|
||
- Hook fetch/XMLHttpRequest request to modify before it is sent. | ||
- Hook fetch/XMLHttpRequest response before it is available to client. | ||
- Comes with type definitions by Typescript for good developer experience. | ||
|
||
## Doc | ||
https://hinaser.github.io/xspy/ | ||
|
||
## Browser Support | ||
Confirmed working in IE9+, Firefox, Chrome, Edge | ||
|
||
## Install | ||
### Browser | ||
Copy `/dist/xspy.es5.min.js` and load into your <script> tag before any fetch/XMLHttpRequest. | ||
|
||
```html | ||
<!-- Rename `xspy.es5.min.js` as you like --> | ||
<script src="xspy.es5.min.js"></script> | ||
``` | ||
|
||
### Webpack project | ||
|
||
``` | ||
npm install xspy -- OR yarn add xspy | ||
``` | ||
#### CommonJS | ||
```js | ||
const xspy = require("xspy").default; | ||
``` | ||
#### Typescript | ||
```typescript | ||
import xspy from "xspy"; | ||
``` | ||
|
||
## Example | ||
|
||
#### Add Authorization header if it doesn't exist on request header. | ||
|
||
```js | ||
xspy.onRequest((req) => { | ||
if(!req.headers["Authorization"]){ | ||
req.headers["Authorization"] = "bearer sakxxd0ejdalkjdalkjfajd"; | ||
} | ||
}); | ||
``` | ||
|
||
#### Return fake API response without sending actual ajax request. | ||
```js | ||
xspy.onRequest((req, callback) => { | ||
const fakeResponse = { | ||
status: 200, | ||
statusText: "OK", | ||
body: "This is fake response!", | ||
}; | ||
callback(fakeResponse); | ||
}); | ||
``` | ||
|
||
#### Log every response headers to console. | ||
```js | ||
xspy.onResponse((req, res) => { | ||
console.log(res.url, res.status, res.headers); | ||
}); | ||
``` | ||
|
||
## Work inspired by | ||
|
||
https://github.com/jpillora/xhook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { RequestHandler, ResponseHandler, WindowEx } from "./index.type"; | ||
declare let window: WindowEx; | ||
export declare class Proxy { | ||
private static _reqListeners; | ||
private static _resListeners; | ||
private static _customXHR; | ||
private static _customFetch; | ||
static readonly OriginalXHR: new () => XMLHttpRequest; | ||
static readonly OriginalFetch: (input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>; | ||
static enable(): void; | ||
static disable(): void; | ||
static isEnabled(): boolean; | ||
static setXMLHttpRequest(m: typeof window["XMLHttpRequest"]): void; | ||
static setFetch(m: typeof window["fetch"]): void; | ||
static getRequestListeners(): RequestHandler<"xhr" | "fetch">[]; | ||
static getResponseListeners(): ResponseHandler<"xhr" | "fetch">[]; | ||
static onRequest(listener: RequestHandler<"xhr" | "fetch">, n?: number): void; | ||
static offRequest(listener: RequestHandler<"xhr" | "fetch">): void; | ||
static onResponse(listener: ResponseHandler<"xhr" | "fetch">, n?: number): void; | ||
static offResponse(listener: ResponseHandler<"xhr" | "fetch">): void; | ||
static clearAll(): void; | ||
static clearRequestHandler(): void; | ||
static clearResponseHandler(): void; | ||
private static _removeEventListener; | ||
} | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { Proxy } from "./Proxy"; | ||
export default Proxy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export declare const IEVersion: number | boolean; | ||
export declare function isIE(op?: "<" | "<=" | ">" | ">=" | "=", version?: number): boolean; | ||
export declare const createEvent: (type: string) => Event; | ||
export declare const toHeaderMap: (responseHeaders: string) => { | ||
[name: string]: string; | ||
}; | ||
export declare const toHeaderString: (headerMap: { | ||
[name: string]: string; | ||
}) => string; | ||
export declare const makeProgressEvent: (type: string, loaded: number, lengthComputable?: boolean, total?: number) => ProgressEvent<XMLHttpRequestEventTarget>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Proxy } from "./Proxy"; | ||
export interface WindowEx extends Window { | ||
XMLHttpRequest: new () => XMLHttpRequest; | ||
xspy: typeof Proxy; | ||
} | ||
export declare type EventType = "request" | "response"; | ||
export declare type RequestCallback<T extends "xhr" | "fetch"> = { | ||
(dummyResponse: TResponse<T>): unknown; | ||
moveToHeaderReceived: (dummyResponse: TResponse<T>) => void; | ||
moveToLoading: (dummyResponse: TResponse<T>) => void; | ||
}; | ||
export declare type RequestHandler<T extends "xhr" | "fetch"> = (this: T extends "xhr" ? XMLHttpRequest : unknown, request: T extends "xhr" ? XhrRequest : FetchRequest, callback?: RequestCallback<T>) => unknown; | ||
export declare type ResponseHandler<T extends "xhr" | "fetch"> = (this: T extends "xhr" ? XMLHttpRequest : unknown, request: T extends "xhr" ? XhrRequest : FetchRequest, response: T extends "xhr" ? XhrResponse : FetchResponse, callback?: ResponseCallback<T>) => unknown; | ||
export declare type ResponseCallback<T extends "xhr" | "fetch"> = (dummyResponse: T extends "xhr" ? XhrResponse : FetchResponse) => unknown; | ||
export interface TRequest<T extends "xhr" | "fetch"> { | ||
ajaxType: T; | ||
method: string; | ||
url: string; | ||
timeout: number; | ||
headers: Record<string, string>; | ||
body?: T extends "xhr" ? BodyInit | null | Document : BodyInit | null; | ||
} | ||
export interface XhrRequest extends TRequest<"xhr"> { | ||
async?: boolean; | ||
username?: string | null; | ||
password?: string | null; | ||
responseType?: XMLHttpRequestResponseType; | ||
withCredentials?: boolean; | ||
upload?: XMLHttpRequestUpload; | ||
} | ||
export interface FetchRequest extends TRequest<"fetch"> { | ||
method: string; | ||
url: string; | ||
body?: BodyInit | null; | ||
cache?: RequestCache; | ||
credentials?: RequestCredentials; | ||
integrity?: string; | ||
keepalive?: boolean; | ||
mode?: RequestMode; | ||
redirect?: RequestRedirect; | ||
referrer?: string; | ||
referrerPolicy?: ReferrerPolicy; | ||
signal?: AbortSignal | null; | ||
} | ||
export interface TResponse<T extends "xhr" | "fetch"> { | ||
ajaxType: T; | ||
status: number; | ||
statusText: string; | ||
headers: Record<string, string>; | ||
body?: T extends "xhr" ? BodyInit | null | Document : BodyInit | null; | ||
} | ||
export interface XhrResponse extends TResponse<"xhr"> { | ||
headers: Record<string, string>; | ||
responseType: XMLHttpRequestResponseType; | ||
response?: BodyInit | null | Document; | ||
responseText?: string; | ||
responseXML?: Document | null; | ||
responseURL?: string; | ||
} | ||
export interface FetchResponse extends TResponse<"fetch"> { | ||
status: number; | ||
statusText: string; | ||
headers: Record<string, string>; | ||
body: BodyInit | null; | ||
ok: boolean; | ||
redirected: boolean; | ||
type: ResponseType; | ||
url: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export declare class ResponseProxy { | ||
private _response; | ||
private _body?; | ||
private _init?; | ||
headers: Headers; | ||
ok: boolean; | ||
redirected: boolean; | ||
status: number; | ||
statusText: string; | ||
trailer: Promise<Headers>; | ||
type: ResponseType; | ||
url: string; | ||
body: ReadableStream<Uint8Array> | null; | ||
bodyUsed: boolean; | ||
constructor(body?: BodyInit | null, init?: ResponseInit); | ||
static error(): Response; | ||
static redirect(url: string, status?: number): Response; | ||
clone(): ResponseProxy; | ||
arrayBuffer(): Promise<ArrayBuffer>; | ||
blob(): Promise<Blob>; | ||
formData(): Promise<FormData>; | ||
json(): Promise<any>; | ||
text(): Promise<string>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
export declare class XHRProxy implements XMLHttpRequest { | ||
static readonly UNSENT: number; | ||
static readonly OPENED: number; | ||
static readonly HEADERS_RECEIVED: number; | ||
static readonly LOADING: number; | ||
static readonly DONE: number; | ||
readonly UNSENT: number; | ||
readonly OPENED: number; | ||
readonly HEADERS_RECEIVED: number; | ||
readonly LOADING: number; | ||
readonly DONE: number; | ||
private _xhr; | ||
private _listeners; | ||
private _readyState; | ||
private _isAborted; | ||
private _hasError; | ||
private _transitioning; | ||
private _request; | ||
private _response; | ||
private _responseText; | ||
private _responseXML; | ||
readyState: number; | ||
status: number; | ||
statusText: string; | ||
timeout: number; | ||
readonly upload: XMLHttpRequestUpload; | ||
response: BodyInit | Document | null | undefined; | ||
responseType: XMLHttpRequestResponseType; | ||
responseURL: string; | ||
get responseText(): string; | ||
get responseXML(): Document | null; | ||
withCredentials: boolean; | ||
onreadystatechange: ((this: XMLHttpRequest, ev: Event) => unknown) | null; | ||
onabort: ((this: XMLHttpRequest, ev: Event) => unknown) | null; | ||
onerror: ((this: XMLHttpRequest, ev: Event) => unknown) | null; | ||
onloadstart: ((this: XMLHttpRequest, ev: Event) => unknown) | null; | ||
onload: ((this: XMLHttpRequest, ev: Event) => unknown) | null; | ||
onloadend: ((this: XMLHttpRequest, ev: Event) => unknown) | null; | ||
ontimeout: ((this: XMLHttpRequest, ev: Event) => unknown) | null; | ||
onprogress: ((this: XMLHttpRequest, ev: Event) => unknown) | null; | ||
constructor(); | ||
private _init; | ||
addEventListener<K extends keyof XMLHttpRequestEventMap>(type: K, listener: (this: XMLHttpRequest, ev: Event | ProgressEvent<XMLHttpRequestEventTarget>) => unknown, options?: boolean | AddEventListenerOptions): void; | ||
removeEventListener<K extends keyof XMLHttpRequestEventMap>(type: K, listener: (this: XMLHttpRequest, ev: Event | ProgressEvent<XMLHttpRequestEventTarget>) => any, options?: boolean | EventListenerOptions): void; | ||
dispatchEvent(event: Event | ProgressEvent<XMLHttpRequestEventTarget>): boolean; | ||
overrideMimeType(mime: string): void; | ||
open(method: string, url: string, async?: boolean, username?: string | null, password?: string | null): void; | ||
send(body?: Document | BodyInit | null): void; | ||
setRequestHeader(name: string, value: string): void; | ||
getResponseHeader(name: string): string | null; | ||
getAllResponseHeaders(): string; | ||
abort(): void; | ||
private static _createRequest; | ||
private static _createResponse; | ||
private _setupVirtualRequestForSending; | ||
private _syncEventListenersToXHR; | ||
private _createRequestCallback; | ||
private _createResponseCallback; | ||
private _loadHeaderFromXHRToVirtualResponse; | ||
private _loadBodyFromXHRToVirtualResponse; | ||
private _syncHeaderFromVirtualResponse; | ||
private _syncBodyFromVirtualResponse; | ||
private _onError; | ||
private _triggerStateAction; | ||
private _runUntil; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export declare function fetchProxy(input: RequestInfo, init?: RequestInit): Promise<Response>; |
Oops, something went wrong.