initial commit
This commit is contained in:
8
util/basePath.js
Normal file
8
util/basePath.js
Normal file
@@ -0,0 +1,8 @@
|
||||
// make sure basePath doesn't end with /
|
||||
let { basePath } = require('../config/host.json');
|
||||
const urlChars = basePath.split('');
|
||||
|
||||
if(basePath.length > 1 && urlChars.pop() === '/') {
|
||||
basePath = urlChars.join('');
|
||||
}
|
||||
module.exports = basePath;
|
||||
48
util/checkDirParts.js
Normal file
48
util/checkDirParts.js
Normal file
@@ -0,0 +1,48 @@
|
||||
const isOkDirPart = str => {
|
||||
if(str.length > 255 || str.length === 0) return false;
|
||||
const end = str.length - 1;
|
||||
for(let i = 0; i < str.length; i++) {
|
||||
const c = str.charCodeAt(i);
|
||||
if(!(c > 47 && c < 58) && // 0-9
|
||||
!(c > 64 && c < 91) && // A-Z
|
||||
!(c > 96 && c < 123) && // a-z
|
||||
!(c === 95) && !(c === 45) && // _ and -
|
||||
!((c === 46 || c === 32) && // period or space if not first or last
|
||||
i !== 0 && i !== end)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
|
||||
checkDir: dir => {
|
||||
if(typeof dir !== 'string') return false;
|
||||
dir = dir.trim();
|
||||
if(dir.length === 0) return 0;
|
||||
if(dir.indexOf('/') > -1) {
|
||||
dir = dir.split('/').filter(p => p.length !== 0);
|
||||
if(dir.length === 1) {
|
||||
if(!isOkDirPart(dir[0])) false;
|
||||
dir = dir[0];
|
||||
} else if(dir.length === 0) {
|
||||
dir = '';
|
||||
} else if(dir.some(part => !isOkDirPart(part))) {
|
||||
return false;
|
||||
}
|
||||
} else if(!isOkDirPart(dir)) {
|
||||
return false;
|
||||
}
|
||||
return Array.isArray(dir) ? dir.join('/') : dir;
|
||||
},
|
||||
|
||||
checkName: name => {
|
||||
if(typeof name !== 'string') return false;
|
||||
name = name.trim();
|
||||
if(name.length === 0) return 0;
|
||||
if(!isOkDirPart(name)) return false;
|
||||
return name;
|
||||
},
|
||||
|
||||
};
|
||||
21
util/freezeSSR.js
Normal file
21
util/freezeSSR.js
Normal file
@@ -0,0 +1,21 @@
|
||||
const freezeSSR = selector => {
|
||||
const FrozenSSR = () => {
|
||||
let __html = '';
|
||||
let props = {};
|
||||
if(typeof document !== 'undefined') {
|
||||
let el = document.querySelector(selector);
|
||||
if(el) {
|
||||
__html = el.innerHTML;
|
||||
el.getAttributeNames().forEach(attr => {
|
||||
const attrKey = attr === 'class' ? 'className' : attr;
|
||||
props[attrKey] = el.getAttribute(attr);
|
||||
});
|
||||
}
|
||||
}
|
||||
return <div {...props} dangerouslySetInnerHTML={{ __html }} />;
|
||||
};
|
||||
|
||||
return { loading: FrozenSSR };
|
||||
};
|
||||
|
||||
export default freezeSSR;
|
||||
36
util/getDocs.js
Normal file
36
util/getDocs.js
Normal file
@@ -0,0 +1,36 @@
|
||||
import fetch from 'isomorphic-unfetch';
|
||||
import parseSort from './parseSort';
|
||||
import getUrl from './getUrl';
|
||||
import getJwt from './getJwt';
|
||||
|
||||
export const $limit = 12; // number of docs per page
|
||||
export const select = ['id', 'name', 'updated', 'dir']
|
||||
.map((f, i) => ({ [`$select[${i}]`]: f }));
|
||||
|
||||
export const getDocs = async (q, jwt) => {
|
||||
const docsRes = await fetch(getUrl('docs', Boolean(jwt)) + q, {
|
||||
headers: { Authorization: jwt || getJwt() }
|
||||
}).catch(({ message }) => ({ ok: false, error: message }));
|
||||
if(docsRes.ok) {
|
||||
const res = await docsRes.json();
|
||||
const total = res.total || 0;
|
||||
const docs = res.data || [];
|
||||
return { docs, total };
|
||||
}
|
||||
return { total: 0, docs: [], error: docsRes.message };
|
||||
};
|
||||
|
||||
export const buildQ = q => {
|
||||
if(!q.$search) delete q.$search;
|
||||
if(!q.$skip) delete q.$skip;
|
||||
else {
|
||||
q.$skip = (q.$skip - 1) * $limit;
|
||||
}
|
||||
const $sort = parseSort(q.$sort ? q.$sort : 'updated:-1');
|
||||
delete q.$sort;
|
||||
select.forEach(sel => q = {...q, ...sel});
|
||||
q = { $limit, ...q };
|
||||
let url = Object.keys(q).map(k => `${k}=${encodeURIComponent(q[k])}`).join('&');
|
||||
url = `?${url}&${$sort}`;
|
||||
return url;
|
||||
};
|
||||
6
util/getJwt.js
Normal file
6
util/getJwt.js
Normal file
@@ -0,0 +1,6 @@
|
||||
export default req => {
|
||||
if(req) return req.jwt;
|
||||
if(typeof window !== 'undefined') {
|
||||
return window.localStorage.getItem('jwt');
|
||||
}
|
||||
};
|
||||
15
util/getUrl.js
Normal file
15
util/getUrl.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const url = require('url');
|
||||
const urljoin = require('url-join');
|
||||
const basePath = require('./basePath');
|
||||
const { host, port, protocol } = require('../config/host.json');
|
||||
|
||||
module.exports = (path, absolute) => {
|
||||
path = urljoin(basePath, path);
|
||||
if(!absolute) return path;
|
||||
return url.format({
|
||||
hostname: host,
|
||||
port,
|
||||
protocol,
|
||||
pathname: path,
|
||||
});
|
||||
};
|
||||
4
util/keys.js
Normal file
4
util/keys.js
Normal file
@@ -0,0 +1,4 @@
|
||||
module.exports = {
|
||||
getKey: e => e.which || e.keyCode,
|
||||
isCtrlKey: key => key === 91 || key === 93 || key === 17
|
||||
};
|
||||
3
util/mapUser.js
Normal file
3
util/mapUser.js
Normal file
@@ -0,0 +1,3 @@
|
||||
export default ({ user }) => {
|
||||
return { user };
|
||||
};
|
||||
18
util/parseSort.js
Normal file
18
util/parseSort.js
Normal file
@@ -0,0 +1,18 @@
|
||||
export default sort => {
|
||||
let key, ascDesc;
|
||||
switch(typeof sort) {
|
||||
case 'object': {
|
||||
key = Object.keys(sort).pop();
|
||||
ascDesc = sort[key];
|
||||
break;
|
||||
}
|
||||
case 'string': {
|
||||
const parts = sort.split(':');
|
||||
key = parts[0];
|
||||
ascDesc = parts[1];
|
||||
break;
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
return `$sort[${key}]=${ascDesc}`;
|
||||
};
|
||||
8
util/stripBase.js
Normal file
8
util/stripBase.js
Normal file
@@ -0,0 +1,8 @@
|
||||
const basePath = require('./basePath');
|
||||
|
||||
module.exports = url => {
|
||||
if(basePath !== '/') {
|
||||
url = url.split(basePath).join('');
|
||||
}
|
||||
return url;
|
||||
};
|
||||
4
util/updStateFromId.js
Normal file
4
util/updStateFromId.js
Normal file
@@ -0,0 +1,4 @@
|
||||
export default function updateStateFromId(e){
|
||||
const el = e.target;
|
||||
this.setState({ [el.id]: el.value });
|
||||
}
|
||||
Reference in New Issue
Block a user