@forklaunch/core 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cache/interfaces/ttlCache.interface.d.ts +47 -0
- package/dist/cache/interfaces/ttlCache.interface.js +3 -0
- package/dist/cache/interfaces/ttlCache.interface.js.map +1 -0
- package/dist/cache/redisTtlCache.d.ts +64 -0
- package/dist/cache/redisTtlCache.js +105 -0
- package/dist/cache/redisTtlCache.js.map +1 -0
- package/dist/cache/types/ttlCacheRecord.d.ts +13 -0
- package/dist/cache/types/ttlCacheRecord.js +3 -0
- package/dist/cache/types/ttlCacheRecord.js.map +1 -0
- package/dist/controllers/interfaces/controller.interface.d.ts +15 -0
- package/dist/controllers/interfaces/controller.interface.js +3 -0
- package/dist/controllers/interfaces/controller.interface.js.map +1 -0
- package/dist/database/mikro/models/entities/base.entity.d.ts +25 -0
- package/dist/database/mikro/models/entities/base.entity.js +53 -0
- package/dist/database/mikro/models/entities/base.entity.js.map +1 -0
- package/dist/entityMapper/interfaces/entityMapper.interface.d.ts +16 -0
- package/dist/entityMapper/interfaces/entityMapper.interface.js +3 -0
- package/dist/entityMapper/interfaces/entityMapper.interface.js.map +1 -0
- package/dist/entityMapper/models/baseEntityMapper.model.d.ts +71 -0
- package/dist/entityMapper/models/baseEntityMapper.model.js +81 -0
- package/dist/entityMapper/models/baseEntityMapper.model.js.map +1 -0
- package/dist/entityMapper/models/requestEntityMapper.model.d.ts +69 -0
- package/dist/entityMapper/models/requestEntityMapper.model.js +77 -0
- package/dist/entityMapper/models/requestEntityMapper.model.js.map +1 -0
- package/dist/entityMapper/models/responseEntityMapper.model.d.ts +60 -0
- package/dist/entityMapper/models/responseEntityMapper.model.js +64 -0
- package/dist/entityMapper/models/responseEntityMapper.model.js.map +1 -0
- package/dist/entityMapper/types/entityMapper.types.d.ts +28 -0
- package/dist/entityMapper/types/entityMapper.types.js +3 -0
- package/dist/entityMapper/types/entityMapper.types.js.map +1 -0
- package/dist/http/middlewares/request.middleware.d.ts +11 -0
- package/dist/http/middlewares/request.middleware.js +187 -0
- package/dist/http/middlewares/request.middleware.js.map +1 -0
- package/dist/http/middlewares/response.middleware.d.ts +2 -0
- package/dist/http/middlewares/response.middleware.js +30 -0
- package/dist/http/middlewares/response.middleware.js.map +1 -0
- package/dist/http/types/api.types.d.ts +50 -0
- package/dist/http/types/api.types.js +3 -0
- package/dist/http/types/api.types.js.map +1 -0
- package/dist/http/types/primitive.types.d.ts +35 -0
- package/dist/http/types/primitive.types.js +3 -0
- package/dist/http/types/primitive.types.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/jest.config.d.ts +3 -0
- package/dist/jest.config.js.map +1 -0
- package/dist/services/interfaces/baseService.d.ts +14 -0
- package/dist/services/interfaces/baseService.js +3 -0
- package/dist/services/interfaces/baseService.js.map +1 -0
- package/dist/tests/dto.test.d.ts +1 -0
- package/dist/tests/dto.test.js +179 -0
- package/dist/tests/dto.test.js.map +1 -0
- package/dist/tests/redisTtlCache.test.d.ts +1 -0
- package/dist/tests/redisTtlCache.test.js +35 -0
- package/dist/tests/redisTtlCache.test.js.map +1 -0
- package/entityMapper/interfaces/entityMapper.interface.ts +17 -0
- package/entityMapper/models/baseEntityMapper.model.ts +93 -0
- package/entityMapper/models/requestEntityMapper.model.ts +91 -0
- package/entityMapper/models/responseEntityMapper.model.ts +77 -0
- package/entityMapper/types/entityMapper.types.ts +29 -0
- package/eslint.config.mjs +12 -0
- package/http/middlewares/request.middleware.ts +201 -0
- package/http/middlewares/response.middleware.ts +37 -0
- package/http/types/api.types.ts +65 -0
- package/http/types/primitive.types.ts +55 -0
- package/index.ts +5 -0
- package/jest.config.ts +10 -0
- package/package.json +47 -0
- package/tests/dto.test.ts +235 -0
- package/tests/redisTtlCache.test.ts +42 -0
@@ -0,0 +1,77 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.RequestEntityMapper = void 0;
|
4
|
+
const baseEntityMapper_model_1 = require("./baseEntityMapper.model");
|
5
|
+
/**
|
6
|
+
* Abstract class representing a request entityMapper.
|
7
|
+
*
|
8
|
+
* @template Entity - A type that extends BaseEntity.
|
9
|
+
* @template SV - A type that extends SchemaValidator.
|
10
|
+
* @extends {BaseEntityMapper<SV>}
|
11
|
+
*/
|
12
|
+
class RequestEntityMapper extends baseEntityMapper_model_1.BaseEntityMapper {
|
13
|
+
/**
|
14
|
+
* The entity.
|
15
|
+
* @type {Entity}
|
16
|
+
* @protected
|
17
|
+
*/
|
18
|
+
_Entity;
|
19
|
+
/**
|
20
|
+
* Creates an instance of RequestEntityMapper.
|
21
|
+
*
|
22
|
+
* @param {SV} schemaValidator - The schema provider.
|
23
|
+
*/
|
24
|
+
constructor(schemaValidator) {
|
25
|
+
super(schemaValidator);
|
26
|
+
}
|
27
|
+
/**
|
28
|
+
* Populates the DTO with data from a JSON object.
|
29
|
+
*
|
30
|
+
* @param {this['_dto']} json - The JSON object.
|
31
|
+
* @returns {this} - The instance of the RequestEntityMapper.
|
32
|
+
*/
|
33
|
+
fromJson(json) {
|
34
|
+
if (!this.schemaValidator.validate(this.schemaValidator.schemify(this.schema), json)) {
|
35
|
+
throw new Error('Invalid DTO');
|
36
|
+
}
|
37
|
+
this.dto = json;
|
38
|
+
return this;
|
39
|
+
}
|
40
|
+
/**
|
41
|
+
* Deserializes a JSON object to an entity.
|
42
|
+
*
|
43
|
+
* @param {this['_dto']} json - The JSON object.
|
44
|
+
* @param {...unknown[]} additionalArgs - Additional arguments.
|
45
|
+
* @returns {Entity} - The entity.
|
46
|
+
*/
|
47
|
+
deserializeJsonToEntity(json, ...additionalArgs) {
|
48
|
+
return this.fromJson(json).toEntity(...additionalArgs);
|
49
|
+
}
|
50
|
+
/**
|
51
|
+
* Creates an instance of a RequestEntityMapper from a JSON object.
|
52
|
+
*
|
53
|
+
* @template EntityMapperType - A type that extends RequestEntityMapper.
|
54
|
+
* @param {EntityMapperConstructor<EntityMapperType>} this - The constructor of the EntityMapperType.
|
55
|
+
* @param {EntityMapperType['_SV']} schemaValidator - The schema provider.
|
56
|
+
* @param {EntityMapperStaticSchema<EntityMapperType>} json - The JSON object.
|
57
|
+
* @returns {EntityMapperType} - An instance of the EntityMapperType.
|
58
|
+
*/
|
59
|
+
static fromJson(schemaValidator, json) {
|
60
|
+
return (0, baseEntityMapper_model_1.construct)(this, schemaValidator).fromJson(json);
|
61
|
+
}
|
62
|
+
/**
|
63
|
+
* Deserializes a JSON object to an entity.
|
64
|
+
*
|
65
|
+
* @template EntityMapperType - A type that extends RequestEntityMapper.
|
66
|
+
* @param {EntityMapperConstructor<EntityMapperType>} this - The constructor of the EntityMapperType.
|
67
|
+
* @param {EntityMapperType['_SV']} schemaValidator - The schema provider.
|
68
|
+
* @param {EntityMapperStaticSchema<EntityMapperType>} json - The JSON object.
|
69
|
+
* @param {...unknown[]} additionalArgs - Additional arguments.
|
70
|
+
* @returns {EntityMapperType['_Entity']} - The entity.
|
71
|
+
*/
|
72
|
+
static deserializeJsonToEntity(schemaValidator, json, ...additionalArgs) {
|
73
|
+
return (0, baseEntityMapper_model_1.construct)(this, schemaValidator).fromJson(json).toEntity(...additionalArgs);
|
74
|
+
}
|
75
|
+
}
|
76
|
+
exports.RequestEntityMapper = RequestEntityMapper;
|
77
|
+
//# sourceMappingURL=requestEntityMapper.model.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"requestEntityMapper.model.js","sourceRoot":"","sources":["../../../entityMapper/models/requestEntityMapper.model.ts"],"names":[],"mappings":";;;AAIA,qEAAuE;AAEvE;;;;;;GAMG;AACH,MAAsB,mBAAqF,SAAQ,yCAAoB;IACnI;;;;OAIG;IACH,OAAO,CAAU;IAEjB;;;;OAIG;IACH,YAAY,eAAmB;QAC3B,KAAK,CAAC,eAAe,CAAC,CAAC;IAC3B,CAAC;IAWD;;;;;OAKG;IACH,QAAQ,CAAC,IAAkB;QACvB,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,EAAE;YAClF,MAAM,IAAI,KAAK,CAAC,aAAa,CAAC,CAAC;SAClC;QACD,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC;QAChB,OAAO,IAAI,CAAC;IAChB,CAAC;IAED;;;;;;OAMG;IACH,uBAAuB,CAAC,IAAkB,EAAE,GAAG,cAAyB;QACpE,OAAO,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,GAAG,cAAc,CAAC,CAAC;IAC3D,CAAC;IAED;;;;;;;;OAQG;IACH,MAAM,CAAC,QAAQ,CAAuI,eAAwC,EAAE,IAA8B;QAC1N,OAAO,IAAA,kCAAS,EAAC,IAAI,EAAE,eAAe,CAAC,CAAC,QAAQ,CAAC,IAA2C,CAAC,CAAC;IAClG,CAAC;IAED;;;;;;;;;OASG;IACH,MAAM,CAAC,uBAAuB,CAAuI,eAAwC,EAAE,IAA8B,EAAE,GAAG,cAAyB;QACvQ,OAAO,IAAA,kCAAS,EAAC,IAAI,EAAE,eAAe,CAAC,CAAC,QAAQ,CAAC,IAAgC,CAAC,CAAC,QAAQ,CAAC,GAAG,cAAc,CAAC,CAAC;IACnH,CAAC;CACJ;AA7ED,kDA6EC"}
|
@@ -0,0 +1,60 @@
|
|
1
|
+
import { SchemaValidator } from "@forklaunch/validator/interfaces";
|
2
|
+
import { BaseEntity } from "../../database/mikro/models/entities/base.entity";
|
3
|
+
import { EntityMapperConstructor } from "../interfaces/entityMapper.interface";
|
4
|
+
import { EntityMapperStaticSchema } from "../types/entityMapper.types";
|
5
|
+
import { BaseEntityMapper } from "./baseEntityMapper.model";
|
6
|
+
/**
|
7
|
+
* Abstract class representing a response entityMapper.
|
8
|
+
*
|
9
|
+
* @template Entity - A type that extends BaseEntity.
|
10
|
+
* @template SV - A type that extends SchemaValidator.
|
11
|
+
* @extends {BaseEntityMapper<SV>}
|
12
|
+
*/
|
13
|
+
export declare abstract class ResponseEntityMapper<Entity extends BaseEntity | unknown, SV extends SchemaValidator> extends BaseEntityMapper<SV> {
|
14
|
+
/**
|
15
|
+
* The entity type.
|
16
|
+
* @type {Entity}
|
17
|
+
* @protected
|
18
|
+
*/
|
19
|
+
_Entity: Entity;
|
20
|
+
/**
|
21
|
+
* Populates entityMapper with DTO from an entity.
|
22
|
+
*
|
23
|
+
* @abstract
|
24
|
+
* @param {Entity} entity - The entity to convert.
|
25
|
+
* @returns {this} - The instance of the ResponseEntityMapper.
|
26
|
+
*/
|
27
|
+
abstract fromEntity(entity: Entity, ...additionalArgs: unknown[]): this;
|
28
|
+
/**
|
29
|
+
* Converts the underlying DTO to a JSON object.
|
30
|
+
*
|
31
|
+
* @param {...unknown[]} additionalArgs - Additional arguments.
|
32
|
+
* @returns {this['_dto']} - The JSON object.
|
33
|
+
*/
|
34
|
+
toJson(): this['_dto'];
|
35
|
+
/**
|
36
|
+
* Serializes an entity to a JSON object.
|
37
|
+
*
|
38
|
+
* @param {Entity} entity - The entity to serialize.
|
39
|
+
* @returns {this['_dto']} - The JSON object.
|
40
|
+
*/
|
41
|
+
serializeEntityToJson(entity: Entity): this['_dto'];
|
42
|
+
/**
|
43
|
+
* Populates entityMapper with DTO from an entity.
|
44
|
+
*
|
45
|
+
* @template EntityMapperType - A type that extends ResponseEntityMapper.
|
46
|
+
* @param {EntityMapperConstructor<EntityMapperType>} this - The constructor of the EntityMapperType.
|
47
|
+
* @param {EntityMapperType['_Entity']} entity - The entity to convert.
|
48
|
+
* @returns {EntityMapperType} - An instance of the EntityMapperType.
|
49
|
+
*/
|
50
|
+
static fromEntity<EntityMapperType extends ResponseEntityMapper<unknown, any>>(this: EntityMapperConstructor<EntityMapperType, EntityMapperType['_SV']>, schemaValidator: EntityMapperType['_SV'], entity: EntityMapperType['_Entity']): EntityMapperType;
|
51
|
+
/**
|
52
|
+
* Serializes an entity to a JSON object.
|
53
|
+
*
|
54
|
+
* @template EntityMapperType - A type that extends ResponseEntityMapper.
|
55
|
+
* @param {EntityMapperConstructor<EntityMapperType>} this - The constructor of the EntityMapperType.
|
56
|
+
* @param {EntityMapperType['_Entity']} entity - The entity to serialize.
|
57
|
+
* @returns {EntityMapperStaticSchema<EntityMapperType>} - The JSON object.
|
58
|
+
*/
|
59
|
+
static serializeEntityToJson<EntityMapperType extends ResponseEntityMapper<unknown, any>>(this: EntityMapperConstructor<EntityMapperType, EntityMapperType['_SV']>, schemaValidator: EntityMapperType['_SV'], entity: EntityMapperType['_Entity']): EntityMapperStaticSchema<EntityMapperType>;
|
60
|
+
}
|
@@ -0,0 +1,64 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.ResponseEntityMapper = void 0;
|
4
|
+
const baseEntityMapper_model_1 = require("./baseEntityMapper.model");
|
5
|
+
/**
|
6
|
+
* Abstract class representing a response entityMapper.
|
7
|
+
*
|
8
|
+
* @template Entity - A type that extends BaseEntity.
|
9
|
+
* @template SV - A type that extends SchemaValidator.
|
10
|
+
* @extends {BaseEntityMapper<SV>}
|
11
|
+
*/
|
12
|
+
class ResponseEntityMapper extends baseEntityMapper_model_1.BaseEntityMapper {
|
13
|
+
/**
|
14
|
+
* The entity type.
|
15
|
+
* @type {Entity}
|
16
|
+
* @protected
|
17
|
+
*/
|
18
|
+
_Entity;
|
19
|
+
/**
|
20
|
+
* Converts the underlying DTO to a JSON object.
|
21
|
+
*
|
22
|
+
* @param {...unknown[]} additionalArgs - Additional arguments.
|
23
|
+
* @returns {this['_dto']} - The JSON object.
|
24
|
+
*/
|
25
|
+
toJson() {
|
26
|
+
if (!this.schemaValidator.validate(this.schemaValidator.schemify(this.schema), this.dto)) {
|
27
|
+
throw new Error('Invalid DTO');
|
28
|
+
}
|
29
|
+
return this.dto;
|
30
|
+
}
|
31
|
+
/**
|
32
|
+
* Serializes an entity to a JSON object.
|
33
|
+
*
|
34
|
+
* @param {Entity} entity - The entity to serialize.
|
35
|
+
* @returns {this['_dto']} - The JSON object.
|
36
|
+
*/
|
37
|
+
serializeEntityToJson(entity) {
|
38
|
+
return this.fromEntity(entity).toJson();
|
39
|
+
}
|
40
|
+
/**
|
41
|
+
* Populates entityMapper with DTO from an entity.
|
42
|
+
*
|
43
|
+
* @template EntityMapperType - A type that extends ResponseEntityMapper.
|
44
|
+
* @param {EntityMapperConstructor<EntityMapperType>} this - The constructor of the EntityMapperType.
|
45
|
+
* @param {EntityMapperType['_Entity']} entity - The entity to convert.
|
46
|
+
* @returns {EntityMapperType} - An instance of the EntityMapperType.
|
47
|
+
*/
|
48
|
+
static fromEntity(schemaValidator, entity) {
|
49
|
+
return (0, baseEntityMapper_model_1.construct)(this, schemaValidator).fromEntity(entity);
|
50
|
+
}
|
51
|
+
/**
|
52
|
+
* Serializes an entity to a JSON object.
|
53
|
+
*
|
54
|
+
* @template EntityMapperType - A type that extends ResponseEntityMapper.
|
55
|
+
* @param {EntityMapperConstructor<EntityMapperType>} this - The constructor of the EntityMapperType.
|
56
|
+
* @param {EntityMapperType['_Entity']} entity - The entity to serialize.
|
57
|
+
* @returns {EntityMapperStaticSchema<EntityMapperType>} - The JSON object.
|
58
|
+
*/
|
59
|
+
static serializeEntityToJson(schemaValidator, entity) {
|
60
|
+
return (0, baseEntityMapper_model_1.construct)(this, schemaValidator).serializeEntityToJson(entity);
|
61
|
+
}
|
62
|
+
}
|
63
|
+
exports.ResponseEntityMapper = ResponseEntityMapper;
|
64
|
+
//# sourceMappingURL=responseEntityMapper.model.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"responseEntityMapper.model.js","sourceRoot":"","sources":["../../../entityMapper/models/responseEntityMapper.model.ts"],"names":[],"mappings":";;;AAIA,qEAAuE;AAEvE;;;;;;GAMG;AACH,MAAsB,oBAAsF,SAAQ,yCAAoB;IACpI;;;;OAIG;IACH,OAAO,CAAU;IAWjB;;;;;OAKG;IACH,MAAM;QACF,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE;YACtF,MAAM,IAAI,KAAK,CAAC,aAAa,CAAC,CAAC;SAClC;QACD,OAAO,IAAI,CAAC,GAAG,CAAC;IACpB,CAAC;IAED;;;;;OAKG;IACH,qBAAqB,CAAC,MAAc;QAChC,OAAO,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,CAAC;IAC5C,CAAC;IAED;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,CAAwI,eAAwC,EAAE,MAAmC;QAClO,OAAO,IAAA,kCAAS,EAAC,IAAI,EAAE,eAAe,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IAC/D,CAAC;IAED;;;;;;;OAOG;IACH,MAAM,CAAC,qBAAqB,CAAwI,eAAwC,EAAE,MAAmC;QAC7O,OAAO,IAAA,kCAAS,EAAC,IAAI,EAAE,eAAe,CAAC,CAAC,qBAAqB,CAAC,MAAM,CAA+C,CAAC;IACxH,CAAC;CACJ;AA/DD,oDA+DC"}
|
@@ -0,0 +1,28 @@
|
|
1
|
+
import { Schema, SchemaCatchall, ValidSchemaObject } from "@forklaunch/validator/";
|
2
|
+
import { SchemaValidator } from "@forklaunch/validator/interfaces";
|
3
|
+
import { UnboxedObjectSchema } from "@forklaunch/validator/types";
|
4
|
+
import { BaseEntityMapper } from "../models/baseEntityMapper.model";
|
5
|
+
/**
|
6
|
+
* Type representing a schema validator object for an entity mapper.
|
7
|
+
*
|
8
|
+
* @template SV - A type that extends SchemaValidator.
|
9
|
+
* @typedef {ValidSchemaObject<SV> | UnboxedObjectSchema<SchemaCatchall<SV>> & {}} EntityMapperSchemaValidatorObject
|
10
|
+
*/
|
11
|
+
export type EntityMapperSchemaValidatorObject<SV extends SchemaValidator> = ValidSchemaObject<SV> | UnboxedObjectSchema<SchemaCatchall<SV>>;
|
12
|
+
/**
|
13
|
+
* Type representing the static schema of an entity mapper.
|
14
|
+
*
|
15
|
+
* @template T - A type that extends BaseEntityMapper with a schema property.
|
16
|
+
* @typedef {Schema<T['schema'], T['_SV']> & {}} EntityMapperStaticSchema
|
17
|
+
*/
|
18
|
+
export type EntityMapperStaticSchema<T extends BaseEntityMapper<any> & {
|
19
|
+
schema: any;
|
20
|
+
}> = Schema<T['schema'], T['_SV']>;
|
21
|
+
/**
|
22
|
+
* Type representing the schema of an entity mapper.
|
23
|
+
*
|
24
|
+
* @template T - A type that extends ValidSchemaObject or UnboxedObjectSchema with SchemaCatchall.
|
25
|
+
* @template SV - A type that extends SchemaValidator.
|
26
|
+
* @typedef {Schema<T, SV> & {}} EntityMapperSchema
|
27
|
+
*/
|
28
|
+
export type EntityMapperSchema<T extends ValidSchemaObject<SV> | UnboxedObjectSchema<SchemaCatchall<SV>>, SV extends SchemaValidator> = Schema<T, SV>;
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"entityMapper.types.js","sourceRoot":"","sources":["../../../entityMapper/types/entityMapper.types.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { SchemaValidator } from "@forklaunch/validator/interfaces";
|
2
|
+
import { ForklaunchNextFunction, ForklaunchRequest, ForklaunchResponse } from "../types/api.types";
|
3
|
+
import { HttpContractDetails, PathParamHttpContractDetails, StringOnlyObject } from "../types/primitive.types";
|
4
|
+
export declare function createRequestContext<Request extends ForklaunchRequest<any, any, any, any>, Response extends ForklaunchResponse<any, any>>(schemaValidator: SchemaValidator): (req: Request, res: Response, next?: Function) => void;
|
5
|
+
export declare function enrichRequestDetails<SV extends SchemaValidator, Request extends ForklaunchRequest<any, any, any, any>, Response extends ForklaunchResponse<any, any>>(contractDetails: PathParamHttpContractDetails<SV> | HttpContractDetails<SV>): (req: Request, _res: Response, next?: Function) => void;
|
6
|
+
export declare function preHandlerParse<SV extends SchemaValidator>(schemaValidator: SV, object: unknown, schemaInput?: StringOnlyObject<SV>): 400 | undefined;
|
7
|
+
export declare function parseRequestParams<Request extends ForklaunchRequest<any, any, any, any>, Response extends ForklaunchResponse<any, any>, NextFunction extends ForklaunchNextFunction>(req: Request, res: Response, next?: NextFunction): void;
|
8
|
+
export declare function parseRequestBody<Request extends ForklaunchRequest<any, any, any, any>, Response extends ForklaunchResponse<any, any>, NextFunction extends ForklaunchNextFunction>(req: Request, res: Response, next?: NextFunction): void;
|
9
|
+
export declare function parseRequestHeaders<Request extends ForklaunchRequest<any, any, any, any>, Response extends ForklaunchResponse<any, any>, NextFunction extends ForklaunchNextFunction>(req: Request, res: Response, next?: NextFunction): void;
|
10
|
+
export declare function parseRequestQuery<Request extends ForklaunchRequest<any, any, any, any>, Response extends ForklaunchResponse<any, any>, NextFunction extends ForklaunchNextFunction>(req: Request, res: Response, next?: NextFunction): void;
|
11
|
+
export declare function parseRequestAuth<Request extends ForklaunchRequest<any, any, any, any>, Response extends ForklaunchResponse<any, any>, NextFunction extends ForklaunchNextFunction>(req: Request, res: Response, next?: NextFunction): Promise<void>;
|
@@ -0,0 +1,187 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
+
if (mod && mod.__esModule) return mod;
|
20
|
+
var result = {};
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
+
__setModuleDefault(result, mod);
|
23
|
+
return result;
|
24
|
+
};
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
26
|
+
exports.parseRequestAuth = exports.parseRequestQuery = exports.parseRequestHeaders = exports.parseRequestBody = exports.parseRequestParams = exports.preHandlerParse = exports.enrichRequestDetails = exports.createRequestContext = void 0;
|
27
|
+
const jose = __importStar(require("jose"));
|
28
|
+
const uuid_1 = require("uuid");
|
29
|
+
function createRequestContext(schemaValidator) {
|
30
|
+
return (req, res, next) => {
|
31
|
+
req.schemaValidator = schemaValidator;
|
32
|
+
let correlationId = (0, uuid_1.v4)();
|
33
|
+
if (req.headers['x-correlation-id']) {
|
34
|
+
correlationId = req.headers['x-correlation-id'];
|
35
|
+
}
|
36
|
+
res.setHeader('x-correlation-id', correlationId);
|
37
|
+
req.context = {
|
38
|
+
correlationId: correlationId
|
39
|
+
};
|
40
|
+
if (next) {
|
41
|
+
next();
|
42
|
+
}
|
43
|
+
};
|
44
|
+
}
|
45
|
+
exports.createRequestContext = createRequestContext;
|
46
|
+
function enrichRequestDetails(contractDetails) {
|
47
|
+
return (req, _res, next) => {
|
48
|
+
req.contractDetails = contractDetails;
|
49
|
+
if (next) {
|
50
|
+
next();
|
51
|
+
}
|
52
|
+
};
|
53
|
+
}
|
54
|
+
exports.enrichRequestDetails = enrichRequestDetails;
|
55
|
+
function preHandlerParse(schemaValidator, object, schemaInput) {
|
56
|
+
if (!schemaInput) {
|
57
|
+
return;
|
58
|
+
}
|
59
|
+
const schema = schemaValidator.schemify(schemaInput);
|
60
|
+
if (!schemaValidator.validate(schema, object)) {
|
61
|
+
return 400;
|
62
|
+
}
|
63
|
+
}
|
64
|
+
exports.preHandlerParse = preHandlerParse;
|
65
|
+
function parseRequestParams(req, res, next) {
|
66
|
+
const params = req.contractDetails.params;
|
67
|
+
if (preHandlerParse(req.schemaValidator, req.params, params) === 400) {
|
68
|
+
res.status(400).send("Invalid request parameters.");
|
69
|
+
if (next) {
|
70
|
+
next(new Error("Invalid request parameters."));
|
71
|
+
}
|
72
|
+
}
|
73
|
+
;
|
74
|
+
if (next) {
|
75
|
+
next();
|
76
|
+
}
|
77
|
+
}
|
78
|
+
exports.parseRequestParams = parseRequestParams;
|
79
|
+
function parseRequestBody(req, res, next) {
|
80
|
+
if (req.headers['content-type'] === 'application/json') {
|
81
|
+
const body = (req.schemaValidator, req.contractDetails).body;
|
82
|
+
if (preHandlerParse(req.body, body) === 400) {
|
83
|
+
res.status(400).send("Invalid request body.");
|
84
|
+
if (next) {
|
85
|
+
next(new Error("Invalid request body."));
|
86
|
+
}
|
87
|
+
}
|
88
|
+
}
|
89
|
+
if (next) {
|
90
|
+
next();
|
91
|
+
}
|
92
|
+
}
|
93
|
+
exports.parseRequestBody = parseRequestBody;
|
94
|
+
function parseRequestHeaders(req, res, next) {
|
95
|
+
const headers = req.contractDetails.requestHeaders;
|
96
|
+
if (preHandlerParse(req.schemaValidator, req.headers, headers) === 400) {
|
97
|
+
res.status(400).send("Invalid request headers.");
|
98
|
+
if (next) {
|
99
|
+
next(new Error("Invalid request headers."));
|
100
|
+
}
|
101
|
+
}
|
102
|
+
if (next) {
|
103
|
+
next();
|
104
|
+
}
|
105
|
+
}
|
106
|
+
exports.parseRequestHeaders = parseRequestHeaders;
|
107
|
+
function parseRequestQuery(req, res, next) {
|
108
|
+
const query = req.contractDetails.query;
|
109
|
+
if (preHandlerParse(req.query, query) === 400) {
|
110
|
+
res.status(400).send("Invalid request query.");
|
111
|
+
if (next) {
|
112
|
+
next(new Error("Invalid request query."));
|
113
|
+
}
|
114
|
+
}
|
115
|
+
if (next) {
|
116
|
+
next();
|
117
|
+
}
|
118
|
+
}
|
119
|
+
exports.parseRequestQuery = parseRequestQuery;
|
120
|
+
async function checkAuthorizationToken(authorizationMethod, authorizationString) {
|
121
|
+
if (!authorizationString) {
|
122
|
+
return [401, "No Authorization token provided."];
|
123
|
+
}
|
124
|
+
switch (authorizationMethod) {
|
125
|
+
case 'jwt': {
|
126
|
+
if (!authorizationString.startsWith('Bearer ')) {
|
127
|
+
return [401, "Invalid Authorization token format."];
|
128
|
+
}
|
129
|
+
try {
|
130
|
+
const decodedJwt = await jose.jwtVerify(authorizationString.split(' ')[1], new TextEncoder().encode(process.env.JWT_SECRET || 'your-256-bit-secret'));
|
131
|
+
return decodedJwt.payload.iss;
|
132
|
+
}
|
133
|
+
catch (error) {
|
134
|
+
console.error(error);
|
135
|
+
return [403, "Invalid Authorization token."];
|
136
|
+
}
|
137
|
+
}
|
138
|
+
default:
|
139
|
+
return [401, "Invalid Authorization method."];
|
140
|
+
}
|
141
|
+
}
|
142
|
+
function mapRoles(authorizationType, authorizationToken) {
|
143
|
+
return [];
|
144
|
+
}
|
145
|
+
function mapPermissions(authorizationType, authorizationToken) {
|
146
|
+
return [];
|
147
|
+
}
|
148
|
+
async function parseRequestAuth(req, res, next) {
|
149
|
+
const auth = req.contractDetails.auth;
|
150
|
+
if (auth) {
|
151
|
+
const errorAndMessage = await checkAuthorizationToken(auth.method, req.headers.authorization);
|
152
|
+
if (Array.isArray(errorAndMessage)) {
|
153
|
+
res.status(errorAndMessage[0]).send(errorAndMessage[1]);
|
154
|
+
if (next) {
|
155
|
+
next(new Error(errorAndMessage[1]));
|
156
|
+
}
|
157
|
+
}
|
158
|
+
// TODO: Implement role and permission checking
|
159
|
+
const permissionSlugs = mapPermissions(auth.method, req.headers.authorization);
|
160
|
+
const roles = mapRoles(auth.method, req.headers.authorization);
|
161
|
+
const permissionErrorMessage = "User does not have sufficient permissions to perform action.";
|
162
|
+
const roleErrorMessage = "User does not have correct role to perform action.";
|
163
|
+
// this is wrong, we need to check if any of the user's permissions are in the allowed permissions, while checking that any of the permissions is not in the forbidden slugs
|
164
|
+
// currently this is checking if any of the user's permissions are NOT in the allowed permissions
|
165
|
+
permissionSlugs.forEach(permissionSlug => {
|
166
|
+
if (!req.contractDetails.auth?.allowedSlugs?.has(permissionSlug) || req.contractDetails.auth?.forbiddenSlugs?.has(permissionSlug)) {
|
167
|
+
res.status(403).send(permissionErrorMessage);
|
168
|
+
if (next) {
|
169
|
+
next(new Error(permissionErrorMessage));
|
170
|
+
}
|
171
|
+
}
|
172
|
+
});
|
173
|
+
roles.forEach(role => {
|
174
|
+
if (!req.contractDetails.auth?.allowedRoles?.has(role) || req.contractDetails.auth?.forbiddenRoles?.has(role)) {
|
175
|
+
res.status(403).send(roleErrorMessage);
|
176
|
+
if (next) {
|
177
|
+
next(new Error(roleErrorMessage));
|
178
|
+
}
|
179
|
+
}
|
180
|
+
});
|
181
|
+
}
|
182
|
+
// if (next) {
|
183
|
+
// next();
|
184
|
+
// }
|
185
|
+
}
|
186
|
+
exports.parseRequestAuth = parseRequestAuth;
|
187
|
+
//# sourceMappingURL=request.middleware.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"request.middleware.js","sourceRoot":"","sources":["../../../http/middlewares/request.middleware.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AACA,2CAA6B;AAC7B,+BAA0B;AAI1B,SAAgB,oBAAoB,CAGlC,eAAgC;IAC9B,OAAO,CAAC,GAAY,EAAE,GAAa,EAAE,IAAe,EAAE,EAAE;QACpD,GAAG,CAAC,eAAe,GAAG,eAAe,CAAC;QAEtC,IAAI,aAAa,GAAG,IAAA,SAAE,GAAE,CAAC;QAEzB,IAAI,GAAG,CAAC,OAAO,CAAC,kBAAkB,CAAC,EAAE;YACjC,aAAa,GAAG,GAAG,CAAC,OAAO,CAAC,kBAAkB,CAAW,CAAC;SAC7D;QAED,GAAG,CAAC,SAAS,CA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CAAC,sBAAsB,CAAC,CAAC,CAAC;iBAC3C;aACJ;QACL,CAAC,CAAC,CAAC;QACH,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;YACjB,IAAI,CAAC,GAAG,CAAC,eAAe,CAAC,IAAI,EAAE,YAAY,EAAE,GAAG,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,eAAe,CAAC,IAAI,EAAE,cAAc,EAAE,GAAG,CAAC,IAAI,CAAC,EAAE;gBAC3G,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;gBACvC,IAAI,IAAI,EAAE;oBACN,IAAI,CAAC,IAAI,KAAK,CAAC,gBAAgB,CAAC,CAAC,CAAC;iBACrC;aACJ;QACL,CAAC,CAAC,CAAC;KACN;IAED,cAAc;IACd,cAAc;IACd,IAAI;AACR,CAAC;AA7CD,4CA6CC"}
|
@@ -0,0 +1,2 @@
|
|
1
|
+
import { ForklaunchNextFunction, ForklaunchRequest, ForklaunchResponse } from "../types/api.types";
|
2
|
+
export declare function parseResponse<Request extends ForklaunchRequest<any, any, any, any>, Response extends ForklaunchResponse<any, any>, NextFunction extends ForklaunchNextFunction>(req: Request, res: Response, next?: NextFunction): void;
|
@@ -0,0 +1,30 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.parseResponse = void 0;
|
4
|
+
function checkAnyValidation(contractDetails) {
|
5
|
+
return contractDetails.body || contractDetails.params || contractDetails.requestHeaders || contractDetails.query;
|
6
|
+
}
|
7
|
+
function parseResponse(req, res, next) {
|
8
|
+
if (req.contractDetails.responseHeaders) {
|
9
|
+
const schema = req.schemaValidator.schemify(req.contractDetails.responseHeaders);
|
10
|
+
req.schemaValidator.validate(schema, res.getHeaders());
|
11
|
+
}
|
12
|
+
if (res.statusCode === 500 ||
|
13
|
+
(checkAnyValidation(req.contractDetails) && res.statusCode === 400) ||
|
14
|
+
(req.contractDetails.auth && (res.statusCode === 401 || res.statusCode === 403))) {
|
15
|
+
return;
|
16
|
+
}
|
17
|
+
if (!req.contractDetails.responses.hasOwnProperty(res.statusCode)) {
|
18
|
+
if (next) {
|
19
|
+
next(new Error(`Response code ${res.statusCode} not defined in contract.`));
|
20
|
+
}
|
21
|
+
;
|
22
|
+
}
|
23
|
+
const schema = req.schemaValidator.schemify(req.contractDetails.responses[res.statusCode]);
|
24
|
+
req.schemaValidator.validate(schema, res.bodyData);
|
25
|
+
if (next) {
|
26
|
+
next();
|
27
|
+
}
|
28
|
+
}
|
29
|
+
exports.parseResponse = parseResponse;
|
30
|
+
//# sourceMappingURL=response.middleware.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"response.middleware.js","sourceRoot":"","sources":["../../../http/middlewares/response.middleware.ts"],"names":[],"mappings":";;;AAIA,SAAS,kBAAkB,CAA6B,eAAwC;IAC5F,OAAO,eAAe,CAAC,IAAI,IAAI,eAAe,CAAC,MAAM,IAAI,eAAe,CAAC,cAAc,IAAI,eAAe,CAAC,KAAK,CAAC;AACrH,CAAC;AAED,SAAgB,aAAa,CAI1B,GAAY,EAAE,GAAa,EAAE,IAAmB;IAC/C,IAAI,GAAG,CAAC,eAAe,CAAC,eAAe,EAAE;QACrC,MAAM,MAAM,GAAG,GAAG,CAAC,eAAe,CAAC,QAAQ,CAAC,GAAG,CAAC,eAAe,CAAC,eAAe,CAAC,CAAC;QACjF,GAAG,CAAC,eAAe,CAAC,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC,UAAU,EAAE,CAAC,CAAC;KAC1D;IAED,IAAI,GAAG,CAAC,UAAU,KAAK,GAAG;QACtB,CAAC,kBAAkB,CAAC,GAAG,CAAC,eAAe,CAAC,IAAI,GAAG,CAAC,UAAU,KAAK,GAAG,CAAC;QACnE,CAAC,GAAG,CAAC,eAAe,CAAC,IAAI,IAAI,CAAC,GAAG,CAAC,UAAU,KAAK,GAAG,IAAI,GAAG,CAAC,UAAU,KAAK,GAAG,CAAC,CAAC,EAClF;QACE,OAAO;KACV;IACD,IAAI,CAAC,GAAG,CAAC,eAAe,CAAC,SAAS,CAAC,cAAc,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE;QAC/D,IAAI,IAAI,EAAE;YACN,IAAI,CAAC,IAAI,KAAK,CAAC,iBAAiB,GAAG,CAAC,UAAU,2BAA2B,CAAC,CAAC,CAAC;SAC/E;QAAA,CAAC;KACL;IAED,MAAM,MAAM,GAAG,GAAG,CAAC,eAAe,CAAC,QAAQ,CAAC,GAAG,CAAC,eAAe,CAAC,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC;IAC3F,GAAG,CAAC,eAAe,CAAC,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC;IAEnD,IAAI,IAAI,EAAE;QACN,IAAI,EAAE,CAAC;KACV;AACL,CAAC;AA5BD,sCA4BC"}
|
@@ -0,0 +1,50 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
import { Prettify } from "@forklaunch/common";
|
3
|
+
import { Schema, SchemaCatchall, ValidSchemaObject } from "@forklaunch/validator";
|
4
|
+
import { SchemaValidator } from "@forklaunch/validator/interfaces";
|
5
|
+
import { IdiomaticSchema } from "@forklaunch/validator/types";
|
6
|
+
import { IncomingHttpHeaders, OutgoingHttpHeader } from "http";
|
7
|
+
import { ParsedQs } from "qs";
|
8
|
+
import { HttpContractDetails, ParamsDictionary, PathParamHttpContractDetails } from "./primitive.types";
|
9
|
+
export interface RequestContext {
|
10
|
+
correlationId: string;
|
11
|
+
idempotencyKey?: string;
|
12
|
+
}
|
13
|
+
export interface ForklaunchRequest<SV extends SchemaValidator, P = ParamsDictionary, ReqBody = any, ReqQuery = ParsedQs, Headers = IncomingHttpHeaders> {
|
14
|
+
context: Prettify<RequestContext>;
|
15
|
+
contractDetails: HttpContractDetails<SV> | PathParamHttpContractDetails<SV>;
|
16
|
+
schemaValidator: SV;
|
17
|
+
params: P;
|
18
|
+
headers: Headers;
|
19
|
+
body: ReqBody;
|
20
|
+
query: ReqQuery;
|
21
|
+
}
|
22
|
+
export interface ForklaunchResponse<ResBody = any, StatusCode = number> {
|
23
|
+
bodyData: unknown;
|
24
|
+
statusCode: StatusCode;
|
25
|
+
corked: boolean;
|
26
|
+
getHeaders: () => OutgoingHttpHeader;
|
27
|
+
setHeader: (key: string, value: string) => void;
|
28
|
+
status: {
|
29
|
+
<U extends keyof ResBody>(code: U): ForklaunchResponse<ResBody[U], U>;
|
30
|
+
<U extends keyof ResBody>(code: U, message?: string): ForklaunchResponse<ResBody[U], U>;
|
31
|
+
<U extends 500>(code: U): ForklaunchResponse<string, U>;
|
32
|
+
<U extends 500>(code: U, message?: string): ForklaunchResponse<string, U>;
|
33
|
+
};
|
34
|
+
send: {
|
35
|
+
<T>(body?: ResBody, close_connection?: boolean): T;
|
36
|
+
<T>(body?: ResBody): T;
|
37
|
+
};
|
38
|
+
json: {
|
39
|
+
(body?: ResBody): boolean;
|
40
|
+
<T>(body?: ResBody): T;
|
41
|
+
};
|
42
|
+
jsonp: {
|
43
|
+
(body?: ResBody): boolean;
|
44
|
+
<T>(body?: ResBody): T;
|
45
|
+
};
|
46
|
+
}
|
47
|
+
export type MapSchema<SV extends SchemaValidator, T extends IdiomaticSchema<SchemaCatchall<SV>> | ValidSchemaObject<SV>> = Schema<T, SV> extends infer U ? {
|
48
|
+
[key: string]: unknown;
|
49
|
+
} extends U ? never : U : never;
|
50
|
+
export type ForklaunchNextFunction = (err?: any) => void;
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"api.types.js","sourceRoot":"","sources":["../../../http/types/api.types.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
import { SchemaCatchall, ValidSchemaObject } from "@forklaunch/validator";
|
2
|
+
import { SchemaValidator } from "@forklaunch/validator/interfaces";
|
3
|
+
import { UnboxedObjectSchema } from "@forklaunch/validator/types";
|
4
|
+
export type ParamsDictionary = {
|
5
|
+
[key: string]: string;
|
6
|
+
};
|
7
|
+
export type StringOnlyObject<SV extends SchemaValidator> = Omit<UnboxedObjectSchema<SchemaCatchall<SV>>, number | symbol>;
|
8
|
+
export type NumberOnlyObject<SV extends SchemaValidator> = Omit<UnboxedObjectSchema<SchemaCatchall<SV>>, string | symbol>;
|
9
|
+
export type BodyObject<SV extends SchemaValidator> = StringOnlyObject<SV> & unknown;
|
10
|
+
export type ParamsObject<SV extends SchemaValidator> = StringOnlyObject<SV> & unknown;
|
11
|
+
export type QueryObject<SV extends SchemaValidator> = StringOnlyObject<SV> & unknown;
|
12
|
+
export type HeadersObject<SV extends SchemaValidator> = StringOnlyObject<SV> & unknown;
|
13
|
+
export type ResponsesObject<SV extends SchemaValidator> = NumberOnlyObject<SV> & unknown;
|
14
|
+
export type Body<SV extends SchemaValidator> = BodyObject<SV> | ValidSchemaObject<SV> | SchemaCatchall<SV>;
|
15
|
+
export type AuthMethod = 'jwt' | 'session';
|
16
|
+
export interface PathParamHttpContractDetails<SV extends SchemaValidator, ParamSchemas extends ParamsObject<SV> = ParamsObject<SV>, ResponseSchemas extends ResponsesObject<SV> = ResponsesObject<SV>, QuerySchemas extends QueryObject<SV> = QueryObject<SV>> {
|
17
|
+
name: string;
|
18
|
+
summary: string;
|
19
|
+
responses: ResponseSchemas;
|
20
|
+
requestHeaders?: HeadersObject<SV>;
|
21
|
+
responseHeaders?: HeadersObject<SV>;
|
22
|
+
params?: ParamSchemas;
|
23
|
+
query?: QuerySchemas;
|
24
|
+
auth?: {
|
25
|
+
method: AuthMethod;
|
26
|
+
allowedSlugs?: Set<string>;
|
27
|
+
forbiddenSlugs?: Set<string>;
|
28
|
+
allowedRoles?: Set<string>;
|
29
|
+
forbiddenRoles?: Set<string>;
|
30
|
+
};
|
31
|
+
}
|
32
|
+
export interface HttpContractDetails<SV extends SchemaValidator, ParamSchemas extends ParamsObject<SV> = ParamsObject<SV>, ResponseSchemas extends ResponsesObject<SV> = ResponsesObject<SV>, BodySchema extends Body<SV> = Body<SV>, QuerySchemas extends QueryObject<SV> = QueryObject<SV>> extends PathParamHttpContractDetails<SV, ParamSchemas, ResponseSchemas, QuerySchemas> {
|
33
|
+
body?: BodySchema;
|
34
|
+
contentType?: 'application/json' | 'multipart/form-data' | 'application/x-www-form-urlencoded';
|
35
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"primitive.types.js","sourceRoot":"","sources":["../../../http/types/primitive.types.ts"],"names":[],"mappings":""}
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
package/dist/index.js
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
// export * from './cache';
|
4
|
+
// export * from './controllers';
|
5
|
+
// export * from './database';
|
6
|
+
// export * from './entityMapper';
|
7
|
+
// export * from './services';
|
8
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;AAAA,2BAA2B;AAC3B,iCAAiC;AACjC,8BAA8B;AAC9B,kCAAkC;AAClC,8BAA8B"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"jest.config.js","sourceRoot":"","sources":["../jest.config.ts"],"names":[],"mappings":";;AAEA,MAAM,MAAM,GAAW;IACrB,OAAO,EAAE,IAAI;IACb,MAAM,EAAE,SAAS;IACjB,eAAe,EAAE,MAAM;IACvB,sBAAsB,EAAE,CAAC,OAAO,EAAE,eAAe,CAAC;CACnD,CAAC;AAEF,kBAAe,MAAM,CAAC"}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { EntityManager } from "@mikro-orm/core";
|
2
|
+
/**
|
3
|
+
* Interface representing a base service.
|
4
|
+
*
|
5
|
+
* @interface BaseService
|
6
|
+
*/
|
7
|
+
export default interface BaseService {
|
8
|
+
/**
|
9
|
+
* The EntityManager instance for managing entities.
|
10
|
+
*
|
11
|
+
* @type {EntityManager}
|
12
|
+
*/
|
13
|
+
em: EntityManager;
|
14
|
+
}
|