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

ajax-interface-checker

v0.3.0

Published

A CLI of npm package publish

Downloads

10

Readme

ajax-interface-checker

Introduction

ajax-interface-checker module would offer your all supports in http request ,
which contains http request methods 、 http error throw and catch module 、
http proxy module and http window module .

When you are developing you program , you could use ajax-interface-checker 
to setting mock data 、test your page when different errors happened 、and 
use real runtime window to show it .

Note that , i used `Promise` 、 `Map` and other ES6 feathers , but i do NOT 
import their compatible code , because i suppose that you would do it in your
project . Right ?

module

HttpAuth

`HttpAuth` module helps you to auth your ajax result , but it aimed to handle
common logic for your ajax code .  

Use Pattern

HttpAuth(ajaxRes , <statusKey> , <messageKey> , <dataKey>) ;

How to use

import { HttpAuth } from "ajax-interface-checker" ;
// normal ajax result mock 
let mockRes = { code : 0 , message : 'ok' , data : {} } ;
HttpAuth(mockRes) ; // mockRes.data
// error ajax result mock
let mockResError = { code : -1 , message : 'database connect error' } ;
HttpAuth(mockResError) ; // mockRes.data

==================================

Http

Http offers three basic methods , get 、post and jsonp , to get server data .
I use axios and jsonp module to do these . You could use theme as bellow . 

And I would offer cache control , but you should be more careful to USE it .  

I would also offer http error test , you could use `options.hasOpenError` to run it 
as bellow value .

Use Pattern

let http = new Http(<options>) ;
http.auth = function(){} ;
// get
http.get(pathname , <params> , <options> , <baseHost> ) ;
// post
http.post(pathname , <data> , <params> , <options> , <baseHost>) ;
// jsonp
http.jsonp(pathname , <params> , <options> , <baseHost>) ;
  • You could rewrite http.auth to auth your ajax result , especially some same error logic handler .

  • http.get method overcames a get request .

    • pathname sets pathname of your get request , link '/getNameList'
    • params sets params , which your want to catch , it is a object .
    • options sets basic options of ajax
      • cache controls whether to use ajax cache
      • cacheCount sets the max count of ajax cache
      • hasOpenError whether to open error test
    • baseHost sets base host of your get request , link "//localhost" .
  • http.post method overcames a post request .

    • pathname sets pathname of your post request , link '/getNameList'
    • data sets data , which your want to catch , it is a object .
    • params sets params , which your want to catch , it is a object .
    • options sets basic options of ajax for example cache 、 cacheCount and so on .
      • cache controls whether to use ajax cache
      • cacheCount sets the max count of ajax cache
      • hasOpenError whether to open error test
    • baseHost sets base host of your get request , link "//localhost" .
  • http.jsonp method overcames a jsonp request .

    • pathname sets pathname of your jsonp request , link '/fetchNameList'
    • params sets params , which your want to catch , it is a object .
    • options sets basic options of ajax for example cache 、 cacheCount and so on .
      • cache controls whether to use ajax cache
      • cacheCount sets the max count of ajax cache
      • hasOpenError whether to open error test
    • baseHost sets base host of your jsonp request , link "//localhost" .

How to use

In your config.js file , you should use NODE_ENV to setting different env about 
server setting . 

config.js

var env = (process.env.NODE_ENV || "development").trim() ;

var config = {
	env: env ,
} ;
switch (env){
	case 'production' :
		Object.assign(config , {
			'protocol': 'https' ,
			'NODE_ENV': env ,
			'apihost' : '' ,
			'domain'  : ''
		}) ;
		break ;
	default : 
		Object.assign(config , {
			'protocol': 'http' ,
			'NODE_ENV': env ,
			'apihost' : '' ,
			'domain'  : ''
		}) ;
		break ;
} ;
module.exports = config ;
client useage

utils/ajax.js

import { Http } from "ajax-interface-chekcer" ;
let http = new Http({
	apihost : "//localhost" ,
	jsonpHost : "//localhost"
}) ;
// reset ajax data auth
http.auth = function(res){
	let { code = 0 , data = {} , message = 'ok' } = res ;
	switch(code){
		case 0 :
			return data ;
		case -1 :
		default :
			console.log("ajax interface error");
			return res ;
	}
} ;
http.get('/getList' , { page : 1 , pagesize : 20 } , {cache : true}).then(data => data) ;
http.post('/fetchList' , { page : 1 , pagesize : 20 } , {cache : true}).then(data => data) ;
http.jsonp('/getList' , { page : 1 , pagesize : 20 } , {cache : true}).then(data => data) ;
server useage
I would use `request` module to run server ajax , like form node to java .
import { Http } from "ajax-interface-checker" ;
import config from "../../config/index" ;
import httpCommonFail from "./httpCommonFail" ;
import request from "request" ;
import auth from "./auth" ;

// config http ajax aim source url
let httpInstance = new Http({
  apihost : config.apihost ,
  jsonpHost : config.jsonphost
}) ;

// default server exception catch ,
// this catching function had been overcomed in ajax-interface-checker ,
// you can rewrite it if you need .
httpInstance.httpCommonFail = httpCommonFail ;

// default interface exception catch ,
// this catching function had been overcomed in ajax-interface-checker ,
// you can rewrite it if you need .
httpInstance.auth = auth ;

// rewrite http request method to run node request 
httpInstance.request = function(params = {}){
	return new Promise((resolve , reject) => {
		request(params , function(err , response , body = ""){
			// err reject
			if(err){
				reject(err) ;
				return ;
			}
			// response
			if(response.statusCode * 1 === 200){
				let res = null ;
				// 兼容返回的不是对象的问题
				try {
					res = JSON.parse(body) ;
				}catch(err){
					res = {
						state : 0 ,
						code : 0 ,
						data : body
					}
				}
				resolve({ data : res }) ;
			}
		}) ;
	}) ;
}	

==================================

About Browser Compatible

IE9+ and all modern browser

LINCESE

MIT