Skip to content

Commit

Permalink
feat: update ioc,backend package;add rpc protocoll package;add cache …
Browse files Browse the repository at this point in the history
…& config module;update docs
  • Loading branch information
nailiable committed Oct 29, 2024
1 parent d5a8806 commit cdf4d5a
Show file tree
Hide file tree
Showing 63 changed files with 1,021 additions and 148 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"farmfe",
"flexable",
"Inversify",
"jexl",
"linkcode",
"Naily",
"nailyjs",
Expand Down
14 changes: 7 additions & 7 deletions docs/.vitepress/cache/deps/_metadata.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
{
"hash": "1cdb2662",
"hash": "e6fe7b68",
"configHash": "6bb8cdf2",
"lockfileHash": "f84ebc7b",
"browserHash": "32d6c109",
"lockfileHash": "4a58e439",
"browserHash": "30c24cb6",
"optimized": {
"vue": {
"src": "../../../../node_modules/.pnpm/[email protected][email protected]/node_modules/vue/dist/vue.runtime.esm-bundler.js",
"file": "vue.js",
"fileHash": "2029d163",
"fileHash": "0114871d",
"needsInterop": false
},
"vitepress > @vue/devtools-api": {
"src": "../../../../node_modules/.pnpm/@[email protected]/node_modules/@vue/devtools-api/dist/index.js",
"file": "vitepress___@vue_devtools-api.js",
"fileHash": "02f5f3ec",
"fileHash": "cec2fd64",
"needsInterop": false
},
"vitepress > @vueuse/core": {
"src": "../../../../node_modules/.pnpm/@[email protected][email protected][email protected]_/node_modules/@vueuse/core/index.mjs",
"file": "vitepress___@vueuse_core.js",
"fileHash": "b0f77eba",
"fileHash": "75c9c5c6",
"needsInterop": false
},
"@shikijs/vitepress-twoslash/client": {
"src": "../../../../node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected][email protected][email protected]/node_modules/@shikijs/vitepress-twoslash/dist/client.mjs",
"file": "@shikijs_vitepress-twoslash_client.js",
"fileHash": "bb186c04",
"fileHash": "43aaa425",
"needsInterop": false
}
},
Expand Down
30 changes: 23 additions & 7 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export default defineConfig({
target: ScriptTarget.ES2022,
module: ModuleKind.ES2022,
moduleResolution: ModuleResolutionKind.Bundler,
experimentalDecorators: true,
emitDecoratorMetadata: true,
},
},
}),
Expand All @@ -28,10 +30,10 @@ export default defineConfig({
label: '简体中文',
lang: 'zh',
},
en: {
label: 'English',
lang: 'en',
},
// en: {
// label: 'English',
// lang: 'en',
// },
},

