Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test pull #12

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"root": true,
"extends": ["standard"],
"parser": "@typescript-eslint/parser",
"ignorePatterns": ["*.js"],
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module",
Expand Down
2 changes: 1 addition & 1 deletion .jscpd.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"ignorePattern": [
"import .* from '.*';"
],
"pattern": ["packages/*/src/**/*.ts"]
"pattern": ["packages/*/{src,tests}/**/*.ts"]
}
24 changes: 10 additions & 14 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
{
"editor.formatOnSave": true,
"gitscrum.company": "triptyk",
"gitscrum.project-slug": "krs-logistics",
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"[typescript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
},
"eslint.validate": [
"javascript",
"typescript"
],
"eslint.format.enable": true,
"typescript.preferences.importModuleSpecifierEnding": "js",
"typescript.preferences.importModuleSpecifier": "project-relative"
"editor.formatOnSave": true,
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"[typescript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
},
"eslint.validate": ["javascript", "typescript"],
"eslint.format.enable": true,
"typescript.preferences.importModuleSpecifierEnding": "js",
"typescript.preferences.importModuleSpecifier": "project-relative",
"jest.jestCommandLine": "pnpm test --"
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

A NodeJS Typescript framework using Koa !

Nothing has changed

## [See the wiki !](https://triptyk.github.io/nfw-core/)
47 changes: 31 additions & 16 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
const ESMPreset = {
preset: "ts-jest/presets/default-esm", // or other ESM presets
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
transform: {
"^.+\\.tsx?$": [
"ts-jest",
{
useESM: true,
},
],
},
};

function createProject(name) {
return {
displayName: name,
coveragePathIgnorePatterns: [`<rootDir>/packages/${name}/tests/.*`],
collectCoverageFrom: [`packages/${name}/src/**/*.ts`],
testMatch: [`<rootDir>/packages/${name}/tests/**/*.test.ts`],
...ESMPreset,
};
}

/** @type {import('ts-jest').JestConfigWithTsJest} */
export default {
testEnvironment: "node",
collectCoverage: Boolean(process.env.CI),
projects: [
{
displayName: "nfw-mikro-orm",
testMatch: ["<rootDir>/packages/mikro-orm/dist/tests/**/*.test.js"],
},
{
displayName: "nfw-core",
testMatch: ["<rootDir>/packages/core/dist/tests/**/*.test.js"],
},
{
displayName: "nfw-http",
testMatch: ["<rootDir>/packages/http/dist/tests/**/*.test.js"],
},
{
displayName: "nfw-jsonapi",
testMatch: ["<rootDir>/packages/jsonapi/dist/tests/**/*.test.js"],
},
createProject("mikro-orm"),
createProject("core"),
createProject("http"),
createProject("jsonapi"),
createProject("jsonapi-new"),
],
};
25 changes: 13 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "nfw-core",
"version": "1.0.0",
"private": "true",
"private": true,
"description": "",
"main": "index.js",
"scripts": {
"lint": "eslint 'packages/*/{src,tests}/**/*.ts'",
"lint:fix": "eslint 'packages/*/{src,tests}/**/*.ts' --fix",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --runInBand",
"test:jsonapi": "node --experimental-vm-modules node_modules/jest/bin/jest.js --selectProjects nfw-jsonapi",
"test:http": "node --experimental-vm-modules node_modules/jest/bin/jest.js --selectProjects nfw-http",
"test:core": "node --experimental-vm-modules node_modules/jest/bin/jest.js --selectProjects nfw-core",
"test:mikro-orm": "node --experimental-vm-modules node_modules/jest/bin/jest.js --selectProjects nfw-mikro-orm",
"test:jsonapi": "node --experimental-vm-modules node_modules/jest/bin/jest.js --selectProjects jsonapi",
"test:http": "node --experimental-vm-modules node_modules/jest/bin/jest.js --selectProjects http",
"test:core": "node --experimental-vm-modules node_modules/jest/bin/jest.js --selectProjects core",
"test:mikro-orm": "node --experimental-vm-modules node_modules/jest/bin/jest.js --selectProjects mikro-orm",
"jscpd": "jscpd --exitCode 1",
"build": "pnpm recursive run build",
"build:clean": "pnpm recursive run build:clean",
Expand All @@ -24,26 +24,27 @@
"@jest/globals": "^29.3.1",
"@jscpd/finder": "^3.5.1",
"@types/jest": "^29.2.3",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.43.0",
"eslint": "^8.27.0",
"@typescript-eslint/eslint-plugin": "^5.44.0",
"@typescript-eslint/parser": "^5.44.0",
"eslint": "^8.28.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "17.0.0",
"eslint-plugin-custom-elements": "^0.0.6",
"eslint-plugin-es": "^4",
"eslint-plugin-filenames": "^1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^27.1.5",
"eslint-plugin-jest": "^27.1.6",
"eslint-plugin-n": "^15.5.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-unicorn": "^44.0.2",
"eslint-plugin-unicorn": "^45.0.0",
"eslint-plugin-unused-imports": "^2.0.0",
"jest": "^29.3.1",
"jest": "^29.0.2",
"jscpd": "~3.5.1",
"npm-run-all": "^4.1.5",
"prettier": "^2",
"prettier": "^2.8.0",
"ts-jest": "^29.0.3",
"typescript": "~4.9.3"
},
"keywords": [],
Expand Down
7 changes: 2 additions & 5 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"esModuleInterop": true
}
}
"extends": "../../tsconfig.json"
}
5 changes: 5 additions & 0 deletions packages/http/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import baseConfig from "../../jest.config.js";

