@shipengine/connect-carrier-api 2.12.18 → 4.0.0-beta

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.
Files changed (249) hide show
  1. package/lib/app/build-app-deployment.d.ts +3 -0
  2. package/lib/app/build-app-deployment.js +41 -0
  3. package/lib/app/build-app-deployment.js.map +1 -0
  4. package/lib/app/create-endpoint-mapping.d.ts +4 -0
  5. package/lib/app/create-endpoint-mapping.js +26 -0
  6. package/lib/app/create-endpoint-mapping.js.map +1 -0
  7. package/lib/app/index.d.ts +4 -2
  8. package/lib/app/index.js +4 -2
  9. package/lib/app/index.js.map +1 -1
  10. package/lib/app/metadata/index.d.ts +2 -0
  11. package/lib/app/metadata/index.js +2 -0
  12. package/lib/app/metadata/index.js.map +1 -1
  13. package/lib/app/{native-rating/data-validator.d.ts → native-rating-data-validator.d.ts} +1 -1
  14. package/lib/app/{native-rating/data-validator.js → native-rating-data-validator.js} +1 -1
  15. package/lib/app/native-rating-data-validator.js.map +1 -0
  16. package/lib/models/addresses/address-base-schema.d.ts +2 -0
  17. package/lib/models/addresses/address-base-schema.js +24 -0
  18. package/lib/models/addresses/address-base-schema.js.map +1 -0
  19. package/lib/models/addresses/address-residential-indicator-schema.d.ts +2 -0
  20. package/lib/models/addresses/address-residential-indicator-schema.js +8 -0
  21. package/lib/models/addresses/address-residential-indicator-schema.js.map +1 -0
  22. package/lib/models/addresses/pudo-location-schema.d.ts +2 -0
  23. package/lib/models/addresses/pudo-location-schema.js +11 -0
  24. package/lib/models/addresses/pudo-location-schema.js.map +1 -0
  25. package/lib/models/addresses/ship-to-schema.d.ts +2 -0
  26. package/lib/models/addresses/ship-to-schema.js +11 -0
  27. package/lib/models/addresses/ship-to-schema.js.map +1 -0
  28. package/lib/models/advanced-options-schema.d.ts +2 -0
  29. package/lib/models/advanced-options-schema.js +15 -0
  30. package/lib/models/advanced-options-schema.js.map +1 -0
  31. package/lib/models/advanced-options.d.ts +0 -5
  32. package/lib/models/advanced-options.js.map +1 -1
  33. package/lib/models/authorization-schema.d.ts +2 -0
  34. package/lib/models/authorization-schema.js +10 -0
  35. package/lib/models/authorization-schema.js.map +1 -0
  36. package/lib/models/billing/index.d.ts +0 -1
  37. package/lib/models/billing/index.js +0 -1
  38. package/lib/models/billing/index.js.map +1 -1
  39. package/lib/models/customs/customs-charges-schema.d.ts +2 -0
  40. package/lib/models/customs/customs-charges-schema.js +13 -0
  41. package/lib/models/customs/customs-charges-schema.js.map +1 -0
  42. package/lib/models/customs/customs-content-types-schema.d.ts +2 -0
  43. package/lib/models/customs/customs-content-types-schema.js +8 -0
  44. package/lib/models/customs/customs-content-types-schema.js.map +1 -0
  45. package/lib/models/customs/customs-item-schema.d.ts +2 -0
  46. package/lib/models/customs/customs-item-schema.js +21 -0
  47. package/lib/models/customs/customs-item-schema.js.map +1 -0
  48. package/lib/models/customs/customs-non-delivery-schema.d.ts +2 -0
  49. package/lib/models/customs/customs-non-delivery-schema.js +8 -0
  50. package/lib/models/customs/customs-non-delivery-schema.js.map +1 -0
  51. package/lib/models/customs/customs-schema.d.ts +2 -0
  52. package/lib/models/customs/customs-schema.js +25 -0
  53. package/lib/models/customs/customs-schema.js.map +1 -0
  54. package/lib/models/customs/customs.d.ts +1 -1
  55. package/lib/models/importer-records/importer-of-record-schema.d.ts +2 -0
  56. package/lib/models/importer-records/importer-of-record-schema.js +20 -0
  57. package/lib/models/importer-records/importer-of-record-schema.js.map +1 -0
  58. package/lib/models/importer-records/{importer_of_record.js → importer-of-record.js} +1 -1
  59. package/lib/models/importer-records/importer-of-record.js.map +1 -0
  60. package/lib/models/inconterms/terms-of-trade-code-schema.d.ts +2 -0
  61. package/lib/models/inconterms/terms-of-trade-code-schema.js +8 -0
  62. package/lib/models/inconterms/terms-of-trade-code-schema.js.map +1 -0
  63. package/lib/models/index.d.ts +0 -3
  64. package/lib/models/index.js +0 -3
  65. package/lib/models/index.js.map +1 -1
  66. package/lib/models/labels/label-message-schema.d.ts +2 -0
  67. package/lib/models/labels/label-message-schema.js +11 -0
  68. package/lib/models/labels/label-message-schema.js.map +1 -0
  69. package/lib/models/pickup/shipped-package-schema.d.ts +2 -0
  70. package/lib/models/pickup/shipped-package-schema.js +25 -0
  71. package/lib/models/pickup/shipped-package-schema.js.map +1 -0
  72. package/lib/models/pickup/shipped-shipment-schema.d.ts +3 -0
  73. package/lib/models/pickup/shipped-shipment-schema.js +21 -0
  74. package/lib/models/pickup/shipped-shipment-schema.js.map +1 -0
  75. package/lib/models/taxes/tax-identifier-schema.d.ts +2 -0
  76. package/lib/models/taxes/tax-identifier-schema.js +13 -0
  77. package/lib/models/taxes/tax-identifier-schema.js.map +1 -0
  78. package/lib/models/taxes/tax-identifier-type-schema.d.ts +2 -0
  79. package/lib/models/taxes/tax-identifier-type-schema.js +8 -0
  80. package/lib/models/taxes/tax-identifier-type-schema.js.map +1 -0
  81. package/lib/models/units/dimension-details-schema.d.ts +2 -0
  82. package/lib/models/units/dimension-details-schema.js +14 -0
  83. package/lib/models/units/dimension-details-schema.js.map +1 -0
  84. package/lib/models/units/dimension-unit-schema.d.ts +2 -0
  85. package/lib/models/units/dimension-unit-schema.js +8 -0
  86. package/lib/models/units/dimension-unit-schema.js.map +1 -0
  87. package/lib/models/units/weight-details-schema.d.ts +2 -0
  88. package/lib/models/units/weight-details-schema.js +13 -0
  89. package/lib/models/units/weight-details-schema.js.map +1 -0
  90. package/lib/models/units/weight-unit-schema.d.ts +2 -0
  91. package/lib/models/units/weight-unit-schema.js +8 -0
  92. package/lib/models/units/weight-unit-schema.js.map +1 -0
  93. package/lib/requests/base-request-schema.d.ts +2 -0
  94. package/lib/requests/base-request-schema.js +12 -0
  95. package/lib/requests/base-request-schema.js.map +1 -0
  96. package/lib/requests/cancel-notification-request-schema.d.ts +2 -0
  97. package/lib/requests/cancel-notification-request-schema.js +12 -0
  98. package/lib/requests/cancel-notification-request-schema.js.map +1 -0
  99. package/lib/requests/create-label-request.d.ts +1 -3
  100. package/lib/requests/create-label-request.js.map +1 -1
  101. package/lib/requests/get-rates-request.d.ts +1 -3
  102. package/lib/requests/get-rates-request.js.map +1 -1
  103. package/package.json +2 -2
  104. package/spec.json +1 -49
  105. package/src/app/build-app-deployment.ts +50 -0
  106. package/src/app/create-endpoint-mapping.ts +25 -0
  107. package/src/app/index.ts +4 -2
  108. package/src/app/metadata/index.ts +2 -0
  109. package/src/app/{native-rating/data-validator.ts → native-rating-data-validator.ts} +1 -1
  110. package/src/models/addresses/address-base-schema.ts +20 -0
  111. package/src/models/addresses/address-residential-indicator-schema.ts +6 -0
  112. package/src/models/addresses/pudo-location-schema.ts +7 -0
  113. package/src/models/addresses/ship-to-schema.ts +7 -0
  114. package/src/models/advanced-options-schema.ts +11 -0
  115. package/src/models/advanced-options.ts +0 -5
  116. package/src/models/authorization-schema.ts +6 -0
  117. package/src/models/billing/index.ts +0 -1
  118. package/src/models/customs/customs-charges-schema.ts +9 -0
  119. package/src/models/customs/customs-content-types-schema.ts +4 -0
  120. package/src/models/customs/customs-item-schema.ts +17 -0
  121. package/src/models/customs/customs-non-delivery-schema.ts +4 -0
  122. package/src/models/customs/customs-schema.ts +21 -0
  123. package/src/models/customs/customs.ts +1 -1
  124. package/src/models/importer-records/importer-of-record-schema.ts +16 -0
  125. package/src/models/inconterms/terms-of-trade-code-schema.ts +4 -0
  126. package/src/models/index.ts +0 -3
  127. package/src/models/labels/label-message-schema.ts +7 -0
  128. package/src/models/pickup/shipped-package-schema.ts +21 -0
  129. package/src/models/pickup/shipped-shipment-schema.ts +18 -0
  130. package/src/models/taxes/tax-identifier-schema.ts +9 -0
  131. package/src/models/taxes/tax-identifier-type-schema.ts +4 -0
  132. package/src/models/units/dimension-details-schema.ts +10 -0
  133. package/src/models/units/dimension-unit-schema.ts +4 -0
  134. package/src/models/units/weight-details-schema.ts +9 -0
  135. package/src/models/units/weight-unit-schema.ts +4 -0
  136. package/src/requests/base-request-schema.ts +8 -0
  137. package/src/requests/cancel-notification-request-schema.ts +8 -0
  138. package/src/requests/create-label-request.ts +0 -3
  139. package/src/requests/get-rates-request.ts +0 -3
  140. package/tsconfig.tsbuildinfo +1 -1
  141. package/lib/app/build-native-rating-routes.d.ts +0 -3
  142. package/lib/app/build-native-rating-routes.js +0 -105
  143. package/lib/app/build-native-rating-routes.js.map +0 -1
  144. package/lib/app/carrier-app.d.ts +0 -15
  145. package/lib/app/carrier-app.js +0 -166
  146. package/lib/app/carrier-app.js.map +0 -1
  147. package/lib/app/create-get-rates-handler.d.ts +0 -4
  148. package/lib/app/create-get-rates-handler.js +0 -100
  149. package/lib/app/create-get-rates-handler.js.map +0 -1
  150. package/lib/app/internal/branded-image.d.ts +0 -9
  151. package/lib/app/internal/branded-image.js +0 -3
  152. package/lib/app/internal/branded-image.js.map +0 -1
  153. package/lib/app/internal/carrier-app-specification.d.ts +0 -16
  154. package/lib/app/internal/carrier-app-specification.js +0 -3
  155. package/lib/app/internal/carrier-app-specification.js.map +0 -1
  156. package/lib/app/internal/carrier-specification.d.ts +0 -38
  157. package/lib/app/internal/carrier-specification.js +0 -86
  158. package/lib/app/internal/carrier-specification.js.map +0 -1
  159. package/lib/app/internal/connect-runtime-app.d.ts +0 -14
  160. package/lib/app/internal/connect-runtime-app.js +0 -3
  161. package/lib/app/internal/connect-runtime-app.js.map +0 -1
  162. package/lib/app/internal/document-template.d.ts +0 -7
  163. package/lib/app/internal/document-template.js +0 -3
  164. package/lib/app/internal/document-template.js.map +0 -1
  165. package/lib/app/internal/index.d.ts +0 -3
  166. package/lib/app/internal/index.js +0 -7
  167. package/lib/app/internal/index.js.map +0 -1
  168. package/lib/app/internal/metadata.d.ts +0 -13
  169. package/lib/app/internal/metadata.js +0 -64
  170. package/lib/app/internal/metadata.js.map +0 -1
  171. package/lib/app/internal/native-rating-specification.d.ts +0 -5
  172. package/lib/app/internal/native-rating-specification.js +0 -3
  173. package/lib/app/internal/native-rating-specification.js.map +0 -1
  174. package/lib/app/internal/route.d.ts +0 -27
  175. package/lib/app/internal/route.js +0 -12
  176. package/lib/app/internal/route.js.map +0 -1
  177. package/lib/app/native-rating/base-rate-context.d.ts +0 -3
  178. package/lib/app/native-rating/base-rate-context.js +0 -83
  179. package/lib/app/native-rating/base-rate-context.js.map +0 -1
  180. package/lib/app/native-rating/context-results.d.ts +0 -3
  181. package/lib/app/native-rating/context-results.js +0 -3
  182. package/lib/app/native-rating/context-results.js.map +0 -1
  183. package/lib/app/native-rating/create-dynamic-carrier.d.ts +0 -7
  184. package/lib/app/native-rating/create-dynamic-carrier.js +0 -63
  185. package/lib/app/native-rating/create-dynamic-carrier.js.map +0 -1
  186. package/lib/app/native-rating/data-validator.js.map +0 -1
  187. package/lib/app/native-rating/get-rates.d.ts +0 -7
  188. package/lib/app/native-rating/get-rates.js +0 -3
  189. package/lib/app/native-rating/get-rates.js.map +0 -1
  190. package/lib/app/native-rating/get-variables.d.ts +0 -6
  191. package/lib/app/native-rating/get-variables.js +0 -3
  192. package/lib/app/native-rating/get-variables.js.map +0 -1
  193. package/lib/app/native-rating/get-zone.d.ts +0 -6
  194. package/lib/app/native-rating/get-zone.js +0 -3
  195. package/lib/app/native-rating/get-zone.js.map +0 -1
  196. package/lib/app/native-rating/implementation-type.d.ts +0 -31
  197. package/lib/app/native-rating/implementation-type.js +0 -8
  198. package/lib/app/native-rating/implementation-type.js.map +0 -1
  199. package/lib/app/native-rating/index.d.ts +0 -3
  200. package/lib/app/native-rating/index.js +0 -7
  201. package/lib/app/native-rating/index.js.map +0 -1
  202. package/lib/app/native-rating/numeric-currency.d.ts +0 -5
  203. package/lib/app/native-rating/numeric-currency.js +0 -3
  204. package/lib/app/native-rating/numeric-currency.js.map +0 -1
  205. package/lib/app/native-rating/rating-context.d.ts +0 -15
  206. package/lib/app/native-rating/rating-context.js +0 -3
  207. package/lib/app/native-rating/rating-context.js.map +0 -1
  208. package/lib/models/bill-to.d.ts +0 -14
  209. package/lib/models/bill-to.js +0 -14
  210. package/lib/models/bill-to.js.map +0 -1
  211. package/lib/models/billing/billing-to-party.d.ts +0 -10
  212. package/lib/models/billing/billing-to-party.js +0 -15
  213. package/lib/models/billing/billing-to-party.js.map +0 -1
  214. package/lib/models/cod-payment-type.d.ts +0 -9
  215. package/lib/models/cod-payment-type.js +0 -14
  216. package/lib/models/cod-payment-type.js.map +0 -1
  217. package/lib/models/collect-on-delivery.d.ts +0 -9
  218. package/lib/models/collect-on-delivery.js +0 -8
  219. package/lib/models/collect-on-delivery.js.map +0 -1
  220. package/lib/models/importer-records/importer_of_record.js.map +0 -1
  221. package/src/app/build-native-rating-routes.ts +0 -119
  222. package/src/app/carrier-app.ts +0 -222
  223. package/src/app/create-get-rates-handler.ts +0 -128
  224. package/src/app/internal/README.md +0 -1
  225. package/src/app/internal/branded-image.ts +0 -9
  226. package/src/app/internal/carrier-app-specification.ts +0 -17
  227. package/src/app/internal/carrier-specification.ts +0 -135
  228. package/src/app/internal/connect-runtime-app.ts +0 -15
  229. package/src/app/internal/document-template.ts +0 -7
  230. package/src/app/internal/index.ts +0 -3
  231. package/src/app/internal/metadata.ts +0 -86
  232. package/src/app/internal/native-rating-specification.ts +0 -5
  233. package/src/app/internal/route.ts +0 -31
  234. package/src/app/native-rating/base-rate-context.ts +0 -101
  235. package/src/app/native-rating/context-results.ts +0 -3
  236. package/src/app/native-rating/create-dynamic-carrier.ts +0 -82
  237. package/src/app/native-rating/get-rates.ts +0 -9
  238. package/src/app/native-rating/get-variables.ts +0 -8
  239. package/src/app/native-rating/get-zone.ts +0 -9
  240. package/src/app/native-rating/implementation-type.ts +0 -34
  241. package/src/app/native-rating/index.ts +0 -3
  242. package/src/app/native-rating/numeric-currency.ts +0 -5
  243. package/src/app/native-rating/rating-context.ts +0 -19
  244. package/src/models/bill-to.ts +0 -15
  245. package/src/models/billing/billing-to-party.ts +0 -10
  246. package/src/models/cod-payment-type.ts +0 -9
  247. package/src/models/collect-on-delivery.ts +0 -10
  248. /package/lib/models/importer-records/{importer_of_record.d.ts → importer-of-record.d.ts} +0 -0
  249. /package/src/models/importer-records/{importer_of_record.ts → importer-of-record.ts} +0 -0