themeConfig: {
Expand All @@ -40,16 +42,30 @@ export default defineConfig({
{
text: '指南',
items: [
{ text: 'RPC指南', link: '/rpc-guide' },
{ text: 'IOC 指南', link: '/ioc-guide' },
{ text: 'RPC 指南', link: '/rpc-guide' },
{ text: 'Restful 指南', link: '/restful-guide' },
],
},
],

sidebar: [
{
text: 'RPC指南',
text: 'IOC 指南',
items: [
{ text: '开始使用 IOC', link: '/ioc-guide' },
],
},
{
text: 'RPC 指南',
items: [
{ text: '开始使用 RPC', link: '/rpc-guide' },
],
},
{
text: 'Restful 指南',
items: [
{ text: '开始使用', link: '/rpc-guide' },
{ text: '开始使用 Restful', link: '/restful-guide' },
],
},
],
Expand Down
2 changes: 1 addition & 1 deletion docs/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ hero:
actions:
- theme: brand
text: Quick Start
link: /rpc-guide
link: /ioc-guide
# - theme: alt
# text: API Examples
# link: /api-examples
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ hero:
actions:
- theme: brand
text: 快速开始
link: /rpc-guide
link: /ioc-guide
# - theme: alt
# text: API Examples
# link: /api-examples
Expand Down
39 changes: 39 additions & 0 deletions docs/ioc-guide/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 指南

Naily.js的核心是一个以`IOC`(即控制反转)为核心的框架,它的目标是提供一个`轻量级` `易扩展`的毛坯房,让你可以根据自己的需求来进行装修。

## 捆绑包大小

你可以使用[pkg-size.dev](https://pkg-size.dev/)来查看捆绑包的大小。

<table>
<thead>
<tr>
<th>捆绑包</th>
<th>安装大小</th>
<th>捆绑包大小</th>
</tr>
</thead>
<tbody>
<tr>
<td>核心容器</td>
<td><a href="https://pkg-size.dev/@nailyjs/ioc"><img src="https://pkg-size.dev/badge/install/383266" title="Install size for @nailyjs/ioc"></a></td>
<td><a href="https://pkg-size.dev/@nailyjs/ioc"><img src="https://pkg-size.dev/badge/bundle/20331" title="Bundle size for @nailyjs/ioc"></a></td>
</tr>
<tr>
<td>后端支持</td>
<td><a href="https://pkg-size.dev/@nailyjs/backend"><img src="https://pkg-size.dev/badge/install/383266" title="Install size for @nailyjs/ioc-backend"></a></td>
<td><a href="https://pkg-size.dev/@nailyjs/backend"><img src="https://pkg-size.dev/badge/bundle/20331" title="Bundle size for @nailyjs/backend"></a></td>
</tr>
<tr>
<td>RPC支持</td>
<td><a href="https://pkg-size.dev/@nailyjs/rpc"><img src="https://pkg-size.dev/badge/install/383266" title="Install size for @nailyjs/rpc"></a></td>
<td><a href="https://pkg-size.dev/@nailyjs/rpc"><img src="https://pkg-size.dev/badge/bundle/20331" title="Bundle size for @nailyjs/rpc"></a></td>
</tr>
<tr>
<td>unplugin-rpc插件</td>
<td><a href="https://pkg-size.dev/unplugin-rpc"><img src="https://pkg-size.dev/badge/install/383266" title="Install size for unplugin-rpc"></a></td>
<td><a href="https://pkg-size.dev/unplugin-rpc"><img src="https://pkg-size.dev/badge/bundle/20331" title="Bundle size for unplugin-rpc"></a></td>
</tr>
</tbody>
</table>
50 changes: 50 additions & 0 deletions docs/restful-guide/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Restful 指南

传统的Restful API同样受Naily.js支持。和`Spring`/`Cell.js`/`Nest.js`/`Midway.js`等一众`IOC框架`一样,Naily.js也提供了一套`Restful`的编程模型。

::: tip 注意
虽然Naily.js支持Restful API,但是我们更主推`Naily RPC`。Restful的理念长久以来一直受到争议,如果是新项目,我们更推荐使用`Naily RPC`,方便快捷创建全栈应用。
:::

## 创建 Restful API

类似其他的`IOC`框架,我们可以通过 `@RestController` `@Get` 等来创建一个Restful API。

```typescript twoslash
// welcome.controller.ts
import { Get, RestController } from '@nailyjs/backend'

@RestController()
export class UserController {
@Get()
getUsers() {
return 'Hello, World!'
}
}
```

## 启动应用程序

`naily.js`内部参考`nest.js`的架构提供了一个`Adapter`,但是这个`Adapter`架构默认不和`nest.js`一样用来切换`express`/`fastify`等底层框架,而是用来切换`node.js`/`bun`/`deno`等运行时环境的。

比如下面一个例子,我们从`@nailyjs/backend`的分包`node-adapter`中导入`NodeBootstrap`,就可以在`node.js`环境下创建一个`HTTP`服务器并启动它了。

```typescript twoslash
// main.ts
import { NodeBootstrap } from '@nailyjs/backend/node-adapter'
import './welcome.controller'

new NodeBootstrap()
.run(3000)
.then(() => console.log(`Backend started on port http://localhost:3000`))
```

创建好了服务器,我们需要导入刚刚我们在`welcome.controller.ts`中创建的`UserController`,这样我们的`UserController`才能被`NodeBootstrap`扫描到。

::: tip
目前`naily.js`只支持`node.js`环境,后续会支持更多的运行时环境,如`bun`等。

以后我们会提供更多的`Adapter`,比如`node-express-adapter`/`node-fastify-adapter`等,但是大概率不会封装在`@nailyjs/backend`中,而是单独的分包。
:::

同样这里也是参考了`cell.js`的机制,但是启动器这块比`cell.js``export default autoBind()`更加的透明,不会让初学者抓不着头脑。
3 changes: 3 additions & 0 deletions fixtures/backend/naily.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
hello: 'world',
}
22 changes: 22 additions & 0 deletions fixtures/backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "backend",
"type": "module",
"version": "0.0.4",
"private": true,
"description": "The backend fixture",
"author": "Naily Zero <[email protected]> (https://naily.cc)",
"scripts": {
"build": "tsup",
"watch": "tsup -w",
"dev": "tsup && node dist/main.js"
},
"dependencies": {
"@nailyjs/backend": "workspace:*",
"@nailyjs/config": "workspace:*",
"@nailyjs/ioc": "workspace:*",
"@nailyjs/jexl": "workspace:*"
},
"devDependencies": {
"tsup": "^8.3.0"
}
}
9 changes: 9 additions & 0 deletions fixtures/backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { NodeBootstrap } from '@nailyjs/backend/node-adapter'
import { Configuration } from '@nailyjs/config'
import './test.controller'
import './test.filter'

new NodeBootstrap()
.use(Configuration())
.then(bootstrap => bootstrap.run(3000))
.then(() => console.log(`Backend started on port http://localhost:3000`))
20 changes: 20 additions & 0 deletions fixtures/backend/src/test.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable ts/consistent-type-imports */

import { Get, RestController } from '@nailyjs/backend'
import { Value } from '@nailyjs/config'
import { Autowired } from '@nailyjs/ioc'
import { JexlExecutor } from '@nailyjs/jexl'

@RestController()
export class TestController {
@Value('1 + 1')
private readonly hello: string

@Autowired()
private readonly jexl: JexlExecutor

@Get()
getString(): string {
return 'Hello World'
}
}
9 changes: 9 additions & 0 deletions fixtures/backend/src/test.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Catch, Filter } from '@nailyjs/ioc'

@Filter()
export class TestFilter {
@Catch()
catch(error: unknown): void {
console.log('catch error', error)
}
}
10 changes: 10 additions & 0 deletions fixtures/backend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "ES2022",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"module": "ES2022",
"moduleResolution": "Bundler"
},
"include": ["src"]
}
11 changes: 11 additions & 0 deletions fixtures/backend/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from 'tsup'

