This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Deserialized<T> = | |
unknown extends T ? unknown : | |
T extends JSONPrimitive ? T : | |
T extends { toJSON: () => infer R } ? Deserialized<R> : | |
T extends Array<infer U> ? Deserialized<U>[] : | |
T extends object ? DeserializedObject<T> : | |
never; | |
type DeserializedObject<T extends object> = { | |
[K in keyof T as T[K] extends NotAssignableToJson ? never : K]: Deserialized<T[K]>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Extends the return of the HTTPError class | |
class HTTPError extends Error { | |
readonly response: any; | |
readonly status: number; | |
readonly statusText: string; | |
constructor(status: number, statusText: string, response: any) { | |
super(statusText); | |
this.status = status; | |
this.statusText = statusText; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ~/server/api/sse.ts | |
export default defineEventHandler(async (event) => { | |
if (!process.dev) return { disabled: true } | |
// Enable SSE endpoint | |
setHeader(event, 'cache-control', 'no-cache') | |
setHeader(event, 'connection', 'keep-alive') | |
setHeader(event, 'content-type', 'text/event-stream') | |
setResponseStatus(event, 200) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ParseRouteParameters<Route> = Route extends `${string}/:${infer Param}/${infer Rest}` | |
? { [Entry in Param | keyof ParseRouteParameters<`/${Rest}`>]: string } | |
: Route extends `${string}/:${infer Param}` | |
? { [Entry in Param]: string } | |
: {}; | |
type X = ParseRouteParameters<'/api/:what/:is/notyou/:happening'>; | |
// type X = { | |
// what: string, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type UnionFromTuple<Tuple extends readonly (string | number | boolean)[]> = Tuple[number]; | |
const animals = ['🦙', '🦑', '🐍', '🦘'] as const; | |
type Animal = UnionFromTuple<typeof animals>; | |
// type Animal = '🦙' | '🦑' | '🐍' | '🦘' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async () => console.log( | |
(await import(`data:text/javascript,export default "Is this a safe way to eval?"`)).default | |
))() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const Status = { | |
Ok: 200, | |
Unassigned: 299, | |
BadRequest: 400, | |
Unauthorized: 401, | |
NotFound: 404, | |
}; | |
export const Methods = { | |
Get: "GET", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const downloadFile = async (uri, path, folder) => { | |
return new Promise(async (resolve, reject) => { | |
try { | |
const file_request = await fetch(uri, { | |
method: 'GET', | |
credentials: 'include', | |
mode: 'cors', | |
}); | |
const file_blob = await file_request.blob(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<label> | |
{#if uploading} | |
<Progress bind:percent={progress} text="Uploading..." /> | |
{:else if processing} | |
<Progress percent={100} text="Processing..." /> | |
{:else} | |
<slot name="content"> | |
</slot> | |
{/if} | |
<input |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From: https://stackoverflow.com/questions/23150333/html5-javascript-dataurl-to-blob-blob-to-dataurl | |
//**dataURL to blob** | |
function dataURLtoBlob(dataurl) { | |
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1], | |
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); | |
while(n--){ | |
u8arr[n] = bstr.charCodeAt(n); | |
} | |
return new Blob([u8arr], {type:mime}); |
NewerOlder