UNPKG

postnl-sdk

Version:

Unofficial PostNL SDK for Node.js inspired by Resend

287 lines (277 loc) 8.39 kB
var __defProp = Object.defineProperty; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; // src/utils.ts function objectToSearchParams(obj) { const searchParams = new URLSearchParams(); Object.keys(obj).forEach((key) => { searchParams.append(key, obj[key]); }); return searchParams; } // src/error.ts var _PostNLError = class _PostNLError extends Error { constructor(path, error) { super(); this.name = "PostNLError"; if (this.isHTTPError(error)) { this.message = error.message; } if (this.isServerError(error)) { this.message = error.fault.faultstring; } for (const { discriminator, handler } of _PostNLError.discriminators) { if (discriminator(path)) { this.message = path + " - " + handler(error); return; } } this.message = JSON.stringify(error); } isHTTPError(error) { return error.http_status_code !== void 0; } isServerError(error) { return error.fault !== void 0; } static registerDiscriminator(discriminator, handler) { this.discriminators.push({ discriminator, handler }); } toString() { return JSON.stringify({ name: this.name, message: this.message, object: this.object }); } }; _PostNLError.discriminators = []; var PostNLError = _PostNLError; // src/endpoint.ts var Endpoint = class { constructor() { this.registerDiscriminator(); } }; // src/barcode/barcode.ts var Barcode = class extends Endpoint { constructor(postnl) { super(); this.postnl = postnl; this.path = "/shipment/v1_1/barcode"; } generate(payload) { return __async(this, null, function* () { const searchParams = objectToSearchParams(payload); return yield this.postnl.get(`${this.path}?${searchParams.toString()}`); }); } registerDiscriminator() { PostNLError.registerDiscriminator( (path) => path === this.path, (error) => `(${error.errors.errorNumber}) ${error.errors.errorMsg}` ); } }; // src/confirm/confirm.ts var Confirm = class extends Endpoint { constructor(postnl) { super(); this.postnl = postnl; this.path = "/shipment/v2/confirm"; } confirmShipment(payload) { return __async(this, null, function* () { return yield this.postnl.post(`${this.path}`, payload); }); } registerDiscriminator() { PostNLError.registerDiscriminator( (path) => path === this.path, (error) => { var _a, _b; return `(${(_a = error.ResponseShipments[0].Errors) == null ? void 0 : _a[0].Code}) ${(_b = error.ResponseShipments[0].Errors) == null ? void 0 : _b[0].Description}}`; } ); } }; // src/label/label.ts var Label = class extends Endpoint { constructor(postnl) { super(); this.postnl = postnl; this.path = "/shipment/v2_2/label"; } /** * * @link https://developer.postnl.nl/docs/#/http/api-endpoints/send-track/labelling/generate-label * @see https://developer.postnl.nl/docs/#/http/api-endpoints/send-track/shipment/generate-shipment-label */ generate(payload, confirm) { return __async(this, null, function* () { const path = confirm ? `${this.path}?confirm=true` : this.path; return yield this.postnl.post(`${path}`, payload); }); } registerDiscriminator() { PostNLError.registerDiscriminator( (path) => path === this.path, (error) => JSON.stringify(error.Errors) ); } }; // src/shipment/shipment.ts var Shipment = class extends Endpoint { constructor(postnl) { super(); this.postnl = postnl; this.path = "/shipment/v2_2/label"; } /** * @link https://developer.postnl.nl/docs/#/http/api-endpoints/send-track/shipment/generate-shipment-label */ generateLabel(payload, confirm) { return __async(this, null, function* () { const path = confirm ? `${this.path}?confirm=true` : this.path; return yield this.postnl.post(`${path}`, payload); }); } registerDiscriminator() { PostNLError.registerDiscriminator( (path) => path === this.path, (error) => JSON.stringify(error.Errors) ); } }; // src/postalcode/postalcode.ts var Postalcode = class extends Endpoint { constructor(postnl) { super(); this.postnl = postnl; this.path = "/shipment/checkout/v1/postalcodecheck"; } /** * @warning This endpoint doesn't work on the sandbox environment. * @link https://developer.postnl.nl/docs/#/http/api-endpoints/checkout-delivery-options/postalcode-check/checkout-postalcode-check */ check(postalcode, housenumber, housenumberaddition) { return __async(this, null, function* () { const searchParams = new URLSearchParams(); searchParams.append("postalcode", postalcode); searchParams.append("housenumber", housenumber.toString()); if (housenumberaddition) { searchParams.append("housenumberaddition", housenumberaddition); } const response = yield this.postnl.get(`${this.path}?${searchParams.toString()}`); if (response.length === 1) { return response[0]; } return response; }); } registerDiscriminator() { PostNLError.registerDiscriminator( (path) => path === this.path, (error) => JSON.stringify(error.Errors) ); } }; // src/postnl.ts var defaultBaseUrl = "https://api.postnl.nl"; var sandboxBaseUrl = "https://api-sandbox.postnl.nl"; var baseUrl = typeof process.env !== "undefined" && process.env.NODE_ENV === "production" ? defaultBaseUrl : sandboxBaseUrl; var PostNL = class { constructor(key) { this.key = key; this.barcode = new Barcode(this); this.confirm = new Confirm(this); this.label = new Label(this); this.shipment = new Shipment(this); this.postalcode = new Postalcode(this); if (!key) { if (typeof process !== "undefined" && process.env) { this.key = process.env.POSTNL_API_KEY; } if (!this.key) { throw new Error( 'Missing API key. Pass it to the constructor `new PostNL("...")`' ); } } this.headers = new Headers({ "apiKey": `${this.key}`, "Content-Type": "application/json" }); } fetchRequest(path, options) { return __async(this, null, function* () { const response = yield fetch(`${baseUrl}${path}`, options); this.lastRequest = { url: `${baseUrl}${path}`, options, response }; if (!response.ok) { const error = yield response.json(); const cleanPath = path.split("?")[0]; throw new PostNLError(cleanPath, error); } return yield response.json(); }); } post(_0, _1) { return __async(this, arguments, function* (path, entity, options = {}) { const requestOptions = __spreadValues({ method: "POST", headers: this.headers, body: JSON.stringify(entity) }, options); return this.fetchRequest(path, requestOptions); }); } get(_0) { return __async(this, arguments, function* (path, options = {}) { const requestOptions = __spreadValues({ method: "GET", headers: this.headers }, options); return this.fetchRequest(path, requestOptions); }); } }; export { Barcode, Confirm, Label, PostNL, Shipment };