A new array containing all supported HTTP methods
// Get methods for modification
const methods = getHttpMethods();
methods.push('CUSTOM'); // Safe to modify
console.log(methods); // ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS', 'CUSTOM']
// Original list is unchanged
console.log(HTTP_METHODS_LIST); // ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS']
// Use in API responses
function getSupportedMethods() {
return {
methods: getHttpMethods(),
count: HTTP_METHODS_LIST.length
};
}
// Filter methods for specific use case
function getReadMethods() {
const allMethods = getHttpMethods();
return allMethods.filter(method => ['GET', 'HEAD', 'OPTIONS'].includes(method));
}
// Pass to functions that might modify the array
function processMethods(methods) {
methods.sort(); // Safe because we passed a copy
return methods;
}
const sortedMethods = processMethods(getHttpMethods());
Returns a copy of all supported HTTP methods as an array.
This function returns a new array containing all supported HTTP methods. The returned array is a shallow copy, so modifications to it won't affect the original HTTP_METHODS_LIST.
Use Cases:
Performance Note: This creates a new array on each call. For high-performance scenarios where you need the methods frequently, consider caching the result or using HTTP_METHODS_LIST directly if you don't need a copy.