Concepts

Making KOS Network Requests

Introduction

  • KOS utilizes a websocket infrastructure for handling network requests between frontend and backend components.

  • KOS provides many capabilities that enable traditional request/response interactions using a websocket transport to provide reliable delivery and sequencing.

  • KOS UI SDK provides utilities that expose a more web-like abstraction that more closely resembles the browser fetch API to further simplify interactions between the frontend and backend. (kosFetch)

  • For most request/response operations within a KOS model it is recommended to use the service factory to expose simplified interactions

  • When working in other contexts or for finer-grained control of the operation, use the kosFetch utility directly for data fetching against the backend.

KOS Model Networking

ServiceFactory

The ServiceFactory provides an abstraction layer that will create a set of model specific calls that help to reduce boiler-plate code associated with using the fetch API. The ServiceFactory utility is intended for use alongside a model where the network requests tend to be aligned with a specific model context.

Build

Building A Service Factory
// sample-service-factory.ts

// Import the ServiceFactory and corresponding support utilities from `@coca-cola/kos-ui-core`
import { resolveServiceUrl, ServiceFactory } from "@coca-cola/kos-ui-core";
import type { KosServiceResponse } from "@coca-cola/kos-ui-core";


// the returned service factory functions will handle:
// 1.  Error Handling
// 2.  JSON Parsing
// 3.  Request formatting with the appropriate underlying HTTP method
// 4.  Calling the lower level kosFetch with the correct parameters.
const { getAll, getModelById } = ServiceFactory.build({
  basePath: `${URL}/samples`
});

// Interface describing the data returned from a service request
interface SampleResponse {

    value1: string;
}


export const getSampleById = async (id: string) => {
    // returns a single Sample response object
  const response = await getModelById<SampleResponse>({ id });
  return response;
};

export const getAllSamples = async () => {

  // returns an array of SampleResponse objects
  const response = await getAll<SampleResponse>({});

  return response;
};

kosFetch

The kosFetch module provides

import { kosFetch as fetch } from "@coca-cola/kos-ui-core";

const response = await fetch('kos:/api/samples', {
    method: "GET"
});
if (!response.ok) {
    throw Error(
    `There was a problem fetching the model; returned status ${response.status}`
    );
}

const json = await response.json();

return json;
Previous
Next
On this page
Java Development
Seamlessly transition from Legacy+ systems to Freestyle microdosing and advanced distributed dispense systems.
UI Development
Using KOS SDKs, integrating Consumer and Non-consumer facing UIs becomes seamless, giving you less hassle and more time to create.
Video Library
Meet some of our development team, as they lead you through the tools, features, and tips and tricks of various KOS tools.
Resources
Familiarize yourself with KOS terminology, our reference materials, and explore additional resources that complement your KOS journey.
Copyright © 2024 TCCC. All rights reserved.