first
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import { SapB1Client, SapB1ClientOptions } from "./client/SapB1Client";
|
||||
import { SapB1Config } from "./config";
|
||||
import { BusinessPartnersResource } from "./resources/BusinessPartners";
|
||||
import { DeliveryNotesResource, InvoicesResource, OrdersResource, PurchaseOrdersResource } from "./resources/Documents";
|
||||
import { ItemsResource } from "./resources/Items";
|
||||
import { StockTransfersResource } from "./resources/StockTransfers";
|
||||
|
||||
export class SapBusinessOneServiceLayer {
|
||||
public readonly client: SapB1Client;
|
||||
public readonly businessPartners: BusinessPartnersResource;
|
||||
public readonly items: ItemsResource;
|
||||
public readonly orders: OrdersResource;
|
||||
public readonly invoices: InvoicesResource;
|
||||
public readonly purchaseOrders: PurchaseOrdersResource;
|
||||
public readonly deliveryNotes: DeliveryNotesResource;
|
||||
public readonly stockTransfers: StockTransfersResource;
|
||||
|
||||
public constructor(config: SapB1Config, options: SapB1ClientOptions = {}) {
|
||||
this.client = new SapB1Client(config, options);
|
||||
this.businessPartners = new BusinessPartnersResource(this.client);
|
||||
this.items = new ItemsResource(this.client);
|
||||
this.orders = new OrdersResource(this.client);
|
||||
this.invoices = new InvoicesResource(this.client);
|
||||
this.purchaseOrders = new PurchaseOrdersResource(this.client);
|
||||
this.deliveryNotes = new DeliveryNotesResource(this.client);
|
||||
this.stockTransfers = new StockTransfersResource(this.client);
|
||||
}
|
||||
|
||||
public login(): Promise<unknown> {
|
||||
return this.client.login();
|
||||
}
|
||||
|
||||
public logout(): Promise<void> {
|
||||
return this.client.logout();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
import "../client/axiosTypes";
|
||||
import { AxiosInstance } from "axios";
|
||||
import { z } from "zod";
|
||||
import { SapB1Config } from "../config";
|
||||
import { SapB1Error } from "../errors/SapB1Error";
|
||||
import { SapB1Logger, noopLogger, sanitizeLogMeta } from "../utils/logger";
|
||||
|
||||
const LoginResponseSchema = z.object({
|
||||
SessionId: z.string().optional(),
|
||||
Version: z.string().optional(),
|
||||
SessionTimeout: z.number().optional()
|
||||
});
|
||||
|
||||
export interface SapB1Session {
|
||||
b1session: string;
|
||||
routeId?: string;
|
||||
expiresAt?: number;
|
||||
}
|
||||
|
||||
export class SessionManager {
|
||||
private session?: SapB1Session;
|
||||
private loginPromise?: Promise<SapB1Session>;
|
||||
|
||||
public constructor(
|
||||
private readonly http: AxiosInstance,
|
||||
private readonly config: SapB1Config,
|
||||
private readonly logger: SapB1Logger = noopLogger
|
||||
) {}
|
||||
|
||||
public get currentSession(): SapB1Session | undefined {
|
||||
return this.session;
|
||||
}
|
||||
|
||||
public async getSession(forceRefresh = false): Promise<SapB1Session> {
|
||||
if (!forceRefresh && this.session && !this.isExpired(this.session)) {
|
||||
return this.session;
|
||||
}
|
||||
|
||||
if (!this.loginPromise) {
|
||||
this.loginPromise = this.login().finally(() => {
|
||||
this.loginPromise = undefined;
|
||||
});
|
||||
}
|
||||
|
||||
return this.loginPromise;
|
||||
}
|
||||
|
||||
public async login(): Promise<SapB1Session> {
|
||||
this.logger.info("SAP B1 login request", sanitizeLogMeta({ baseUrl: this.config.baseUrl, companyDB: this.config.companyDB }));
|
||||
|
||||
try {
|
||||
const response = await this.http.post(
|
||||
"/Login",
|
||||
{
|
||||
CompanyDB: this.config.companyDB,
|
||||
UserName: this.config.username,
|
||||
Password: this.config.password,
|
||||
Language: this.config.language
|
||||
},
|
||||
{ skipAuth: true }
|
||||
);
|
||||
|
||||
LoginResponseSchema.parse(response.data);
|
||||
const cookies = this.extractCookies(response.headers["set-cookie"]);
|
||||
const b1session = cookies.B1SESSION || response.data.SessionId;
|
||||
|
||||
if (!b1session) {
|
||||
throw new SapB1Error({ message: "SAP B1 login did not return B1SESSION cookie or SessionId" });
|
||||
}
|
||||
|
||||
const timeoutMinutes = typeof response.data.SessionTimeout === "number" ? response.data.SessionTimeout : 30;
|
||||
this.session = {
|
||||
b1session,
|
||||
routeId: cookies.ROUTEID,
|
||||
expiresAt: Date.now() + Math.max(timeoutMinutes - 1, 1) * 60_000
|
||||
};
|
||||
|
||||
return this.session;
|
||||
} catch (error) {
|
||||
if (error instanceof SapB1Error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw new SapB1Error({ message: "SAP B1 login failed", cause: error });
|
||||
}
|
||||
}
|
||||
|
||||
public async logout(): Promise<void> {
|
||||
if (!this.session) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await this.http.post("/Logout", undefined, { skipAuth: false });
|
||||
} finally {
|
||||
this.session = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
public buildCookieHeader(session: SapB1Session): string {
|
||||
const parts = [`B1SESSION=${session.b1session}`];
|
||||
|
||||
if (session.routeId) {
|
||||
parts.push(`ROUTEID=${session.routeId}`);
|
||||
}
|
||||
|
||||
return parts.join("; ");
|
||||
}
|
||||
|
||||
public clearSession(): void {
|
||||
this.session = undefined;
|
||||
}
|
||||
|
||||
private isExpired(session: SapB1Session): boolean {
|
||||
return session.expiresAt !== undefined && session.expiresAt <= Date.now();
|
||||
}
|
||||
|
||||
private extractCookies(setCookieHeader: string[] | string | undefined): Record<string, string> {
|
||||
const headers = Array.isArray(setCookieHeader) ? setCookieHeader : setCookieHeader ? [setCookieHeader] : [];
|
||||
const cookies: Record<string, string> = {};
|
||||
|
||||
for (const header of headers) {
|
||||
const [nameValue] = header.split(";");
|
||||
const separatorIndex = nameValue.indexOf("=");
|
||||
|
||||
if (separatorIndex > 0) {
|
||||
cookies[nameValue.slice(0, separatorIndex)] = nameValue.slice(separatorIndex + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return cookies;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
import "./axiosTypes";
|
||||
import https from "node:https";
|
||||
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, Method } from "axios";
|
||||
import { z } from "zod";
|
||||
import { SessionManager } from "../auth/SessionManager";
|
||||
import { SapB1Config, SapB1ConfigSchema } from "../config";
|
||||
import { SapB1Error } from "../errors/SapB1Error";
|
||||
import { ODataQuery, ODataResponse } from "../types/odata";
|
||||
import { buildODataParams, extractNextLink } from "./odata";
|
||||
import { SapB1Logger, noopLogger, sanitizeLogMeta } from "../utils/logger";
|
||||
import { sleep } from "../utils/sleep";
|
||||
|
||||
export interface SapB1ClientOptions {
|
||||
logger?: SapB1Logger;
|
||||
http?: AxiosInstance;
|
||||
}
|
||||
|
||||
export class SapB1Client {
|
||||
private readonly http: AxiosInstance;
|
||||
private readonly logger: SapB1Logger;
|
||||
public readonly sessionManager: SessionManager;
|
||||
public readonly config: SapB1Config;
|
||||
|
||||
public constructor(config: SapB1Config, options: SapB1ClientOptions = {}) {
|
||||
this.config = SapB1ConfigSchema.parse(config);
|
||||
this.logger = options.logger ?? noopLogger;
|
||||
this.http =
|
||||
options.http ??
|
||||
axios.create({
|
||||
baseURL: this.buildServiceLayerBaseUrl(this.config.baseUrl),
|
||||
timeout: this.config.timeout,
|
||||
httpsAgent: new https.Agent({ rejectUnauthorized: this.config.rejectUnauthorized })
|
||||
});
|
||||
this.sessionManager = new SessionManager(this.http, this.config, this.logger);
|
||||
}
|
||||
|
||||
public login(): Promise<unknown> {
|
||||
return this.sessionManager.getSession(true);
|
||||
}
|
||||
|
||||
public logout(): Promise<void> {
|
||||
return this.sessionManager.logout();
|
||||
}
|
||||
|
||||
public get<T>(path: string, query?: ODataQuery, schema?: z.ZodType<T>): Promise<T> {
|
||||
return this.request<T>("GET", path, undefined, { params: buildODataParams(query), schema });
|
||||
}
|
||||
|
||||
public post<T>(path: string, data?: unknown, schema?: z.ZodType<T>): Promise<T> {
|
||||
return this.request<T>("POST", path, data, { schema });
|
||||
}
|
||||
|
||||
public patch<T>(path: string, data?: unknown, schema?: z.ZodType<T>): Promise<T> {
|
||||
return this.request<T>("PATCH", path, data, { schema });
|
||||
}
|
||||
|
||||
public delete<T>(path: string, schema?: z.ZodType<T>): Promise<T> {
|
||||
return this.request<T>("DELETE", path, undefined, { schema });
|
||||
}
|
||||
|
||||
public async getAll<T>(path: string, query: ODataQuery = {}, schema?: z.ZodType<T>): Promise<T[]> {
|
||||
const items: T[] = [];
|
||||
let nextPath: string | undefined = path;
|
||||
let nextQuery: ODataQuery | undefined = query;
|
||||
|
||||
while (nextPath) {
|
||||
const response: ODataResponse<T> = await this.get<ODataResponse<T>>(nextPath, nextQuery);
|
||||
const value = schema ? z.array(schema).parse(response.value) : response.value;
|
||||
items.push(...value);
|
||||
const nextLink: string | undefined = extractNextLink(response);
|
||||
nextPath = nextLink ? this.normalizeNextLink(nextLink) : undefined;
|
||||
nextQuery = undefined;
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
private async request<T>(
|
||||
method: Method,
|
||||
path: string,
|
||||
data?: unknown,
|
||||
options: AxiosRequestConfig & { schema?: z.ZodType<T> } = {}
|
||||
): Promise<T> {
|
||||
const requestConfig: AxiosRequestConfig = {
|
||||
...options,
|
||||
method,
|
||||
url: path,
|
||||
data,
|
||||
headers: {
|
||||
...(options.headers || {})
|
||||
}
|
||||
};
|
||||
|
||||
for (let attempt = 0; attempt <= this.config.retryCount; attempt += 1) {
|
||||
try {
|
||||
if (!requestConfig.skipAuth) {
|
||||
const session = await this.sessionManager.getSession();
|
||||
requestConfig.headers = {
|
||||
...(requestConfig.headers || {}),
|
||||
Cookie: this.sessionManager.buildCookieHeader(session)
|
||||
};
|
||||
}
|
||||
|
||||
this.logger.debug("SAP B1 request", sanitizeLogMeta({ method, url: path, attempt }));
|
||||
const response = await this.http.request(requestConfig);
|
||||
return options.schema ? options.schema.parse(response.data) : (response.data as T);
|
||||
} catch (error) {
|
||||
const sapError = this.toSapB1Error(error, method, path);
|
||||
const canRelogin = sapError.status === 401 && !requestConfig.skipAuth && attempt === 0;
|
||||
|
||||
if (canRelogin) {
|
||||
this.sessionManager.clearSession();
|
||||
await this.sessionManager.getSession(true);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!requestConfig.skipRetry && sapError.retryable && attempt < this.config.retryCount) {
|
||||
await sleep(this.config.retryDelayMs * (attempt + 1));
|
||||
continue;
|
||||
}
|
||||
|
||||
throw sapError;
|
||||
}
|
||||
}
|
||||
|
||||
throw new SapB1Error({ message: "SAP B1 request failed after retries", method, url: path });
|
||||
}
|
||||
|
||||
private toSapB1Error(error: unknown, method: Method, path: string): SapB1Error {
|
||||
if (error instanceof SapB1Error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
if (axios.isAxiosError(error)) {
|
||||
const axiosError = error as AxiosError<{ error?: { code?: string; message?: { value?: string } | string } }>;
|
||||
const status = axiosError.response?.status;
|
||||
const sapError = axiosError.response?.data?.error;
|
||||
const message =
|
||||
typeof sapError?.message === "string"
|
||||
? sapError.message
|
||||
: sapError?.message?.value || axiosError.message || "SAP B1 request failed";
|
||||
|
||||
return new SapB1Error({
|
||||
status,
|
||||
code: sapError?.code,
|
||||
message,
|
||||
method,
|
||||
url: path,
|
||||
retryable: status === 408 || status === 429 || (status !== undefined && status >= 500),
|
||||
cause: error
|
||||
});
|
||||
}
|
||||
|
||||
return new SapB1Error({ message: "SAP B1 request failed", method, url: path, cause: error });
|
||||
}
|
||||
|
||||
private buildServiceLayerBaseUrl(baseUrl: string): string {
|
||||
const trimmed = baseUrl.replace(/\/+$/, "");
|
||||
return trimmed.endsWith("/b1s/v1") ? trimmed : `${trimmed}/b1s/v1`;
|
||||
}
|
||||
|
||||
private normalizeNextLink(nextLink: string): string {
|
||||
try {
|
||||
const parsed = new URL(nextLink);
|
||||
return parsed.pathname.replace(/^.*\/b1s\/v1\/?/, "");
|
||||
} catch {
|
||||
return nextLink.replace(/^\/?b1s\/v1\/?/, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import "axios";
|
||||
|
||||
declare module "axios" {
|
||||
export interface AxiosRequestConfig {
|
||||
skipAuth?: boolean;
|
||||
skipRetry?: boolean;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { ODataQuery, ODataQuerySchema } from "../types/odata";
|
||||
|
||||
export function buildODataParams(query: ODataQuery = {}): Record<string, string | number> {
|
||||
const parsed = ODataQuerySchema.parse(query);
|
||||
const params: Record<string, string | number> = {};
|
||||
|
||||
if (parsed.select?.length) {
|
||||
params.$select = parsed.select.join(",");
|
||||
}
|
||||
|
||||
if (parsed.filter) {
|
||||
params.$filter = parsed.filter;
|
||||
}
|
||||
|
||||
if (parsed.top !== undefined) {
|
||||
params.$top = parsed.top;
|
||||
}
|
||||
|
||||
if (parsed.skip !== undefined) {
|
||||
params.$skip = parsed.skip;
|
||||
}
|
||||
|
||||
if (parsed.orderby) {
|
||||
params.$orderby = Array.isArray(parsed.orderby) ? parsed.orderby.join(",") : parsed.orderby;
|
||||
}
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
export function extractNextLink<T>(response: {
|
||||
"odata.nextLink"?: string;
|
||||
"@odata.nextLink"?: string;
|
||||
}): string | undefined {
|
||||
return response["@odata.nextLink"] || response["odata.nextLink"];
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import "dotenv/config";
|
||||
import { z } from "zod";
|
||||
|
||||
const booleanFromEnv = z
|
||||
.union([z.boolean(), z.string()])
|
||||
.optional()
|
||||
.transform((value) => {
|
||||
if (typeof value === "boolean") {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (value === undefined || value === "") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return ["1", "true", "yes", "on"].includes(value.toLowerCase());
|
||||
});
|
||||
|
||||
export const SapB1ConfigSchema = z.object({
|
||||
baseUrl: z.string().url(),
|
||||
companyDB: z.string().min(1),
|
||||
username: z.string().min(1),
|
||||
password: z.string().min(1),
|
||||
language: z.string().min(1).optional(),
|
||||
timeout: z.number().int().positive().default(30_000),
|
||||
rejectUnauthorized: z.boolean().default(true),
|
||||
retryCount: z.number().int().min(0).default(2),
|
||||
retryDelayMs: z.number().int().min(0).default(250)
|
||||
});
|
||||
|
||||
export type SapB1Config = z.infer<typeof SapB1ConfigSchema>;
|
||||
|
||||
export function loadSapB1ConfigFromEnv(env: NodeJS.ProcessEnv = process.env): SapB1Config {
|
||||
const rejectUnauthorized = booleanFromEnv.parse(env.SAP_B1_REJECT_UNAUTHORIZED);
|
||||
|
||||
return SapB1ConfigSchema.parse({
|
||||
baseUrl: env.SAP_B1_BASE_URL,
|
||||
companyDB: env.SAP_B1_COMPANY_DB,
|
||||
username: env.SAP_B1_USERNAME,
|
||||
password: env.SAP_B1_PASSWORD,
|
||||
language: env.SAP_B1_LANGUAGE || undefined,
|
||||
timeout: env.SAP_B1_TIMEOUT_MS ? Number(env.SAP_B1_TIMEOUT_MS) : undefined,
|
||||
rejectUnauthorized,
|
||||
retryCount: env.SAP_B1_RETRY_COUNT ? Number(env.SAP_B1_RETRY_COUNT) : undefined,
|
||||
retryDelayMs: env.SAP_B1_RETRY_DELAY_MS ? Number(env.SAP_B1_RETRY_DELAY_MS) : undefined
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
export interface SapB1ErrorDetails {
|
||||
status?: number;
|
||||
code?: string;
|
||||
message?: string;
|
||||
method?: string;
|
||||
url?: string;
|
||||
retryable?: boolean;
|
||||
cause?: unknown;
|
||||
}
|
||||
|
||||
export class SapB1Error extends Error {
|
||||
public readonly status?: number;
|
||||
public readonly code?: string;
|
||||
public readonly method?: string;
|
||||
public readonly url?: string;
|
||||
public readonly retryable: boolean;
|
||||
public override readonly cause?: unknown;
|
||||
|
||||
public constructor(details: SapB1ErrorDetails) {
|
||||
super(details.message || "SAP Business One Service Layer request failed");
|
||||
this.name = "SapB1Error";
|
||||
this.status = details.status;
|
||||
this.code = details.code;
|
||||
this.method = details.method;
|
||||
this.url = details.url;
|
||||
this.retryable = details.retryable ?? false;
|
||||
this.cause = details.cause;
|
||||
}
|
||||
}
|
||||
+123
-13
@@ -1,35 +1,145 @@
|
||||
import express from "express";
|
||||
import { SapBusinessOneServiceLayer } from "./SapBusinessOneServiceLayer";
|
||||
|
||||
export { SessionManager, SapB1Session } from "./auth/SessionManager";
|
||||
export { SapB1Client, SapB1ClientOptions } from "./client/SapB1Client";
|
||||
export { buildODataParams, extractNextLink } from "./client/odata";
|
||||
export { loadSapB1ConfigFromEnv, SapB1Config, SapB1ConfigSchema } from "./config";
|
||||
export { SapB1Error } from "./errors/SapB1Error";
|
||||
export { SapBusinessOneServiceLayer } from "./SapBusinessOneServiceLayer";
|
||||
export * from "./types/entities";
|
||||
export * from "./types/odata";
|
||||
|
||||
const app = express();
|
||||
const port = Number(process.env.PORT || 3000);
|
||||
const rootPath = process.env.ROOT_PATH || "";
|
||||
const serviceName = "SAP Business One";
|
||||
const serviceId = "sap-bo";
|
||||
|
||||
function openApiDocument(basePath = "") {
|
||||
return {
|
||||
openapi: "3.0.3",
|
||||
info: {
|
||||
title: "SAP Business One connector service",
|
||||
version: "1.0.0",
|
||||
description: "Health and documentation wrapper for the SAP Business One Service Layer TypeScript connector."
|
||||
},
|
||||
servers: [{ url: basePath || "/" }],
|
||||
paths: {
|
||||
"/": {
|
||||
get: {
|
||||
summary: "Service metadata",
|
||||
responses: {
|
||||
"200": { description: "Service status" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"/health": {
|
||||
get: {
|
||||
summary: "Health check",
|
||||
responses: {
|
||||
"200": { description: "Service is ready to accept traffic" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"/openapi.json": {
|
||||
get: {
|
||||
summary: "OpenAPI schema",
|
||||
responses: {
|
||||
"200": { description: "OpenAPI document" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function docsHtml(basePath = "") {
|
||||
const openApiUrl = `${basePath}/openapi.json`.replace("//", "/");
|
||||
|
||||
return `<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>SAP Business One connector docs</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; margin: 2rem; line-height: 1.5; color: #1f2937; }
|
||||
code, pre { background: #f3f4f6; border-radius: 4px; padding: 0.2rem 0.35rem; }
|
||||
a { color: #0f766e; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>SAP Business One connector</h1>
|
||||
<p>This service exposes AppFactory health and OpenAPI metadata. The connector itself is used as a TypeScript library.</p>
|
||||
<ul>
|
||||
<li><a href="${openApiUrl}">OpenAPI JSON</a></li>
|
||||
<li><code>GET ${basePath || ""}/health</code></li>
|
||||
</ul>
|
||||
<button type="button" id="try-health">Try health</button>
|
||||
<button type="button" id="try-openapi">Try OpenAPI</button>
|
||||
<pre id="result" aria-live="polite"></pre>
|
||||
<script>
|
||||
const result = document.getElementById("result");
|
||||
async function show(url) {
|
||||
const response = await fetch(url);
|
||||
const contentType = response.headers.get("content-type") || "";
|
||||
const body = contentType.includes("application/json") ? await response.json() : await response.text();
|
||||
result.textContent = JSON.stringify({ status: response.status, body }, null, 2);
|
||||
}
|
||||
document.getElementById("try-health").addEventListener("click", () => show("${basePath}/health".replace("//", "/")));
|
||||
document.getElementById("try-openapi").addEventListener("click", () => show("${openApiUrl}"));
|
||||
</script>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
|
||||
function serviceMetadata() {
|
||||
return {
|
||||
name: serviceName,
|
||||
service: serviceId,
|
||||
status: "ok"
|
||||
};
|
||||
}
|
||||
|
||||
app.get("/", (_req, res) => {
|
||||
res.json({
|
||||
name: "SAP Business One",
|
||||
service: "sap-bo",
|
||||
status: "ok"
|
||||
});
|
||||
res.json(serviceMetadata());
|
||||
});
|
||||
|
||||
app.get("/health", (_req, res) => {
|
||||
res.json({ status: "ok" });
|
||||
});
|
||||
|
||||
app.get("/openapi.json", (_req, res) => {
|
||||
res.json(openApiDocument(rootPath));
|
||||
});
|
||||
|
||||
app.get("/docs", (_req, res) => {
|
||||
res.type("html").send(docsHtml(rootPath));
|
||||
});
|
||||
|
||||
if (rootPath) {
|
||||
app.get(rootPath, (_req, res) => {
|
||||
res.json({
|
||||
name: "SAP Business One",
|
||||
service: "sap-bo",
|
||||
status: "ok"
|
||||
});
|
||||
res.json(serviceMetadata());
|
||||
});
|
||||
|
||||
app.get(rootPath + "/health", (_req, res) => {
|
||||
res.json({ status: "ok" });
|
||||
});
|
||||
|
||||
app.get(rootPath + "/openapi.json", (_req, res) => {
|
||||
res.json(openApiDocument(rootPath));
|
||||
});
|
||||
|
||||
app.get(rootPath + "/docs", (_req, res) => {
|
||||
res.type("html").send(docsHtml(rootPath));
|
||||
});
|
||||
}
|
||||
|
||||
app.listen(port, "0.0.0.0", () => {
|
||||
console.log("sap-bo listening on port " + port);
|
||||
});
|
||||
if (require.main === module) {
|
||||
app.listen(port, "0.0.0.0", () => {
|
||||
console.log(serviceId + " listening on port " + port);
|
||||
});
|
||||
}
|
||||
|
||||
export { app };
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { SapB1Client } from "../client/SapB1Client";
|
||||
import { BusinessPartner, BusinessPartnerCreateDto, BusinessPartnerUpdateDto } from "../types/entities";
|
||||
import { BusinessPartnerSchema } from "../types/schemas";
|
||||
import { Resource } from "./Resource";
|
||||
|
||||
export class BusinessPartnersResource extends Resource<
|
||||
BusinessPartner,
|
||||
BusinessPartnerCreateDto,
|
||||
BusinessPartnerUpdateDto,
|
||||
string
|
||||
> {
|
||||
public constructor(client: SapB1Client) {
|
||||
super(client, "BusinessPartners", BusinessPartnerSchema);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { SapB1Client } from "../client/SapB1Client";
|
||||
import { MarketingDocument, MarketingDocumentCreateDto, MarketingDocumentUpdateDto } from "../types/entities";
|
||||
import { MarketingDocumentSchema } from "../types/schemas";
|
||||
import { Resource } from "./Resource";
|
||||
|
||||
export class OrdersResource extends Resource<MarketingDocument, MarketingDocumentCreateDto, MarketingDocumentUpdateDto, number> {
|
||||
public constructor(client: SapB1Client) {
|
||||
super(client, "Orders", MarketingDocumentSchema, false);
|
||||
}
|
||||
}
|
||||
|
||||
export class InvoicesResource extends Resource<MarketingDocument, MarketingDocumentCreateDto, MarketingDocumentUpdateDto, number> {
|
||||
public constructor(client: SapB1Client) {
|
||||
super(client, "Invoices", MarketingDocumentSchema, false);
|
||||
}
|
||||
}
|
||||
|
||||
export class PurchaseOrdersResource extends Resource<
|
||||
MarketingDocument,
|
||||
MarketingDocumentCreateDto,
|
||||
MarketingDocumentUpdateDto,
|
||||
number
|
||||
> {
|
||||
public constructor(client: SapB1Client) {
|
||||
super(client, "PurchaseOrders", MarketingDocumentSchema, false);
|
||||
}
|
||||
}
|
||||
|
||||
export class DeliveryNotesResource extends Resource<
|
||||
MarketingDocument,
|
||||
MarketingDocumentCreateDto,
|
||||
MarketingDocumentUpdateDto,
|
||||
number
|
||||
> {
|
||||
public constructor(client: SapB1Client) {
|
||||
super(client, "DeliveryNotes", MarketingDocumentSchema, false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { SapB1Client } from "../client/SapB1Client";
|
||||
import { Item, ItemCreateDto, ItemUpdateDto } from "../types/entities";
|
||||
import { ItemSchema } from "../types/schemas";
|
||||
import { Resource } from "./Resource";
|
||||
|
||||
export class ItemsResource extends Resource<Item, ItemCreateDto, ItemUpdateDto, string> {
|
||||
public constructor(client: SapB1Client) {
|
||||
super(client, "Items", ItemSchema);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import { z } from "zod";
|
||||
import { SapB1Client } from "../client/SapB1Client";
|
||||
import { ODataQuery, ODataResponse } from "../types/odata";
|
||||
|
||||
export class Resource<TEntity, TCreate, TUpdate, TKey extends string | number = string | number> {
|
||||
public constructor(
|
||||
protected readonly client: SapB1Client,
|
||||
protected readonly path: string,
|
||||
protected readonly schema: z.ZodType<TEntity>,
|
||||
private readonly supportsDelete = true
|
||||
) {}
|
||||
|
||||
public list(query: ODataQuery = {}): Promise<ODataResponse<TEntity>> {
|
||||
return this.client.get<ODataResponse<TEntity>>(this.path, query);
|
||||
}
|
||||
|
||||
public listAll(query: ODataQuery = {}): Promise<TEntity[]> {
|
||||
return this.client.getAll<TEntity>(this.path, query, this.schema);
|
||||
}
|
||||
|
||||
public get(id: TKey): Promise<TEntity> {
|
||||
return this.client.get<TEntity>(`${this.path}(${this.formatKey(id)})`, undefined, this.schema);
|
||||
}
|
||||
|
||||
public create(data: TCreate): Promise<TEntity> {
|
||||
return this.client.post<TEntity>(this.path, data, this.schema);
|
||||
}
|
||||
|
||||
public update(id: TKey, data: TUpdate): Promise<void> {
|
||||
return this.client.patch<void>(`${this.path}(${this.formatKey(id)})`, data);
|
||||
}
|
||||
|
||||
public delete(id: TKey): Promise<void> {
|
||||
if (!this.supportsDelete) {
|
||||
throw new Error(`${this.path} does not support delete through this connector`);
|
||||
}
|
||||
|
||||
return this.client.delete<void>(`${this.path}(${this.formatKey(id)})`);
|
||||
}
|
||||
|
||||
protected formatKey(id: TKey): string {
|
||||
return typeof id === "number" ? String(id) : `'${String(id).replace(/'/g, "''")}'`;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { SapB1Client } from "../client/SapB1Client";
|
||||
import { StockTransfer, StockTransferCreateDto, StockTransferUpdateDto } from "../types/entities";
|
||||
import { StockTransferSchema } from "../types/schemas";
|
||||
import { Resource } from "./Resource";
|
||||
|
||||
export class StockTransfersResource extends Resource<StockTransfer, StockTransferCreateDto, StockTransferUpdateDto, number> {
|
||||
public constructor(client: SapB1Client) {
|
||||
super(client, "StockTransfers", StockTransferSchema, false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
export interface BusinessPartner {
|
||||
CardCode: string;
|
||||
CardName?: string;
|
||||
CardType?: "cCustomer" | "cSupplier" | "cLid";
|
||||
Phone1?: string;
|
||||
EmailAddress?: string;
|
||||
Currency?: string;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export type BusinessPartnerCreateDto = Pick<BusinessPartner, "CardCode"> & Partial<BusinessPartner>;
|
||||
export type BusinessPartnerUpdateDto = Partial<Omit<BusinessPartner, "CardCode">>;
|
||||
|
||||
export interface Item {
|
||||
ItemCode: string;
|
||||
ItemName?: string;
|
||||
ItemsGroupCode?: number;
|
||||
InventoryItem?: "tYES" | "tNO";
|
||||
SalesItem?: "tYES" | "tNO";
|
||||
PurchaseItem?: "tYES" | "tNO";
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export type ItemCreateDto = Pick<Item, "ItemCode"> & Partial<Item>;
|
||||
export type ItemUpdateDto = Partial<Omit<Item, "ItemCode">>;
|
||||
|
||||
export interface DocumentLine {
|
||||
ItemCode?: string;
|
||||
Quantity?: number;
|
||||
UnitPrice?: number;
|
||||
WarehouseCode?: string;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export interface MarketingDocument {
|
||||
DocEntry: number;
|
||||
DocNum?: number;
|
||||
CardCode?: string;
|
||||
DocDate?: string;
|
||||
DocDueDate?: string;
|
||||
TaxDate?: string;
|
||||
DocumentLines?: DocumentLine[];
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export type MarketingDocumentCreateDto = Omit<Partial<MarketingDocument>, "DocEntry" | "DocNum"> & {
|
||||
CardCode: string;
|
||||
DocumentLines?: DocumentLine[];
|
||||
};
|
||||
export type MarketingDocumentUpdateDto = Partial<Omit<MarketingDocument, "DocEntry" | "DocNum">>;
|
||||
|
||||
export interface StockTransferLine {
|
||||
ItemCode?: string;
|
||||
Quantity?: number;
|
||||
WarehouseCode?: string;
|
||||
FromWarehouseCode?: string;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export interface StockTransfer {
|
||||
DocEntry: number;
|
||||
DocNum?: number;
|
||||
DocDate?: string;
|
||||
FromWarehouse?: string;
|
||||
ToWarehouse?: string;
|
||||
StockTransferLines?: StockTransferLine[];
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export type StockTransferCreateDto = Omit<Partial<StockTransfer>, "DocEntry" | "DocNum"> & {
|
||||
StockTransferLines?: StockTransferLine[];
|
||||
};
|
||||
export type StockTransferUpdateDto = Partial<Omit<StockTransfer, "DocEntry" | "DocNum">>;
|
||||
@@ -0,0 +1,34 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export interface ODataQuery {
|
||||
select?: string[];
|
||||
filter?: string;
|
||||
top?: number;
|
||||
skip?: number;
|
||||
orderby?: string | string[];
|
||||
}
|
||||
|
||||
export interface ODataResponse<T> {
|
||||
value: T[];
|
||||
"odata.metadata"?: string;
|
||||
"odata.nextLink"?: string;
|
||||
"@odata.context"?: string;
|
||||
"@odata.nextLink"?: string;
|
||||
}
|
||||
|
||||
export const ODataQuerySchema = z.object({
|
||||
select: z.array(z.string().min(1)).optional(),
|
||||
filter: z.string().min(1).optional(),
|
||||
top: z.number().int().positive().optional(),
|
||||
skip: z.number().int().min(0).optional(),
|
||||
orderby: z.union([z.string().min(1), z.array(z.string().min(1))]).optional()
|
||||
});
|
||||
|
||||
export const ODataResponseSchema = <T extends z.ZodTypeAny>(itemSchema: T) =>
|
||||
z.object({
|
||||
value: z.array(itemSchema),
|
||||
"odata.metadata": z.string().optional(),
|
||||
"odata.nextLink": z.string().optional(),
|
||||
"@odata.context": z.string().optional(),
|
||||
"@odata.nextLink": z.string().optional()
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const UnknownRecordSchema = z.object({}).catchall(z.unknown());
|
||||
|
||||
export const BusinessPartnerSchema = UnknownRecordSchema.extend({
|
||||
CardCode: z.string()
|
||||
});
|
||||
|
||||
export const ItemSchema = UnknownRecordSchema.extend({
|
||||
ItemCode: z.string()
|
||||
});
|
||||
|
||||
export const MarketingDocumentSchema = UnknownRecordSchema.extend({
|
||||
DocEntry: z.number()
|
||||
});
|
||||
|
||||
export const StockTransferSchema = UnknownRecordSchema.extend({
|
||||
DocEntry: z.number()
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
export interface SapB1Logger {
|
||||
debug(message: string, meta?: Record<string, unknown>): void;
|
||||
info(message: string, meta?: Record<string, unknown>): void;
|
||||
warn(message: string, meta?: Record<string, unknown>): void;
|
||||
error(message: string, meta?: Record<string, unknown>): void;
|
||||
}
|
||||
|
||||
export const noopLogger: SapB1Logger = {
|
||||
debug: () => undefined,
|
||||
info: () => undefined,
|
||||
warn: () => undefined,
|
||||
error: () => undefined
|
||||
};
|
||||
|
||||
export function sanitizeLogMeta(meta: Record<string, unknown>): Record<string, unknown> {
|
||||
const forbidden = new Set(["password", "b1session", "routeid", "cookie", "authorization"]);
|
||||
|
||||
return Object.fromEntries(
|
||||
Object.entries(meta).map(([key, value]) => {
|
||||
if (forbidden.has(key.toLowerCase())) {
|
||||
return [key, "[redacted]"];
|
||||
}
|
||||
|
||||
return [key, value];
|
||||
})
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
Reference in New Issue
Block a user