fix(core): update
This commit is contained in:
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@mojoio/letterxpress',
|
||||
version: '1.0.12',
|
||||
description: 'an unofficial API package for the letterxpress API'
|
||||
}
|
@ -1 +1 @@
|
||||
export * from './letterxpress.classes.account';
|
||||
export * from './letterxpress.classes.account.js';
|
||||
|
@ -1,5 +1,6 @@
|
||||
import * as plugins from './letterxpress.plugins';
|
||||
import { response } from 'express';
|
||||
import { IObjectAction } from '@tsclass/tsclass/dist_ts/database/objectaction.js';
|
||||
|
||||
import * as plugins from './letterxpress.plugins.js';
|
||||
|
||||
export interface ILetterXpressConstructorOptions {
|
||||
username: string;
|
||||
@ -7,69 +8,103 @@ export interface ILetterXpressConstructorOptions {
|
||||
}
|
||||
|
||||
export class LetterXpressAccount {
|
||||
// STATIC
|
||||
public static async createAndInit(optionsArg: ConstructorParameters<typeof LetterXpressAccount>[0]) {
|
||||
const letterXpressInstance = new LetterXpressAccount(optionsArg);
|
||||
await letterXpressInstance.init();
|
||||
return letterXpressInstance;
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
public baseApiUrl = 'https://api.letterxpress.de/v1';
|
||||
public options: ILetterXpressConstructorOptions;
|
||||
public smartletterInstance: plugins.smartletter.Smartletter;
|
||||
|
||||
public letterSentObservable = new plugins.smartrx.rxjs.Subject<plugins.smartletter.Letter>();
|
||||
public letterSentObservable = new plugins.smartrx.rxjs.Subject<plugins.tsclass.business.ILetter>();
|
||||
|
||||
constructor(optionsArg: ILetterXpressConstructorOptions) {
|
||||
const done = plugins.smartpromise.defer();
|
||||
this.options = optionsArg;
|
||||
}
|
||||
|
||||
public async init() {
|
||||
this.smartletterInstance = await plugins.smartletter.Smartletter.createAndInit();
|
||||
}
|
||||
|
||||
/**
|
||||
* sends a letter
|
||||
* @param letterArg
|
||||
*/
|
||||
public async sendLetter(letterArg: plugins.smartletter.Letter) {
|
||||
const letterPdfResult = await letterArg.getPdfResult();
|
||||
public async sendLetter(letterArg: plugins.tsclass.business.ILetter) {
|
||||
const letter = await this.smartletterInstance.createLetterFromData(letterArg);
|
||||
const pdfToSend = await letter.getCombinedPdf();
|
||||
const response = await this.request('/setJob', 'POST', {
|
||||
letter: {
|
||||
base64_file: letterPdfResult.buffer.toString('base64'),
|
||||
base64_file: plugins.smartbuffer.arrayBufferToBase64(pdfToSend.buffer),
|
||||
base64_checksum: await plugins.smarthash.md5FromString(
|
||||
letterPdfResult.buffer.toString('base64')
|
||||
plugins.smartbuffer.arrayBufferToBase64(pdfToSend.buffer)
|
||||
),
|
||||
specification: {
|
||||
color: '4',
|
||||
mode: 'simplex',
|
||||
ship: letterArg.options.to.country === 'Germany' ? 'national' : 'international'
|
||||
}
|
||||
}
|
||||
ship: letterArg.to.address.country === 'Germany' ? 'national' : 'international',
|
||||
},
|
||||
},
|
||||
});
|
||||
letterArg.setProcessingId(response.body.letter.job_id);
|
||||
letterArg.objectActions.push({
|
||||
name: 'letterxpress-sent',
|
||||
message: 'requested letterxpress to send the letter',
|
||||
privateMessage: null,
|
||||
data: response.body.letter.job_id,
|
||||
timestamp: Date.now(),
|
||||
userId: null
|
||||
})
|
||||
return letterArg;
|
||||
}
|
||||
|
||||
public async cancelLetter(letterArg: plugins.smartletter.Letter) {
|
||||
const processingId = letterArg.getProcessingId();
|
||||
public async cancelLetter(letterArg: plugins.tsclass.business.ILetter) {
|
||||
const processingId = letterArg.objectActions.reduce<string>((previousVal, currentVal) => {
|
||||
if (currentVal.name === 'letterxpress-sent') {
|
||||
return currentVal.data
|
||||
} else {
|
||||
return previousVal;
|
||||
}
|
||||
}, null);
|
||||
return await this.cancelLetterByProcessingId(processingId);
|
||||
}
|
||||
|
||||
public async cancelLetterByProcessingId(processingId: string) {
|
||||
const response = await this.request(`/deleteJob/${processingId}`, 'DELETE');
|
||||
const response = await this.request(`/deleteJob/${processingId}`, 'DELETE', {
|
||||
letter: {}
|
||||
});
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* fires the request
|
||||
*/
|
||||
private async request(routeArg: string, methodArg: 'GET' | 'POST' | 'DELETE', payload: any = {}) {
|
||||
private async request(routeArg: string, methodArg: 'GET' | 'POST' | 'DELETE', payload: any) {
|
||||
const requestUrl = `${this.baseApiUrl}${routeArg}`;
|
||||
console.log(requestUrl);
|
||||
const requestData = {
|
||||
auth: {
|
||||
username: this.options.username,
|
||||
apikey: this.options.apiKey
|
||||
apikey: this.options.apiKey,
|
||||
},
|
||||
...payload
|
||||
...payload,
|
||||
};
|
||||
const response = await plugins.smartrequest.request(requestUrl, {
|
||||
method: methodArg,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
requestBody: JSON.stringify(requestData)
|
||||
requestBody: JSON.stringify(requestData),
|
||||
});
|
||||
console.log(response.body);
|
||||
return response;
|
||||
}
|
||||
|
||||
public async stop() {
|
||||
await this.smartletterInstance.stop();
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,15 @@
|
||||
import * as smartletter from '@pushrocks/smartletter';
|
||||
import * as smartbuffer from '@pushrocks/smartbuffer';
|
||||
import * as smarthash from '@pushrocks/smarthash';
|
||||
import * as smartletter from '@pushrocks/smartletter';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
import * as smartrx from '@pushrocks/smartrx';
|
||||
|
||||
export { smarthash, smartletter, smartrequest, smartrx };
|
||||
export { smartbuffer, smarthash, smartletter, smartpromise, smartrequest, smartrx };
|
||||
|
||||
// tsclass scope
|
||||
import * as tsclass from '@tsclass/tsclass';
|
||||
|
||||
export {
|
||||
tsclass
|
||||
}
|
||||
|
Reference in New Issue
Block a user