@gruzf/request
Version:
An easy way to create an http request for NextJS and more
2 lines (1 loc) • 953 B
JavaScript
import R,{HTTPError as T}from"ky";import j from"./error.js";import{getRequestOptions as w,isFormData as E}from"./utils/helpers.js";var _=(n,y)=>async(...m)=>{let[q,s]=m,[a,p]=q.split(":"),i=p?a:void 0,u=p||a,{cache:f,onDownloadProgress:l,next:g,signal:h,...t}=y||{},O=process.env.NEXT_PUBLIC_REQUEST_PAGE||"request",o;E(s)?(s.append("_source",n),s.append("_group",i||""),s.append("_query",u),s.append("_options",JSON.stringify(t)),o={body:s}):o={json:{source:n,group:i,query:u,variables:s,options:t}};try{return await R.post(`/api/${O}`,{...o,credentials:"include",cache:f,onDownloadProgress:l,next:g,signal:h,timeout:t.timeout}).json()}catch(e){if(e instanceof T){let r=await e.response.json(),d=r,{options:c}=w(o.body||o.json),{rawData:P}=typeof c=="object"?c:{};throw d=P?{...r,status:e.response.status,headers:e.response.headers}:{status:e.response.status,headers:e.response.headers,data:r},new j(d)}throw e}},x=_;export{x as default,_ as request};