


/**
 * Cloud Code test
 curl -X POST -H "Content-Type: application/json" -H 'X-Parse-Application-Id: edu-textbook' -d '{ "isbn": "9787302609865" }' http://127.0.0.1:61337/parse/functions/tbookISBN
 */


/**
 * 
 */
export function defineTbookISBN(){
    Parse.Cloud.define("tbookISBN", async (request) => {
        let isbn = request.params.isbn;
        if(isbn) {
            try{
                let url = "https://www.tbook.com.cn/api/api/public/index/search/book_isbn_info";
                let response = await fetch(url,{
                    method:"POST",
                    json:true,
                    body:JSON.stringify({isbn:isbn})
                });
                let text = await response?.text()
                try{
                    let result = JSON.parse(text)
                    // console.log(result)
                    if(result?.data?.[0]){
                        return result?.data?.[0];
                    }
                }catch(err){}
            }catch(err){
                console.error(err)
            }
        }
        throw new Parse.Error(404,"未找到该图书信息")
    },{
        fields : {
            isbn:{
                required:true
            }
        }
    });
}
  