-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
191 lines (166 loc) · 10.5 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2019 Benny Megidish
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { Destinations, Destination } from './src/destinations';
import * as utils from './src/service-utils';
import { OptionsType } from './types/options';
import { ResponseParser } from './src/response-parser';
export const Options: OptionsType = require('./src/options');
/**
* Exposes Israel postal service API
* @author Benny Megidish
*/
export class IPS {
private destinations: Destinations;
constructor() {
this.destinations = new Destinations();
}
/**
* calculate shipping rate for regular (non bulk) shipments
* @param {string} destination destination country name (in CamelCase English)
* @param {float} weight weight of the shipment in grams
* @param {string} shipmentType type of shipment (as defines in {@class Options})
* @param {object} shipmentSubtype subtype of shipment (as define in the shipmentType {@class Options})
* @param {string} serviceOption additional service options (nullable)
* @param {integer} quantity amount of packages
* @returns {Promise<ResponseParser>} a promise with the parsed shipment data (@see {@class ResponseParser})
*/
calculateShippingRate(destination: string, weight: number, shipmentType: string, shipmentSubtype: any, serviceOption=null, quantity=1): Promise<ResponseParser> {
let isLocal: boolean = this.isLocalShipment(destination);
return isLocal ?
this.calculateLocalShippingRate(weight, shipmentType, shipmentSubtype, serviceOption, quantity) :
this.calculateAbroadShippingRate(destination, weight, shipmentType, shipmentSubtype, serviceOption, quantity);
}
/**
* calculate shipping rate for bulk shipments
* @param {string} destination destination country name (in CamelCase English)
* @param {float} weight weight of the shipment in grams
* @param {string} shipmentType type of shipment (as defines in {@class Options})
* @param {object} shipmentSubtype subtype of shipment (as define in the shipmentType {@class Options})
* @param {string} serviceOption additional service options (nullable)
* @param {integer} quantity amount of packages
* @returns {Promise<ResponseParser>} a promise with the parsed shipment data (@see {@class ResponseParser})
*/
calculateBulkShippingRate(destination: string, weight: number, shipmentType: string, shipmentSubtype: any, serviceOption=null, quantity=1): Promise<ResponseParser> {
let isLocal: boolean = this.isLocalShipment(destination);
return isLocal ?
this.calculateLocalBulkShippingRate(weight, shipmentType, shipmentSubtype, serviceOption, quantity) :
this.calculateAbroadBulkShippingRate(destination, weight, shipmentType, shipmentSubtype, serviceOption, quantity);
}
/**
* calculate shipping rate for abroad shipments
* @deprecated [#1] @since version 2.5.0 [#2] use {@link IPS#calculateShippingRate} instead
* @param {string} destination destination country name (in CamelCase English)
* @param {float} weight weight of the shipment in grams
* @param {string} shipmentType type of shipment (as defines in {@class Options})
* @param {object} shipmentSubtype subtype of shipment (as define in the shipmentType {@class Options})
* @param {string} serviceOption additional service options (nullable)
* @param {integer} quantity amount of packages
* @returns {Promise<ResponseParser>} a promise with the parsed shipment data (@see {@class ResponseParser})
*/
calculateAbroadShippingRate(destination: string, weight: number, shipmentType: string, shipmentSubtype: any, serviceOption=null, quantity=1): Promise<ResponseParser> {
let destinationHE: Destination = this.destinations.getDestinationHe(destination, shipmentType);
let type: string = "משלוח דואר לחו\"ל";
let serviceType: string = type + "~" + shipmentType;
return utils.calculateShippingRate(destinationHE, weight, serviceType, shipmentSubtype, serviceOption, quantity);
}
/**
* calculate shipping rate for local shipments (in Israel)
* @deprecated [#1] @since version 2.5.0 [#2] use {@link IPS#calculateShippingRate} instead
* @param {float} weight weight of the shipment in grams
* @param {string} shipmentType type of shipment (as defines in {@class Options})
* @param {object} shipmentSubtype subtype of shipment (as define in the shipmentType {@class Options})
* @param {string} serviceOption additional service options (nullable)
* @param {integer} quantity amount of packages
* @returns {Promise<ResponseParser>} a promise with the parsed shipment data (@see {@class ResponseParser})
*/
calculateLocalShippingRate(weight: number, shipmentType: string, shipmentSubtype: any, serviceOption=null, quantity=1): Promise<ResponseParser> {
let destinationHE: Destination = { id: "0", name: ""};
let type: string = "משלוח דואר בארץ";
let serviceType: string = type + "~" + shipmentType;
return utils.calculateShippingRate(destinationHE, weight, serviceType, shipmentSubtype, serviceOption, quantity);
}
/**
* calculate bulk shipping rate for abroad bulk shipments
* @deprecated [#1] @since version 2.5.0 [#2] use {@link IPS#calculateBulkShippingRate} instead
* @param {string} destination destination country name (in CamelCase English)
* @param {float} weight weight of the shipment in grams
* @param {string} shipmentType type of shipment (as defines in {@class Options})
* @param {object} shipmentSubtype subtype of shipment (as define in the shipmentType {@class Options})
* @param {string} serviceOption additional service options (nullable)
* @param {integer} quantity amount of packages
* @returns {Promise<ResponseParser>} a promise with the parsed shipment data (@see {@class ResponseParser})
*/
calculateAbroadBulkShippingRate(destination: string, weight: number, shipmentType: string, shipmentSubtype: any, serviceOption=null, quantity=1): Promise<ResponseParser> {
let destinationHE: Destination = this.destinations.getDestinationHe(destination, shipmentType);
let type: string = "משלוח דואר כמותי";
let serviceType: string = type + "~" + shipmentType;
return utils.calculateShippingRate(destinationHE, weight, serviceType, shipmentSubtype, serviceOption, quantity);
}
/**
* calculate bulk shipping rate for local bulk shipments (in Israel)
* @deprecated [#1] @since version 2.5.0 [#2] use {@link IPS#calculateBulkShippingRate} instead
* @param {float} weight weight of the shipment in grams
* @param {string} shipmentType type of shipment (as defines in {@class Options})
* @param {object} shipmentSubtype subtype of shipment (as define in the shipmentType {@class Options})
* @param {string} serviceOption additional service options (nullable)
* @param {integer} quantity amount of packages
* @returns {Promise<ResponseParser>} a promise with the parsed shipment data (@see {@class ResponseParser})
*/
calculateLocalBulkShippingRate(weight: number, shipmentType: string, shipmentSubtype: any, serviceOption=null, quantity=1): Promise<ResponseParser> {
let destinationHE: Destination = { id: "0", name: ""};
let type: string = "משלוח דואר כמותי";
let serviceType: string = type + "~" + shipmentType;
return utils.calculateShippingRate(destinationHE, weight, serviceType, shipmentSubtype, serviceOption, quantity);
}
/**
* pre-loads the destinations for the requested shipment method
* by default, the destinations are loaded lazily on-demand
* @param {string} shipmentMethod method of shipment as defined in the {@class Options#ShipmentMethods} class
*/
preloadDestinations(shipmentMethod: string) {
this.destinations.loadDestinationMap(shipmentMethod);
}
/**
* return all the available destinations for the shipment type
* @param {string} shipmentType type of shipment as defined in the {@class Options} class
* @returns {Array<string>} array that contains all the available destination for the shipment type
*/
getAllDestinations(shipmentType: string): Array<string> {
return this.destinations.getAllDestination(shipmentType);
}
/**
* verify if the shipment is eligible for economic shipment
* @param {string} shipmentType type of shipment as defined in the {@class Options} class
* @param {string} destination destination country name (in CamelCase English)
* @returns {boolean} true, if the shipment is eligible for economic shipment, otherwise, false
*/
isEligibleForExpressShipment(shipmentType: string, destination: string): boolean {
const abroadOptions = Options.AbroadMailOptions;
this.destinations._verifyDestinationMapLoaded(abroadOptions.EMS.shipmentType);
return (shipmentType == abroadOptions.EMS.shipmentType || shipmentType == abroadOptions.PARCEL.shipmentType)
&& this.destinations.emsDestinationMap.has(destination);
}
/**
* verify if the shipment is eligible for economic shipment
* @param {string} shipmentType type of shipment as defined in the {@class Options} class
* @param {string} destination destination country name (in CamelCase English)
* @returns {boolean} true, if the shipment is eligible for economic shipment, otherwise, false
*/
isEligibleForEconomicShipment(shipmentType: string, destination: string): boolean {
const abroadOptions = Options.AbroadMailOptions;
this.destinations._verifyDestinationMapLoaded(abroadOptions.ECO.shipmentType);
return (shipmentType == abroadOptions.ECO.shipmentType || shipmentType == abroadOptions.PARCEL.shipmentType)
&& this.destinations.economicDestinationMap.has(destination);
}
private isLocalShipment(destination: string): boolean {
return destination.toLowerCase() === 'israel';
}
}