Skip to content

Instantly share code, notes, and snippets.

View szymonkorytnicki's full-sized avatar
🎯
Focusing

szymonkorytnicki

🎯
Focusing
View GitHub Profile
@szymonkorytnicki
szymonkorytnicki / bot.html
Last active August 6, 2017 22:49
Messenger Declarative Bot #1
<response pattern="hello, hi, how are you, good morning" id="start">
<text>Hello!</text>
<text>How are you doing?</text>
<button href="#mood__fine">Fine</button>
<button href="#mood__bad">Could be better</button>
<button href="http://korytnicki.pl">Interesting website</button>
</response>
<response id="mood__fine" pattern="good, fine, great">
<text>Great!</text>
</response>
class Zagadka {
constructor(pass) {
this.pass = pass;
this.letters = [];
}
literka(letter) {
this.letters.push(letter.toLowerCase());
return this.haslo();
}
haslo() {
import axios from 'axios';
import { myModule } from './MyModule';
export function middleware() {
MyModule.log('sending a request'); // are you sure it works?
axios.get('http:/myservice.com'); // won't work! Depends on library compatibility with Edge Runtime
return NextResponse.next();
}
const { origin } = request.nextUrl.clone();
fetch(origin + '/api/myAPI', {
...
});
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;
// 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);
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];
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;
}
export const config = {
matcher: ['/about/:path*', '/dashboard/:path*'],
};
// 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!