@shiker/rest-api-client
v1.0.2
Published
Halo REST API client for JavaScript
Downloads
94
Maintainers
Readme
Installation
npm install @shiker/rest-api-client
use require
or import
to import the library.
// CommonJS
const { HaloRestAPIClient } = require('@shiker/rest-api-client')
// ES modules
import { HaloRestAPIClient } from '@shiker/rest-api-client'
Usage
const client = new HaloRestAPIClient({
baseUrl: 'https://example.halo.run',
})
The auth
parameter supports the following ways:
- Use API token authentication
auth: {
apiToken: process.env.HALO_API_TOKEN
}
- Use Admin token authentication
auth: {
adminToken: process.env.HALO_ADMIN_TOKEN
}
- Use custom header auth
auth: {
type: "customizeAuth",
authHeader
:
"Admin-Authorization",
getToken()
{
return localStorage.getItem("Access_Token")
}
}
- Use OAuth token authentication
auth: {
oAuthToken: process.env.HALO_OAUTH_TOKEN
}
Basic Auth
const client = new HaloRestAPIClient({
baseUrl: 'https://example.halo.run',
// Use basic authentication
basicAuth: { username: 'user', password: 'password' },
})
In addition to this, it also supports automatic authentication of Token Provider:
import {
HaloRestAPIClient,
LocalStorageTokenStore,
// FileTokenStore,
// TokenStore,
DefaultTokenProvider,
} from '@shiker/rest-clint-api'
// Use LocalStorageTokenStore to persistence AccessToken to localStorage(in browser only)
// you can use FileTokenStore if in the Node environment.
// If there is no suitable Token store implemention, you can implement your own token storage strategy through the TokenStore interface.
const localStorageTokenStore = new LocalStorageTokenStore()
// halo api base url.
const baseUrl = process.env.VUE_APP_BASE_URL
const haloRestApiClient = new HaloRestAPIClient({
baseUrl: baseUrl,
})
const buildTokenProvider = (credentials) => {
return new DefaultTokenProvider(
{
...credentials,
},
baseUrl,
localStorageTokenStore,
)
}
const tokenProvider = buildTokenProvider({
username: 'your halo username',
password: 'your password',
})
haloRestApiClient.setTokenProvider(tokenProvider)
// now you can use haloRestApiClient to build your api client
Http Request
const haloRestApiClient = new HaloRestAPIClient({
baseUrl: 'https://example.halo.run',
basicAuth: { username: 'user', password: 'password' },
})
// build http client to perform http request
const client = haloRestApiClient.buildHttpClient()
// api parameters
const parameters = {}
// http get
client.get('https://example.halo.run', parameters)
// http post
client.post('https://example.halo.run', parameters)