Skip to content

Getting Started

Installation

bash
yarn add @apihive/core
bash
npm install @apihive/core
bash
jsr add @apihive/apihive-core

Basic Usage

typescript
import { HTTPRequestFactory } from '@apihive/core';

const factory = new HTTPRequestFactory(); //reusable factory instance

const response = await factory
  .createGETRequest('https://api.github.com/users/octocat')
  .withHeader('Accept', 'application/vnd.github.v3+json')
  .execute();

console.log(response.login); // 'octocat'

API Configuration

typescript
import { HTTPRequestFactory } from '@apihive/core';

export default new HTTPRequestFactory()
  .withAPIConfig({
    name: 'api',
    baseURL: 'https://jsonplaceholder.typicode.com',
    headers: {
      'Authentication': (config) => config.url.includes('/admin/') && mySessionObject.isAuthenticated() ? `Bearer ${mySessionObject.getAccessToken()}` : undefined
    },
    endpoints: {
      'get-posts': {
        target: '/posts'
      },
      'post-edit': {
        target: '/admin/posts/:id',
        method: 'PUT'
      }
    }
  }, /* other APIS */);
typescript
import requestFactory from './request-factory.ts';

//Execute a GET request from the API
const posts = await requestFactory.createAPIRequest('get-posts')
  .execute();

//Execute a PUT request from the API that automatically adds authentication headers
const post = await requestFactory.createAPIRequest('post-edit')
  .withJSONBody({
    title: 'foo',
    body: 'bar'
  })
  .execute();

Next Steps