Skip to main content

API

Basic Use

import { api, action } from '@plexusjs/core'
const myCoolApi = api('https://api.example.com')

const fetchUsers = action(async ({ onCatch }) => {
onCatch(console.error)
return myCoolApi.get('/users')
})

const users = await fetchUsers() //=> [{id: 1, name: "John"}, {id: 2, name: "Jane"}] or something

Properties

api .config

The current base configuration of this api

Returns
  • .headers {Record<string, string>}: Refer to Official Documentation.
  • .body {BodyInit | null}: Refer to Official Documentation.
  • .cache {RequestCache}: Refer to Official Documentation.
  • .credentials {RequestCredentials}: Refer to Official Documentation.
  • .integrity {string}: Refer to Official Documentation.
  • .keepalive {boolean}: Refer to Official Documentation.
  • .mode {RequestMode}: Refer to Official Documentation.
  • .redirect {RequestRedirect}: Refer to Official Documentation.
  • .referrer {string}: Refer to Official Documentation.
  • .referrerPolicy {ReferrerPolicy}: Refer to Official Documentation.
  • .signal {AbortSigna | nulll}: Refer to Official Documentation.
  • .window {null}: Refer to Official Documentation.

Methods

api .get()

Send a get request

Arguments
  • Path {string}: The path to call on the api.
  • Query? {Record<string, string>}: The object to use for the url query string.
api .post()

Send a post request

Arguments
  • Path {string}: The path to call on the api.
  • Body? {Record<string, string>}: The object to use for the request body.
api .put()

Send a put request

Arguments
  • Path {string}: The path to call on the api.
  • Body? {Record<string, string>}: The object to use for the request body.
api .delete()

Send a delete request

Arguments
  • Path {string}: The path to call on the api.
api .patch()

Send a patch request

Arguments
  • Path {string}: The path to call on the api.
  • Body? {Record<string, string>}: The object to use for the request body.
api .options()

Set the configuration options for fetch

Arguments
  • Path {string}: The path to call on the api.
  • Query? {Record<string, string>}: The object to use for the url query string
api .auth()

Set the the Authorization header for the request

Stored Differently From other Headers

This is stored seprately from other headers. If there is another value for the Authorization header in options, this value will override it.

Arguments
  • AuthType {"bearer" | "basic" | "jwt"}: The type of API token being used. This determines the prefix of the Authorization header.
  • TokenValue {string}: The object to use for the url query string
api .headers()

Set headers for the requests of this API Instance

Arguments
  • HeaderObject {Record<string, string>}: An object of headers to set for the request.
api .reset()

Reset this routes configuration

Returns
  • {ApiInstance}: Returns the reset API Instance

Response

apiResponse .data

The data returned from the request in JSON format. If the data is not supposed to be JSON, use apiResponse.rawData

Returns
  • {string}: The internal id of the PreAction.
apiResponse .status

The HTTP status of the request

Returns
  • {number}: The HTTP status code of the response.
apiResponse .ok

Is the status code between 200 to 299?

Returns
  • {boolean}: True if the response status was between 200 and 299. False if otherwise.
apiResponse .rawData

The raw string of the response data

Returns
  • {string}: The internal id of the PreAction.
apiResponse .response

The HTTP response object (ResponseInit)

Returns
  • {ResponseInit}: Refer to Official Documentation.