type-graphql-utils
Advanced tools
Comparing version 2.0.0 to 2.1.0
# Changelog | ||
## [2.1.0](https://github.com/ChrisLahaye/type-graphql-utils/compare/v2.0.0...v2.1.0) (2023-07-05) | ||
### Features | ||
* add option to apply directives ([651a32e](https://github.com/ChrisLahaye/type-graphql-utils/commit/651a32ede98dc3a344aff09507ca09a3530782ce)) | ||
## [2.0.0](https://github.com/ChrisLahaye/type-graphql-utils/compare/v1.1.0...v2.0.0) (2022-11-16) | ||
@@ -4,0 +11,0 @@ |
@@ -6,7 +6,10 @@ import { ClassType } from 'type-graphql'; | ||
export declare function buildEnum<T extends ClassType>(Class: T, name: string): Record<keyof InstanceType<T>, keyof InstanceType<T>>; | ||
export declare function buildType(BaseClass: ClassType, buildFn: (f: FieldMetadata) => FieldMetadata | FieldMetadata[] | undefined): any; | ||
export declare function Pick<T extends ClassType, K extends keyof InstanceType<T>>(BaseClass: T, names: Record<K, 1>): ClassType<Pick<InstanceType<T>, K>>; | ||
export declare function Omit<T extends ClassType, K extends keyof InstanceType<T>>(BaseClass: T, names: Record<K, 1>): ClassType<Omit<InstanceType<T>, K>>; | ||
export declare function Partial<T extends ClassType, K extends keyof InstanceType<T> = keyof InstanceType<T>>(BaseClass: T, names?: Record<K, 1>): ClassType<PartialBy<InstanceType<T>, K>>; | ||
export declare function Required<T extends ClassType, K extends keyof InstanceType<T> = keyof InstanceType<T>>(BaseClass: T, names?: Record<K, 1>): ClassType<RequiredBy<InstanceType<T>, K>>; | ||
export interface Options { | ||
directives?: boolean; | ||
} | ||
export declare function buildType(BaseClass: ClassType, buildFn: (f: FieldMetadata) => FieldMetadata | FieldMetadata[] | undefined, options?: Options): any; | ||
export declare function Pick<T extends ClassType, K extends keyof InstanceType<T>>(BaseClass: T, names: Record<K, 1>, options?: Options): ClassType<Pick<InstanceType<T>, K>>; | ||
export declare function Omit<T extends ClassType, K extends keyof InstanceType<T>>(BaseClass: T, names: Record<K, 1>, options?: Options): ClassType<Omit<InstanceType<T>, K>>; | ||
export declare function Partial<T extends ClassType, K extends keyof InstanceType<T> = keyof InstanceType<T>>(BaseClass: T, names?: Record<K, 1> | null, options?: Options): ClassType<PartialBy<InstanceType<T>, K>>; | ||
export declare function Required<T extends ClassType, K extends keyof InstanceType<T> = keyof InstanceType<T>>(BaseClass: T, names?: Record<K, 1> | null, options?: Options): ClassType<RequiredBy<InstanceType<T>, K>>; | ||
export {}; |
@@ -23,3 +23,3 @@ "use strict"; | ||
exports.buildEnum = buildEnum; | ||
function buildType(BaseClass, buildFn) { | ||
function buildType(BaseClass, buildFn, options) { | ||
let ChildClass = class ChildClass { | ||
@@ -43,20 +43,30 @@ }; | ||
}); | ||
if (options?.directives) { | ||
const fieldNames = metadata.fields.filter((f) => f.target === ChildClass).map((f) => f.name); | ||
metadata.fieldDirectives.forEach((f) => { | ||
if (f.target !== BaseClass && !f.target.isPrototypeOf(BaseClass)) | ||
return; | ||
if (fieldNames.includes(f.fieldName)) { | ||
metadata.fieldDirectives.push({ ...f, target: ChildClass }); | ||
} | ||
}); | ||
} | ||
return ChildClass; | ||
} | ||
exports.buildType = buildType; | ||
function Pick(BaseClass, names) { | ||
return buildType(BaseClass, (f) => f.name in names ? f : undefined); | ||
function Pick(BaseClass, names, options) { | ||
return buildType(BaseClass, (f) => f.name in names ? f : undefined, options); | ||
} | ||
exports.Pick = Pick; | ||
function Omit(BaseClass, names) { | ||
return buildType(BaseClass, (f) => f.name in names ? undefined : f); | ||
function Omit(BaseClass, names, options) { | ||
return buildType(BaseClass, (f) => f.name in names ? undefined : f, options); | ||
} | ||
exports.Omit = Omit; | ||
function Partial(BaseClass, names) { | ||
return buildType(BaseClass, (f) => !names || f.name in names ? { ...f, typeOptions: { ...f.typeOptions, nullable: true } } : f); | ||
function Partial(BaseClass, names, options) { | ||
return buildType(BaseClass, (f) => !names || f.name in names ? { ...f, typeOptions: { ...f.typeOptions, nullable: true } } : f, options); | ||
} | ||
exports.Partial = Partial; | ||
function Required(BaseClass, names) { | ||
return buildType(BaseClass, (f) => !names || f.name in names ? { ...f, typeOptions: { ...f.typeOptions, nullable: false } } : f); | ||
function Required(BaseClass, names, options) { | ||
return buildType(BaseClass, (f) => !names || f.name in names ? { ...f, typeOptions: { ...f.typeOptions, nullable: false } } : f, options); | ||
} | ||
exports.Required = Required; |
@@ -26,5 +26,10 @@ import { ClassType, getMetadataStorage, InputType, registerEnumType, ObjectType } from 'type-graphql'; | ||
export interface Options { | ||
directives?: boolean; | ||
} | ||
export function buildType( | ||
BaseClass: ClassType, | ||
buildFn: (f: FieldMetadata) => FieldMetadata | FieldMetadata[] | undefined, | ||
options?: Options, | ||
): any { | ||
@@ -51,19 +56,31 @@ @InputType({ isAbstract: true }) | ||
if (options?.directives) { | ||
const fieldNames = metadata.fields.filter((f) => f.target === ChildClass).map((f) => f.name); | ||
metadata.fieldDirectives.forEach((f) => { | ||
if (f.target !== BaseClass && !f.target.isPrototypeOf(BaseClass)) return; | ||
if (fieldNames.includes(f.fieldName)) { | ||
metadata.fieldDirectives.push({ ...f, target: ChildClass }); | ||
} | ||
}); | ||
} | ||
return ChildClass; | ||
} | ||
export function Pick<T extends ClassType, K extends keyof InstanceType<T>>(BaseClass: T, names: Record<K, 1>): ClassType<Pick<InstanceType<T>, K>> { | ||
return buildType(BaseClass, (f) => f.name in names ? f : undefined); | ||
export function Pick<T extends ClassType, K extends keyof InstanceType<T>>(BaseClass: T, names: Record<K, 1>, options?: Options): ClassType<Pick<InstanceType<T>, K>> { | ||
return buildType(BaseClass, (f) => f.name in names ? f : undefined, options); | ||
} | ||
export function Omit<T extends ClassType, K extends keyof InstanceType<T>>(BaseClass: T, names: Record<K, 1>): ClassType<Omit<InstanceType<T>, K>> { | ||
return buildType(BaseClass, (f) => f.name in names ? undefined : f); | ||
export function Omit<T extends ClassType, K extends keyof InstanceType<T>>(BaseClass: T, names: Record<K, 1>, options?: Options): ClassType<Omit<InstanceType<T>, K>> { | ||
return buildType(BaseClass, (f) => f.name in names ? undefined : f, options); | ||
} | ||
export function Partial<T extends ClassType, K extends keyof InstanceType<T> = keyof InstanceType<T>>(BaseClass: T, names?: Record<K, 1>): ClassType<PartialBy<InstanceType<T>, K>> { | ||
return buildType(BaseClass, (f) => !names || f.name in names ? { ...f, typeOptions: { ...f.typeOptions, nullable: true } } : f); | ||
export function Partial<T extends ClassType, K extends keyof InstanceType<T> = keyof InstanceType<T>>(BaseClass: T, names?: Record<K, 1> | null, options?: Options): ClassType<PartialBy<InstanceType<T>, K>> { | ||
return buildType(BaseClass, (f) => !names || f.name in names ? { ...f, typeOptions: { ...f.typeOptions, nullable: true } } : f, options); | ||
} | ||
export function Required<T extends ClassType, K extends keyof InstanceType<T> = keyof InstanceType<T>>(BaseClass: T, names?: Record<K, 1>): ClassType<RequiredBy<InstanceType<T>, K>> { | ||
return buildType(BaseClass, (f) => !names || f.name in names ? { ...f, typeOptions: { ...f.typeOptions, nullable: false } } : f); | ||
export function Required<T extends ClassType, K extends keyof InstanceType<T> = keyof InstanceType<T>>(BaseClass: T, names?: Record<K, 1> | null, options?: Options): ClassType<RequiredBy<InstanceType<T>, K>> { | ||
return buildType(BaseClass, (f) => !names || f.name in names ? { ...f, typeOptions: { ...f.typeOptions, nullable: false } } : f, options); | ||
} |
{ | ||
"name": "type-graphql-utils", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "utilities to transform type-graphql types", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -86,16 +86,20 @@ # type-graphql-utils | ||
### `Pick(BaseClass, names)` | ||
**`options`**: The transformation options. An optional object containing any of the following properties: | ||
- `directives`: A boolean that indicates whether to apply directives. By default, false. | ||
### `Pick(BaseClass, names, [options])` | ||
Constructs a type by picking the keys of `names` from `BaseClass`. | ||
### `Omit(BaseClass, names)` | ||
### `Omit(BaseClass, names, [options])` | ||
Constructs a type by picking all fields from `BaseClass` and then removing the keys of `names`. | ||
### `Partial(BaseClass, [names])` | ||
### `Partial(BaseClass, [names], [options])` | ||
Constructs a type by picking all fields from `BaseClass` and then setting the keys of `names` to optional. The opposite of Required. By default, `names` contains all names. | ||
### `Required(BaseClass, [names])` | ||
### `Required(BaseClass, [names], [options])` | ||
Constructs a type by picking all fields from `BaseClass` and then setting the keys of `names` to required. The opposite of Partial. By default, `names` contains all names. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12405
147
105