Skip to content

Commit

Permalink
feat(ui): Script > Post Request > add method to get response status c…
Browse files Browse the repository at this point in the history
…ode (#264)
  • Loading branch information
kobenguyent committed Sep 30, 2024
1 parent b51a080 commit b8ee500
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/ui/src/components/SnippetDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ export default defineComponent ({
type: 'option',
label: 'Returns the response body as a parsed JSON object',
value: 'rf.response.getBodyJSON()'
},
{
type: 'option',
label: 'Returns the status code of the response',
value: 'rf.response.getStatusCode()'
}
]
Expand Down
5 changes: 5 additions & 0 deletions packages/ui/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ export default {
label: 'rf.response.getBodyJSON()',
type: 'function',
info: 'Returns the response body as a parsed JSON object'
},
{
label: 'rf.response.getStatusCode()',
type: 'function',
info: 'Returns the status code of the response',
}
],
},
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,8 @@ export function scriptConversion(scriptToConvert: string, scriptType: 'postmanTo
postmanToRestfox: {
'pm.environment.set': 'rf.setEnvVar',
'pm.environment.get': 'rf.getEnvVar',
'pm.response.json()': 'rf.response.getBodyJSON()'
'pm.response.json()': 'rf.response.getBodyJSON()',
'pm.response.code': 'rf.response.getStatusCode()'
},
restfoxToPostman: {
'rf.setEnvVar': 'pm.environment.set',
Expand Down
4 changes: 4 additions & 0 deletions packages/ui/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export async function createRequestContextForPlugin(cacheId: string, request: Co
export function createResponseContextForPlugin(response: RequestFinalResponse, environment: any, setEnvironmentVariable: (name: string, value: string) => void, testResults: PluginTestResult[]): { expose: PluginExpose } {
let bufferCopy = response.buffer.slice(0)
const headers = response.headers
const statusCode = response.status

const generalContextMethods = {
...generalContextMethodsBase,
Expand Down Expand Up @@ -216,6 +217,9 @@ export function createResponseContextForPlugin(response: RequestFinalResponse, e
const header = headers.find((header: RequestInitialResponseHeader) => header[0].toLowerCase() == headerName.toLowerCase())
return header ? header[1] : undefined
},
getStatusCode() {
return statusCode
},
// deprecated but won't be removed
getEnvironmentVariable: generalContextMethods.getEnvVar,
setEnvironmentVariable: generalContextMethods.setEnvVar,
Expand Down

0 comments on commit b8ee500

Please sign in to comment.