-
-
Notifications
You must be signed in to change notification settings - Fork 678
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(decorators): support registering arg by `createParameterDecorato…
…r` (#1680) * Support registering arg in createParameterDecorator * Update docs * Update examples * Update changelog * Rename to RandomIdArg
- Loading branch information
1 parent
79d216f
commit bbcea46
Showing
17 changed files
with
246 additions
and
42 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
4 changes: 2 additions & 2 deletions
4
examples/middlewares-custom-decorators/decorators/current-user.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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { createParamDecorator } from "type-graphql"; | ||
import { createParameterDecorator } from "type-graphql"; | ||
import { type Context } from "../context.type"; | ||
|
||
export function CurrentUser() { | ||
return createParamDecorator<Context>(({ context }) => context.currentUser); | ||
return createParameterDecorator<Context>(({ context }) => context.currentUser); | ||
} |
24 changes: 24 additions & 0 deletions
24
examples/middlewares-custom-decorators/decorators/random-id-arg.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 @@ | ||
import { Int, createParameterDecorator } from "type-graphql"; | ||
|
||
const MAX_ID_VALUE = 3; // Number.MAX_SAFE_INTEGER | ||
|
||
export function RandomIdArg(argName = "id") { | ||
return createParameterDecorator( | ||
({ args }) => args[argName] ?? Math.round(Math.random() * MAX_ID_VALUE), | ||
{ | ||
arg: { | ||
name: argName, | ||
typeFunc: () => Int, | ||
options: { | ||
nullable: true, | ||
description: "Accepts provided id or generates a random one.", | ||
validateFn: (value: number): void => { | ||
if (value < 0 || value > MAX_ID_VALUE) { | ||
throw new Error(`Invalid value for ${argName}`); | ||
} | ||
}, | ||
}, | ||
}, | ||
}, | ||
); | ||
} |
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
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 was deleted.
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { SymbolKeysNotSupportedError } from "@/errors"; | ||
import { getMetadataStorage } from "@/metadata/getMetadataStorage"; | ||
import { type ParameterDecorator, type ResolverData } from "@/typings"; | ||
import { type ArgOptions } from "./Arg"; | ||
import { type ReturnTypeFunc } from "./types"; | ||
import { getParamInfo } from "../helpers/params"; | ||
import { type CustomParamOptions } from "../metadata/definitions"; | ||
|
||
export interface CustomParameterOptions { | ||
arg?: { | ||
name: string; | ||
typeFunc: ReturnTypeFunc; | ||
options?: ArgOptions; | ||
}; | ||
} | ||
|
||
export type ParameterResolver<TContextType extends object = object> = ( | ||
resolverData: ResolverData<TContextType>, | ||
) => any; | ||
|
||
export function createParameterDecorator<TContextType extends object = object>( | ||
resolver: ParameterResolver<TContextType>, | ||
paramOptions: CustomParameterOptions = {}, | ||
): ParameterDecorator { | ||
return (prototype, propertyKey, parameterIndex) => { | ||
if (typeof propertyKey === "symbol") { | ||
throw new SymbolKeysNotSupportedError(); | ||
} | ||
|
||
const options: CustomParamOptions = {}; | ||
if (paramOptions.arg) { | ||
options.arg = { | ||
kind: "arg", | ||
name: paramOptions.arg.name, | ||
description: paramOptions.arg.options?.description, | ||
deprecationReason: paramOptions.arg.options?.deprecationReason, | ||
...getParamInfo({ | ||
prototype, | ||
propertyKey, | ||
parameterIndex, | ||
returnTypeFunc: paramOptions.arg.typeFunc, | ||
options: paramOptions.arg.options, | ||
argName: paramOptions.arg.name, | ||
}), | ||
}; | ||
} | ||
|
||
getMetadataStorage().collectHandlerParamMetadata({ | ||
kind: "custom", | ||
target: prototype.constructor, | ||
methodName: propertyKey, | ||
index: parameterIndex, | ||
resolver, | ||
options, | ||
}); | ||
}; | ||
} |
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
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.