-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ff0c18
commit f0ea57e
Showing
8 changed files
with
448 additions
and
0 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 @@ | ||
--- | ||
"@codelytv/criteria-from-next-request": major | ||
--- | ||
|
||
add from next request |
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 @@ | ||
<p align="center"> | ||
<a href="https://codely.com"> | ||
<picture> | ||
<source media="(prefers-color-scheme: dark)" srcset="https://codely.com/logo/codely_logo-dark.svg"> | ||
<source media="(prefers-color-scheme: light)" srcset="https://codely.com/logo/codely_logo-light.svg"> | ||
<img alt="Codely logo" src="https://codely.com/logo/codely_logo.svg"> | ||
</picture> | ||
</a> | ||
</p> | ||
|
||
<h1 align="center"> | ||
Criteria from Next.js Request | ||
</h1> | ||
|
||
<p align="center"> | ||
<a href="https://github.com/CodelyTV"><img src="https://img.shields.io/badge/Codely-OS-green.svg?style=flat-square" alt="codely.com"/></a> | ||
</p> | ||
|
||
## 📥 Installation | ||
|
||
```sh | ||
npm i @codelytv/criteria-from-next-request | ||
``` |
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,22 @@ | ||
{ | ||
"name": "@codelytv/criteria-from-next-request", | ||
"version": "0.0.1", | ||
"description": "", | ||
"keywords": [], | ||
"author": "Codely (https://codely.com)", | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"test": "node --import tsx --test test/*.test.ts", | ||
"build": "tsc --build --verbose tsconfig.json" | ||
}, | ||
"dependencies": { | ||
"@codelytv/criteria": "workspace:^", | ||
"@codelytv/criteria-from-url": "workspace:^", | ||
"next": ">=13.0.0" | ||
}, | ||
"devDependencies": { | ||
"@codelytv/criteria-test-mother": "workspace:^" | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/criteria-from-next-request/src/CriteriaFromNextRequestConverter.ts
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 @@ | ||
/* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
import { Criteria, FiltersPrimitives } from "@codelytv/criteria"; | ||
import { CriteriaFromUrlConverter } from "@codelytv/criteria-from-url"; | ||
import { NextRequest } from "next/server"; | ||
|
||
export class CriteriaFromNextRequestConverter { | ||
private readonly urlConverter: CriteriaFromUrlConverter; | ||
|
||
constructor() { | ||
this.urlConverter = new CriteriaFromUrlConverter(); | ||
} | ||
|
||
public toCriteria(request: NextRequest): Criteria { | ||
const url = new URL(request.url); | ||
|
||
return this.urlConverter.toCriteria(url); | ||
} | ||
|
||
public toFiltersPrimitives(request: NextRequest): FiltersPrimitives[] { | ||
const url = new URL(request.url); | ||
|
||
return this.urlConverter.toFiltersPrimitives(url); | ||
} | ||
} |
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 @@ | ||
export * from "./CriteriaFromNextRequestConverter"; |
112 changes: 112 additions & 0 deletions
112
packages/criteria-from-next-request/test/CriteriaFromNextRequestConverter.test.ts
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,112 @@ | ||
import assert from "node:assert"; | ||
import { describe, it } from "node:test"; | ||
|
||
import { CriteriaMother } from "@codelytv/criteria-test-mother"; | ||
import { NextRequest } from "next/server"; | ||
|
||
import { CriteriaFromNextRequestConverter } from "../src/CriteriaFromNextRequestConverter"; | ||
|
||
describe("CriteriaFromNextRequestConverter should", () => { | ||
const converter = new CriteriaFromNextRequestConverter(); | ||
|
||
it("Converts a url with one filter", () => { | ||
const url = new URL( | ||
"http://localhost:3000/api/users?filters[0][field]=name&filters[0][operator]=CONTAINS&filters[0][value]=Javi", | ||
); | ||
const request = new NextRequest(url); | ||
|
||
const expectedCriteria = CriteriaMother.withOneFilter("name", "CONTAINS", "Javi"); | ||
|
||
assert.deepEqual(converter.toCriteria(request), expectedCriteria); | ||
}); | ||
|
||
it("Converts a url with multiple filters", () => { | ||
const url = new URL( | ||
"http://localhost:3000/api/users?filters[0][field]=name&filters[0][operator]=CONTAINS&filters[0][value]=Javi&filters[1][field]=email&filters[1][operator]=CONTAINS&filters[1][value]=gmail", | ||
); | ||
const request = new NextRequest(url); | ||
|
||
const expectedCriteria = CriteriaMother.create({ | ||
filters: [ | ||
{ | ||
field: "name", | ||
operator: "CONTAINS", | ||
value: "Javi", | ||
}, | ||
{ | ||
field: "email", | ||
operator: "CONTAINS", | ||
value: "gmail", | ||
}, | ||
], | ||
orderBy: null, | ||
orderType: null, | ||
pageSize: null, | ||
pageNumber: null, | ||
}); | ||
|
||
assert.deepEqual(converter.toCriteria(request), expectedCriteria); | ||
}); | ||
|
||
it("Converts a url with multiple filters order and pagination", () => { | ||
const url = new URL( | ||
"http://localhost:3000/api/users" + | ||
"?filters[0][field]=name&filters[0][operator]=CONTAINS&filters[0][value]=Javi" + | ||
"&filters[1][field]=email&filters[1][operator]=CONTAINS&filters[1][value]=gmail", | ||
); | ||
const request = new NextRequest(url); | ||
|
||
const expectedCriteria = CriteriaMother.create({ | ||
filters: [ | ||
{ | ||
field: "name", | ||
operator: "CONTAINS", | ||
value: "Javi", | ||
}, | ||
{ | ||
field: "email", | ||
operator: "CONTAINS", | ||
value: "gmail", | ||
}, | ||
], | ||
orderBy: null, | ||
orderType: null, | ||
pageSize: null, | ||
pageNumber: null, | ||
}); | ||
|
||
assert.deepEqual(converter.toCriteria(request), expectedCriteria); | ||
}); | ||
|
||
it("Converts a url with multiple filters order and pagination", () => { | ||
const url = new URL( | ||
"http://localhost:3000/api/users" + | ||
"?filters[0][field]=name&filters[0][operator]=CONTAINS&filters[0][value]=Javi" + | ||
"&filters[1][field]=email&filters[1][operator]=CONTAINS&filters[1][value]=gmail" + | ||
"&orderBy=name&order=ASC" + | ||
"&pageSize=10&pageNumber=2", | ||
); | ||
const request = new NextRequest(url); | ||
|
||
const expectedCriteria = CriteriaMother.create({ | ||
filters: [ | ||
{ | ||
field: "name", | ||
operator: "CONTAINS", | ||
value: "Javi", | ||
}, | ||
{ | ||
field: "email", | ||
operator: "CONTAINS", | ||
value: "gmail", | ||
}, | ||
], | ||
orderBy: "name", | ||
orderType: "ASC", | ||
pageSize: 10, | ||
pageNumber: 2, | ||
}); | ||
|
||
assert.deepEqual(converter.toCriteria(request), expectedCriteria); | ||
}); | ||
}); |
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,7 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist" | ||
}, | ||
"include": ["src/**/*"], | ||
} |
Oops, something went wrong.