npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@space-ai/contracts

v0.1.9

Published

Типы и функции HTTP-запросов к API (axios устанавливается вместе с пакетом)

Readme

@space-ai/contracts

npm-пакет с TypeScript-типами и функциями HTTP-запросов к API.

Axios устанавливается автоматически как зависимость пакета. Базовый URL API задаётся через BASE_API_URL на глобальном объекте: window (браузер) или global / globalThis (Node.js, тесты).

Содержание


Установка

npm install @space-ai/contracts

Отдельно ставить axios не нужно.


Быстрый старт

1. Задайте базовый URL API:

// браузер
window.BASE_API_URL = 'https://api.example.com';

// Node.js / тесты
global.BASE_API_URL = 'http://localhost:3000';

2. Подключите автоинициализацию axios (один раз в точке входа):

import '@space-ai/contracts/axios';

3. Используйте функции запросов где угодно:

import { getConnectors } from '@space-ai/contracts';

const { data: connectors } = await getConnectors();

Axios создаётся с baseURL: BASE_API_URL, сохраняется в window.axios / global.axios и используется всеми API-функциями.


BASE_API_URL (window / global)

Пакет читает и пишет одни и те же поля на globalThis — в браузере это window, в Node.js — global.

| Свойство | Тип | Описание | |----------|-----|----------| | BASE_API_URL | string | Базовый URL API → axios.create({ baseURL }) | | API_PREFIX | string | Опциональный префикс (api{BASE_API_URL}/api) | | axios | AxiosInstance | Экземпляр axios (создаётся пакетом) | | CONTRACTS_AUTH | AuthProvider | Провайдер токена (альтернатива configureAuth) |

Примеры:

// браузер
window.BASE_API_URL = '/api';

// Node.js, Vitest, Jest
global.BASE_API_URL = 'http://localhost:3000';

Инициализация axios и авторизация

Пакет создаёт axios как на фронтенде:

  • baseURL из BASE_API_URL + опциональный API_PREFIX
  • заголовки Content-Type / Accept: application/json
  • request interceptor — подставляет Authorization: Bearer <token>
  • response interceptor — проверка статуса и проброс ошибок

Провайдер токена

Keycloak, Zustand и прочее остаются в приложении. Пакет не знает про них — вы передаёте AuthProvider:

import { configureAuth, setupApiClient } from '@space-ai/contracts';
import { getAccessToken, updateToken } from '@/api/keycloak';
import { dispatchLogout, useAuth } from '@/stores/auth';

configureAuth({
  getAccessToken: () => getAccessToken() ?? useAuth.getState().token ?? null,
  updateToken: (minValiditySeconds = 30) => updateToken(minValiditySeconds),
  onSessionExpired: () => dispatchLogout(),
});

window.BASE_API_URL = import.meta.env.VITE_API_URL;
window.API_PREFIX = 'api'; // опционально

setupApiClient();

Логика interceptor (как в src/api/axios.ts на фронте):

  1. getAccessToken() — если токена нет, запрос уходит без Authorization
  2. updateToken(30) — обновление перед запросом; при ошибке → onSessionExpired() и Session expired
  3. повторный getAccessToken() — свежий токен в заголовок

Альтернатива — global.CONTRACTS_AUTH

global.CONTRACTS_AUTH = {
  getAccessToken: () => getAccessToken() ?? useAuth.getState().token ?? null,
  updateToken,
  onSessionExpired: dispatchLogout,
};

Полный пример main.tsx

import { configureAuth, setupApiClient } from '@space-ai/contracts';
import { getAccessToken, updateToken } from '@/api/keycloak';
import { dispatchLogout, useAuth } from '@/stores/auth';

configureAuth({
  getAccessToken: () => getAccessToken() ?? useAuth.getState().token ?? null,
  updateToken: (s = 30) => updateToken(s),
  onSessionExpired: dispatchLogout,
});

window.BASE_API_URL = import.meta.env.VITE_API_URL;
window.API_PREFIX = 'api';

setupApiClient();

import { createRoot } from 'react-dom/client';
import App from './App';

createRoot(document.getElementById('root')!).render(<App />);

Способы инициализации

Способ 1 — Vite / React (рекомендуется)

Импорты в ES-модулях поднимаются (hoisting) и выполняются раньше остального кода файла. Поэтому window.BASE_API_URL нужно задать до загрузки приложения.

index.html — задайте URL до бандла:

<!DOCTYPE html>
<html lang="ru">
  <head>...</head>
  <body>
    <div id="root"></div>
    <script>
      window.BASE_API_URL = '/api';
    </script>
    <script type="module" src="/src/main.tsx"></script>
  </body>
</html>

main.tsx:

import '@space-ai/contracts/axios';
import { createRoot } from 'react-dom/client';
import App from './App';

createRoot(document.getElementById('root')!).render(<App />);

Vite: URL из .env

В .env:

VITE_BASE_API_URL=https://api.example.com

В index.html (Vite подставляет %VITE_*% при сборке):

<script>
  window.BASE_API_URL = '%VITE_BASE_API_URL%';
</script>

Или через vite.config.ts — плагин transformIndexHtml, если нужна подстановка иначе.


Способ 2 — явный вызов setupApiClient()

Если BASE_API_URL задаётся в коде (например, из import.meta.env), вызовите setup после присвоения:

// main.tsx
window.BASE_API_URL = import.meta.env.VITE_BASE_API_URL;

import { setupApiClient } from '@space-ai/contracts';

