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

@protorians/air-rest

v0.1.8

Published

api rest manager

Downloads

7

Readme

Protorians AirREST

Protorians AirREST est un gestionnaire d'API REST côté client.

Installation

Via PNPM

pnpm install @protorians/api-rest

Via YARN

yarn add @protorians/api-rest

Via NPM

npm install @protorians/api-rest

Importations

Toute la classe en default

import AirREST from "@protorians/air-rest";
import { AirRestEndPoint, AirRestServer } from "@protorians/air-rest";

Typages

IAirMethods

'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'

IEndpointResponse

{
  [ R : string ] : any
}

IEndpointPayload

{
  [ P : string ] : any
}

À vous de définir : Payload — Charge utile

Définir la liste des paramètres à passer dans le body du fetch (BodyInit) sous forme d'object

interface myPayload extends IEndpointPayload {
  id: number;
}

À vous de définir : Réponses

Définir la réponse attendu.

interface myResponse extends IEndpointResponse {
  response: boolean;
  cause: string;
}

Utilisation

Serveur des points de chute

const myAirRestServer = () =>
  new AirREST.Server("https://example.com/api", {
    cache: "no-store",
  });

Utilisation du point de chute & méthodes

Méthode GET

const myEndpoint = () =>
  useEndpoint<myPayload, myResponse>()
    .use(myAirRestServer())
    .route("/my-end-point")
    .method("GET");

Méthode POST

const myEndpoint = () =>
  useEndpoint<myPayload, myResponse>()
    .use(myAirRestServer())
    .route("/my-end-point")
    .method("POST");

Méthode PUT

const myEndpoint = () =>
  useEndpoint<myPayload, myResponse>()
    .use(myAirRestServer())
    .route("/my-end-point")
    .method("PUT");

Méthode PATCH

const myEndpoint = () =>
  useEndpoint<myPayload, myResponse>()
    .use(myAirRestServer())
    .route("/my-end-point")
    .method("PATCH");

Méthode DELETE

const myEndpoint = () =>
  useEndpoint<myPayload, myResponse>()
    .use(myAirRestServer())
    .route("/my-end-point")
    .method("DELETE");

Resolution du point de chute

myEndpoint()
  .payload({
    id: 7,
  })
  .send()
  ?.then((response) => {
    console.log("Response", response);
  });

Classe : AirRestServer

AirRestServer

Création du server de point de chute

const server = new AirRestServer("https://exemple.com/api");

Avec options

const server = new AirRestServer("https://exemple.com/api", {
  cache: "no-store",
});

* Les options sont du même type que ceux de la fonction fecth RequestInit

AirRestServer.post

Resoudre le point de chute avec la méthode POST

const server = new AirRestServer("https://exemple.com/api");
const endpoint = new AirRestEndPoint<PayloadInterface, ResponseInterface>();
...
server.post<ResponseInterface>( endpoint ).then( response => console.log(response) )

AirRestServer.get

Resoudre le point de chute avec la méthode GET

const server = new AirRestServer("https://exemple.com/api");
const endpoint = new AirRestEndPoint<PayloadInterface, ResponseInterface>();
...
server.get<ResponseInterface>( endpoint ).then( response => console.log(response) )

AirRestServer.put

Resoudre le point de chute avec la méthode PUT

const server = new AirRestServer("https://exemple.com/api");
const endpoint = new AirRestEndPoint<PayloadInterface, ResponseInterface>();
...
server.put<ResponseInterface>( endpoint ).then( response => console.log(response) )

AirRestServer.patch

Resoudre le point de chute avec la méthode PATCH

const server = new AirRestServer("https://exemple.com/api");
const endpoint = new AirRestEndPoint<PayloadInterface, ResponseInterface>();
...
server.patch<ResponseInterface>( endpoint ).then( response => console.log(response) )

AirRestServer.delete

Resoudre le point de chute avec la méthode DELETE

const server = new AirRestServer("https://exemple.com/api");
const endpoint = new AirRestEndPoint<PayloadInterface, ResponseInterface>();
...
server.delete<ResponseInterface>( endpoint ).then( response => console.log(response) )

