-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from g0ngjie/feature/function-redirect
Feature/function redirect
- Loading branch information
Showing
14 changed files
with
359 additions
and
165 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
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
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,51 @@ | ||
import { IRedirectHeader } from './types'; | ||
|
||
|
||
const errLog = function (...args: any[]) { | ||
console.log("%c[AjaxProxy][error]: ", "color: #ff4d4f", ...args) | ||
} | ||
|
||
type Req = { | ||
url: string | ||
method: string | ||
} | ||
|
||
type Next = { | ||
url: string, | ||
headers?: { [key: IRedirectHeader['key']]: IRedirectHeader['value'] } | ||
} | ||
|
||
function isNext(x: any): x is Next { | ||
if (!!x) return true | ||
if (x.url && !x.headers) return true | ||
return x.headers && Object.prototype.toString.call(x.headers) == '[object Object]' | ||
} | ||
|
||
export function execSetup(req: Req, funcText: string): Promise<Next> { | ||
return new Promise(resolve => { | ||
try { | ||
const source = ';(' + funcText + ')' | ||
const execFunc = window.eval(source) | ||
const type = typeof execFunc | ||
if (type === "function") { | ||
if (!funcText.includes('next(')) { | ||
errLog("The structure of 'next' is incorrect [code 1]") | ||
// original | ||
return resolve({ url: req.url }) | ||
} | ||
execFunc(req, (next: Next) => { | ||
// custom | ||
if (isNext(next)) { | ||
return resolve({ url: next.url, headers: next.headers }) | ||
} | ||
}) | ||
return resolve({ url: req.url }) | ||
} | ||
errLog("Please enter a correct 'function' [code 2]") | ||
return resolve({ url: req.url }) | ||
} catch (error) { | ||
errLog(error) | ||
return resolve({ url: req.url }) | ||
} | ||
}) | ||
} |
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,13 @@ | ||
import { IRedirectHeader } from './types'; | ||
declare type Req = { | ||
url: string; | ||
method: string; | ||
}; | ||
declare type Next = { | ||
url: string; | ||
headers?: { | ||
[key: IRedirectHeader['key']]: IRedirectHeader['value']; | ||
}; | ||
}; | ||
export declare function execSetup(req: Req, funcText: string): Promise<Next>; | ||
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
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
Oops, something went wrong.