@deenruv/inpost 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.
Files changed (141) hide show
  1. package/LICENSE +23 -0
  2. package/dist/api/organizations/index.d.ts +18 -0
  3. package/dist/api/organizations/index.js +34 -0
  4. package/dist/api/organizations/shipments.d.ts +17 -0
  5. package/dist/api/organizations/shipments.js +33 -0
  6. package/dist/api/organizations.d.ts +18 -0
  7. package/dist/api/organizations.js +33 -0
  8. package/dist/api/shipments/index.d.ts +14 -0
  9. package/dist/api/shipments/index.js +26 -0
  10. package/dist/api/shipments.d.ts +19 -0
  11. package/dist/api/shipments.js +37 -0
  12. package/dist/error.d.ts +6 -0
  13. package/dist/error.js +14 -0
  14. package/dist/index.d.ts +16 -0
  15. package/dist/index.js +43 -0
  16. package/dist/index.test.d.ts +1 -0
  17. package/dist/index.test.js +99 -0
  18. package/dist/middleware/auth.d.ts +2 -0
  19. package/dist/middleware/auth.js +12 -0
  20. package/dist/middleware/endpoint.d.ts +2 -0
  21. package/dist/middleware/endpoint.js +10 -0
  22. package/dist/middleware/expect_200.d.ts +2 -0
  23. package/dist/middleware/expect_200.js +26 -0
  24. package/dist/middleware/get.d.ts +2 -0
  25. package/dist/middleware/get.js +8 -0
  26. package/dist/middleware/index.d.ts +9 -0
  27. package/dist/middleware/index.js +25 -0
  28. package/dist/middleware/json_request.d.ts +2 -0
  29. package/dist/middleware/json_request.js +14 -0
  30. package/dist/middleware/json_response.d.ts +5 -0
  31. package/dist/middleware/json_response.js +14 -0
  32. package/dist/middleware/middleware.d.ts +4 -0
  33. package/dist/middleware/middleware.js +2 -0
  34. package/dist/middleware/post.d.ts +2 -0
  35. package/dist/middleware/post.js +8 -0
  36. package/dist/middleware/values.d.ts +4 -0
  37. package/dist/middleware/values.js +14 -0
  38. package/dist/models/address.d.ts +13 -0
  39. package/dist/models/address.js +2 -0
  40. package/dist/models/carrier.d.ts +1 -0
  41. package/dist/models/carrier.js +2 -0
  42. package/dist/models/country_code.d.ts +4 -0
  43. package/dist/models/country_code.js +2 -0
  44. package/dist/models/currency_code.d.ts +3 -0
  45. package/dist/models/currency_code.js +2 -0
  46. package/dist/models/index.d.ts +11 -0
  47. package/dist/models/index.js +27 -0
  48. package/dist/models/money.d.ts +5 -0
  49. package/dist/models/money.js +2 -0
  50. package/dist/models/offer.d.ts +4 -0
  51. package/dist/models/offer.js +2 -0
  52. package/dist/models/organization.d.ts +19 -0
  53. package/dist/models/organization.js +2 -0
  54. package/dist/models/parcel.d.ts +69 -0
  55. package/dist/models/parcel.js +2 -0
  56. package/dist/models/person.d.ts +10 -0
  57. package/dist/models/person.js +2 -0
  58. package/dist/models/service.d.ts +1 -0
  59. package/dist/models/service.js +2 -0
  60. package/dist/models/shipment.d.ts +32 -0
  61. package/dist/models/shipment.js +2 -0
  62. package/dist/models/transaction.d.ts +7 -0
  63. package/dist/models/transaction.js +2 -0
  64. package/dist/validators/address.d.ts +2 -0
  65. package/dist/validators/address.js +18 -0
  66. package/dist/validators/array.d.ts +1 -0
  67. package/dist/validators/array.js +5 -0
  68. package/dist/validators/carrier.d.ts +2 -0
  69. package/dist/validators/carrier.js +13 -0
  70. package/dist/validators/is_country_code.d.ts +2 -0
  71. package/dist/validators/is_country_code.js +13 -0
  72. package/dist/validators/is_currency_code.d.ts +2 -0
  73. package/dist/validators/is_currency_code.js +12 -0
  74. package/dist/validators/is_money.d.ts +2 -0
  75. package/dist/validators/is_money.js +8 -0
  76. package/dist/validators/is_parcel.d.ts +2 -0
  77. package/dist/validators/is_parcel.js +8 -0
  78. package/dist/validators/is_shipment.d.ts +2 -0
  79. package/dist/validators/is_shipment.js +7 -0
  80. package/dist/validators/number.d.ts +1 -0
  81. package/dist/validators/number.js +5 -0
  82. package/dist/validators/object.d.ts +1 -0
  83. package/dist/validators/object.js +5 -0
  84. package/dist/validators/optional.d.ts +1 -0
  85. package/dist/validators/optional.js +5 -0
  86. package/dist/validators/organization.d.ts +2 -0
  87. package/dist/validators/organization.js +28 -0
  88. package/dist/validators/person.d.ts +2 -0
  89. package/dist/validators/person.js +17 -0
  90. package/dist/validators/services.d.ts +2 -0
  91. package/dist/validators/services.js +24 -0
  92. package/dist/validators/string.d.ts +1 -0
  93. package/dist/validators/string.js +5 -0
  94. package/package.json +30 -0
  95. package/src/api/organizations/index.ts +51 -0
  96. package/src/api/organizations/shipments.ts +53 -0
  97. package/src/api/organizations.ts +44 -0
  98. package/src/api/shipments/index.ts +44 -0
  99. package/src/api/shipments.ts +59 -0
  100. package/src/error.ts +10 -0
  101. package/src/index.test.ts +127 -0
  102. package/src/index.ts +33 -0
  103. package/src/middleware/auth.ts +12 -0
  104. package/src/middleware/endpoint.ts +12 -0
  105. package/src/middleware/expect_200.ts +28 -0
  106. package/src/middleware/get.ts +6 -0
  107. package/src/middleware/index.ts +9 -0
  108. package/src/middleware/json_request.ts +15 -0
  109. package/src/middleware/json_response.ts +18 -0
  110. package/src/middleware/middleware.ts +7 -0
  111. package/src/middleware/post.ts +7 -0
  112. package/src/middleware/values.ts +23 -0
  113. package/src/models/address.ts +16 -0
  114. package/src/models/carrier.ts +1 -0
  115. package/src/models/country_code.ts +4 -0
  116. package/src/models/currency_code.ts +3 -0
  117. package/src/models/index.ts +11 -0
  118. package/src/models/money.ts +5 -0
  119. package/src/models/offer.ts +4 -0
  120. package/src/models/organization.ts +19 -0
  121. package/src/models/parcel.ts +75 -0
  122. package/src/models/person.ts +11 -0
  123. package/src/models/service.ts +15 -0
  124. package/src/models/shipment.ts +39 -0
  125. package/src/models/transaction.ts +7 -0
  126. package/src/validators/address.ts +19 -0
  127. package/src/validators/array.ts +4 -0
  128. package/src/validators/carrier.ts +13 -0
  129. package/src/validators/is_country_code.ts +11 -0
  130. package/src/validators/is_currency_code.ts +10 -0
  131. package/src/validators/is_money.ts +8 -0
  132. package/src/validators/is_parcel.ts +11 -0
  133. package/src/validators/is_shipment.ts +5 -0
  134. package/src/validators/number.ts +1 -0
  135. package/src/validators/object.ts +2 -0
  136. package/src/validators/optional.ts +4 -0
  137. package/src/validators/organization.ts +29 -0
  138. package/src/validators/person.ts +18 -0
  139. package/src/validators/services.ts +24 -0
  140. package/src/validators/string.ts +1 -0
  141. package/tsconfig.json +13 -0
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ # License 1
2
+
3
+ The MIT License
4
+
5
+ Copyright (c) 2025-present Aexol
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
+
9
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
+
13
+ # License 2
14
+
15
+ The MIT License
16
+
17
+ Copyright (c) 2018-2025 Michael Bromley
18
+
19
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
20
+
21
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
22
+
23
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ import { FetchFn } from "../../middleware/index.js";
2
+ import { Shipment } from "../../models/index.js";
3
+ import { Shipments } from "./shipments.js";
4
+ export interface ShipmentListResponse {
5
+ href: string;
6
+ count: number;
7
+ page: number;
8
+ per_page: number;
9
+ items: Shipment[];
10
+ }
11
+ export declare const isShipmentList: (v: unknown) => v is ShipmentListResponse;
12
+ export declare class Organization {
13
+ private apiBase;
14
+ private fetchFn;
15
+ constructor(apiBase: URL, fetchFn: FetchFn);
16
+ fetch(): Promise<import("../../models/organization.js").Organization>;
17
+ shipments(): Shipments;
18
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Organization = exports.isShipmentList = void 0;
4
+ const index_js_1 = require("../../middleware/index.js");
5
+ const array_js_1 = require("../../validators/array.js");
6
+ const number_js_1 = require("../../validators/number.js");
7
+ const object_js_1 = require("../../validators/object.js");
8
+ const organization_js_1 = require("../../validators/organization.js");
9
+ const is_shipment_js_1 = require("../../validators/is_shipment.js");
10
+ const string_js_1 = require("../../validators/string.js");
11
+ const shipments_js_1 = require("./shipments.js");
12
+ const mustShipmentList = (v) => (0, string_js_1.isString)(v.href) &&
13
+ (0, number_js_1.isNumber)(v.count) &&
14
+ (0, number_js_1.isNumber)(v.page) &&
15
+ (0, number_js_1.isNumber)(v.per_page) &&
16
+ (0, array_js_1.isArray)(is_shipment_js_1.isShipment)(v.items);
17
+ const isShipmentList = (v) => (0, object_js_1.isObject)(v) && mustShipmentList(v);
18
+ exports.isShipmentList = isShipmentList;
19
+ class Organization {
20
+ apiBase;
21
+ fetchFn;
22
+ constructor(apiBase, fetchFn) {
23
+ this.apiBase = apiBase;
24
+ this.fetchFn = fetchFn;
25
+ }
26
+ async fetch() {
27
+ const res = await (0, index_js_1.jsonResponse)(organization_js_1.isOrganization)((0, index_js_1.get)((0, index_js_1.is2xx)(this.fetchFn)))(this.apiBase);
28
+ return res.jsonResponse;
29
+ }
30
+ shipments() {
31
+ return new shipments_js_1.Shipments(this.apiBase, (0, index_js_1.endpoint)("shipments")(this.fetchFn));
32
+ }
33
+ }
34
+ exports.Organization = Organization;
@@ -0,0 +1,17 @@
1
+ import { FetchFn } from '../../middleware/index.js';
2
+ import { Shipment } from '../../models/index.js';
3
+ export interface ShipmentListResponse {
4
+ href: string;
5
+ count: number;
6
+ page: number;
7
+ per_page: number;
8
+ items: Shipment[];
9
+ }
10
+ export declare const isShipmentList: (v: unknown) => v is ShipmentListResponse;
11
+ export declare class Shipments {
12
+ private apiBase;
13
+ private fetchFn;
14
+ constructor(apiBase: URL, fetchFn: FetchFn);
15
+ list(): Promise<ShipmentListResponse>;
16
+ create(data: Shipment): Promise<Shipment>;
17
+ }
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Shipments = exports.isShipmentList = void 0;
4
+ const index_js_1 = require("../../middleware/index.js");
5
+ const array_js_1 = require("../../validators/array.js");
6
+ const number_js_1 = require("../../validators/number.js");
7
+ const object_js_1 = require("../../validators/object.js");
8
+ const is_shipment_js_1 = require("../../validators/is_shipment.js");
9
+ const string_js_1 = require("../../validators/string.js");
10
+ const mustShipmentList = (v) => (0, string_js_1.isString)(v.href) &&
11
+ (0, number_js_1.isNumber)(v.count) &&
12
+ (0, number_js_1.isNumber)(v.page) &&
13
+ (0, number_js_1.isNumber)(v.per_page) &&
14
+ (0, array_js_1.isArray)(is_shipment_js_1.isShipment)(v.items);
15
+ const isShipmentList = (v) => (0, object_js_1.isObject)(v) && mustShipmentList(v);
16
+ exports.isShipmentList = isShipmentList;
17
+ class Shipments {
18
+ apiBase;
19
+ fetchFn;
20
+ constructor(apiBase, fetchFn) {
21
+ this.apiBase = apiBase;
22
+ this.fetchFn = fetchFn;
23
+ }
24
+ async list() {
25
+ const res = await (0, index_js_1.jsonResponse)(exports.isShipmentList)((0, index_js_1.get)((0, index_js_1.is2xx)(this.fetchFn)))(this.apiBase);
26
+ return res.jsonResponse;
27
+ }
28
+ async create(data) {
29
+ const res = await (0, index_js_1.jsonResponse)(is_shipment_js_1.isShipment)((0, index_js_1.post)((0, index_js_1.jsonRequest)(data)((0, index_js_1.is2xx)(this.fetchFn))))(this.apiBase);
30
+ return res.jsonResponse;
31
+ }
32
+ }
33
+ exports.Shipments = Shipments;
@@ -0,0 +1,18 @@
1
+ import { FetchFn } from '../middleware/index.js';
2
+ import { Organization } from '../models/index.js';
3
+ import { Organization as OrganizationAPI } from './organizations/index.js';
4
+ export interface OrganizationListResponse {
5
+ href: string;
6
+ count: number;
7
+ page: number;
8
+ per_page: number;
9
+ items: Organization[];
10
+ }
11
+ export declare const isOrganizationList: (v: unknown) => v is OrganizationListResponse;
12
+ export declare class Organizations {
13
+ private apiBase;
14
+ private fetch;
15
+ constructor(apiBase: URL, fetch: FetchFn);
16
+ list(): Promise<OrganizationListResponse>;
17
+ get(id: number): OrganizationAPI;
18
+ }
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Organizations = exports.isOrganizationList = void 0;
4
+ const index_js_1 = require("../middleware/index.js");
5
+ const array_js_1 = require("../validators/array.js");
6
+ const number_js_1 = require("../validators/number.js");
7
+ const object_js_1 = require("../validators/object.js");
8
+ const organization_js_1 = require("../validators/organization.js");
9
+ const string_js_1 = require("../validators/string.js");
10
+ const index_js_2 = require("./organizations/index.js");
11
+ const mustOrganizationList = (v) => (0, string_js_1.isString)(v.href) &&
12
+ (0, number_js_1.isNumber)(v.count) &&
13
+ (0, number_js_1.isNumber)(v.page) &&
14
+ (0, number_js_1.isNumber)(v.per_page) &&
15
+ (0, array_js_1.isArray)(organization_js_1.isOrganization)(v.items);
16
+ const isOrganizationList = (v) => (0, object_js_1.isObject)(v) && mustOrganizationList(v);
17
+ exports.isOrganizationList = isOrganizationList;
18
+ class Organizations {
19
+ apiBase;
20
+ fetch;
21
+ constructor(apiBase, fetch) {
22
+ this.apiBase = apiBase;
23
+ this.fetch = fetch;
24
+ }
25
+ async list() {
26
+ const res = await (0, index_js_1.jsonResponse)(exports.isOrganizationList)((0, index_js_1.get)((0, index_js_1.is2xx)(this.fetch)))(this.apiBase);
27
+ return res.jsonResponse;
28
+ }
29
+ get(id) {
30
+ return new index_js_2.Organization(this.apiBase, (0, index_js_1.endpoint)(`${id}`)(this.fetch));
31
+ }
32
+ }
33
+ exports.Organizations = Organizations;
@@ -0,0 +1,14 @@
1
+ import { FetchFn } from '../../middleware/index.js';
2
+ export declare class Shipment {
3
+ private apiBase;
4
+ private fetchFn;
5
+ constructor(apiBase: URL, fetchFn: FetchFn);
6
+ fetch(): Promise<import("../../index.js").Shipment>;
7
+ label(args?: {
8
+ format?: 'Pdf' | 'Zpl' | 'Epl';
9
+ type?: 'normal' | 'A6' | 'dpi300';
10
+ }): Promise<ReadableStream<Uint8Array>>;
11
+ buy(body: {
12
+ offer_id: number;
13
+ }): Promise<import("../../index.js").Shipment>;
14
+ }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Shipment = void 0;
4
+ const index_js_1 = require("../../middleware/index.js");
5
+ const is_shipment_js_1 = require("../../validators/is_shipment.js");
6
+ class Shipment {
7
+ apiBase;
8
+ fetchFn;
9
+ constructor(apiBase, fetchFn) {
10
+ this.apiBase = apiBase;
11
+ this.fetchFn = fetchFn;
12
+ }
13
+ async fetch() {
14
+ const res = await (0, index_js_1.jsonResponse)(is_shipment_js_1.isShipment)((0, index_js_1.get)((0, index_js_1.is2xx)(this.fetchFn)))(this.apiBase);
15
+ return res.jsonResponse;
16
+ }
17
+ async label(args = {}) {
18
+ const res = await (0, index_js_1.endpoint)('label')((0, index_js_1.values)(args)((0, index_js_1.get)((0, index_js_1.is2xx)(this.fetchFn))))(this.apiBase);
19
+ return res.body;
20
+ }
21
+ async buy(body) {
22
+ const res = await (0, index_js_1.jsonResponse)(is_shipment_js_1.isShipment)((0, index_js_1.endpoint)('buy')((0, index_js_1.jsonRequest)(body)((0, index_js_1.post)((0, index_js_1.is2xx)(this.fetchFn)))))(this.apiBase);
23
+ return res.jsonResponse;
24
+ }
25
+ }
26
+ exports.Shipment = Shipment;
@@ -0,0 +1,19 @@
1
+ import { FetchFn } from '../middleware/index.js';
2
+ import { Shipment } from '../models/index.js';
3
+ import { Shipment as ShipmentAPI } from './shipments/index.js';
4
+ export interface ShipmentListResponse {
5
+ href: string;
6
+ count: number;
7
+ page: number;
8
+ per_page: number;
9
+ items: Shipment[];
10
+ }
11
+ export declare const isShipmentList: (v: unknown) => v is ShipmentListResponse;
12
+ export declare class Shipments {
13
+ private apiBase;
14
+ private fetchFn;
15
+ constructor(apiBase: URL, fetchFn: FetchFn);
16
+ list(): Promise<ShipmentListResponse>;
17
+ create(data: Shipment): Promise<Shipment>;
18
+ get(id: number): ShipmentAPI;
19
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Shipments = exports.isShipmentList = void 0;
4
+ const index_js_1 = require("../middleware/index.js");
5
+ const array_js_1 = require("../validators/array.js");
6
+ const number_js_1 = require("../validators/number.js");
7
+ const object_js_1 = require("../validators/object.js");
8
+ const is_shipment_js_1 = require("../validators/is_shipment.js");
9
+ const string_js_1 = require("../validators/string.js");
10
+ const index_js_2 = require("./shipments/index.js");
11
+ const mustShipmentList = (v) => (0, string_js_1.isString)(v.href) &&
12
+ (0, number_js_1.isNumber)(v.count) &&
13
+ (0, number_js_1.isNumber)(v.page) &&
14
+ (0, number_js_1.isNumber)(v.per_page) &&
15
+ (0, array_js_1.isArray)(is_shipment_js_1.isShipment)(v.items);
16
+ const isShipmentList = (v) => (0, object_js_1.isObject)(v) && mustShipmentList(v);
17
+ exports.isShipmentList = isShipmentList;
18
+ class Shipments {
19
+ apiBase;
20
+ fetchFn;
21
+ constructor(apiBase, fetchFn) {
22
+ this.apiBase = apiBase;
23
+ this.fetchFn = fetchFn;
24
+ }
25
+ async list() {
26
+ const res = await (0, index_js_1.jsonResponse)(exports.isShipmentList)((0, index_js_1.get)((0, index_js_1.is2xx)(this.fetchFn)))(this.apiBase);
27
+ return res.jsonResponse;
28
+ }
29
+ async create(data) {
30
+ const res = await (0, index_js_1.jsonResponse)(is_shipment_js_1.isShipment)((0, index_js_1.post)((0, index_js_1.jsonRequest)(data)((0, index_js_1.is2xx)(this.fetchFn))))(this.apiBase);
31
+ return res.jsonResponse;
32
+ }
33
+ get(id) {
34
+ return new index_js_2.Shipment(this.apiBase, (0, index_js_1.endpoint)(`${id}`)(this.fetchFn));
35
+ }
36
+ }
37
+ exports.Shipments = Shipments;
@@ -0,0 +1,6 @@
1
+ export declare class InvalidBody extends Error {
2
+ }
3
+ export declare class InvalidResponseStatus extends Error {
4
+ res: Response;
5
+ constructor(msg: string, res: Response);
6
+ }
package/dist/error.js ADDED
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InvalidResponseStatus = exports.InvalidBody = void 0;
4
+ class InvalidBody extends Error {
5
+ }
6
+ exports.InvalidBody = InvalidBody;
7
+ class InvalidResponseStatus extends Error {
8
+ res;
9
+ constructor(msg, res) {
10
+ super(msg);
11
+ this.res = res;
12
+ }
13
+ }
14
+ exports.InvalidResponseStatus = InvalidResponseStatus;
@@ -0,0 +1,16 @@
1
+ import { Organizations } from "./api/organizations.js";
2
+ import { Shipments } from "./api/shipments.js";
3
+ export declare class Client {
4
+ private opts;
5
+ private auth;
6
+ private base;
7
+ constructor(opts: {
8
+ host: string;
9
+ version?: string;
10
+ apiKey: string;
11
+ });
12
+ private doRequest;
13
+ organizations(): Organizations;
14
+ shipments(): Shipments;
15
+ }
16
+ export * from "./models/index.js";
package/dist/index.js ADDED
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.Client = void 0;
18
+ const organizations_js_1 = require("./api/organizations.js");
19
+ const shipments_js_1 = require("./api/shipments.js");
20
+ const index_js_1 = require("./middleware/index.js");
21
+ class Client {
22
+ opts;
23
+ auth;
24
+ base;
25
+ constructor(opts) {
26
+ this.opts = opts;
27
+ this.auth = (0, index_js_1.auth)(this.opts.apiKey);
28
+ this.base = new URL(`https://${this.opts.host}`);
29
+ }
30
+ doRequest() {
31
+ return (info, init) => {
32
+ return (0, index_js_1.endpoint)(this.opts.version || "v1")(this.auth(fetch))(info, init);
33
+ };
34
+ }
35
+ organizations() {
36
+ return new organizations_js_1.Organizations(this.base, (0, index_js_1.endpoint)("organizations")(this.doRequest()));
37
+ }
38
+ shipments() {
39
+ return new shipments_js_1.Shipments(this.base, (0, index_js_1.endpoint)("shipments")(this.doRequest()));
40
+ }
41
+ }
42
+ exports.Client = Client;
43
+ __exportStar(require("./models/index.js"), exports);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const node_test_1 = require("node:test");
4
+ const node_assert_1 = require("node:assert");
5
+ const index_js_1 = require("./index.js");
6
+ const fs_1 = require("fs");
7
+ const node_os_1 = require("node:os");
8
+ const promises_1 = require("fs/promises");
9
+ const node_path_1 = require("node:path");
10
+ const node_stream_1 = require("node:stream");
11
+ const { INPOST_HOST: host = "sandbox-api-shipx-pl.easypack24.net", INPOST_API_KEY: apiKey, } = process.env;
12
+ (0, node_test_1.describe)("inpost client tests", { skip: !apiKey }, () => {
13
+ const client = new index_js_1.Client({ host, apiKey });
14
+ (0, node_test_1.it)("lists organizations", () => (0, node_assert_1.doesNotReject)(client.organizations().list()));
15
+ (0, node_test_1.it)("fetches organization", () => (0, node_assert_1.doesNotReject)(client
16
+ .organizations()
17
+ .list()
18
+ .then((orgs) => client.organizations().get(orgs.items[0].id).fetch())));
19
+ (0, node_test_1.it)("fetches shipments", () => (0, node_assert_1.doesNotReject)(client
20
+ .organizations()
21
+ .list()
22
+ .then((orgs) => client.organizations().get(orgs.items[0].id).shipments().list())));
23
+ (0, node_test_1.it)("test shipment flow", () => (0, node_assert_1.doesNotReject)(async () => {
24
+ const orgs = await client.organizations().list();
25
+ const org = orgs.items[0];
26
+ // is not falsy
27
+ (0, node_assert_1.notEqual)(!!org, false);
28
+ let shipment = await client
29
+ .organizations()
30
+ .get(org.id)
31
+ .shipments()
32
+ .create({
33
+ receiver: {
34
+ first_name: "Test",
35
+ last_name: "Test",
36
+ email: "email@example.org",
37
+ phone: "321321321",
38
+ address: {
39
+ street: "Czerniakowska",
40
+ building_number: "87A",
41
+ city: "Warszawa",
42
+ post_code: "00-718",
43
+ country_code: "PL" /* CountryCode.pl */,
44
+ },
45
+ },
46
+ sender: {
47
+ company_name: "Company_name",
48
+ first_name: "Test",
49
+ last_name: "Test",
50
+ email: "email@example.com",
51
+ phone: "888000000",
52
+ address: {
53
+ line1: "Cybernetyki 10",
54
+ city: "Warszawa",
55
+ post_code: "02-677",
56
+ country_code: "PL" /* CountryCode.pl */,
57
+ },
58
+ },
59
+ parcels: [{ template: "small" }],
60
+ service: "inpost_locker_standard",
61
+ custom_attributes: {
62
+ target_point: "KRA010",
63
+ },
64
+ });
65
+ while (!shipment.status || shipment.status === "created") {
66
+ console.log("Waiting for shipment to be created...");
67
+ await new Promise((resolve) => setTimeout(resolve, 500));
68
+ shipment = await client.shipments().get(shipment.id).fetch();
69
+ }
70
+ shipment = await client
71
+ .shipments()
72
+ .get(shipment.id)
73
+ .buy({ offer_id: shipment.offers[0].id });
74
+ let found = shipment.transactions?.find((t) => t.status === "success" && t.offer_id === shipment.offers?.[0].id);
75
+ while (!found) {
76
+ console.log("Waiting for transaction to succeed...");
77
+ await new Promise((resolve) => setTimeout(resolve, 500));
78
+ shipment = await client.shipments().get(shipment.id).fetch();
79
+ found = shipment.transactions?.find((t) => t.status === "success");
80
+ if (!found) {
81
+ await client
82
+ .shipments()
83
+ .get(shipment.id)
84
+ .buy({ offer_id: shipment.offers[0].id });
85
+ }
86
+ }
87
+ while (!shipment.status ||
88
+ shipment.status === "created" ||
89
+ shipment.status === "offer_selected") {
90
+ console.log("Waiting for shipment to be ready...");
91
+ await new Promise((resolve) => setTimeout(resolve, 500));
92
+ shipment = await client.shipments().get(shipment.id).fetch();
93
+ }
94
+ const tmp = await (0, promises_1.mkdtemp)((0, node_path_1.join)((0, node_os_1.tmpdir)(), "test-inpost-label"));
95
+ const label = await client.shipments().get(shipment.id).label();
96
+ await label.pipeTo(node_stream_1.Writable.toWeb((0, fs_1.createWriteStream)((0, node_path_1.join)(tmp, "label.pdf"))));
97
+ await (0, promises_1.rm)(tmp, { force: true, recursive: true });
98
+ }));
99
+ });
@@ -0,0 +1,2 @@
1
+ import { Middleware } from './middleware.js';
2
+ export declare const auth: (token: string) => Middleware;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.auth = void 0;
4
+ const auth = (token) => (next) => (info, init) => {
5
+ const headers = new Headers(init?.headers);
6
+ headers.set('authorization', `Bearer ${token}`);
7
+ return next(info, {
8
+ ...init,
9
+ headers,
10
+ });
11
+ };
12
+ exports.auth = auth;
@@ -0,0 +1,2 @@
1
+ import { Middleware } from './middleware.js';
2
+ export declare const endpoint: (endpoint: string) => Middleware;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.endpoint = void 0;
4
+ const buildEndpoint = (url, endpoint) => {
5
+ url = new URL(url);
6
+ url.pathname = `${endpoint.replace(/\/+$/, '')}/${url.pathname.replace(/^\/+/, '')}`;
7
+ return url;
8
+ };
9
+ const endpoint = (endpoint) => (next) => (info, init) => next(buildEndpoint(info, endpoint), init);
10
+ exports.endpoint = endpoint;
@@ -0,0 +1,2 @@
1
+ import { Middleware } from "./middleware.js";
2
+ export declare const is2xx: Middleware;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.is2xx = void 0;
4
+ const error_js_1 = require("../error.js");
5
+ const is2xx = (next) => async (info, init) => {
6
+ const res = await next(info, init);
7
+ if (res.status < 200 || res.status > 299) {
8
+ if ((process.env.LOG_LEVEL || "").toLowerCase() === "debug") {
9
+ console.log(await res.text());
10
+ }
11
+ // Let user know message from the response if available
12
+ let error;
13
+ try {
14
+ error = await res.json();
15
+ }
16
+ catch {
17
+ error = {
18
+ message: "Unknown error",
19
+ details: "No additional details available",
20
+ };
21
+ }
22
+ throw new error_js_1.InvalidResponseStatus(`${error.message}: ${error.details}, (status: ${res.status})`, res);
23
+ }
24
+ return res;
25
+ };
26
+ exports.is2xx = is2xx;
@@ -0,0 +1,2 @@
1
+ import { Middleware } from './middleware.js';
2
+ export declare const get: Middleware;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.get = void 0;
4
+ const get = (next) => (info, init) => next(info, {
5
+ ...init,
6
+ method: 'GET',
7
+ });
8
+ exports.get = get;
@@ -0,0 +1,9 @@
1
+ export * from './middleware.js';
2
+ export * from './get.js';
3
+ export * from './post.js';
4
+ export * from './endpoint.js';
5
+ export * from './json_request.js';
6
+ export * from './json_response.js';
7
+ export * from './auth.js';
8
+ export * from './expect_200.js';
9
+ export * from './values.js';
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./middleware.js"), exports);
18
+ __exportStar(require("./get.js"), exports);
19
+ __exportStar(require("./post.js"), exports);
20
+ __exportStar(require("./endpoint.js"), exports);
21
+ __exportStar(require("./json_request.js"), exports);
22
+ __exportStar(require("./json_response.js"), exports);
23
+ __exportStar(require("./auth.js"), exports);
24
+ __exportStar(require("./expect_200.js"), exports);
25
+ __exportStar(require("./values.js"), exports);
@@ -0,0 +1,2 @@
1
+ import { Middleware } from './middleware.js';
2
+ export declare const jsonRequest: (rawBody: unknown) => Middleware;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.jsonRequest = void 0;
4
+ const jsonRequest = (rawBody) => (next) => async (info, init) => {
5
+ const headers = new Headers(init?.headers);
6
+ headers.set('content-type', `application/json`);
7
+ const body = JSON.stringify(rawBody);
8
+ return next(info, {
9
+ ...init,
10
+ headers,
11
+ body,
12
+ });
13
+ };
14
+ exports.jsonRequest = jsonRequest;
@@ -0,0 +1,5 @@
1
+ import { Middleware } from './middleware.js';
2
+ export type JSONResponse<T> = {
3
+ jsonResponse: T;
4
+ };
5
+ export declare const jsonResponse: <T>(validate: (v: unknown) => v is T) => Middleware<JSONResponse<T>>;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.jsonResponse = void 0;
4
+ const error_js_1 = require("../error.js");
5
+ const jsonResponse = (validate) => (next) => async (info, init) => {
6
+ const res = await next(info, init);
7
+ const data = await res.json();
8
+ if (!validate(data)) {
9
+ throw new error_js_1.InvalidBody('invalid response body');
10
+ }
11
+ res.jsonResponse = data;
12
+ return res;
13
+ };
14
+ exports.jsonResponse = jsonResponse;
@@ -0,0 +1,4 @@
1
+ export type FetchFn<T = unknown> = (info: string | URL, init?: RequestInit) => Promise<T & Response>;
2
+ export interface Middleware<T = unknown> {
3
+ <U = unknown>(next: FetchFn<U>): FetchFn<T & U>;
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ import { Middleware } from './middleware.js';
2
+ export declare const post: Middleware;