Schlepp
API client used for making authenticated JSON requests using a JWT bearer token to a specified endpoint. Unauthenticated requests are also supported.
This API client follows the convention of storing the bearer token in local storage. When creating an instance of the API client, pass the function the name of the key in local storage to use when retrieving the bearer token for authenticated requests.
Setup
Create an instance of Schlepp passing in your API host and local storage bearer token key
; const api = bearerTokenKeyInLocalStorage: 'auth_token' // assumes localStorage.getItem('auth_token') will return the bearer token headers: Accept: 'application/json' // default is { 'Content-Type': 'application/json' } host: 'https://example.com';
Usage
Use the authenticated
and unauthenticated
objects to send an HTTP request to
the server. Both objects expose the same HTTP method functions (get
, delete
,
post
, patch
).
The difference between the authenticated
and unauthenticated
request is that the authenticated
requests include an Authorization
header: ({ Authorization: 'Bearer my-bearer-token' }
).
Request methods
The following request methods can be performed as either authenticated or unauthenticated requests
#delete
promise // Example:apiauthenticated;apiunauthenticated;
#get
promise // Example:apiauthenticated;apiunauthenticated;
#patch
promise // Example:apiauthenticated;apiunauthenticated;
#post
promise // Example:apiauthenticated;apiunauthenticated;
Examples
Unauthenticated requests
; const api = bearerTokenKeyInLocalStorage: 'auth_token' host: 'https://example.com'; apiunauthenticated;apiunauthenticated;apiunauthenticated;apiunauthenticated);
Authenticated requests
; const api = bearerTokenKeyInLocalStorage: 'auth_token' host: 'https://example.com'; apiauthenticated;apiauthenticated;apiauthenticated;apiauthenticated;
Specifying headers
By default, request headers will be set to { 'Content-Type': 'application/json' }
. Request headers can be amended/overridden in 2 ways.
1) Instantiating the class with headers
If you'd like to set headers to be used across all requests made by the API client, you may want to set headers when creating an instance of the Schlepp class.
; const api = bearerTokenKeyInLocalStorage: 'auth_token' headers: Accept: 'application/json' host: 'https://example.com'; // in the above example all requests send from the `api` constant will include the following headers:// { Accept: 'application/json', 'Content-Type': 'application/json' } ; const api = bearerTokenKeyInLocalStorage: 'auth_token' headers: 'Content-Type': 'text/plain' host: 'https://example.com'; // in the above example all requests send from the `api` constant will include the following headers:// { 'Content-Type': 'text/plain' }
2) Setting headers on each request
To override or amend the default or instance headers, you can specify the headers of an individual request.
; const api = bearerTokenKeyInLocalStorage: 'auth_token' headers: Accept: 'text/plain' host: 'https://example.com'; apiunauthenticated; // in the above example the request will include the following headers:// { Accept: 'application/json', 'Content-Type': 'application/json' } ; const api = bearerTokenKeyInLocalStorage: 'auth_token' host: 'https://example.com'; apiunauthenticated; // in the above example the request will include the following headers:// { 'Content-Type': 'text/plain' }