CKN Technology Core · API Extensions

CKN Technology Core
API Extensions

Api is a lightweight HTTP helper class designed to simplify common REST operations such as GET, POST, PUT, DELETE, PATCH with automatic header handling and response parsing. It is part of the CKN Framework and integrates with the internal Log system.

Import

A lightweight HTTP helper class designed to simplify common REST operations (GET, POST, PUT, DELETE, PATCH) with automatic header handling and response parsing. It integrates with the internal Log system.

import { Api } from "@ckn-technology/core";

Loading Log Extensions

To load and enable the custom Log extensions used by Api, import the core package once at the entry point of your application (or any module that runs during startup). This action registers the utility functions directly onto the Log class, making them globally accessible on all Log instances within your project.

import "@ckn-technology/core";

Note: All examples in this document assume these extensions have already been loaded globally (e.g. by importing this module once at application startup).

Class: Api – Constructor

constructor()

Description
Creates a new instance of the API handler and initializes the internal logger used to trace requests and responses. This prepares the instance to perform HTTP operations with unified configuration and error handling.

Signature

constructor()

Example

import { Api } from "@ckn-technology/core";

const api = new Api();

Methods

1. get(url, headers?)

Description
Performs an HTTP GET request. The method:

  • Builds final headers (including JSON, auth or other defaults when configured).
  • Calls fetch (or compatible HTTP client) with GET method.
  • Automatically parses the response as JSON when the Content-Type is JSON, otherwise returns plain text.
  • Logs both request and response details using the internal Log class.

Signature

get(url: string, headers?: Record<string, string>): Promise<any>

Parameters

  • url (string): Target endpoint URL.
  • headers (object, optional): Custom request headers to merge with defaults.

Returns

Parsed response data (JSON object or text), wrapped in a Promise.

Example

const api = new Api();

const data = await api.get("https://api.example.com/user");
console.log(data);

2. post(url, data, headers?)

Description
Performs an HTTP POST request and automatically serializes JSON or form data depending on the provided body and headers.

  • If data is an object and Content-Type is JSON, it is serialized using JSON.stringify.
  • Handles other encodings when appropriate (e.g. URL-encoded or FormData).
  • Uses POST as HTTP method and merges custom headers with defaults.
  • Parses and returns the response similarly to get().

Signature

post(
    url: string,
    data?: any,
    headers?: Record<string, string>
): Promise<any>

Example

const api = new Api();

await api.post("/api/login", {
    username: "santi",
    password: "1234"
});

3. put(url, data, headers?)

Description
Performs an HTTP PUT request to update full resources on the server. Serialization rules are the same as for post().

Signature

put(
    url: string,
    data?: any,
    headers?: Record<string, string>
): Promise<any>

Example

const api = new Api();

await api.put("/api/user/10", {
    name: "Santi Chongkaonar"
});

4. delete(url, data?, headers?)

Description
Performs an HTTP DELETE request. Optionally supports sending a request body when the backend requires it (e.g. for confirmation flags or complex delete criteria).

Signature

delete(
    url: string,
    data?: any,
    headers?: Record<string, string>
): Promise<any>

Example

const api = new Api();

await api.delete("/api/user/10", { confirm: true });

5. patch(url, data, headers?)

Description
Performs an HTTP PATCH request for partial updates. Similar to put() but typically used when only a subset of fields should be changed.

Signature

patch(
    url: string,
    data?: any,
    headers?: Record<string, string>
): Promise<any>

Example

const api = new Api();

await api.patch("/api/user/10", {
    status: "active"
});

Usage Summary

Typical usage pattern combining all methods:

import { Api } from "@ckn-technology/core";

const api = new Api();

// GET
const info = await api.get("/api/info");

// POST
const created = await api.post("/api/create", { foo: "bar" });

// PUT
const updated = await api.put("/api/update/5", { x: 100 });

// DELETE
const removed = await api.delete("/api/remove/5");

// PATCH
const patched = await api.patch("/api/edit/7", { enabled: true });

Key Features

  • Automatic JSON/text response handling based on response Content-Type.
  • Error-safe operations with centralized logging via the internal Log class.
  • Automatic header normalization so you don’t have to repeat common headers for every request.
  • Unified API interface across get, post, put, delete, and patch.
  • Environment flexibility: can be used in browser, Node.js, and SSR environments (depending on your underlying fetch or HTTP adapter configuration).

Summary

The Api class is a versatile and lightweight HTTP client designed for seamless interaction with RESTful APIs. It provides a unified interface for common operations such as GET, POST, PUT, DELETE, PATCH, featuring automatic serialization of request bodies and parsing of responses (JSON or text).

Key benefits include error-safe operations via the Log system, automatic header normalization, and broad compatibility across browser, Node.js, and SSR environments. By centralizing your HTTP logic in Api, your application code becomes cleaner, easier to maintain, and consistent across modules and microservices.

Use Api whenever you need to talk to REST endpoints inside the CKN Framework – from simple data fetches to complex workflows with authentication and error tracking.