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

semantic-api

v2.0.0

Published

Create API url with powerful chainable way

Downloads

42

Readme

Semantic Api

npm Coverage Status Bundle License

🎏 SemanticApi provides a powerful way to declare and interact with the API url. ( <1kb )

繁體中文 | 簡體中文

Table of Contents

What is Semantic Api

Still remember how people hard-code the url or use es6 template literal to interpolate the variable in ?

SemanticApi provides a powerful way to declare and interact with the API Url.

const baseUrl = 'https://api.example.com/'
const options = { page: 2 }

const ex1 = baseUrl + "v4/user/" + UserID + "/filter?page=" + options.page

const ex2 = `${baseUrl}v4/user/${UserID}/filter?page=${options.page}`
// ====================================================================
const ex3 = SemanticApi(baseUrl).v4.user(UserID).filter.query(options)
// ====================================================================

// => https://api.example.com/v4/user/9527/filter?page=2

Install

SemanticApi targets Node.js 8.0+ and the latest version of Chrome/FF/Safari(NO IE).
This module is powered by ES6 Proxy ( Can I Use ), and there is no way to provide a fallback/polyfill for older browser/Node.js versions.

Node.js

  • Node.js >= 8.0
npm install semantic-api --save

Browser

Getting Started

import SemanticApi from 'semantic-api'

console.log(SemanticApi().try.to.test.api)
// => "try/to/test/api"

console.log(SemanticApi('/').user.id(9527).profile)
// => "/user/id/9527/profile"

Of course, it's recommended to use a wrapper on it.

import SemanticApi from 'semantic-api'

const API = {
    get spotify () {
        return SemanticApi('https://api.spotify.com/')
    }
}

API.spotify.music.category(7).filter.query({ premium: true })
// => https://api.spotify.com/music/category/7/filter?premium=true

Usage

Bind function

You can bind the function like fetch, axios to perform more actions within SemanticApi.

import SemanticApi from 'semantic-api'

class Instagram {
    static get api () {
        const baseUrl = 'https://api.instagram.com/'
        const customFn = {
            get: function (args, calls, url) { ... },
            post: function (args, calls, url) {
                return fetch(url, args.shift(), { method: 'post', ... })
            }
        }
        return SemanticApi(baseUrl, customFn)
    }

    static login (data) {
        const options = { client_id: 'CLIENT-ID', redirect_uri: 'REDIRECT-URI' }
        Instagram.api.oauth.authorize.query(options).post(data)
            .then(...)
    }
}

Instagram.login(...)
// POST https://api.instagram.com/oauth/authorize?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI

API

SemanticApi(baseUrl?, customMethods?)

baseUrl

Type: string
Default: ""

customMethods

Type: object
Default: {}

Example:

SemanticApi().api.user(9527).test.fnName(123, '456')
customMethods = {
    fnName: function (args, calls, url) {
        // args is the list of arguments. Ex: [123, '456']
        // calls is the list of access history. Ex: ['api', 'user', 9527, 'test']
        // url is the current url. Ex: 'api/user/9527/test'

        // it's ok to interact with calls
        calls.push('anotherFnName')

        // `return` will stop the chaining and return the value immediately.
        return fetch(url, options)
    }
}

.query(data)

Type: object

NOTICE: data didn't support nested object.
You can override the function in customMethods for better functionality.

const obj = { name: 'bob', age: 16, test: true }
SemanticApi().query(obj)
// => ?name=bob&age=16&test=true

Credits

License

MIT