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

dynamic-apis

v1.2.5

Published

dynamic apis ,use proxy to create restful controller.

Downloads

12

Readme

dynamic-apis

dynamic apis,use proxy create restful controller

install

npm i dynamic-apis

usage

create controller

import { createController } from "dynamic-apis";
//使用json-server 测试
const baseURL = "http://127.0.0.1:3000/";
const postCtrler = createController("posts", baseURL);
//with header
const postCtrler = createController("posts", baseURL, {
  "some-header": "header-value",
});
//dynamic header
let count = 0;
const postCtrler = createController("posts", baseURL, () => {
  return {
    "some-header": "header-value",
    "count-header": count++,
    AuthToken: getToken(),
  };
});

get all

let posts = await postCtrler.get();
//fetch   :GET  http://127.0.0.1:3000/posts

get by id

let posts = await postCtrler.get(1);
//fetch   :GET  http://127.0.0.1:3000/posts/1

//batch
let posts = await postCtrler.get(1, 2, 3);
//fetch   :GET  http://127.0.0.1:3000/posts/1,2,3
//warning json-server not support

query

let posts = await postCtrler.get({ author: "saharan" });
//fetch   :GET  http://127.0.0.1:3000/posts?author=saharan

let posts = await postCtrler.get({ author: ["saharan","you","others" });
//fetch   :GET  http://127.0.0.1:3000/posts?author=saharan&author=you&author=others

let posts = await postCtrler.get({ user: { author: "saharan" } });
//fetch   :GET  http://127.0.0.1:3000/posts?user.author=saharan

post

let result = await postCtrler.add({
  title: "dynamic api",
  author: "saharan",
});
//fetch :POST http://127.0.0.1:3000/posts
//body :{...}
//postCtrler.post also work

put

let result = await postCtrler.put(1, {
  title: "dynamic api",
  author: "saharan",
});
//fetch :PUT http://127.0.0.1:3000/posts/1
//body :{...}
//postCtrler.update also work

patch

let result = await postCtrler.modify(1, {
  author: "saharan",
});
//fetch :PATCH http://127.0.0.1:3000/posts/1
//body :{...}
//postCtrler.patch also work

delete

let result = await postCtrler.del(1);
//fetch :DELETE http://127.0.0.1:3000/posts/1

//batch
let result = await postCtrler.del(1, 2, 3);
//fetch :DELETE http://127.0.0.1:3000/posts/1,2,3

path,id support

const result = await postCtrler.id(2).comments.get();
//fetch :GET  http://127.0.0.1:3000/posts/1/comments
//  postCtrler.path(2).comments.get() also work

const addResult = await postCtrler.id(2).comments.add({
  body: "some comment!!!",
  postId: 2,
});
//fetch :POST  http://127.0.0.1:3000/posts/1/comments

//some server api name contains (get|add |...) method prefix

const result = await someCtrler.path("getUser").get(userId);

//fetch :GET http://127.0.0.1:3000/some.../getUser/userId

New Features

ActionsMap As A Arg

const postCtrler = createController(
  "posts",
  baseURL,
  {
    "some-header": "header-value",
  },
  (handler) => {}, //beforeSends   do something before send fetch request
  (handler) => {}, //afterReceives do something after receive data from respose
  { //actionsMap somthing.get() == something.fetch()  
    get: "fetch",
    post: ["add"],
    put: ["update"],
    patch: ["modify"],
    delete: ["del", "remove"],
  },
  ["get","add","update","modify"] //parseActions, getSomething =>fetch:GET  something 
);

Every Propertie Can Use As A Function

const result = await postCtrler(2).comments.get();
//fetch :GET  http://127.0.0.1:3000/posts/1/comments
//  postCtrler.path(2).comments.get() also work
//  postCtrler.id(2).comments.get();

const result = await postCtrler(2).comments(1).get();
//fetch :GET  http://127.0.0.1:3000/posts/1/comments/1