@brushes/ajax
Advanced tools
Comparing version
@@ -1,1 +0,1 @@ | ||
export declare const initImpl: (keyword?: string) => void; | ||
export declare const initImpl: () => void; |
@@ -5,27 +5,23 @@ "use strict"; | ||
var ajax_hook_1 = require("ajax-hook"); | ||
var keyWord = 'kezi'; | ||
var getQueryVariable = function (url, variable) { | ||
if (url === void 0) { url = ''; } | ||
var vars = url.split("&"); | ||
for (var i = 0; i < vars.length; i++) { | ||
var pair = vars[i].split("="); | ||
if (pair[0] === variable) { | ||
return pair[1]; | ||
} | ||
var getHost = function (url) { | ||
if (!url.includes('host')) { | ||
return {}; | ||
} | ||
return false; | ||
var searchIndex = url.indexOf('?'); | ||
var urlSearchParams = new URLSearchParams(url.substring(searchIndex)); | ||
return Object.fromEntries(urlSearchParams.entries()); | ||
}; | ||
var initImpl = function (keyword) { | ||
if (keyword === void 0) { keyword = keyWord; } | ||
var initImpl = function () { | ||
(0, ajax_hook_1.proxy)({ | ||
// //请求发起前进入 | ||
onRequest: function (config, handler) { | ||
console.log(18, config, keyword, document.referrer); | ||
if (document.referrer.indexOf(keyword) > 0) { | ||
var host = getQueryVariable(document.referrer.split('?')[1], 'host'); | ||
var path = config.url.split('.cn')[1] || config.url.split('.com')[1]; | ||
console.log(22, host, keyword, path); | ||
config.url = "".concat(host).concat(path); | ||
var search = getHost(document.referrer); | ||
if (search) { | ||
var host = search.host, token = search.token; | ||
if (host && token) { | ||
var path = config.url.split('.cn')[1] || config.url.split('.com')[1]; | ||
config.url = "".concat(host).concat(path); | ||
config.headers['saas-token'] = token; | ||
} | ||
} | ||
config.headers['saas-token'] = localStorage.getItem('saas-token'); | ||
handler.next(config); | ||
@@ -32,0 +28,0 @@ }, |
{ | ||
"name": "@brushes/ajax", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"main": "dist/index.js", | ||
@@ -11,7 +11,9 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@types/lodash": "^4.14.191", | ||
"typescript": "^4.4.4" | ||
}, | ||
"dependencies": { | ||
"ajax-hook": "^2.1.3" | ||
"ajax-hook": "^2.1.3", | ||
"lodash": "^4.17.21" | ||
} | ||
} |
import {proxy} from "ajax-hook"; | ||
const keyWord = 'kezi'; | ||
const getQueryVariable = (url: String = '', variable: String) => { | ||
let vars = url.split("&"); | ||
for (let i=0;i<vars.length;i++) { | ||
let pair = vars[i].split("="); | ||
if(pair[0] === variable){return pair[1];} | ||
const getHost = (url: string) => { | ||
if (!url.includes('host')) { | ||
return {} | ||
} | ||
return false; | ||
const searchIndex = url.indexOf('?') | ||
const urlSearchParams = new URLSearchParams(url.substring(searchIndex)) | ||
return Object.fromEntries(urlSearchParams.entries()) | ||
} | ||
export const initImpl = (keyword = keyWord) => { | ||
export const initImpl = () => { | ||
proxy({ | ||
// //请求发起前进入 | ||
onRequest: (config:any, handler: { next: Function }) => { | ||
console.log(18, config, keyword, document.referrer) | ||
if(document.referrer.indexOf(keyword) > 0) { | ||
const host = getQueryVariable(document.referrer.split('?')[1], 'host') | ||
const path = config.url.split('.cn')[1] || config.url.split('.com')[1]; | ||
console.log(22, host, keyword, path) | ||
config.url = `${host}${path}` | ||
onRequest: (config: any, handler: { next: Function }) => { | ||
const search = getHost(document.referrer); | ||
if (search) { | ||
const {host, token} = search | ||
if (host && token) { | ||
const path = config.url.split('.cn')[1] || config.url.split('.com')[1] | ||
config.url = `${host}${path}`; | ||
config.headers['saas-token'] = token | ||
} | ||
} | ||
config.headers['saas-token'] = localStorage.getItem('saas-token'); | ||
handler.next(config); | ||
@@ -29,0 +26,0 @@ }, |
@@ -9,3 +9,8 @@ { | ||
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ | ||
// "lib": [], /* Specify library files to be included in the compilation. */ | ||
"lib": [ | ||
"dom", | ||
"es7", | ||
"es2019", | ||
"dom.iterable" | ||
], /* Specify library files to be included in the compilation. */ | ||
// "allowJs": true, /* Allow javascript files to be compiled. */ | ||
@@ -12,0 +17,0 @@ // "checkJs": true, /* Report errors in .js files. */ |
139
0.72%8796
-2.41%2
100%2
100%+ Added
+ Added