UNPKG

@shaggytools/nhtsa-api-wrapper

Version:

Universal javascript wrapper for the NHTSA.dot.gov VPIC 'vehicles' API, useful for VIN decoding, etc.

748 lines (747 loc) 19.4 kB
const b = ({ name: e, value: t, required: n, types: r, errorMode: o = "error" }) => { if (f(e) !== "string") throw Error("'name', is required and must be of type string"); let u = ""; const c = f(t), s = `error validating argument named "${e}",`, y = `received value: ${t} - of type: <${c}>`; if (r && (f(r) !== "array" || !r.length)) throw Error(`${s} 'types' must be an array of strings`); const a = r ? `<${r.join(" | ")}>` : ""; if (n && !r ? t || (u = `${s} is required, ${y}`) : r && !n ? t !== void 0 && !r.includes(c) && (u = `${s} must be of type(s) ${a}, ${y}`) : n && r && (!t || !r.includes(c)) && (u = `${s} is required and must be of type(s) ${a}, ${y}`), u.length) { if (o === "boolean") return !1; throw Error(u); } return !0; }, i = ({ args: e, mode: t = "default" }) => { if (f(e) !== "array" || !e.length) throw Error( 'catchInvalidArguments requires "args" that must be an array of IArgToValidate objects' ); if (t === "default") e.forEach((n) => b(n)); else if (t === "atLeast" && !e.find((r) => !!r.value)) throw Error( `must provide at least one of the following arguments: ${e.map((r) => r.name).join(", ")}` ); return !0; }, G = (e) => f(e) === "error", k = (e) => { let t = "an unknown error occurred."; return G(e) ? (e.message || (e.message = t), e) : (f(e) === "string" && (t = e), Error(t)); }, l = async (e) => Promise.reject(k(e)), f = (e) => { const t = Object.prototype.toString.call(e).toLowerCase(); return t.slice(8, t.length - 1); }, $ = { A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7, H: 8, J: 1, K: 2, L: 3, M: 4, N: 5, P: 7, R: 9, S: 2, T: 3, U: 4, V: 5, W: 6, X: 7, Y: 8, Z: 9 }, N = [ 8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2 ]; function A(e) { if (typeof e != "string" || e.length != 17) return !1; e = e.toUpperCase(); const t = e.split(""), n = t[8]; if (isNaN(parseInt(n)) && n !== "X") return !1; const r = n === "X" ? 10 : parseInt(n); return t.map((u, c) => { let s; isNaN(parseInt(u)) ? s = $[u] : s = parseInt(u); const y = N[c]; return s * y; }).reduce((u, c) => u + c, 0) % 11 === r; } const j = "https://vpic.nhtsa.dot.gov/api/vehicles", V = "json", C = (e) => (b({ name: "params", value: e, required: !0, types: ["object"] }), Object.entries(e).filter( ([n, r]) => b({ name: n, types: ["string", "number", "boolean"], value: r, errorMode: "boolean" }) ).reduce((n, [r, o]) => (n[r] = encodeURIComponent(o), n), {})), I = (e = {}, t = !1) => { b({ name: "params", value: e, types: ["object"] }); const n = C({ ...e, format: V }); return "?" + Object.entries(n).map(([r, o], u, c) => o.length || t && o === "" ? `${r}=${o}${u < c.length - 1 ? "&" : ""}` : "").join(""); }; async function E(e, t, n = !0) { const r = "DecodeVin"; try { typeof t == "boolean" && (n = t, t = void 0); const o = [ { name: "vin", value: e, required: !0, types: ["string"] }, { name: "params", value: t, types: ["object"] }, { name: "modelYear", value: t == null ? void 0 : t.modelYear, types: ["string", "number"] } ]; i({ args: o }); const { get: u, createCachedUrl: c, getCachedUrl: s } = d(); return c({ endpointName: r, path: e, params: t }), n ? u() : s(); } catch (o) { return l(o); } } async function S(e, t, n = !0) { const r = "DecodeVinExtended"; try { typeof t == "boolean" && (n = t, t = void 0); const o = [ { name: "vin", value: e, required: !0, types: ["string"] }, { name: "params", value: t, types: ["object"] }, { name: "modelYear", value: t == null ? void 0 : t.modelYear, types: ["string", "number"] } ]; i({ args: o }); const { get: u, createCachedUrl: c, getCachedUrl: s } = d(); return c({ endpointName: r, path: e, params: t }), n ? u() : s(); } catch (o) { return l(o); } } async function w(e, t, n = !0) { const r = "DecodeVinValues"; try { typeof t == "boolean" && (n = t, t = void 0); const o = [ { name: "vin", value: e, required: !0, types: ["string"] }, { name: "params", value: t, types: ["object"] }, { name: "modelYear", value: t == null ? void 0 : t.modelYear, types: ["string", "number"] } ]; i({ args: o }); const { get: u, createCachedUrl: c, getCachedUrl: s } = d(); return c({ endpointName: r, path: e, params: t }), n ? u() : s(); } catch (o) { return l(o); } } async function Y(e, t = !0) { const n = "DecodeVinValuesBatch"; try { i({ args: [ { name: "inputString", value: e, required: !0, types: ["string"] } ] }); const { post: o, createCachedUrl: u, getCachedUrl: c } = d(); return u({ endpointName: n, includeQueryString: !1 }), t ? o(c(), { body: e }) : c(); } catch (r) { return l(r); } } async function D(e, t, n = !0) { const r = "DecodeVinValuesExtended"; try { typeof t == "boolean" && (n = t, t = void 0); const o = [ { name: "vin", value: e, required: !0, types: ["string"] }, { name: "params", value: t, types: ["object"] }, { name: "modelYear", value: t == null ? void 0 : t.modelYear, types: ["string", "number"] } ]; i({ args: o }); const { get: u, createCachedUrl: c, getCachedUrl: s } = d(); return c({ endpointName: r, path: e, params: t }), n ? u() : s(); } catch (o) { return l(o); } } async function P(e, t = !0) { const n = "DecodeWMI"; try { i({ args: [ { name: "WMI", value: e, required: !0, types: ["string"] } ] }); const { get: o, createCachedUrl: u, getCachedUrl: c } = d(); return u({ endpointName: n, path: e }), t ? o() : c(); } catch (r) { return l(r); } } async function L(e = !0) { const t = "GetAllMakes"; try { const { get: n, createCachedUrl: r, getCachedUrl: o } = d(); return r({ endpointName: t }), e ? n() : o(); } catch (n) { return l(n); } } async function F(e, t = !0) { const n = "GetAllManufacturers"; try { typeof e == "boolean" && (t = e, e = void 0); const r = [ { name: "params", value: e, types: ["object"] }, { name: "manufacturerType", value: e == null ? void 0 : e.manufacturerType, types: ["string"] }, { name: "page", value: e == null ? void 0 : e.page, types: ["string", "number"] } ]; i({ args: r }); const { get: o, createCachedUrl: u, getCachedUrl: c } = d(); return u({ endpointName: n, params: e }), t ? o() : c(); } catch (r) { return l(r); } } async function R(e, t = !0) { const n = "GetCanadianVehicleSpecifications"; try { const r = [ { name: "params", value: e, required: !0, types: ["object"] }, { name: "year", value: e.year, required: !0, types: ["string", "number"] }, { name: "make", value: e.make, types: ["string"] }, { name: "model", value: e.model, types: ["string"] }, { name: "units", value: e.units, types: ["string"] } ]; i({ args: r }); const { get: o, createCachedUrl: u, getCachedUrl: c } = d(); return u({ endpointName: n, params: { make: "", model: "", units: "", ...e }, allowEmptyParams: !0 }), t ? o() : c(); } catch (r) { return l(r); } } async function O(e, t = !0) { const n = "GetEquipmentPlantCodes"; try { const r = [ { name: "params", value: e, required: !0, types: ["object"] }, { name: "equipmentType", value: e == null ? void 0 : e.equipmentType, required: !0, types: ["string", "number"] }, { name: "reportType", value: e == null ? void 0 : e.reportType, required: !0, types: ["string"] }, { name: "year", value: e == null ? void 0 : e.year, required: !0, types: ["string", "number"] } ]; i({ args: r }); const { get: o, createCachedUrl: u, getCachedUrl: c } = d(); return u({ endpointName: n, params: e }), t ? o() : c(); } catch (r) { return l(r); } } async function _(e, t = !0) { const n = "GetMakeForManufacturer"; try { i({ args: [ { name: "manufacturer", value: e, required: !0, types: ["string", "number"] } ] }); const { get: o, createCachedUrl: u, getCachedUrl: c } = d(); return u({ endpointName: n, path: e.toString() }), t ? o() : c(); } catch (r) { return l(r); } } async function x(e, t, n = !0) { const r = "GetMakesForManufacturerAndYear"; try { const o = [ { name: "manufacturer", value: e, required: !0, types: ["string", "number"] }, { name: "params", value: t, required: !0, types: ["object"] }, { name: "year", value: t.year, required: !0, types: ["string", "number"] } ]; i({ args: o }); const { get: u, createCachedUrl: c, getCachedUrl: s } = d(); return c({ endpointName: r, path: e.toString(), params: t }), n ? u() : s(); } catch (o) { return l(o); } } async function W(e, t = !0) { const n = "GetMakesForVehicleType"; try { i({ args: [ { name: "typeName", value: e, required: !0, types: ["string"] } ] }); const { get: o, createCachedUrl: u, getCachedUrl: c } = d(); return u({ endpointName: n, path: e }), t ? o() : c(); } catch (r) { return l(r); } } async function B(e, t = !0) { const n = "GetManufacturerDetails"; try { i({ args: [ { name: "manufacturer", value: e, required: !0, types: ["string", "number"] } ] }); const { get: o, createCachedUrl: u, getCachedUrl: c } = d(); return u({ endpointName: n, path: e.toString() }), t ? o() : c(); } catch (r) { return l(r); } } async function H(e, t = !0) { const n = "GetModelsForMake"; try { i({ args: [ { name: "makeName", value: e, required: !0, types: ["string"] } ] }); const { get: o, createCachedUrl: u, getCachedUrl: c } = d(); return u({ endpointName: n, path: e }), t ? o() : c(); } catch (r) { return l(r); } } async function Q(e, t = !0) { const n = "GetModelsForMakeId"; try { i({ args: [ { name: "makeId", value: e, required: !0, types: ["string", "number"] } ] }); const { get: o, createCachedUrl: u, getCachedUrl: c } = d(); return u({ endpointName: n, path: e.toString() }), t ? o() : c(); } catch (r) { return l(r); } } async function J(e, t = !0) { const n = "GetModelsForMakeIdYear"; try { const r = [ { name: "modelYear", value: e.modelYear, types: ["string", "number"] }, { name: "vehicleType", value: e.vehicleType, types: ["string"] } ], o = [ { name: "params", value: e, required: !0, types: ["object"] }, { name: "makeId", value: e.makeId, required: !0, types: ["string", "number"] }, ...r ]; i({ args: o }), i({ args: r, mode: "atLeast" }); const { makeId: u, modelYear: c, vehicleType: s } = C(e); let y = `makeId/${u}/`; y += c ? `modelYear/${c}` : "", y += s ? `${c ? "/" : ""}vehicleType/${s}` : ""; const { get: a, createCachedUrl: g, getCachedUrl: v } = d(); return g({ endpointName: n, path: y }), t ? a() : v(); } catch (r) { return l(r); } } async function X(e, t = !0) { const n = "GetModelsForMakeYear"; try { const r = [ { name: "modelYear", value: e.modelYear, types: ["string", "number"] }, { name: "vehicleType", value: e.vehicleType, types: ["string"] } ], o = [ { name: "params", value: e, required: !0, types: ["object"] }, { name: "make", value: e.make, required: !0, types: ["string"] }, ...r ]; i({ args: o }), i({ args: r, mode: "atLeast" }); const { make: u, modelYear: c, vehicleType: s } = C(e); let y = `make/${u}/`; y += c ? `modelYear/${c}` : "", y += s ? `${c ? "/" : ""}vehicleType/${s}` : ""; const { get: a, createCachedUrl: g, getCachedUrl: v } = d(); return g({ endpointName: n, path: y }), t ? a() : v(); } catch (r) { return l(r); } } async function K(e, t = !0) { const n = "GetParts"; typeof e == "boolean" && (t = e, e = void 0); try { const r = [ { name: "params", value: e, types: ["object"] }, { name: "manufacturer", value: e == null ? void 0 : e.manufacturer, types: ["string", "number"] }, { name: "type", value: e == null ? void 0 : e.type, types: ["number"] }, { name: "fromDate", value: e == null ? void 0 : e.fromDate, types: ["string"] }, { name: "toDate", value: e == null ? void 0 : e.toDate, types: ["string"] }, { name: "page", value: e == null ? void 0 : e.page, types: ["string", "number"] } ]; i({ args: r }); const { get: o, createCachedUrl: u, getCachedUrl: c } = d(); return u({ endpointName: n, params: e }), t ? o() : c(); } catch (r) { return l(r); } } async function Z(e, t = !0) { const n = "GetVehicleTypesForMake"; try { i({ args: [ { name: "makeName", value: e, required: !0, types: ["string"] } ] }); const { get: o, createCachedUrl: u, getCachedUrl: c } = d(); return u({ endpointName: n, path: e }), t ? o() : c(); } catch (r) { return l(r); } } async function z(e, t = !0) { const n = "GetVehicleTypesForMakeId"; try { i({ args: [ { name: "makeId", value: e, required: !0, types: ["string", "number"] } ] }); const { get: o, createCachedUrl: u, getCachedUrl: c } = d(); return u({ endpointName: n, path: e.toString() }), t ? o() : c(); } catch (r) { return l(r); } } async function ee(e = !0) { const t = "GetVehicleVariableList"; try { const { get: n, createCachedUrl: r, getCachedUrl: o } = d(); return r({ endpointName: t }), e ? n() : o(); } catch (n) { return l(n); } } async function te(e, t = !0) { const n = "GetVehicleVariableValuesList"; try { i({ args: [ { name: "variableValue", value: e, required: !0, types: ["string", "number"] } ] }); const { get: o, createCachedUrl: u, getCachedUrl: c } = d(); return u({ endpointName: n, path: e.toString() }), t ? o() : c(); } catch (r) { return l(r); } } async function re(e, t = !0) { const n = "GetWMIsForManufacturer"; try { const r = [ { name: "manufacturer", value: e == null ? void 0 : e.manufacturer, types: ["string", "number"] }, { name: "vehicleType", value: e == null ? void 0 : e.vehicleType, types: ["string", "number"] } ], o = [ { name: "params", value: e, required: !0, types: ["object"] }, ...r ]; i({ args: o }), i({ args: r, mode: "atLeast" }); const u = e != null && e.manufacturer ? encodeURIComponent(e.manufacturer) : "", c = (e == null ? void 0 : e.vehicleType) || "", { get: s, createCachedUrl: y, getCachedUrl: a } = d(); return y({ endpointName: n, path: u, params: { vehicleType: c } }), t ? s() : a(); } catch (r) { return l(r); } } const d = () => { let e; const t = (a) => e = a, n = () => e, r = () => e = "", o = (a) => { if (typeof a == "string") return t(a), a; const { endpointName: g, allowEmptyParams: v = !1, includeQueryString: h = !0, path: m = "", params: p, saveUrl: M = !0 } = a; if (!g) throw Error("Endpoint name is required to create a VPIC URL string"); const T = h ? I(p, v) : "", U = encodeURI( `${j}/${g}/${m}${T}` ); return M && t(U), U; }, u = (a) => o({ ...a, saveUrl: !1 }), c = (a) => encodeURI( `DATA=${a ? a + "&" : ""}format=${V}` ), s = async (a, g = { saveUrl: !0, method: "GET" }) => (a && f(a) === "object" && (a = o({ ...a, saveUrl: g.saveUrl })), a = f(a) === "string" ? a : n(), i({ args: [ { name: "url", value: a, required: !0, types: ["string"] }, { name: "options", value: g, types: ["object"] } ] }), g.saveUrl && (e = a), await fetch(a, g).then(async (h) => { if (!h) throw Error( "APi responded with an error, no response object returned" ); const m = h.headers.get("content-type"), p = `content-type: ${m}, responseStatus: ${h.status}, responseUrl: ${h.url}`; if (!h.ok) throw Error(`APi response not ok, got ${p}`); if (!["application/json", "text/json"].some((q) => m == null ? void 0 : m.includes(q)) || typeof h.json != "function") throw Error(`API response not in JSON format, got ${p}`); const U = await h.json(); if (U) return U; throw Error(`VPIC API returned no data, got ${p}`); }).catch((h) => (h.message = `There was an error fetching API data: ${h.message}`, l(h)))); return { setCachedUrl: t, getCachedUrl: n, clearCachedUrl: r, createCachedUrl: o, createUrl: u, createPostBody: c, get: s, post: async (a, g = { saveUrl: !0 }) => (a && f(a) === "object" && (a = o({ ...a, saveUrl: g.saveUrl, includeQueryString: !1 })), a = f(a) === "string" ? a : n(), i({ args: [ { name: "url", value: a, required: !0, types: ["string"] }, { name: "options", value: g, types: ["object"] }, { name: "options.body", value: g.body, types: ["string"] } ] }), await s(a, { ...g, method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: c(g.body) })) }; }; export { E as DecodeVin, S as DecodeVinExtended, w as DecodeVinValues, Y as DecodeVinValuesBatch, D as DecodeVinValuesExtended, P as DecodeWMI, L as GetAllMakes, F as GetAllManufacturers, R as GetCanadianVehicleSpecifications, O as GetEquipmentPlantCodes, _ as GetMakeForManufacturer, x as GetMakesForManufacturerAndYear, W as GetMakesForVehicleType, B as GetManufacturerDetails, H as GetModelsForMake, Q as GetModelsForMakeId, J as GetModelsForMakeIdYear, X as GetModelsForMakeYear, K as GetParts, Z as GetVehicleTypesForMake, z as GetVehicleTypesForMakeId, ee as GetVehicleVariableList, te as GetVehicleVariableValuesList, re as GetWMIsForManufacturer, I as createQueryString, C as encodeQueryStringParams, A as isValidVin, d as useNHTSA }; //# sourceMappingURL=nhtsa-api-wrapper.mjs.map