@forklaunch/core 0.1.2 → 0.1.5

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 (55) hide show
  1. package/dist/http/httpStatusCodes.d.ts +74 -0
  2. package/dist/http/httpStatusCodes.js +1050 -0
  3. package/dist/http/httpStatusCodes.js.map +1 -0
  4. package/dist/http/index.d.ts +3 -1
  5. package/dist/http/index.js +3 -1
  6. package/dist/http/index.js.map +1 -1
  7. package/dist/http/middleware/index.js.map +1 -0
  8. package/dist/http/middleware/request.middleware.js.map +1 -0
  9. package/dist/http/middleware/response.middleware.js.map +1 -0
  10. package/dist/http/openApiV3Generator.d.ts +4 -0
  11. package/dist/http/openApiV3Generator.js +137 -0
  12. package/dist/http/openApiV3Generator.js.map +1 -0
  13. package/dist/http/types/forklaunch.types.d.ts +13 -0
  14. package/dist/http/types/forklaunch.types.js +3 -0
  15. package/dist/http/types/forklaunch.types.js.map +1 -0
  16. package/dist/http/types/index.d.ts +1 -0
  17. package/dist/http/types/index.js +1 -0
  18. package/dist/http/types/index.js.map +1 -1
  19. package/dist/jest.config.js +10 -0
  20. package/dist/tests/http.middleware.test.js +1 -1
  21. package/dist/tests/http.middleware.test.js.map +1 -1
  22. package/dist/tests/openApiV3Generator.test.d.ts +1 -0
  23. package/dist/tests/openApiV3Generator.test.js +71 -0
  24. package/dist/tests/openApiV3Generator.test.js.map +1 -0
  25. package/package.json +4 -2
  26. package/.prettierignore +0 -2
  27. package/.prettierrc +0 -6
  28. package/dist/http/middlewares/index.js.map +0 -1
  29. package/dist/http/middlewares/request.middleware.js.map +0 -1
  30. package/dist/http/middlewares/response.middleware.js.map +0 -1
  31. package/entityMapper/index.ts +0 -2
  32. package/entityMapper/interfaces/entityMapper.interface.ts +0 -17
  33. package/entityMapper/models/baseEntityMapper.model.ts +0 -110
  34. package/entityMapper/models/requestEntityMapper.model.ts +0 -111
  35. package/entityMapper/models/responseEntityMapper.model.ts +0 -98
  36. package/entityMapper/types/entityMapper.types.ts +0 -12
  37. package/eslint.config.mjs +0 -12
  38. package/http/index.ts +0 -2
  39. package/http/middlewares/index.ts +0 -2
  40. package/http/middlewares/request.middleware.ts +0 -257
  41. package/http/middlewares/response.middleware.ts +0 -63
  42. package/http/types/api.types.ts +0 -83
  43. package/http/types/index.ts +0 -2
  44. package/http/types/primitive.types.ts +0 -76
  45. package/index.ts +0 -6
  46. package/jest.config.ts +0 -10
  47. package/tests/entityMapper.test.ts +0 -219
  48. package/tests/http.middleware.test.ts +0 -99
  49. package/tests/redisTtlCache.test.ts +0 -62
  50. /package/dist/http/{middlewares → middleware}/index.d.ts +0 -0
  51. /package/dist/http/{middlewares → middleware}/index.js +0 -0
  52. /package/dist/http/{middlewares → middleware}/request.middleware.d.ts +0 -0
  53. /package/dist/http/{middlewares → middleware}/request.middleware.js +0 -0
  54. /package/dist/http/{middlewares → middleware}/response.middleware.d.ts +0 -0
  55. /package/dist/http/{middlewares → middleware}/response.middleware.js +0 -0
