Skip to content

Commit

Permalink
Merge pull request #237 from w3bdesign/develop
Browse files Browse the repository at this point in the history
Test nytt api
  • Loading branch information
w3bdesign authored Apr 8, 2023
2 parents 99706e7 + 65c05c9 commit b653cfd
Show file tree
Hide file tree
Showing 2 changed files with 304 additions and 82 deletions.
38 changes: 25 additions & 13 deletions api/getRegNummer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,54 @@ import { sanitize } from "isomorphic-dompurify";
import { VercelRequest, VercelResponse } from "@vercel/node";
import { IStatensVegvesenFullData } from "../scripts/types/typeDefinitions";

export default async function getRegNummer(
// https://autosys-kjoretoy-api.atlas.vegvesen.no/api-ui/index-enkeltoppslag.html

export default async function handler(
req: VercelRequest,
res: VercelResponse
): Promise<void> {
const { regNummer = "" } = req.query;

if (regNummer === undefined) {
res.send({ error: "Mangler regNummer parameter" });
res.status(400).json({ error: "Mangler regNummer parameter" });
return;
}

const urlToFetch = `https://www.vegvesen.no/ws/no/vegvesen/kjoretoy/kjoretoyoppslag/v1/kjennemerkeoppslag/kjoretoy/${regNummer}`;
const urlToFetch = `https://www.vegvesen.no/ws/no/vegvesen/kjoretoy/felles/datautlevering/enkeltoppslag/kjoretoydata?kjennemerke=${regNummer}`;

try {
const response = await axios.get<IStatensVegvesenFullData>(urlToFetch);
const response = await axios.get<IStatensVegvesenFullData>(urlToFetch, {
headers: {
"SVV-Authorization": `Apikey ${process.env.SVV_API_KEY}`,
"X-Client-Identifier": "my-app",
},
});

if (response.status === 200) {
const {
kjennemerke,
registrering: { forstegangsregistrering, forstegangsregistreringEier },
periodiskKjoretoykontroll: { sistKontrollert },
kjoretoydataListe: [
{
kjoretoyId: { kjennemerke },
forstegangsregistrering: {
registrertForstegangNorgeDato: forstegangsregistrering,
},
periodiskKjoretoyKontroll: { sistGodkjent: sistKontrollert },
},
],
} = response.data;

const sanitizedData = {
kjennemerke: sanitize(kjennemerke),
forstegangsregistreringEier: sanitize(forstegangsregistreringEier),
forstegangsregistrering: sanitize(forstegangsregistrering),
sistKontrollert: sanitize(sistKontrollert),
forstegangsregistrering: sanitize(forstegangsregistrering.toString()),
sistKontrollert: sanitize(sistKontrollert.toString()),
};

res.send(sanitizedData);
res.status(200).json(sanitizedData);
} else {
res.send({ error: "Feil under henting av data" });
res.status(500).json({ error: "Feil under henting av data " });
}
} catch (error) {
console.error(error);
res.send({ error: "Feil under henting av data" });
res.status(500).json({ error: "Feil under henting av data" });
}
}
Loading

1 comment on commit b653cfd

@vercel
Copy link

@vercel vercel bot commented on b653cfd Apr 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.