export default defineConfig({
entry: {
main: './src/main.ts',
},
dts: true,
sourcemap: true,
clean: true,
format: ['cjs', 'esm'],
})
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
"build:rpc": "pnpm -F @nailyjs/rpc build",
"build:rpc-docs-protocol": "pnpm -F @nailyjs/rpc-docs-protocol build",
"build:unplugin-rpc": "pnpm -F unplugin-rpc build",
"build:jexl": "pnpm -F @nailyjs/jexl build",
"build:config": "pnpm -F @nailyjs/config build",
"play:rpc": "pnpm -F rpc-playground dev",
"play:build": "pnpm -F rpc-playground build",
"dev:backend": "pnpm -F backend dev",
"lint": "eslint .",
"postinstall": "npx simple-git-hooks",
"test": "vitest",
Expand Down
8 changes: 2 additions & 6 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"./utils": {
"types": "./dist/utils.d.ts",
"import": "./dist/utils.js",
"require": "./dist/utils.cjs"
},
"./node-adapter": {
"types": "./dist/node-adapter.d.ts",
"import": "./dist/node-adapter.js",
Expand All @@ -44,7 +39,8 @@
"prepublishOnly": "tsup"
},
"dependencies": {
"@nailyjs/ioc": "workspace:*"
"@nailyjs/ioc": "workspace:*",
"path-to-regexp": "^8.2.0"
},
"devDependencies": {
"tsup": "^8.3.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/backend-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export abstract class AbstractHttpAdapter<ServerInstance = any> extends BackendC
* @return {Promise<void>}
* @memberof AbstractHttpAdapter
*/
abstract listen(port: number, callback?: () => any): Promise<void>
abstract listen(port: number, callback?: () => any): Promise<any>
/**
* ### Close the server.
*
Expand Down
Loading

0 comments on commit cdf4d5a

Please sign in to comment.