🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

fetch-default

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fetch-default

Add global default properties for the web fetch

Source
npmnpm
Version
1.0.13
Version published
Weekly downloads
103
-29.93%
Maintainers
1
Weekly downloads
 
Created
Source

fetch-default

Add global default properties for the web fetch

install

npm i fetch-default --save

use

require('fetch-default');

//es
import 'fetch-default';

default setting uri prefix

	fetch.default({
		uriPrefix: '/api'
	});

	//default request /api/async/get
	
	fetch('/async/get', {
    	method: 'GET'
		})
		.then((response) => response.json())
		.then((json) => console.log(json));
		

default setting optoin

fetch.default({ 
    method: 'GET',
   	headers: myHeaders,
   	mode: 'cors',
   	cache: 'default' 
   	...
});
/*
	//default
  	method: 'GET',
	headers: myHeaders,
 	mode: 'cors',
 	cache: 'default'
 	...
*/
    
	fetch('/async/get', {
    	method: 'POST'//覆盖默认设置后请求为post(covering)
		})
		.then((response) => response.json())
		.then((json) => console.log(json));

default setting beforeSend

fetch.default({ 
	beforeSend() {
		if(example)this.uri=this.uri+'/example';
	}
});

default setting dataFilter

fetch.default({ 
	dataFilter(response) {

    	if (!response.ok) {
    		//if you use fetch-abort (https://www.npmjs.com/package/fetch-abort)
    		this.abort()
        	message.error(`${response.status}\n${response.statusText}`);
    	}

    	return response.json();

	}
});

default setting fail

fetch.default({ 
	fail(e) {
		//if you use fetch-abort (https://www.npmjs.com/package/fetch-abort)
		this.abort()
    	message.error(e.toString());
	}
});

A complete example

fetch.default({
	method: 'POST',
	headers: {
    	'Accept': 'application/json',
    	'Content-Type': ' application/json',
	},
	credentials: 'include',
	beforeSend() {
    	//excludes serviceWorker
    	if (!/http:\/\//.test(this.uri)&&process.env.HOME_PAGE) this.uri = `${process.env.FETCH_PREFIX}${this.uri}`;
	},
	async dataFilter(response) {

    	//excludes serviceWorker request flies
    	if(!/^((ht|f)tps?):\/\/[\s\S]+\/[\s\S]+\.[\s\S]+$/.test(response.url)){

        	if (response.ok===false) {
            	message.error(`${response.status}\n${response.statusText}`);
            	return {}
        	}

        	let data = await response.json();

        	let {code,message:messageDes,messageBody} = data;

        	//not login
        	// if(code === 5000){
        	//     message.error(messageDes);
        	//     store.dispatch(actiontor.loginFlag(false));
        	// }

        	if(code !== '9000'){
            	message.error(messageDes);
        	}

        	return data;

    	}else{
        	return response
    	}

	},
	fail(e) {
    	message.error(e.toString());
    	return e
	}
});        		

Community

github npm

Keywords

fetch、web、global、default

FAQs

Package last updated on 11 Oct 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts