Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
paths.forEach(function(path) {
p.routes = [path]
var tokens = pathToRegexp.parse(path.path)
var parts = {}
// console.log(tokens)
tokens.forEach(function(token) {
if (typeof token === "object") {
parts[token.name] = "whatever"
}
})
var url = p.toPath(parts)
var expected = pathToRegexp.compile(path.path)(parts)
// console.log(url)
url.should.eql(expected)
})
function toReg(pathname: string, keys: any[], options: any): RegExp {
// First parse path into tokens.
const tokens = parse(pathname);
// Find the last token (checking for splat params).
const splat: any = tokens[tokens.length - 1];
// Check if the last token is a splat and make it optional.
if (splat && splat.asterisk) {
splat.optional = true;
}
// Convert the tokens to a regexp.
const re = tokensToRegExp(tokens, options);
// Assign keys to from regexp.
(re as any).keys = keys;
for (let i = 0, len = tokens.length; i < len; i++) {
if (typeof tokens[i] === "object") {
const fetch = (options) => {
const { method = 'get', data = {} } = options
let { url } = options
const cloneData = lodash.cloneDeep(data)
try {
let domin = ''
if (url.match(/[a-zA-z]+:\/\/[^/]*/)) {
domin = url.match(/[a-zA-z]+:\/\/[^/]*/)[0]
url = url.slice(domin.length)
}
const match = pathToRegexp.parse(url)
url = pathToRegexp.compile(url)(data)
for (const item of match) {
if (item instanceof Object && item.name in cloneData) {
delete cloneData[item.name]
}
}
url = domin + url
} catch (e) {
console.log(e)
}
switch (method.toLowerCase()) {
case 'get':
return axios.get(url, {
params: cloneData,
})
const [tokens] = useState(() => parse(pattern))
const [namedParams, setNamedParams] = useState(() =>
private validateRoutePath(path: string): string {
if (isUndefined(path)) {
return '';
}
if (Array.isArray(path)) {
path = head(path);
}
let pathWithParams = '';
for (const item of pathToRegexp.parse(path)) {
pathWithParams += isString(item) ? item : `${item.prefix}{${item.name}}`;
}
return pathWithParams === '/' ? '' : validatePath(pathWithParams);
}
routesRouter = routesRouter.filter(route => {
let tokens = pathToRegexp.parse(route)
let params = tokens.filter(token => typeof token === 'object')
return params.length === 1 && params[0].name === 'lang'
})
routesRouter.forEach(routeWithLang => {
return (params: ?Object): string => {
if (params) {
const tokenNames = p2r.parse(route).map((t) => t.name).filter(Boolean)
const queryKeys = Object.keys(params).filter((key) => !tokenNames.includes(key))
return `${p2r.compile(route)(params)}?${qs.stringify(pick(params, queryKeys))}`.replace(/\?$/, '')
}
return route
}
}
export default function request(options) {
let { data, url, method = 'get' } = options
const cloneData = cloneDeep(data)
try {
let domain = ''
const urlMatch = url.match(/[a-zA-z]+:\/\/[^/]*/)
if (urlMatch) {
;[domain] = urlMatch
url = url.slice(domain.length)
}
const match = pathToRegexp.parse(url)
url = pathToRegexp.compile(url)(data)
for (const item of match) {
if (item instanceof Object && item.name in cloneData) {
delete cloneData[item.name]
}
}
url = domain + url
} catch (e) {
message.error(e.message)
}
options.url = url
options.params = cloneData
options.cancelToken = new CancelToken(cancel => {
window.cancelRequest.set(Symbol(Date.now()), {
let {
method = 'post',
data,
fetchType,
url
} = options
const cloneData = lodash.cloneDeep(data)
try {
let domin = ''
if (url.match(/[a-zA-z]+:\/\/[^/]*/)) {
domin = url.match(/[a-zA-z]+:\/\/[^/]*/)[0]
url = url.slice(domin.length)
}
const match = pathToRegexp.parse(url)
url = pathToRegexp.compile(url)(data)
for (let item of match) {
if (item instanceof Object && item.name in cloneData) {
delete cloneData[item.name]
}
}
url = domin + url
} catch (e) {
message.error(e.message)
}
if (fetchType === 'JSONP') {
return new Promise((resolve, reject) => {
jsonp(url, {
param: `${qs.stringify(data)}&callback`,
name: `jsonp_${new Date().getTime()}`,