@shaggytools/nhtsa-api-wrapper
Version:
Universal javascript wrapper for the NHTSA.dot.gov VPIC 'vehicles' API, useful for VIN decoding, etc.
3 lines (2 loc) • 13.7 kB
JavaScript
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const U=({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 a="";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 u=r?`<${r.join(" | ")}>`:"";if(n&&!r?t||(a=`${s} is required, ${y}`):r&&!n?t!==void 0&&!r.includes(c)&&(a=`${s} must be of type(s) ${u}, ${y}`):n&&r&&(!t||!r.includes(c))&&(a=`${s} is required and must be of type(s) ${u}, ${y}`),a.length){if(o==="boolean")return!1;throw Error(a)}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=>U(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},q=e=>f(e)==="error",I=e=>{let t="an unknown error occurred.";return q(e)?(e.message||(e.message=t),e):(f(e)==="string"&&(t=e),Error(t))},l=async e=>Promise.reject(I(e)),f=e=>{const t=Object.prototype.toString.call(e).toLowerCase();return t.slice(8,t.length-1)},N={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},$=[8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2];function j(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((a,c)=>{let s;isNaN(parseInt(a))?s=N[a]:s=parseInt(a);const y=$[c];return s*y}).reduce((a,c)=>a+c,0)%11===r}const A="https://vpic.nhtsa.dot.gov/api/vehicles",G="json",M=e=>(U({name:"params",value:e,required:!0,types:["object"]}),Object.entries(e).filter(([n,r])=>U({name:n,types:["string","number","boolean"],value:r,errorMode:"boolean"})).reduce((n,[r,o])=>(n[r]=encodeURIComponent(o),n),{})),k=(e={},t=!1)=>{U({name:"params",value:e,types:["object"]});const n=M({...e,format:G});return"?"+Object.entries(n).map(([r,o],a,c)=>o.length||t&&o===""?`${r}=${o}${a<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:a,createCachedUrl:c,getCachedUrl:s}=d();return c({endpointName:r,path:e,params:t}),n?a():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:a,createCachedUrl:c,getCachedUrl:s}=d();return c({endpointName:r,path:e,params:t}),n?a():s()}catch(o){return l(o)}}async function D(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:a,createCachedUrl:c,getCachedUrl:s}=d();return c({endpointName:r,path:e,params:t}),n?a():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:a,getCachedUrl:c}=d();return a({endpointName:n,includeQueryString:!1}),t?o(c(),{body:e}):c()}catch(r){return l(r)}}async function F(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:a,createCachedUrl:c,getCachedUrl:s}=d();return c({endpointName:r,path:e,params:t}),n?a():s()}catch(o){return l(o)}}async function w(e,t=!0){const n="DecodeWMI";try{i({args:[{name:"WMI",value:e,required:!0,types:["string"]}]});const{get:o,createCachedUrl:a,getCachedUrl:c}=d();return a({endpointName:n,path:e}),t?o():c()}catch(r){return l(r)}}async function P(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 L(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:a,getCachedUrl:c}=d();return a({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:a,getCachedUrl:c}=d();return a({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:a,getCachedUrl:c}=d();return a({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:a,getCachedUrl:c}=d();return a({endpointName:n,path:e.toString()}),t?o():c()}catch(r){return l(r)}}async function W(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:a,createCachedUrl:c,getCachedUrl:s}=d();return c({endpointName:r,path:e.toString(),params:t}),n?a():s()}catch(o){return l(o)}}async function x(e,t=!0){const n="GetMakesForVehicleType";try{i({args:[{name:"typeName",value:e,required:!0,types:["string"]}]});const{get:o,createCachedUrl:a,getCachedUrl:c}=d();return a({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:a,getCachedUrl:c}=d();return a({endpointName:n,path:e.toString()}),t?o():c()}catch(r){return l(r)}}async function Q(e,t=!0){const n="GetModelsForMake";try{i({args:[{name:"makeName",value:e,required:!0,types:["string"]}]});const{get:o,createCachedUrl:a,getCachedUrl:c}=d();return a({endpointName:n,path:e}),t?o():c()}catch(r){return l(r)}}async function H(e,t=!0){const n="GetModelsForMakeId";try{i({args:[{name:"makeId",value:e,required:!0,types:["string","number"]}]});const{get:o,createCachedUrl:a,getCachedUrl:c}=d();return a({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:a,modelYear:c,vehicleType:s}=M(e);let y=`makeId/${a}/`;y+=c?`modelYear/${c}`:"",y+=s?`${c?"/":""}vehicleType/${s}`:"";const{get:u,createCachedUrl:g,getCachedUrl:v}=d();return g({endpointName:n,path:y}),t?u():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:a,modelYear:c,vehicleType:s}=M(e);let y=`make/${a}/`;y+=c?`modelYear/${c}`:"",y+=s?`${c?"/":""}vehicleType/${s}`:"";const{get:u,createCachedUrl:g,getCachedUrl:v}=d();return g({endpointName:n,path:y}),t?u():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:a,getCachedUrl:c}=d();return a({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:a,getCachedUrl:c}=d();return a({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:a,getCachedUrl:c}=d();return a({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:a,getCachedUrl:c}=d();return a({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 a=e!=null&&e.manufacturer?encodeURIComponent(e.manufacturer):"",c=(e==null?void 0:e.vehicleType)||"",{get:s,createCachedUrl:y,getCachedUrl:u}=d();return y({endpointName:n,path:a,params:{vehicleType:c}}),t?s():u()}catch(r){return l(r)}}const d=()=>{let e;const t=u=>e=u,n=()=>e,r=()=>e="",o=u=>{if(typeof u=="string")return t(u),u;const{endpointName:g,allowEmptyParams:v=!1,includeQueryString:h=!0,path:m="",params:p,saveUrl:C=!0}=u;if(!g)throw Error("Endpoint name is required to create a VPIC URL string");const V=h?k(p,v):"",b=encodeURI(`${A}/${g}/${m}${V}`);return C&&t(b),b},a=u=>o({...u,saveUrl:!1}),c=u=>encodeURI(`DATA=${u?u+"&":""}format=${G}`),s=async(u,g={saveUrl:!0,method:"GET"})=>(u&&f(u)==="object"&&(u=o({...u,saveUrl:g.saveUrl})),u=f(u)==="string"?u:n(),i({args:[{name:"url",value:u,required:!0,types:["string"]},{name:"options",value:g,types:["object"]}]}),g.saveUrl&&(e=u),await fetch(u,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(T=>m==null?void 0:m.includes(T))||typeof h.json!="function")throw Error(`API response not in JSON format, got ${p}`);const b=await h.json();if(b)return b;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:a,createPostBody:c,get:s,post:async(u,g={saveUrl:!0})=>(u&&f(u)==="object"&&(u=o({...u,saveUrl:g.saveUrl,includeQueryString:!1})),u=f(u)==="string"?u:n(),i({args:[{name:"url",value:u,required:!0,types:["string"]},{name:"options",value:g,types:["object"]},{name:"options.body",value:g.body,types:["string"]}]}),await s(u,{...g,method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:c(g.body)}))}};exports.DecodeVin=E;exports.DecodeVinExtended=S;exports.DecodeVinValues=D;exports.DecodeVinValuesBatch=Y;exports.DecodeVinValuesExtended=F;exports.DecodeWMI=w;exports.GetAllMakes=P;exports.GetAllManufacturers=L;exports.GetCanadianVehicleSpecifications=R;exports.GetEquipmentPlantCodes=O;exports.GetMakeForManufacturer=_;exports.GetMakesForManufacturerAndYear=W;exports.GetMakesForVehicleType=x;exports.GetManufacturerDetails=B;exports.GetModelsForMake=Q;exports.GetModelsForMakeId=H;exports.GetModelsForMakeIdYear=J;exports.GetModelsForMakeYear=X;exports.GetParts=K;exports.GetVehicleTypesForMake=Z;exports.GetVehicleTypesForMakeId=z;exports.GetVehicleVariableList=ee;exports.GetVehicleVariableValuesList=te;exports.GetWMIsForManufacturer=re;exports.createQueryString=k;exports.encodeQueryStringParams=M;exports.isValidVin=j;exports.useNHTSA=d;
//# sourceMappingURL=nhtsa-api-wrapper.cjs.map