Skip to content

Instantly share code, notes, and snippets.

View serebano's full-sized avatar

Sergiu Toderascu serebano

View GitHub Profile
@jeremyhewett
jeremyhewett / Deserialized.d.ts
Last active December 4, 2024 23:41
A Typescript generic to describe any object (or primitive) that has gone through JSON serialization and deserialization. Using JSON.stringify to serialize an object and then using JSON.parse to deserialize it will produce a result that can differ from the original in many ways. For instance, any function properties will be removed, any Date prop…
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]>;
@DouglasdeMoura
DouglasdeMoura / api.ts
Last active August 11, 2024 20:22
Tiny wrapper around fetch
// 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;
@atinux
atinux / sse.ts
Last active May 2, 2025 06:02
SSE endpoint example with Nuxt 3
// ~/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)
@olafsulich
olafsulich / parse-route-parameters.ts
Created September 7, 2021 20:35
TypeScript - parse route parameters
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,
@olafsulich
olafsulich / tuple-to-union-type.ts
Last active July 19, 2024 16:11
TypeScript - tuple to union type
type UnionFromTuple<Tuple extends readonly (string | number | boolean)[]> = Tuple[number];
const animals = ['🦙', '🦑', '🐍', '🦘'] as const;
type Animal = UnionFromTuple<typeof animals>;
// type Animal = '🦙' | '🦑' | '🐍' | '🦘'
(async () => console.log(
(await import(`data:text/javascript,export default "Is this a safe way to eval?"`)).default
))()
@abdelgrib
abdelgrib / http_helper_code.ts
Last active August 11, 2024 20:27
TypeScript - HTTP and Fetch API helper
export const Status = {
Ok: 200,
Unassigned: 299,
BadRequest: 400,
Unauthorized: 401,
NotFound: 404,
};
export const Methods = {
Get: "GET",
@dannysmc95
dannysmc95 / pure-downloaded-capacitor.js
Last active February 18, 2024 03:02
Download a file with pure Capacitor FileSystem and the Fetch API.
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();
@antony
antony / SimpleFileUploader.svelte
Created September 18, 2019 16:03
File Uploader Svelte
<label>
{#if uploading}
<Progress bind:percent={progress} text="Uploading..." />
{:else if processing}
<Progress percent={100} text="Processing..." />
{:else}
<slot name="content">
</slot>
{/if}
<input
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});