style: 🎨 deno fmt

This commit is contained in:
Julien Oculi 2024-06-13 12:43:29 +02:00
parent bdbe932872
commit 0f4c187c26
3 changed files with 34 additions and 21 deletions

View file

@ -4,14 +4,14 @@ import { respondApi } from '../../src/utils.ts'
export function handler(request: Request, ctx: FreshContext) {
// Check CSRF token
if (['POST', 'PATCH', 'PUT', 'DELETE', 'OPTIONS'].includes(request.method)) {
const session = SessionStore.getFromRequest(request)
const csrf = session?.get('_csrf')
if (['POST', 'PATCH', 'PUT', 'DELETE', 'OPTIONS'].includes(request.method)) {
const session = SessionStore.getFromRequest(request)
const csrf = session?.get('_csrf')
if (csrf === undefined || request.headers.get('X-CSRF-TOKEN') !== csrf) {
return respondApi('error', new Error('invalid csrf token'), 401)
}
}
if (csrf === undefined || request.headers.get('X-CSRF-TOKEN') !== csrf) {
return respondApi('error', new Error('invalid csrf token'), 401)
}
}
return ctx.next()
return ctx.next()
}

View file

@ -24,10 +24,13 @@ export function main() {
})
self.addEventListener('push', (event) => {
const { title, options } = (event.data?.json() ?? {}) as { title?: string, options?: Partial<NotificationOptions> }
const { title, options } = (event.data?.json() ?? {}) as {
title?: string
options?: Partial<NotificationOptions>
}
if (title) {
event.waitUntil(
self.registration.showNotification(title, options)
self.registration.showNotification(title, options),
)
}
})

View file

@ -5,15 +5,20 @@ export type JsonCompatible = JsonValue | { toJSON(): JsonValue } | unknown
export function respondApi<
Kind extends ApiPayload['kind'],
Payload extends JsonCompatible,
>(kind: Kind, payload?: Payload, status?: number, statusText?: string): Response {
>(
kind: Kind,
payload?: Payload,
status?: number,
statusText?: string,
): Response {
if (kind === 'error') {
return Response.json({
kind: 'error',
error: String(payload ?? ''),
} as ApiPayload, {
status: status ?? 500,
statusText
})
status: status ?? 500,
statusText,
})
}
return Response.json({
@ -30,16 +35,19 @@ export async function requestApi<
method: 'GET' | 'POST' | 'DELETE' | 'PATCH',
payload?: Payload | null,
): Promise<ApiResponse> {
const csrf = getCookie('_CSRF') ?? ''
const csrf = getCookie('_CSRF') ?? ''
const base = new URL('/api/', location.origin)
const endpoint = new URL(route.startsWith('/') ? `.${route}` : route, base.href)
const endpoint = new URL(
route.startsWith('/') ? `.${route}` : route,
base.href,
)
const response = await fetch(endpoint, {
method,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'X-CSRF-TOKEN': csrf
'X-CSRF-TOKEN': csrf,
},
body: payload ? JSON.stringify(payload) : null,
})
@ -63,7 +71,9 @@ export type ApiPayload<ApiResponse extends JsonCompatible = never> = {
}
function getCookie(name: string): string | undefined {
const cookiesEntries = document.cookie.split(';').map(cookie => cookie.trim().split('='))
const cookies = Object.fromEntries(cookiesEntries)
return cookies[name]
const cookiesEntries = document.cookie.split(';').map((cookie) =>
cookie.trim().split('=')
)
const cookies = Object.fromEntries(cookiesEntries)
return cookies[name]
}