-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add jest test ci workflow * chore: improved .vscode settings * chore: removed `@alessiofrittoli/post-install-scripts` * chore: improved project structure * feat: add `getRequestIp` utility function * feat: add `readJsonBody` utility function
- Loading branch information
1 parent
b845cb4
commit b6ce250
Showing
19 changed files
with
364 additions
and
150 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
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,25 @@ | ||
on: [ "push", "pull_request" ] | ||
|
||
name: Unit Tests | ||
|
||
jobs: | ||
test: | ||
name: Run Unit Tests | ||
runs-on: ubuntu-latest | ||
steps: | ||
|
||
- uses: actions/checkout@v4 | ||
|
||
- name: Use Node.js 20.x | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 20.x | ||
|
||
- name: Install dependencies | ||
run: | | ||
npm install -g pnpm@latest | ||
pnpm i | ||
- name: Run tests | ||
run: | | ||
pnpm test:ci:coverage |
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,23 @@ | ||
{ | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"name": "Debug Tests", | ||
"request": "launch", | ||
"args": [ | ||
"test", | ||
"--", | ||
"--runInBand", | ||
"--watchAll=false", | ||
"--testNamePattern", | ||
"${jest.testNamePattern}", | ||
"--runTestsByPath", | ||
"${jest.testFile}" | ||
], | ||
"console": "integratedTerminal", | ||
"internalConsoleOptions": "neverOpen", | ||
"disableOptimisticBPs": true, | ||
"runtimeExecutable": "pnpm" | ||
} | ||
] | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,11 +1,9 @@ | ||
import Exception from '@alessiofrittoli/exception/code' | ||
import { ErrorCode as Exception } from '@alessiofrittoli/exception/code' | ||
|
||
export enum Next | ||
{ | ||
// other custom error codes... | ||
} | ||
|
||
const ErrorCode = { Exception, Next } | ||
type ErrorCode = MergedEnumValue<typeof ErrorCode> | ||
|
||
export default ErrorCode | ||
export const ErrorCode = { Exception, Next } | ||
export type ErrorCode = MergedEnumValue<typeof ErrorCode> |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export { default as NextResponse } from './server' | ||
export { default as ErrorCode } from './error' | ||
export * from '@/error' | ||
export * from '@/request' | ||
export * from '@/response' | ||
export * from '@/route-wrappers' | ||
export type * from './types' |
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 type { NextRequest } from 'next/server' | ||
|
||
export * from './read' | ||
|
||
|
||
/** | ||
* Get request IP address. | ||
* | ||
* @param request The NextRequest Instance. | ||
* @returns The Request IP address. | ||
*/ | ||
export const getRequestIp = async ( request?: NextRequest ) => { | ||
|
||
const headers = request?.headers || await ( await import( 'next/headers' ) ).headers() | ||
const forwarded = headers.get( 'X-Forwarded-For' )?.replace( /\s/g, '' ).split( ',' ).at( -1 ) | ||
const realIp = headers.get( 'X-Real-Ip' )?.replace( /\s/g, '' ).split( ',' ).at( -1 ) | ||
|
||
return forwarded || realIp || 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,24 @@ | ||
import type { Api } from '@/types/api' | ||
|
||
/** | ||
* Read Incoming Request Json Body. | ||
* | ||
* @param body The Request|Body instance. | ||
* @returns The Awaited result of {@link Body.json}, null if Body is empty, locked or invalid. | ||
*/ | ||
export const readJsonBody = async < | ||
T extends Api.Route.RequestBody = Api.Route.RequestBody | ||
>( body: Api.Route.Request<T> ) => { | ||
|
||
try { | ||
|
||
return await body.clone().json<T>() | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
} catch ( error ) { | ||
|
||
return null | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.