setupApiClient();

Side-effect импорт @space-ai/contracts/axios здесь не обязателен — достаточно setupApiClient().


Способ 3 — side-effect импорт + отложенная инициализация

Если используете только:

window.BASE_API_URL = import.meta.env.VITE_BASE_API_URL;
import '@space-ai/contracts/axios';

из-за hoisting импорт выполнится раньше присвоения BASE_API_URL. Пакет это учитывает: при импорте @space-ai/contracts/axios включается auto-init, а axios создаётся при первом HTTP-запросе, когда window.BASE_API_URL уже задан.

// main.tsx — порядок строк в файле не важен для auto-init
import '@space-ai/contracts/axios';

window.BASE_API_URL = import.meta.env.VITE_BASE_API_URL;

// axios создастся при первом вызове getConnectors(), getGuardrailsList() и т.д.

Для interceptors и baseURL до первого запроса надёжнее Способ 1 или Способ 2.


Способ 4 — полностью ручная настройка

Свой экземпляр axios с interceptors, таймаутами и т.д.:

import axios from 'axios';
import { initApiClient } from '@space-ai/contracts';

const api = axios.create({
  baseURL: window.BASE_API_URL ?? '/api',
  timeout: 10_000,
});

api.interceptors.request.use((config) => {
  // auth token, headers...
  return config;
});

initApiClient(api);

Использование API-функций

import {
  getConnectors,
  postConnector,
  getGuardrailsList,
} from '@space-ai/contracts';

const { data: connectors } = await getConnectors();

const { data: guardrails } = await getGuardrailsList();

Доступные API-функции:

  • Connectors: getConnectors(), postConnector(payload), getConnector(id), patchConnector(id, body), deleteConnector(id), getConnectorHealth(id), postConnectorExecute(payload), runConnectorExecuteTest(payload)
  • Guardrails: getGuardrailsList(), getGuardrail(id), postGuardrail(payload), putGuardrail(id, payload), deleteGuardrail(id), postGuardrailRegister(payload), postApplyGuardrail(payload), postRiskScoreGuardrail(payload), getGuardrailSubmissions(query?), getGuardrailSubmission(id), postGuardrailSubmissionApprove(id), postGuardrailSubmissionReject(id), getGuardrailMajorAirlines(), getGuardrailProviderSpecificParams()
  • Policies: postPolicy(payload), getPoliciesList(), getPolicy(id), putPolicy(id, payload), deletePolicy(id), deletePolicyAllVersions(name), getPolicyVersionsByName(name), postPolicyVersion(name, payload), getPolicyResolvedGuardrails(id)
  • Policy validate/list: getPolicyList(), getPolicyName(id), getPolicyTemplates(), postPolicyValidate(payload)
  • Access: getAccessCheck()
  • Audit: getAuditEvents(), getAuditViolations()
  • LLM Providers: getLLMProviders(), postLLMProvider(payload), getLLMProvider(id), putLLMProvider(id, payload), deleteLLMProvider(id)
  • Users: putUserRoles(userId, data)
  • Roles: getRoles(), postRole(payload), patchRole(id, body), roleHasPermission(role, key, perm), mergeRolePermission(role, key, perm)
  • Organizations: getOrganizations(), getOrganization(id), patchOrganization(data)
  • MCP: postMcpProxy(body), getMcpServers(), postMcpServer(payload), getMcpServer(id), deleteMcpServer(id)
  • Applications: getApplications(), postApplication(app), getApplication(id), patchApplication(id, app), deleteApplication(id)

Примеры:

import { getConnectors, putUserRoles, type IAssignUserRolesRequest } from '@space-ai/contracts';

const { data: connectors } = await getConnectors();

const body: IAssignUserRolesRequest = { roles: ['admin', 'user'] };
await putUserRoles('[email protected]', body);

Прямой доступ к axios (после инициализации):

const client = globalThis.axios; // window.axios в браузере
await client.get('/health');

TypeScript

Пакет расширяет глобальную область (global / window):

declare global {
  var BASE_API_URL: string | undefined;
  var axios: import('axios').AxiosInstance | undefined;
}

Дополнительные декларации в проекте не нужны.


Локальная разработка пакета

git clone <repo>
cd contracts
npm install
npm run build

Структура:

src/
  api/          — функции запросов к эндпоинтам
  types/        — типы запросов/ответов
  core/         — axios, init, setup
  axios.ts      — entry point @space-ai/contracts/axios
  index.ts      — публичное API @space-ai/contracts
dist/           — сборка для npm (не коммитится)

Публикация в npm

  1. Организация @space-ai на npmjs.com.
  2. Granular Access Token (Read and write, Bypass 2FA) → GitHub Secret NPM_TOKEN.
  3. Поднять версию и запушить тег:
npm version patch
git push origin main --tags

CI (.github/workflows/publish.yml) выполнит npm publish при теге v*.*.*.

| Workflow | Назначение | |----------|------------| | ci.yml | сборка на PR и push в main | | publish.yml | публикация по git-тегу |


Частые проблемы

| Симптом | Решение | |---------|---------| | Axios не найден | Задайте BASE_API_URL на window / global, импортируйте @space-ai/contracts/axios или вызовите setupApiClient() | | Запросы без baseURL | BASE_API_URL не был задан до инициализации — используйте Способ 1 или Способ 2 | | Двойной axios | Не вызывайте и setupApiClient(), и initApiClient() с разными инстансами без необходимости |