Skip to content

Commit

Permalink
feat(keto-cli): type and validate ConfigService values
Browse files Browse the repository at this point in the history
  • Loading branch information
getlarge committed Apr 3, 2024
1 parent 7ad906d commit f4d8242
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 13 deletions.
41 changes: 40 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"@nestjs/core": "^10.0.2",
"@nestjs/platform-express": "^10.0.2",
"@ory/client": "^1.4.9",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"defekt": "^9.3.1",
"nest-commander": "^3.12.5",
"reflect-metadata": "^0.1.13",
Expand Down Expand Up @@ -40,6 +42,7 @@
"eslint-plugin-unused-imports": "^3.0.0",
"jest": "^29.4.1",
"jest-environment-node": "^29.4.1",
"nest-commander-testing": "^3.3.0",
"nx": "17.2.8",
"prettier": "^2.6.2",
"supertest": "^6.3.3",
Expand Down
24 changes: 15 additions & 9 deletions packages/keto-cli/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,30 @@ import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';

import { CheckPermissionCommand } from './check-permission.command';
import { OryKetoEnvironmentVariables, validate } from './environment-variables';

@Module({
imports: [
ConfigModule.forRoot({}),
ConfigModule.forRoot({
isGlobal: true,
validate,
cache: true,
}),
OryPermissionsModule.forRootAsync({
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
basePath: configService.get(
'ORY_KETO_PUBLIC_URL',
'http://localhost:4466'
),
useFactory: (
configService: ConfigService<OryKetoEnvironmentVariables, true>
) => ({
basePath: configService.get('ORY_KETO_PUBLIC_URL'),
}),
}),
OryRelationshipsModule.forRootAsync({
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
accessToken: configService.get('ORY_KETO_API_KEY', ''),
basePath: configService.get('ORY_KETO_ADMIN_URL', ''),
useFactory: (
configService: ConfigService<OryKetoEnvironmentVariables, true>
) => ({
accessToken: configService.get('ORY_KETO_API_KEY'),
basePath: configService.get('ORY_KETO_ADMIN_URL'),
}),
}),
],
Expand Down
49 changes: 49 additions & 0 deletions packages/keto-cli/src/app/environment-variables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Expose, plainToInstance } from 'class-transformer';
import { IsOptional, IsString, IsUrl, validateSync } from 'class-validator';

export class OryKetoEnvironmentVariables {
@Expose()
@IsUrl({
require_protocol: true,
require_valid_protocol: true,
require_host: true,
require_tld: false,
})
ORY_KETO_ADMIN_URL?: string = 'http://localhost:4467';

@Expose()
@IsUrl({
require_protocol: true,
require_valid_protocol: true,
require_host: true,
require_tld: false,
})
ORY_KETO_PUBLIC_URL?: string = 'http://localhost:4466';

@Expose()
@IsString()
@IsOptional()
ORY_KETO_API_KEY?: string = undefined;
}

export function validate(env: Record<string, string>) {
const validatedConfig = plainToInstance(OryKetoEnvironmentVariables, env, {
enableImplicitConversion: true,
excludeExtraneousValues: true,
exposeDefaultValues: true,
});

const errors = validateSync(validatedConfig, {
skipMissingProperties: false,
forbidUnknownValues: true,
whitelist: true,
validationError: {
target: false,
},
});

if (errors.length > 0) {
throw new Error(errors.toString());
}
return validatedConfig;
}
5 changes: 3 additions & 2 deletions packages/keto-cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { CommandFactory } from 'nest-commander';

import { version } from '../package.json';
import { AppModule } from './app/app.module';

async function bootstrap(): Promise<void> {
await CommandFactory.run(AppModule, {
logger: ['log', 'error', 'warn', 'debug', 'verbose'],
enablePositionalOptions: true,
enablePassThroughOptions: true,
cliName: 'keto-cli',
version: '0.0.1',
cliName: '@getlarge/keto-cli',
version,
usePlugins: true,
});
}
Expand Down
3 changes: 2 additions & 1 deletion packages/keto-cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true
},
"files": [],
"include": [],
Expand Down

0 comments on commit f4d8242

Please sign in to comment.