Comments (5)
Hi,
have you tried using the setOptions method in order to update your cacheExpiration
value from your header response ?
from apipeline.
Hi Exilz,
Thanks for your reply.
Yes, i tried setOptions method and i updated cacheExpiration, But this is set globally across all my API request. I have many API endpoints every endpoints having different expiry time.
Please see below my code. After calling the fetch method i can get this particular endpoint expiry time from Response "expirytime": 60000,
Now i want to set this time to my cache expiry time for this particular offername - offerone. (I have many offers name like offerone, offertwo, offerthree, ..... etc and each has different expiry time)
//Sample Code
import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View } from 'react-native';
import OfflineFirstAPI from 'react-native-offline-api';
// Default API configuration
const API_OPTIONS = {
domains: { default: 'http://myapidomain.example.com'},
prefixes: { default: '' },
debugAPI: false,
printNetworkRequests: true
};
// Default Service configuration
const API_SERVICES = {
offers: {
path: 'offers/:offername'
}
};
const api = new OfflineFirstAPI(API_OPTIONS, API_SERVICES);
export default class Offers extends Component {
constructor(props) {
super(props);
}
async componentDidMount() {
const response = await api.fetch(
'offers',
{
pathParameters: {offername:'offerone'},
}
);
// Below is my API Response, Here i can get this endpoint expirytime in milliseconds.
// I want to set this expiry time to this particular API request
// API Response JSON data
/*
{
"success": true,
"status": 200,
"expirytime": 60000
"data": {
"id": 1,
"name": "First Page",
"description": "My Page description",
}
}
*/
}
render() {
return (
<View>
<Text>First Offers Page</Text>
</View>
);
}
}
Thanks.
from apipeline.
Allright I get it now.
Can you declare a service for each one of your offers ? Or do you need to call them dynamically in your application ? Do you have like 3 different offername
s that you know in advance ?
If you do, you could create a service for each one of them, and then use setServices
to update the expiration
value according to your header. I guess this is kinda hacky though...
If you don't, sadly I don't think there is a feature that lets you do that for now.
I'd be interested in hearing your ideas to resolve this particular problem by adding a feature in the API. This is probably not something a lot of people need to do, but if it's useful to you, someone else might need it in the future.
from apipeline.
Hi,
Sorry for the delay.
We are modified your code, We are taking expiration time from response.
In our API response header contain below Cache-Control header and its expiration time in max-age.
So we are setting this time to this particular endpoint. This is working fine.
Cache-Control: max-age=47394, must-revalidate, private
Basically the HTTP response header must contain the cache expiration time or data last modified time. Based on this time we set the cache expiration time on client side.
Please read this below API Cache Blog and get more idea about this.
http://www.apiacademy.co/how-to-http-caching-for-restful-hypermedia-apis/
Thanks,
from apipeline.
I'm not sure I understood what you meant by "We are modified your code"
Did you fork this plugin ? Or did my previous insight help you implement what you need ? Just so I know if I can close this issue.
from apipeline.
Related Issues (17)
- How to implement Promise.all() in offlineapi HOT 2
- Fetch both headers at data in the same request HOT 4
- How to post raw data as JSON HOT 2
- Do you support HTTP/2 PUSH ?
- add retry feature HOT 3
- Graphql support HOT 1
- Caching HOT 3
- app is crash when open HOT 2
- Add PATCH HTTP method HOT 3
- how to apply global headers? HOT 2
- Impossible to add data in body in middleware.
- How to know if cache is returned
- json() undefined when rawData is true HOT 1
- updating cache on referch with disableCache: true
- Fetching any kind of data, not just JSON HOT 2
- Process responses through middlewares HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apipeline.