PT.js 948 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import https from "https";
  2. export const getPT = async (params) => {
  3. try {
  4. const controller = new AbortController();
  5. const timeoutId = setTimeout(() => controller.abort(), 10000);
  6. const httpsAgent = new https.Agent({
  7. rejectUnauthorized: false,
  8. });
  9. let url = "https://api.kemdikbud.go.id:8243/pddikti/1.2/pt";
  10. if (params.id) {
  11. url += `/${params.id}`;
  12. } else if (params.search) {
  13. url += `?q=${params.search}`;
  14. }
  15. const res = await fetch(url, {
  16. method: "GET",
  17. headers: new Headers({
  18. Authorization: "Bearer 5b62f743-eef2-3370-8c66-6951b2e9c2c5",
  19. Accept: "application/json",
  20. "Access-Control-Allow-Origin": "*",
  21. }),
  22. agent: httpsAgent,
  23. signal: controller.signal,
  24. });
  25. if (!res.ok) {
  26. throw new Error(`Error! status: ${response.status}`);
  27. }
  28. clearTimeout(timeoutId);
  29. const result = await res.json();
  30. return result;
  31. } catch (error) {
  32. console.log("error", error);
  33. return false;
  34. }
  35. };