-
Notifications
You must be signed in to change notification settings - Fork 8
/
GetModelsForMakeIdYear.ts
157 lines (145 loc) · 4.98 KB
/
GetModelsForMakeIdYear.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* @module api/endpoints/GetModelsForMakeIdYear
* @category API Endpoints
*/
import { useNHTSA } from '@/api'
import {
catchInvalidArguments,
encodeQueryStringParams,
rejectWithError,
} from '@/utils'
import type { IArgToValidate, NhtsaResponse, AtLeastOne } from '@/types'
/**
* ::: tip :bulb: More Information
* See: [GetModelsForMakeIdYear Documentation](/api/endpoints/get-models-for-make-id-year)
* :::
*
* `GetModelsForMakeIdYear` returns the Models in the vPIC dataset for a specified Model Year
* and Make whose name is LIKE the Make in the vPIC Dataset.
*
* `params.makeId` is an integer and is **required**.
*
* A minimum of one of the following are also **required** (or a combination of both):
* - `params.modelYear` year you want to search for (years >= 1995 are supported according to NHTSA
* docs)
* - `params.vehicleType` can be a partial name, or a full name for more specificity, e.g.,
* "Vehicle", "Moto", "Low Speed Vehicle", etc.
*
* You can get `makeID`s via `MAKE_ID` key in Results objects of the following endpoints:
* - `GetAllMakes` endpoint
* - `GetMakeForManufacturer` endpoint
* - `GetModelsForMake` endpoint
* - `GetModelsForMakeYear` endpoint
*
* You can get `makeID`s via `MakeID` key in Results objects of the following endpoints:
* - `DecodeVinValues`
* - `DecodeVinValuesBatch`
*
* You can get `makeID`s via `ValueId` key in Results objects of the following endpoints.
* One of the objects in the `Results` array will contain both `Variable: "Make"` and
* `VariableId: 26`. The `ValueId` key in that same object is the `makeID` for use in this
* endpoint.
* - `DecodeVin`
* - `DecodeVinExtended`
*
* _NOTE:_ This endpoint requires special handling of the params object, such that none of the
* params are used in the query string and are instead used as part of the URL path for the
* endpoint. To account for this, we pass the params object to the `createUrl` function as the
* `path`, after encoding the params object key:values into a url path string.
*
* @param params - Object of Query Search names and values to append to the URL as a query string
* @param {(string|number)} params.makeId - Make ID to search
* @param {(string|number)} [params.modelYear] - A number representing the model year to search
* @param {string} [params.vehicleType] - String representing the vehicle type to search
* @param {boolean} [doFetch=true] - Whether to fetch the data or just return the URL
* (default: `true`)
* @returns {(Promise<NhtsaResponse<GetModelsForMakeIdYearResults> | string>)} - Api Response
* `object` -or- url `string` if `doFetch = false`
*/
function GetModelsForMakeIdYear(
params: {
makeId: string | number
} & AtLeastOne<{
modelYear?: string | number
vehicleType?: string
}>,
doFetch?: true
): Promise<NhtsaResponse<GetModelsForMakeIdYearResults>>
function GetModelsForMakeIdYear(
params: {
makeId: string | number
} & AtLeastOne<{
modelYear?: string | number
vehicleType?: string
}>,
doFetch: false
): Promise<string>
async function GetModelsForMakeIdYear(
params: {
makeId: string | number
} & AtLeastOne<{
modelYear?: string | number
vehicleType?: string
}>,
doFetch = true
): Promise<NhtsaResponse<GetModelsForMakeIdYearResults> | string> {
const endpointName = 'GetModelsForMakeIdYear'
try {
/* Validate the arguments */
const atLeastOne: IArgToValidate[] = [
{
name: 'modelYear',
value: params.modelYear,
types: ['string', 'number'],
},
{
name: 'vehicleType',
value: params.vehicleType,
types: ['string'],
},
]
const args: IArgToValidate[] = [
{ name: 'params', value: params, required: true, types: ['object'] },
{
name: 'makeId',
value: params.makeId,
required: true,
types: ['string', 'number'],
},
...atLeastOne,
]
catchInvalidArguments({ args })
catchInvalidArguments({ args: atLeastOne, mode: 'atLeast' })
/*
* Params for this endpoint are not part of the query string, instead they are part of the URL
* path. This means params are never run through createQueryString() and won't be URI component
* encoded without this.
*/
const { makeId, modelYear, vehicleType } = encodeQueryStringParams(params)
/* Build the URL */
let path = `makeId/${makeId}/`
path += modelYear ? `modelYear/${modelYear}` : ''
path += vehicleType
? `${modelYear ? '/' : ''}vehicleType/${vehicleType}`
: ''
const { get, createCachedUrl, getCachedUrl } = useNHTSA()
createCachedUrl({ endpointName, path })
if (!doFetch) {
return getCachedUrl()
} else {
return get()
}
} catch (error) {
return rejectWithError(error)
}
}
export { GetModelsForMakeIdYear }
/**
* Objects found in the `Results` array of `GetModelsForMakeIdYear` endpoint response.
*/
export type GetModelsForMakeIdYearResults = {
Make_ID: number
Make_Name: string
Model_ID: number
Model_Name: string
}