Skip to content

Commit

Permalink
Create nestjs-shared package (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
samteb authored Mar 26, 2024
1 parent 7b470f1 commit e468b19
Show file tree
Hide file tree
Showing 33 changed files with 274 additions and 38 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/nestjs-shared.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: '@narval/nestjs-shared CI'

on:
push:
paths:
- packages/nestjs-shared/**
- .github/workflows/nestjs-shared.yml
- jest.config.ts
- jest.preset.js
- .eslintrc.json
- .prettierrc
- package.json
- package-lock.json

jobs:
build-and-test:
name: Build and test

runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@master

- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: '20.4.0'

- name: Install dependencies
run: |
make install/ci
- name: Code format
shell: bash
run: |
make nestjs-shared/format/check
make nestjs-shared/lint/check
- name: Test types
shell: bash
run: |
make nestjs-shared/test/type
- name: Test upstream application types
shell: bash
run: |
make policy-engine/test/type
make armory/test/type
- name: Test unit
shell: bash
run: |
make nestjs-shared/test/unit
- name: Send Slack notification on failure
if: failure() && github.ref == 'refs/heads/main'
uses: 8398a7/action-slack@v3
with:
username: GitHub
author_name: '@narval/nestjs-shared CI failed'
status: ${{ job.status }}
fields: message,commit,author
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ include ./apps/armory/Makefile
include ./apps/devtool/Makefile
include ./apps/policy-engine/Makefile
include ./apps/vault/Makefile
include ./packages/nestjs-shared/Makefile
include ./packages/policy-engine-shared/Makefile
include ./packages/transaction-request-intent/Makefile
include ./packages/signature/Makefile
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

| Project | Status |
| ------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [@app/armory](./apps/armory/README.md) | <a href="https://github.com/narval-xyz/narval/actions/workflows/armory.yml" target="_blank"><img src="https://github.com/narval-xyz/narval/actions/workflows/armory.yml/badge.svg?branch=main" alt="@app/armory CI status" /></a> |
| [@app/policy-engine](./apps/policy-engine/README.md) | <a href="https://github.com/narval-xyz/narval/actions/workflows/policy-engine.yml" target="_blank"><img src="https://github.com/narval-xyz/narval/actions/workflows/policy-engine.yml/badge.svg?branch=main" alt="@app/policy-engine CI status" /></a> |
| [@app/armory](./apps/armory/README.md) | <a href="https://github.com/narval-xyz/narval/actions/workflows/armory.yml" target="_blank"><img src="https://github.com/narval-xyz/narval/actions/workflows/armory.yml/badge.svg?branch=main" alt="@app/armory CI status" /></a> |
| [@app/policy-engine](./apps/policy-engine/README.md) | <a href="https://github.com/narval-xyz/narval/actions/workflows/policy-engine.yml" target="_blank"><img src="https://github.com/narval-xyz/narval/actions/workflows/policy-engine.yml/badge.svg?branch=main" alt="@app/policy-engine CI status" /></a> |
| [@narval/encryption](./packages/encryption/README.md) | <a href="https://github.com/narval-xyz/narval/actions/workflows/encryption.yml" target="_blank"><img src="https://github.com/narval-xyz/narval/actions/workflows/encryption.yml/badge.svg?branch=main" alt="@narval/encryption CI status" /></a> |
| [@narval/policy-engine-shared](./packages/policy-engine-shared/README.md) | <a href="https://github.com/narval-xyz/narval/actions/workflows/policy-engine-shared.yml" target="_blank"><img src="https://github.com/narval-xyz/narval/actions/workflows/policy-engine-shared.yml/badge.svg?branch=main" alt="@narval/policy-engine-shared CI status" /></a> |
| [@narval/nestjs-shared](./packages/nestjs-shared/README.md) | <a href="https://github.com/narval-xyz/narval/actions/workflows/nestjs-shared.yml" target="_blank"><img src="https://github.com/narval-xyz/narval/actions/workflows/nestjs-shared.yml/badge.svg?branch=main" alt="@narval/nestjs-shared CI status" /></a> |
| [@narval/policy-engine-shared](./packages/policy-engine-shared/README.md) | <a href="https://github.com/narval-xyz/narval/actions/workflows/policy-engine-shared.yml" target="_blank"><img src="https://github.com/narval-xyz/narval/actions/workflows/policy-engine-shared.yml/badge.svg?branch=main" alt="@narval/policy-engine-shared CI status" /></a> |
| [@narval/signature](./packages/signature/README.md) | <a href="https://github.com/narval-xyz/narval/actions/workflows/signature.yml" target="_blank"><img src="https://github.com/narval-xyz/narval/actions/workflows/signature.yml/badge.svg?branch=main" alt="@narval/signature CI status" /></a> |
| [@narval/transaction-request-intent](./packages/transaction-request-intent/README.md) | <a href="https://github.com/narval-xyz/narval/actions/workflows/transaction-request-intent.yml" target="_blank"><img src="https://github.com/narval-xyz/narval/actions/workflows/transaction-request-intent.yml/badge.svg?branch=main" alt="@narval/transaction-request-intent CI status" /></a> |

Expand Down
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 @@
NESTJS_SHARED_PROJECT_NAME := nestjs-shared
NESTJS_SHARED_PROJECT_DIR := ./packages/nestjs-shared

# == Code format ==

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

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

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

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

# == Testing ==

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

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

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

This library contains the shared nestjs utilities such as validators, DTOs and decorators.

## Testing

```bash
make nestjs-shared/test/unit
make nestjs-shared/test/unit/watch
```

## Formatting

```bash
make nestjs-shared/format
make nestjs-shared/lint

make nestjs-shared/format/check
make nestjs-shared/lint/check
```
18 changes: 18 additions & 0 deletions packages/nestjs-shared/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Config } from 'jest'

const config: Config = {
displayName: 'nestjs-shared',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]sx?$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json'
}
]
}
}

export default config
9 changes: 9 additions & 0 deletions packages/nestjs-shared/jest.unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Config } from 'jest'
import sharedConfig from './jest.config'

const config: Config = {
...sharedConfig,
testMatch: ['<rootDir>/**/__test__/unit/**/*.spec.ts']
}

export default config
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/nestjs-shared/**/*.ts"]
}
},
"test:unit": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "packages/nestjs-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"]
}
Loading

0 comments on commit e468b19

Please sign in to comment.