Leo Lamprecht
7 years ago
2 changed files with 0 additions and 181 deletions
@ -1,112 +0,0 @@ |
|||
// Native
|
|||
const { homedir } = require('os') |
|||
const path = require('path') |
|||
|
|||
// Packages
|
|||
const fs = require('fs-extra') |
|||
const ms = require('ms') |
|||
|
|||
// Ours
|
|||
const { get: getUser } = require('./user') |
|||
|
|||
// `8h` is arbitrarily used based on the average sleep time
|
|||
const TTL = ms('8h') |
|||
|
|||
let file = process.env.NOW_JSON |
|||
? path.resolve(process.env.NOW_JSON) |
|||
: path.resolve(homedir(), '.now.json') |
|||
|
|||
function setConfigFile(nowjson) { |
|||
file = path.resolve(nowjson) |
|||
} |
|||
|
|||
function save(data) { |
|||
fs.writeFileSync(file, JSON.stringify(data, null, 2)) |
|||
} |
|||
|
|||
/** |
|||
* Reads the config file |
|||
* |
|||
* Optionally, always queries the API to get the user info even if the |
|||
* config file is not present |
|||
* |
|||
* @param {Boolean} force [false] Queries the API even if the config |
|||
* file is not present. If `true`, `token` |
|||
* *must* be specified |
|||
* @param {String} token Will be used to autenticate in the API |
|||
if needed |
|||
* @param {String} apiUrl URL of the API to be used |
|||
* @return {Object} |
|||
*/ |
|||
async function read({ force = false, token, apiUrl } = {}) { |
|||
let existing = {} |
|||
try { |
|||
existing = fs.readFileSync(file, 'utf8') |
|||
existing = JSON.parse(existing) |
|||
} catch (err) {} |
|||
|
|||
// Will happen if `force`d or if `--token` is used and it's different from
|
|||
// The one that's stored (which can be `undefined`)
|
|||
if ((force && token) || (token && token !== existing.token)) { |
|||
const user = await getUser({ token, apiUrl }) |
|||
if (user) { |
|||
return { |
|||
token, |
|||
user: { |
|||
uid: user.uid, |
|||
username: user.username, |
|||
email: user.email |
|||
}, |
|||
currentTeam: existing.currentTeam |
|||
} |
|||
} |
|||
return {} |
|||
} |
|||
|
|||
if (!existing.token) { |
|||
return {} |
|||
} |
|||
|
|||
if (!existing.lastUpdate || Date.now() - existing.lastUpdate > TTL) { |
|||
// TODO: update `teams` info
|
|||
const token = existing.token |
|||
const user = await getUser({ token }) |
|||
|
|||
if (user) { |
|||
existing.user = user |
|||
existing.lastUpdate = Date.now() |
|||
save(existing) |
|||
} |
|||
} |
|||
return existing |
|||
} |
|||
|
|||
/** |
|||
* Merges the `data` object onto the |
|||
* JSON config stored in `.now.json`. |
|||
* |
|||
* (atomic) |
|||
* @param {Object} data |
|||
*/ |
|||
async function merge(data) { |
|||
const cfg = Object.assign({}, await read(), data) |
|||
save(cfg) |
|||
} |
|||
|
|||
// Removes a key from the config and store the result
|
|||
async function remove(key) { |
|||
const cfg = await read() |
|||
delete cfg[key] |
|||
fs.writeFileSync(file, JSON.stringify(cfg, null, 2)) |
|||
} |
|||
|
|||
// We need to remove the config file when running `now logout`
|
|||
const removeFile = async () => fs.remove(file) |
|||
|
|||
module.exports = { |
|||
setConfigFile, |
|||
read, |
|||
merge, |
|||
remove, |
|||
removeFile |
|||
} |
@ -1,69 +0,0 @@ |
|||
const _fetch = require('node-fetch') |
|||
const { responseError } = require('./error') |
|||
|
|||
function _filter(data) { |
|||
data = data.user |
|||
|
|||
return { |
|||
uid: data.uid, |
|||
username: data.username, |
|||
email: data.email |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Gets all the info we have about an user |
|||
* |
|||
* @param {Object} fetch Optionally, _our_ `fetch` can be passed here |
|||
* @param {String} token Only necessary if `fetch` is undefined |
|||
* @param {String} apiUrl Only necessary if `fetch` is undefined |
|||
* @param {Boolean} filter If `true`, the `filter` used will to the data |
|||
* before returning |
|||
* @return {Object} |
|||
*/ |
|||
async function get( |
|||
{ fetch, token, apiUrl = 'https://api.zeit.co', filter = true } = {} |
|||
) { |
|||
let headers = {} |
|||
const endpoint = '/www/user' |
|||
const url = fetch ? endpoint : apiUrl + endpoint |
|||
|
|||
if (!fetch) { |
|||
headers = { |
|||
Authorization: `Bearer ${token}` |
|||
} |
|||
fetch = _fetch |
|||
} |
|||
|
|||
const res = await fetch(url, { headers }) |
|||
|
|||
if (res.status === 403) { |
|||
const err = Error( |
|||
'Your authentication token is invalid. Try running `now login` to log in again.' |
|||
) |
|||
err.userError = true |
|||
throw err |
|||
} |
|||
|
|||
if (res.status >= 400 && res.status < 500) { |
|||
const err = await responseError(res) |
|||
throw err |
|||
} |
|||
|
|||
if (res.status !== 200) { |
|||
throw new Error('API Error getting user data') |
|||
} |
|||
|
|||
const json = await res.json() |
|||
|
|||
if (filter) { |
|||
return _filter(json) |
|||
} |
|||
|
|||
return json |
|||
} |
|||
|
|||
module.exports = { |
|||
get, |
|||
filter: _filter |
|||
} |
Loading…
Reference in new issue