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

Make sure Nammatham v2 is testable #106

Merged
merged 15 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
apply new rule lint
  • Loading branch information
mildronize committed Jan 11, 2024
commit d81ee043270c89bb870efb28122f512634c445ed
47 changes: 38 additions & 9 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
/* eslint-env node */
module.exports = {
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
root: true,
ignorePatterns: ["**.test.ts"],
rules: {
'@typescript-eslint/consistent-type-imports': 'error',
}
};
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'perfectionist', 'unused-imports'],
root: true,
ignorePatterns: ['**.test.ts'],
rules: {
/**
* Unused import and vars:
* https://github.com/sweepline/eslint-plugin-unused-imports
*/
'unused-imports/no-unused-imports': 'error',
'@typescript-eslint/consistent-type-imports': 'error',
/**
* For config: https://eslint-plugin-perfectionist.azat.io/rules/sort-imports
*/
'perfectionist/sort-imports': [
'error',
{
type: 'line-length',
order: 'asc',
groups: [
'type',
['builtin', 'external'],
'internal-type',
'internal',
['parent-type', 'sibling-type', 'index-type'],
['parent', 'sibling', 'index'],
'side-effect',
'style',
'object',
'unknown',
],
'newlines-between': 'always',
'internal-pattern': ['@/nammatham/**'],
},
],
},
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"@typescript-eslint/parser": "^5.55.0",
"@vitest/coverage-v8": "^1.1.3",
"eslint": "^8.36.0",
"eslint-plugin-perfectionist": "^2.5.0",
"eslint-plugin-unused-imports": "^3.0.0",
"execa": "^8.0.1",
"fs-extra": "^11.2.0",
"nodemon": "^2.0.20",
Expand Down
3 changes: 2 additions & 1 deletion packages/azure-functions/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BaseRuntimeAdapter, NammathamApp } from '@nammatham/core';
import { AzureFunctionsHandlerResolver } from './handler-resolver';

import { AzureFunctionsTrigger } from './trigger';
import { AzureFunctionsHandlerResolver } from './handler-resolver';

