ts-procedures 2.0.1 → 2.1.1

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.
Files changed (40) hide show
  1. package/build/implementations/http/express-rpc/index.d.ts +40 -39
  2. package/build/implementations/http/express-rpc/index.js +40 -20
  3. package/build/implementations/http/express-rpc/index.js.map +1 -1
  4. package/build/implementations/http/express-rpc/index.test.js +138 -58
  5. package/build/implementations/http/express-rpc/index.test.js.map +1 -1
  6. package/build/implementations/http/express-rpc/types.d.ts +1 -1
  7. package/build/implementations/types.d.ts +1 -1
  8. package/package.json +1 -1
  9. package/src/implementations/http/express-rpc/README.md +49 -25
  10. package/src/implementations/http/express-rpc/index.test.ts +170 -58
  11. package/src/implementations/http/express-rpc/index.ts +74 -45
  12. package/src/implementations/http/express-rpc/types.ts +6 -2
  13. package/src/implementations/types.ts +1 -1
  14. package/build/implementations/http/client/index.d.ts +0 -1
  15. package/build/implementations/http/client/index.js +0 -2
  16. package/build/implementations/http/client/index.js.map +0 -1
  17. package/build/implementations/http/express/example/factories.d.ts +0 -97
  18. package/build/implementations/http/express/example/factories.js +0 -4
  19. package/build/implementations/http/express/example/factories.js.map +0 -1
  20. package/build/implementations/http/express/example/procedures/auth.d.ts +0 -1
  21. package/build/implementations/http/express/example/procedures/auth.js +0 -22
  22. package/build/implementations/http/express/example/procedures/auth.js.map +0 -1
  23. package/build/implementations/http/express/example/procedures/users.d.ts +0 -1
  24. package/build/implementations/http/express/example/procedures/users.js +0 -30
  25. package/build/implementations/http/express/example/procedures/users.js.map +0 -1
  26. package/build/implementations/http/express/example/server.d.ts +0 -3
  27. package/build/implementations/http/express/example/server.js +0 -49
  28. package/build/implementations/http/express/example/server.js.map +0 -1
  29. package/build/implementations/http/express/example/server.test.d.ts +0 -1
  30. package/build/implementations/http/express/example/server.test.js +0 -110
  31. package/build/implementations/http/express/example/server.test.js.map +0 -1
  32. package/build/implementations/http/express/index.d.ts +0 -35
  33. package/build/implementations/http/express/index.js +0 -75
  34. package/build/implementations/http/express/index.js.map +0 -1
  35. package/build/implementations/http/express/index.test.d.ts +0 -1
  36. package/build/implementations/http/express/index.test.js +0 -329
  37. package/build/implementations/http/express/index.test.js.map +0 -1
  38. package/build/implementations/http/express/types.d.ts +0 -17
  39. package/build/implementations/http/express/types.js +0 -2
  40. package/build/implementations/http/express/types.js.map +0 -1
