UNPKG

@thepassle/app-tools

Version:

Collection of tools I regularly use to build apps. Maybe they're useful to somebody else. Maybe not. Most of these are thin wrappers around native API's, like the native `<dialog>` element, `fetch` API, and `URLPattern`.

29 lines (27 loc) 684 B
function getCookie(name, _document = document) { const match = _document.cookie.match(new RegExp(`(^|;\\s*)(${name})=([^;]*)`)); return match ? decodeURIComponent(match[3]) : null; } /** * @param {{ * xsrfCookieName?: string, * xsrfHeaderName?: string, * }} options * @returns {import('../index.js').Plugin} */ export function xsrfPlugin({ xsrfCookieName = 'XSRF-TOKEN', xsrfHeaderName = 'X-CSRF-TOKEN' } = {}) { const csrfToken = getCookie(xsrfCookieName); return { name: 'xsrf', beforeFetch: (meta) => { if(csrfToken) { meta.headers.set(xsrfHeaderName, csrfToken); } return meta; } } } export const xsrf = xsrfPlugin();