Pārlūkot izejas kodu

Refactor API

Improve API of summarizer.
master
TheoryOfNekomata pirms 1 gada
vecāks
revīzija
66b3f742b1
4 mainītis faili ar 19 papildinājumiem un 22 dzēšanām
  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 Parādīt failu

@@ -6,12 +6,18 @@ export type ProcessEvent = {
contentType?: string, 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 { export interface SummarizerEventEmitter extends NodeJS.EventEmitter {
process(): void;
process<T extends SummarizerProcessParams>(params: T): void;
on(eventType: 'process', callback: ProcessEventCallback): this; on(eventType: 'process', callback: ProcessEventCallback): this;
on(eventType: 'error', callback: ErrorEventCallback): this; on(eventType: 'error', callback: ErrorEventCallback): this;
on(eventType: 'end', callback: () => void): this; on(eventType: 'end', callback: () => void): this;
@@ -25,8 +31,5 @@ export interface OpenAiParams {
} }


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

+ 0
- 6
src/index.ts Parādīt failu

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


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


+ 4
- 7
src/video-types/youtube/index.ts Parādīt failu

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


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


this.emit('process', { this.emit('process', {


+ 6
- 3
test/index.test.ts Parādīt failu

@@ -20,8 +20,9 @@ describe('blah', () => {
it('works', () => new Promise<void>((done) => { it('works', () => new Promise<void>((done) => {
const summarizer = createSummarizer({ const summarizer = createSummarizer({
type: VideoType.YOUTUBE, 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) => { summarizer.on('process', (data) => {
@@ -51,6 +52,8 @@ describe('blah', () => {
done(); done();
}); });


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

Notiek ielāde…
Atcelt
Saglabāt