Skip to content

Commit

Permalink
Create nestjs-shared package
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel committed Mar 26, 2024
1 parent 325a86c commit 63ae3c3
Show file tree
Hide file tree
Showing 28 changed files with 165 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Address, Hex, IsHexString } from '@narval/policy-engine-shared'
import { IsHexString } from '@narval/nestjs-shared'
import { Address, Hex } from '@narval/policy-engine-shared'
import { ApiPropertyOptional } from '@nestjs/swagger'
import { Transform, Type } from 'class-transformer'
import {
Expand Down
18 changes: 18 additions & 0 deletions apps/devtool/src/app/components/EditorComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client'

import Editor from '@monaco-editor/react'
import { EntityUtil, entityDataSchema, policyDataSchema } from '@narval/policy-engine-shared'
import { Jwk, Payload, SigningAlg, hash, hexToBase64Url, signJwt } from '@narval/signature'
import { getAccount, signMessage } from '@wagmi/core'
import axios from 'axios'
Expand Down Expand Up @@ -37,6 +38,23 @@ const EditorComponent = () => {

const { entity, policy } = JSON.parse(data)

const validation = EntityUtil.validate(entity)

if (!validation.success) {
console.log(validation.issues)
}

const entityValidationResult = entityDataSchema.safeParse({ entity: { data: entity } })
const policyValidationResult = policyDataSchema.safeParse({ policy: { data: policy } })

if (!entityValidationResult.success) {
console.log('Invalid entity', entityValidationResult.error.errors)
}

if (!policyValidationResult.success) {
console.log('Invalid policy', policyValidationResult.error.errors)
}

const jwtSigner = async (msg: string) => {
const jwtSig = await signMessage(config, { message: msg })

Expand Down
9 changes: 2 additions & 7 deletions apps/policy-engine/src/engine/evaluation-request.dto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import {
AccountId,
Action,
FiatCurrency,
SignMessageRequestDataDto,
SignTransactionRequestDataDto
} from '@narval/policy-engine-shared'
import { SignMessageRequestDataDto, SignTransactionRequestDataDto } from '@narval/nestjs-shared'
import { AccountId, Action, FiatCurrency } from '@narval/policy-engine-shared'
import { ApiExtraModels, ApiProperty, getSchemaPath } from '@nestjs/swagger'
import { Type } from 'class-transformer'
import { IsDefined, IsOptional, ValidateNested } from 'class-validator'
Expand Down
3 changes: 2 additions & 1 deletion apps/vault/src/vault/http/rest/dto/import-private-key-dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Hex, IsHexString } from '@narval/policy-engine-shared'
import { IsHexString } from '@narval/nestjs-shared'
import { Hex } from '@narval/policy-engine-shared'
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import { IsOptional, IsString } from 'class-validator'

Expand Down
3 changes: 2 additions & 1 deletion apps/vault/src/vault/http/rest/dto/sign-request.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Action, SignMessageRequestDataDto, SignTransactionRequestDataDto } from '@narval/policy-engine-shared'
import { SignMessageRequestDataDto, SignTransactionRequestDataDto } from '@narval/nestjs-shared'
import { Action } from '@narval/policy-engine-shared'
import { ApiExtraModels, ApiProperty, getSchemaPath } from '@nestjs/swagger'
import { Type } from 'class-transformer'
import { IsDefined, ValidateNested } from 'class-validator'
Expand Down
18 changes: 18 additions & 0 deletions packages/nestjs-shared/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
29 changes: 29 additions & 0 deletions packages/nestjs-shared/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
POLICY_ENGINE_SHARED_PROJECT_NAME := nestjs-shared
POLICY_ENGINE_SHARED_PROJECT_DIR := ./packages/nestjs-shared

# == Code format ==

nestjs-shared/format:
npx nx format:write --projects ${POLICY_ENGINE_SHARED_PROJECT_NAME}

nestjs-shared/lint:
npx nx lint ${POLICY_ENGINE_SHARED_PROJECT_NAME} -- --fix

nestjs-shared/format/check:
npx nx format:check --projects ${POLICY_ENGINE_SHARED_PROJECT_NAME}

nestjs-shared/lint/check:
npx nx lint ${POLICY_ENGINE_SHARED_PROJECT_NAME}

# == Testing ==

nestjs-shared/test/type:
npx tsc \
--project ${POLICY_ENGINE_SHARED_PROJECT_DIR}/tsconfig.lib.json \
--noEmit

nestjs-shared/test/unit:
npx nx test:unit ${POLICY_ENGINE_SHARED_PROJECT_NAME} -- ${ARGS}

nestjs-shared/test/unit/watch:
make nestjs-shared/test/unit ARGS=--watch
7 changes: 7 additions & 0 deletions packages/nestjs-shared/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# nestjs-shared

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test nestjs-shared` to execute the unit tests via [Jest](https://jestjs.io).
11 changes: 11 additions & 0 deletions packages/nestjs-shared/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable */
export default {
displayName: 'nestjs-shared',
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }]
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/packages/nestjs-shared'
}
24 changes: 24 additions & 0 deletions packages/nestjs-shared/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "nestjs-shared",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/nestjs-shared/src",
"projectType": "library",
"targets": {
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/policy-engine-shared/**/*.ts"]
}
},
"test:unit": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "packages/policy-engine-shared/jest.unit.ts",
"verbose": true
}
}
},
"tags": []
}
2 changes: 2 additions & 0 deletions packages/nestjs-shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './lib/decorators'
export * from './lib/dto'
5 changes: 5 additions & 0 deletions packages/nestjs-shared/src/lib/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './is-account-id.decorator'
export * from './is-asset-id.decorator'
export * from './is-hex-string.decorator'
export * from './is-not-empty-array-enum.decorator'
export * from './is-not-empty-array-string.decorator'
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isAccountId } from '@narval/policy-engine-shared'
import { ValidationOptions, registerDecorator } from 'class-validator'
import { isAccountId } from '../util/caip.util'

export function IsAccountId(validationOptions?: ValidationOptions) {
return function (object: object, propertyName: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isAssetId } from '@narval/policy-engine-shared'
import { ValidationOptions, registerDecorator } from 'class-validator'
import { isAssetId } from '../util/caip.util'

export function IsAssetId(validationOptions?: ValidationOptions) {
return function (object: object, propertyName: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isHexString } from '@narval/policy-engine-shared'
import { ValidationOptions, registerDecorator } from 'class-validator'
import { isHexString } from '../util/typeguards'

export function IsHexString(validationOptions?: ValidationOptions) {
return function (object: object, propertyName: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Action } from '@narval/policy-engine-shared'
import { ApiProperty } from '@nestjs/swagger'
import { IsDefined, IsIn, IsString } from 'class-validator'
import { Action } from '../type/action.type'

export class BaseActionDto {
@IsIn(Object.values(Action))
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Action } from '@narval/policy-engine-shared'
import { ApiProperty } from '@nestjs/swagger'
import { IsDefined, IsIn, IsString } from 'class-validator'
import { Action } from '../type/action.type'
import { BaseActionDto } from './'

export class SignMessageRequestDataDto extends BaseActionDto {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Action, Address, Hex } from '@narval/policy-engine-shared'
import { ApiProperty } from '@nestjs/swagger'
import { Transform, Type } from 'class-transformer'
import { IsDefined, IsEthereumAddress, IsIn, IsInt, IsOptional, IsString, Min, ValidateNested } from 'class-validator'
import { IsHexString } from '../decorators/is-hex-string.decorator'
import { Action } from '../type/action.type'
import { Address, Hex } from '../type/domain.type'
import { BaseActionDto } from './'

class AccessListDto {
Expand Down
16 changes: 16 additions & 0 deletions packages/nestjs-shared/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs"
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
11 changes: 11 additions & 0 deletions packages/nestjs-shared/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts"]
}
9 changes: 9 additions & 0 deletions packages/nestjs-shared/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts", "src/**/*.d.ts"]
}
8 changes: 0 additions & 8 deletions packages/policy-engine-shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
export * from './lib/decorators/is-account-id.decorator'
export * from './lib/decorators/is-asset-id.decorator'
export * from './lib/decorators/is-hex-string.decorator'
export * from './lib/decorators/is-not-empty-array-enum.decorator'
export * from './lib/decorators/is-not-empty-array-string.decorator'

export * from './lib/dto'

export * from './lib/schema/address.schema'
export * from './lib/schema/data-store.schema'
export * from './lib/schema/domain.schema'
Expand Down
1 change: 1 addition & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"paths": {
"@narval/config-module": ["packages/config-module/src/index.ts"],
"@narval/encryption-module": ["packages/encryption-module/src/index.ts"],
"@narval/nestjs-shared": ["packages/nestjs-shared/src/index.ts"],
"@narval/policy-engine-shared": ["packages/policy-engine-shared/src/index.ts"],
"@narval/signature": ["packages/signature/src/index.ts"],
"@narval/transaction-engine-module": ["packages/transaction-engine-module/src/index.ts"],
Expand Down

0 comments on commit 63ae3c3

Please sign in to comment.