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

@kreisler/createapi

v3.0.0

Published

Un forma diferente de hacer peticiones a una API

Downloads

81

Readme

Create API

Create API is a simple package to create API requests with ease. @kreisler/createapi

Usage

Install package

npm i @kreisler/createapi

Import module

import { createApi } from "@kreisler/createapi";

or

Import commonjs

const { createApi } = require("@kreisler/createapi");

Example #1

(async () => {
  // createApi
  const api = createApi('https://nekobot.xyz/api')
  const output = await api.image({ type: 'neko' })
  // final url: https://nekobot.xyz/api/image?type=neko
  console.log({ output })
  // output: 
  // {
  // "success": true,
  // "message": "https://i0.nekobot.xyz/7/0/6/217595fa8b7cae5b7f6524169230c.png",
  // "color": 13419230,
  // "version": "2021070801"
  // }
})();

Example #2 Typescript

interface Adviceslip {
  advice: () => Promise<{
    slip: {
      id: number
      advice: string
    }

  }>
}
const adviceslip = async () => {
  // createApi
  const api: Adviceslip = createApi('https://api.adviceslip.com')
  const output = await api.advice()
  // final url: https://api.adviceslip.com/advice
  console.log({ output }, output.slip.advice)
  /* {
    output: {
      slip: {
        id: 135,
        advice: 'If you want to be happily married, marry a happy person.'
      }
    }
  } */
}
adviceslip()

Example #3 Typescript


import { createApi } from "@kreisler/createapi";
interface NekoBot {
  image: (arg: {
    type: 'neko' | 'food' | 'coffee' | '4k'
  }) => Promise<{
    success: boolean
    message: string
    color: number
    version: string
  }>
}
const neko = async () => {
  const api: NekoBot = createApi('https://nekobot.xyz/api')
  const res = await api.image({ type: 'neko' })
  console.log(res)
  /* {
    success: true,
    message: 'https://cdn.nekobot.xyz/a/e/4/72cc9cda5e8db2b370991737314b4.png',
    color: 15655656,
    version: '2021070801'
  } */
}
neko()

Example #4 Typescript Advanced

import { createApi } from "@kreisler/createapi";
// replace any with your types or interfaces
interface PostmanEcho {
  get: (id?: any, params?: any, extraparams?: RequestInit) => Promise<any>
  post: (id?: any, params?: any, extraparams?: RequestInit) => Promise<any>
  put: (id?: any, params?: any, extraparams?: RequestInit) => Promise<any>
  delete: (id?: any, params?: any, extraparams?: RequestInit) => Promise<any>
  [key: string]: (id?: any, params?: any, extraparams?: RequestInit) => Promise<any>
}

const postman = createApi('https://postman-echo.com', {
  headers: {
    'Content-Type': 'application/json'
  }
}) as PostmanEcho

postman.get({ id: 1 }, null, { method: 'GET' }).then(console.log)
postman.post(null, null, { method: 'POST', body: JSON.stringify({ id: 1 }) }).then(console.log)
postman.put(null, null, { method: 'PUT', body: JSON.stringify({ id: 1 }) }).then(console.log)
postman.delete(null, null, { method: 'DELETE', body: JSON.stringify({ id: 1 }) }).then(console.log)