Skip to content

Commit

Permalink
Merge pull request #3 from npmstudy/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
npmstudy authored Oct 27, 2023
2 parents af7d35d + 9f846dd commit 34f22d4
Show file tree
Hide file tree
Showing 18 changed files with 272 additions and 63 deletions.
4 changes: 2 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"private": true,
"scripts": {
"start": "nodemon dist/app",
"start": "tsx --no-cache src/app.ts",
"build": "tsup src -- --dts-resolve",
"build:fast": "tsup src",
"test": "vitest run",
Expand All @@ -28,4 +28,4 @@
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}
}
107 changes: 55 additions & 52 deletions example/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,70 @@
import { createServer } from '@tomrpc/core';
import mount from '@tomrpc/mount';

const rpc = createServer({
base: import.meta.url,
beforeOne: function (ctx: any, key: string) {
console.log(ctx.path);
console.log(ctx.method);
console.log('beforeOne key=' + key);
},
});
(async () => {
const rpc = createServer({
base: import.meta.url,
beforeOne: function (ctx: any, key: string) {
console.log(ctx.path);
console.log(ctx.method);
console.log('beforeOne key=' + key);
},
});

rpc.fn('getUsers', function (a: string) {
return {
a: a,
msg: 'getUsers',
};
});

rpc.fn('postUsers', function (a: string) {
return {
a: a,
msg: 'postUsers',
};
});

rpc.fn('a', (a: string) => {
return a;
});
rpc.fn('getUsers', function (a: string) {
return {
a: a,
msg: 'getUsers',
};
});

rpc.fn('a.a', (a: string, ctx: any) => {
// console.dir(ctx);
if (ctx.method === 'POST') {
console.dir('post');
rpc.fn('postUsers', function (a: string) {
return {
a: a,
// b: b,
msg: 'postUsers',
};
} else {
console.dir('get' + ctx.method);
});

rpc.fn('a', (a: string) => {
return a;
}
});
});

rpc.fn('b', () => {
console.dir('test b');
});
rpc.fn('a.a', (a: string, ctx: any) => {
// console.dir(ctx);
if (ctx.method === 'POST') {
console.dir('post');
return {
a: a,
// b: b,
};
} else {
console.dir('get' + ctx.method);
return a;
}
});

rpc.add({
c: (a: string) => {
return a;
},
a: function (a: string, b: string) {
return `${this.path} , ${a} c2 ${b}`;
},
});
rpc.fn('b', () => {
console.dir('test b');
});

rpc.add({
c: (a: string) => {
return a;
},
a: function (a: string, b: string) {
return `${this.path} , ${a} c2 ${b}`;
},
});

rpc.base = import.meta.url;
mount(rpc, './fn');
// console.dir(mount);
rpc.base = import.meta.url;
await mount(rpc, './fn');

// https://bobbyhadz.com/blog/typescript-no-overload-matches-this-call
// rpc.dump();
// console.dir(rpc.dump());
// https://bobbyhadz.com/blog/typescript-no-overload-matches-this-call
// rpc.dump();
// console.dir(rpc.dump());

rpc.mount();
// rpc.mount();

rpc.listen(3000);
rpc.listen(3000);
})();
7 changes: 7 additions & 0 deletions packages/app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @tomrpc/mount

## 1.0.0

### Major Changes

- init
31 changes: 31 additions & 0 deletions packages/app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

# @tomrpc/app


tom.json

```
{
name:'hi'
base: import.meta.url
static: 'public'
core: true
view: true
jwt: true
port: 3000
mount: './fn'
lifeCyle: {
}
}
```


```js
import app from '@tomrpc/app';

const rpc = createServer(cfg);

rpc.start();

```
37 changes: 37 additions & 0 deletions packages/app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@tomrpc/app",
"version": "1.0.0",
"description": "My Awesome lib",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"author": "npmstudy <[email protected]>",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "tsup src -- --dts-resolve",
"build:fast": "tsup src",
"dev": "tsup src --watch",
"test": "vitest run",
"test:watch": "vitest watch",
"coverage": "vitest run --coverage"
},
"keywords": [
"awesome-keywords"
],
"devDependencies": {
"@vitest/coverage-v8": "^0.34.2",
"happy-dom": "^6.0.4"
},
"dependencies": {
"@tomrpc/import-dir": "^1.2.4",
"debug": "^4.3.4",
"desm": "^1.3.0",
"flat": "^6.0.1"
}
}
9 changes: 9 additions & 0 deletions packages/app/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, expect, it } from 'vitest';