@@ -1,97 +0,0 @@
1
- export interface ProceduresContext {
2
- headers: Record<string, string>;
3
- ipAddress: string;
4
- requestId: string;
5
- }
6
- export interface ProtectedContext extends ProceduresContext {
7
- userId: string;
8
- }
9
- export interface PublicContext extends ProceduresContext {
10
- userId?: undefined;
11
- }
12
- export declare const ProtectedFactory: {
13
- getProcedures: () => import("../../../../index.js").TProcedureRegistration<ProtectedContext, HTTPRouteConfig>[];
14
- Create: <TName extends string, TParams, TReturnType>(name: TName, config: {
15
- description?: string;
16
- schema?: {
17
- params?: TParams | undefined;
18
- returnType?: TReturnType | undefined;
19
- } | undefined;
20
- } & HTTPRouteConfig, handler: (ctx: {
21
- userId: string;
22
- headers: Record<string, string>;
23
- ipAddress: string;
24
- requestId: string;
25
- error: (message: string, meta?: object) => import("../../../../errors.js").ProcedureError;
26
- }, params: import("../../../../exports.js").TSchemaLib<TParams>) => Promise<import("../../../../exports.js").TSchemaLib<TReturnType>>) => { [K in TName]: (ctx: {
27
- userId: string;
28
- headers: Record<string, string>;
29
- ipAddress: string;
30
- requestId: string;
31
- }, params: import("../../../../exports.js").TSchemaLib<TParams>) => Promise<import("../../../../exports.js").TSchemaLib<TReturnType>>; } & {
32
- procedure: (ctx: {
33
- userId: string;
34
- headers: Record<string, string>;
35
- ipAddress: string;
36
- requestId: string;
37
- }, params: import("../../../../exports.js").TSchemaLib<TParams>) => Promise<import("../../../../exports.js").TSchemaLib<TReturnType>>;
38
- info: {
39
- name: TName;
40
- description?: string;
41
- schema: {
42
- params?: TParams | undefined;
43
- returnType?: TReturnType | undefined;
44
- };
45
- validation?: {
46
- params?: ((params: any) => {
47
- errors?: any[];
48
- }) | undefined;
49
- } | undefined;
50
- } & HTTPRouteConfig;
51
- };
52
- };
53
- export declare const PublicFactory: {
54
- getProcedures: () => import("../../../../index.js").TProcedureRegistration<PublicContext, HTTPRouteConfig>[];
55
- Create: <TName extends string, TParams, TReturnType>(name: TName, config: {
56
- description?: string;
57
- schema?: {
58
- params?: TParams | undefined;
59
- returnType?: TReturnType | undefined;
60
- } | undefined;
61
- } & HTTPRouteConfig, handler: (ctx: {
62
- userId?: undefined | undefined;
63
- headers: Record<string, string>;
64
- ipAddress: string;
65
- requestId: string;
66
- error: (message: string, meta?: object) => import("../../../../errors.js").ProcedureError;
67
- }, params: import("../../../../exports.js").TSchemaLib<TParams>) => Promise<import("../../../../exports.js").TSchemaLib<TReturnType>>) => { [K in TName]: (ctx: {
68
- userId?: undefined | undefined;
69
- headers: Record<string, string>;
70
- ipAddress: string;
71
- requestId: string;
72
- }, params: import("../../../../exports.js").TSchemaLib<TParams>) => Promise<import("../../../../exports.js").TSchemaLib<TReturnType>>; } & {
73
- procedure: (ctx: {
74
- userId?: undefined | undefined;
75
- headers: Record<string, string>;
76
- ipAddress: string;
77
- requestId: string;
78
- }, params: import("../../../../exports.js").TSchemaLib<TParams>) => Promise<import("../../../../exports.js").TSchemaLib<TReturnType>>;
79
- info: {
80
- name: TName;
81
- description?: string;
82
- schema: {
83
- params?: TParams | undefined;
84
- returnType?: TReturnType | undefined;
85
- };
86
- validation?: {
87
- params?: ((params: any) => {
88
- errors?: any[];
89
- }) | undefined;
90
- } | undefined;
91
- } & HTTPRouteConfig;
92
- };
93
- };
94
- export interface HTTPRouteConfig {
95
- path: string;
96
- method: 'get' | 'post' | 'put' | 'delete' | 'patch';
97
- }
@@ -1,4 +0,0 @@
1
- import { Procedures } from '../../../../index.js';
2
- export const ProtectedFactory = Procedures();
3
- export const PublicFactory = Procedures();
4
- //# sourceMappingURL=factories.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"factories.js","sourceRoot":"","sources":["../../../../../src/implementations/http/express/example/factories.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AAiBjD,MAAM,CAAC,MAAM,gBAAgB,GAAG,UAAU,EAAoC,CAAA;AAE9E,MAAM,CAAC,MAAM,aAAa,GAAG,UAAU,EAAiC,CAAA"}
@@ -1,22 +0,0 @@
1
- import { PublicFactory } from '../factories.js';
2
- import { Type } from 'typebox';
3
- PublicFactory.Create('Authenticate', {
4
- path: '/authenticate',
5
- method: 'post',
6
- schema: {
7
- params: Type.Object({
8
- username: Type.String({ minLength: 3 }),
9
- password: Type.String({ minLength: 6 }),
10
- }),
11
- returnType: Type.Object({
12
- token: Type.String(),
13
- })
14
- },
15
- description: 'Authenticate as a user and obtain a token',
16
- }, async (ctx, params) => {
17
- // In a real implementation, you would verify user credentials here, ie: ctx.services.userService.authenticate(params.username, params.password)
18
- return {
19
- token: 'fake-jwt-token',
20
- };
21
- });
22
- //# sourceMappingURL=auth.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"auth.js","sourceRoot":"","sources":["../../../../../../src/implementations/http/express/example/procedures/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAE9B,aAAa,CAAC,MAAM,CAAC,cAAc,EAAE;IACnC,IAAI,EAAE,eAAe;IACrB,MAAM,EAAE,MAAM;IACd,MAAM,EAAE;QACN,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC;YAClB,QAAQ,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,CAAC;YACvC,QAAQ,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,SAAS,EAAE,CAAC,EAAG,CAAC;SACzC,CAAC;QACF,UAAU,EAAE,IAAI,CAAC,MAAM,CAAC;YACtB,KAAK,EAAE,IAAI,CAAC,MAAM,EAAE;SACrB,CAAC;KACH;IACD,WAAW,EAAE,2CAA2C;CACvD,EACD,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE;IACpB,gJAAgJ;IAEhJ,OAAO;QACL,KAAK,EAAE,gBAAgB;KACxB,CAAA;AACL,CAAC,CAAC,CAAA"}
@@ -1,30 +0,0 @@
1
- import { ProtectedFactory } from '../factories.js';
2
- import { Type } from 'typebox';
3
- ProtectedFactory.Create('Get User Profile', {
4
- path: '/users/user-profile/:id',
5
- method: 'get',
6
- schema: {
7
- params: Type.Object({
8
- // Our router in this example will map :id to this param
9
- id: Type.String(),
10
- }),
11
- returnType: Type.Object({
12
- user: Type.Object({
13
- id: Type.String(),
14
- name: Type.String(),
15
- email: Type.String(),
16
- }),
17
- })
18
- },
19
- description: 'Get the profile of a specific user',
20
- }, async (ctx, params) => {
21
- // In a real implementation, you would fetch the user profile from a database or service here, ie: ctx.services.userService.getUserProfile(params.userId)
22
- return {
23
- user: {
24
- id: params.id,
25
- name: 'Jane Doe',
26
- email: 'test@gmail.com'
27
- },
28
- };
29
- });
30
- //# sourceMappingURL=users.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"users.js","sourceRoot":"","sources":["../../../../../../src/implementations/http/express/example/procedures/users.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAE9B,gBAAgB,CAAC,MAAM,CAAC,kBAAkB,EAAE;IAC1C,IAAI,EAAE,yBAAyB;IAC/B,MAAM,EAAE,KAAK;IACb,MAAM,EAAE;QACN,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC;YAClB,wDAAwD;YACxD,EAAE,EAAE,IAAI,CAAC,MAAM,EAAE;SAClB,CAAC;QACF,UAAU,EAAE,IAAI,CAAC,MAAM,CAAC;YACtB,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC;gBAChB,EAAE,EAAE,IAAI,CAAC,MAAM,EAAE;gBACjB,IAAI,EAAE,IAAI,CAAC,MAAM,EAAE;gBACnB,KAAK,EAAE,IAAI,CAAC,MAAM,EAAE;aACrB,CAAC;SACH,CAAC;KACH;IACD,WAAW,EAAE,oCAAoC;CAChD,EACD,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE;IACpB,yJAAyJ;IAEzJ,OAAO;QACL,IAAI,EAAE;YACJ,EAAE,EAAE,MAAM,CAAC,EAAE;YACb,IAAI,EAAE,UAAU;YAChB,KAAK,EAAE,gBAAgB;SACxB;KACF,CAAA;AACL,CAAC,CAAC,CAAA"}
@@ -1,3 +0,0 @@
1
- import './procedures/auth.js';
2
- import './procedures/users.js';
3
- export declare const app: import("express-serve-static-core").Express;
@@ -1,49 +0,0 @@
1
- import express from 'express';
2
- import { PublicFactory, ProtectedFactory } from './factories.js';
3
- import { registerExpressRoutes } from '../index.js';
4
- // Import procedures for their side effects (they register with the factories)
5
- import './procedures/auth.js';
6
- import './procedures/users.js';
7
- const authTokenService = (authToken) => {
8
- // Dummy implementation - replace with real token validation logic
9
- return {
10
- isValid: authToken === 'valid-token',
11
- userId: authToken === 'valid-token' ? 'user-123' : null,
12
- };
13
- };
14
- export const app = express();
15
- app.use(express.json());
16
- registerExpressRoutes(app, {
17
- getContext: async (req, res) => {
18
- return {
19
- ipAddress: '',
20
- requestId: '',
21
- headers: {},
22
- };
23
- }
24
- }, PublicFactory.getProcedures());
25
- registerExpressRoutes(app, {
26
- getContext: async (req, res) => {
27
- const authServiceResult = authTokenService(req.headers['authorization']?.toString() || '');
28
- if (authServiceResult.isValid) {
29
- return {
30
- userId: authServiceResult.userId,
31
- ipAddress: '',
32
- requestId: '',
33
- headers: {}
34
- };
35
- }
36
- res.status(401).json({
37
- error: 'Unauthorized'
38
- });
39
- throw new Error('Unauthorized');
40
- }
41
- }, ProtectedFactory.getProcedures());
42
- // Only start server when run directly (not when imported for testing)
43
- const isMainModule = import.meta.url.endsWith(process.argv[1]?.replace(/\\/g, '/') || '');
44
- if (isMainModule) {
45
- app.listen(4841, () => {
46
- console.log('Server is running on http://localhost:4841');
47
- });
48
- }
49
- //# sourceMappingURL=server.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"server.js","sourceRoot":"","sources":["../../../../../src/implementations/http/express/example/server.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAA;AAC7B,OAAO,EACL,aAAa,EACb,gBAAgB,EACjB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAA;AAEnD,8EAA8E;AAC9E,OAAO,sBAAsB,CAAA;AAC7B,OAAO,uBAAuB,CAAA;AAE9B,MAAM,gBAAgB,GAAG,CAAC,SAAiB,EAAE,EAAE;IAC7C,kEAAkE;IAClE,OAAO;QACL,OAAO,EAAE,SAAS,KAAK,aAAa;QACpC,MAAM,EAAE,SAAS,KAAK,aAAa,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI;KACxD,CAAA;AACH,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,GAAG,GAAG,OAAO,EAAE,CAAA;AAC5B,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAA;AAEvB,qBAAqB,CACnB,GAAG,EACH;IACE,UAAU,EAAC,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE;QAC5B,OAAO;YACL,SAAS,EAAC,EAAE;YACZ,SAAS,EAAC,EAAE;YACZ,OAAO,EAAC,EAAE;SACX,CAAA;IACH,CAAC;CACF,EACD,aAAa,CAAC,aAAa,EAAE,CAC9B,CAAA;AAED,qBAAqB,CACnB,GAAG,EACH;IACE,UAAU,EAAC,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE;QAC5B,MAAM,iBAAiB,GAAG,gBAAgB,CAAC,GAAG,CAAC,OAAO,CAAC,eAAe,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAA;QAE1F,IAAI,iBAAiB,CAAC,OAAO,EAAE,CAAC;YAC9B,OAAO;gBACL,MAAM,EAAC,iBAAiB,CAAC,MAAO;gBAChC,SAAS,EAAC,EAAE;gBACZ,SAAS,EAAC,EAAE;gBACZ,OAAO,EAAC,EAAE;aACX,CAAA;QACH,CAAC;QAED,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;YACnB,KAAK,EAAE,cAAc;SACtB,CAAC,CAAA;QACF,MAAM,IAAI,KAAK,CAAC,cAAc,CAAC,CAAA;IACjC,CAAC;CACF,EACD,gBAAgB,CAAC,aAAa,EAAE,CACjC,CAAA;AAED,sEAAsE;AACtE,MAAM,YAAY,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,IAAI,EAAE,CAAC,CAAA;AACzF,IAAI,YAAY,EAAE,CAAC;IACjB,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE;QACpB,OAAO,CAAC,GAAG,CAAC,4CAA4C,CAAC,CAAA;IAC3D,CAAC,CAAC,CAAA;AACJ,CAAC"}
@@ -1,110 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import supertest from 'supertest';
3
- import { app } from './server.js';
4
- describe('Express Server Tests', () => {
5
- describe('Public Endpoints', () => {
6
- describe('POST /authenticate', () => {
7
- it('should return token with valid credentials', async () => {
8
- const response = await supertest(app)
9
- .post('/authenticate')
10
- .send({ username: 'john', password: 'secret123' });
11
- expect(response.status).toBe(200);
12
- expect(response.body).toEqual({ token: 'fake-jwt-token' });
13
- });
14
- it('should return 422 when username is missing', async () => {
15
- const response = await supertest(app).post('/authenticate').send({ password: 'secret123' });
16
- expect(response.status).toBe(422);
17
- expect(response.body).toHaveProperty('error');
18
- });
19
- it('should return 422 when username is too short (minLength: 3)', async () => {
20
- const response = await supertest(app)
21
- .post('/authenticate')
22
- .send({ username: 'ab', password: 'secret123' });
23
- expect(response.status).toBe(422);
24
- expect(response.body).toHaveProperty('error');
25
- });
26
- it('should return 422 when password is missing', async () => {
27
- const response = await supertest(app).post('/authenticate').send({ username: 'john' });
28
- expect(response.status).toBe(422);
29
- expect(response.body).toHaveProperty('error');
30
- });
31
- it('should return 422 when password is too short (minLength: 6)', async () => {
32
- const response = await supertest(app)
33
- .post('/authenticate')
34
- .send({ username: 'john', password: '12345' });
35
- expect(response.status).toBe(422);
36
- expect(response.body).toHaveProperty('error');
37
- });
38
- it('should return 422 when body is empty', async () => {
39
- const response = await supertest(app).post('/authenticate').send({});
40
- expect(response.status).toBe(422);
41
- expect(response.body).toHaveProperty('error');
42
- });
43
- });
44
- });
45
- describe('Protected Endpoints', () => {
46
- describe('GET /users/user-profile/:id', () => {
47
- it('should return 401 when no authorization header is provided', async () => {
48
- const response = await supertest(app).get('/users/user-profile/123');
49
- expect(response.status).toBe(401);
50
- expect(response.body).toEqual({ error: 'Unauthorized' });
51
- });
52
- it('should return 401 when authorization token is invalid', async () => {
53
- const response = await supertest(app)
54
- .get('/users/user-profile/123')
55
- .set('Authorization', 'invalid-token');
56
- expect(response.status).toBe(401);
57
- expect(response.body).toEqual({ error: 'Unauthorized' });
58
- });
59
- it('should return user profile with valid token', async () => {
60
- const response = await supertest(app)
61
- .get('/users/user-profile/123')
62
- .set('Authorization', 'valid-token');
63
- expect(response.status).toBe(200);
64
- expect(response.body).toEqual({
65
- user: {
66
- id: '123',
67
- name: 'Jane Doe',
68
- email: 'test@gmail.com',
69
- },
70
- });
71
- });
72
- it('should return user profile with different id from path', async () => {
73
- const response = await supertest(app)
74
- .get('/users/user-profile/abc')
75
- .set('Authorization', 'valid-token');
76
- expect(response.status).toBe(200);
77
- expect(response.body).toEqual({
78
- user: {
79
- id: 'abc',
80
- name: 'Jane Doe',
81
- email: 'test@gmail.com',
82
- },
83
- });
84
- });
85
- });
86
- });
87
- describe('Response Structure', () => {
88
- it('authenticate returns correct token structure', async () => {
89
- const response = await supertest(app)
90
- .post('/authenticate')
91
- .send({ username: 'testuser', password: 'password123' });
92
- expect(response.status).toBe(200);
93
- expect(response.body).toHaveProperty('token');
94
- expect(typeof response.body.token).toBe('string');
95
- });
96
- it('user profile returns correct nested user object with id, name, email', async () => {
97
- const response = await supertest(app)
98
- .get('/users/user-profile/test-id')
99
- .set('Authorization', 'valid-token');
100
- expect(response.status).toBe(200);
101
- expect(response.body).toHaveProperty('user');
102
- expect(response.body.user).toHaveProperty('id', 'test-id');
103
- expect(response.body.user).toHaveProperty('name');
104
- expect(response.body.user).toHaveProperty('email');
105
- expect(typeof response.body.user.name).toBe('string');
106
- expect(typeof response.body.user.email).toBe('string');
107
- });
108
- });
109
- });
110
- //# sourceMappingURL=server.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"server.test.js","sourceRoot":"","sources":["../../../../../src/implementations/http/express/example/server.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC7C,OAAO,SAAS,MAAM,WAAW,CAAA;AACjC,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AAEjC,QAAQ,CAAC,sBAAsB,EAAE,GAAG,EAAE;IACpC,QAAQ,CAAC,kBAAkB,EAAE,GAAG,EAAE;QAChC,QAAQ,CAAC,oBAAoB,EAAE,GAAG,EAAE;YAClC,EAAE,CAAC,4CAA4C,EAAE,KAAK,IAAI,EAAE;gBAC1D,MAAM,QAAQ,GAAG,MAAM,SAAS,CAAC,GAAG,CAAC;qBAClC,IAAI,CAAC,eAAe,CAAC;qBACrB,IAAI,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAC,CAAA;gBAEpD,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;gBACjC,MAAM,CAAC,QA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IAAI,CAAC,IAAI,CAAC,CAAC,cAAc,CAAC,IAAI,EAAE,SAAS,CAAC,CAAA;YAC1D,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,cAAc,CAAC,MAAM,CAAC,CAAA;YACjD,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,cAAc,CAAC,OAAO,CAAC,CAAA;YAClD,MAAM,CAAC,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;YACrD,MAAM,CAAC,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QACxD,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
@@ -1,35 +0,0 @@
1
- import express from 'express';
2
- import { TProcedureRegistration } from '../../../index.js';
3
- import { ExpressHttpRouteHelpersContext } from './types.js';
4
- /**
5
- * Maps path parameters from Express route to an object
6
- *
7
- * Path /users/:id with /users/123 => { id: '123' }
8
- *
9
- * @param path
10
- * @param url
11
- */
12
- export declare function mapPathParamsToObject(path: string, url: string): Record<string, string>;
13
- /**
14
- * A convenience function to register multiple procedures as Express routes.
15
- *
16
- * Provide the Express app, a context generator function, and an array of procedure registrations.
17
- *
18
- * @param app
19
- * @param callbacks
20
- * @param procedures
21
- */
22
- export declare function registerExpressRoutes<ProceduresContext>(app: express.Application, callbacks: {
23
- /** Get procedure factory context for handler */
24
- getContext: (req: express.Request, res: express.Response) => Promise<ProceduresContext & ExpressHttpRouteHelpersContext>;
25
- /** Optional error handler for procedure handler errors */
26
- onHandlerError?: (error: Error, req: express.Request, res: express.Response) => void;
27
- /** Optional validation error handler */
28
- onValidationError?: (errors: Array<{
29
- message: string;
30
- path: string[];
31
- }>, req: express.Request, res: express.Response) => void;
32
- }, procedures: Array<TProcedureRegistration<ProceduresContext, {
33
- method: 'get' | 'post' | 'patch' | 'delete' | 'put';
34
- path: string;
35
- }>>): void;
@@ -1,75 +0,0 @@
1
- /**
2
- * Maps path parameters from Express route to an object
3
- *
4
- * Path /users/:id with /users/123 => { id: '123' }
5
- *
6
- * @param path
7
- * @param url
8
- */
9
- export function mapPathParamsToObject(path, url) {
10
- const urlObj = new URL(url, 'http://localhost'); // Base URL is required but irrelevant here
11
- const pathSegments = path.split('/').filter(Boolean);
12
- const urlSegments = urlObj.pathname.split('/').filter(Boolean);
13
- const params = {};
14
- pathSegments.forEach((segment, index) => {
15
- if (segment.startsWith(':')) {
16
- const paramName = segment.slice(1);
17
- params[paramName] = urlSegments[index] || '';
18
- }
19
- });
20
- return params;
21
- }
22
- /**
23
- * Combines path, query, and body parameters from an Express request into a single object
24
- * @param req
25
- */
26
- function getAllParamsFromExpressRequest(req) {
27
- const pathParams = mapPathParamsToObject(req.route.path, req.url);
28
- const queryParams = req.query || {};
29
- const bodyParams = req.body || {};
30
- return { ...pathParams, ...queryParams, ...bodyParams };
31
- }
32
- /**
33
- * A convenience function to register multiple procedures as Express routes.
34
- *
35
- * Provide the Express app, a context generator function, and an array of procedure registrations.
36
- *
37
- * @param app
38
- * @param callbacks
39
- * @param procedures
40
- */
41
- export function registerExpressRoutes(app, callbacks,
42
- // The procedure extended config must include method and path
43
- procedures) {
44
- procedures.forEach(({ handler, config }) => {
45
- const { method, path } = config;
46
- app[method](path, async (req, res) => {
47
- const context = await callbacks.getContext(req, res);
48
- const allParams = getAllParamsFromExpressRequest(req);
49
- if (config.validation?.params) {
50
- const { errors } = config.validation.params(allParams);
51
- if (errors && errors.length > 0) {
52
- if (callbacks.onValidationError) {
53
- callbacks.onValidationError(errors, req, res);
54
- }
55
- else {
56
- res.status(422).json({ error: 'Validation Error', details: errors });
57
- }
58
- return;
59
- }
60
- }
61
- try {
62
- const result = await handler(context, allParams);
63
- res.json(result);
64
- }
65
- catch (error) {
66
- if (callbacks.onHandlerError) {
67
- callbacks.onHandlerError(error, req, res);
68
- return;
69
- }
70
- res.status(500).json({ error: error.message });
71
- }
72
- });
73
- });
74
- }
75
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/implementations/http/express/index.ts"],"names":[],"mappings":"AAIA;;;;;;;GAOG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAY,EAAE,GAAW;IAC7D,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,GAAG,EAAE,kBAAkB,CAAC,CAAA,CAAC,2CAA2C;IAC3F,MAAM,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;IACpD,MAAM,WAAW,GAAG,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;IAE9D,MAAM,MAAM,GAA2B,EAAE,CAAA;IACzC,YAAY,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE;QACtC,IAAI,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YAC5B,MAAM,SAAS,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;YAClC,MAAM,CAAC,SAAS,CAAC,GAAG,WAAW,CAAC,KAAK,CAAC,IAAI,EAAE,CAAA;QAC9C,CAAC;IACH,CAAC,CAAC,CAAA;IAEF,OAAO,MAAM,CAAA;AACf,CAAC;AAED;;;GAGG;AACH,SAAS,8BAA8B,CAAC,GAAoB;IAC1D,MAAM,UAAU,GAAG,qBAAqB,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,CAAC,GAAG,CAAC,CAAA;IACjE,MAAM,WAAW,GAAG,GAAG,CAAC,KAAK,IAAI,EAAE,CAAA;IACnC,MAAM,UAAU,GAAG,GAAG,CAAC,IAAI,IAAI,EAAE,CAAA;IAEjC,OA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