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

@apicase/services

v0.8.2

Published

Powerful API layer based on `@apicase/core`

Downloads

461

Readme

Apicase services

Powerful API layer based on @apicase/core

Full documentation

Quick start with Apicase
Apicase services

Installation

yarn add @apicase/services
npm install @apicase/services

Create service

import fetch from '@apicase/adapter-fetch'
import { ApiService } from '@apicase/services'

const SomeService = new ApiService({
  adapter: fetch,
  url: '/api/posts'
  method: 'GET'
})

// { "url": "/api/posts", "method": "GET", "query": { "userId": 1 } }
SomeService.doRequest({
  query: { userId: 1 }
})

ApiTree

To reduce boilerplate code, you can declare your services as JSON object

import { ApiTree } from '@apicase/services'

const api = new ApiTree([
  { url: '/api', children: [
    { url: 'posts', children: [
      { name: 'getAllPosts',   url: '',    method: 'GET'    },
      { name: 'createPost',    url: '',    method: 'POST'   },
      { name: 'getOnePost',    url: ':id', method: 'GET'    },
      { name: 'updateOnePost', url: ':id', method: 'PUT'    },
      { name: 'removeOnePost', url: ':id', method: 'REMOVE' }
    ] },
    { url: 'profile', children: [...] }
  ] }
])

api('getAllPosts').doRequest()
api('createPost').doRequest({ body })

Parent service

You can also pass parent service instead of adapter. It may flatten structure

const Root = new ApiService(fetch, { url: '/api' })

const api = new ApiTree(Root, [
  { url: 'posts', children: [
    { name: 'getAllPosts',   url: '',    method: 'GET'    },
    { name: 'createPost',    url: '',    method: 'POST'   },
    { name: 'getOnePost',    url: ':id', method: 'GET'    },
    { name: 'updateOnePost', url: ':id', method: 'PUT'    },
    { name: 'removeOnePost', url: ':id', method: 'REMOVE' }
  ] },
  { url: 'profile', children: [...] }
])

Shorter requests

api("someService", payload) === api("someService").doRequest(payload)

ApiObjectTree helper

Alternative way to avoid string api (but there are no children option):

import { ApiObjectTree } from '@apicase/services'

const api = new ApiTree(BaseService, {
  getAllPosts: { url: '' },
  createPost:  { url: '',    method: 'POST' },
  getOnePost:  { url: ':id', method: 'POST' },
  updOnePost:  { url: ':id', method: 'PUT' },
  rmvOnePost:  { url: ':id', method: 'DELETE' }
})

api.getAllPosts.doRequest()
api.createPost.doRequest({ body })

rest and wrappedRest helpers

Helper to work with REST APIs just automatically generates urls, methods and names

import { ApiTree, rest } from "@apicase/services"

/*
  If you are lucky - default structure doesn't need to write URL's
  postsGetAll: GET    /
  postsGetOne: GET    /:id
  postsCreate: POST   /
  postsUpdOne: PUT    /:id
  postsRmvOne: DELETE /:id
*/
const posts = rest("posts", ["getAll", "getOne", "create", "updOne", "rmvOne"])

/* Skip 2nd argument to get just all routes */
const posts = rest("posts")

/* Otherwise, still OK */
const profile = rest("profile", {
  getAll: { url: "we/have" },
  create: { url: "custom/routes" },
  getOne: { url: "no_refactoring/:id" },
  updOne: { url: "since_2008/:id" },
  rmvOne: { url: "legacy_shit" }
})

new ApiTree(Root, [
  { url: "posts", children: posts },
  { url: "profile", children: profile }
])

wrappedRest helper is similar to rest but also wraps it into url with name:

import { ApiTree, wrappedRest } from "@apicase/services"

new ApiTree(Root, [wrappedRest("posts"), wrappedRest("profile")])

License

MIT © Anton Kosykh