-
Notifications
You must be signed in to change notification settings - Fork 2
signed fetch
Mobile Framework provides some wrapper around the standard Fetch API to use authentication simply.
These tools are located in /app/infra/fetchWithCache.ts
and allow to use oAuth signed request and cache storage.
Sign a fetch request work only if authentication is active, when the user has logged in successfully. It will raise an exception if you try to perform a signer fetch without an active oAuth token. (See "Authentication" section of this documentation.)
You can simply use signed fetch with this simple API :
import { signedFetch } from "../../infra/fetchWithCache";
myResponse = await signedFetch(`${Conf.currentPlatform.url}/conversation/toggleUnread`, init);
Url must be absolute. Consider include the platform url at the beginning of your url.
The second parameter is the init objet, same as if you were using the Fetch API.
As same as fetch
, signedFetch
returns a promise which resolves with the received data.
You can also use signedFetchJson
, that work same as signedFetch
, except the received data is parsed as a JSON string, and then the promise resolves with the resulting JS data.
Sometimes, data is received not by using Fetch API, but with data source (images, videos, audio, etc...) You may need to sign data sources.
The module /app/infra/oauth.ts
provides a signUrl
function that allow you get a signed source object from an string url.
The source is signed only if it is an absolute path starting with an "/" or a relative path. If a protocol is present ("something://"), the source object returned is not signed.
If you don't have an url to sign, but you need to get a authentication header, you can use getAuthHeader()
. This function just returns a signed source object with no url.
You can also use signImagesUrls(images: Array<{ src: string; alt: string }>)
to convert an image array to a signed one.
In addition, you can save a cache result of any signed fetch request by using fetchWithCache()
and fetchJSONWithCache()
API.
async function fetchWithCache(
path: string,
init: any = {},
forceSync: boolean = true,
platform: string = Conf.currentPlatform.url,
getBody = r => r.text(),
getCacheResult = cr => new Response(...cr)
) => Promise<Response>
async function fetchJSONWithCache(
path: string,
init: any = {},
forceSync: boolean = true,
platform: string = Conf.currentPlatform.url
) => Promise<Any>
You can use these two function the same way that you use signedFetch
and signedFetchJson
, except :
- Don't put the protocol and the domain name in the url. This will be inserted with the
platform
parameter. The path must be a absolute url stating with "/". - A copy of the response is stored in the AsyncStorage of the device, which will be used if the path is requested again without an internet connection.
-
getBody
andgetCacheResult
can be used to handle other format than raw data or JSON. They respectively must return the resulting JS data that came from the server's response and from the AsyncStorage.
You can clear all cached requests by invoking the async clearRequestsCache()
function.