Closed DarkHelmet67 closed 6 years ago
The api
layer is specific to Contactlab's requirements so the token
is mandatory in order to interact with our APIs.
The request
layer already gives you enough "power" to develop a custom solution when you need to decode/validate a response without specifing a token in the request (e.g. in order to fetch a json file).
import {ApiTask, ApiError, DecoderError} from '@contactlab/appy/lib/api';
import {AppyResponse, Mixed, get} from '@contactlab/appy/lib/request';
import * as t from 'io-ts';
import {fromEither} from 'fp-ts/lib/TaskEither';
// --- Define decoder
const fooBar = t.interface({
foo: t.string;
})
type FooBar = t.TypeOf<typeof fooBar>;
// --- Fn in order to handle decoding
const applyDecoder = <A>(
aresponse: AppyResponse<Mixed>,
decoder: Decoder<Mixed, A>
): Either<ApiError, AppyResponse<A>> =>
decoder
.decode(aresponse.body)
.bimap(err => new DecoderError(err), body => ({...aresponse, body}));
// --- Get the json from url with decoder, but without token
const getJson = <A>(url: string, decoder: Decoder<Mixed, A>): ApiTask<FooBar> =>
get(url, {} /*any request configuration*/)
.mapLeft<ApiError>(x => x)
.chain(resp => fromEither(applyDecoder(resp, decoder)))
// --- Make the Task
getJson('https://my.json.com', fooBar);
Actually calling a public web service without a token appends "Authorization: Bearer" to request headers, which can cause issues. Can we make it optional like ID and VERSION?