2.0.0 • Published 2 years ago

flinj v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Flinj

:construction: This project is still in development. You should use it with caution.

The fasest way to build REST API

Flinching

Installation

npm i flinj

Usage

import { createApp } from 'flinj';
// import morgan from 'morgan';

const app = await createApp({
	controllersDir: '/path/to/controllers',
	middlewaresDir: '/path/to/middlewares',
	debug: true, // to see which routes was registered
});

// app.addMiddleware(morgan('tiny'));

app.start(3000);
// /path/to/controllers/auth.js

/** @type {import('flinj').Controller} */
export function GET(ctx) {
	const { firstName, lastName } = ctx.query;

	return { message: `Hello ${firstName} ${lastName}!` };
}

/** @type {import('flinj').Controller} */
export async function POST(ctx) {
	const { email, password } = ctx.body;

	await db.createUser({ email, password });
}

/** @type {import('flinj').Controller} */
export async function POST_login(ctx) {
	const { email, password } = ctx.body;

	const user = await login(email, password);
	ctx.setCookie('jwt', 'eyTOKEN', { httpOnly: true, secure: true, maxAge: 1000 * 60 * 60 * 24 * 3 });

	return user;
}

/** @type {import('flinj').Controller} */
export async function DELETE_$id(ctx) {
	const { id } = ctx.params;

	await db.deleteUser(id);
}
// /path/to/middlewares/auth.js

import { error } from 'flinj';

/** @type {import('flinj').Controller} */
export default async ctx => {
	const { cookies } = ctx;

	const token = cookies?.jwt;
	ctx.setHeaders({
		'x-custom-header': 'x-custom',
	});

	try {
		const tokenResponse = await validateToken(token);
		ctx.stuff.auth = {
			userId: tokenResponse.userId,
		};
	} catch (err) {
		throw error(401, 'Unauthorized');
	}
};

/** @type {import('flinj').Routes} */
export const use = ['auth/*'];

TOOD:

  • add tests
1.0.0-next.0

2 years ago

2.0.0-next.4

2 years ago

1.0.0-next.1

2 years ago

1.0.0-next.2

2 years ago

2.0.0-next.2

2 years ago

2.0.0-next.3

2 years ago

2.0.0-next.0

2 years ago

2.0.0-next.1

2 years ago

2.0.0

2 years ago

0.5.0

2 years ago

0.4.0

2 years ago

0.3.0

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.0

2 years ago