From 398ac6299aa57c59c04f16d29e6da54bf47f62d8 Mon Sep 17 00:00:00 2001 From: Nick Downing Date: Sun, 23 Jan 2022 21:11:09 -0500 Subject: [PATCH] Remove repetitive code in interactive pages, use api_call() directly everywhere --- _config/Problem.mjs | 2 +- _config/site.jst | 2 +- _lib/navbar.jst | 59 +++------------ contact/index.html.jst | 35 ++------- js/api_call.js.min | 8 ++ my_account/index.html.jst | 74 ++++--------------- my_account/password_reset/index.html.jst | 20 +---- .../send_verification_email/index.html.jst | 22 ++---- my_account/sign_up/index.html.jst | 56 +++----------- my_account/verify_email/index.html.jst | 22 +----- my_account/verify_password/index.html.jst | 22 +----- 11 files changed, 66 insertions(+), 256 deletions(-) diff --git a/_config/Problem.mjs b/_config/Problem.mjs index 58b06e3..ef11607 100644 --- a/_config/Problem.mjs +++ b/_config/Problem.mjs @@ -9,7 +9,7 @@ let Problem = class { return ( error instanceof Problem ? error : - new Problem('Bad request', error.message, 400) + new Problem('Bad request', (error.stack || error.message), 400) ) } } diff --git a/_config/site.jst b/_config/site.jst index 9e37c6d..4e7d2e6 100644 --- a/_config/site.jst +++ b/_config/site.jst @@ -147,7 +147,7 @@ return async (resources, root, prev_site) => { await session.set('expires', expires.getTime()) env.response.setHeader( 'Set-Cookie', - `session_key=${env.session_key}; expires=${expires.toUTCString()}; path=/;` + `session_key=${env.session_key}; expires=${expires.toUTCString()}; path=/; httponly;` ) await transaction.commit() diff --git a/_lib/navbar.jst b/_lib/navbar.jst index 6420b2f..7ec7234 100644 --- a/_lib/navbar.jst +++ b/_lib/navbar.jst @@ -313,19 +313,6 @@ return async (env, head, body, scripts) => { script(src="/js/api_call.js") {} script { - let api_account_sign_in = async (...args) => api_call( - '/api/account/sign_in.json', - ...args - ) - let api_account_sign_out = async (...args) => api_call( - '/api/account/sign_out.json', - ...args - ) - let api_feedback = async (...args) => api_call( - '/api/feedback.json', - ...args - ) - // this function can be overridden in a further script function sign_in_out(status) { } @@ -357,23 +344,14 @@ return async (env, head, body, scripts) => { let email try { email = document.getElementById('sign-in-email').value.slice(0, 256).toLowerCase() - await api_account_sign_in( + await api_call( + '/api/account/sign_in.json', email, document.getElementById('sign-in-password').value.slice(0, 256) ) } catch (error) { - let problem = - error instanceof Problem ? - error : - new Problem( - // title - 'Bad request', - // details - (error.stack || error.message).toString() - // status - 400 - ) + let problem = Problem.from(error) if (problem.title === 'Email not yet verified') { location.href = `/my_account/send_verification_email?email=${encodeURIComponent(email)}` @@ -403,20 +381,12 @@ return async (env, head, body, scripts) => { 'click', async () => { try { - await api_account_sign_out() + await api_call( + '/api/account/sign_out.json' + ) } catch (error) { - let problem = - error instanceof Problem ? - error : - new Problem( - // title - 'Bad request', - // details - (error.stack || error.message).toString() - // status - 400 - ) + let problem = Problem.from(error) document.getElementById('message-modal-message').textContent = problem.detail $('#sign-in-modal').modal('hide') @@ -457,23 +427,14 @@ return async (env, head, body, scripts) => { 'click', async () => { try { - await api_feedback( + await api_call( + '/api/feedback.json', location.href, document.getElementById('feedback-message').value.slice(0, 65536) ) } catch (error) { - let problem = - error instanceof Problem ? - error : - new Problem( - // title - 'Bad request', - // details - (error.stack || error.message).toString() - // status - 400 - ) + let problem = Problem.from(error) document.getElementById('message-modal-message').textContent = problem.detail $('#feedback-modal').modal('hide') diff --git a/contact/index.html.jst b/contact/index.html.jst index 888226f..a6b78a8 100644 --- a/contact/index.html.jst +++ b/contact/index.html.jst @@ -145,23 +145,11 @@ return async env => { //script(src="/js/api_call.js") {} script { - //let api_contact_get_draft = async (...args) => api_call( - // '/api/contact/get_draft.json', - // ...args - //) - let api_contact_set_draft = async (...args) => api_call( - '/api/contact/set_draft.json', - ...args - ) - let api_contact_send_enquiry = async (...args) => api_call( - '/api/contact/send_enquiry.json', - ...args - ) - let draft_timeout_running = false let draft_timeout_handler = async () => { draft_timeout_running = false - await api_contact_set_draft( + await api_call( + '/api/contact/set_draft.json', { given_names: document.getElementById('given-names').value.slice(0, 256), family_name: document.getElementById('family-name').value.slice(0, 256), @@ -170,7 +158,7 @@ return async env => { message: document.getElementById('message').value.slice(0, 65536) } ) - //console.log('draft', await api_contact_get_draft()) + //console.log('draft', await api_call('/api/contact/get_draft.json')) } let draft_change_handler = () => { if (!draft_timeout_running) { @@ -214,20 +202,13 @@ return async env => { document.getElementById('step-2').scrollIntoView() try { - await api_contact_send_enquiry(details) + await api_call( + '/api/contact/send_enquiry.json', + details + ) } catch (error) { - let problem = - error instanceof Problem ? - error : - new Problem( - // title - 'Bad request', - // detail - (error.stack || error.message).toString() - // status - 400 - ) + let problem = Problem.from(error) $('#step-2-tick').hide() $('#step-2-cross').show() diff --git a/js/api_call.js.min b/js/api_call.js.min index 12ebdae..bde4a54 100644 --- a/js/api_call.js.min +++ b/js/api_call.js.min @@ -4,6 +4,14 @@ Problem = class { this.detail = detail this.status = status } + + static from(error) { + return ( + error instanceof Problem ? + error : + new Problem('Bad request', (error.stack || error.message), 400) + ) + } } api_call = async (endpoint, ...args) => { diff --git a/my_account/index.html.jst b/my_account/index.html.jst index fe11c4b..765aaa6 100644 --- a/my_account/index.html.jst +++ b/my_account/index.html.jst @@ -210,32 +210,12 @@ return async env => { //script(src="/js/api_call.js") {} script { - let api_account_change_details_get = async (...args) => api_call( - '/api/account/change_details/get.json', - ...args - ) - let api_account_change_details_set = async (...args) => api_call( - '/api/account/change_details/set.json', - ...args - ) - //let api_account_change_details_get_draft = async (...args) => api_call( - // '/api/account/change_details/get_draft.json', - // ...args - //) - let api_account_change_details_set_draft = async (...args) => api_call( - '/api/account/change_details/set_draft.json', - ...args - ) - let api_account_change_password = async (...args) => api_call( - '/api/account/change_password.json', - ...args - ) - let step_1_dirty = ${JSON.stringify(draft_details !== null)} let draft_timeout_running = false let draft_timeout_handler = async () => { draft_timeout_running = false - await api_account_change_details_set_draft( + await api_call( + '/api/account/change_details/set_draft.json', step_1_dirty ? { given_names: document.getElementById('given-names').value.slice(0, 256), @@ -244,7 +224,7 @@ return async env => { } : null ) - //console.log('draft', await api_account_change_details_get_draft()) + //console.log('draft', await api_call('/api/account/change_details/get_draft.json')) } document.addEventListener( @@ -282,20 +262,12 @@ return async env => { let details try { - details = await api_account_change_details_get() + details = await api_call( + '/api/account/change_details/get.json' + ) } catch (error) { - let problem = - error instanceof Problem ? - error : - new Problem( - // title - 'Bad request', - // detail - (error.stack || error.message).toString() - // status - 400 - ) + let problem = Problem.from(error) console.log(problem.detail) $('#step-1-tick').hide() @@ -339,7 +311,8 @@ return async env => { $('#step-1-spinner').show() try { - await api_account_change_details_set( + await api_call( + '/api/account/change_details/set.json', { given_names: document.getElementById('given-names').value.slice(0, 256), family_name: document.getElementById('family-name').value.slice(0, 256), @@ -348,17 +321,7 @@ return async env => { ) } catch (error) { - let problem = - error instanceof Problem ? - error : - new Problem( - // title - 'Bad request', - // detail - (error.stack || error.message).toString() - // status - 400 - ) + let problem = Problem.from(error) console.log(problem.detail) $('#step-1-tick').hide() @@ -424,25 +387,14 @@ return async env => { $('#step-2-spinner').show() try { - await api_account_change_password( - // old_password + await api_call( + '/api/account/change_password.json', document.getElementById('old-password').value.slice(0, 256), - // new_password document.getElementById('new-password').value.slice(0, 256) ) } catch (error) { - let problem = - error instanceof Problem ? - error : - new Problem( - // title - 'Bad request', - // detail - (error.stack || error.message).toString() - // status - 400 - ) + let problem = Problem.from(error) console.log(problem.detail) $('#step-2-tick').hide() diff --git a/my_account/password_reset/index.html.jst b/my_account/password_reset/index.html.jst index 3e9a639..b788899 100644 --- a/my_account/password_reset/index.html.jst +++ b/my_account/password_reset/index.html.jst @@ -99,11 +99,6 @@ return async env => { //script(src="/js/api_call.js") {} script { - let api_account_password_reset = async (...args) => api_call( - '/api/account/password_reset.json', - ...args - ) - let step_1 = async () => { if ( !document.getElementById('email').reportValidity() || @@ -129,23 +124,14 @@ return async env => { let email try { email = document.getElementById('email').value.slice(0, 256).toLowerCase() - await api_account_password_reset( + await api_call( + '/api/account/password_reset.json', email, document.getElementById('password').value.slice(0, 256) ) } catch (error) { - let problem = - error instanceof Problem ? - error : - new Problem( - // title - 'Bad request', - // details - (error.stack || error.message).toString() - // status - 400 - ) + let problem = Problem.from(error) $('#step-2-tick').hide() $('#step-2-cross').show() diff --git a/my_account/send_verification_email/index.html.jst b/my_account/send_verification_email/index.html.jst index f5fec01..03ffbd6 100644 --- a/my_account/send_verification_email/index.html.jst +++ b/my_account/send_verification_email/index.html.jst @@ -91,11 +91,6 @@ return async env => { //script(src="/js/api_call.js") {} script { - let api_account_sign_up_send_email_verification_link = async (...args) => api_call( - '/api/account/sign_up/send_email_verification_link.json', - ...args - ) - let step_1 = async () => { if (!document.getElementById('email').reportValidity()) { $('#step-1-tick').hide() @@ -118,20 +113,13 @@ return async env => { let email try { email = document.getElementById('email').value.slice(0, 256).toLowerCase() - await api_account_sign_up_send_email_verification_link(email) + await api_call( + '/api/account/sign_up/send_email_verification_link.json', + email + ) } catch (error) { - let problem = - error instanceof Problem ? - error : - new Problem( - // title - 'Bad request', - // details - (error.stack || error.message).toString() - // status - 400 - ) + let problem = Problem.from(error) $('#step-2-tick').hide() $('#step-2-cross').show() diff --git a/my_account/sign_up/index.html.jst b/my_account/sign_up/index.html.jst index c7ef86f..9ca59e2 100644 --- a/my_account/sign_up/index.html.jst +++ b/my_account/sign_up/index.html.jst @@ -191,27 +191,11 @@ return async env => { //script(src="/js/api_call.js") {} script { - let api_account_sign_up_create_account = async (...args) => api_call( - '/api/account/sign_up/create_account.json', - ...args - ) - //let api_account_sign_up_get_draft = async (...args) => api_call( - // '/api/account/sign_up/get_draft.json', - // ...args - //) - let api_account_sign_up_set_draft = async (...args) => api_call( - '/api/account/sign_up/set_draft.json', - ...args - ) - let api_account_sign_up_send_email_verification_link = async (...args) => api_call( - '/api/account/sign_up/send_email_verification_link.json', - ...args - ) - let draft_timeout_running = false let draft_timeout_handler = async () => { draft_timeout_running = false - await api_account_sign_up_set_draft( + await api_call( + '/api/account/sign_up/set_draft.json', { email: document.getElementById('email').value.slice(0, 256).toLowerCase(), given_names: document.getElementById('given-names').value.slice(0, 256), @@ -219,7 +203,7 @@ return async env => { contact_me: document.getElementById('contact-me').checked ? true : false } ) - //console.log('draft', await api_account_sign_up_get_draft()) + //console.log('draft', await api_call('/api/account/sign_up/get_draft.json')) } let draft_change_handler = () => { if (!draft_timeout_running) { @@ -263,25 +247,14 @@ return async env => { document.getElementById('step-2').scrollIntoView() try { - await api_account_sign_up_create_account( - // verification_code + await api_call( + '/api/account/sign_up/create_account.json', document.getElementById('verification-code').value.slice(0, 6).toLowerCase(), - // details details ) } catch (error) { - let problem = - error instanceof Problem ? - error : - new Problem( - // title - 'Bad request', - // detail - (error.stack || error.message).toString() - // status - 400 - ) + let problem = Problem.from(error) $('#step-2-tick').hide() $('#step-2-cross').show() @@ -305,20 +278,13 @@ return async env => { document.getElementById('step-3').scrollIntoView() try { - await api_account_sign_up_send_email_verification_link(details.email) + await api_call( + '/api/account/sign_up/send_email_verification_link.json', + details.email + ) } catch (error) { - let problem = - error instanceof Problem ? - error : - new Problem( - // title - 'Bad request', - // detail - (error.stack || error.message).toString() - // status - 400 - ) + let problem = Problem.from(error) $('#step-3-tick').hide() $('#step-3-cross').show() diff --git a/my_account/verify_email/index.html.jst b/my_account/verify_email/index.html.jst index 4e27a61..417dc33 100644 --- a/my_account/verify_email/index.html.jst +++ b/my_account/verify_email/index.html.jst @@ -105,11 +105,6 @@ return async env => { //script(src="/js/api_call.js") {} script { - let api_account_sign_up_verify_email = async (...args) => api_call( - '/api/account/sign_up/verify_email.json', - ...args - ) - let step_1 = async () => { if ( !document.getElementById('email').reportValidity() || @@ -135,25 +130,14 @@ return async env => { let email try { email = document.getElementById('email').value.slice(0, 256).toLowerCase() - await api_account_sign_up_verify_email( - // email + await api_call( + '/api/account/sign_up/verify_email.json', email, - // link_code document.getElementById('link-code').value.slice(0, 32).toLowerCase() ) } catch (error) { - let problem = - error instanceof Problem ? - error : - new Problem( - // title - 'Bad request', - // details - (error.stack || error.message).toString() - // status - 400 - ) + let problem = Problem.from(error) $('#step-2-tick').hide() $('#step-2-cross').show() diff --git a/my_account/verify_password/index.html.jst b/my_account/verify_password/index.html.jst index 2c8034a..52944be 100644 --- a/my_account/verify_password/index.html.jst +++ b/my_account/verify_password/index.html.jst @@ -105,11 +105,6 @@ return async env => { //script(src="/js/api_call.js") {} script { - let api_account_verify_password = async (...args) => api_call( - '/api/account/verify_password.json', - ...args - ) - let step_1 = async () => { if ( !document.getElementById('email').reportValidity() || @@ -135,25 +130,14 @@ return async env => { let email try { email = document.getElementById('email').value.slice(0, 256).toLowerCase() - await api_account_verify_password( - // email + await api_call( + '/api/account/verify_password.json', email, - // link_code document.getElementById('link-code').value.slice(0, 32).toLowerCase() ) } catch (error) { - let problem = - error instanceof Problem ? - error : - new Problem( - // title - 'Bad request', - // details - (error.stack || error.message).toString() - // status - 400 - ) + let problem = Problem.from(error) $('#step-2-tick').hide() $('#step-2-cross').show() -- 2.34.1