Skip to content

Instantly share code, notes, and snippets.

@elithrar
Created December 6, 2024 16:45
Show Gist options
  • Save elithrar/572562752aae21990be111b449e2d077 to your computer and use it in GitHub Desktop.
Save elithrar/572562752aae21990be111b449e2d077 to your computer and use it in GitHub Desktop.
workers.es - a (silly) example of writing Cloudflare Workers in Spanish (2024/12/6)
import { Solicitud, Respuesta, ContextoDeEjecucion, Entorno, Prometo, URL } from "./workers.es";
export default {
async obtener(solicitud: Solicitud, entorno: Entorno, contexto: ContextoDeEjecucion): Prometo<Respuesta> {
let url = new URL(solicitud.url);
let nombre = url.parametrosBusqueda.get("nombre") || "mundo";
return Respuesta.json({
mensaje: `hola ${nombre}`,
});
},
};
export type Prometo<T> = Promise;
export class URL {
constructor(url: string, base?: string | URLEspanol);
hash: string;
host: string;
hostname: string;
href: string;
readonly origen: string;
contrasena: string;
ruta: string;
rutaBase: string;
puerto: string;
protocolo: string;
busqueda: string;
parametrosBusqueda: URLSearchParams;
usuario: string;
toString(): string;
toJSON(): string;
}
export interface Solicitud extends Body {
readonly url: string;
readonly metodo: string;
readonly encabezados: Headers;
readonly redireccion: RequestRedirect;
readonly modo: RequestMode;
readonly credenciales: RequestCredentials;
readonly cache: RequestCache;
readonly referencia: string;
readonly referenciaPolicy: RequestReferrerPolicy;
readonly integridad: string;
readonly keepalive: boolean;
readonly senal: AbortSignal;
clonar(): Solicitud;
texto(): Promise<string>;
json<T>(): Promise<T>;
formData(): Promise<FormData>;
arrayBuffer(): Promise<ArrayBuffer>;
blob(): Promise<Blob>;
}
export interface Respuesta extends Body {
readonly ok: boolean;
readonly redirigido: boolean;
readonly estado: number;
readonly estadoTexto: string;
readonly encabezados: Headers;
readonly tipo: ResponseType;
readonly url: string;
clonar(): Respuesta;
texto(): Promise<string>;
json<T>(): Promise<T>;
formData(): Promise<FormData>;
arrayBuffer(): Promise<ArrayBuffer>;
blob(): Promise<Blob>;
}
export interface RespuestaConstructor {
new (cuerpo?: BodyInit | null, init?: ResponseInit): Respuesta;
error(): Respuesta;
redirigir(url: string, estado?: number): Respuesta;
json(datos: any, init?: ResponseInit): Respuesta;
}
export const Respuesta: RespuestaConstructor;
export interface ContextoDeEjecucion {
pasarAlFondo(promesa: Promise<any>): void;
esperarHasta(promesa: Promise<any>): void;
}
export interface Entorno {
[clave: string]: any;
}
export type ManejadorSolicitud = (solicitud: Solicitud, entorno: Entorno, contexto: ContextoDeEjecucion) => Promise<Respuesta>;
export interface TrabajadorNube {
obtener(solicitud: Solicitud, entorno: Entorno, contexto: ContextoDeEjecucion): Promise<Respuesta>;
}
export default TrabajadorNube;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment