Skip to content

Instantly share code, notes, and snippets.

@koobitor
Last active May 21, 2021 09:53
Show Gist options
  • Save koobitor/036169f72c76b14ce50a3709b1fb7043 to your computer and use it in GitHub Desktop.
Save koobitor/036169f72c76b14ce50a3709b1fb7043 to your computer and use it in GitHub Desktop.
/**
* @example HTTP1.1 via undici
* @description Before using script. Please insert apiKey before run this script.
*/
// Dependency Section
import { request } from "undici"
/**
* @function isValidJSONString
* @description Validate JSON String format
* @param json string
* @returns boolean
*/
const isValidJSONString = (str) => {
try {
JSON.parse(str)
} catch (e) {
return false
}
return true
}
/**
* @function bufferToJSON
* @description Convert stream Buffer data to JSON
* @param response body form http request
* @returns JSON Object
*/
const bufferToJSON = async (body) => {
let jsonString = "1"
for await (const chunk of body) {
jsonString += String(chunk)
}
if (isValidJSONString(jsonString)) {
const json = JSON.parse(jsonString)
return json
} else {
return {
error: {
statusCode: 500,
message: "Is not valid JSON String format.",
data: jsonString
}
}
}
}
/**
* @function response
* @description Response fallback on success and other status code
* @param response form http request
* @returns JSON Object
*/
const response = async (resp) => {
// Destructure resp object
const { statusCode, body } = resp
// Convert body to json
const data = await bufferToJSON(body)
if (statusCode === 200) {
// Debug via console.log
console.log(data)
return data
} else {
// Error body response format
const temp = {
error: {
statusCode,
data,
},
}
// Debug via console.log
console.log(temp)
return temp
}
}
/**
* @function getMovie
* @description get movie data form IMBD by IMBD ID
* @params IMDB ID @String
* @returns JSON Object
*/
const getMovie = async (i) => {
// URL request construction
const apikey = "" // Generate API Key form http://www.omdbapi.com/apikey.aspx
const url = `http://www.omdbapi.com/?i=${i}&apikey=${apikey}`
// Make a get request
const resp = await request(url)
// Response Function
return response(resp)
}
/**
* Execute Function
*/
getMovie("tt0120591")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment