mirror of
https://github.com/ThatGuySam/doesitarm.git
synced 2026-05-15 06:35:20 -07:00
Replace all in-scope axios callsites with a new helpers/http.js wrapper over native fetch, including JSON/text GET, JSON POST, HEAD checks, and transient 5xx retry behavior; update all browser, build, script, and proxy API clients to use it; add focused unit tests; and remove axios from package dependencies. Constraint: Preserve API/build and deployment behavior while lowering transport surface area. Rejected: inline fetch replacements at each callsite | rejected to avoid inconsistent error/retry semantics. Confidence: high Scope-risk: moderate Directive: Keep helper in place as the transport boundary and update tests when changing request semantics. Tested: pnpm run -s typecheck, pnpm -s run test-prebuild, pnpm -s run test, pnpm -s run test:browser, pnpm -s run netlify-build, smoke GETs on /apple-silicon-app-test and /apple-silicon-app-test/?version=2 Not-tested: branch/netlify deployment health in CI pipeline after merge
90 lines
2.6 KiB
JavaScript
90 lines
2.6 KiB
JavaScript
// Based on GitHub Proxy demo
|
|
// https://gist.github.com/v1vendi/75d5e5dad7a2d1ef3fcb48234e4528cb
|
|
|
|
// Example uses:
|
|
|
|
// DoesItAPI.get() // GET /api
|
|
// DoesItAPI.apps.get() // GET /api/apps.json
|
|
// DoesItAPI.apps(7).get() // GET /api/apps/7.json
|
|
// DoesItAPI.apps(7).whatever.delete() // DELETE /api/apps/7/whatever.json
|
|
// DoesItAPI.apps.put({ whatever: 1 })
|
|
|
|
// GET /api/tiles/public/static/3/4/2.json?turn=37038&games=wot
|
|
// DoesItAPI.tiles.public.static(3)(4)(`${2}.json`).get({ turn: 37, games: 'wot' })
|
|
|
|
import { getApiUrl } from '~/helpers/url.js'
|
|
import { requestJson } from '~/helpers/http.js'
|
|
|
|
// Use msw
|
|
import '~/test/msw/use.js'
|
|
|
|
// const defaultFetchMethod = (...args) => console.log(...args) // mock
|
|
|
|
const defaultFetchMethod = async function (...args) {
|
|
return requestJson(...args)
|
|
.catch( error => {
|
|
if ( error?.response?.status !== 404 ) {
|
|
console.error( error )
|
|
}
|
|
|
|
throw error
|
|
})
|
|
}
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
|
|
const HTTP_METHODS = [
|
|
'GET',
|
|
// 'POST',
|
|
// 'PUT',
|
|
// 'DELETE',
|
|
// 'PATCH'
|
|
]
|
|
|
|
const apiBaseUrl = `${ getApiUrl().replace(/\/$/, '') }/api`
|
|
|
|
|
|
export function generateAPI ( {
|
|
apiUrl = apiBaseUrl,
|
|
fetchMethod = defaultFetchMethod
|
|
} = {} ) {
|
|
|
|
// console.log( 'apiUrl', apiUrl )
|
|
|
|
// a hack, so we can use field either as property or a method
|
|
const callable = () => {}
|
|
callable.url = apiUrl
|
|
|
|
return new Proxy(callable, {
|
|
get({ url }, propKey) {
|
|
// If we're just getting the url, return it
|
|
if ( propKey === 'url' ) return `${ url }.json`
|
|
|
|
// If we're using an HTTP method
|
|
// then do a request to the url
|
|
if ( HTTP_METHODS.includes(propKey.toUpperCase()) ) {
|
|
return (data) => fetchMethod({
|
|
url: `${ url }.json`,
|
|
method: propKey.toUpperCase(),
|
|
data
|
|
})
|
|
}
|
|
|
|
// Otherwise drill down to the next property
|
|
// Example:
|
|
// From DoesItAPI.kind...
|
|
// To DoesItAPI.kind.apps...
|
|
return generateAPI({ apiUrl: `${url}/${propKey}` })
|
|
|
|
},
|
|
// Handles when () goes after a property key
|
|
// Example: DoesItAPI() or DoesItAPI.app()
|
|
// Proxy.handler.apply - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/apply
|
|
apply({ url }, thisArg, [arg] = []) {
|
|
const apiUrl = arg ? `${url}/${arg}` : url
|
|
return generateAPI({ apiUrl: apiUrl })
|
|
}
|
|
})
|
|
|
|
}
|
|
|
|
export const DoesItAPI = generateAPI()
|