updated format and lint scripts and applied them
This commit is contained in:
@@ -1,36 +1,39 @@
|
||||
import fetch from 'isomorphic-unfetch';
|
||||
import parseSort from './parseSort';
|
||||
import getUrl from './getUrl';
|
||||
import getJwt from './getJwt';
|
||||
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 $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 };
|
||||
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 };
|
||||
};
|
||||
return { total: 0, docs: [], error: docsRes.message }
|
||||
}
|
||||
|
||||
export const buildQ = q => {
|
||||
if(!q.$search) delete q.$search;
|
||||
if(!q.$skip) delete q.$skip;
|
||||
if (!q.$search) delete q.$search
|
||||
if (!q.$skip) delete q.$skip
|
||||
else {
|
||||
q.$skip = (q.$skip - 1) * $limit;
|
||||
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;
|
||||
};
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user