11/** @fileoverview Common functions used by multiple parts of the build process */
22
3- import { AxiosError , type AxiosResponse } from "axios" ;
4-
53import type { WcagItem } from "./guidelines" ;
64
75/** Generates an ID for heading permalinks. Equivalent to wcag:generate-id in base.xslt. */
@@ -17,10 +15,7 @@ export function generateId(title: string) {
1715export const resolveDecimalVersion = ( version : `${number } `) => version . split ( "" ) . join ( "." ) ;
1816
1917/** Sort function for ordering WCAG principle/guideline/SC numbers ascending */
20- export function wcagSort (
21- a : WcagItem ,
22- b : WcagItem
23- ) {
18+ export function wcagSort ( a : WcagItem , b : WcagItem ) {
2419 const aParts = a . num . split ( "." ) . map ( ( n ) => + n ) ;
2520 const bParts = b . num . split ( "." ) . map ( ( n ) => + n ) ;
2621
@@ -31,22 +26,32 @@ export function wcagSort(
3126 return 0 ;
3227}
3328
29+ type FetchText = ( ...args : Parameters < typeof fetch > ) => Promise < string > ;
30+
31+ /** Performs a fetch, returning body text or throwing an error if a 4xx/5xx response occurs. */
32+ export const fetchText : FetchText = ( input , init ) =>
33+ fetch ( input , init ) . then (
34+ ( response ) => {
35+ if ( response . status >= 400 )
36+ throw new Error ( `fetching ${ input } yielded ${ response . status } response` ) ;
37+ return response . text ( ) ;
38+ } ,
39+ ( error ) => {
40+ throw new Error ( `fetching ${ input } yielded error: ${ error . message } ` ) ;
41+ }
42+ ) ;
43+
3444/**
35- * Handles HTTP error responses from Axios requests in local dev;
36- * re- throws error during builds to fail loudly.
37- * This should only be used for non-critical requests that can tolerate null data
45+ * Performs a fetch that ignores errors in local dev;
46+ * throws during builds to fail loudly.
47+ * This should only be used for non-critical requests that can tolerate no data
3848 * without major side effects.
3949 */
40- export const wrapAxiosRequest = < T , D > ( promise : Promise < AxiosResponse < T , D > > ) =>
41- promise . catch ( ( error ) => {
42- if ( ! ( error instanceof AxiosError ) || ! error . response || ! error . request ) throw error ;
43- const { response, request } = error ;
44- console . warn (
45- `AxiosError: status ${ response . status } received from ${
46- request . protocol + "//" + request . host
47- } ${ request . path || "" } `
48- ) ;
49-
50- if ( process . env . ELEVENTY_RUN_MODE === "build" ) throw error ;
51- else return { data : null } ;
52- } ) ;
50+ export const fetchOptionalText : FetchText =
51+ process . env . ELEVENTY_RUN_MODE === "build"
52+ ? fetchText
53+ : ( input , init ) =>
54+ fetchText ( input , init ) . catch ( ( error ) => {
55+ console . warn ( "Bypassing fetch error:" , error . message ) ;
56+ return "" ;
57+ } ) ;
0 commit comments