MCPcopy
hub / github.com/ThatGuySam/doesitarm / request

Function request

helpers/http.js:109–186  ·  view source on GitHub ↗
( input, options = {} )

Source from the content-addressed store, hash-verified

107}
108
109export async function request ( input, options = {} ) {
110 const config = toRequestConfig( input, options )
111 const {
112 attempts = 1,
113 cache,
114 credentials,
115 data,
116 delayMs = 1000,
117 headers: inputHeaders,
118 method: inputMethod = 'GET',
119 mode,
120 redirect,
121 responseType = 'json',
122 signal,
123 url
124 } = config
125 const method = inputMethod.toUpperCase()
126 const headers = createHeaders( inputHeaders )
127
128 let body
129 if ( data !== undefined ) {
130 body = JSON.stringify( data )
131
132 if ( !headers.has( 'Accept' ) ) {
133 headers.set( 'Accept', 'application/json' )
134 }
135
136 if ( !headers.has( 'Content-Type' ) ) {
137 headers.set( 'Content-Type', 'application/json' )
138 }
139 }
140
141 let lastError
142
143 for ( let attempt = 1; attempt <= attempts; attempt += 1 ) {
144 try {
145 const response = await fetch( url, buildRequestInit({
146 body,
147 cache,
148 credentials,
149 headers,
150 method,
151 mode,
152 redirect,
153 signal
154 }) )
155 const responseData = await parseResponseBody( response, responseType )
156
157 if ( !response.ok ) {
158 throw new HttpError(
159 `${ method } ${ url } failed with status ${ response.status }`,
160 {
161 data: responseData,
162 method,
163 status: response.status,
164 statusText: response.statusText,
165 url
166 }

Callers 5

getJsonFunction · 0.85
getTextFunction · 0.85
postJsonFunction · 0.85
requestJsonFunction · 0.85
headOkFunction · 0.85

Calls 8

toRequestConfigFunction · 0.85
createHeadersFunction · 0.85
buildRequestInitFunction · 0.85
parseResponseBodyFunction · 0.85
shouldRetryErrorFunction · 0.85
sleepFunction · 0.85
hasMethod · 0.80
setMethod · 0.80

Tested by

no test coverage detected