1.2.1 • Published 2 years ago

@nayotta/mta-fetch v1.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

mta-fetch

Build & Lint & Test

a simple browser fetch module.

install

$ npm install @nayotta/mta-fetch --save

example

const MtaFetch = require('@nayotta/mta-fetch')

const mtaFetch = new MtaFetch({
	host: 'http://example.com',
	apis: {
		signin: {
			url: '/v1/signin',
			method: 'post',
			errMsgs: {
				200: 'signin success',
				401: 'unauthorization',
				500: 'sever error'
			}
		},
		getInfo: {
			url: '/v1/books/type/:id',
			method: 'get'
		}
	}
})

mtaFetch.send({
	type: 'signin',
	// default content-type is application/json
	// form-data can be set by format argument
	data: {
		username: 'username',
		password: 'password'
	}
}).then(res => {
	const { ok, status, errMsg, data } = res
	console.log(ok, status, errMsg, data)
})

mtaFetch.setToken('Bearer token')

mtaFetch.send({
	type: 'getInfo',
	urlParams: {
		id: 'info_id'
	},
	query: {
		search: 'keyword'
	},
	errMsg: {
		200: 'success',
		400: 'bad request',
		401: 'unauthorization',
		404: 'not found',
		500: 'server error'
	}
}).then(res => {
	const { ok, status, errMsg, data } = res
	console.log(ok, status, errMsg, data)
})
1.2.0

2 years ago

1.2.1

2 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago