Contensis Core API Library

1.2.2 · active · verified Tue Apr 21

The `contensis-core-api` package, currently at version 1.2.2, serves as a foundational dependency library within the Contensis ecosystem. Written in TypeScript, it provides common types, interfaces, and base methods shared across higher-level Contensis client libraries, specifically `contensis-delivery-api` and `contensis-management-api`. Its primary purpose is to offer standardized HTTP request handling, error structures, and data models that ensure consistency and reduce code duplication across various Contensis API interactions. While not typically consumed directly by most application developers, it provides the underlying infrastructure for interacting with the Contensis HTTP Delivery API and Management API. As a core dependency, its release cadence is usually tied to the evolution of the broader Contensis API clients, adhering to semantic versioning for internal changes.

Common errors

Warnings

Install

Imports

Quickstart

Illustrates the usage of core types like `HttpClient`, `HttpMethod`, and `CallError`, demonstrating a low-level HTTP request pattern. This package is typically a dependency and not used directly for API calls by end-user applications.

import { HttpClient, HttpMethod, CallError, IHttpClient } from 'contensis-core-api';

interface MyApiOptions {
  rootUrl: string;
  accessToken: string;
}

// Demonstrating how to set up a basic HttpClient (typically abstracted by higher-level Contensis clients)
class CustomContensisHttpClient implements IHttpClient {
  private client: HttpClient;

  constructor(options: MyApiOptions) {
    this.client = new HttpClient(options.rootUrl);
    // In a real scenario, authentication headers would be added here or via middleware
    this.client.setDefaultHeader('Authorization', `Bearer ${options.accessToken}`);
  }

  async request<T>(method: HttpMethod, path: string, options?: RequestInit): Promise<T> {
    try {
      const response = await this.client.request<T>(method, path, options);
      console.log(`Successfully made ${method} request to ${path}`);
      return response;
    } catch (error) {
      if (error instanceof CallError) {
        console.error(`API Call Error: ${error.message} (Status: ${error.status})`);
      } else {
        console.error('An unexpected error occurred:', error);
      }
      throw error;
    }
  }
}

// Example usage (conceptual, as direct use is rare)
const myClient = new CustomContensisHttpClient({
  rootUrl: process.env.CONTENSIS_ROOT_URL ?? 'https://api.contensis.com',
  accessToken: process.env.CONTENSIS_ACCESS_TOKEN ?? 'YOUR_ACCESS_TOKEN_HERE'
});

async function fetchData() {
  try {
    // This is a simplified example; actual paths and types would vary.
    // Higher-level Contensis SDKs (`contensis-delivery-api`) provide structured methods.
    const data = await myClient.request<any>(HttpMethod.GET, '/some/api/path');
    console.log('Received data:', data);
  } catch (e) {
    console.log('Failed to fetch data via core client.');
  }
}

fetchData();

view raw JSON →