Network
The Network package includes an opinionated fetch
api and also ships a http
driver by default powered by axios
, but you can always write your own http plugin implementation whenever you need. Flew also provides other drivers as plugins so you can easily switch data source in runtime without the headache of refactoring the whole code base. Take a look at firebase
and parse
plugins for more details.
Install
npm install @flew/core @flew/network
Configure
import { setup } from '@flew/core';
setup({
options: {
silent: true, // disable internal logs
driver: 'http', // set default driver
baseURL: 'https://api.thecatapi.com',
endpoint: '/v1',
httpConfig: {
timeout: 60 * 1000 * 10,
// any other http option
},
},
plugins: [
// ... list of custom or oficial plugins
],
});
const { setup } = require('@flew/core');
setup({
options: {
silent: true, // disable internal logs
driver: 'http', // set default driver
baseURL: 'https://api.thecatapi.com',
endpoint: '/v1',
httpConfig: {
timeout: 60 * 1000 * 10,
// any other axios http option
},
},
plugins: [
// ... list of custom or oficial plugins
],
});
Network call
import { fetch } from '@flew/network';
// Get a random kitty
fetch('kitty')
.get('/images/search?size=small&mime_types=gif')
.subscribe(
kitty => console.log(kitty),
err => console.log(err),
);
const { fetch } = require('@flew/network');
// Get a random kitty
fetch('kitty')
.get('/images/search?size=small&mime_types=gif')
.subscribe(
kitty => console.log(kitty),
err => console.log(err),
);
Overriding
With Flew you can always override default options and drivers in runtime, not to mention you can use the same chaining methods expecting the same result regardless the driver in use.
Custom Options
fetch('kitty', {
silent: false, // show logs
baseURL: 'https://api.thecatapi.com',
endpoint: '/v1/images/search',
})
.where('size', '==', 'small')
.where('mime_types', '==', 'gif')
.find() // same as http.get
.subscribe(
kitty => console.log(kitty),
err => console.log(err),
);
Custom Driver
fetch('kitty', {
silent: false, // show logs
})
.from('firestore') // use firestore driver from firebase
.where('size', '==', 'small')
.where('mime_types', '==', 'gif')
.find()
.subscribe(
kitty => console.log(kitty),
err => console.log(err),
);
Chaining Methods
object
Used to set whether or not return results as ParseObject. Default: false
fetch('kitty').from('parse').object(true).find().subscribe();
http
Used to configure http instance in runtime.
client server networkfetch('kitty', {
baseURL: 'https://api.thecatapi.com',
endpoint: '/v1',
})
.http(config => {
// modify axios config
config.headers['Authorization'] = 'Bearer xyz';
})
.get('/images/search?size=small&mime_types=gif')
.subscribe(
kitty => console.log(kitty),
err => console.log(err),
);
from
Used to configure driver in runtime. Default: http
fetch('kitty').from('firebase').subscribe();
fetch('kitty').from('firestore').subscribe();
fetch('kitty').from('parse').subscribe();
fetch('kitty').from('http').subscribe();
network
Used to set whether or not make an actual network call. Default: true
fetch('kitty').network(true).find().subscribe();
fetch('kitty').network(false).find().subscribe();
cache
Used to set whether or not use results from cache. Default: true
fetch('kitty').cache(true).find().subscribe();
fetch('kitty').cache(false).find().subscribe();
key
Used to define a custom key internally which can also be used to grab or set data externally. Default: auto-generated based on requested payload
fetch('kitty')
.key('my-custom-key')
.where('size', '==', 'small')
.find()
.subscribe();
getState('my-custom-key');
setState('my-custom-key');
getCache('my-custom-key');
setCache('my-custom-key');
query
Used to make complex queries based on Parse JS Client SDK.
client server parseSimple request
fetch('kitty')
.from('parse')
.query({
equalTo: () => ['size', 'small'],
})
.findOne();
and
operator
fetch('kitty')
.from('parse')
.query({
and: [
{
equalTo: () => ['size', 'small'],
},
{
greaterThanOrEqualTo: () => ['createdAt', new Date()],
},
],
})
.findOne();
or
operator
fetch('kitty')
.from('parse')
.query({
or: [
{
equalTo: () => ['size', 'small'],
},
{
equalTo: () => ['size', 'large'],
},
],
})
.findOne();
aggregate
operator
fetch('kitty')
.from('parse')
.query({
aggregate: [
{
$match: {
size: 'small',
},
},
],
})
.find();
where
Used to restraint data results by compounding queries.
client server networkfetch('kitty')
.where('size', '==', 'small')
.where('mime_types', '==', 'gif')
.find()
.subscribe(
kitty => console.log(kitty),
err => console.log(err),
);
sort
Used to sort data results by compounding queries.
client server networkfetch('kitty')
.where('size', '==', 'small')
.sort({ createdAt: 'desc' })
.find()
.subscribe();
size
Used to limit data length results by compounding queries.
client server networkfetch('kitty')
.where('size', '==', 'small')
.sort({ createdAt: 'desc' })
.size(5)
.find()
.subscribe();
at after client server network
Used to add a cursor and define the start point for a query. The at()
method includes the start point, while the after()
method excludes it.
fetch('kitty')
.where('size', '==', 'small')
.sort({ createdAt: 'desc' })
.at(20)
.find()
.subscribe();
fetch('kitty')
.where('size', '==', 'small')
.sort({ createdAt: 'desc' })
.after(20)
.find()
.subscribe();
ref
Used to define a document path for a request.
client server firebasefetch('kitty').ref('lovely/cats').on().subscribe();
include
Used to include data from a Parse relationship.
client server parsefetch('kitty')
.where('size', '==', 'small')
.include(['owner'])
.find()
.subscribe();
doc
Used to define a document id to make update/delete operations.
client server parse firestorefetch('kitty')
.doc('some-id')
.update({
size: 'small',
})
.subscribe();
token
Used to define an Authorization
token header.
fetch('kitty')
.where('size', '==', 'small')
.token('my-access-token')
.subscribe();
master
Used to define whether or not use Parse's Master Key. Default: false
fetch('kitty')
.doc('some-id')
.update({
size: 'small',
})
.master(true)
.subscribe();
select
Used to define what attribute should be returned in the response.
client server parsefetch('kitty')
.where('size', '==', 'small')
.sort({ createdAt: 'desc' })
.size(5)
.select(['name', 'size'])
.subscribe();
state
Used to set whether or not use results from memory. Default: true
fetch('kitty').state(true).find().subscribe();
fetch('kitty').state(false).find().subscribe();
near
Adds a proximity based constraint for finding objects with key point values near the point given.
client server parseimport { geopoint } from '@flew/parse';
fetch('kitty')
.where('size', '==', 'small')
.near('location', geopoint(40.0, -30.0))
.find()
.subscribe();
withinKilometers
Adds a proximity based constraint for finding objects with key point values near the point given and within the maximum distance given. Radius of earth used is 6371.0 kilometers.
client server parseimport { geopoint } from '@flew/parse';
fetch('kitty')
.where('size', '==', 'small')
.withinKilometers('location', geopoint(40.0, -30.0))
.find()
.subscribe();
withinMiles
Adds a proximity based constraint for finding objects with key point values near the point given and within the maximum distance given. Radius of earth used is 3958.8 miles.
client server parseimport { geopoint } from '@flew/parse';
fetch('kitty')
.where('size', '==', 'small')
.withinMiles('location', geopoint(40.0, -30.0))
.find()
.subscribe();
diff
Used to customize Flew's internal differentiating test to determine whether or not a result should be delivered in the subscribe stream.
client server networkfetch('kitty')
.where('size', '==', 'small')
.diff((cache, network) => {
if (cache !== network) {
return true;
}
return false;
})
.find()
.subscribe();
response
Used to hook into Flew's network data regardless differentiating test.
client server networkfetch('kitty')
.where('size', '==', 'small')
.response(network => {
// response from network is always delivered in here
console.log(network);
})
.find()
.subscribe(cacheOrStateOrNetwork => {
// response from network might not be delivered in here
// if cache or state has been already delivered
// and is the same as network result
console.log(cacheOrStateOrNetwork);
});
context
Used to set custom call attributes.
client server parsefetch('kitty')
.where('size', '==', 'small')
.context({
here: 'is',
a: 'custom',
context: 'which',
can: 'be',
captured: 'within',
parse: 'hooks',
})
.find()
.subscribe();
Chaining Verbs
Verbs are all the final methods contained in a fetch
chain.
get
Same as the find
method.
fetch('kitty').where('size', '==', 'small').get().subscribe();
delete client server network
fetch('kitty').doc('some-id').delete().subscribe();
fetch('kitty').where('size', '==', 'small').delete().subscribe();
post client server network
fetch('kitty')
.post(url, {
name: 'frajola',
})
.subscribe();
patch client server network
fetch('kitty')
.patch(url, {
name: 'Frajola',
})
.subscribe();
find
same as the get
method.
fetch('kitty').where('size', '==', 'small').find().subscribe();
findOne
same as the find
method but returns the first result only.
fetch('kitty')
.where('size', '==', 'small')
.sort({ createdAt: 'desc' })
.findOne()
.subscribe();
set client server network
fetch('kitty')
.set({
name: 'Tom',
})
.subscribe();
update client server network
fetch('kitty')
.doc('some-id')
.update({
name: 'Garfield',
})
.subscribe();
on client server firebase firestore parse
fetch('kitty').doc('some-id').on().subscribe();
count client server parse
fetch('kitty').where('size', '==', 'small').count().subscribe();
run client server parse
fetch('kitty')
.run('MyCloudFunction', { some: 'custom', attributes: true })
.subscribe();