Преглед на файлове

Refactor API

Improve API of summarizer.
master
TheoryOfNekomata преди 1 година
родител
ревизия
66b3f742b1
променени са 4 файла, в които са добавени 19 реда и са изтрити 22 реда
  1. +9
    -6
      src/common.ts
  2. +0
    -6
      src/index.ts
  3. +4
    -7
      src/video-types/youtube/index.ts
  4. +6
    -3
      test/index.test.ts

+ 9
- 6
src/common.ts Целия файл

@@ -6,12 +6,18 @@ export type ProcessEvent = {
contentType?: string,
};

type ProcessEventCallback = (event: ProcessEvent) => void;
export type ProcessEventCallback = (event: ProcessEvent) => void;

type ErrorEventCallback = (event: Error) => void;
export type ErrorEventCallback = (event: Error) => void;

export interface SummarizerProcessParams {
url: string;
language?: string;
country?: string;
}

export interface SummarizerEventEmitter extends NodeJS.EventEmitter {
process(): void;
process<T extends SummarizerProcessParams>(params: T): void;
on(eventType: 'process', callback: ProcessEventCallback): this;
on(eventType: 'error', callback: ErrorEventCallback): this;
on(eventType: 'end', callback: () => void): this;
@@ -25,8 +31,5 @@ export interface OpenAiParams {
}

export interface CreateBaseSummarizerParams {
url: string;
language?: string;
country?: string;
openAiParams: OpenAiParams;
}

+ 0
- 6
src/index.ts Целия файл

@@ -15,19 +15,13 @@ export interface CreateSummarizerParams extends CreateYouTubeSummarizerParams {
export const createSummarizer = (params: CreateSummarizerParams): SummarizerEventEmitter => {
const {
type: videoType,
url,
openAiParams,
language,
country,
} = params;

switch (videoType as string) {
case VideoType.YOUTUBE:
return new YouTubeSummarizerEventEmitter({
url,
openAiParams,
language,
country,
});
default:
break;


+ 4
- 7
src/video-types/youtube/index.ts Целия файл

@@ -1,5 +1,5 @@
import { EventEmitter } from 'events';
import { CreateBaseSummarizerParams, SummarizerEventEmitter } from '../../common';
import { CreateBaseSummarizerParams, SummarizerEventEmitter, SummarizerProcessParams } from '../../common';
import {
retrieveVideoId,
getVideoPage,
@@ -15,12 +15,9 @@ export class YouTubeSummarizerEventEmitter extends EventEmitter implements Summa
super();
}

process() {
const {
url,
openAiParams,
...config
} = this.params;
process(params: SummarizerProcessParams) {
const { url, ...config } = params;
const { openAiParams } = this.params;
const identifier = retrieveVideoId(url);

this.emit('process', {


+ 6
- 3
test/index.test.ts Целия файл

@@ -20,8 +20,9 @@ describe('blah', () => {
it('works', () => new Promise<void>((done) => {
const summarizer = createSummarizer({
type: VideoType.YOUTUBE,
url: 'https://www.youtube.com/watch?v=WeNgDxtBiyw',
openaiApiKey: process.env.OPENAI_API_KEY as string,
openAiParams: {
apiKey: process.env.OPENAI_API_KEY as string,
},
});

summarizer.on('process', (data) => {
@@ -51,6 +52,8 @@ describe('blah', () => {
done();
});

summarizer.process();
summarizer.process({
url: 'https://www.youtube.com/watch?v=WeNgDxtBiyw',
});
}), { timeout: 180000 });
});

Зареждане…
Отказ
Запис