-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(assert): add isNetworkPort, isHttpMethod (#1157)
* fix(assert): isNumberSafeInt * feat: add http method and network port guards
- Loading branch information
1 parent
0939128
commit 0d3b113
Showing
22 changed files
with
361 additions
and
11 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,5 @@ | ||
--- | ||
"@httpx/assert": patch | ||
--- | ||
|
||
Fix isPlainObject when testing Object.create(null) |
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,5 @@ | ||
--- | ||
"@httpx/assert": minor | ||
--- | ||
|
||
Add network port and http methods typeguard and assertions |
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,5 @@ | ||
--- | ||
"@httpx/assert": patch | ||
--- | ||
|
||
Fix isNumberSafeInt return |
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
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 { assertHttpMethod, assertHttpValidMethod } from '../http.asserts'; | ||
|
||
describe('http assertions tests', () => { | ||
describe('assertHttpValidMethod', () => { | ||
it('should not throw when given value is a valid http method', () => { | ||
expect(() => assertHttpValidMethod('get')).not.toThrow(); | ||
expect(() => assertHttpValidMethod('GET')).not.toThrow(); | ||
}); | ||
it('should throw when not a valid http method', () => { | ||
expect(() => assertHttpValidMethod('glue')).toThrow( | ||
new TypeError( | ||
'Value is expected to be an http method, got: string(length:4)' | ||
) | ||
); | ||
}); | ||
it('should throw custom error when value is invalid', () => { | ||
const e = new Error('cool'); | ||
expect(() => assertHttpValidMethod([], () => e)).toThrow(e); | ||
}); | ||
}); | ||
describe('assertHttpMethod', () => { | ||
it('should not throw when given value is a valid http method', () => { | ||
expect(() => assertHttpMethod('GET', 'get')).not.toThrow(); | ||
expect(() => assertHttpMethod('POST', 'POST')).not.toThrow(); | ||
}); | ||
it('should throw when not a valid http method', () => { | ||
expect(() => assertHttpMethod('GET', 'glue')).toThrow( | ||
new TypeError( | ||
"Value is expected to be an http 'GET' method, got: string(length:4)" | ||
) | ||
); | ||
}); | ||
it('should throw custom error when value is invalid', () => { | ||
const e = new Error('cool'); | ||
expect(() => assertHttpMethod('GET', [], () => e)).toThrow(e); | ||
}); | ||
}); | ||
}); |
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,49 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { isHttpMethod, isHttpValidMethod } from '../http.guards'; | ||
import type { HttpMethod } from '../http.types'; | ||
|
||
describe('Http typeguards tests', () => { | ||
describe('isHttpValidMethod', () => { | ||
it.each([ | ||
[false, null], | ||
[false, ''], | ||
[false, 'GETT'], | ||
[false, []], | ||
[false, BigInt(10)], | ||
[false, new Date()], | ||
[true, 'GET'], | ||
[true, 'PUT'], | ||
[true, 'POST'], | ||
[true, 'TRACE'], | ||
[true, 'DELETE'], | ||
[true, 'HEAD'], | ||
[true, 'OPTIONS'], | ||
[true, 'CONNECT'], | ||
[true, 'get'], | ||
[true, 'put'], | ||
[true, 'post'], | ||
[true, 'trace'], | ||
[true, 'delete'], | ||
[true, 'head'], | ||
[true, 'options'], | ||
[true, 'connect'], | ||
])('should return %s when %s is given', (expected, v) => { | ||
expect(isHttpValidMethod(v)).toBe(expected); | ||
}); | ||
}); | ||
describe('isHttpMethod', () => { | ||
it.each([ | ||
[false, 'GET', 'POST'], | ||
[false, undefined, 'POST'], | ||
[true, 'GET', 'GET'], | ||
[true, 'get', 'GET'], | ||
[true, 'post', 'POST'], | ||
] as [expectation: boolean, v: unknown, method: HttpMethod][])( | ||
'should return %s when %s is given against %s', | ||
(expected, v, method) => { | ||
expect(isHttpMethod(method, v)).toBe(expected); | ||
} | ||
); | ||
}); | ||
}); |
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,20 @@ | ||
import { assertNetworkPort } from '../network.asserts'; | ||
|
||
describe('Network assertions tests', () => { | ||
it('should not throw when given value is a valid http method', () => { | ||
expect(() => assertNetworkPort(0)).not.toThrow(); | ||
expect(() => assertNetworkPort(65_535)).not.toThrow(); | ||
expect(() => assertNetworkPort(443)).not.toThrow(); | ||
}); | ||
it('should throw when not a valid http method', () => { | ||
expect(() => assertNetworkPort(1_234_567)).toThrow( | ||
new TypeError( | ||
'Value is expected to be a network port, got: number(length:7)' | ||
) | ||
); | ||
}); | ||
it('should throw custom error when value is invalid', () => { | ||
const e = new Error('cool'); | ||
expect(() => assertNetworkPort([], () => e)).toThrow(e); | ||
}); | ||
}); |
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,20 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { isNetworkPort } from '../network.guards'; | ||
|
||
describe('Network typeguards tests', () => { | ||
describe('isNetworkPort', () => { | ||
it.each([ | ||
[false, null], | ||
[false, undefined], | ||
[false, 128_000], | ||
[false, BigInt(10)], | ||
[false, new Date()], | ||
[true, 0], | ||
[true, 65_535], | ||
[true, 80], | ||
])('should return %s when %s is given', (expected, v) => { | ||
expect(isNetworkPort(v)).toBe(expected); | ||
}); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { isHttpMethod, isHttpValidMethod } from './http.guards'; | ||
import type { HttpMethod } from './http.types'; | ||
import { formatErrMsg } from './messages/errorMessages'; | ||
import type { MsgOrErrorFactory } from './types/internal.types'; | ||
import { createAssertException } from './utils/createAssertException'; | ||
/** | ||
* Assert the value is a valid http method (case-insensitive) | ||
* @throws TypeError | ||
*/ | ||
export function assertHttpValidMethod( | ||
v: unknown, | ||
msgOrErrorFactory?: MsgOrErrorFactory | ||
): asserts v is HttpMethod { | ||
if (!isHttpValidMethod(v)) { | ||
throw createAssertException( | ||
msgOrErrorFactory, | ||
formatErrMsg('http method', v) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @throws TypeError | ||
*/ | ||
export function assertHttpMethod<T extends HttpMethod>( | ||
method: T, | ||
v: unknown, | ||
msgOrErrorFactory?: MsgOrErrorFactory | ||
): asserts v is T { | ||
if (!isHttpMethod(method, v)) { | ||
throw createAssertException( | ||
msgOrErrorFactory, | ||
formatErrMsg(`http '${method}' method`, v) | ||
); | ||
} | ||
} |
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,11 @@ | ||
export const httpMethods = [ | ||
'GET', | ||
'POST', | ||
'HEAD', | ||
'PUT', | ||
'DELETE', | ||
'CONNECT', | ||
'OPTIONS', | ||
'PATCH', | ||
'TRACE', | ||
] as const; |
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,20 @@ | ||
import { httpMethods } from './http.consts'; | ||
import type { HttpMethod } from './http.types'; | ||
|
||
/** | ||
* Check whether the value is a valid http method (GET, PUT...) in | ||
* a case-insensitive manner. | ||
*/ | ||
export const isHttpValidMethod = (v: unknown): v is HttpMethod => { | ||
return ( | ||
typeof v === 'string' && | ||
(httpMethods as unknown as string[]).includes(v.toUpperCase()) | ||
); | ||
}; | ||
|
||
export const isHttpMethod = <T extends HttpMethod>( | ||
method: T, | ||
v: unknown | ||
): v is T => { | ||
return typeof v === 'string' && method === v.toUpperCase(); | ||
}; |
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,10 @@ | ||
export type HttpMethod = | ||
| 'GET' | ||
| 'POST' | ||
| 'HEAD' | ||
| 'PUT' | ||
| 'DELETE' | ||
| 'CONNECT' | ||
| 'OPTIONS' | ||
| 'PATCH' | ||
| 'TRACE'; |
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
Oops, something went wrong.