pdr2 typen erweitert und überarbeitet

This commit is contained in:
nwaschk 2025-06-23 17:00:54 +02:00
parent 23c052e98b
commit 8feb2f9e13
4 changed files with 152 additions and 68 deletions

View File

@ -16,20 +16,26 @@ app.on('ready', async () => {
} }
ipcMain.handle('ping', () => { ipcMain.handle('ping', () => {
const req = new ProdressConnection( const req = new ProdressConnection({
{ header: {
port: 4788, port: 4788,
host: 'localhost', host: 'localhost',
database: 'test', database: 'test',
handler: 'Shopify', handler: 'Shopify',
action: 'artikelSync', action: 'artikelSync',
}, },
[['string', 'HalloWelt']], body: {
[ additionalInformation: [],
['line1', 'string'], arguments: [[['string', 'HalloWelt']]],
['line2', 'string'], },
], response: {
); isStatic: true,
responseTupel: [
['line1', 'string'],
['line2', 'string'],
],
},
});
return req.handleRequest(); return req.handleRequest();
}); });
}); });

View File

@ -1,15 +1,7 @@
import {RequestLeser as ProdressListener, type R2AnfrageBlaupause} from '@prodress/pdr2com'; import {RequestLeser as ProdressListener, type R2AnfrageBlaupause} from '@prodress/pdr2com';
import {type ResponseTupel, type ResponseObject, ProdressRequest, type RequestTupel} from './wrapper.js';
import {Socket} from 'node:net'; import {Socket} from 'node:net';
import {stringifyError} from '../utils.js'; import {stringifyError} from '../utils.js';
import {ProdressRequest} from './wrapper.js';
export interface ProdressHeader {
port?: number;
host?: string;
database: string;
handler: string;
action: string;
}
/** /**
* Initializes a TCP Socket, sends a ProdressRequest and listens to this Socket for one Response * Initializes a TCP Socket, sends a ProdressRequest and listens to this Socket for one Response
@ -22,26 +14,27 @@ export class ProdressConnection {
private socket: Socket; private socket: Socket;
private request: ProdressRequest; private request: ProdressRequest;
private listener: ProdressListener; private listener: ProdressListener;
private requestTupelList: RequestTupel[]; private requestBody: Pdr2_RequestBody;
private responseTupelList: ResponseTupel[]; private responseSchema: Pdr2_ResponseSchema;
/** /**
* @param header Headerdata to initialize the the TCP Socket and the ProdressRequest * @param header Headerdata to initialize the the TCP Socket and the ProdressRequest
* @param requestTupelList Requestdata as list of tupel * @param body Requestdata as list of tupel
* @param responseTupelList Expected Responsedata as list of tupel * @param response Expected Responsedata as list of tupel
*/ */
public constructor(header: ProdressHeader, requestTupelList: RequestTupel[], responseTupelList: ResponseTupel[]) { public constructor({header, body, response}: Pdr2_Request) {
this.port = header.port ?? 4788; this.port = header.port ?? 4788;
this.host = header.host ?? 'localhost'; this.host = header.host ?? 'localhost';
this.socket = new Socket(); this.socket = new Socket();
this.request = new ProdressRequest(header.database, header.handler, header.action); this.request = new ProdressRequest(header.database, header.handler, header.action);
this.listener = new ProdressListener(); this.listener = new ProdressListener();
this.requestTupelList = requestTupelList; this.requestBody = body;
this.responseTupelList = responseTupelList; this.responseSchema = response;
} }
/** /**
* Handels the request- and response-flow * Handels the request- and response-flow
* @returns the stringified response
*/ */
public async handleRequest(): Promise<string> { public async handleRequest(): Promise<string> {
let response: string = ''; let response: string = '';
@ -61,7 +54,7 @@ export class ProdressConnection {
// Sends the writes and sends the request when the connecting is established // Sends the writes and sends the request when the connecting is established
this.socket.once('connect', () => { this.socket.once('connect', () => {
try { try {
this.request.writeEntries(this.requestTupelList); this.request.writeEntries(this.requestBody);
this.request.sende(this.socket); this.request.sende(this.socket);
} catch (error) { } catch (error) {
response = stringifyError(error as Error); response = stringifyError(error as Error);
@ -116,30 +109,36 @@ export class ProdressConnection {
* @param responseInstance The Listener Instance of the active TCP Socket * @param responseInstance The Listener Instance of the active TCP Socket
* @returns ResponseObject * @returns ResponseObject
*/ */
private readResponse(responseInstance: R2AnfrageBlaupause): ResponseObject { private readResponse(responseInstance: R2AnfrageBlaupause): Pdr2_ResponseObject {
const response: ResponseObject = {}; const response: Pdr2_ResponseObject = {};
let amountOfEntries = 1;
for (const [key, type] of this.responseTupelList) { if (!this.responseSchema.isStatic) {
switch (type) { amountOfEntries = responseInstance.nächsteGanzzahl();
case 'string':
response[key] = responseInstance.nächsterText();
break;
case 'integer':
response[key] = responseInstance.nächsteGanzzahl();
break;
case 'float':
response[key] = responseInstance.nächsteDezimalzahl();
break;
case 'date':
response[key] = responseInstance.nächstesDatum();
break;
case 'date_time':
//@todo missing method in lib
response[key] = responseInstance.nächstesDatum();
break;
}
} }
for (let i = 1; i <= amountOfEntries; i++) {
for (const [key, type] of this.responseSchema.responseTupel) {
switch (type) {
case 'string':
response[key] = responseInstance.nächsterText();
break;
case 'integer':
response[key] = responseInstance.nächsteGanzzahl();
break;
case 'float':
response[key] = responseInstance.nächsteDezimalzahl();
break;
case 'date':
response[key] = responseInstance.nächstesDatum();
break;
case 'date_time':
//@todo missing method in lib
response[key] = responseInstance.nächstesDatum();
break;
}
}
}
return response; return response;
} }
} }

View File

@ -1,23 +1,5 @@
import {Request} from '@prodress/pdr2com'; import {Request} from '@prodress/pdr2com';
export interface DataTypeConversion {
string: () => string;
integer: () => number;
float: () => number;
date: () => Date | null;
date_time: () => Date | null;
}
export interface ResponseObject {
[key: string]: ReturnType<DataTypeConversion[keyof DataTypeConversion]>;
}
export type ResponseTupel = [string, keyof DataTypeConversion];
export type RequestTupel = {
[K in keyof DataTypeConversion]: [K, NonNullable<ReturnType<DataTypeConversion[K]>>];
}[keyof DataTypeConversion];
export class ProdressRequest extends Request { export class ProdressRequest extends Request {
public constructor(db: string, handler: string, action: string) { public constructor(db: string, handler: string, action: string) {
super( super(
@ -45,19 +27,30 @@ export class ProdressRequest extends Request {
/** /**
* writes entries based on the provided requestTupelList into the request * writes entries based on the provided requestTupelList into the request
* @param requestTupelList * @param requestBody
*/ */
public writeEntries(requestTupelList: RequestTupel[]): void { public writeEntries(requestBody: Pdr2_RequestBody): void {
for (const tupel of requestTupelList) { // write additionalInformation
for (const tupel of requestBody.additionalInformation) {
this.writeEntry(tupel); this.writeEntry(tupel);
} }
// write amount of argument entries
this.writeEntry(['integer', requestBody.arguments.length]);
// write arguments
for (const args of requestBody.arguments) {
for (const tupel of args) {
this.writeEntry(tupel);
}
}
} }
/** /**
* writes an entry based on the provided requestTupel into the request * writes an entry based on the provided requestTupel into the request
* @param requestTupel * @param requestTupel
*/ */
public writeEntry(requestTupel: RequestTupel): void { public writeEntry(requestTupel: Pdr2_RequestTupel): void {
const [type, value] = requestTupel; const [type, value] = requestTupel;
switch (type) { switch (type) {
case 'string': case 'string':

86
types.d.ts vendored
View File

@ -11,3 +11,89 @@ interface Window {
type UnsubscribeFunction = () => void; type UnsubscribeFunction = () => void;
type SubscribeFunction<Event extends keyof EventPayloadMapping> = (payload: EventPayloadMapping[Event]) => void; type SubscribeFunction<Event extends keyof EventPayloadMapping> = (payload: EventPayloadMapping[Event]) => void;
// --- Prodress R2 Request ---
/**
* structure of a handler based prodress r2 request schema
*/
type Pdr2_Schema = {
handler: string;
actions: Pdr2_ActionSchema[];
};
/**
* structure of an action based prodress r2 request subschema
*/
type Pdr2_ActionSchema = {
action: string;
additionalInformation: Pdr2_RequestTupel[];
arguments: Pdr2_RequestTupel[];
response: Pdr2_ResponseSchema;
};
/**
* structure of the prodressd r2 request header
*/
interface Pdr2_Header {
port?: number;
host?: string;
database: string;
handler: string;
action: string;
}
/**
* mapping for string alias to scalar type with pdr2 read and write methods
*/
interface Pdr2_DataTypeConversion {
string: () => string;
integer: () => number;
float: () => number;
date: () => Date | null;
date_time: () => Date | null;
}
/**
* structrue of a response object which is already read
*/
interface Pdr2_ResponseObject {
[key: string]: ReturnType<Pdr2_DataTypeConversion[keyof Pdr2_DataTypeConversion]>;
}
/**
* structure of a response tupel used inside of the response schema
*/
type Pdr2_ResponseTupel = [string, keyof Pdr2_DataTypeConversion];
/**
* strucutre of a request tupel used inside the request body and schema
*/
type Pdr2_RequestTupel = {
[K in keyof Pdr2_DataTypeConversion]: [K, NonNullable<ReturnType<Pdr2_DataTypeConversion[K]>>];
}[keyof Pdr2_DataTypeConversion];
/**
* structure of a prodress r2 request object recieved by the user
*/
type Pdr2_Request = {
header: Pdr2_Header;
body: Pdr2_RequestBody;
response: Pdr2_ResponseSchema;
};
/**
* structure of a prodress r2 request body
*/
type Pdr2_RequestBody = {
additionalInformation: Pdr2_RequestTupel[];
arguments: Pdr2_RequestTupel[][];
};
/**
* structure of a prodress r2 response schema
*/
type Pdr2_ResponseSchema = {
isStatic: boolean;
responseTupel: Pdr2_ResponseTupel[];
};