export class AzureFunctionsAdapter extends BaseRuntimeAdapter<AzureFunctionsTrigger> {
createTrigger() {
Expand Down
15 changes: 9 additions & 6 deletions packages/azure-functions/src/handler-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import type { NammathamApp, AfterServerStartedMetadata } from '@nammatham/core';
import { BaseHandlerResolver, logger } from '@nammatham/core';
import type { Cookie, LogLevel } from '@azure/functions';
import { HttpResponse, InvocationContext } from '@azure/functions';
import type { AzureFunctionsEndpoint } from './types';
import type { NammathamApp, AfterServerStartedMetadata } from '@nammatham/core';
import type {
Request as ExpressRequest,
Response as ExpressResponse,
CookieOptions as ExpressCookieOptions,
} from 'express';
import { HttpRequest } from './http/HttpRequest';

import { yellow } from 'colorette';
import { v4 as uuidv4 } from 'uuid';
import { BaseHandlerResolver, logger } from '@nammatham/core';
import { HttpResponse, InvocationContext } from '@azure/functions';

import type { AzureFunctionsEndpoint } from './types';

import { HttpRequest } from './http/HttpRequest';
import { printRegisteredFunctions, printRegisteredNonHttpFunctions } from './utils';
import { yellow } from 'colorette';

function logExecutedFunction(
startTime: number,
Expand Down
6 changes: 4 additions & 2 deletions packages/azure-functions/src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { WithEndpointOption } from '@nammatham/core';
import { NammathamContext } from './nammatham-context';
import type { InvocationContext } from '@azure/functions';
import type { WithEndpointOption } from '@nammatham/core';

import type { HandlerFunction, RegisterFunctionOption, AzureFunctionsEndpoint, FunctionOption } from './types';

import { NammathamContext } from './nammatham-context';

export class AzureFunctionsHandler<TTriggerType, TReturnType> {
constructor(
public funcName: string,
Expand Down
8 changes: 5 additions & 3 deletions packages/azure-functions/src/http/HttpRequest.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Adapted from https://github.com/Azure/azure-functions-nodejs-library/blob/v4.x/src/http/HttpRequest.ts
import type * as types from '@azure/functions';
import type { HttpRequestParams, HttpRequestUser } from '@azure/functions';
import type { Blob } from 'buffer';
import type express from 'express';
import type { URLSearchParams } from 'url';
import type { ReadableStream } from 'stream/web';
import type * as types from '@azure/functions';
import type { FormData, Headers } from 'undici';
import type { ReadableStream } from 'stream/web';
import type { HttpRequestParams, HttpRequestUser } from '@azure/functions';

import { logger } from '@nammatham/core';
import { Request as uRequest } from 'undici';

import {
convertExpressQueryToURLSearchParams,
convertExpressReqHeaderToHeadersInit,
Expand Down
2 changes: 1 addition & 1 deletion packages/azure-functions/src/http/http-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IncomingHttpHeaders } from 'node:http';
import type express from 'express';
import type { IncomingHttpHeaders } from 'node:http';

export function convertExpressReqHeaderToHeadersInit(_headers: IncomingHttpHeaders): HeadersInit {
const headers: Record<string, string | ReadonlyArray<string>> = {};
Expand Down
1 change: 1 addition & 0 deletions packages/azure-functions/src/nammatham-context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { InvocationContext } from '@azure/functions';

import { NammathamContextBase } from '@nammatham/core';

export class NammathamContext<TTriggerType> extends NammathamContextBase {
Expand Down
1 change: 0 additions & 1 deletion packages/azure-functions/src/trigger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ test('Trigger HTTP', () => {
const result = trigger.http('test');
expect(result).toBeInstanceOf(AzureFunctionsHandler);
});

9 changes: 6 additions & 3 deletions packages/azure-functions/src/trigger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { HttpEndpointOption, WithEndpointOption } from '@nammatham/core';
import type {
GenericFunctionOptions,
HttpFunctionOptions,
Expand All @@ -8,12 +9,14 @@ import type {
TimerFunctionOptions,
HttpMethodFunctionOptions,
} from '@azure/functions';

import { app } from '@azure/functions';
import { AzureFunctionsHandler } from './handler';
import type { FunctionOption } from './types';
import type { HttpEndpointOption, WithEndpointOption } from '@nammatham/core';
import { BaseFunctionTrigger } from '@nammatham/core';

import type { FunctionOption } from './types';

import { AzureFunctionsHandler } from './handler';

export class AzureFunctionsTrigger extends BaseFunctionTrigger {
generic(funcName: string, option: Omit<GenericFunctionOptions, 'handler'>) {
return new AzureFunctionsHandler<unknown, unknown | void>(
Expand Down
3 changes: 2 additions & 1 deletion packages/azure-functions/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { NammamthamEndpoint, PromiseLike } from '@nammatham/core';
import type { FunctionInput, FunctionOutput, InvocationContext } from '@azure/functions';

import type { NammathamContext } from './nammatham-context';
import type { NammamthamEndpoint, PromiseLike } from '@nammatham/core';

export type HandlerFunction<TTriggerType, TReturnType> = (
ctx: NammathamContext<TTriggerType>
Expand Down
4 changes: 3 additions & 1 deletion packages/azure-functions/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { blue, green, yellow } from 'colorette';
import type { AfterServerStartedMetadata, NammathamApp } from '@nammatham/core';

import { trimSlash } from '@nammatham/core';
import { blue, green, yellow } from 'colorette';

import type { AzureFunctionsEndpoint } from './types';

export function getMethods(func: AzureFunctionsEndpoint<unknown, unknown>): string[] {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/bases/base-handler-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AfterServerStartedMetadata, NammamthamEndpoint } from '../types';
import type { Request, Response } from 'express';

import type { NammathamApp } from '../nammatham-app';
import type { AfterServerStartedMetadata, NammamthamEndpoint } from '../types';

export abstract class BaseHandlerResolver {
abstract resolveHandler(endpoint: NammamthamEndpoint, req: Request, res: Response): any;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/default-adapter/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NammathamApp } from '../nammatham-app';
import { BaseRuntimeAdapter } from '../bases';
import { DefaultTrigger } from './trigger';
import { BaseRuntimeAdapter } from '../bases';
import { NammathamApp } from '../nammatham-app';
import { DefaultHandlerResolver } from './handler-resolver';

export class DefaultAdapter extends BaseRuntimeAdapter<DefaultTrigger> {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/default-adapter/handler-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseHandlerResolver } from '../bases';
import { logger } from '../main';
import { BaseHandlerResolver } from '../bases';

export class DefaultHandlerResolver extends BaseHandlerResolver {
override resolveHandler() {
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/init-nammatham.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { BaseRuntimeAdapter } from './bases/base-runtime-adapter';
import { DefaultAdapter } from './default-adapter';
import { yellow } from 'colorette';

import type { NammathamApp } from './nammatham-app';
import type { BaseRuntimeAdapter } from './bases/base-runtime-adapter';

import { logger } from './main';
import { yellow } from 'colorette';
import { DefaultAdapter } from './default-adapter';

export interface NammathamRuntime<Adapter extends BaseRuntimeAdapter<unknown>> {
func: ReturnType<Adapter['createTrigger']>;
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/nammatham-app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { magenta } from 'colorette';
import { logger } from './main';
import type { BaseHandlerResolver } from './bases';

import type { NammamthamEndpoint } from './types';
import type { BaseHandlerResolver } from './bases';

import { logger } from './main';

export class NammathamApp {
protected readonly _functions: NammamthamEndpoint[] = [];
Expand Down
7 changes: 5 additions & 2 deletions packages/express/src/express-server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import express from 'express';
import { createExpressMiddleware } from './middleware';
import type { BaseHandlerResolver, NammathamApp } from '@nammatham/core';

import express from 'express';
import { logger } from '@nammatham/core';

import type { NammathamHttpHandlerOption } from './types';

import { createExpressMiddleware } from './middleware';

export interface ExpressServerOption {
prefix?: string;
port?: number;
Expand Down
4 changes: 3 additions & 1 deletion packages/express/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Adapted from: https://github.com/trpc/trpc/tree/main/packages/server/src/adapters/express.ts
import type express from 'express';
import type { NammathamHttpHandlerOption } from './types';
import type { HttpMethod } from '@nammatham/core';

import { logger } from '@nammatham/core';

import type { NammathamHttpHandlerOption } from './types';

interface NammathamAppRequestOption extends NammathamHttpHandlerOption {
req: express.Request;
res: express.Response;
Expand Down
14 changes: 8 additions & 6 deletions packages/trpc-azure-functions/src/trpc-azure-functions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { AnyRouter } from '@trpc/server';
import type { BaseHandlerResolver } from '@nammatham/core';
import { NammathamApp, logger, trimSlash } from '@nammatham/core';
import type { ExpressServerOption } from '@nammatham/express';
import type { HttpRequest as AzureHttpRequest } from '@azure/functions';
import type { AzureFunctionsOptions } from 'trpc-azure-functions-adapter';
import { wrapAzureFunctionsRequestHandler } from 'trpc-azure-functions-adapter';
import type { AnyRouter } from '@trpc/server';
import { AzureFunctionsTrigger } from '@nammatham/azure-functions';
import type { ExpressServerOption } from '@nammatham/express';
import { startExpress } from '@nammatham/express';

import express from 'express';
import { startExpress } from '@nammatham/express';
import * as trpcExpress from '@trpc/server/adapters/express';
import { NammathamApp, logger, trimSlash } from '@nammatham/core';
import { AzureFunctionsTrigger } from '@nammatham/azure-functions';
import { wrapAzureFunctionsRequestHandler } from 'trpc-azure-functions-adapter';

import { createContext } from './trpc';

export interface TrpcAzureFunctionsPluginOption<TRouter extends AnyRouter> {
Expand Down
1 change: 0 additions & 1 deletion packages/trpc-azure-functions/src/trpc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { initTRPC } from '@trpc/server';
import type * as trpcExpress from '@trpc/server/adapters/express';

// created for each request
Expand Down
Loading