Skip to content

Commit

Permalink
Merge pull request #40 from cdaringe/feat/ts-idioms
Browse files Browse the repository at this point in the history
refactor: project toolkit
  • Loading branch information
arunvishnun committed Jan 29, 2024
2 parents db532c9 + 10ee783 commit 17f894d
Show file tree
Hide file tree
Showing 14 changed files with 287 additions and 246 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: ci
on:
push:
branches: ['*']
branches: ["*"]
pull_request:

# Allows you to run this workflow manually from the Actions tab
Expand All @@ -12,13 +12,13 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [14.x,16.x,18.x,20.x]
node: [14.x, 16.x, 18.x, 20.x]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm i
- run: npm run coverage
- run: npm run test
env:
CI: true
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
registry=https://registry.npmjs.org/
engine-strict=true
engine-strict=false
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# docs †o be purged in separate changeset for less PR thrash
docs
CHANGELOG.md
lib
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ This module requires Node v14.x.x+.
- [API](#api)
- [electrodeServer](#electrodeserver)
- [`app` decorator](#app-decorator)
- [Enable compression](#enable-compression)
- [Increase bodyLimit Size](#increase-bodylimit-size)
- [Contributions](#contributions)
- [License](#license)

Expand Down Expand Up @@ -101,11 +103,10 @@ require("@xarc/fastify-server")({ deferStart: true }).then(server => {

You can pass in a config object that controls every aspect of the Fastify server.


```js
const config = {
connection: {
port: 9000,
port: 9000
}
};

Expand Down Expand Up @@ -468,30 +469,37 @@ handler: (request, reply) => {
console.log("Listening on ", request.app.config.connection.port);
};
```

## Enable compression

In Fastify compression can be achieved using [@fastify/compress](https://github.com/fastify/fastify-compress#fastifycompress)

```js
'@fastify/compress':{
priority: 200,
options:
{ global: true, encodings:['gzip'] }
options:
{ global: true, encodings:['gzip'] }
},
```

Currently, the following encoding tokens are supported, using the first acceptable token in this order:

```js
br
gzip
deflate
* (no preference — @fastify/compress will use gzip)
identity (no compression)
```

## Increase bodyLimit Size

```js
server: {
bodyLimit : 1048576//new size limit
}
```
server: {
bodyLimit: 1048576; //new size limit
}
```

## Contributions

Make sure you sign the CLA. Checkout the [contribution guide](https://github.com/electrode-io/electrode/blob/master/CONTRIBUTING.md)
Expand Down
7 changes: 0 additions & 7 deletions index.js

This file was deleted.

25 changes: 25 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/configuration
*/
import type { Config } from "jest";

const config: Config = {
preset: "ts-jest",
testEnvironment: "node",
collectCoverage: true,
coverageThreshold: {
global: {
statements: 95,
branches: 95,
functions: 95,
lines: 95
}
},
coverageDirectory: "coverage",
coverageProvider: "v8",
maxWorkers: 1,
testPathIgnorePatterns: ["/node_modules/", "/lib/", "src/config/test.ts"]
};

export default config;
76 changes: 14 additions & 62 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
"name": "@xarc/fastify-server",
"version": "4.0.9",
"description": "A configurable Fastify web server",
"main": "index.js",
"main": "lib/index.js",
"types": "./lib/index.d",
"scripts": {
"build": "rm -rf lib && tsc",
"prepublishOnly": "rm -rf lib && tsc && xrun xarc/check",
"test": "xrun xarc/test-only",
"coverage": "xrun xarc/test-cov",
"check": "xrun xarc/check",
"sample": "node test/sample/index.js",
"docs": "xrun xarc/docs"
"test": "jest",
"lint": "eslint",
"format": "prettier . --write",
"format:check": "prettier . --check",
"check": "run-p lint format:check",
"sample": "node test/sample/index.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -63,78 +64,29 @@
"xaa": "^1.7.0"
},
"devDependencies": {
"@babel/eslint-parser": "^7.22.15",
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@types/chai": "^4.2.14",
"@types/jest": "^29.5.11",
"@types/mocha": "^8.2.0",
"@types/node": "^14.14.16",
"@types/sinon": "^9.0.10",
"@types/sinon-chai": "^3.2.5",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"@xarc/module-dev": "^4.0.0",
"@xarc/run": "^1.0.4",
"@babel/eslint-parser": "^7.22.15",
"chai": "^4.2.0",
"eslint": "^7.16.0",
"eslint-config-walmart": "^2.2.1",
"eslint-plugin-filenames": "^1.1.0",
"eslint-plugin-jsdoc": "^30.7.9",
"eslint-plugin-tsdoc": "^0.2.11",
"intercept-stdout": "^0.1.2",
"jest": "^29.7.0",
"mitm": "^1.2.0",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"npm-run-all": "4.1.5",
"prettier": "3.2.4",
"run-verify": "^1.2.1",
"sinon": "^17.0.1",
"sinon-chai": "^3.5.0",
"source-map-support": "^0.5.19",
"superagent": "^7.0.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typedoc": "^0.24.8",
"typescript": "^5.0.0",
"typescript": "^5.3.3",
"xstdout": "^0.1.1"
},
"nyc": {
"extends": [
"@istanbuljs/nyc-config-typescript"
],
"all": true,
"reporter": [
"lcov",
"text",
"text-summary"
],
"exclude": [
"*clap.js",
"*clap.ts",
".eslintrc.js",
"coverage",
"dist",
"docs",
"gulpfile.js",
"lib",
"test",
"xrun*.js",
"xrun*.ts",
"index.js",
"tester"
],
"check-coverage": true,
"statements": 95,
"branches": 95,
"functions": 95,
"lines": 95,
"cache": true
},
"@xarc/module-dev": {
"features": [
"eslint",
"eslintTS",
"mocha",
"typedoc",
"typescript"
]
},
"mocha": {
"require": [
"ts-node/register",
Expand Down
1 change: 0 additions & 1 deletion src/electrode-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ async function startElectrodeServer(context): Promise<ElectrodeFastifyInstance>
await context.server.ready();
await context.registerPluginsPromise;
await server.listen(<FastifyListenOptions>{

port: config.connection.port,
host: config.connection.address
});
Expand Down
106 changes: 86 additions & 20 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,90 @@ import {

/* eslint-disable max-len */
export type {
LightMyRequestChain, InjectOptions, LightMyRequestResponse, LightMyRequestCallback, // 'light-my-request'
FastifyRequest, RequestGenericInterface, // './types/request'
LightMyRequestChain,
InjectOptions,
LightMyRequestResponse,
LightMyRequestCallback, // 'light-my-request'
FastifyRequest,
RequestGenericInterface, // './types/request'
FastifyReply, // './types/reply'
FastifyPluginCallback, FastifyPluginAsync, FastifyPluginOptions, FastifyPlugin, // './types/plugin'
FastifyListenOptions, FastifyInstance, PrintRoutesOptions, // './types/instance'
FastifyLoggerOptions, FastifyBaseLogger, FastifyLoggerInstance, FastifyLogFn, LogLevel, // './types/logger'
FastifyRequestContext, FastifyContextConfig, FastifyReplyContext, // './types/context'
RouteHandler, RouteHandlerMethod, RouteOptions, RouteShorthandMethod, RouteShorthandOptions, RouteShorthandOptionsWithHandler, RouteGenericInterface, // './types/route'
FastifyRegister, FastifyRegisterOptions, RegisterOptions, // './types/register'
FastifyBodyParser, FastifyContentTypeParser, AddContentTypeParser, hasContentTypeParser, getDefaultJsonParser, ProtoAction, ConstructorAction, // './types/content-type-parser'
FastifyPluginCallback,
FastifyPluginAsync,
FastifyPluginOptions,
FastifyPlugin, // './types/plugin'
FastifyListenOptions,
FastifyInstance,
PrintRoutesOptions, // './types/instance'
FastifyLoggerOptions,
FastifyBaseLogger,
FastifyLoggerInstance,
FastifyLogFn,
LogLevel, // './types/logger'
FastifyRequestContext,
FastifyContextConfig,
FastifyReplyContext, // './types/context'
RouteHandler,
RouteHandlerMethod,
RouteOptions,
RouteShorthandMethod,
RouteShorthandOptions,
RouteShorthandOptionsWithHandler,
RouteGenericInterface, // './types/route'
FastifyRegister,
FastifyRegisterOptions,
RegisterOptions, // './types/register'
FastifyBodyParser,
FastifyContentTypeParser,
AddContentTypeParser,
hasContentTypeParser,
getDefaultJsonParser,
ProtoAction,
ConstructorAction, // './types/content-type-parser'
FastifyError, // '@fastify/error'
FastifySchema, FastifySchemaCompiler, // './types/schema'
HTTPMethods, RawServerBase, RawRequestDefaultExpression, RawReplyDefaultExpression, RawServerDefault, ContextConfigDefault, RequestBodyDefault, RequestQuerystringDefault, RequestParamsDefault, RequestHeadersDefault, // './types/utils'
DoneFuncWithErrOrRes, HookHandlerDoneFunction, RequestPayload, onCloseAsyncHookHandler, onCloseHookHandler, onErrorAsyncHookHandler, onErrorHookHandler, onReadyAsyncHookHandler, onReadyHookHandler, onRegisterHookHandler, onRequestAsyncHookHandler, onRequestHookHandler, onResponseAsyncHookHandler, onResponseHookHandler, onRouteHookHandler, onSendAsyncHookHandler, onSendHookHandler, onTimeoutAsyncHookHandler, onTimeoutHookHandler, preHandlerAsyncHookHandler, preHandlerHookHandler, preParsingAsyncHookHandler, preParsingHookHandler, preSerializationAsyncHookHandler, preSerializationHookHandler, preValidationAsyncHookHandler, preValidationHookHandler, // './types/hooks'
FastifyServerFactory, FastifyServerFactoryHandler, // './types/serverFactory'
FastifyTypeProvider, FastifyTypeProviderDefault, // './types/type-provider'
FastifyErrorCodes, // './types/errors'
FastifySchema,
FastifySchemaCompiler, // './types/schema'
HTTPMethods,
RawServerBase,
RawRequestDefaultExpression,
RawReplyDefaultExpression,
RawServerDefault,
ContextConfigDefault,
RequestBodyDefault,
RequestQuerystringDefault,
RequestParamsDefault,
RequestHeadersDefault, // './types/utils'
DoneFuncWithErrOrRes,
HookHandlerDoneFunction,
RequestPayload,
onCloseAsyncHookHandler,
onCloseHookHandler,
onErrorAsyncHookHandler,
onErrorHookHandler,
onReadyAsyncHookHandler,
onReadyHookHandler,
onRegisterHookHandler,
onRequestAsyncHookHandler,
onRequestHookHandler,
onResponseAsyncHookHandler,
onResponseHookHandler,
onRouteHookHandler,
onSendAsyncHookHandler,
onSendHookHandler,
onTimeoutAsyncHookHandler,
onTimeoutHookHandler,
preHandlerAsyncHookHandler,
preHandlerHookHandler,
preParsingAsyncHookHandler,
preParsingHookHandler,
preSerializationAsyncHookHandler,
preSerializationHookHandler,
preValidationAsyncHookHandler,
preValidationHookHandler, // './types/hooks'
FastifyServerFactory,
FastifyServerFactoryHandler, // './types/serverFactory'
FastifyTypeProvider,
FastifyTypeProviderDefault, // './types/type-provider'
FastifyErrorCodes // './types/errors'
} from "fastify";
/* eslint-enable max-len */

Expand All @@ -32,11 +99,10 @@ export type ServerInfo = {
port: number;
};

export interface ElectrodeFastifyInstance
extends FastifyInstance {
info: ServerInfo;
start: () => Promise<any>;
app: { config: any } & Record<string, any>;
export interface ElectrodeFastifyInstance extends FastifyInstance {
info: ServerInfo;
start: () => Promise<any>;
app: { config: any } & Record<string, any>;
}

/**
Expand Down
16 changes: 8 additions & 8 deletions test/dist/html/hello.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<head>
<meta charset="UTF-8" />
<title>Hello</title>
</head>
<body>
Hello Test!
</body>
</html>
</head>
<body>
Hello Test!
</body>
</html>

0 comments on commit 17f894d

Please sign in to comment.