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

sap-bridge

v0.1.0

Published

Wrapper TypeScript para interactuar con SAP Service Layer y HANA

Readme

SAP Bridge

Este es un wrapper TypeScript para interactuar con dos sistemas SAP: el Service Layer (API OData REST de SAP Business One) y la base de datos SAP HANA.


Arquitectura general

SapConn()           → lee credenciales de variables de entorno
SapProvider()       → factory: crea SessionHandler + SapApi listos para usar
SessionHandler      → maneja el ciclo de vida de la sesión (login, storage, expiración)
SapApi              → cliente HTTP con métodos get/post/patch + consultas HANA

Flujo de una llamada a la API

  1. Se llama api.get("Orders") (decorado con @OnSession)
  2. El decorador verifica si hay sesión activa; si no, hace login() automáticamente
  3. Inyecta session y apiUrl al método real
  4. Si la respuesta indica sesión expirada (expired: true), limpia la sesión, re-loguea y reintenta

Para HANA, el decorador @OnHana inyecta los parámetros de conexión y ejecuta la query con @sap/hana-client.


Almacenamiento de sesión (dos modos)

| Modo | Clase | Cuándo usar | |------|-------|-------------| | json (default) | JsonFileSessionAdapter | Desarrollo local o apps single-process | | redis | RedisSessionAdapter | Entornos multi-proceso/servidor |

La sesión guarda: { id, node, timeout } — el B1SESSION cookie y el ROUTEID.

Redis es una dependencia opcional. Solo es necesario instalarla si se usa storageType: 'redis':

npm install redis

Respuesta unificada

Todos los métodos devuelven ApiResponse<T>:

{ isOk: boolean, mssg: string, data?: T, expired?: boolean }

SAP Service Layer puede responder en tres formatos distintos. La librería los clasifica internamente con type guards y siempre expone el mismo ApiResponse:

| Formato SAP | Guard | data que recibe el usuario | |-------------|-------|------------------------------| | Colección (value: T[]) | isCollection | El array directo | | Entidad única (odata.metadata + objeto) | isSingle | El objeto completo | | Especial (sin odata.metadata) | isSpecial | El objeto tal cual | | Sin body (HTTP 204) | — | undefined |

El usuario nunca necesita distinguir el formato SAP — siempre accede a result.data.


Variables de entorno requeridas

# Service Layer
SAP_API_COMPANY=...
SAP_API_SL_URL=...
SAP_API_SL_UID=...
SAP_API_SL_PWD=...
SAP_API_SL_STG=...

# HANA
SAP_API_HN_URL=...
SAP_API_HN_UID=...
SAP_API_HN_PWD=...
SAP_API_HN_SSL=...

Uso típico

const api = SapProvider(SapConn(), { debug: true });

// GET — sesión manejada automáticamente por @OnSession
const result = await api.get('Orders?$top=10');
if (result.isOk) console.log(result.data);

// HANA — consulta SQL directa
const rows = await api.hana.query(`
  SELECT "DocNum", "CardCode", "DocTotal"
  FROM "${api.company}"."ORDR"
  LIMIT 10
`);
if (rows.isOk) console.log(rows.data);

Batch requests

El método batch() permite enviar múltiples operaciones en una sola llamada HTTP usando el protocolo multipart/mixed de SAP.

El payload debe construirse manualmente — cada parte es una operación HTTP independiente:

function buildPayload(collection: string, records: any[]): [string, string] {
  const boundary = `batch_${Date.now()}`;
  const parts: string[] = [];

  records.forEach((record, idx) => {
    const part = [
      `--${boundary}`,
      'Content-Type: application/http',
      'Content-Transfer-Encoding: binary',
      `Content-ID: ${idx + 1}`,
      '',
      `POST /b1s/v1/${collection} HTTP/1.1`,
      'Content-Type: application/json',
      'Accept: application/json',
      'B1S-ReplaceCollectionsOnPatch: false',
      '',
      JSON.stringify(record),
      '',
    ].join('\r\n');
    parts.push(part);
  });

  parts.push(`--${boundary}--`);
  return [boundary, parts.join('\r\n')] as const;
}

const [boundary, body] = buildPayload('MyCollection', records);

const result = await api.batch('$batch', body, {
  'Content-Type': `multipart/mixed; boundary=${boundary}`,
  'Accept': 'multipart/mixed',
  'Prefer': 'odata.continue-on-error',
});

if (result.isOk) {
  // result.data es el string multipart crudo de la respuesta SAP
  console.log(result.data);
} else {
  console.log('Error:', result.mssg);
}

La respuesta de SAP (result.data) es un string multipart/mixed crudo que debe parsearse según las necesidades de cada caso.