
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.
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.
// 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;
};
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;