Skip to content

Commit 425f62f

Browse files
committed
Initial commit
1 parent ca6a36c commit 425f62f

22 files changed

+8422
-357
lines changed

.eslintrc

+1-15
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,5 @@
44
"project": "tsconfig.eslint.json"
55
},
66
"plugins": ["@typeofweb/eslint-plugin"],
7-
"extends": ["plugin:@typeofweb/recommended"],
8-
"rules": {
9-
"@typescript-eslint/consistent-type-assertions": "off",
10-
"eslint-comments/require-description": "off",
11-
"@typescript-eslint/no-non-null-assertion": "off",
12-
"@typescript-eslint/no-non-null-asserted-optional-chain": "off"
13-
},
14-
"overrides": [
15-
{
16-
"files": ["src/validators/**.ts", "src/modifiers/**.ts", "src/errors.ts"],
17-
"rules": {
18-
"functional/no-this-expression": 0
19-
}
20-
}
21-
]
7+
"extends": ["plugin:@typeofweb/recommended"]
228
}

.prettierrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"semi": true,
33
"singleQuote": true,
44
"trailingComma": "all",
5-
"printWidth": 100
5+
"printWidth": 120
66
}

README.md

+71
Original file line numberDiff line numberDiff line change
@@ -1 +1,72 @@
11
# @typeofweb/server
2+
3+
```ts
4+
const app = await createApp({
5+
host: 'localhost',
6+
port: 3000,
7+
cors: [],
8+
});
9+
10+
app.plugin(dbPlugin);
11+
app.plugin(authPlugin);
12+
13+
app._rawRouter;
14+
15+
app.route({
16+
path: '/health-check/:count',
17+
method: 'GET',
18+
validation: {
19+
query: {},
20+
params: {
21+
count: number(),
22+
},
23+
payload: {},
24+
response: {},
25+
},
26+
_rawMiddlewares: [],
27+
async handler(request) {
28+
const { query, params, payload, response } = request;
29+
await request.plugins.db.findOne();
30+
31+
request.events.emit('health-check', params.count);
32+
33+
request._rawReq;
34+
request._rawRes;
35+
},
36+
});
37+
38+
const server = await app.listen();
39+
```
40+
41+
```ts
42+
// dbPlugin.ts
43+
44+
declare module '@typeofweb/server' {
45+
interface TypeOfWebServerMeta {
46+
readonly db: PrismaClient;
47+
}
48+
49+
interface TypeOfWebRequestMeta {
50+
readonly auth: { readonly session: Session };
51+
}
52+
53+
interface TypeOfWebServerEvents {
54+
readonly 'health-check': number;
55+
}
56+
}
57+
58+
export const dbPlugin = createPlugin('db', async (app) => {
59+
return {
60+
server: new Prisma(),
61+
};
62+
});
63+
64+
export const authPlugin = createPlugin('auth', async (app) => {
65+
return {
66+
request(request) {
67+
const session = await request.plugins.db.sessions.findOne({ id: request.cookies.session });
68+
return { session };
69+
},
70+
};
71+
});
72+
```

examples/simple.ts

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { number } from '@typeofweb/schema';
2+
3+
import { createApp, createPlugin } from '../dist/index.js';
4+
5+
declare function findOne(): unknown;
6+
declare function findMany(): unknown;
7+
declare module '../dist' {
8+
interface TypeOfWebServerMeta {
9+
readonly db: {
10+
readonly findOne: typeof findOne;
11+
readonly findMany: typeof findMany;
12+
};
13+
}
14+
15+
interface TypeOfWebEvents {
16+
readonly 'health-check': number;
17+
}
18+
}
19+
20+
// plugins
21+
export const dbPlugin = createPlugin('db', () => {
22+
return {
23+
server() {
24+
return { findOne, findMany };
25+
},
26+
};
27+
});
28+
29+
const loggerPlugin = createPlugin('logger', () => {
30+
return {
31+
server() {
32+
console.info('Server started!');
33+
},
34+
request() {
35+
console.info(`Request coming through!`);
36+
},
37+
response() {
38+
console.info(`The server has responded.`);
39+
},
40+
};
41+
});
42+
43+
// app
44+
const app = createApp({
45+
hostname: 'localhost',
46+
port: 3000,
47+
cors: [],
48+
});
49+
50+
app.plugin(loggerPlugin);
51+
app.plugin(dbPlugin);
52+
53+
app.route({
54+
path: '/health-check/:count',
55+
method: 'get',
56+
validation: {
57+
params: {
58+
count: number(),
59+
},
60+
response: number(),
61+
},
62+
_rawMiddlewares: [],
63+
handler(_request) {
64+
// const { query, params } = request;
65+
// request.server.plugins.db.findMany();
66+
// request.server.events.emit('health-check', 123);
67+
return 1;
68+
},
69+
});
70+
71+
app
72+
.start()
73+
.then((server) => {
74+
console.log(`🙌 Server started at ${server.address.toString()}`);
75+
})
76+
.catch(console.error);

package.json

