sdx-mailer-sdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/package.json +32 -0
- package/src/CourierMail.ts +50 -0
- package/src/entities/CourierExceptionRequest.ts +13 -0
- package/src/entities/CourierLink.ts +4 -0
- package/src/entities/CourierRequest.ts +10 -0
- package/src/entities/ServiceResponse.ts +40 -0
- package/src/entities/WebServiceException.ts +47 -0
- package/src/services/CourierService.ts +28 -0
- package/src/utils/Commons.ts +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 softdirex
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sdx-mailer-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A api rest sdx-mailer-sender sdk",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/softdirex/sdx-mailer-sdk.git"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"typescript",
|
|
17
|
+
"npm",
|
|
18
|
+
"package"
|
|
19
|
+
],
|
|
20
|
+
"author": "Softdirex",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"typescript": "^5.2.2"
|
|
24
|
+
},
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/yourusername/my-package/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/yourusername/my-package#readme",
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"axios": "^1.5.1"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import CourierExceptionRequest from "./entities/CourierExceptionRequest";
|
|
2
|
+
import CourierLink from "./entities/CourierLink";
|
|
3
|
+
import CourierRequest from "./entities/CourierRequest";
|
|
4
|
+
import WebServiceException from "./entities/WebServiceException";
|
|
5
|
+
import CourierService from "./services/CourierService";
|
|
6
|
+
import Commons from "./utils/Commons";
|
|
7
|
+
|
|
8
|
+
export default class CourierMail {
|
|
9
|
+
|
|
10
|
+
private service: CourierService;
|
|
11
|
+
|
|
12
|
+
constructor() {
|
|
13
|
+
this.service = new CourierService();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public createCourierRequest(title:string, subject:string,message:string,receiver:string,reply_mail:string,btn_label:string|null,btn_link:string|null):CourierRequest {
|
|
17
|
+
let request:CourierRequest = new CourierRequest();
|
|
18
|
+
request.title = title
|
|
19
|
+
request.subject = subject
|
|
20
|
+
request.message = message
|
|
21
|
+
request.receiver = receiver
|
|
22
|
+
request.reply_mail = reply_mail
|
|
23
|
+
if(btn_label != null || btn_link != null){
|
|
24
|
+
request.link = new CourierLink();
|
|
25
|
+
request.link.btn_label = btn_label
|
|
26
|
+
request.link.btn_link = btn_link
|
|
27
|
+
}
|
|
28
|
+
return request;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public createCourierExceptionRequest(error:string, detail:string,context:string|null):CourierExceptionRequest {
|
|
32
|
+
return new CourierExceptionRequest(error,detail,context);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public async sendCourierMail(endpoint:string,tokenRQ:any,productId:number,isBasic:boolean) {
|
|
36
|
+
const url = endpoint + '/courier' + ((isBasic)? '/basic' : '');
|
|
37
|
+
if(!Commons.validField(tokenRQ.trx)){
|
|
38
|
+
throw WebServiceException.BAD_REQUEST("Request must be signed")
|
|
39
|
+
}
|
|
40
|
+
return await this.service.postMail(null, tokenRQ, url, 'sendContactMail',productId);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public async sendExceptionMail(endpoint:string,tokenRQ:any,productId:number) {
|
|
44
|
+
const url = endpoint + '/courier';
|
|
45
|
+
if(!Commons.validField(tokenRQ.trx)){
|
|
46
|
+
throw WebServiceException.BAD_REQUEST("Request must be signed")
|
|
47
|
+
}
|
|
48
|
+
return await this.service.postMail(null, tokenRQ, url, 'sendContactMail',productId);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
|
|
2
|
+
export default class CourierExceptionRequest {
|
|
3
|
+
|
|
4
|
+
constructor(error:string,detail:string,context:string|null){
|
|
5
|
+
this.error = error
|
|
6
|
+
this.detail = detail
|
|
7
|
+
this.context = context
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public error: string;
|
|
11
|
+
public detail: string|null;
|
|
12
|
+
public context: string|null;
|
|
13
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
|
|
2
|
+
export default class ServiceResponse {
|
|
3
|
+
public code: number
|
|
4
|
+
public message: string
|
|
5
|
+
public detail: string
|
|
6
|
+
|
|
7
|
+
constructor(code: number, message: string) {
|
|
8
|
+
this.code = code
|
|
9
|
+
this.message = message
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public static BAD_REQUEST(): ServiceResponse {
|
|
13
|
+
var result = new ServiceResponse(400, "Bad Request")
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public static BUSINESS_ERROR(): ServiceResponse {
|
|
18
|
+
return new ServiceResponse(500, "Business Error")
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public static OK(): ServiceResponse {
|
|
22
|
+
return new ServiceResponse(200, "Ok")
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public static UNAUTHORIZED(): ServiceResponse {
|
|
26
|
+
return new ServiceResponse(401, "Unauthorized")
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public static NOT_FOUND(): ServiceResponse {
|
|
30
|
+
return new ServiceResponse(404, "Not Found")
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public static SERVICE_UNAVAILABLE(): ServiceResponse {
|
|
34
|
+
return new ServiceResponse(503, "Service Unavailable")
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public static METHOD_NOT_ALLOWED(): ServiceResponse {
|
|
38
|
+
return new ServiceResponse(405, "Method Not Allowed")
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import ServiceResponse from "./ServiceResponse";
|
|
2
|
+
|
|
3
|
+
export default class WebServiceException {
|
|
4
|
+
public code: number;
|
|
5
|
+
public detail: string;
|
|
6
|
+
public message: string;
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
constructor(message: string, code: number, detail: string) {
|
|
10
|
+
this.message = message;
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.detail = detail;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public static BAD_REQUEST(detail: string): WebServiceException {
|
|
16
|
+
return this.buildStaticClass(ServiceResponse.BAD_REQUEST(), detail);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public static BUSINESS_ERROR(detail: string): WebServiceException {
|
|
20
|
+
return this.buildStaticClass(ServiceResponse.BUSINESS_ERROR(), detail);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public static OK(detail: string): WebServiceException {
|
|
24
|
+
return this.buildStaticClass(ServiceResponse.OK(), detail);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public static UNAUTHORIZED(detail: string): WebServiceException {
|
|
28
|
+
return this.buildStaticClass(ServiceResponse.UNAUTHORIZED(), detail);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public static NOT_FOUND(detail: string): WebServiceException {
|
|
32
|
+
return this.buildStaticClass(ServiceResponse.NOT_FOUND(), detail);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public static METHOD_NOT_ALLOWED(detail: string): WebServiceException {
|
|
36
|
+
return this.buildStaticClass(ServiceResponse.METHOD_NOT_ALLOWED(), detail);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public static SERVICE_UNAVAILABLE(detail: string): WebServiceException {
|
|
40
|
+
return this.buildStaticClass(ServiceResponse.SERVICE_UNAVAILABLE(), detail);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public static buildStaticClass(sr: ServiceResponse, detail: string): WebServiceException {
|
|
44
|
+
var result = new WebServiceException(sr.message, sr.code, detail)
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import Commons from '../utils/Commons';
|
|
3
|
+
|
|
4
|
+
export default class CourierService {
|
|
5
|
+
public async postMail(basicAuth: string | null, body: any, url: string, methodName: string, productId: number) {
|
|
6
|
+
const res = await axios.post(url, body, this.buildOptions(basicAuth, true,productId)).catch(function (err) {
|
|
7
|
+
throw Commons.buildWebServiceException(err, 'POST', methodName);
|
|
8
|
+
});
|
|
9
|
+
return res.data;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
private buildOptions(basicAuth: string | null, fullView: boolean, productId: number) {
|
|
13
|
+
if (basicAuth != null) {
|
|
14
|
+
return {
|
|
15
|
+
headers: {
|
|
16
|
+
'Authorization': basicAuth,
|
|
17
|
+
'product-id': productId,
|
|
18
|
+
'view': (fullView) ? 'full' : 'basic'
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
headers: {
|
|
24
|
+
'product-id': productId
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import WebServiceException from '../entities/WebServiceException';
|
|
2
|
+
import ServiceResponse from '../entities/ServiceResponse';
|
|
3
|
+
|
|
4
|
+
export default class Commons {
|
|
5
|
+
/**
|
|
6
|
+
* This method throws an exception when the call service request fail
|
|
7
|
+
* @param err
|
|
8
|
+
* @param verbMethod
|
|
9
|
+
* @param methodName
|
|
10
|
+
*/
|
|
11
|
+
static buildWebServiceException(err: any, verbMethod: string, methodName: string): WebServiceException {
|
|
12
|
+
var ws: any = null
|
|
13
|
+
if (this.validField(err)
|
|
14
|
+
&& this.validField(err.response)
|
|
15
|
+
&& this.validField(err.response.data)
|
|
16
|
+
&& this.validField(err.response.data.code)) {
|
|
17
|
+
ws = new ServiceResponse(err.response.data.code, err.response.data.message)
|
|
18
|
+
ws.detail = err.response.data.detail
|
|
19
|
+
}
|
|
20
|
+
if (ws != null) {
|
|
21
|
+
return WebServiceException.buildStaticClass(ws, this.buildDetailResponse(ws))
|
|
22
|
+
}
|
|
23
|
+
return WebServiceException.SERVICE_UNAVAILABLE('Error in ' + verbMethod + ' ' + methodName + ' service')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Detail builder for service response in throws, used by RequestServiceUtils
|
|
28
|
+
* @param ws
|
|
29
|
+
* @returns
|
|
30
|
+
*/
|
|
31
|
+
static buildDetailResponse(ws: any): string {
|
|
32
|
+
if (Commons.validField(ws) && Commons.validField(ws.message)) {
|
|
33
|
+
return 'API_RESPONSE: ' + ((Commons.validField(ws.detail)) ? ws.detail : ws.message);
|
|
34
|
+
}
|
|
35
|
+
return 'API_RESPONSE: Without message'
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static validField(arg: any) {
|
|
39
|
+
return arg != null && arg != undefined
|
|
40
|
+
}
|
|
41
|
+
}
|