Skip to content

Commit

Permalink
refactor: move extractHttpMethod function
Browse files Browse the repository at this point in the history
  • Loading branch information
anoack93 committed Sep 18, 2023
1 parent ed21148 commit c1927d1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
11 changes: 1 addition & 10 deletions src/serve-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import chalk from 'chalk'
import { resolve, sep } from 'path'
import Ajv from 'ajv'
import { mockFileTypes } from './mock-file-types.js'
import { HttpMethod } from './utilities/http-method.js'
import { extractHttpMethod, HttpMethod } from './utilities/http-method.js'
import { runInNewContext } from 'vm'

const ajv = new Ajv()
Expand All @@ -15,15 +15,6 @@ const ajv = new Ajv()
* @param {object} mapping
* @return {string}
*/
function extractHttpMethod (mapping) {
const supportedMethods = Object.values(HttpMethod)

const potentialMethod = mapping.split('.').reduce(
(_, current, index, array) => index === array.length - 1 ? current : 'none'
)

return supportedMethods.includes(potentialMethod) ? potentialMethod : HttpMethod.GET
}

// String which will be replaced by '/' in api endpoint
// this is being used for directories which have the same name as a file like /test.jpg/medium
Expand Down
14 changes: 14 additions & 0 deletions src/utilities/http-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,17 @@ export const HttpMethod = {
GET: 'get',
POST: 'post',
}

/**
* @param {object} mapping
* @return {HttpMethod}
*/
export function extractHttpMethod (mapping) {
const supportedMethods = Object.values(HttpMethod)

const potentialMethod = mapping.split('.').reduce(
(_, current, index, array) => index === array.length - 1 ? current : 'none'
)

return supportedMethods.includes(potentialMethod) ? potentialMethod : HttpMethod.GET
}

0 comments on commit c1927d1

Please sign in to comment.