Check warning on line 1 in packages/app/src/__tests__/index.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

'expect' is defined but never used

import mount from '../mount';

Check warning on line 3 in packages/app/src/__tests__/index.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

'mount' is defined but never used

describe('lib', () => {
it('should render lib', () => {
// expect(lib()).toBe('lib');
});
});
25 changes: 25 additions & 0 deletions packages/app/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import debug from 'debug';

import mount from './mount';
import { isFunction } from './utils';

const log = debug('@tomrpc/mount');

// {a:{a:1, b:2}}
// {a.b:2, a.a:1}
// a.b => /a/b + function

export default async (rpc, dir) => {
const files = await mount(rpc.base, dir);

log(files);

Object.keys(files).forEach((key) => {
if (isFunction(files[key])) {
log('add funtion key=' + key);
rpc.fn(key, files[key]);
} else {
console.dir(`key=${key} is not a function`);
}
});
};
20 changes: 20 additions & 0 deletions packages/app/src/mount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import importDir, { join } from '@tomrpc/import-dir';
import debug from 'debug';
import { flatten } from 'flat';

const log = debug('@tomrpc/mount');

// {a:{a:1, b:2}}
// {a.b:2, a.a:1}
// a.b => /a/b + function

export default async (base, dir) => {
const files = await importDir(join(base, dir), {
recurse: true,
extensions: ['.ts', '.js', '.json'],
});

log(files);

return flatten(files);
};
6 changes: 6 additions & 0 deletions packages/app/src/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import mount from './mount';

(async () => {
const files = await mount(import.meta.url, './f');
console.dir(files);
})();
18 changes: 18 additions & 0 deletions packages/app/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const toString = Object.prototype.toString;

export function isFunction(fn) {
if (!fn) {
return false;
}
const string = toString.call(fn);
return (
string === '[object Function]' ||
(typeof fn === 'function' && string !== '[object RegExp]') ||
(typeof window !== 'undefined' &&
// IE8 and below
(fn === window.setTimeout ||
fn === window.alert ||
fn === window.confirm ||
fn === window.prompt))
);
}
8 changes: 8 additions & 0 deletions packages/app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": [
"vitest/globals"
]
}
}
12 changes: 12 additions & 0 deletions packages/app/tsup.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineConfig } from 'tsup';

export default defineConfig((options) => ({
entry: 'src/index.ts',
sourcemap: !options.watch,
minify: !options.watch,
dts: true,
format: ['esm'],
loader: {
'.js': 'jsx',
},
}));
10 changes: 10 additions & 0 deletions packages/app/vitest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference types="vitest" />

import { defineConfig } from 'vite';

export default defineConfig({
test: {
globals: true,
environment: 'happy-dom',
},
});
4 changes: 2 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ export const createServer = function (config?: any) {
const supportMethods = [];
httpMethods.forEach(function (m) {
if (lastKey.indexOf(m) != -1) {
console.log(m);
log(m);
supportMethods.push(m);
return m;
}
});
console.log(supportMethods);
// console.log(supportMethods);

if (supportMethods.length === 0) {
log('没有匹配到包含get/post等方法的函数');
Expand Down
23 changes: 23 additions & 0 deletions packages/mount/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

# @tomrpc/mount


```js
import mount from '@tomrpc/mount';


const rpc = createServer({
base: import.meta.url,
beforeOne: function (ctx: any, key: string) {
console.log(ctx.path);
console.log(ctx.method);
console.log('beforeOne key=' + key);
},
});

rpc.base = import.meta.url;
mount(rpc, './fn');

rpc.listen(3000);

```
6 changes: 3 additions & 3 deletions packages/mount/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"publishConfig": {
"access": "public"
},
"author": "npmstudy <npmstudy@126.com>",
"author": "npmstudy <npmstudy@qq.com>",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
Expand All @@ -29,9 +29,9 @@
"happy-dom": "^6.0.4"
},
"dependencies": {
"@tomrpc/import-dir": "^1.2.2",
"@tomrpc/import-dir": "^1.2.4",
"debug": "^4.3.4",
"desm": "^1.3.0",
"flat": "^6.0.1"
}
}
}
Loading

0 comments on commit 34f22d4

Please sign in to comment.