1.1.0 • Published 2 years ago

@halo-dev/rest-api-client v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Installation

npm install @halo-dev/rest-api-client

use require or import to import the library.

// CommonJS
const { HaloRestAPIClient } = require('@halo-dev/rest-api-client')

// ES modules
import { HaloRestAPIClient } from '@halo-dev/rest-api-client'

Usage

const client = new HaloRestAPIClient({
  baseUrl: 'https://example.halo.run',
})

The auth parameter supports the following ways:

  1. Use API token authentication
auth: {
  apiToken: process.env.HALO_API_TOKEN
}
  1. Use Admin token authentication
auth: {
  adminToken: process.env.HALO_ADMIN_TOKEN
}
  1. Use custom header auth
auth: {
  type: "customizeAuth",
    authHeader
:
  "Admin-Authorization",
    getToken()
  {
    return localStorage.getItem("Access_Token")
  }
}
  1. 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 '@halo-dev/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)

License

MIT license

1.1.0

2 years ago

1.0.0

2 years ago

1.0.0-alpha.50

2 years ago

1.0.0-alpha.49

2 years ago

1.0.0-alpha.48

2 years ago

1.0.0-alpha.45

2 years ago

1.0.0-alpha.47

2 years ago

1.0.0-alpha.46

2 years ago

1.0.0-alpha.19

2 years ago

1.0.0-alpha.30

2 years ago

1.0.0-alpha.32

2 years ago

1.0.0-alpha.31

2 years ago

1.0.0-alpha.38

2 years ago

1.0.0-alpha.16

2 years ago

1.0.0-alpha.37

2 years ago

1.0.0-alpha.15

2 years ago

1.0.0-alpha.18

2 years ago

1.0.0-alpha.39

2 years ago

1.0.0-alpha.17

2 years ago

1.0.0-alpha.34

2 years ago

1.0.0-alpha.33

2 years ago

1.0.0-alpha.36

2 years ago

1.0.0-alpha.14

2 years ago

1.0.0-alpha.35

2 years ago

1.0.0-alpha.13

2 years ago

1.0.0-alpha.41

2 years ago

1.0.0-alpha.43

2 years ago

1.0.0-alpha.21

2 years ago

1.0.0-alpha.42

2 years ago

1.0.0-alpha.20

2 years ago

1.0.0-alpha.27

2 years ago

1.0.0-alpha.26

2 years ago

1.0.0-alpha.29

2 years ago

1.0.0-alpha.28

2 years ago

1.0.0-alpha.23

2 years ago

1.0.0-alpha.44

2 years ago

1.0.0-alpha.22

2 years ago

1.0.0-alpha.25

2 years ago

1.0.0-alpha.24

2 years ago

1.0.0-alpha.12

3 years ago

1.0.0-alpha.11

3 years ago

1.0.0-alpha.10

3 years ago

1.0.0-alpha.9

3 years ago

1.0.0-alpha.6

3 years ago

1.0.0-alpha.5

3 years ago

1.0.0-alpha.4

3 years ago

1.0.0-alpha.3

3 years ago

1.0.0-alpha.2

3 years ago

1.0.0-alpha.1

3 years ago