Classe : AirRestEndPoint

AirRestEndPoint

Création d'un point de chute

const endpoint = new AirRestEndPoint<PayloadInterface, ResponseInterface>();

AirRestEndPoint._route

La route utilisé

const endpoint = new AirRestEndPoint<PayloadInterface, ResponseInterface>();
endpoint.route("/test");
console.log(endpoint._route); // test

endpoint.route("/hello");
console.log(endpoint._route); // hello

AirRestEndPoint._payload

Les paramètres de la requête utilisé

const endpoint = new AirRestEndPoint<PayloadInterface, ResponseInterface>();
endpoint.payload({
  test: "test",
});
console.log(endpoint._payload); // { "test" : test" }

endpoint.payload({
  test: "hello",
});
console.log(endpoint._payload); // { "test" : hello" }

AirRestEndPoint._method

La méthode de la requête utilisé

const endpoint = new AirRestEndPoint<PayloadInterface, ResponseInterface>();
endpoint.method("GET");
console.log(endpoint._method); // GET

endpoint.method("POST");
console.log(endpoint._method); // POST

AirRestEndPoint.use

Utilisation avec un serveur de points de chutes

const server = new AirRestServer();
const endpoint = new AirRestEndPoint<PayloadInterface, ResponseInterface>();
endpoint.use(server);

AirRestEndPoint.method

Definir la méthode de la requête

  • method : IAirMethods
const endpoint = new AirRestEndPoint<PayloadInterface, ResponseInterface>();
endpoint.method("GET");

AirRestEndPoint.route

Definir la route du point de chute de la requête

  • route : string
const endpoint = new AirRestEndPoint<PayloadInterface, ResponseInterface>();
endpoint.route("/my-endpoint-route");

AirRestEndPoint.slugs

Definir des variables et leurs positions dans la route. Dans le cadre où la route serait générique, donc comporterait par exemple des sous-dossiers.

  • ...slugs : (string | number)[]

Exemple : Pour my-endpoint-route/{user-id}, la route à utiliser doit être my-endpoint-route/{$1}

const endpoint = new AirRestEndPoint<PayloadInterface, ResponseInterface>();
endpoint.route("/my-endpoint-route/$1").slugs("user-id-here");

AirRestEndPoint.payload

Definir la charge utile (paramètres dans la requête) à envoyer au point de chute.

  • payload : PayloadInterface
const endpoint = new AirRestEndPoint<PayloadInterface, ResponseInterface>();
endpoint.payload({
  param1: "value-1",
  param2: "value-2",
});

AirRestEndPoint.send

Resoudre le point de chute executant les caratéristiques construites

const endpoint = new AirRestEndPoint<PayloadInterface, ResponseInterface>();
endpoint.send();

Exemple de construction de point de chute

const endpoint = new AirRestEndPoint<PayloadInterface, ResponseInterface>();
endpoint

  // Definition du serveur
  .use(
    new AirRestServer("https://example.com/api", {
      cache: "no-store",
    })
  )

  // Route Générique
  .route("/my-end-point/$1/$2")

  // Mise à jour de la route avec de nouveau paramètre
  .slugs("slug-1", "slug-2")

  // Paramètres
  .payload({
    param1: "value-1",
    param2: "value-2",
  })

  //Envoie
  .send();

Fonctionnalité : stringifyPayload

Convertir un object de type IEndpointPayload en tableau de chaînes de caractères.

const query = stringifyPayload({
  param1: "value-1",
  param2: "value-2",
});

console.log(query);

Fonctionnalité : useEndpoint

Retourne une nouvelle instance de AirRestEndPoint pour enchaîner les définitions.

useEndpoint<PayloadInterface, ResponseInterface>().route('/end-point-route')...

Fonctionnalité : render

Résoudre un fetch.

  • endpoint : RequestInfo
  • options : RequestInit | undefined
render('https//exemple.com/test', {
  cache: 'no-store,
  method: 'GET'
})

Soutenir le développement

Via Paypal

"Faire un don paypal"


License MIT

Copyright 2023 — Protorians

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.