Skip to content

Commit

Permalink
Merge pull request #8748 from ever-co/fix/package-constants
Browse files Browse the repository at this point in the history
[Refactor] Package Utils/Constants
  • Loading branch information
rahul-rocket authored Jan 24, 2025
2 parents 9e5549c + ba8db69 commit 202f34e
Show file tree
Hide file tree
Showing 58 changed files with 360 additions and 342 deletions.
11 changes: 3 additions & 8 deletions apps/api/src/plugin-config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import * as path from 'path';
import * as chalk from 'chalk';
import {
ApplicationPluginConfig,
DEFAULT_API_PORT,
DEFAULT_GRAPHQL_API_PATH,
DEFAULT_API_HOST,
DEFAULT_API_BASE_URL
} from '@gauzy/common';
import { ApplicationPluginConfig } from '@gauzy/common';
import { DEFAULT_API_PORT, DEFAULT_GRAPHQL_API_PATH, DEFAULT_API_HOST, DEFAULT_API_BASE_URL } from '@gauzy/constants';
import {
dbTypeOrmConnectionConfig,
dbMikroOrmConnectionConfig,
Expand Down Expand Up @@ -61,7 +56,7 @@ console.log('Plugin Config -> assetPublicPath: ' + assetPublicPath);
console.log('DB Synchronize: ' + process.env.DB_SYNCHRONIZE);

/**
* Application plugin configuration
* Application plugin configuration for production environment.
*/
export const pluginConfig: ApplicationPluginConfig = {
apiConfigOptions: {
Expand Down
1 change: 1 addition & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"dependencies": {
"@apollo/server": "^4.11.3",
"@gauzy/constants": "^0.1.0",
"@gauzy/contracts": "^0.1.0",
"@mikro-orm/nestjs": "^6.0.2",
"@nestjs/common": "^10.4.15",
Expand Down
1 change: 0 additions & 1 deletion packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* Public API Surface of @gauzy/common
*/
export * from './lib/constants';
export * from './lib/decorators';
export * from './lib/enums';
export * from './lib/guards';
Expand Down
12 changes: 0 additions & 12 deletions packages/common/src/lib/constants.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SetMetadata } from '@nestjs/common';
import { FEATURE_METADATA } from '@gauzy/constants';
import { FeatureEnum } from '@gauzy/contracts';
import { FEATURE_METADATA } from './../constants';

export const FeatureFlag = (feature: FeatureEnum) => SetMetadata(FEATURE_METADATA, feature);
4 changes: 2 additions & 2 deletions packages/common/src/lib/decorators/public.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CustomDecorator, SetMetadata } from '@nestjs/common';
import { PUBLIC_METHOD_METADATA } from './../constants';
import { PUBLIC_METHOD_METADATA } from '@gauzy/constants';

/**
* Decorator that assigns metadata to the class/function using the
Expand All @@ -12,4 +12,4 @@ import { PUBLIC_METHOD_METADATA } from './../constants';
*
* @publicApi
*/
export const Public = (): CustomDecorator => SetMetadata(PUBLIC_METHOD_METADATA, true);
export const Public = (): CustomDecorator => SetMetadata(PUBLIC_METHOD_METADATA, true);
23 changes: 9 additions & 14 deletions packages/common/src/lib/guards/feature-flag-enabled.guard.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import {
CanActivate,
ExecutionContext,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { CanActivate, ExecutionContext, Injectable, NotFoundException } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { FEATURE_METADATA } from '@gauzy/constants';
import { FeatureEnum, IAuthenticationFlagFeatures } from '@gauzy/contracts';
import { FEATURE_METADATA } from '../constants';

/**
* Check if a specific feature is enabled based on the environment variable.
Expand All @@ -15,7 +10,7 @@ import { FEATURE_METADATA } from '../constants';
* @returns True if the feature is enabled, otherwise false.
*/
const featureEnabled = (feature: Extract<keyof IAuthenticationFlagFeatures, string>) => {
return process.env[feature] !== 'false';
return process.env[feature] !== 'false';
};

/**
Expand Down Expand Up @@ -44,7 +39,7 @@ export const flagFeatures: IAuthenticationFlagFeatures = {
FEATURE_MICROSOFT_LOGIN: featureEnabled(FeatureEnum.FEATURE_MICROSOFT_LOGIN),

/** Flag indicating whether LinkedIn login is enabled. */
FEATURE_LINKEDIN_LOGIN: featureEnabled(FeatureEnum.FEATURE_LINKEDIN_LOGIN),
FEATURE_LINKEDIN_LOGIN: featureEnabled(FeatureEnum.FEATURE_LINKEDIN_LOGIN)
};

/**
Expand All @@ -63,19 +58,19 @@ export class FeatureFlagEnabledGuard implements CanActivate {
*/
async canActivate(context: ExecutionContext) {
/*
* Retrieve metadata for a specified key for a specified set of features
*/
* Retrieve metadata for a specified key for a specified set of features
*/
const flag = this._reflector.getAllAndOverride<FeatureEnum>(FEATURE_METADATA, [
context.getHandler(), // Returns a reference to the handler (method) that will be invoked next in the request pipeline.
context.getClass(), // Returns the *type* of the controller class which the current handler belongs to.
context.getClass() // Returns the *type* of the controller class which the current handler belongs to.
]);
if (!!flagFeatures[flag]) {
return true;
}

/**
* If the feature is not enabled, throw a NotFoundException.
*/
* If the feature is not enabled, throw a NotFoundException.
*/
const httpContext = context.switchToHttp();
const request = httpContext.getRequest();
throw new NotFoundException(`Cannot ${request.method} ${request.url}`);
Expand Down
2 changes: 2 additions & 0 deletions packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"lib:watch": "yarn nx build config --watch"
},
"dependencies": {
"@gauzy/constants": "^0.1.0",
"@gauzy/common": "^0.1.0",
"@gauzy/contracts": "^0.1.0",
"@gauzy/utils": "^0.1.0",
"@mikro-orm/better-sqlite": "^6.4.2",
Expand Down
10 changes: 3 additions & 7 deletions packages/config/src/lib/default-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ import * as dotenv from 'dotenv';
dotenv.config();

import * as path from 'path';
import {
DEFAULT_API_HOST,
DEFAULT_API_PORT,
DEFAULT_API_BASE_URL,
DEFAULT_GRAPHQL_API_PATH,
ApplicationPluginConfig
} from '@gauzy/common';
import { ApplicationPluginConfig } from '@gauzy/common';
import { DEFAULT_API_HOST, DEFAULT_API_PORT, DEFAULT_API_BASE_URL, DEFAULT_GRAPHQL_API_PATH } from '@gauzy/constants';
import {} from '@gauzy/contracts';
import { dbTypeOrmConnectionConfig, dbMikroOrmConnectionConfig, dbKnexConnectionConfig } from './database';

process.cwd();
Expand Down
7 changes: 6 additions & 1 deletion packages/constants/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
export * from './lib/task';
export * from './lib/ai';
export * from './lib/api';
export * from './lib/code';
export * from './lib/github';
export * from './lib/reflect-metadata';
export * from './lib/organization';
export * from './lib/task';
4 changes: 4 additions & 0 deletions packages/constants/src/lib/ai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Injection token for Gauzy AI configuration.
*/
export const GAUZY_AI_CONFIG_TOKEN = 'GAUZY_AI_CONFIG_TOKEN';
7 changes: 7 additions & 0 deletions packages/constants/src/lib/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* API-related default constants used across all packages.
*/
export const DEFAULT_API_PORT = 3000;
export const DEFAULT_API_HOST = '127.0.0.1';
export const DEFAULT_API_BASE_URL = `http://${DEFAULT_API_HOST}:${DEFAULT_API_PORT}`;
export const DEFAULT_GRAPHQL_API_PATH = 'graphql';
10 changes: 10 additions & 0 deletions packages/constants/src/lib/code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Represents the default length of generated alphanumeric codes.
*/
export const ALPHA_NUMERIC_CODE_LENGTH = 6;

/**
* Represents a demo or magic code that allows authentication without a password.
* Typically used for demo environments or special login scenarios.
*/
export const DEMO_PASSWORD_LESS_MAGIC_CODE = '123456';
12 changes: 12 additions & 0 deletions packages/constants/src/lib/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Sync tag constants used to categorize synchronization sources.
*/
export const SyncTags = {
GITHUB: 'GitHub',
GAUZY: 'Gauzy'
} as const;

/**
* Type definition for valid sync tag values.
*/
export type SyncTag = (typeof SyncTags)[keyof typeof SyncTags];
7 changes: 7 additions & 0 deletions packages/constants/src/lib/reflect-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Metadata keys used for decorators and security features.
*/
export const PUBLIC_METHOD_METADATA = '__public:route__';
export const ROLES_METADATA = '__roles__';
export const PERMISSIONS_METADATA = '__permissions__';
export const FEATURE_METADATA = '__feature__';
Loading

0 comments on commit 202f34e

Please sign in to comment.