@congruent-stack/congruent-api-express 0.4.0 → 0.7.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/index.cjs +35 -3
- package/dist/index.d.cts +7 -2
- package/dist/index.d.mts +7 -2
- package/dist/index.mjs +35 -4
- package/package.json +6 -2
package/dist/index.cjs
CHANGED
|
@@ -2,8 +2,40 @@
|
|
|
2
2
|
|
|
3
3
|
var congruentApi = require('@congruent-stack/congruent-api');
|
|
4
4
|
|
|
5
|
-
function
|
|
6
|
-
|
|
5
|
+
function createExpressRegistry(app, diContainer, apiContract) {
|
|
6
|
+
const registry = congruentApi.createRegistry(diContainer, apiContract, {
|
|
7
|
+
handlerRegisteredCallback: (entry) => {
|
|
8
|
+
const { genericPath } = entry.methodEndpoint;
|
|
9
|
+
const method = entry.methodEndpoint.method.toLowerCase();
|
|
10
|
+
app[method](genericPath, async (req, res) => {
|
|
11
|
+
req.pathParams = req.params;
|
|
12
|
+
const result = await entry.trigger(req);
|
|
13
|
+
const resultHeaders = new Map(
|
|
14
|
+
Object.entries(result.headers || {})
|
|
15
|
+
);
|
|
16
|
+
res.status(result.code).setHeaders(resultHeaders).json(result.body);
|
|
17
|
+
});
|
|
18
|
+
},
|
|
19
|
+
middlewareHandlerRegisteredCallback: (entry) => {
|
|
20
|
+
app.use(entry.genericPath, async (req, res, next) => {
|
|
21
|
+
req.pathParams = req.params;
|
|
22
|
+
const haltResult = await entry.trigger(req, next);
|
|
23
|
+
if (haltResult && congruentApi.isHttpResponseObject(haltResult)) {
|
|
24
|
+
const haltResultHeaders = new Map(
|
|
25
|
+
Object.entries(haltResult.headers || {})
|
|
26
|
+
);
|
|
27
|
+
res.status(haltResult.code).setHeaders(haltResultHeaders).json(haltResult.body);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
return registry;
|
|
33
|
+
}
|
|
34
|
+
function expressPreHandler(app, prehandler) {
|
|
35
|
+
return (({ methodEndpoint: { lowerCasedMethod, genericPath } }) => {
|
|
36
|
+
app[lowerCasedMethod](genericPath, prehandler);
|
|
37
|
+
});
|
|
7
38
|
}
|
|
8
39
|
|
|
9
|
-
exports.
|
|
40
|
+
exports.createExpressRegistry = createExpressRegistry;
|
|
41
|
+
exports.expressPreHandler = expressPreHandler;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import * as _congruent_stack_congruent_api from '@congruent-stack/congruent-api';
|
|
2
|
+
import { IApiContractDefinition, ValidateApiContractDefinition, DIContainer, ApiContract, IHttpMethodEndpointDefinition, ValidateHttpMethodEndpointDefinition, PrepareRegistryEntryCallback } from '@congruent-stack/congruent-api';
|
|
3
|
+
import { Express, RequestHandler } from 'express';
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
declare function createExpressRegistry<TDef extends IApiContractDefinition & ValidateApiContractDefinition<TDef>, TDIContainer extends DIContainer>(app: Express, diContainer: TDIContainer, apiContract: ApiContract<TDef>): _congruent_stack_congruent_api.ApiHandlersRegistry<TDef, TDIContainer, "">;
|
|
6
|
+
declare function expressPreHandler<TDef extends IHttpMethodEndpointDefinition & ValidateHttpMethodEndpointDefinition<TDef>, TDIContainer extends DIContainer, TPathParams extends string>(app: Express, prehandler: RequestHandler): PrepareRegistryEntryCallback<TDef, TDIContainer, TPathParams>;
|
|
7
|
+
|
|
8
|
+
export { createExpressRegistry, expressPreHandler };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import * as _congruent_stack_congruent_api from '@congruent-stack/congruent-api';
|
|
2
|
+
import { IApiContractDefinition, ValidateApiContractDefinition, DIContainer, ApiContract, IHttpMethodEndpointDefinition, ValidateHttpMethodEndpointDefinition, PrepareRegistryEntryCallback } from '@congruent-stack/congruent-api';
|
|
3
|
+
import { Express, RequestHandler } from 'express';
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
declare function createExpressRegistry<TDef extends IApiContractDefinition & ValidateApiContractDefinition<TDef>, TDIContainer extends DIContainer>(app: Express, diContainer: TDIContainer, apiContract: ApiContract<TDef>): _congruent_stack_congruent_api.ApiHandlersRegistry<TDef, TDIContainer, "">;
|
|
6
|
+
declare function expressPreHandler<TDef extends IHttpMethodEndpointDefinition & ValidateHttpMethodEndpointDefinition<TDef>, TDIContainer extends DIContainer, TPathParams extends string>(app: Express, prehandler: RequestHandler): PrepareRegistryEntryCallback<TDef, TDIContainer, TPathParams>;
|
|
7
|
+
|
|
8
|
+
export { createExpressRegistry, expressPreHandler };
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,38 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createRegistry, isHttpResponseObject } from '@congruent-stack/congruent-api';
|
|
2
2
|
|
|
3
|
-
function
|
|
4
|
-
|
|
3
|
+
function createExpressRegistry(app, diContainer, apiContract) {
|
|
4
|
+
const registry = createRegistry(diContainer, apiContract, {
|
|
5
|
+
handlerRegisteredCallback: (entry) => {
|
|
6
|
+
const { genericPath } = entry.methodEndpoint;
|
|
7
|
+
const method = entry.methodEndpoint.method.toLowerCase();
|
|
8
|
+
app[method](genericPath, async (req, res) => {
|
|
9
|
+
req.pathParams = req.params;
|
|
10
|
+
const result = await entry.trigger(req);
|
|
11
|
+
const resultHeaders = new Map(
|
|
12
|
+
Object.entries(result.headers || {})
|
|
13
|
+
);
|
|
14
|
+
res.status(result.code).setHeaders(resultHeaders).json(result.body);
|
|
15
|
+
});
|
|
16
|
+
},
|
|
17
|
+
middlewareHandlerRegisteredCallback: (entry) => {
|
|
18
|
+
app.use(entry.genericPath, async (req, res, next) => {
|
|
19
|
+
req.pathParams = req.params;
|
|
20
|
+
const haltResult = await entry.trigger(req, next);
|
|
21
|
+
if (haltResult && isHttpResponseObject(haltResult)) {
|
|
22
|
+
const haltResultHeaders = new Map(
|
|
23
|
+
Object.entries(haltResult.headers || {})
|
|
24
|
+
);
|
|
25
|
+
res.status(haltResult.code).setHeaders(haltResultHeaders).json(haltResult.body);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
return registry;
|
|
31
|
+
}
|
|
32
|
+
function expressPreHandler(app, prehandler) {
|
|
33
|
+
return (({ methodEndpoint: { lowerCasedMethod, genericPath } }) => {
|
|
34
|
+
app[lowerCasedMethod](genericPath, prehandler);
|
|
35
|
+
});
|
|
5
36
|
}
|
|
6
37
|
|
|
7
|
-
export {
|
|
38
|
+
export { createExpressRegistry, expressPreHandler };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@congruent-stack/congruent-api-express",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.7.0",
|
|
5
5
|
"description": "Typescript schema-first tooling for agnostic REST APIs.",
|
|
6
6
|
"keywords": [],
|
|
7
7
|
"author": "congruent-stack",
|
|
@@ -20,9 +20,13 @@
|
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
|
-
"@
|
|
23
|
+
"@types/express": "5.0.3",
|
|
24
|
+
"express": "5.1.0",
|
|
25
|
+
"@congruent-stack/congruent-api": "0.7.0"
|
|
24
26
|
},
|
|
25
27
|
"devDependencies": {
|
|
28
|
+
"@types/express": "5.0.3",
|
|
29
|
+
"express": "5.1.0",
|
|
26
30
|
"pkgroll": "2.14.5",
|
|
27
31
|
"typescript": "^5.9.2"
|
|
28
32
|
},
|