-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add status codes and event methods to log requests
This commit moves the logRequestLine function into a separate file and adds status codes and event methods. It grabs both get and post requests
- Loading branch information
1 parent
ef08e31
commit 4e3e92c
Showing
4 changed files
with
66 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {timestampDateFormat} from '../constants.js' | ||
import {Response as CliKitResponse} from '@shopify/cli-kit/node/http' | ||
import {outputContent, outputInfo, outputToken} from '@shopify/cli-kit/node/output' | ||
import {H3Event} from 'h3' | ||
|
||
const CHARACTER_TRUNCATION_LIMIT = 80 | ||
|
||
export function logRequestLine(event: H3Event, response: CliKitResponse | Response) { | ||
const truncatedPath = | ||
event.path.length > CHARACTER_TRUNCATION_LIMIT | ||
? `${event.path.substring(0, CHARACTER_TRUNCATION_LIMIT)}...` | ||
: event.path | ||
const serverTiming = response.headers.get('server-timing') | ||
const requestDuration = serverTiming?.match(/cfRequestDuration;dur=([\d.]+)/)?.[1] | ||
const durationString = requestDuration ? `${Math.round(Number(requestDuration))}ms` : '' | ||
|
||
const statusColor = getColorizeStatus(response.status) | ||
|
||
const eventMethodAligned = event.method.padStart(6) | ||
|
||
outputInfo( | ||
outputContent`• ${timestampDateFormat.format(new Date())} Request ${outputToken.raw( | ||
'»', | ||
)} ${eventMethodAligned} ${truncatedPath} ${statusColor(String(response.status))} ${outputToken.gray( | ||
`${durationString}`, | ||
)}`, | ||
) | ||
} | ||
|
||
function getColorizeStatus(status: number) { | ||
if (status < 300) { | ||
return outputToken.green | ||
} else if (status < 400) { | ||
return outputToken.yellow | ||
} else { | ||
return outputToken.errorText | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters