@pcg/core 1.0.0-alpha.0 → 1.0.0-alpha.2

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 (98) hide show
  1. package/dist/index.d.ts +21 -1189
  2. package/dist/index.js +76 -1867
  3. package/dist/index.js.map +1 -1
  4. package/package.json +16 -3
  5. package/.turbo/turbo-build.log +0 -15
  6. package/CHANGELOG.md +0 -7
  7. package/src/abstracts/index.ts +0 -3
  8. package/src/abstracts/nestjs-resource-service.ts +0 -154
  9. package/src/abstracts/nestjs-service.ts +0 -25
  10. package/src/configs/app.config.ts +0 -185
  11. package/src/configs/db.config.ts +0 -122
  12. package/src/configs/index.ts +0 -4
  13. package/src/configs/logger.config.ts +0 -62
  14. package/src/context/action-context.ts +0 -34
  15. package/src/context/current-user.ts +0 -49
  16. package/src/context/index.ts +0 -5
  17. package/src/context/platform-method-context.ts +0 -5
  18. package/src/context/service-method-context.ts +0 -47
  19. package/src/db/snake-naming.strategy.ts +0 -277
  20. package/src/enums/app-env.enum.ts +0 -36
  21. package/src/enums/app-mode.enum.ts +0 -5
  22. package/src/enums/app-server.enum.ts +0 -39
  23. package/src/enums/index.ts +0 -4
  24. package/src/enums/worker-mode.enum.ts +0 -11
  25. package/src/errors/access-denied.error.ts +0 -18
  26. package/src/errors/bad-request.error.ts +0 -9
  27. package/src/errors/forbidden.error.ts +0 -9
  28. package/src/errors/index.ts +0 -8
  29. package/src/errors/input-validation.error.ts +0 -16
  30. package/src/errors/nest-error.filter.ts +0 -70
  31. package/src/errors/nest-error.ts +0 -63
  32. package/src/errors/not-found.error.ts +0 -9
  33. package/src/errors/unauthorized.error.ts +0 -9
  34. package/src/exceptions/http-exception-response.ts +0 -34
  35. package/src/exceptions/http-exceptions.filter.ts +0 -95
  36. package/src/index.ts +0 -32
  37. package/src/jwt/extractors.ts +0 -80
  38. package/src/jwt/types.ts +0 -209
  39. package/src/logger/classes/logger-factory.ts +0 -54
  40. package/src/logger/classes/logger.ts +0 -340
  41. package/src/logger/classes/nest-system-logger.ts +0 -63
  42. package/src/logger/classes/typeorm-logger.ts +0 -83
  43. package/src/logger/index.ts +0 -20
  44. package/src/logger/logger.constants.ts +0 -24
  45. package/src/logger/logger.interfaces.ts +0 -98
  46. package/src/logger/logger.module.ts +0 -45
  47. package/src/logger/logger.providers.ts +0 -140
  48. package/src/logger/winston.tools.ts +0 -241
  49. package/src/middlewares/app.middleware.ts +0 -26
  50. package/src/middlewares/index.ts +0 -1
  51. package/src/modules/hooks/base-hook.ts +0 -64
  52. package/src/modules/hooks/decorators/on-hook.decorator.ts +0 -19
  53. package/src/modules/hooks/hooks.module.ts +0 -10
  54. package/src/modules/hooks/hooks.service.ts +0 -28
  55. package/src/modules/hooks/index.ts +0 -11
  56. package/src/modules/id/id.module.ts +0 -26
  57. package/src/modules/id/id.service.ts +0 -57
  58. package/src/modules/id/index.ts +0 -2
  59. package/src/modules/postgres-pubsub/index.ts +0 -3
  60. package/src/modules/postgres-pubsub/postgres-pubsub.module.ts +0 -14
  61. package/src/modules/postgres-pubsub/postgres-pubsub.ts +0 -461
  62. package/src/pagination/constants.ts +0 -9
  63. package/src/pagination/cursor/cursor-pagination.exception.ts +0 -16
  64. package/src/pagination/cursor/cursor-pagination.helpers.ts +0 -145
  65. package/src/pagination/cursor/cursor-pagination.input.ts +0 -96
  66. package/src/pagination/cursor/cursor-pagination.types.ts +0 -127
  67. package/src/pagination/index.ts +0 -9
  68. package/src/pagination/offset/offset-pagination.exception.ts +0 -15
  69. package/src/pagination/offset/offset-pagination.helpers.ts +0 -122
  70. package/src/pagination/offset/offset-pagination.input.ts +0 -30
  71. package/src/pagination/offset/offset-pagination.types.ts +0 -82
  72. package/src/pagination/tools.ts +0 -53
  73. package/src/tools/compose.ts +0 -92
  74. package/src/tools/convert-to-bigint.ts +0 -27
  75. package/src/tools/create-list-meta.ts +0 -64
  76. package/src/tools/define-statuses.ts +0 -15
  77. package/src/tools/env.ts +0 -139
  78. package/src/tools/fetch-total-with-query.ts +0 -48
  79. package/src/tools/generate-entity-id.ts +0 -23
  80. package/src/tools/get-request-language.ts +0 -13
  81. package/src/tools/is-object.ts +0 -10
  82. package/src/tools/postgres/locale-to-pg-collate.ts +0 -21
  83. package/src/tools/remove-undefined-properties.ts +0 -20
  84. package/src/tools/request-id.ts +0 -25
  85. package/src/tools/stringify-opts.ts +0 -20
  86. package/src/tools/typeorm/add-filter.ts +0 -164
  87. package/src/tools/typeorm/ensure-inner-join.ts +0 -36
  88. package/src/tools/typeorm/ensure-left-join.ts +0 -36
  89. package/src/tools/typeorm/is-alias-already-busy.ts +0 -25
  90. package/src/tools/wait.ts +0 -26
  91. package/src/types/express-request.ts +0 -8
  92. package/src/types/list-mehod-options.ts +0 -32
  93. package/src/types/list-meta.ts +0 -16
  94. package/src/types/maybe.ts +0 -2
  95. package/src/validation/index.ts +0 -1
  96. package/src/validation/validation-pipe.ts +0 -14
  97. package/tsconfig.lib.json +0 -9
  98. package/tsdown.config.ts +0 -15
@@ -1,95 +0,0 @@
1
- import {
2
- ArgumentsHost, Catch, HttpException, HttpServer,
3
- } from '@nestjs/common';
4
- import { isObject } from '@nestjs/common/utils/shared.utils';
5
- import { GqlArgumentsHost } from '@nestjs/graphql/dist/services/gql-arguments-host';
6
-
7
- import { AppEnv } from '#/enums/app-env.enum';
8
- import {
9
- Logger, LoggerContext, LoggerFactory,
10
- } from '#/logger';
11
- import { HttpExceptionResponse } from './http-exception-response.js';
12
-
13
- type ContextType = 'http' | 'ws' | 'rpc' | 'graphql';
14
-
15
- /**
16
- * Http exception filter for NestJS
17
- * It is used to correctly log all http exceptions
18
- * @deprecated use NestErrorFilter instead
19
- * @see NestErrorFilter
20
- * @example
21
- * ```ts
22
- * // main.ts
23
- * const { httpAdapter } = app.get(HttpAdapterHost);
24
- * const loggerFactory = app.get(WINSTON_MODULE_FACTORY_PROVIDER);
25
- * app.useGlobalFilters(new HttpExceptionFilter(httpAdapter, loggerFactory));
26
- * ```
27
- */
28
- @Catch(HttpException)
29
- export class HttpExceptionFilter {
30
- private readonly logger: Logger;
31
-
32
- constructor(
33
- protected readonly applicationRef: HttpServer,
34
- private readonly loggerFactory: LoggerFactory,
35
- ) {
36
- this.logger = this.loggerFactory.create({
37
- scope: HttpExceptionFilter.name,
38
- });
39
- }
40
-
41
- catch(exception: HttpException, host: ArgumentsHost) {
42
- const gqlHost = GqlArgumentsHost.create(host);
43
- const type = gqlHost.getType<ContextType>();
44
-
45
- const response = exception.getResponse() as HttpExceptionResponse;
46
- if (!response?.silent) {
47
- if (isObject(response)) {
48
- const logger = this.loggerFactory.create(response.context);
49
-
50
- if (response.error instanceof Error) {
51
- logger.error(response.message, response.error);
52
- } else {
53
- logger.error(exception);
54
- }
55
- } else {
56
- this.logger.error(exception);
57
- }
58
- }
59
-
60
- if (type !== 'http') {
61
- return exception;
62
- }
63
-
64
- /**
65
- * If request is http, then we need to send response to client
66
- */
67
- let body: {
68
- message: string;
69
- key?: string;
70
- stack?: string;
71
- context?: LoggerContext;
72
- statusCode: number;
73
- };
74
-
75
- if (isObject(response)) {
76
- body = {
77
- key: response.key,
78
- message: response.message,
79
- context: response.context,
80
- statusCode: exception.getStatus(),
81
- };
82
-
83
- if (response.error instanceof Error && process.env.NODE_ENV !== AppEnv.PRODUCTION) {
84
- body.stack = response.error.stack;
85
- }
86
- } else {
87
- body = {
88
- statusCode: exception.getStatus(),
89
- message: response,
90
- };
91
- }
92
-
93
- this.applicationRef.reply(host.getArgByIndex(1), body, exception.getStatus());
94
- }
95
- }
package/src/index.ts DELETED
@@ -1,32 +0,0 @@
1
- export * from './abstracts/index.js';
2
- export * from './configs/index.js';
3
- export * from './context/index.js';
4
- export * from './enums/index.js';
5
- export * from './errors/index.js';
6
- export * from './logger/index.js';
7
-
8
- export * from './tools/compose.js';
9
- export * from './tools/convert-to-bigint.js';
10
- export * from './tools/create-list-meta.js';
11
- export * from './tools/define-statuses.js';
12
- export * from './tools/env.js';
13
- export * from './tools/fetch-total-with-query.js';
14
- export * from './tools/generate-entity-id.js';
15
- export * from './tools/is-object.js';
16
- export * from './tools/get-request-language.js';
17
- export * from './tools/postgres/locale-to-pg-collate.js';
18
- export * from './tools/remove-undefined-properties.js';
19
- export * from './tools/request-id.js';
20
- export * from './tools/stringify-opts.js';
21
- export * from './tools/wait.js';
22
-
23
- export * from './tools/typeorm/add-filter.js';
24
- export * from './tools/typeorm/ensure-inner-join.js';
25
- export * from './tools/typeorm/ensure-left-join.js';
26
- export * from './tools/typeorm/is-alias-already-busy.js';
27
-
28
- export * from './validation/index.js';
29
-
30
- export * from './exceptions/http-exception-response.js';
31
- export * from './exceptions/http-exceptions.filter.js';
32
-
@@ -1,80 +0,0 @@
1
- import { Maybe } from '#/types/maybe';
2
-
3
- /**
4
- * The request interface
5
- */
6
- export interface IRequest {
7
- query?: {
8
- access_token?: string;
9
- };
10
- headers: {
11
- authorization?: string;
12
- Authorization?: string;
13
- };
14
- }
15
-
16
- /**
17
- * Extract the access token from the request query
18
- * @param request - The request (like express request)
19
- * @returns The access token or undefined
20
- * @example
21
- * request.query.access_token = '123';
22
- * extractJwtFromQuery(request) // '123'
23
- */
24
- export const extractJwtFromQuery = (request: IRequest): Maybe<string> => {
25
- if (!request?.query) {
26
- return;
27
- }
28
-
29
- const { access_token: accessToken } = request.query;
30
- if (accessToken) {
31
- return accessToken;
32
- }
33
- };
34
-
35
- /**
36
- * Extract the access token from the authorization header
37
- *
38
- * @param string - authorization header string
39
- * @returns the access token token or undefined
40
- *
41
- * @example
42
- * const str = 'Bearer 123';
43
- * extractJwtFromBearerString(str) // '123'
44
- */
45
- export const extractJwtFromBearerString = (string: string): Maybe<string> => {
46
- const [method, value] = string.split(' ');
47
-
48
- if (method === 'Bearer' && value) {
49
- return value;
50
- }
51
- };
52
-
53
- /**
54
- * Extract the access token from the authorization header
55
- * @param request The request (like express request)
56
- * @returns The access token or undefined
57
- * @example
58
- * request.headers.authorization = 'Bearer 123';
59
- * extractJwtFromBearerToken(request) // '123'
60
- */
61
- export const extractJwtFromBearerToken = (request: IRequest): Maybe<string> => {
62
- const header = request?.headers?.authorization || request?.headers?.Authorization;
63
- if (typeof header === 'string') {
64
- const [method, value] = header.split(' ');
65
-
66
- if (method === 'Bearer' && value) {
67
- return value;
68
- }
69
- }
70
- };
71
-
72
- /**
73
- * Extract the access token from the request
74
- * @param request - The request (like express request)
75
- * @returns The access token or undefined
76
- */
77
- export const extractJwt = (request: IRequest) => {
78
- return extractJwtFromQuery(request) || extractJwtFromBearerToken(request);
79
- };
80
-
package/src/jwt/types.ts DELETED
@@ -1,209 +0,0 @@
1
- /**
2
- * Supported JWT token types
3
- */
4
- export enum JwtTokenType {
5
- /**
6
- * Access token. This is authorization token that is used to access protected resources.
7
- * Lives short time and can be refreshed by refresh token
8
- */
9
- ACCESS = 'access',
10
-
11
- /**
12
- * Refresh token.
13
- * Lives long time and can be used to refresh access token
14
- */
15
- REFRESH = 'refresh',
16
-
17
- /**
18
- * Invite token.
19
- * Used for user invitation
20
- */
21
- INVITE = 'invite',
22
-
23
- /**
24
- * Service token.
25
- * Lives long time and can be used for service account
26
- */
27
- SERVICE = 'service',
28
-
29
- /**
30
- * Subscription token.
31
- * USed for GraphQL subscriptions only. Lives long time
32
- */
33
- SUBSCRIPTION = 'subscription',
34
- }
35
-
36
- export interface BaseJwtTokenPayload {
37
- /**
38
- * Token type
39
- */
40
- sub: JwtTokenType;
41
-
42
- /**
43
- * Issuer. Service shortname that issued this token
44
- * @example 'bo'
45
- */
46
- iss: string;
47
-
48
- /**
49
- * User ID
50
- * @example 'hcu:1xgetnx5xq3'
51
- */
52
- uid: string;
53
-
54
- /**
55
- * Parent User ID who logged in as this user
56
- * @example 'hcu:2zgetnx5x2h'
57
- */
58
- pid?: string;
59
- }
60
-
61
- /**
62
- * Payload to create new JWT access token
63
- */
64
- export interface JwtAccessTokenPayload extends BaseJwtTokenPayload {
65
- sub: JwtTokenType.ACCESS;
66
-
67
- /**
68
- * Audience. Service shortnames that can use this token
69
- */
70
- aud: string[];
71
- }
72
-
73
- /**
74
- * Payload to create new JWT Subscription token
75
- */
76
- export interface JwtSubscriptionTokenPayload extends BaseJwtTokenPayload {
77
- sub: JwtTokenType.SUBSCRIPTION;
78
-
79
- /**
80
- * Audience. Service shortnames that can use this token
81
- */
82
- aud: string[];
83
- }
84
-
85
- /**
86
- * Payload to create new JWT refresh token
87
- */
88
- export interface JwtRefreshTokenPayload extends BaseJwtTokenPayload {
89
- sub: JwtTokenType.REFRESH;
90
-
91
- /**
92
- * Refresh token ID.
93
- * This is used to check if refresh token is exists in DB and not revoked
94
- */
95
- id: string;
96
-
97
- }
98
-
99
- /**
100
- * Payload to create new JWT service token
101
- */
102
- export interface JwtServiceTokenPayload extends BaseJwtTokenPayload {
103
- sub: JwtTokenType.SERVICE;
104
-
105
- /**
106
- * Audience. Service shortnames that can use this token
107
- */
108
- aud: string[];
109
-
110
- /**
111
- * Service token ID.
112
- * This is used to check if service token is exists in DB and not revoked
113
- */
114
- id: string;
115
-
116
- /**
117
- * Client ID
118
- * @example 'ext-simedia'
119
- */
120
- cid?: string;
121
- }
122
-
123
- /**
124
- * Decoded JWT access token
125
- */
126
- export interface JwtAccessToken extends JwtAccessTokenPayload {
127
- /**
128
- * Expiration time
129
- */
130
- exp: number;
131
- }
132
-
133
- /**
134
- * Decoded JWT access token
135
- */
136
- export interface JwtSubscriptionToken extends JwtSubscriptionTokenPayload {
137
- /**
138
- * Expiration time
139
- */
140
- exp: number;
141
- }
142
-
143
- /**
144
- * Decoded JWT refresh token
145
- */
146
- export interface JwtRefreshToken extends JwtRefreshTokenPayload {
147
- /**
148
- * Expiration time
149
- */
150
- exp: number;
151
- }
152
-
153
- /**
154
- * Decoded JWT service token
155
- */
156
- export interface JwtServiceToken extends JwtServiceTokenPayload {
157
- /**
158
- * Expiration time
159
- */
160
- exp?: number;
161
- }
162
-
163
- /**
164
- * Checks if the token is a JWT access token
165
- * @param token - The JWT token payload to check
166
- * @returns True if the token is an access token, false otherwise
167
- * @example
168
- * const payload = { sub: JwtTokenType.ACCESS, ... };
169
- * isJwtAccessToken(payload) // true
170
- */
171
- export const isJwtAccessToken = (token: BaseJwtTokenPayload): token is JwtAccessToken => {
172
- return token.sub === JwtTokenType.ACCESS;
173
- };
174
-
175
- /**
176
- * Checks if the token is a JWT subscription token
177
- * @param token - The JWT token payload to check
178
- * @returns True if the token is a subscription token, false otherwise
179
- * @example
180
- * const payload = { sub: JwtTokenType.SUBSCRIPTION, ... };
181
- * isJwtSubscriptionToken(payload) // true
182
- */
183
- export const isJwtSubscriptionToken = (token: BaseJwtTokenPayload): token is JwtSubscriptionToken => {
184
- return token.sub === JwtTokenType.SUBSCRIPTION;
185
- };
186
-
187
- /**
188
- * Checks if the token is a JWT refresh token
189
- * @param token - The JWT token payload to check
190
- * @returns True if the token is a refresh token, false otherwise
191
- * @example
192
- * const payload = { sub: JwtTokenType.REFRESH, ... };
193
- * isJwtRefreshToken(payload) // true
194
- */
195
- export const isJwtRefreshToken = (token: BaseJwtTokenPayload): token is JwtRefreshToken => {
196
- return token.sub === JwtTokenType.REFRESH;
197
- };
198
-
199
- /**
200
- * Checks if the token is a JWT service token
201
- * @param token - The JWT token payload to check
202
- * @returns True if the token is a service token, false otherwise
203
- * @example
204
- * const payload = { sub: JwtTokenType.SERVICE, ... };
205
- * isJwtServiceToken(payload) // true
206
- */
207
- export const isJwtServiceToken = (token: BaseJwtTokenPayload): token is JwtServiceToken => {
208
- return token.sub === JwtTokenType.SERVICE;
209
- };
@@ -1,54 +0,0 @@
1
- import { Logger as WinstonLogger } from 'winston';
2
-
3
- import { LoggerContext } from '../logger.interfaces.js';
4
- import { Logger } from './logger.js';
5
- import { TypeOrmLogger, TypeOrmLoggerOptions } from './typeorm-logger.js';
6
-
7
- /**
8
- * Logger factory for creating loggers with predefined context
9
- * Factory is singleton, so it keep original winston `Logger` instance
10
- * and is can be injected to any service
11
- */
12
- export class LoggerFactory {
13
- constructor(
14
- private readonly winstonLogger: WinstonLogger,
15
- private context: LoggerContext = {
16
- },
17
- ) {}
18
-
19
- /**
20
- * Created new logger with specific context merge it with global context
21
- * @param context - specific action with scope, and other data
22
- * @returns - new logger with context
23
- * @example
24
- * ```ts
25
- * class SomeService {
26
- * logger: Logger;
27
- *
28
- * constructor(
29
- * @InjectLoggerFactory() private readonly loggerFactory: LoggerFactory,
30
- * ) {
31
- * // Create logger for this service
32
- * this.logger = this.loggerFactory.create({
33
- * scope: SomeService.name,
34
- * })
35
- * }
36
- * ```
37
- */
38
- create(context: LoggerContext = {
39
- }) {
40
- return new Logger(this.winstonLogger, {
41
- ...this.context,
42
- ...context,
43
- });
44
- }
45
-
46
- createTypeOrmLogger(opts: Omit<TypeOrmLoggerOptions, 'logger'>) {
47
- return new TypeOrmLogger({
48
- logger: this.create({
49
- scope: 'TypeORM',
50
- }),
51
- ...opts,
52
- });
53
- }
54
- }