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

feat: Add new types file and update imports to use new types. #3

Merged
merged 1 commit into from
Jul 20, 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
3 changes: 1 addition & 2 deletions src/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@
import { ValidationError } from './error';
import { Request, Response } from 'express';
import { describe, it, mock } from 'node:test';
import { CacheType, OpenAIServiceType } from './util';
import { CacheType, OpenAIServiceType } from './types';
import {
getDownloadCommitDotShHandler,
getHealthzHandler,
getIndexHandler,
postGenerateCommitMessageHandler,
} from './handler';
import { createSourceMapSource } from 'typescript';

describe('getHealthzHandler', () => {
it('should return ok', () => {
const req = {} as Request;

const status = mock.fn<(status: number) => Response>(() => res);
const json = mock.fn<(body: any) => Response>(() => res);

Check warning on line 19 in src/handler.test.ts

View workflow job for this annotation

GitHub Actions / ESLint (22.x)

Unexpected any. Specify a different type

Check warning on line 19 in src/handler.test.ts

View workflow job for this annotation

GitHub Actions / ESLint (22.x)

Unexpected any. Specify a different type
const res = {
status,
json,
Expand All @@ -41,7 +40,7 @@
const extractDomain = mock.fn<(req: Request) => string>(() => 'http://localhost:3000');

const status = mock.fn<(status: number) => Response>(() => res);
const json = mock.fn<(body: any) => Response>(() => res);

Check warning on line 43 in src/handler.test.ts

View workflow job for this annotation

GitHub Actions / ESLint (22.x)

Unexpected any. Specify a different type

Check warning on line 43 in src/handler.test.ts

View workflow job for this annotation

GitHub Actions / ESLint (22.x)

Unexpected any. Specify a different type
const res = {
status,
json,
Expand Down Expand Up @@ -80,7 +79,7 @@
};

const statusMock = mock.fn<(status: number) => Response>(() => res);
const jsonMock = mock.fn<(body: any) => Response>(() => res);

Check warning on line 82 in src/handler.test.ts

View workflow job for this annotation

GitHub Actions / ESLint (22.x)

Unexpected any. Specify a different type

Check warning on line 82 in src/handler.test.ts

View workflow job for this annotation

GitHub Actions / ESLint (22.x)

Unexpected any. Specify a different type
const res = {
status: statusMock,
json: jsonMock,
Expand Down Expand Up @@ -115,7 +114,7 @@
};

const statusMock = mock.fn<(status: number) => Response>(() => res);
const jsonMock = mock.fn<(body: any) => Response>(() => res);

Check warning on line 117 in src/handler.test.ts

View workflow job for this annotation

GitHub Actions / ESLint (22.x)

Unexpected any. Specify a different type

Check warning on line 117 in src/handler.test.ts

View workflow job for this annotation

GitHub Actions / ESLint (22.x)

Unexpected any. Specify a different type
const res = {
status: statusMock,
json: jsonMock,
Expand Down Expand Up @@ -158,7 +157,7 @@
};

const statusMock = mock.fn<(status: number) => Response>(() => res);
const jsonMock = mock.fn<(body: any) => Response>(() => res);

Check warning on line 160 in src/handler.test.ts

View workflow job for this annotation

GitHub Actions / ESLint (22.x)

Unexpected any. Specify a different type

Check warning on line 160 in src/handler.test.ts

View workflow job for this annotation

GitHub Actions / ESLint (22.x)

Unexpected any. Specify a different type
const res = {
status: statusMock,
json: jsonMock,
Expand Down Expand Up @@ -201,7 +200,7 @@
};

const statusMock = mock.fn<(status: number) => Response>(() => res);
const jsonMock = mock.fn<(body: any) => Response>(() => res);

Check warning on line 203 in src/handler.test.ts

View workflow job for this annotation

GitHub Actions / ESLint (22.x)

Unexpected any. Specify a different type

Check warning on line 203 in src/handler.test.ts

View workflow job for this annotation

GitHub Actions / ESLint (22.x)

Unexpected any. Specify a different type
const res = {
status: statusMock,
json: jsonMock,
Expand Down Expand Up @@ -252,7 +251,7 @@

const setHeaderMock = mock.fn<(name: string, value: string) => Response>(() => res);
const statusMock = mock.fn<(status: number) => Response>(() => res);
const sendMock = mock.fn<(body: any) => Response>(() => res);

Check warning on line 254 in src/handler.test.ts

View workflow job for this annotation

GitHub Actions / ESLint (22.x)

Unexpected any. Specify a different type

Check warning on line 254 in src/handler.test.ts

View workflow job for this annotation

GitHub Actions / ESLint (22.x)

Unexpected any. Specify a different type
const res = {
setHeader: setHeaderMock,
status: statusMock,
Expand Down
11 changes: 3 additions & 8 deletions src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { ValidationError } from './error';
import { Request, Response } from 'express';
import { OpenAIServiceType, CacheType } from './util';

interface GenerateCommitMessageRequest extends Request {
body: {
diff: string;
};
}
import { GenerateCommitMessageRequest, CacheType, OpenAIServiceType } from './types';

export function getHealthzHandler() {
return (req: Request, res: Response) => {
Expand Down Expand Up @@ -49,7 +43,7 @@ export function getIndexHandler(extractDomain: (req: Request) => string, commitD
}

export function postGenerateCommitMessageHandler(OpenAIService: OpenAIServiceType) {
return async (req: Request, res: Response) => {
return async (req: GenerateCommitMessageRequest, res: Response) => {
const { diff } = req.body;

if (!diff || !diff.trim().length) {
Expand All @@ -60,6 +54,7 @@ export function postGenerateCommitMessageHandler(OpenAIService: OpenAIServiceTyp
// Tokens in GPT-3 are more complex and can be part of a word, punctuation, or whitespace.
// For more accurate token counting, consider using a tokenizer library.
const MAX_TOKENS = 16385;

const tokenLength = diff.split(/\s+/).length;

if (tokenLength > MAX_TOKENS) {
Expand Down
32 changes: 32 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { OpenAI } from 'openai';
import { Request } from 'express';

export interface GenerateCommitMessageRequest extends Request {
body: {
diff: string;
};
}

export interface CacheType {
set(key: string, value: string): void;
get(key: string): string | null;
clear(key: string): void;
}

export interface OpenAIServiceType {
openai: OpenAI;
generateCommitMessage(diff: string): Promise<string | null>;
}

export interface ConfigItem<T> {
readonly value: any;
readonly default?: T;
readonly type?: (value: any) => T;
readonly required: boolean;
}

export interface Logger {
debug: (...value: any) => void;
error: (...value: any) => void;
info: (...value: any) => void;
}
21 changes: 2 additions & 19 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import { Request } from 'express';
import { OpenAI } from 'openai';
import { appConfig } from './config';
import { ValidationError } from './error';

export interface CacheType {
set(key: string, value: string): void;
get(key: string): string | null;
clear(key: string): void;
}
import { CacheType, ConfigItem, Logger, OpenAIServiceType } from './types';

function Cache(): CacheType {
const cache: { [key: string]: string | null } = {};
Expand All @@ -27,7 +22,7 @@ function Cache(): CacheType {

export const cache = Cache();

export const logger = {
export const logger: Logger = {
debug: (...value: any) => {
const timestamp = new Date().toLocaleString();
console.debug(`\x1b[33m 🐛 ${timestamp}`, ...value, '\x1b[0m');
Expand All @@ -50,13 +45,6 @@ export function extractDomain(req: Request): string {
return url;
}

export interface ConfigItem<T> {
readonly value: any;
readonly default?: T;
readonly type?: (value: any) => T;
readonly required: boolean;
}

export function validateConfig<T extends Record<string, ConfigItem<any>>>(
config: T,
): Readonly<{ [K in keyof T]: T[K]['type'] extends (value: any) => infer R ? R : T[K]['value'] }> {
Expand Down Expand Up @@ -111,11 +99,6 @@ export function getIpAddress(req: Request): string {
return clientIp;
}

export interface OpenAIServiceType {
openai: OpenAI;
generateCommitMessage(diff: string): Promise<string | null>;
}

/**
* Reference
*
Expand Down
Loading