| Conditions | 4 |
| Total Lines | 54 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import axios from "axios"; |
||
| 5 | |||
| 6 | // https://autosys-kjoretoy-api.atlas.vegvesen.no/api-ui/index-enkeltoppslag.html |
||
| 7 | |||
| 8 | export default async function handler( |
||
| 9 | req: VercelRequest, |
||
| 10 | res: VercelResponse |
||
| 11 | ): Promise<void> { |
||
| 12 | const { regNummer = "" } = req.query; |
||
| 13 | |||
| 14 | if (regNummer === undefined) { |
||
| 15 | res.status(400).json({ error: "Mangler regNummer parameter" }); |
||
| 16 | return; |
||
| 17 | } |
||
| 18 | |||
| 19 | const urlToFetch = `https://www.vegvesen.no/ws/no/vegvesen/kjoretoy/felles/datautlevering/enkeltoppslag/kjoretoydata?kjennemerke=${regNummer}`; |
||
| 20 | |||
| 21 | try { |
||
| 22 | const response = await axios.get<IStatensVegvesenFullData>(urlToFetch, { |
||
| 23 | headers: { |
||
| 24 | "SVV-Authorization": `Apikey ${process.env.SVV_API_KEY}`, |
||
| 25 | "X-Client-Identifier": "my-app", |
||
| 26 | }, |
||
| 27 | }); |
||
| 28 | |||
| 29 | if (response.status === 200) { |
||
| 30 | const { |
||
| 31 | kjoretoydataListe: [ |
||
| 32 | { |
||
| 33 | kjoretoyId: { kjennemerke }, |
||
| 34 | forstegangsregistrering: { |
||
| 35 | registrertForstegangNorgeDato: forstegangsregistrering, |
||
| 36 | }, |
||
| 37 | periodiskKjoretoyKontroll: { sistGodkjent: sistKontrollert }, |
||
| 38 | }, |
||
| 39 | ], |
||
| 40 | } = response.data; |
||
| 41 | |||
| 42 | console.log("response.data: ", response.data) |
||
| 43 | |||
| 44 | const sanitizedData = { |
||
| 45 | kjennemerke: sanitize(kjennemerke), |
||
| 46 | //forstegangsregistrering: sanitize(forstegangsregistrering.toString()), |
||
| 47 | forstegangsregistrering: sanitize(forstegangsregistrering), |
||
| 48 | //sistKontrollert: sanitize(sistKontrollert.toString()), |
||
| 49 | sistKontrollert: sanitize(sistKontrollert), |
||
| 50 | }; |
||
| 51 | |||
| 52 | res.status(200).json(sanitizedData); |
||
| 53 | } else { |
||
| 54 | res.status(500).json({ error: `Feil under henting av data - ${response}` }); |
||
| 55 | } |
||
| 56 | } catch (error) { |
||
| 57 | console.error(error); |
||
| 58 | res.status(500).json({ error: `Feil under henting av data - ${error}` }); |
||
| 59 | } |
||
| 61 |