Client from the contract
Build types from the contract of the API, send the Cabinet Boundary, walk a Window with the cursor, distinguish errors by their code and choose the language of labels.
The API publishes its contract in OpenAPI 3.1 at /api/v1/openapi.json. Build your client from it: the types then match the API that answers your calls. The examples use openapi-typescript and openapi-fetch.
Build types from /api/v1/openapi.json
Take the contract from the API itself, not from this site: the site is for reading and has no copy of the contract. Keep the contract in your repository and compare it with the API in CI.
curl -s https://rm-api.ovkch.com/api/v1/openapi.json -H "Authorization: Bearer $TOKEN" > openapi.json
npx openapi-typescript openapi.json -o api.d.tsimport createClient from 'openapi-fetch'
import type { paths } from './api.d.ts'
export const api = createClient<paths>({
baseUrl: 'https://rm-api.ovkch.com/api/v1',
headers: { Authorization: `Bearer ${token}` },
})Send the Cabinet Boundary in params.header
The contract declares x-cabinets as a required header, so openapi-fetch requires it in params.header of every call. Build it from the Cabinets of the person behind the call. null below stands for the whole catalog, *.
export const within = (cabinets: readonly string[] | null) =>
({ header: { 'x-cabinets': cabinets === null ? '*' : cabinets.join(',') } }) as constDo not call the API with an empty list: when the person has no Cabinets, report the missing access yourself. The Cabinet Boundary rule gives the limits.
Walk a Window with the cursor
Pass the nextCursor of each Page in cursor until a response comes without it:
let cursor: string | undefined
do {
const { data, error } = await api.GET('/releases', {
params: { ...within(cabinets), query: { cabinet: ['label-one'], orderBy: 'key', cursor } },
})
if (error) throw error
for (const item of data.items) handle(item)
cursor = data.nextCursor
} while (cursor)To read a whole Window, order it by key: the order by attention or status changes over time, so Pages can skip or repeat rows. The Page rule describes the cursor.
Distinguish errors by code
An error is application/problem+json. Choose what to do by code, not by the text of detail: detail is for the developer. Treat a code that you do not know as internal.
import type { components } from './api.d.ts'
type Problem = components['schemas']['Problem']
const { data, error } = await api.GET('/release', { params: { ...within(cabinets), query: { key } } })
if (error) {
const problem = error as Problem
if (problem.code === 'not_found') return undefined
throw problem
}Each code has its own section on the rules page, and the retry rule says which errors you may retry.
Choose the language of labels with Accept-Language
Labels for people, such as the label of a status, come in the language of the Accept-Language header: en or ru. Without the header, they are in English.
const { data } = await api.GET('/release', {
params: { header: { 'x-cabinets': 'label-one', 'Accept-Language': 'ru' }, query: { key } },
})Make decisions by codes, not by labels: next to each label is a fixed code. The language rule lists the labels and explains how to cache them.