Portive

Portive Client

Client

The Client object is used to make API calls to the Portive API.

Usually, you create the Client object using the createClient method then pass it to other methods. for example, the uploadFile method take a Client as one of its arguments.

Instantiation Methods

createClient

Create an instance of the Portive Client.

function createClient({
  apiKey?: string | () => string | () => Promise<string>
  authToken?: string | () => string | () => Promise<string>
  apiOrigin?: string
}): Client

You must provide an apiKey or an authToken which can be either a string, a function that returns a string or an async function that returns a Promise<string>.

You can also optionally provide a string that represents an apiOrigin which is an http origin where the API call should be made to like http://localhost:3001. By default this is set the origin for the Portive API at https://api.portive.com. Normally you would only change the apiOrigin for testing purposes only.

// sample instantiation
import { createClient } from "@portive/client"

const client = createClient({ apiKey: "API_KEY" })

Instance Methods

client.post

client.post(path: string, data: JsonObject): Promise<JsonObject>

Posts against an API endpoint. Usually, we don't call this directly and instead use specific methods in this client library like upload; however, underneath the hood, all API calls are made using this post method.

Note that the return value is Promise<JsonObject> but the API always returns values in a subset of JSON called JSend Format. It looks like:

{status: "success", data: {value: "success values returned in 'data' object"}}
{status: "fail", data: {value: "fail values returned in 'data' object"}}
{status: "error", message: "There was an error"}

client.getApiKey

Returns the apiKey as a string. Resolves it if necessary.

client.getApiKey(): Promise<string | undefined>

Takes the apiKey that the Client was created with and then returns it as a string. For example, if the apiKey was passed in as a function, it would execute the function and return the value of the function.

Note that this returns as a Promise so use with await

const client = new Client({ apiKey: async () => "API_KEY" })
const resolvedApiKey = await client.getApiKey()
// => "API_KEY"

client.getAuthToken

Returns the authToken as a string. Resolves it if necessary.

client.getAuthToken(): Promise<string | undefined>

Takes the authToken that the Client was created with and then resolves it to a string. For example, if the authToken was passed in as a function, it would execute the function and return the value of the function.

Note that this returns as a Promise so use with await

const client = new Client({ authToken: async () => "AUTH_TOKEN" })
const resolvedAuthToken = await client.getAuthToken()
// => "AUTH_TOKEN"