UNPKG

@requestkit/node

Version:

Request Kit Node.js Client

2 lines 6.63 kB
import l from"ky";import{z as e}from"zod";var u=e.object({baseURL:e.string({invalid_type_error:"baseURL must be a string"}).url({message:"baseURL must be a valid URL"}).optional(),apiKey:e.string({invalid_type_error:"apiKey must be a string",required_error:"apiKey is required"}),retries:e.number().optional(),logging:e.object({enabled:e.coerce.boolean().default(!1).optional()}).optional()}),p=e.object({id:e.string(),title:e.string(),description:e.string(),createdAt:e.string(),upvotes:e.number(),comments:e.number(),isUpvoted:e.boolean().optional(),status:e.object({name:e.string(),color:e.string(),description:e.string().optional(),disableComments:e.boolean(),isPublic:e.boolean()}).optional(),requester:e.object({email:e.string().email().optional(),name:e.string().optional(),identifier:e.string()}).optional()}),d=e.object({id:e.string(),content:e.string(),createdAt:e.string(),commentor:e.object({email:e.string().email().optional(),identifier:e.string(),name:e.string().optional()})}),h=e.object({limit:e.number().min(1).max(100).default(10).optional(),page:e.number().min(1).default(1).optional(),includeRequester:e.boolean().default(!1).optional(),orderBy:e.array(e.enum(["date","upvotes"])).default(["date"]).optional(),order:e.enum(["asc","desc"]).default("desc").optional(),userIdentifier:e.string().optional()}),q=e.object({totalRequests:e.number(),requests:e.array(p)}),g=e.object({title:e.string({invalid_type_error:"title must be a string",required_error:"title is required"}),description:e.string({invalid_type_error:"description must be a string",required_error:"description is required"}),status:e.string().optional(),requester:e.object({identifier:e.string({invalid_type_error:"identifier must be a string",required_error:"identifier is required"}),name:e.string().optional(),email:e.string().email().optional()}).optional(),files:e.union([e.array(e.instanceof(Blob)),e.instanceof(Blob).transform(a=>[a])]).optional()}),y=e.object({id:e.string()}),R=e.object({user:e.object({identifier:e.string(),name:e.string().optional(),email:e.string().email().optional()}).optional()}),b=e.object({id:e.string()}),S=e.object({userIdentifier:e.string()}),w=e.object({includeRequester:e.boolean().default(!1).optional(),includeComments:e.boolean().default(!1).optional(),userIdentifier:e.string().optional()}),v=e.object({limit:e.number().min(1).max(100).default(10).optional(),page:e.number().min(1).default(1).optional()}),K=e.object({comments:e.array(d)}),j=e.object({content:e.string({invalid_type_error:"content must be a string",required_error:"content is required"}),commentor:e.object({identifier:e.string({invalid_type_error:"identifier must be a string",required_error:"identifier is required"}),name:e.string().optional(),email:e.string().email().optional()}),files:e.array(e.instanceof(Blob)).optional()}),C=e.object({id:e.string()}),x=e.object({userIdentifier:e.string()}),c=e.object({message:e.string(),code:e.string().optional(),details:e.record(e.unknown()).optional()});var n=class extends Error{constructor(r,t,o,i){super(r);this.code=t;this.details=o;this.status=i;this.name="RequestKitError"}},m=class{constructor(s){this.options=s;let r=u.parse(s);this.$fetch=l.create({prefixUrl:r.baseURL??"https://requestkit.dev/api/v1",headers:{"x-request-kit-key":r.apiKey},retry:{limit:r.retries??3,methods:["get","put","head","delete","options","trace"],statusCodes:[408,413,429,500,502,503,504],backoffLimit:1e3,delay:t=>t*1e3},hooks:{beforeRequest:[t=>(r.logging?.enabled&&console.log(`[RequestKit] ${t.method} ${t.url}`),t)],afterResponse:[(t,o,i)=>(r.logging?.enabled&&console.log(`[RequestKit] ${i.status} ${t.method} ${t.url}`),i)]}})}$fetch;async handleResponse(s){if(!s.ok){let r=await s.json().catch(()=>null);if(r&&c.safeParse(r).success){let t=r;throw new n(t.message,t.code,t.details,s.status)}throw new n(`HTTP error! status: ${s.status}`,void 0,void 0,s.status)}return s.json()}async getRequests(s){let r=s?Object.fromEntries(Object.entries({...s,"orderBy[]":s.orderBy?.join(","),orderBy:void 0}).filter(([t,o])=>o!==void 0)):void 0;try{let t=await this.$fetch("requests",{searchParams:r});return this.handleResponse(t)}catch(t){throw t instanceof n?t:new n(t instanceof Error?t.message:"Unknown error occurred")}}async createRequest(s){let r=new FormData;if(r.append("title",s.title),r.append("description",s.description),s.status&&r.append("status",s.status),s.requester&&(r.append("requester.identifier",s.requester.identifier),s.requester.name&&r.append("requester.name",s.requester.name),s.requester.email&&r.append("requester.email",s.requester.email)),s.files){let t=Array.isArray(s.files)?s.files:[s.files];for(let o of t)r.append("files[]",o)}try{let t=await this.$fetch("requests",{method:"POST",body:r});return this.handleResponse(t)}catch(t){throw t instanceof n?t:new n(t instanceof Error?t.message:"Unknown error occurred")}}async upvoteRequest(s,r){try{let t=await this.$fetch(`requests/${s}/upvote`,{method:"POST",json:r});return this.handleResponse(t)}catch(t){throw t instanceof n?t:new n(t instanceof Error?t.message:"Unknown error occurred")}}async removeUpvoteFromRequest(s,r){try{let t=await this.$fetch(`requests/${s}/upvote`,{method:"DELETE",searchParams:r});return this.handleResponse(t)}catch(t){throw t instanceof n?t:new n(t instanceof Error?t.message:"Unknown error occurred")}}async getRequest(s,r){try{let t=await this.$fetch(`requests/${s}`,{searchParams:r});return this.handleResponse(t)}catch(t){throw t instanceof n?t:new n(t instanceof Error?t.message:"Unknown error occurred")}}async getComments(s,r){try{let t=await this.$fetch(`requests/${s}/comments`,{searchParams:r});return this.handleResponse(t)}catch(t){throw t instanceof n?t:new n(t instanceof Error?t.message:"Unknown error occurred")}}async createComment(s,r){let t=new FormData;t.append("content",r.content),t.append("commentor.identifier",r.commentor.identifier),r.commentor.name&&t.append("commentor.name",r.commentor.name),r.commentor.email&&t.append("commentor.email",r.commentor.email),r.files&&(Array.isArray(r.files)?r.files:[r.files]).forEach(i=>{t.append("files",i)});try{let o=await this.$fetch(`requests/${s}/comments`,{method:"POST",body:t});return this.handleResponse(o)}catch(o){throw o instanceof n?o:new n(o instanceof Error?o.message:"Unknown error occurred")}}async deleteComment(s,r,t){try{let o=await this.$fetch(`requests/${s}/comments/${r}`,{method:"DELETE",json:t});return this.handleResponse(o)}catch(o){throw o instanceof n?o:new n(o instanceof Error?o.message:"Unknown error occurred")}}};export{m as RequestKit,n as RequestKitError}; //# sourceMappingURL=index.js.map