Source code has been moved into ./src and distributed file goes to ./lib;
Distributed file(./lib/fetchql.js) is unminified now.
Pros:
lightweight
wrap query methods
easily set server-side
get enum types by names (with cache)
built-in interceptors
request state callbacks
written in ES2015 and modules
Usage
FetchQL distributes both ES module and CommonJS module
Import FetchQL:
import FetchQL from 'fetchql'
const FetchQL = require('fetchql')
ES module support is determined by your bundler tool(webpack, rollup, etc.).
Documentation
Class FetchQL
var Query = new FetchQL({ url, interceptors, headers, onStart, onEnd, omitEmptyVariables })
{url: '',// GraphQL server addressinterceptors: [],headers: {},// customized headers of all requests,onStart: function(requestQueueLength){},// callback of a new request queueonEnd: function(requestQueueLength){}// callback of a request queue finishedomitEmptyVariables: false,// remove null props(null or '') from the variables}
interceptor
interceptors is an optional parameter of the class. It can be an Array or an Object.
{url: '',interceptors: [interceptor]// or// interceptors: interceptor}// interceptor{request: function(url,config){// Modify the url or config herereturn[url,config];},requestError: function(error){// Called when an error occured during another 'request' interceptor callreturnPromise.reject(error);},response: function(response){// Modify the reponse objectreturnresponse;},responseError: function(error){// Handle an fetch errorreturnPromise.reject(error);}}
FetchQL.addInterceptors(interceptor[]|interceptor) => function
Add more interceptors. Arguments are same with interceptors of class.
It will return a function to remove added interceptors.
When FetchQL make a new request, if it belongs to a new queue(means there are no requests before), will call onStart().
By this, you can know that now there are some network requests within FetchQL.
When FetchQL finishes a request and find that there are no requests anymore, will call onEnd().
By this, you can know that all requests within FetchQL have been finished.
These two callbacks are useful when you want to watch the state of FetchQL's network requesting.
For example, you may have an indicator(loading spinner, loading bar) in your web application, with this feature you can easily manage the indicator's state(display or not);
omitEmptyVariables - Boolean(default to false) remove null props(null or '') from the variables in query
Sometimes null or '' could be different meanings in backend logic.
And if you just want the backend to ignore these variables, use this option to remove them.
Notice: Only properties those in an object of 'variables', will be removed.
Use index.mjs for Node (version >= 6.0) without babel, because Node doesn't support ES2015 modules natively.
Develop
npm install
./test/server.js could be used as a development server.
LICENSE
The MIT License (MIT)
Copyright (c) 2016 Cheng Gu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
请发表评论