+15-10
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"version": "0.0.0",
44
"type": "module",
55
"exports": {
6-
"import": "./dist/index.mjs",
7-
"default": "./dist/index.mjs",
6+
"import": "./dist/index.js",
7+
"default": "./dist/index.js",
88
"require": "./dist/index.cjs"
99
},
1010
"main": "./dist/index.cjs",
@@ -33,37 +33,41 @@
3333
"server",
3434
"api"
3535
],
36+
"dependencies": {
37+
"@typeofweb/schema": "0.8.0-3",
38+
"@types/express": "4.17.12",
39+
"express": "4.17.1",
40+
"stoppable": "1.1.0"
41+
},
3642
"devDependencies": {
3743
"@rollup/plugin-commonjs": "19.0.0",
38-
"@rollup/plugin-node-resolve": "13.0.0",
44+
"@rollup/plugin-json": "4.1.0",
3945
"@rollup/plugin-typescript": "8.2.1",
4046
"@tsconfig/node12": "1.0.7",
4147
"@tsconfig/node14": "1.0.0",
4248
"@typeofweb/eslint-plugin": "0.1.10",
4349
"@types/jest": "26.0.23",
4450
"@types/node": "12",
45-
"@types/qs": "6.9.6",
46-
"@types/ramda": "0.27.40",
51+
"@types/stoppable": "1.1.1",
4752
"all-contributors-cli": "6.20.0",
4853
"eslint": "7.28.0",
4954
"fast-check": "2.16.0",
5055
"husky": "6.0.0",
5156
"jest": "27.0.4",
5257
"lint-staged": "11.0.0",
58+
"nodemon": "2.0.7",
5359
"prettier": "2.3.1",
54-
"qs": "6.10.1",
55-
"ramda": "0.27.1",
5660
"rimraf": "3.0.2",
5761
"rollup": "2.51.1",
5862
"rollup-plugin-filesize": "9.1.1",
5963
"rollup-plugin-license": "2.5.0",
6064
"rollup-plugin-prettier": "2.1.0",
6165
"rollup-plugin-terser": "7.0.2",
6266
"ts-jest": "27.0.3",
67+
"ts-node": "10.0.0",
6368
"tsd": "https://github.com/typeofweb/tsd#pkg",
6469
"tslib": "2.2.0",
65-
"typescript": "4.3.2",
66-
"weak-napi": "2.0.2"
70+
"typescript": "4.3.2"
6771
},
6872
"scripts": {
6973
"prejest": "yarn build",
@@ -73,7 +77,8 @@
7377
"build": "rimraf dist && rollup --config",
7478
"build:watch": "rollup --config --watch",
7579
"prepublishOnly": "yarn build",
76-
"prepare": "husky install"
80+
"prepare": "husky install",
81+
"ts-node": "nodemon -w dist -e js -x 'node --loader ts-node/esm'"
7782
},
7883
"lint-staged": {
7984
"**/*.ts": [

rollup.config.js

+35-20
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// @ts-check
22
import commonjs from '@rollup/plugin-commonjs';
3-
import resolve from '@rollup/plugin-node-resolve';
43
import typescript from '@rollup/plugin-typescript';
54
import prettier from 'rollup-plugin-prettier';
65
import { terser } from 'rollup-plugin-terser';
76
import filesize from 'rollup-plugin-filesize';
87
import license from 'rollup-plugin-license';
8+
import json from '@rollup/plugin-json';
99

1010
import pkg from './package.json';
1111

1212
const shouldCompress = process.env.COMPRESS_BUNDLES ? true : false;
13+
const shouldPrettify = !shouldCompress && (process.env.PRETTIFY ? true : false);
1314

1415
const rollupConfig = [
1516
{
@@ -22,15 +23,22 @@ const rollupConfig = [
2223
entryFileNames: pkg.exports.import.replace(/^\.\//, ''),
2324
sourcemap: true,
2425
plugins: [
25-
shouldCompress
26-
? terser({
27-
compress: true,
28-
mangle: true,
29-
ecma: 2019,
30-
})
31-
: prettier({
32-
parser: 'typescript',
33-
}),
26+
...(shouldCompress
27+
? [
28+
terser({
29+
compress: true,
30+
mangle: true,
31+
ecma: 2020,
32+
}),
33+
]
34+
: []),
35+
...(shouldPrettify
36+
? [
37+
prettier({
38+
parser: 'typescript',
39+
}),
40+
]
41+
: []),
3442
],
3543
},
3644
{
@@ -40,20 +48,27 @@ const rollupConfig = [
4048
entryFileNames: pkg.exports.require.replace(/^\.\//, ''),
4149
sourcemap: true,
4250
plugins: [
43-
shouldCompress
44-
? terser({
45-
compress: true,
46-
mangle: true,
47-
ecma: 2019,
48-
})
49-
: prettier({
50-
parser: 'typescript',
51-
}),
51+
...(shouldCompress
52+
? [
53+
terser({
54+
compress: true,
55+
mangle: true,
56+
ecma: 2020,
57+
}),
58+
]
59+
: []),
60+
...(shouldPrettify
61+
? [
62+
prettier({
63+
parser: 'typescript',
64+
}),
65+
]
66+
: []),
5267
],
5368
},
5469
],
5570
plugins: [
56-
resolve(),
71+
json(),
5772
commonjs({
5873
include: 'node_modules/**',
5974
}),

src/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
export {};
1+
export { createApp } from './modules/app';
2+
export { HttpStatusCode } from './modules/httpStatusCodes';
3+
export { createPlugin } from './modules/plugins';
4+
5+
export type { HttpMethod } from './modules/httpStatusCodes';
6+
export type { TypeOfWebApp, AppOptions, TypeOfWebServer } from './modules/shared';
7+
8+
export type { TypeOfWebServerMeta, TypeOfWebRequestMeta, TypeOfWebEvents } from './modules/augment';

0 commit comments

Comments
 (0)