@@ -1,76 +0,0 @@
1
- import { AnySchemaValidator } from '@forklaunch/validator';
2
- import { UnboxedObjectSchema } from '@forklaunch/validator/types';
3
-
4
- export type ParamsDictionary = { [key: string]: string };
5
-
6
- export type StringOnlyObject<SV extends AnySchemaValidator> = Omit<
7
- UnboxedObjectSchema<SV>,
8
- number | symbol
9
- >;
10
- export type NumberOnlyObject<SV extends AnySchemaValidator> = Omit<
11
- UnboxedObjectSchema<SV>,
12
- string | symbol
13
- >;
14
-
15
- export type BodyObject<SV extends AnySchemaValidator> = StringOnlyObject<SV> &
16
- unknown;
17
- export type ParamsObject<SV extends AnySchemaValidator> = StringOnlyObject<SV> &
18
- unknown;
19
- export type QueryObject<SV extends AnySchemaValidator> = StringOnlyObject<SV> &
20
- unknown;
21
- export type HeadersObject<SV extends AnySchemaValidator> =
22
- StringOnlyObject<SV> & unknown;
23
- export type ResponsesObject<SV extends AnySchemaValidator> = {
24
- [key: number]:
25
- | SV['_ValidSchemaObject']
26
- | UnboxedObjectSchema<SV>
27
- | string
28
- | SV['string'];
29
- } & unknown;
30
-
31
- export type Body<SV extends AnySchemaValidator> =
32
- | BodyObject<SV>
33
- | SV['_ValidSchemaObject']
34
- | SV['_SchemaCatchall'];
35
-
36
- export type AuthMethod = 'jwt' | 'session';
37
- export interface PathParamHttpContractDetails<
38
- SV extends AnySchemaValidator,
39
- ParamSchemas extends ParamsObject<SV> = ParamsObject<SV>,
40
- ResponseSchemas extends ResponsesObject<SV> = ResponsesObject<SV>,
41
- QuerySchemas extends QueryObject<SV> = QueryObject<SV>
42
- > {
43
- name: string;
44
- summary: string;
45
- responses: ResponseSchemas;
46
- requestHeaders?: HeadersObject<SV>;
47
- responseHeaders?: HeadersObject<SV>;
48
- params?: ParamSchemas;
49
- query?: QuerySchemas;
50
- auth?: {
51
- method: AuthMethod;
52
- allowedSlugs?: Set<string>;
53
- forbiddenSlugs?: Set<string>;
54
- allowedRoles?: Set<string>;
55
- forbiddenRoles?: Set<string>;
56
- };
57
- }
58
-
59
- export interface HttpContractDetails<
60
- SV extends AnySchemaValidator,
61
- ParamSchemas extends ParamsObject<SV> = ParamsObject<SV>,
62
- ResponseSchemas extends ResponsesObject<SV> = ResponsesObject<SV>,
63
- BodySchema extends Body<SV> = Body<SV>,
64
- QuerySchemas extends QueryObject<SV> = QueryObject<SV>
65
- > extends PathParamHttpContractDetails<
66
- SV,
67
- ParamSchemas,
68
- ResponseSchemas,
69
- QuerySchemas
70
- > {
71
- body?: BodySchema;
72
- contentType?:
73
- | 'application/json'
74
- | 'multipart/form-data'
75
- | 'application/x-www-form-urlencoded';
76
- }
package/index.ts DELETED
@@ -1,6 +0,0 @@
1
- export * from './cache';
2
- export * from './controllers';
3
- export * from './database';
4
- export * from './entityMapper';
5
- export * from './http';
6
- export * from './services';
package/jest.config.ts DELETED
@@ -1,10 +0,0 @@
1
- import type { Config } from 'jest';
2
-
3
- const config: Config = {
4
- verbose: true,
5
- preset: 'ts-jest',
6
- testEnvironment: 'node',
7
- testPathIgnorePatterns: ['dist/', 'node_modules/']
8
- };
9
-
10
- export default config;
@@ -1,219 +0,0 @@
1
- import {
2
- TypeboxSchemaValidator,
3
- number,
4
- string
5
- } from '@forklaunch/validator/typebox';
6
- import { BaseEntity } from '../database/mikro/models/entities/base.entity';
7
- import { RequestEntityMapper } from '../entityMapper/models/requestEntityMapper.model';
8
- import { ResponseEntityMapper } from '../entityMapper/models/responseEntityMapper.model';
9
-
10
- class TestEntity extends BaseEntity {
11
- name: string;
12
- age: number;
13
- }
14
-
15
- class TestRequestEntityMapper extends RequestEntityMapper<
16
- TestEntity,
17
- TypeboxSchemaValidator
18
- > {
19
- schema = {
20
- id: string,
21
- name: string,
22
- age: number
23
- };
24
-
25
- toEntity(...additionalArgs: unknown[]): TestEntity {
26
- const entity = new TestEntity();
27
- entity.id = this.dto.id;
28
- entity.name = this.dto.name;
29
- entity.age = this.dto.age;
30
-
31
- return entity;
32
- }
33
- }
34
-
35
- class TestResponseEntityMapper extends ResponseEntityMapper<
36
- TestEntity,
37
- TypeboxSchemaValidator
38
- > {
39
- schema = {
40
- id: string,
41
- name: string,
42
- age: number
43
- };
44
-
45
- fromEntity(entity: TestEntity): this {
46
- this.dto = {
47
- id: entity.id,
48
- name: entity.name,
49
- age: entity.age
50
- };
51
-
52
- return this;
53
- }
54
- }
55
-
56
- function extractNonTimeBasedEntityFields<T extends BaseEntity>(entity: T): T {
57
- entity.createdAt = new Date(0);
58
- entity.updatedAt = new Date(0);
59
- return entity;
60
- }
61
-
62
- describe('Request Entity Mapper Test', () => {
63
- let TestRequestEM: TestRequestEntityMapper;
64
-
65
- beforeAll(() => {
66
- TestRequestEM = new TestRequestEntityMapper(new TypeboxSchemaValidator());
67
- });
68
-
69
- test('Schema Equality', async () => {
70
- expect(TestRequestEM.schema).toEqual(TestRequestEntityMapper.schema());
71
- });
72
-
73
- test('From JSON', async () => {
74
- const json = {
75
- id: '123',
76
- name: 'test',
77
- age: 1
78
- };
79
-
80
- const responseEM = TestRequestEM.fromJson(json);
81
- const staticEM = TestRequestEntityMapper.fromJson(
82
- new TypeboxSchemaValidator(),
83
- json
84
- );
85
- const expectedDto = {
86
- id: '123',
87
- name: 'test',
88
- age: 1
89
- };
90
-
91
- expect(staticEM.dto).toEqual(expectedDto);
92
- expect(responseEM.dto).toEqual(expectedDto);
93
- expect(responseEM.dto).toEqual(staticEM.dto);
94
- });
95
-
96
- test('Deserialization Equality', async () => {
97
- const json = {
98
- id: '123',
99
- name: 'test',
100
- age: 1
101
- };
102
-
103
- const entity = extractNonTimeBasedEntityFields(
104
- TestRequestEM.deserializeJsonToEntity(json)
105
- );
106
- const objectEntity = extractNonTimeBasedEntityFields(
107
- TestRequestEM.fromJson(json).toEntity()
108
- );
109
- const staticEntity = extractNonTimeBasedEntityFields(
110
- TestRequestEntityMapper.deserializeJsonToEntity(
111
- new TypeboxSchemaValidator(),
112
- json
113
- )
114
- );
115
- let expectedEntity = new TestEntity();
116
- expectedEntity.id = '123';
117
- expectedEntity.name = 'test';
118
- expectedEntity.age = 1;
119
-
120
- expectedEntity = extractNonTimeBasedEntityFields(expectedEntity);
121
-
122
- expect(entity).toEqual(expectedEntity);
123
- expect(objectEntity).toEqual(expectedEntity);
124
- expect(staticEntity).toEqual(expectedEntity);
125
- expect(entity).toEqual(objectEntity);
126
- expect(entity).toEqual(staticEntity);
127
- expect(staticEntity).toEqual(expectedEntity);
128
- expect(staticEntity).toEqual(objectEntity);
129
- });
130
-
131
- test('Serialization Failure', async () => {
132
- const json = {
133
- id: '123',
134
- name: 'test'
135
- };
136
-
137
- // @ts-expect-error
138
- expect(() => TestRequestEM.fromJson(json)).toThrow();
139
- expect(() =>
140
- // @ts-expect-error
141
- TestRequestEntityMapper.fromJson(new TypeboxSchemaValidator(), json)
142
- ).toThrow();
143
- });
144
- });
145
-
146
- describe('Response Entity Mapper Test', () => {
147
- let TestResponseEM: TestResponseEntityMapper;
148
-
149
- beforeAll(() => {
150
- TestResponseEM = new TestResponseEntityMapper(new TypeboxSchemaValidator());
151
- });
152
-
153
- test('Schema Equality', async () => {
154
- expect(TestResponseEM.schema).toEqual(TestResponseEntityMapper.schema());
155
- });
156
-
157
- test('From Entity', async () => {
158
- const entity = new TestEntity();
159
- entity.id = '123';
160
- entity.name = 'test';
161
- entity.age = 1;
162
-
163
- const responseEM = TestResponseEM.fromEntity(entity);
164
- const staticEM = TestResponseEntityMapper.fromEntity(
165
- new TypeboxSchemaValidator(),
166
- entity
167
- );
168
- const expectedDto = {
169
- id: '123',
170
- name: 'test',
171
- age: 1
172
- };
173
-
174
- expect(staticEM.dto).toEqual(expectedDto);
175
- expect(responseEM.dto).toEqual(expectedDto);
176
- expect(responseEM.dto).toEqual(staticEM.dto);
177
- });
178
-
179
- test('Serialization Equality', async () => {
180
- const entity = new TestEntity();
181
- entity.id = '123';
182
- entity.name = 'test';
183
- entity.age = 1;
184
-
185
- const json = TestResponseEM.serializeEntityToJson(entity);
186
- const objectJson = TestResponseEM.fromEntity(entity).toJson();
187
- const staticJson = TestResponseEntityMapper.serializeEntityToJson(
188
- new TypeboxSchemaValidator(),
189
- entity
190
- );
191
- const expectedJson = {
192
- id: '123',
193
- name: 'test',
194
- age: 1
195
- };
196
-
197
- expect(json).toEqual(expectedJson);
198
- expect(objectJson).toEqual(expectedJson);
199
- expect(staticJson).toEqual(expectedJson);
200
- expect(json).toEqual(objectJson);
201
- expect(json).toEqual(staticJson);
202
- expect(staticJson).toEqual(expectedJson);
203
- expect(staticJson).toEqual(objectJson);
204
- });
205
-
206
- test('Serialization Failure', async () => {
207
- const entity = new TestEntity();
208
- entity.id = '123';
209
- entity.name = 'test';
210
-
211
- expect(() => TestResponseEM.fromEntity(entity).toJson()).toThrow();
212
- expect(() =>
213
- TestResponseEntityMapper.fromEntity(
214
- new TypeboxSchemaValidator(),
215
- entity
216
- ).toJson()
217
- ).toThrow();
218
- });
219
- });
@@ -1,99 +0,0 @@
1
- import { SchemaValidator } from '@forklaunch/validator';
2
- import {
3
- MockSchemaValidator,
4
- literal,
5
- mockSchemaValidator,
6
- optional,
7
- union
8
- } from '@forklaunch/validator/tests/mockSchemaValidator';
9
- import { ForklaunchRequest, ForklaunchResponse, HttpContractDetails, RequestContext, createRequestContext, enrichRequestDetails, parseRequestBody, parseRequestHeaders, parseRequestParams, parseRequestQuery, parseResponse } from '../http';
10
-
11
- describe('Http Middleware Tests', () => {
12
- let contractDetails: HttpContractDetails<MockSchemaValidator>;
13
- let req: ForklaunchRequest<MockSchemaValidator>;
14
- let resp: ForklaunchResponse;
15
-
16
- const nextFunction = (err?: unknown) => {
17
- expect(err).toBeFalsy()
18
- };
19
-
20
- const testSchema = {
21
- test: union(['a', optional(literal('test'))] as const)
22
- };
23
-
24
- beforeAll(() => {
25
- contractDetails = {
26
- name: 'Test Contract',
27
- summary: 'Test Contract Summary',
28
- body: testSchema,
29
- params: testSchema,
30
- requestHeaders: testSchema,
31
- query: testSchema,
32
- responses: {
33
- 200: testSchema
34
- }
35
- };
36
-
37
- req = {
38
- context: {} as RequestContext,
39
- contractDetails: {} as HttpContractDetails<MockSchemaValidator>,
40
- schemaValidator: {} as SchemaValidator,
41
- params: testSchema,
42
- headers: testSchema,
43
- body: testSchema,
44
- query: testSchema
45
- };
46
-
47
- resp = {
48
- bodyData: {},
49
- statusCode: 200,
50
- corked: false,
51
- getHeaders: jest.fn(),
52
- setHeader: jest.fn(),
53
- status: jest.fn(),
54
- send: jest.fn(),
55
- json: jest.fn(),
56
- jsonp: jest.fn(),
57
- };
58
- });
59
-
60
-
61
-
62
- test('Create Request Context', async () => {
63
- req.context = {} as RequestContext;
64
- req.schemaValidator = {} as SchemaValidator;
65
- createRequestContext(mockSchemaValidator)(req, resp, nextFunction);
66
- expect(req.context.correlationId).not.toBe('123');
67
- expect(req.schemaValidator).toBe(mockSchemaValidator);
68
- });
69
-
70
- test('Enrich Request Details', async () => {
71
- req.contractDetails = {} as HttpContractDetails<MockSchemaValidator>;
72
- enrichRequestDetails(contractDetails)(req, resp, nextFunction);
73
- expect(req.contractDetails).toEqual(contractDetails);
74
- });
75
-
76
- test('Validate Request Params', async () => {
77
- parseRequestParams(req, resp, nextFunction);
78
- });
79
-
80
- test('Validate Request Headers', async () => {
81
- parseRequestHeaders(req, resp, nextFunction);
82
- });
83
-
84
- test('Validate Request Body', async () => {
85
- parseRequestBody(req, resp, nextFunction);
86
- });
87
-
88
- test('Validate Request Query Params', async () => {
89
- parseRequestQuery(req, resp, nextFunction);
90
- });
91
-
92
- test('Validate Response', async () => {
93
- parseResponse(req, resp, nextFunction);
94
- });
95
-
96
- // Not supported yet
97
- // test('Validate Auth', async () => {
98
- // });
99
- });
@@ -1,62 +0,0 @@
1
- import { GenericContainer, StartedTestContainer } from 'testcontainers';
2
- import { RedisTtlCache } from '../cache/redisTtlCache';
3
-
4
- describe('RedisTtlCache', () => {
5
- let container: StartedTestContainer;
6
- let cache: RedisTtlCache;
7
- let key: string;
8
- let value: unknown;
9
- let ttlMilliseconds: number;
10
-
11
- beforeAll(async () => {
12
- container = await new GenericContainer('redis')
13
- .withExposedPorts(6379)
14
- .start();
15
-
16
- cache = new RedisTtlCache(5000, {
17
- url: `redis://${container.getHost()}:${container.getMappedPort(6379)}`
18
- });
19
-
20
- key = 'testKey';
21
- value = { data: 'testValue' };
22
- ttlMilliseconds = 1000;
23
- }, 30000);
24
-
25
- afterAll(async () => {
26
- await cache.disconnect();
27
- await container.stop();
28
- });
29
-
30
- it('PutRecord', async () => {
31
- await cache.putRecord({ key, value, ttlMilliseconds });
32
- });
33
-
34
- test('Read Record', async () => {
35
- const storedValue = await cache.readRecord(key);
36
-
37
- expect(storedValue).toEqual({
38
- key,
39
- ttlMilliseconds,
40
- value
41
- });
42
- });
43
-
44
- test('Peek Record', async () => {
45
- const exists = await cache.peekRecord(key);
46
-
47
- expect(exists).toBeTruthy();
48
- });
49
-
50
- test('Delete Record', async () => {
51
- await cache.deleteRecord(key);
52
- const existsAfterDelete = await cache.peekRecord(key);
53
-
54
- expect(existsAfterDelete).toBeFalsy();
55
- });
56
-
57
- test('Check No Record', async () => {
58
- await Promise.resolve(setTimeout(async () => {}, ttlMilliseconds));
59
- const existsAfterTtl = await cache.peekRecord(key);
60
- expect(existsAfterTtl).toBeFalsy();
61
- });
62
- });
File without changes