All files / src request.js

100% Statements 71/71
100% Branches 11/11
100% Functions 4/4
100% Lines 70/70

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 712x 2x 2x 2x 2x 2x 2x 2x 2x 14x 14x 14x 6x 14x 8x 8x 14x 14x 14x 2x 2x 2x 2x 2x 2x 2x 2x 2x 12x 12x 12x 12x 12x 12x 12x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 4x 4x 4x 4x 4x 6x 6x  
import { pick } from 'ramda'
 
/**
 * Get the request body from a request object
 *
 * @param {Request} request
 * @returns {Promise<Object>}
 */
export async function getRequestBody(request) {
	let body = null
	try {
		body = await request.formData()
		body = Object.fromEntries(body.entries())
	} catch (err) {
		body = await request.json()
	}
 
	return body
}
 
/**
 * Get the request data from a request object
 *
 * @param {Request} request
 * @param {URL} url
 * @returns {Promise<Object>}
 */
export async function getRequestData({ request, url }) {
	const body = await getRequestBody(request)
	const data = {
		...Object.fromEntries(url.searchParams.entries()),
		...body
	}
	return data
}
 
/**
 * Split the auth data from a request object
 *
 * @param {Request} event
 * @returns {Promise<Object>}
 */
export async function splitAuthData(event) {
	const data = await getRequestData(event)
	const { mode } = data
 
	const credentials = pick(['email', 'password', 'token', 'provider'], data)
	const options = {
		...pick(['scopes', 'params', 'redirect'], data)
	}
 
	return { mode, credentials, options }
}
 
/**
 * Convert an object to a URL with params
 *
 * @param {Request} request
 * @param {URL} url
 */
export function asURLWithParams(host, path = '', data = {}) {
	let params = ''
	if (data && typeof data === 'object') {
		params = Object.entries(data)
			.map(([key, value]) => `${key}=${value}`)
			.join('&')
		params = params.length ? `?${params}` : params
	}
	return host + path + params
}