From ad0d582fb15c9e223c955b79cfedc6e021d257a1 Mon Sep 17 00:00:00 2001 From: Peter Date: Mon, 25 Oct 2021 11:23:14 +0200 Subject: [PATCH] added applyQueryParseOutput function + issue templates --- .github/ISSUE_TEMPLATE/bug-report.md | 33 ++++++++++++++++++ .github/ISSUE_TEMPLATE/config.yml | 6 ++++ .github/ISSUE_TEMPLATE/suggest-a-feature.md | 26 +++++++++++++++ SECURITY.md | 4 +++ src/query/index.ts | 1 + src/query/parameter/fields/module.ts | 4 +-- src/query/parameter/pagination/module.ts | 4 +-- src/query/parameter/relations/module.ts | 4 +-- src/query/parameter/sort/module.ts | 6 ++-- src/query/utils.ts | 37 +++++++++++++++++++++ test/unit/api/index.spec.ts | 24 +++++++++++++ test/unit/api/sort.spec.ts | 6 ++-- 12 files changed, 143 insertions(+), 12 deletions(-) create mode 100644 .github/ISSUE_TEMPLATE/bug-report.md create mode 100644 .github/ISSUE_TEMPLATE/config.yml create mode 100644 .github/ISSUE_TEMPLATE/suggest-a-feature.md create mode 100644 SECURITY.md create mode 100644 src/query/utils.ts create mode 100644 test/unit/api/index.spec.ts diff --git a/.github/ISSUE_TEMPLATE/bug-report.md b/.github/ISSUE_TEMPLATE/bug-report.md new file mode 100644 index 00000000..d9a31019 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug-report.md @@ -0,0 +1,33 @@ +--- +name: 🚨 Bug report +about: Report a bug report, to improve this project. +title: 'Bug: ' +labels: 'bug-report' +assignees: '' + +--- + + + +### Versions +- Node: +- OS: + +### Reproduction + +
+Additional Details +
+ +### Steps to reproduce + + +### What is Expected? + + +### What is actually happening? diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 00000000..475ed299 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,6 @@ +blank_issues_enabled: false +contact_links: + - name: ❗️ All other issues + url: https://github.com/PHT-Medic/central-ui/discussions + about: | + Please use GitHub Discussions for other issues and asking questions. diff --git a/.github/ISSUE_TEMPLATE/suggest-a-feature.md b/.github/ISSUE_TEMPLATE/suggest-a-feature.md new file mode 100644 index 00000000..700d91de --- /dev/null +++ b/.github/ISSUE_TEMPLATE/suggest-a-feature.md @@ -0,0 +1,26 @@ +--- +name: 🧠 Feature request +about: Suggest an idea or enhancement for this project +title: 'Feature: ' +labels: 'feature-request' +assignees: '' + +--- + + + +### Is your feature request related to a problem? Please describe. + + + +### Describe the solution you'd like + + + +### Describe alternatives you've considered + + + +### Additional context + + diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 00000000..3ed9a051 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,4 @@ +# Security Policy +## Reporting a Vulnerability + +If you discover a security vulnerability regarding this project, please e-mail me to contact@tada5hi.net! diff --git a/src/query/index.ts b/src/query/index.ts index 246cb3e4..7e0a7d6c 100644 --- a/src/query/index.ts +++ b/src/query/index.ts @@ -1,4 +1,5 @@ export * from './parameter'; +export * from './utils'; diff --git a/src/query/parameter/fields/module.ts b/src/query/parameter/fields/module.ts index 2279e5aa..0d0592a1 100644 --- a/src/query/parameter/fields/module.ts +++ b/src/query/parameter/fields/module.ts @@ -13,7 +13,7 @@ import {FieldsApplyOptions, FieldsApplyOutput} from "./type"; * @param data */ /* istanbul ignore next */ -export function applyParsedQueryFields( +export function applyQueryFieldsParseOutput( query: SelectQueryBuilder, data: FieldsApplyOutput ) { @@ -53,7 +53,7 @@ export function applyQueryFields( data: unknown, options?: FieldsApplyOptions ) : FieldsApplyOutput { - return applyParsedQueryFields(query, parseQueryFields(data, options)); + return applyQueryFieldsParseOutput(query, parseQueryFields(data, options)); } /** diff --git a/src/query/parameter/pagination/module.ts b/src/query/parameter/pagination/module.ts index 763cf49c..4ea2c1d2 100644 --- a/src/query/parameter/pagination/module.ts +++ b/src/query/parameter/pagination/module.ts @@ -8,7 +8,7 @@ import {PaginationApplyOptions, PaginationApplyOutput} from "./type"; * @param query * @param data */ -export function applyParsedQueryPagination( +export function applyQueryPaginationParseOutput( query: SelectQueryBuilder, data: PaginationApplyOutput ) { @@ -41,7 +41,7 @@ export function applyQueryPagination( data: unknown, options?: PaginationApplyOptions ) : PaginationApplyOutput { - return applyParsedQueryPagination(query, parseQueryPagination(data, options)); + return applyQueryPaginationParseOutput(query, parseQueryPagination(data, options)); } /** diff --git a/src/query/parameter/relations/module.ts b/src/query/parameter/relations/module.ts index 128c46c6..b0bb72eb 100644 --- a/src/query/parameter/relations/module.ts +++ b/src/query/parameter/relations/module.ts @@ -8,7 +8,7 @@ import {RelationsApplyOptions, RelationsApplyOutput} from "./type"; * @param query * @param data */ -export function applyParsedQueryRelations( +export function applyQueryRelationsParseOutput( query: SelectQueryBuilder, data: RelationsParseOutput ) : RelationsApplyOutput { @@ -32,7 +32,7 @@ export function applyQueryRelations( data: unknown, options?: RelationsApplyOptions ) : RelationsApplyOutput { - return applyParsedQueryRelations(query, parseQueryRelations(data, options)); + return applyQueryRelationsParseOutput(query, parseQueryRelations(data, options)); } /** diff --git a/src/query/parameter/sort/module.ts b/src/query/parameter/sort/module.ts index 54f3389e..3a8d50db 100644 --- a/src/query/parameter/sort/module.ts +++ b/src/query/parameter/sort/module.ts @@ -10,7 +10,7 @@ import {SortApplyOptions, SortApplyOutput} from "./type"; * @param query * @param data */ -export function applyParsedQuerySort( +export function applyQuerySortParseOutput( query: SelectQueryBuilder, data: SortParseOutput ) : SortApplyOutput { @@ -44,7 +44,7 @@ export function applyQuerySort( data: unknown, options?: SortApplyOptions ) : SortParseOutput { - return applyParsedQuerySort(query, parseQuerySort(data, options)); + return applyQuerySortParseOutput(query, parseQuerySort(data, options)); } /** @@ -59,6 +59,6 @@ export function applySort( data: unknown, options?: SortApplyOptions ) : SortParseOutput { - return applyParsedQuerySort(query, parseQuerySort(data, options)); + return applyQuerySortParseOutput(query, parseQuerySort(data, options)); } diff --git a/src/query/utils.ts b/src/query/utils.ts new file mode 100644 index 00000000..745fcfa5 --- /dev/null +++ b/src/query/utils.ts @@ -0,0 +1,37 @@ +import {Parameter, ParameterType, ParseOutput} from "@trapi/query"; + +import { + applyQueryFieldsParseOutput, + applyQueryPaginationParseOutput, + applyQueryRelationsParseOutput, + applyQueryFiltersParseOutput +} from "./parameter"; + +import {SelectQueryBuilder} from "typeorm"; + +export function applyQueryParseOutput( + query: SelectQueryBuilder, + context: ParseOutput +) : ParseOutput { + for(const key in context) { + switch (key as ParameterType) { + case Parameter.FIELDS: + applyQueryFieldsParseOutput(query, context.fields); + break; + case Parameter.FILTERS: + applyQueryFiltersParseOutput(query, context.filters); + break; + case Parameter.PAGINATION: + applyQueryPaginationParseOutput(query, context.pagination); + break; + case Parameter.RELATIONS: + applyQueryRelationsParseOutput(query, context.relations); + break; + case Parameter.SORT: + applyQueryRelationsParseOutput(query, context.sort); + break; + } + } + + return context; +} diff --git a/test/unit/api/index.spec.ts b/test/unit/api/index.spec.ts new file mode 100644 index 00000000..48b7cb09 --- /dev/null +++ b/test/unit/api/index.spec.ts @@ -0,0 +1,24 @@ +import {FakeSelectQueryBuilder} from "../../data/typeorm/FakeSelectQueryBuilder"; +import {applyQueryParseOutput} from "../../../src"; + +describe('src/api/sort.ts', () => { + const query = new FakeSelectQueryBuilder(); + + it('should apply query output', () => { + const data = applyQueryParseOutput(query, { + relations: [], + fields: [], + filters: [], + pagination: {}, + sort: [] + }); + + expect(data).toEqual({ + relations: [], + fields: [], + filters: [], + pagination: {}, + sort: [] + }); + }); +}); diff --git a/test/unit/api/sort.spec.ts b/test/unit/api/sort.spec.ts index 0912c0a3..ce647706 100644 --- a/test/unit/api/sort.spec.ts +++ b/test/unit/api/sort.spec.ts @@ -1,14 +1,14 @@ import {parseQuerySort} from "@trapi/query"; -import {applyQuerySort, applyParsedQuerySort, applySort} from "../../../src"; +import {applyQuerySort, applyQuerySortParseOutput, applySort} from "../../../src"; import {FakeSelectQueryBuilder} from "../../data/typeorm/FakeSelectQueryBuilder"; describe('src/api/sort.ts', () => { const query = new FakeSelectQueryBuilder(); it('should apply sort transformed', () => { - let data = applyParsedQuerySort(query, parseQuerySort('id', {allowed: ['id']})); + let data = applyQuerySortParseOutput(query, parseQuerySort('id', {allowed: ['id']})); expect(data).toBeDefined(); - data = applyParsedQuerySort(query, []); + data = applyQuerySortParseOutput(query, []); expect(data).toEqual([]); });