Skip to content

Latest commit

 

History

History
768 lines (451 loc) · 18.6 KB

modules.md

File metadata and controls

768 lines (451 loc) · 18.6 KB

@panenco/papi / Exports

@panenco/papi

Table of contents

Contract Enumerations

Contract Classes

Error Classes

Authorization Interfaces

Other Interfaces

Authorization Type Aliases

Authorization Functions

Contract Functions

Decorator Functions

Helpers Functions

Middleware Functions

Authorization Type Aliases

IRequirement

Ƭ IRequirement: (request: Request) => Promise<void> | void

Type declaration

▸ (request): Promise<void> | void

Authorization definition base type

Implementation should validate required check and throw [[Unauthorized]] or [[Forbidden]] Exception

Parameters
Name Type
request Request
Returns

Promise<void> | void

Defined in

authorization/requirement.interface.ts:10

Authorization Functions

createAccessToken

createAccessToken(secret, expiresIn, data): Promise<IAccessToken>

Generate an HS256 JWT Bearer token signed by a provided secret

Parameters

Name Type Description
secret string Secret used to sign the JWT
expiresIn number Expiry Time in seconds
data object Data to be serialized in the JWT

Returns

Promise<IAccessToken>

An object containing the expiry time and the generated token

Defined in

authorization/accessTokens.ts:15


getAccessTokenData

getAccessTokenData(token, secret): any

Decode and verify JWT token

Parameters

Name Type Description
token string JWT token to decode and validate
secret string The secret that should have been used to sign the token

Returns

any

Decoded token data

Defined in

authorization/accessTokens.ts:30


getAuthenticator

getAuthenticator(jwtSecret): (action: Action, requirements: IRequirement | (IRequirement | IRequirement[])[]) => Promise<boolean>

Integrates with routing controller @Authorize decorator Supply an array of groups where within a group all requirements need to be valid Example1: [[valid1, valid2], [valid3, invalid1]] => success Example2: [valid1, invalid1] => success Example3: [[valid1, invalid1]] => fail

Parameters

Name Type Description
jwtSecret string JWT secret to be used to verify tokens

Returns

fn

Authentication checker that should be passed into useExpressServer => authorizationChecker

▸ (action, requirements): Promise<boolean>

Parameters
Name Type
action Action
requirements IRequirement | (IRequirement | IRequirement[])[]
Returns

Promise<boolean>

Defined in

authorization/authenticator.ts:16


Contract Functions

createListRepresentation

createListRepresentation<TChild>(type): ClassType<ListRepresentation<TChild>>

Output DTO for paginated lists. In order to generate the correct metadata for the validation and API documentation for the list schema we need to generate the class with the class type of the child DTO of the list. This function can be used in combination with the representer. But a shorthand [[listRepresenter]] als o exists.

Type parameters

Name
TChild

Parameters

Name Type Description
type ClassType<TChild> The class type of a single list item

Returns

ClassType<ListRepresentation<TChild>>

The class type of a list representation with the necessary metadata for the items array. Similar to [[ListRepresentation]] but with metadata.

Defined in

contracts/representations/list.representation.ts:28


Decorator Functions

Body

Body(bodyOptions?, validatorOptions?, type?): (target: Object, propertyKey: string, parameterIndex: number) => void

Allows to inject and validate a request body value to the controller action parameter.

Must be applied on a controller action parameter.

Parameters

Name Type Description
bodyOptions? InputOptions routing-controllers body options for binding the request body
validatorOptions? ValidatorOptions class-validator validation options for the validation to be performed.
type? Newable<any> Override inferred type, set the expected type of the body

Returns

fn

▸ (target, propertyKey, parameterIndex): void

Parameters
Name Type
target Object
propertyKey string
parameterIndex number
Returns

void

Defined in

decorators/body.decorator.ts:22


ConditionalExpose

ConditionalExpose(condition): (target: any, key: string) => void

Extended version of [[class-transformer]][[Expose]] decorator where a condition can be passed to optionally return the property this is applied on.

Parameters

Name Type Description
condition (object: any) => boolean Condition to Expose the property. With argument the complete current object.

Returns

fn

▸ (target, key): void

Parameters
Name Type
target any
key string
Returns

void

Defined in

decorators/conditionalExpose.decorator.ts:11


Date

Date(): (target: any, key: string) => void

Extended version of [[class-validator]][[IsDate]] decorator

Checks if a value is a date.

Transforms string dates into JS Dates.

Returns

fn

▸ (target, key): void

Parameters
Name Type
target any
key string
Returns

void

Defined in

decorators/date.decorator.ts:13


DefaultValue

DefaultValue(value): (target: any, key: string) => void

Decorator that produces a default value for non-existing or empty properties when transforming with [[class-transformer]]

Parameters

Name Type Description
value any The default value to be set.

Returns

fn

▸ (target, key): void

Parameters
Name Type
target any
key string
Returns

void

Defined in

decorators/defaultValue.decorator.ts:9


Lower

Lower(): PropertyDecorator

Transforms strings to lowercase.

Returns

PropertyDecorator

Defined in

decorators/lower.decorator.ts:8


Nested

Nested<T>(type, isArray?): (object: any, propertyName: string) => void

Nested objects with this decorator will also be validated.

