@collectivtech/alana-sdk-js / resource / ApiContext
Class: ApiContext
Defined in: src/resource.ts:10
The API context holds various stateful data needed to communicate with the ALANA API, such as user access tokens and persistent options.
Constructors
Constructor
new ApiContext(
fetcher,options?):ApiContext
Defined in: src/resource.ts:45
Create a new API context.
Parameters
fetcher
$Fetch
The customized ofetch instance to use
options?
SdkOptions = {}
Default options to set for all API calls using this context
Returns
ApiContext
Properties
accessToken?
optionalaccessToken?:string
Defined in: src/resource.ts:25
The user's access token, once the user has logged in. Access tokens are used for protected routes that may only be accessed by authorized users.
fetcher
fetcher:
$Fetch
Defined in: src/resource.ts:14
The fetcher function used for this context: a custom ofetch instance.
idToken?
optionalidToken?:string
Defined in: src/resource.ts:31
The user's ID token, once the user has logged in. The ID token is only used as part of the authentication flow.
options
options:
SdkOptions
Defined in: src/resource.ts:19
Default SDK options that are used with all calls made through this context.
refreshToken?
optionalrefreshToken?:string
Defined in: src/resource.ts:37
The refresh token, once the user has logged in. This may be used to refresh an expired access or ID token without requiring the user to log in again.
Methods
fetch()
fetch(
route,options?):Promise<any>
Defined in: src/resource.ts:57
Call the given ALANA API endpoint and return the raw result as JSON.
Parameters
route
string
A route string, which should be the API endpoint relative to the base URL
options?
FetchOptions<ResponseType, any>
Additional options to pass to the fetcher
Returns
Promise<any>
A raw API response as a JSON object
fetchOptionalProtected()
fetchOptionalProtected(
route,options?):Promise<any>
Defined in: src/resource.ts:103
Make an API call to the given route, optionally including the user's access token. In other words, if the user has logged in, this will be treated as a logged-in call; if not, it is as a guest user.
Parameters
route
string
A route string, which should be the API endpoint relative to the base URL
options?
FetchOptions<ResponseType, any>
Additional options to pass to the fetcher
Returns
Promise<any>
A raw API response as a JSON object
fetchProtected()
fetchProtected(
route,options?):Promise<any>
Defined in: src/resource.ts:69
Call the given ALANA API endpoint using the user's access token for authorization. This returns the raw response as JSON.
Parameters
route
string
A route string, which should be the API endpoint relative to the base URL
options?
FetchOptions<ResponseType, any>
Additional options to pass to the fetcher
Returns
Promise<any>
A raw API response as a JSON object
refreshAuthTokens()
refreshAuthTokens():
Promise<void>
Defined in: src/resource.ts:114
Refresh the current user's authentication tokens. In most cases, this method is called automatically when the access token is expired, but you may also call it manually if needed.
Returns
Promise<void>