npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

purratz

v1.1.4

Published

A little Framework for tiny REST API

Downloads

18

Readme

Logo Purratz ><

purratz

Micro back-end framework based on Koa. Plugin for auth, database coming soon.\

purratz doc

to access all the documentation, go to the github purratz.

Version 1.1.1

Add jsDocs.

New Feature : MultiMiddleware

You can now add a second middleware in your controller.

import { Ctrl }  from "purratz";
import PiratzService from "./piratkatz.service";

Ctrl.initService(PiratzService); // inject your service
Ctrl.guard(myGuard); // inject your service
Ctrl.get('/purr',_=> Ctrl.service().purr(), ()=> 'second middleware'); // With second Middleware
Ctrl.get('/wouaf',_=> Ctrl.service().wouaf(), true, ()=> 'second middleware'); // With second Middleware AND guard
Ctrl.get('/forbidden',_=> Ctrl.service().forbidden(), true); // With just a guard.

export default Ctrl.export('/');

Getting Started

install Purratz from npm :

npm install --save purratz

You can use Purratz-cli from npm :

npm install -g purratz-cli

and use this command for create a new project with Purratz-cli:

purratz-create-app

import / require

Purratz-cli support import / export. If you use just Purratz, use require().

Create a server:

Default port is 3005. For change, add PORT in .env.\

Simple mode

You can use the simple mode. The CORS is enabled and the body parser accept 'JSON, TEXT and FORM'.

import server from 'purratz';
server.autoConfig();

Classic mode

import { Server }  from 'purratz';
    const corsOptions = {
        credential: true
    };
const parserOptions = ['text','json'];

Server.cors(corsOptions);
Server.bodyParser(parserOptions);

Create a controller

First, you must create a folder in src.
Exemple :

piratz

then, create file name.controller.js. Name is a same of folder.
Exemple:

piratz.controller.js

You must import ctrl from Purratz :

import { Ctrl } from "purratz";

or

import { ctrl } from "purratz";
const Ctrl = new ctrl();

You can fix problem with guard like this

and export your controller with your global endpoint:

export default Ctrl.export('/piratz');

Now, you can add routes :

import { Ctrl } from "purratz";

Ctrl.get('/string', _=> Ctrl.ctx("Hello World !"));
// return "Hello World !"

Ctrl.get('/object', _=> Ctrl.ctx({data: "Hello World !", status: 200})); 
//return "{data: Hello World !}"

Ctrl.get('/withfunc', _=> Ctrl.ctx(()=> {
    return {message: "Hello World !", status: 200}
}));

//return "{message: Hello World !}"

export default Ctrl.export('/cat');

with Ctrl.ctx(path, service), you can return a string, an object, number, boolean or function (but function must be return a value). If return an object with status key, this status is add to the response status and it's don't appear in response body.\

verb

you can use verb :

  • Ctrl.get()
  • Ctrl.post()
  • Ctrl.delete()
  • Ctrl.put()

Adding routes in server

In your index, add this:

import piratzCtrl from "./src/piratz/piratz.controller";

server.addRoutes(piratzCtrl);

server.addRoutes() can take one controller or array of controllers.

import piratzCtrl from "./src/piratz/piratz.controller";
import someCtrl from "./src/some/some.controller";

server.addRoutes([
    piratzCtrl,
    someCtrl
]);

Service for good controller

Ctrl.ctx() is a basic method for return value with route.

create a service

you can inject a service in controller :

import { Ctrl }  from "purratz";
import PiratzService from "./piratkatz.service";

Ctrl.initService(PiratzService); // inject your service
Ctrl.get('/purr',_=> Ctrl.service().purr()); // use your service in controller
Ctrl.get('/re/:id',_=> Ctrl.service().reqWithParams());
Ctrl.get('/woof',_=> Ctrl.service().woof());
Ctrl.get('/async', _=> Ctrl.service().bodyAsync());

export default Ctrl.export('/');

use parent class Service

For inject ctx and next and global functions. your function must return a any value. Return is inject in body response. Exemple of service :

import { Service } from "purratz"; // import Service for extend class

export default class PiratkatzService  extends Service{ 

    purr() {return "RonRonRonRonRonRon..."} //just return a string.

    async reqWithParams(){
        const id = this.params.id // acces to GET params with this.params.
        return `Your ID is ${id}`; // inject return in response body.
    }

    async bodyAsync(){
        const p1 =  new Promise(resolve => {
            resolve("Cool !")
        });
        return this.withPromise(p1) //you can use this.withPromise() for return async result. 
    }

    woof(){
        //return a object.
        return {
            status: 403,
            message: "my message",
            dog: 'medor',
            say: "Wouaf wouaf!"
        }
    }
}

add a second middleware

you can inject a second middleware in your controller :

import { Ctrl }  from "purratz";
import PiratzService from "./piratkatz.service";

Ctrl.initService(PiratzService); // inject your service
Ctrl.guard(myGuard); // inject your service
Ctrl.get('/purr',_=> Ctrl.service().purr(), ()=> 'second middleware'); // With second Middleware
Ctrl.get('/wouaf',_=> Ctrl.service().wouaf(), true, ()=> 'second middleware'); // With second Middleware AND guard
Ctrl.get('/forbidden',_=> Ctrl.service().forbidden(), true); // With just a guard.

export default Ctrl.export('/');

Statics endpoints assets

See the doc here.

DTO

for add DTO. See the doc here.

Guard

Add middleware route control access. See the doc here.

Utils functions

Some built-in utility functions. See the doc here.