NestJSではじめるSymbol-SDK
Node.js まわりのバージョン
- Yarn v4.6.0
- Node.js v22.13.0
- NestJS v10.0.0
- Symbol SDK v3.2.3
CLI のインストール
npm install -g @nestjs/cli
※Yarn
はグローバルインストール出来なくなった
NestJS プロジェクト作成
例として test-nestjs
というプロジェクトを作成します。
nest new test-nestjs
yarn を使用するので yarn
を選択。
⚡ We will scaffold your app in a few seconds..
? Which package manager would you ❤️ to use?
npm
> yarn
pnpm
yarn が node_modules
フォルダを作成するようファイルを作成します。
echo "nodeLinker: node-modules" > .yarnrc.yml
Node.js などのバージョンのピン留めもしておきます。
volta pin node
volta pin npm
volta pin yarn
パッケージをインストールします。
yarn
NestJS ESM 化
package.json の編集
package.json
に "type": "module"
を追加します。
{
"name": "test-nestjs",
"version": "0.0.1",
"description": "",
"author": "",
"type": "module",
...
tsconfig.json の編集
tsconfig.json
を編集します。
module
を Node16
に変更し、"moduleResolution": "node16",
を追加します。
{
"compilerOptions": {
"module": "Node16",
"moduleResolution": "node16",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES2021",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
}
}
.eslintrc.js のファイル名変更と編集
.eslintrc.js
の拡張子を js
から cjs
に変更します。
また、ファイル内の ignorePatterns
にもファイル名があるのでそちらも変更します。
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.cjs'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
Jest の ESM 化
ESM 化すると Jest が動かなくなるので、Jest も ESM に対応させます。
パッケージ ts-jest-resolver
をインストール。
yarn add -D ts-jest-resolver
package.json
に moduleNameMapper
と resolver
を追記します。
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node",
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/src/$1"
},
"resolver": "ts-jest-resolver"
},
ソースの変更
import
のファイル名に .js
を付ける。
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller.js';
import { AppService } from './app.service.js';
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service.js';
import { Module } from '@nestjs/common';
import { AppController } from './app.controller.js';
import { AppService } from './app.service.js';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module.js';
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { AppModule } from './../src/app.module.js';
一旦 NestJS 動作確認
NestJS の確認
以下のコマンドで NestJS を起動し、http://localhost:3000
にアクセスして Hello World!
が表示されれば OK。
yarn start:dev
Jest の確認
テストもエラーなく通ることを確認する。
yarn test
Symbol-SDK のインストール
symbol-sdk
をインストールします。
yarn add symbol-sdk
動作確認
確認のため簡単なコードを動かしてみます。
元からあった app.service.ts
にある getHello
メソッドを書き換えます。
import { Injectable } from '@nestjs/common';
import { PrivateKey } from 'symbol-sdk';
import { Address, Network, SymbolFacade } from 'symbol-sdk/symbol';
@Injectable()
export class AppService {
getHello(): string {
// faced生成
const facade = new SymbolFacade(Network.TESTNET);
// 秘密鍵生成
const privateKey = PrivateKey.random();
// 秘密鍵からキーペア生成
const keyPair = new SymbolFacade.KeyPair(privateKey);
// アドレス取得
const address = new Address(
facade.network.publicKeyToAddress(keyPair.publicKey),
);
return address.toString();
}
}
以下のコマンドで NestJS を起動し、http://localhost:3000
にアクセスして Symbol のアドレスが表示されれば OK。
yarn start:dev
Discussion