style: 🎨 deno fmt
This commit is contained in:
parent
bdbe932872
commit
0f4c187c26
|
@ -4,14 +4,14 @@ import { respondApi } from '../../src/utils.ts'
|
||||||
|
|
||||||
export function handler(request: Request, ctx: FreshContext) {
|
export function handler(request: Request, ctx: FreshContext) {
|
||||||
// Check CSRF token
|
// Check CSRF token
|
||||||
if (['POST', 'PATCH', 'PUT', 'DELETE', 'OPTIONS'].includes(request.method)) {
|
if (['POST', 'PATCH', 'PUT', 'DELETE', 'OPTIONS'].includes(request.method)) {
|
||||||
const session = SessionStore.getFromRequest(request)
|
const session = SessionStore.getFromRequest(request)
|
||||||
const csrf = session?.get('_csrf')
|
const csrf = session?.get('_csrf')
|
||||||
|
|
||||||
if (csrf === undefined || request.headers.get('X-CSRF-TOKEN') !== csrf) {
|
if (csrf === undefined || request.headers.get('X-CSRF-TOKEN') !== csrf) {
|
||||||
return respondApi('error', new Error('invalid csrf token'), 401)
|
return respondApi('error', new Error('invalid csrf token'), 401)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.next()
|
return ctx.next()
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,10 +24,13 @@ export function main() {
|
||||||
})
|
})
|
||||||
|
|
||||||
self.addEventListener('push', (event) => {
|
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) {
|
if (title) {
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
self.registration.showNotification(title, options)
|
self.registration.showNotification(title, options),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
30
src/utils.ts
30
src/utils.ts
|
@ -5,15 +5,20 @@ export type JsonCompatible = JsonValue | { toJSON(): JsonValue } | unknown
|
||||||
export function respondApi<
|
export function respondApi<
|
||||||
Kind extends ApiPayload['kind'],
|
Kind extends ApiPayload['kind'],
|
||||||
Payload extends JsonCompatible,
|
Payload extends JsonCompatible,
|
||||||
>(kind: Kind, payload?: Payload, status?: number, statusText?: string): Response {
|
>(
|
||||||
|
kind: Kind,
|
||||||
|
payload?: Payload,
|
||||||
|
status?: number,
|
||||||
|
statusText?: string,
|
||||||
|
): Response {
|
||||||
if (kind === 'error') {
|
if (kind === 'error') {
|
||||||
return Response.json({
|
return Response.json({
|
||||||
kind: 'error',
|
kind: 'error',
|
||||||
error: String(payload ?? ''),
|
error: String(payload ?? ''),
|
||||||
} as ApiPayload, {
|
} as ApiPayload, {
|
||||||
status: status ?? 500,
|
status: status ?? 500,
|
||||||
statusText
|
statusText,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return Response.json({
|
return Response.json({
|
||||||
|
@ -30,16 +35,19 @@ export async function requestApi<
|
||||||
method: 'GET' | 'POST' | 'DELETE' | 'PATCH',
|
method: 'GET' | 'POST' | 'DELETE' | 'PATCH',
|
||||||
payload?: Payload | null,
|
payload?: Payload | null,
|
||||||
): Promise<ApiResponse> {
|
): Promise<ApiResponse> {
|
||||||
const csrf = getCookie('_CSRF') ?? ''
|
const csrf = getCookie('_CSRF') ?? ''
|
||||||
|
|
||||||
const base = new URL('/api/', location.origin)
|
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, {
|
const response = await fetch(endpoint, {
|
||||||
method,
|
method,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json; charset=utf-8',
|
'Content-Type': 'application/json; charset=utf-8',
|
||||||
'X-CSRF-TOKEN': csrf
|
'X-CSRF-TOKEN': csrf,
|
||||||
},
|
},
|
||||||
body: payload ? JSON.stringify(payload) : null,
|
body: payload ? JSON.stringify(payload) : null,
|
||||||
})
|
})
|
||||||
|
@ -63,7 +71,9 @@ export type ApiPayload<ApiResponse extends JsonCompatible = never> = {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCookie(name: string): string | undefined {
|
function getCookie(name: string): string | undefined {
|
||||||
const cookiesEntries = document.cookie.split(';').map(cookie => cookie.trim().split('='))
|
const cookiesEntries = document.cookie.split(';').map((cookie) =>
|
||||||
const cookies = Object.fromEntries(cookiesEntries)
|
cookie.trim().split('=')
|
||||||
return cookies[name]
|
)
|
||||||
|
const cookies = Object.fromEntries(cookiesEntries)
|
||||||
|
return cookies[name]
|
||||||
}
|
}
|
Loading…
Reference in a new issue