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

fix: should export ContextDelegation and Context both #7

Merged
merged 1 commit into from
Jun 15, 2024
Merged
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
11 changes: 5 additions & 6 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import onFinished from 'on-finished';
import statuses from 'statuses';
import compose from 'koa-compose';
import { HttpError } from 'http-errors';
import Context from './context.js';
import Request from './request.js';
import Response from './response.js';
import { Context } from './context.js';
import { Request } from './request.js';
import { Response } from './response.js';
import type { ContextDelegation } from './context.js';
import type { CustomError, AnyProto } from './types.js';

Expand All @@ -22,7 +22,6 @@ const debug = debuglog('koa:application');
export type ProtoImplClass<T = object> = new(...args: any[]) => T;
export type Next = () => Promise<void>;
export type MiddlewareFunc = (ctx: ContextDelegation, next: Next) => Promise<void> | void;
export type { ContextDelegation as Context } from './context.js';

/**
* Expose `Application` class.
Expand All @@ -44,7 +43,7 @@ export class Application extends Emitter {
middleware: MiddlewareFunc[];
ctxStorage: AsyncLocalStorage<ContextDelegation>;
silent: boolean;
ContextClass: ProtoImplClass<Context>;
ContextClass: ProtoImplClass<ContextDelegation>;
context: AnyProto;
RequestClass: ProtoImplClass<Request>;
request: AnyProto;
Expand Down Expand Up @@ -82,7 +81,7 @@ export class Application extends Emitter {
this.middleware = [];
this.ctxStorage = getAsyncLocalStorage();
this.silent = false;
this.ContextClass = class ApplicationContext extends Context {};
this.ContextClass = class ApplicationContext extends Context {} as any;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

The extracted methods and properties from the base classes Context, Request, and Response show a comprehensive list of functionalities. However, the extended classes ApplicationContext, ApplicationRequest, and ApplicationResponse do not seem to implement or override these methods and properties, as they are mostly empty or contain minimal logic.

  • ApplicationContext extends Context but does not add any new methods or properties.
  • ApplicationRequest extends Request but also does not add any new methods or properties.
  • ApplicationResponse extends Response and similarly does not add new methods or properties.

Given this information, it appears that the extended classes are not implementing the expected behaviors of their base classes.

  • src/application.ts: Ensure ApplicationContext, ApplicationRequest, and ApplicationResponse implement or override the necessary methods and properties from Context, Request, and Response respectively.
Analysis chain

Ensure the extended classes (ApplicationContext, ApplicationRequest, ApplicationResponse) are properly implementing the expected behaviors of their base classes.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify correct behavior implementation in extended classes.
rg --type ts -A 10 "class ApplicationContext" src/application.ts
rg --type ts -A 10 "class ApplicationRequest" src/application.ts
rg --type ts -A 10 "class ApplicationResponse" src/application.ts

Length of output: 1201



Script:

#!/bin/bash
# Description: Locate the definitions of the base classes and examine their methods and properties.
rg --type ts "class Context" 
rg --type ts "class Request" 
rg --type ts "class Response" 

Length of output: 209



Script:

#!/bin/bash
# Description: Extract methods and properties from the base classes and compare them with the extended classes.

# Extract methods and properties from the base classes
echo "Methods and properties in Context:"
rg --type ts --pcre2 --only-matching -N "(public|protected|private|static)?\s*(async\s*)?(get|set)?\s*\w+\(.*\)\s*{?" src/context.ts
echo "Methods and properties in Request:"
rg --type ts --pcre2 --only-matching -N "(public|protected|private|static)?\s*(async\s*)?(get|set)?\s*\w+\(.*\)\s*{?" src/request.ts
echo "Methods and properties in Response:"
rg --type ts --pcre2 --only-matching -N "(public|protected|private|static)?\s*(async\s*)?(get|set)?\s*\w+\(.*\)\s*{?" src/response.ts

# Compare with the extended classes
echo "Methods and properties in ApplicationContext:"
rg --type ts --pcre2 --only-matching -N "(public|protected|private|static)?\s*(async\s*)?(get|set)?\s*\w+\(.*\)\s*{?" src/application.ts
echo "Methods and properties in ApplicationRequest:"
rg --type ts --pcre2 --only-matching -N "(public|protected|private|static)?\s*(async\s*)?(get|set)?\s*\w+\(.*\)\s*{?" src/application.ts
echo "Methods and properties in ApplicationResponse:"
rg --type ts --pcre2 --only-matching -N "(public|protected|private|static)?\s*(async\s*)?(get|set)?\s*\w+\(.*\)\s*{?" src/application.ts

Length of output: 13463

this.context = this.ContextClass.prototype;
this.RequestClass = class ApplicationRequest extends Request {};
this.request = this.RequestClass.prototype;
Expand Down
8 changes: 4 additions & 4 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import httpAssert from 'http-assert';
import delegate from 'delegates';
import statuses from 'statuses';
import Cookies from 'cookies';
import type Application from './application.js';
import type Request from './request.js';
import type Response from './response.js';
import type { Application } from './application.js';
import type { Request } from './request.js';
import type { Response } from './response.js';
import type { CustomError, AnyProto } from './types.js';

export default class Context {
export class Context {
app: Application;
req: IncomingMessage;
res: ServerResponse;
Expand Down
12 changes: 6 additions & 6 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import contentType from 'content-type';
import parse from 'parseurl';
import typeis from 'type-is';
import fresh from 'fresh';
import type Application from './application.js';
import type Context from './context.js';
import type Response from './response.js';
import type { Application } from './application.js';
import type { ContextDelegation } from './context.js';
import type { Response } from './response.js';

export default class Request {
export class Request {
app: Application;
req: IncomingMessage;
res: ServerResponse;
ctx: Context;
ctx: ContextDelegation;
response: Response;
originalUrl: string;

constructor(app: Application, ctx: Context, req: IncomingMessage, res: ServerResponse) {
constructor(app: Application, ctx: ContextDelegation, req: IncomingMessage, res: ServerResponse) {
this.app = app;
this.req = req;
this.res = res;
Expand Down
6 changes: 3 additions & 3 deletions src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import statuses from 'statuses';
import destroy from 'destroy';
import vary from 'vary';
import encodeUrl from 'encodeurl';
import type Application from './application.js';
import type { Application } from './application.js';
import type { ContextDelegation } from './context.js';
import type Request from './request.js';
import type { Request } from './request.js';

export default class Response {
export class Response {
app: Application;
req: IncomingMessage;
res: ServerResponse;
Expand Down
6 changes: 3 additions & 3 deletions test/application.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expectType } from 'tsd';
import { Context } from '../src/index.js';
import { ContextDelegation } from '../src/index.js';
import Application from '../src/application.js';

const ctx = {} as Context;
const ctx = {} as ContextDelegation;
expectType<string>(ctx.ip);
expectType<Application>(ctx.app);

const app = {} as Application;
expectType<string>(app.env);
expectType<Context | undefined>(app.ctxStorage.getStore());
expectType<ContextDelegation | undefined>(app.ctxStorage.getStore());
Loading