Transforms the nested objects to the defined type

Type parameters

Name
T

Parameters

Name Type Default value Description
type ClassType<T> undefined The type to validate and transform the nested property
isArray boolean false Whether the property is an object or an array of objects

Returns

fn

▸ (object, propertyName): void

Parameters
Name Type
object any
propertyName string
Returns

void

Defined in

decorators/nested.decorator.ts:16


Numeric

Numeric(): (target: any, key: string) => void

Checks if a value is a number.

Transforms strings and other matching types to numbers

Returns

fn

▸ (target, key): void

Parameters
Name Type
target any
key string
Returns

void

Defined in

decorators/numeric.decorator.ts:11


Query

Query(options?, validatorOptions?, type?): (target: Object, propertyKey: string | symbol, parameterIndex: number) => void

Allows to inject and validate a request query params to the controller action parameter.

Must be applied on a controller action parameter.

Parameters

Name Type Description
options InputOptions [[routing-controllers]] param options for binding the request query
validatorOptions? ValidatorOptions [[class-validator]] validation options for the validation to be performed.
type? Newable<any> Override inferred type, set the expected type of the query

Returns

fn

▸ (target, propertyKey, parameterIndex): void

Parameters
Name Type
target Object
propertyKey string | symbol
parameterIndex number
Returns

void

Defined in

decorators/query.decorator.ts:16


Representer

Representer(representation, statusCode?, options?): (target: any, key: string) => void

Decorator to be used on [[routing-controllers]] controller method

Apply representation transformation on a controller method output.

The result of the transformed return value will be JSON serialized as the response body.

Must be applied on a controller method.

Parameters

Name Type Description
representation ClassType<any> class type of the representation to be used by [[class-transformer]]
statusCode? number Optional Status code of the success response
options Object OpenAPI options for API documentation
options.contentType? string -
options.description? string -

Returns

fn

▸ (target, key): void

Parameters
Name Type
target any
key string
Returns

void

Defined in

decorators/representer.decorator.ts:20


ListRepresenter

ListRepresenter(representation, statusCode?, options?): (target: any, key: string) => void

Decorator to be used on [[routing-controllers]] controller method

Apply paginated list representation transformation on a controller method output.

The result of the transformed return value will be JSON serialized as the response body.

Must be applied on a controller method.

Parameters

Name Type Default value Description
representation ClassType<any> undefined class type of the representation to be used by [[class-transformer]]
statusCode number 200 Optional Status code of the success response
options Object {} OpenAPI options for API documentation Structure of the response: { items: object[], count: number }
options.contentType? string undefined -
options.description? string undefined -

Returns

fn

▸ (target, key): void

Parameters
Name Type
target any
key string
Returns

void

Defined in

decorators/representer.decorator.ts:58


ArrayRepresenter

ArrayRepresenter(representation, statusCode?, options?): (target: any, key: string) => void

Decorator to be used on [[routing-controllers]] controller method

Apply array representation transformation on a controller method output.

The result of the transformed return value will be JSON serialized as the response body.

Must be applied on a controller method.

Parameters

Name Type Default value Description
representation ClassType<any> undefined class type of the representation to be used by [[class-transformer]]
statusCode number 200 Optional Status code of the success response
options Object {} OpenAPI options for API documentation Structure of the response: object[]
options.contentType? string undefined -
options.description? string undefined -

Returns

fn

▸ (target, key): void

Parameters
Name Type
target any
key string
Returns

void

Defined in

decorators/representer.decorator.ts:91


TransformBooleanString

TransformBooleanString(): PropertyDecorator

Transformation decorator

Transforms string boolean into JS boolean.

Returns

PropertyDecorator

Defined in

decorators/transformBooleanString.decorator.ts:10


Helpers Functions

importClasses

importClasses<TClassType>(directories): [ClassType<TClassType>]

Import all classes within a list of directories (js files)

Type parameters

Name Type
TClassType any

Parameters

Name Type Description
directories string[] directories glob to scan for classes

Returns

[ClassType<TClassType>]

Array of class types

Defined in

utils/helpers/importClasses.ts:12


stringToObject

stringToObject(path, value, obj?): object

Creates an object from a property path

Parameters

Name Type Description
path string Path to create an object from. Ex. root.child.subChild
value any The value to set on the lowest level Ex. true
obj object Optional root object

Returns

object

The object that has been created from the path. Ex. { root: { child: { subChild: true } } }

Defined in

utils/helpers/stringToObject.ts:10


validator

validator(object, options?): Promise<void>

Validates an object with class-validator [[validate]]

Parameters

Name Type Description
object object The object to validate
options? ValidatorOptions validation options

Returns

Promise<void>

Defined in

utils/helpers/validator.ts:12


Middleware Functions

errorMiddleware

errorMiddleware(error, req, res, next): void

Express middleware to process and output errors.

Errors extended from [[ErrorBase]] or [[HttpError]] will be handled and serialized to the response with the corresponding code

Parameters

Name Type Description
error Error Error to be processed. ErrorBase will be processed accordingly. Any other Error will result into a 500 with the specified message.
req Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>> express Request
res Response<any, Record<string, any>> express Response
next NextFunction express NextFunction

Returns

void

Defined in

utils/middleware/error.middleware.ts:23