@@ -1,3 +0,0 @@
1
- export * from './connect-runtime-app';
2
- export * from './route';
3
- export * from './branded-image';
@@ -1,86 +0,0 @@
1
- import { CarrierAppDefinition } from '../carrier-app-definition';
2
- import { ShippingProviderConnector } from '../metadata/shipping-provider-connector';
3
- import { FunctionSpecification } from '../metadata/function';
4
-
5
- import { CarrierAppSpecification } from './carrier-app-specification';
6
- import { CarrierSpecification } from './carrier-specification';
7
- import { AuthSpecification } from '../metadata/auth-specification';
8
-
9
- const fn = (name: string): FunctionSpecification => ({
10
- Name: name,
11
- IsSandboxed: false,
12
- });
13
-
14
- const mapFunctions = (app: CarrierAppDefinition): FunctionSpecification[] => {
15
- const fns: FunctionSpecification[] = [];
16
-
17
- if (app.Register) {
18
- fns.push(fn('Register'));
19
- }
20
-
21
- if (app.CreateLabel) {
22
- fns.push(fn('CreateLabel'));
23
- }
24
-
25
- if (app.VoidLabels) {
26
- fns.push(fn('VoidLabels'));
27
- }
28
-
29
- if (app.CreateManifest) {
30
- fns.push(fn('CreateManifest'));
31
- }
32
-
33
- if (app.GetManifest) {
34
- fns.push(fn('GetManifest'));
35
- }
36
-
37
- if (app.SchedulePickup) {
38
- fns.push(fn('SchedulePickup'));
39
- }
40
-
41
- if (app.CancelPickup) {
42
- fns.push(fn('CancelPickup'));
43
- }
44
-
45
- if (app.GetRates) {
46
- fns.push(fn('GetRates'));
47
- }
48
-
49
- if (app.Track) {
50
- fns.push(fn('Track'));
51
- }
52
-
53
- if (app.ImportTrackingEvents) {
54
- fns.push(fn('ImportTrackingEvents'));
55
- }
56
-
57
- return fns;
58
- };
59
-
60
- export class Metadata implements CarrierAppSpecification {
61
- Id: string;
62
- Name: string;
63
- Connector: ShippingProviderConnector;
64
- AuthProcess?: AuthSpecification;
65
- Carriers: CarrierSpecification[];
66
-
67
- constructor(app: CarrierAppDefinition) {
68
- this.Id = app.Metadata.Id;
69
- this.Name = app.Metadata.Name;
70
- this.AuthProcess = app.Metadata.AuthProcess;
71
- if (this.AuthProcess) {
72
- this.AuthProcess.Identifier.Version = '2.0';
73
- }
74
- this.Carriers = app.Metadata.Carriers.map((c) => new CarrierSpecification(c));
75
-
76
- this.Connector = {
77
- DiagnosticRoutes: {
78
- Liveness: '/diagnostics/liveness',
79
- Readiness: '/diagnostics/readiness',
80
- Version: '/diagnostics/version',
81
- },
82
- ApiVersion: '2.0.0',
83
- Functions: mapFunctions(app),
84
- };
85
- }
86
- }
@@ -1,5 +0,0 @@
1
- /** Specification for a Native Rating implementation */
2
- export interface NativeRatingSpecification {
3
- /** External id of the default rate card */
4
- DefaultRateCard: string;
5
- }
@@ -1,31 +0,0 @@
1
- import { Request } from 'express';
2
-
3
- export enum Method {
4
- POST = 'post',
5
- GET = 'get',
6
- PUT = 'put',
7
- PATCH = 'patch',
8
- DELETE = 'delete',
9
- }
10
-
11
- /** @description The definition for a route that will serve up the contents of a file */
12
- export interface FileRoute {
13
- /** @description The file that should be sent to the client. If this is set, handler will not be used. */
14
- file: string | ((req: Request) => string | Promise<string>);
15
- }
16
-
17
- /** @description The definition for a route and how it will be handled */
18
- export interface HandlerRoute {
19
- /** @description The method that will be called to handle this request */
20
- handler?: (req: any) => any | Promise<any> | AsyncGenerator<any>;
21
- /** @description The optional method used to validate the response for this route. Used for warning purposes. */
22
- validateResponse?: (response: any) => string[] | undefined;
23
- }
24
-
25
- /** @description The definition for all types of routes */
26
- export type Route = {
27
- /** @description The path to the endpoint you wish to expose @example "/CreateLabel", "/sales_orders_export" */
28
- path: string;
29
- /** @description The http verb used for this endpoint @example "GET", "POST" */
30
- method: Method;
31
- } & (FileRoute | HandlerRoute);
@@ -1,101 +0,0 @@
1
- import { resolve } from 'path';
2
- import { readFileSync } from 'fs';
3
- import winston, { format, transports } from 'winston';
4
- import { NumericCurrency } from './numeric-currency';
5
- import { RatingContext } from './rating-context';
6
- import { RateCard } from '../metadata/rate-card';
7
-
8
- interface DataLookup {
9
- rates: (keys: string[]) => Promise<{ [key: string]: NumericCurrency }>;
10
- variables: (keys: string[]) => Promise<{ [key: string]: any }>;
11
- }
12
-
13
- type ZoneLookup = (keys: string[]) => Promise<{ [key: string]: any }>;
14
-
15
- /** Predicate that returns true if the value is defined, false if not */
16
- const valueIsUndefined = <T>([key, value]: [string, T | undefined]): boolean => {
17
- const isIncluded = value !== undefined;
18
-
19
- console.log(`${isIncluded ? '✔️' : '❌'} ${key}`);
20
-
21
- return isIncluded;
22
- };
23
-
24
- /** Build a lookup function to find values for a list of keys */
25
- const buildLookup =
26
- <TIn, TOut>(name: string, items: { key: string; value: TIn }[], mapper?: (item: TIn) => TOut) =>
27
- (keys: string[]): Promise<{ [dataKey: string]: TOut }> => {
28
- console.log(`Getting ${name} keys (${keys.length} total):`);
29
- console.group();
30
- try {
31
- const things = keys.map((key) => {
32
- const foundValue = items.find((item) => key === item.key)?.value;
33
- const mappedValue = foundValue && mapper ? mapper(foundValue) : foundValue;
34
- return [key, mappedValue] as [string, TOut | undefined];
35
- });
36
-
37
- const values = Object.fromEntries(things.filter(valueIsUndefined)) as {
38
- [key: string]: TOut;
39
- };
40
- return Promise.resolve(values);
41
- } finally {
42
- console.groupEnd();
43
- }
44
- };
45
-
46
- const loadData = (basePath: string) => (dataType: string) => {
47
- try {
48
- const data = readFileSync(resolve(basePath, `${dataType}.json`));
49
- const json = JSON.parse(data.toString());
50
- return [dataType, json?.[dataType] || []];
51
- } catch (err) {
52
- return [dataType, []];
53
- }
54
- };
55
-
56
- export const createContextBuilder = (basePath: string) => {
57
- let zoneLookup: ZoneLookup;
58
- const getZoneLookup = () => {
59
- if (!zoneLookup) {
60
- const zones = loadData(basePath)('zones')[1];
61
- zoneLookup = buildLookup('zone', zones);
62
- }
63
- return zoneLookup;
64
- };
65
-
66
- const log = winston.createLogger({
67
- transports: [
68
- new transports.Console({
69
- level: 'debug',
70
- format: format.combine(format.colorize(), format.timestamp(), format.metadata()),
71
- }),
72
- ],
73
- });
74
-
75
- return (rateCard: RateCard): RatingContext => {
76
- let dataLookup: DataLookup;
77
- const getDataLookup = () => {
78
- if (!dataLookup) {
79
- const loadedData = Object.fromEntries(
80
- ['rates', 'variables'].map(loadData(resolve(basePath, rateCard.Id))),
81
- );
82
-
83
- dataLookup = {
84
- rates: buildLookup('rate', loadedData.rates, (x: any) => ({
85
- amount: +x,
86
- currency: rateCard.Currency,
87
- })),
88
- variables: buildLookup('variables', loadedData.variables),
89
- };
90
- }
91
- return dataLookup;
92
- };
93
-
94
- return {
95
- getRates: (keys: string[]) => getDataLookup().rates(keys),
96
- getVariables: (keys: string[]) => getDataLookup().variables(keys),
97
- getZone: (keys: string[]) => getZoneLookup()(keys),
98
- log,
99
- };
100
- };
101
- };
@@ -1,3 +0,0 @@
1
- export interface ContextResults<T> {
2
- [dataKey: string]: T;
3
- }
@@ -1,82 +0,0 @@
1
- import { VM, VMScript } from 'vm2';
2
- import { RateResultsAndId, RatingCarrier, ShipmentAndId } from './implementation-type';
3
- import { RatingContext } from './rating-context';
4
-
5
- /** Results of the carrier code validation **/
6
- interface CodeValidationResults {
7
- errors?: string[];
8
- /** Does the carrier code contain the get zones function */
9
- hasGetZonesFunction: boolean;
10
- }
11
-
12
- /** Ensure that the carrier module has the correct shape
13
- * @param module Object into which code should be exported
14
- */
15
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
- const validateCode = (module?: any): CodeValidationResults => {
17
- if (module?.exports?.default === undefined) {
18
- return {
19
- errors: ['Code must have a default export'],
20
- hasGetZonesFunction: false,
21
- };
22
- }
23
-
24
- const errors = [];
25
- const { rateShipments, getZones } = module.exports.default;
26
- const hasGetZonesFunction = getZones !== undefined;
27
-
28
- if (typeof rateShipments !== 'function') {
29
- errors.push("Code must contain a function named 'rateShipments'");
30
- } else if (rateShipments.length !== 2) {
31
- errors.push("'rateShipments' function must have two parameters");
32
- }
33
-
34
- return {
35
- errors,
36
- hasGetZonesFunction,
37
- };
38
- };
39
-
40
- /** Create a dynamic carrier
41
- * @param code Code that should be used for the carrier
42
- * @returns Carrier with code ready to be executed
43
- */
44
- const createDynamicCarrier = (code: string): RatingCarrier => {
45
- // This is to make sure that the code we need to run doesn't get excluded because of dangling comments, etc.
46
- const resetCode = code + ';\n /* */ \n';
47
-
48
- const validationResults = new VM({
49
- sandbox: { module: {}, validateCode },
50
- }).run(`${resetCode}; validateCode(module);`);
51
- if (validationResults.errors?.length > 0) {
52
- throw new Error(validationResults.errors.join('; '));
53
- }
54
-
55
- const rateShipmentsScript = new VMScript(
56
- `${resetCode} module.exports.default.rateShipments(context, shipment);`,
57
- );
58
-
59
- const executionVM = new VM({
60
- sandbox: {
61
- module: {},
62
- context: {},
63
- shipment: {},
64
- zoneContext: {},
65
- origin: {},
66
- destination: {},
67
- },
68
- });
69
-
70
- const carrier: RatingCarrier = {
71
- rateShipments: (
72
- context: RatingContext,
73
- shipment: ShipmentAndId[],
74
- ): Promise<RateResultsAndId[]> => {
75
- return executionVM.setGlobals({ context, shipment }).run(rateShipmentsScript);
76
- },
77
- };
78
-
79
- return carrier;
80
- };
81
-
82
- export default createDynamicCarrier;
@@ -1,9 +0,0 @@
1
- import { ContextResults } from './context-results';
2
- import { NumericCurrency } from './numeric-currency';
3
-
4
- export type GetRatesResults = ContextResults<NumericCurrency>;
5
-
6
- /** Function to get rates from the context */
7
- export interface GetRates {
8
- (dataKeys: string[]): Promise<GetRatesResults>;
9
- }
@@ -1,8 +0,0 @@
1
- import { ContextResults } from './context-results';
2
-
3
- export type GetVariableResults = ContextResults<any>;
4
-
5
- /** Function to get rating variables from the context */
6
- export interface GetVariables {
7
- (dataKeys: string[]): Promise<GetVariableResults>;
8
- }
@@ -1,9 +0,0 @@
1
- import { ContextResults } from './context-results';
2
-
3
- export type GetZoneResults = ContextResults<any>;
4
-
5
- /** Function to get zone data from the context */
6
- export interface GetZone {
7
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
- (dataKeys: string[]): Promise<GetZoneResults>;
9
- }
@@ -1,34 +0,0 @@
1
- import { Rate } from '../../models/rates';
2
- import { GetRatesRequest } from '../../requests/get-rates-request';
3
- import { RatingContext } from './rating-context';
4
-
5
- /** A rate request shipment with its corresponding id */
6
- export interface ShipmentAndId {
7
- /** rate_request_identifier for the rate request */
8
- id: string;
9
- /** Shipment for the rate request */
10
- shipment: GetRatesRequest;
11
- }
12
-
13
- /** Rate results with its corresponding rate request id */
14
- export interface RateResultsAndId {
15
- /** rate_request_identifier for the corresponding rate request */
16
- id: string;
17
- /** Error, if any */
18
- error?: unknown;
19
- /** Rates for a given rate request */
20
- rates: Rate[];
21
- }
22
-
23
- /** Implementation of a carrier */
24
- export interface RatingCarrier {
25
- /** Rate shipments
26
- * @param context Native Rating context that can be used by the implementation to interact with the underlying service
27
- * @param shipment Shipments that should be rated
28
- * @returns List of rates for the given shipments
29
- */
30
- rateShipments: (context: RatingContext, shipment: ShipmentAndId[]) => Promise<RateResultsAndId[]>;
31
- }
32
-
33
- /** Signify a validation error from the carrier */
34
- export class CarrierValidationError extends Error {}
@@ -1,3 +0,0 @@
1
- export * from './implementation-type';
2
- export * from './numeric-currency';
3
- export * from './rating-context';
@@ -1,5 +0,0 @@
1
- /** NumericCurrency so that we don't have to convert from the string Currency all over the place */
2
- export interface NumericCurrency {
3
- currency: string;
4
- amount: number | undefined;
5
- }
@@ -1,19 +0,0 @@
1
- import { Logger } from 'winston';
2
- import { GetVariables } from './get-variables';
3
- import { GetRates } from './get-rates';
4
- import { GetZone } from './get-zone';
5
-
6
- /** Rates request context */
7
- export interface RatingContext {
8
- /** Function to get rates for given keys */
9
- getRates: GetRates;
10
-
11
- /** Function to get variables for given keys */
12
- getVariables: GetVariables;
13
-
14
- /** Function to get zone for given keys */
15
- getZone: GetZone;
16
-
17
- /** Logger that implementers can use */
18
- log: Logger;
19
- }
@@ -1,15 +0,0 @@
1
- import { AddressBase } from './addresses';
2
- import { BillToParty } from './billing';
3
-
4
- /**
5
- * @description Bill to is a generic object used for billing a part of the shipment, duties, taxes, or otherwise to a party
6
- other than the shipper (the shipper is considered the default party responsible). Ref: BillTo extends AddressBase.
7
- since address fields are often required when associating billing to
8
- another account. Additionally, this allows for a contact to be specified, if necessary.
9
- */
10
- export class BillTo extends AddressBase {
11
- /** @description The bill_to_party mentions the type of billing option*/
12
- bill_to_party?: BillToParty;
13
- /** @description Specified if there is an account number associated with the Bill To Party, and the Bill To Party is not the account holder. */
14
- account_number?: string;
15
- }
@@ -1,10 +0,0 @@
1
- /** @description The type of party responsible for a given category of charges associated with a shipment, such as
2
- * shipping charges or import duties. */
3
- export enum BillToParty {
4
- /** @description The shipper is generally regarded as the party responsible for the billing. */
5
- Shipper = 'shipper',
6
- /** @description The recipient may be responsible for the billing of some entities, that could include the shipping cost. */
7
- Recipient = 'recipient',
8
- /** @description A shipper may want to bill to a third-party; especially common for 3PLs and larger volume. merchants */
9
- ThirdPartyAccount = 'third_party_account',
10
- }
@@ -1,9 +0,0 @@
1
- /** @description Specifies which payment method is permitted when Collect on Delivery (COD) is requested on a shipment */
2
- export enum CodPaymentType {
3
- /** @description Any payment method is permitted */
4
- Any = 'any',
5
- /** @description Only cash is permitted */
6
- Cash = 'cash',
7
- /** @description Only guaranteed funds are permitted, such as a money order or cashier's check */
8
- GuaranteedFunds = 'guaranteed_funds',
9
- }
@@ -1,10 +0,0 @@
1
- import { CodPaymentType } from './cod-payment-type';
2
- import { Currency } from './currency';
3
-
4
- /** @description This model represents the option to defer payment for a shipment, otherwise known as Collect on Delivery (COD) */
5
- export class CollectOnDelivery {
6
- /** @description The payment amount for Collect on Delivery (COD) */
7
- payment_amount!: Currency;
8
- /** @description The payment method permitted for Collect on Delivery (COD) */
9
- payment_type?: CodPaymentType;
10
- }