export default {
...baseConfig,
};
4 changes: 2 additions & 2 deletions packages/http/src/decorators/use-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import { container } from '@triptyk/nfw-core';
import type { Class } from 'type-fest';
import { MetadataStorage } from '../storages/metadata-storage.js';
import type { AnyMiddleware } from '../types/any-middleware.js';
import type { AnyMiddlewareType } from '../types/any-middleware.js';

export function UseMiddleware (middleware: AnyMiddleware) {
export function UseMiddleware (middleware: AnyMiddlewareType) {
return function (target: Class<unknown> | unknown, propertyName?: string) {
container.resolve(MetadataStorage).addMiddlewareUsage({
target,
Expand Down
3 changes: 3 additions & 0 deletions packages/http/src/errors/unknown-special-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class UnknownSpecialContextError extends Error {
public message = 'Unknown special context';
}
19 changes: 19 additions & 0 deletions packages/http/src/executables/executable-controller-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { RouterContext } from '@koa/router';
import type { ExecutableInterface } from '../interfaces/executable.js';
import type { ControllerContextType } from '../types/controller-context.js';
import { executeParams } from '../utils/execute-params.js';
import type { ExecutableParam } from './executable-param.js';

export class ExecutableControllerAction implements ExecutableInterface {
public constructor (
public controllerContext: ControllerContextType,
public params: ExecutableParam[]
) {}

public async execute (ctx: RouterContext) {
const controllerMethod = (this.controllerContext.controllerInstance as any)[this.controllerContext.controllerAction] as Function;
const params = await executeParams(this.params, ctx);

return controllerMethod.call(this.controllerContext.controllerInstance, ...params);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { RouterContext } from '@koa/router';
import { ForbiddenError } from '../errors/forbidden.js';
import type { ExecutableInterface } from '../interfaces/executable.js';
import type { GuardInterface } from '../interfaces/guard.js';
import type { ControllerContext } from '../types/controller-context.js';
import type { ControllerContextType } from '../types/controller-context.js';
import { executeParams } from '../utils/execute-params.js';
import type { ExecutableParam } from './executable-param.js';

export class ExecutableGuard {
export class ExecutableGuard implements ExecutableInterface {
public constructor (
public instance: GuardInterface,
public controllerContext: ControllerContext,
public controllerContext: ControllerContextType,
public params: ExecutableParam[]
) {}

Expand Down
21 changes: 21 additions & 0 deletions packages/http/src/executables/executable-param.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { RouterContext } from '@koa/router';
import type { ExecutableInterface } from '../interfaces/executable.js';
import type { ControllerContextType } from '../types/controller-context.js';
import type { ResolvedParamType } from '../types/resolved-param.js';

export class ExecutableParam implements ExecutableInterface {
public constructor (
public controllerContext: ControllerContextType,
private param: ResolvedParamType
) {}

public execute (ctx: RouterContext) {
if (typeof this.param === 'function') {
return this.param({
ctx,
...this.controllerContext
});
}
return this.param;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import type { RouterContext } from '@koa/router';
import type { ExecutableInterface } from '../interfaces/executable.js';
import type { ResponseHandlerInterface } from '../interfaces/response-handler.js';
import type { ControllerContext } from '../types/controller-context.js';
import type { ControllerContextType } from '../types/controller-context.js';
import { executeParams } from '../utils/execute-params.js';
import type { ExecutableParam } from './executable-param.js';

export class ExecutableResponseHandler {
export class ExecutableResponseHandler implements ExecutableInterface {
public constructor (
public instance: ResponseHandlerInterface,
public controllerContext: ControllerContext,
public controllerContext: ControllerContextType,
public params: ExecutableParam[]
) {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type Koa from 'koa';
import { RouterBuilderFactory } from './controller.js';
import { RouterBuilder } from '../routing/router-builder.js';
import type { Class } from 'type-fest';
import { container } from '@triptyk/nfw-core';
import { MetadataStorage } from '../storages/metadata-storage.js';
Expand All @@ -11,9 +11,10 @@ export interface CreateApplicationOptions {

export async function createApplication (options: CreateApplicationOptions) {
const app = options.server;
const metadataStorage = container.resolve(MetadataStorage);

for (const controller of options.controllers) {
const routerBuilder = new RouterBuilderFactory(container.resolve(MetadataStorage), app, controller);
const routerBuilder = new RouterBuilder(metadataStorage, app, controller);
await routerBuilder.createRoute();
}
}
22 changes: 16 additions & 6 deletions packages/http/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ export * from './decorators/use-middleware.js';

export * from './storages/metadata/endpoint.js';

export * from './types/any-middleware.js';
export * from './types/controller-context.js';
export * from './types/resolved-param.js';

export * from './interfaces/middleware.js';
export * from './interfaces/response-handler.js';
export * from './interfaces/guard.js';
export * from './interfaces/router-builder.js';
export * from './interfaces/metadata-storage.js';

export * from './factories/application.js';
export * from './factories/controller.js';
export * from './factories/create-application.js';
export * from './routing/router-builder.js';

export * from './utils/custom-decorator.js';
export * from './utils/execute-params.js';
Expand All @@ -41,10 +45,16 @@ export * from './errors/forbidden.js';
export * from './errors/router-metadata-not-found.js';

export * from './routing/controller-action.js';
export * from './routing/param-resolver.js';
export * from './routing/executable-guard.js';
export * from './routing/executable-param.js';
export * from './routing/executable-response-handler.js';

export * from './resolvers/controller-action-resolver.js';
export * from './resolvers/guard-resolver.js';
export * from './resolvers/param-resolver.js';
export * from './resolvers/response-handler-resolver.js';

export * from './executables/executable-controller-action.js';
export * from './executables/executable-guard.js';
export * from './executables/executable-param.js';
export * from './executables/executable-response-handler.js';

export * from './enums/http-method.js';

Expand Down
3 changes: 3 additions & 0 deletions packages/http/src/interfaces/executable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface ExecutableInterface {
execute(...args: unknown[]): unknown,
}
5 changes: 5 additions & 0 deletions packages/http/src/interfaces/resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { ExecutableInterface } from './executable.js';

export interface ResolverInterface {
resolve(...args: unknown[]): ExecutableInterface | ExecutableInterface[] | undefined,
}
19 changes: 19 additions & 0 deletions packages/http/src/resolvers/controller-action-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'reflect-metadata';
import type { MetadataStorageInterface } from '../interfaces/metadata-storage.js';
import type { ResolverInterface } from '../interfaces/resolver.js';
import type { ControllerContextType } from '../types/controller-context.js';
import { resolveParams } from '../utils/resolve-params.js';
import { ExecutableControllerAction } from '../executables/executable-controller-action.js';

export class ControllerActionResolver implements ResolverInterface {
public constructor (
public metadataStorage: MetadataStorageInterface,
public controllerContext: ControllerContextType
) {}

public resolve (): ExecutableControllerAction {
const paramsForRouteMetadata = this.metadataStorage.sortedParametersForEndpoint(this.controllerContext.controllerInstance.constructor, this.controllerContext.controllerAction);
const handles = resolveParams(paramsForRouteMetadata, this.controllerContext);
return new ExecutableControllerAction(this.controllerContext, handles);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { container } from '@triptyk/nfw-core';
import 'reflect-metadata';
import { ExecutableGuard } from '../executables/executable-guard.js';
import type { MetadataStorageInterface } from '../interfaces/metadata-storage.js';
import type { ResolverInterface } from '../interfaces/resolver.js';
import type { UseGuardMetadataArgs } from '../storages/metadata/use-guard.js';
import type { ParamsHandleFunction } from '../storages/metadata/use-param.js';
import type { ControllerContext } from '../types/controller-context.js';
import type { ControllerContextType } from '../types/controller-context.js';
import { resolveParams } from '../utils/resolve-params.js';
import { ExecutableGuard } from './executable-guard.js';

export type ResolvedParam = ControllerContext<unknown> | unknown[] | ParamsHandleFunction<any>;

export class GuardResolver {
export class GuardResolver implements ResolverInterface {
public constructor (
public metadataStorage: MetadataStorageInterface,
public controllerContext: ControllerContext
public controllerContext: ControllerContextType
) {}

public resolve (guardUsageMeta: UseGuardMetadataArgs): ExecutableGuard {
public resolve (): ExecutableGuard[] {
const guardUsageMetas = this.metadataStorage.getGuardsForEndpoint(this.controllerContext.controllerInstance.constructor, this.controllerContext.controllerAction);
return guardUsageMetas.map((guardUsageMeta) => this.resolveOneGuard(guardUsageMeta));
}

private resolveOneGuard (guardUsageMeta: UseGuardMetadataArgs) {
const paramsForGuardMetadata = this.metadataStorage.sortedParametersForTarget(guardUsageMeta.guard);
const handles = resolveParams(paramsForGuardMetadata, this.controllerContext);
const guardInstance = container.resolve(guardUsageMeta.guard);
Expand Down
Loading