Skip to content

Instantly share code, notes, and snippets.

View szymonkorytnicki's full-sized avatar
🎯
Focusing

szymonkorytnicki

🎯
Focusing
View GitHub Profile
{"zzz":"","a2":24,"5":"BUI Technologies","0":19,"1":[{"t":{"a":[{"b":1016,"a":0}],"b":0},"b":{"a":[{"a":680,"b":1016}],"b":0},"d":{"b":0,"a":[{"a":340,"b":1016}]},"e":{"a":[{"b":1016,"a":140}],"b":0},"d0":23,"4":"MaterialIcons-Regular","c":{"a":[{"a":306,"b":1016}],"b":0},"z":"4","f":"uicol_systemCyan-100","3":""},{"e":{"b":0,"a":[{"b":1016,"a":140}]},"t":{"b":0,"a":[{"b":1016,"a":0}]},"d0":20,"c":{"a":[{"b":1016,"a":666}],"b":0},"b":{"a":[{"b":1016,"a":700}],"b":0},"d":{"a":[{"b":1016,"a":340}],"b":0},"4":"MaterialIcons-Regular","z":"4","3":"","f":"uicol_systemCyan-100"},{"c":{"b":0,"a":[{"a":486,"b":1016}]},"z":"4","3":"VPN 5","b":{"b":0,"a":[{"a":700,"b":1016}]},"f":"uicol_systemCyan-100","t":{"a":[{"a":0,"b":1016}],"b":0},"4":"ss_","d":{"b":0,"a":[{"b":1016,"a":340}]},"e":{"a":[{"a":140,"b":1016}],"b":0},"d0":19},{"t":{"b":0,"a":[{"a":0,"b":1224}]},"d":{"a":[{"a":720,"b":1224}],"b":0},"1":"System Semi Bold","66":[{"6":"Endpoint","19":"GET","18":"https:\/\/YOURENDPOINT.deno.dev\/","5":"JSON Endpoint","23
@szymonkorytnicki
szymonkorytnicki / dash.ts
Created April 24, 2023 20:28
Beeminder widgy utils
import { serve } from "https://deno.land/std@0.155.0/http/server.ts";
serve(async (req: Request) => {
const data = await fetch(`https://www.beeminder.com/api/v1/users/${Deno.env.get('username')}/goals.json?auth_token=${Deno.env.get('token')}`).then(d => d.json());
let goals = "";
let load = 0;
let productivityScore = 0;
data.forEach((curr) => {
const urgencyLoad = Math.max(0, 7 - curr.safebuf);
const productivity = Math.min(curr.safebuf, 7);
const { pathname, origin, locale, search, protocol } = request.nextUrl;
// const { pathname } = request.nextUrl;
function isPage(pathname: string): boolean {
return !(pathname.includes('.') || pathname.startsWith('/api') || pathname.includes('_next'));
}
// and consequently, you can work out how to identify a file a or an API route with this snippet!
export const config = {
matcher: ['/about/:path*', '/dashboard/:path*'],
};
export async function middleware(request: NextRequest) {
const response = NextResponse.next();
for await (const middlewareFunction of MIDDLEWARES) {
const middlewareResponse = await middlewareFunction(request, response);
if (isRedirecting(middlewareResponse)) {
return middlewareResponse;
}
if (isRewriting(middlewareResponse)) {
return middlewareResponse;
}
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { anotherMiddleware } from './middlewares/anotherMiddleware';
import { someMiddleware } from './middlewares/someMiddleware';
type middlewareFunction = (request: NextRequest, response: NextResponse) => NextResponse | Promise<NextResponse>;
const MIDDLEWARES: middlewareFunction[] = [someMiddleware, anotherMiddleware];
// works, get replaced in build time
const someVar = process.env.MY_VARIABLE;
// doesn't work! How should Next know how to replace it in build time?
function getVariable(name) {
return process.env[name];
}
const someVar = getVariable(MY_VARIABLE);
// try this!
console.log(process.env);
const handler: NextApiHandler = (request: NextApiRequest, response: NextApiResponse) => {
MyModule.log('sending a request'); // yup, no worries
axios.get('http://myservice.com'); // fine, why not!
return response.status(200).json({ ok: true });
};
export default handler;
const { origin } = request.nextUrl.clone();
fetch(origin + '/api/myAPI', {
...
});